storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/pkg/bucket/policy/condition/stringequalsignorecasefunc.go (about)

     1  /*
     2   * MinIO Cloud Storage, (C) 2018 MinIO, Inc.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package condition
    18  
    19  import (
    20  	"fmt"
    21  	"net/http"
    22  	"sort"
    23  	"strings"
    24  
    25  	"github.com/minio/minio-go/v7/pkg/set"
    26  )
    27  
    28  func toStringEqualsIgnoreCaseFuncString(n name, key Key, values set.StringSet) string {
    29  	valueStrings := values.ToSlice()
    30  	sort.Strings(valueStrings)
    31  
    32  	return fmt.Sprintf("%v:%v:%v", n, key, valueStrings)
    33  }
    34  
    35  // stringEqualsIgnoreCaseFunc - String equals function. It checks whether value by Key in given
    36  // values map is in condition values.
    37  // For example,
    38  //   - if values = ["mybucket/foo"], at evaluate() it returns whether string
    39  //     in value map for Key is in values.
    40  type stringEqualsIgnoreCaseFunc struct {
    41  	k      Key
    42  	values set.StringSet
    43  }
    44  
    45  // evaluate() - evaluates to check whether value by Key in given values is in
    46  // condition values, ignores case.
    47  func (f stringEqualsIgnoreCaseFunc) evaluate(values map[string][]string) bool {
    48  	requestValue, ok := values[http.CanonicalHeaderKey(f.k.Name())]
    49  	if !ok {
    50  		requestValue = values[f.k.Name()]
    51  	}
    52  
    53  	fvalues := f.values.ApplyFunc(substFuncFromValues(values))
    54  
    55  	for _, v := range requestValue {
    56  		if !fvalues.FuncMatch(strings.EqualFold, v).IsEmpty() {
    57  			return true
    58  		}
    59  	}
    60  
    61  	return false
    62  }
    63  
    64  // key() - returns condition key which is used by this condition function.
    65  func (f stringEqualsIgnoreCaseFunc) key() Key {
    66  	return f.k
    67  }
    68  
    69  // name() - returns "StringEqualsIgnoreCase" condition name.
    70  func (f stringEqualsIgnoreCaseFunc) name() name {
    71  	return stringEqualsIgnoreCase
    72  }
    73  
    74  func (f stringEqualsIgnoreCaseFunc) String() string {
    75  	return toStringEqualsIgnoreCaseFuncString(stringEqualsIgnoreCase, f.k, f.values)
    76  }
    77  
    78  // toMap - returns map representation of this function.
    79  func (f stringEqualsIgnoreCaseFunc) toMap() map[Key]ValueSet {
    80  	if !f.k.IsValid() {
    81  		return nil
    82  	}
    83  
    84  	values := NewValueSet()
    85  	for _, value := range f.values.ToSlice() {
    86  		values.Add(NewStringValue(value))
    87  	}
    88  
    89  	return map[Key]ValueSet{
    90  		f.k: values,
    91  	}
    92  }
    93  
    94  // stringNotEqualsIgnoreCaseFunc - String not equals function. It checks whether value by Key in
    95  // given values is NOT in condition values.
    96  // For example,
    97  //   - if values = ["mybucket/foo"], at evaluate() it returns whether string
    98  //     in value map for Key is NOT in values.
    99  type stringNotEqualsIgnoreCaseFunc struct {
   100  	stringEqualsIgnoreCaseFunc
   101  }
   102  
   103  // evaluate() - evaluates to check whether value by Key in given values is NOT in
   104  // condition values.
   105  func (f stringNotEqualsIgnoreCaseFunc) evaluate(values map[string][]string) bool {
   106  	return !f.stringEqualsIgnoreCaseFunc.evaluate(values)
   107  }
   108  
   109  // name() - returns "StringNotEqualsIgnoreCase" condition name.
   110  func (f stringNotEqualsIgnoreCaseFunc) name() name {
   111  	return stringNotEqualsIgnoreCase
   112  }
   113  
   114  func (f stringNotEqualsIgnoreCaseFunc) String() string {
   115  	return toStringEqualsIgnoreCaseFuncString(stringNotEqualsIgnoreCase, f.stringEqualsIgnoreCaseFunc.k, f.stringEqualsIgnoreCaseFunc.values)
   116  }
   117  
   118  func validateStringEqualsIgnoreCaseValues(n name, key Key, values set.StringSet) error {
   119  	return validateStringEqualsValues(n, key, values)
   120  }
   121  
   122  // newStringEqualsIgnoreCaseFunc - returns new StringEqualsIgnoreCase function.
   123  func newStringEqualsIgnoreCaseFunc(key Key, values ValueSet) (Function, error) {
   124  	valueStrings, err := valuesToStringSlice(stringEqualsIgnoreCase, values)
   125  	if err != nil {
   126  		return nil, err
   127  	}
   128  
   129  	return NewStringEqualsIgnoreCaseFunc(key, valueStrings...)
   130  }
   131  
   132  // NewStringEqualsIgnoreCaseFunc - returns new StringEqualsIgnoreCase function.
   133  func NewStringEqualsIgnoreCaseFunc(key Key, values ...string) (Function, error) {
   134  	sset := set.CreateStringSet(values...)
   135  	if err := validateStringEqualsIgnoreCaseValues(stringEqualsIgnoreCase, key, sset); err != nil {
   136  		return nil, err
   137  	}
   138  
   139  	return &stringEqualsIgnoreCaseFunc{key, sset}, nil
   140  }
   141  
   142  // newStringNotEqualsIgnoreCaseFunc - returns new StringNotEqualsIgnoreCase function.
   143  func newStringNotEqualsIgnoreCaseFunc(key Key, values ValueSet) (Function, error) {
   144  	valueStrings, err := valuesToStringSlice(stringNotEqualsIgnoreCase, values)
   145  	if err != nil {
   146  		return nil, err
   147  	}
   148  
   149  	return NewStringNotEqualsIgnoreCaseFunc(key, valueStrings...)
   150  }
   151  
   152  // NewStringNotEqualsIgnoreCaseFunc - returns new StringNotEqualsIgnoreCase function.
   153  func NewStringNotEqualsIgnoreCaseFunc(key Key, values ...string) (Function, error) {
   154  	sset := set.CreateStringSet(values...)
   155  	if err := validateStringEqualsIgnoreCaseValues(stringNotEqualsIgnoreCase, key, sset); err != nil {
   156  		return nil, err
   157  	}
   158  
   159  	return &stringNotEqualsIgnoreCaseFunc{stringEqualsIgnoreCaseFunc{key, sset}}, nil
   160  }