2021-04-18 15:41:13 -04:00
|
|
|
// Copyright (c) 2015-2021 MinIO, Inc.
|
|
|
|
//
|
|
|
|
// This file is part of MinIO Object Storage stack
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2018-04-24 18:53:30 -04:00
|
|
|
|
|
|
|
package condition
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-12-26 05:03:28 -05:00
|
|
|
"net/http"
|
2018-04-24 18:53:30 -04:00
|
|
|
"sort"
|
|
|
|
|
2020-07-14 12:38:05 -04:00
|
|
|
"github.com/minio/minio-go/v7/pkg/s3utils"
|
|
|
|
"github.com/minio/minio-go/v7/pkg/set"
|
2018-04-24 18:53:30 -04:00
|
|
|
"github.com/minio/minio/pkg/wildcard"
|
|
|
|
)
|
|
|
|
|
|
|
|
func toStringLikeFuncString(n name, key Key, values set.StringSet) string {
|
|
|
|
valueStrings := values.ToSlice()
|
|
|
|
sort.Strings(valueStrings)
|
|
|
|
|
|
|
|
return fmt.Sprintf("%v:%v:%v", n, key, valueStrings)
|
|
|
|
}
|
|
|
|
|
|
|
|
// stringLikeFunc - String like function. It checks whether value by Key in given
|
|
|
|
// values map is widcard matching in condition values.
|
|
|
|
// For example,
|
|
|
|
// - if values = ["mybucket/foo*"], at evaluate() it returns whether string
|
|
|
|
// in value map for Key is wildcard matching in values.
|
|
|
|
type stringLikeFunc struct {
|
|
|
|
k Key
|
|
|
|
values set.StringSet
|
|
|
|
}
|
|
|
|
|
|
|
|
// evaluate() - evaluates to check whether value by Key in given values is wildcard
|
|
|
|
// matching in condition values.
|
|
|
|
func (f stringLikeFunc) evaluate(values map[string][]string) bool {
|
2018-12-26 05:03:28 -05:00
|
|
|
requestValue, ok := values[http.CanonicalHeaderKey(f.k.Name())]
|
|
|
|
if !ok {
|
|
|
|
requestValue = values[f.k.Name()]
|
|
|
|
}
|
|
|
|
|
2019-01-20 23:57:14 -05:00
|
|
|
fvalues := f.values.ApplyFunc(substFuncFromValues(values))
|
|
|
|
|
2018-12-26 05:03:28 -05:00
|
|
|
for _, v := range requestValue {
|
2019-01-20 23:57:14 -05:00
|
|
|
if !fvalues.FuncMatch(wildcard.Match, v).IsEmpty() {
|
2018-04-24 18:53:30 -04:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// key() - returns condition key which is used by this condition function.
|
|
|
|
func (f stringLikeFunc) key() Key {
|
|
|
|
return f.k
|
|
|
|
}
|
|
|
|
|
|
|
|
// name() - returns "StringLike" function name.
|
|
|
|
func (f stringLikeFunc) name() name {
|
|
|
|
return stringLike
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f stringLikeFunc) String() string {
|
|
|
|
return toStringLikeFuncString(stringLike, f.k, f.values)
|
|
|
|
}
|
|
|
|
|
|
|
|
// toMap - returns map representation of this function.
|
|
|
|
func (f stringLikeFunc) toMap() map[Key]ValueSet {
|
|
|
|
if !f.k.IsValid() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
values := NewValueSet()
|
|
|
|
for _, value := range f.values.ToSlice() {
|
|
|
|
values.Add(NewStringValue(value))
|
|
|
|
}
|
|
|
|
|
|
|
|
return map[Key]ValueSet{
|
|
|
|
f.k: values,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// stringNotLikeFunc - String not like function. It checks whether value by Key in given
|
|
|
|
// values map is NOT widcard matching in condition values.
|
|
|
|
// For example,
|
|
|
|
// - if values = ["mybucket/foo*"], at evaluate() it returns whether string
|
|
|
|
// in value map for Key is NOT wildcard matching in values.
|
|
|
|
type stringNotLikeFunc struct {
|
|
|
|
stringLikeFunc
|
|
|
|
}
|
|
|
|
|
|
|
|
// evaluate() - evaluates to check whether value by Key in given values is NOT wildcard
|
|
|
|
// matching in condition values.
|
|
|
|
func (f stringNotLikeFunc) evaluate(values map[string][]string) bool {
|
|
|
|
return !f.stringLikeFunc.evaluate(values)
|
|
|
|
}
|
|
|
|
|
|
|
|
// name() - returns "StringNotLike" function name.
|
|
|
|
func (f stringNotLikeFunc) name() name {
|
|
|
|
return stringNotLike
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f stringNotLikeFunc) String() string {
|
|
|
|
return toStringLikeFuncString(stringNotLike, f.stringLikeFunc.k, f.stringLikeFunc.values)
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateStringLikeValues(n name, key Key, values set.StringSet) error {
|
|
|
|
for _, s := range values.ToSlice() {
|
|
|
|
switch key {
|
|
|
|
case S3XAmzCopySource:
|
2018-12-26 20:39:30 -05:00
|
|
|
bucket, object := path2BucketAndObject(s)
|
|
|
|
if object == "" {
|
|
|
|
return fmt.Errorf("invalid value '%v' for '%v' for %v condition", s, S3XAmzCopySource, n)
|
|
|
|
}
|
|
|
|
if err := s3utils.CheckValidBucketName(bucket); err != nil {
|
|
|
|
return err
|
2018-04-24 18:53:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// newStringLikeFunc - returns new StringLike function.
|
|
|
|
func newStringLikeFunc(key Key, values ValueSet) (Function, error) {
|
|
|
|
valueStrings, err := valuesToStringSlice(stringLike, values)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return NewStringLikeFunc(key, valueStrings...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewStringLikeFunc - returns new StringLike function.
|
|
|
|
func NewStringLikeFunc(key Key, values ...string) (Function, error) {
|
|
|
|
sset := set.CreateStringSet(values...)
|
|
|
|
if err := validateStringLikeValues(stringLike, key, sset); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &stringLikeFunc{key, sset}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// newStringNotLikeFunc - returns new StringNotLike function.
|
|
|
|
func newStringNotLikeFunc(key Key, values ValueSet) (Function, error) {
|
|
|
|
valueStrings, err := valuesToStringSlice(stringNotLike, values)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return NewStringNotLikeFunc(key, valueStrings...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewStringNotLikeFunc - returns new StringNotLike function.
|
|
|
|
func NewStringNotLikeFunc(key Key, values ...string) (Function, error) {
|
|
|
|
sset := set.CreateStringSet(values...)
|
|
|
|
if err := validateStringLikeValues(stringNotLike, key, sset); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &stringNotLikeFunc{stringLikeFunc{key, sset}}, nil
|
|
|
|
}
|