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

     1  /*
     2   * MinIO Cloud Storage, (C) 2020 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  	"time"
    23  )
    24  
    25  func toDateLessThanFuncString(n name, key Key, value time.Time) string {
    26  	return fmt.Sprintf("%v:%v:%v", n, key, value.Format(time.RFC3339))
    27  }
    28  
    29  // dateLessThanFunc - String equals function. It checks whether value by Key in given
    30  // values map is in condition values.
    31  // For example,
    32  //   - if values = ["mybucket/foo"], at evaluate() it returns whether string
    33  //     in value map for Key is in values.
    34  type dateLessThanFunc struct {
    35  	k     Key
    36  	value time.Time
    37  }
    38  
    39  // evaluate() - evaluates to check whether value by Key in given values is in
    40  // condition values.
    41  func (f dateLessThanFunc) evaluate(values map[string][]string) bool {
    42  	requestValue, ok := values[http.CanonicalHeaderKey(f.k.Name())]
    43  	if !ok {
    44  		requestValue = values[f.k.Name()]
    45  	}
    46  
    47  	if len(requestValue) == 0 {
    48  		return false
    49  	}
    50  
    51  	t, err := time.Parse(time.RFC3339, requestValue[0])
    52  	if err != nil {
    53  		return false
    54  	}
    55  
    56  	return t.Before(f.value)
    57  }
    58  
    59  // key() - returns condition key which is used by this condition function.
    60  func (f dateLessThanFunc) key() Key {
    61  	return f.k
    62  }
    63  
    64  // name() - returns "DateLessThan" condition name.
    65  func (f dateLessThanFunc) name() name {
    66  	return dateLessThan
    67  }
    68  
    69  func (f dateLessThanFunc) String() string {
    70  	return toDateLessThanFuncString(dateLessThan, f.k, f.value)
    71  }
    72  
    73  // toMap - returns map representation of this function.
    74  func (f dateLessThanFunc) toMap() map[Key]ValueSet {
    75  	if !f.k.IsValid() {
    76  		return nil
    77  	}
    78  
    79  	values := NewValueSet()
    80  	values.Add(NewStringValue(f.value.Format(time.RFC3339)))
    81  
    82  	return map[Key]ValueSet{
    83  		f.k: values,
    84  	}
    85  }
    86  
    87  // dateNotEqualsFunc - String not equals function. It checks whether value by Key in
    88  // given values is NOT in condition values.
    89  // For example,
    90  //   - if values = ["mybucket/foo"], at evaluate() it returns whether string
    91  //     in value map for Key is NOT in values.
    92  type dateLessThanEqualsFunc struct {
    93  	dateLessThanFunc
    94  }
    95  
    96  // evaluate() - evaluates to check whether value by Key in given values is NOT in
    97  // condition values.
    98  func (f dateLessThanEqualsFunc) evaluate(values map[string][]string) bool {
    99  	requestValue, ok := values[http.CanonicalHeaderKey(f.k.Name())]
   100  	if !ok {
   101  		requestValue = values[f.k.Name()]
   102  	}
   103  
   104  	if len(requestValue) == 0 {
   105  		return false
   106  	}
   107  
   108  	t, err := time.Parse(time.RFC3339, requestValue[0])
   109  	if err != nil {
   110  		return false
   111  	}
   112  
   113  	return t.Before(f.value) || t.Equal(f.value)
   114  }
   115  
   116  // name() - returns "DateNotEquals" condition name.
   117  func (f dateLessThanEqualsFunc) name() name {
   118  	return dateLessThanEquals
   119  }
   120  
   121  func (f dateLessThanEqualsFunc) String() string {
   122  	return toDateLessThanFuncString(dateNotEquals, f.dateLessThanFunc.k, f.dateLessThanFunc.value)
   123  }
   124  
   125  // newDateLessThanFunc - returns new DateLessThan function.
   126  func newDateLessThanFunc(key Key, values ValueSet) (Function, error) {
   127  	v, err := valueToTime(dateLessThan, values)
   128  	if err != nil {
   129  		return nil, err
   130  	}
   131  
   132  	return NewDateLessThanFunc(key, v)
   133  }
   134  
   135  // NewDateLessThanFunc - returns new DateLessThan function.
   136  func NewDateLessThanFunc(key Key, value time.Time) (Function, error) {
   137  	return &dateLessThanFunc{key, value}, nil
   138  }
   139  
   140  // newDateNotEqualsFunc - returns new DateNotEquals function.
   141  func newDateLessThanEqualsFunc(key Key, values ValueSet) (Function, error) {
   142  	v, err := valueToTime(dateNotEquals, values)
   143  	if err != nil {
   144  		return nil, err
   145  	}
   146  
   147  	return NewDateLessThanEqualsFunc(key, v)
   148  }
   149  
   150  // NewDateLessThanEqualsFunc - returns new DateNotEquals function.
   151  func NewDateLessThanEqualsFunc(key Key, value time.Time) (Function, error) {
   152  	return &dateLessThanEqualsFunc{dateLessThanFunc{key, value}}, nil
   153  }