storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/pkg/bucket/policy/principal.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 policy 18 19 import ( 20 "encoding/json" 21 22 "github.com/minio/minio-go/v7/pkg/set" 23 24 "storj.io/minio/pkg/wildcard" 25 ) 26 27 // Principal - policy principal. 28 type Principal struct { 29 AWS set.StringSet 30 } 31 32 // IsValid - checks whether Principal is valid or not. 33 func (p Principal) IsValid() bool { 34 return len(p.AWS) != 0 35 } 36 37 // Equals - returns true if principals are equal. 38 func (p Principal) Equals(pp Principal) bool { 39 return p.AWS.Equals(pp.AWS) 40 } 41 42 // Intersection - returns principals available in both Principal. 43 func (p Principal) Intersection(principal Principal) set.StringSet { 44 return p.AWS.Intersection(principal.AWS) 45 } 46 47 // MarshalJSON - encodes Principal to JSON data. 48 func (p Principal) MarshalJSON() ([]byte, error) { 49 if !p.IsValid() { 50 return nil, Errorf("invalid principal %v", p) 51 } 52 53 // subtype to avoid recursive call to MarshalJSON() 54 type subPrincipal Principal 55 sp := subPrincipal(p) 56 return json.Marshal(sp) 57 } 58 59 // Match - matches given principal is wildcard matching with Principal. 60 func (p Principal) Match(principal string) bool { 61 for _, pattern := range p.AWS.ToSlice() { 62 if wildcard.MatchSimple(pattern, principal) { 63 return true 64 } 65 } 66 67 return false 68 } 69 70 // UnmarshalJSON - decodes JSON data to Principal. 71 func (p *Principal) UnmarshalJSON(data []byte) error { 72 // subtype to avoid recursive call to UnmarshalJSON() 73 type subPrincipal Principal 74 var sp subPrincipal 75 76 if err := json.Unmarshal(data, &sp); err != nil { 77 var s string 78 if err = json.Unmarshal(data, &s); err != nil { 79 return err 80 } 81 82 if s != "*" { 83 return Errorf("invalid principal '%v'", s) 84 } 85 86 sp.AWS = set.CreateStringSet("*") 87 } 88 89 *p = Principal(sp) 90 91 return nil 92 } 93 94 // Clone clones Principal structure 95 func (p Principal) Clone() Principal { 96 return NewPrincipal(p.AWS.ToSlice()...) 97 98 } 99 100 // NewPrincipal - creates new Principal. 101 func NewPrincipal(principals ...string) Principal { 102 return Principal{AWS: set.CreateStringSet(principals...)} 103 }