@@ -27,22 +27,24 @@ import (
2727
2828 "github.com/minio/minio-go/v7/pkg/set"
2929 "github.com/minio/minio-go/v7/pkg/tags"
30+ "github.com/minio/minio/internal/arn"
3031 "github.com/minio/minio/internal/auth"
3132 "github.com/minio/minio/internal/logger"
3233 "github.com/pgsty/silo-pkg/v3/policy"
3334 "github.com/pgsty/silo-pkg/v3/policy/condition"
35+ "github.com/puzpuzpuz/xsync/v3"
3436)
3537
36- // authorizeRequestWithUnreadTags authorizes action on an object whose stored
37- // tags could not be read because reading the object failed with readErr .
38+ // authorizeRequestWithUnreadTags decides whether to disclose readErr for an
39+ // object whose stored tags could not be read. It must not grant object access .
3840//
3941// Without the tags, an Allow statement that tests s3:ExistingObjectTag cannot
4042// match, so a caller allowed only by such a statement would get AccessDenied,
4143// which clients do not retry, for a transient server error such as lost read
42- // quorum. For a server error the request is therefore also authorized as if
43- // the object carried tags that the policies ask for: a caller that some tags
44- // let through gets the error, a caller that no tags let through still gets
45- // AccessDenied . For any other error, NoSuchKey among them, the tags stay empty.
44+ // quorum. For a server error, try a bounded set of tags from the caller's
45+ // policies and return the storage error if normal authorization accepts one.
46+ // If no candidate passes, keep AccessDenied. This is not an exhaustive policy
47+ // solver . For any other error, NoSuchKey among them, the tags stay empty.
4648func authorizeRequestWithUnreadTags (ctx context.Context , r * http.Request , action policy.Action , readErr error ) APIErrorCode {
4749 s3Err := authorizeRequest (ctx , r , action )
4850 if s3Err == ErrNone || toAPIError (ctx , readErr ).HTTPStatusCode < http .StatusInternalServerError {
@@ -76,17 +78,16 @@ func policyExistingObjectTags(ctx context.Context, r *http.Request, action polic
7678 bucket , object := reqInfo .BucketName , reqInfo .ObjectName
7779 var (
7880 allows [][]map [string ]string // per policy set, the tags its Allow statements ask for
79- denies []map [string ]string // tags that keep a Deny statement from matching
81+ denies [][] map [string ]string // per Deny statement, alternative tags that keep it from matching
8082 )
81- // add files the tags of a statement that applies to action on the
83+ // add collects the tags of a statement that applies to action on the
8284 // object, whatever its conditions.
8385 add := func (effect policy.Effect , conditions condition.Functions , values map [string ][]string , fromSet * []map [string ]string ) {
84- if tagMap , ok := existingObjectTagsFor (effect , conditions , values ); ok {
85- if effect == policy .Allow {
86- * fromSet = append (* fromSet , tagMap )
87- } else {
88- denies = append (denies , tagMap )
89- }
86+ candidates := existingObjectTagsCandidatesFor (effect , conditions , values )
87+ if effect == policy .Allow {
88+ * fromSet = append (* fromSet , candidates ... )
89+ } else if len (candidates ) > 0 {
90+ denies = append (denies , candidates )
9091 }
9192 }
9293 switch {
@@ -107,17 +108,7 @@ func policyExistingObjectTags(ctx context.Context, r *http.Request, action polic
107108 allows = append (allows , fromSet )
108109 case newGlobalAuthZPluginFn () == nil && globalIAMSys .Initialized ():
109110 values := getConditionValues (r , "" , reqInfo .Cred )
110- // From the cache only: reading the drives may be what just failed.
111- docs , _ := globalIAMSys .store .listPolicyDocs (ctx , "" )
112- policySets := [][]policy.Policy {nil }
113- for _ , name := range slices .Sorted (maps .Keys (docs )) {
114- policySets [0 ] = append (policySets [0 ], docs [name ].Policy )
115- }
116- if sp , ok := reqInfo .Cred .Claims [sessionPolicyNameExtracted ].(string ); ok {
117- if p , err := policy .ParseConfig (strings .NewReader (sp )); err == nil {
118- policySets = append (policySets , []policy.Policy {* p })
119- }
120- }
111+ policySets := globalIAMSys .unreadTagPolicySets (reqInfo .Cred )
121112 for _ , policies := range policySets {
122113 var fromSet []map [string ]string
123114 for _ , p := range policies {
@@ -132,72 +123,131 @@ func policyExistingObjectTags(ctx context.Context, r *http.Request, action polic
132123 allows = append (allows , fromSet )
133124 }
134125 }
135- return combineExistingObjectTags (allows , denies )
126+ return combineExistingObjectTags (append ( allows , denies ... ) )
136127}
137128
138- // combineExistingObjectTags merges the tags of one Allow statement of each
139- // policy set, or of none for a set that may allow without tags, and tries each
140- // result also with the tags that keep the Deny statements from matching. The
141- // first tags win a conflict; the authorization of the result settles it.
142- func combineExistingObjectTags (allows [][]map [string ]string , denies []map [string ]string ) []string {
143- merge := func (parts ... map [string ]string ) map [string ]string {
144- merged := make (map [string ]string )
145- for _ , part := range parts {
146- for k , v := range part {
147- if _ , ok := merged [k ]; ! ok {
148- merged [k ] = v
149- }
129+ // unreadTagMappings reuses the normal user/group mapping lookup without lazy
130+ // storage reads. Missing cache entries contribute no candidates; the regular
131+ // authorization still decides whether any candidate is allowed.
132+ type unreadTagMappings struct {
133+ * IAMStoreSys
134+ }
135+
136+ func (unreadTagMappings ) loadMappedPolicy (context.Context , string , IAMUserType , bool , * xsync.MapOf [string , MappedPolicy ]) error {
137+ return errNoSuchPolicy
138+ }
139+
140+ func (unreadTagMappings ) loadGroup (context.Context , string , map [string ]GroupInfo ) error {
141+ return errNoSuchGroup
142+ }
143+
144+ // unreadTagPolicySets selects only this credential's cached policies. Base
145+ // and session policies remain separate because both must allow the request.
146+ // This only supplies candidates, so identity, claims and policy validity are
147+ // left to the normal authorization on each candidate.
148+ func (sys * IAMSys ) unreadTagPolicySets (cred auth.Credentials ) [][]policy.Policy {
149+ derived := cred .IsTemp () || cred .IsServiceAccount ()
150+ name := cred .AccessKey
151+ if derived {
152+ name = cred .ParentUser
153+ }
154+ cache := sys .store .rlock ()
155+ defer sys .store .runlock ()
156+ var names []string
157+ roleARN := (policy.Args {Claims : cred .Claims }).GetRoleArn ()
158+ switch {
159+ case name == globalActiveCred .AccessKey :
160+ case derived && roleARN != "" :
161+ parsed , err := arn .Parse (roleARN )
162+ if err != nil {
163+ return nil
164+ }
165+ names = newMappedPolicy (sys .rolesMap [parsed ]).toSlice ()
166+ default :
167+ loader := unreadTagMappings {sys .store }
168+ var err error
169+ names , _ , err = cache .policyDBGet (loader , name , false , false )
170+ if err != nil {
171+ return nil
172+ }
173+ groups , err := cache .policyDBGetGroups (loader , len (names ) > 0 , cred .Groups ... )
174+ if err != nil {
175+ return nil
176+ }
177+ names = append (names , groups ... )
178+ if derived && len (names ) == 0 {
179+ claims , _ := policy .GetPoliciesFromClaims (cred .Claims , iamPolicyClaimNameOpenID ())
180+ names = claims .ToSlice ()
181+ }
182+ }
183+
184+ sets := [][]policy.Policy {nil }
185+ slices .Sort (names )
186+ for _ , name := range slices .Compact (names ) {
187+ if doc , ok := cache .iamPolicyDocsMap [name ]; ok {
188+ sets [0 ] = append (sets [0 ], doc .Policy )
189+ }
190+ }
191+ if derived && (cred .IsTemp () || cred .Claims [iamPolicyClaimNameSA ()] != inheritedPolicyType ) {
192+ if sp , ok := cred .Claims [sessionPolicyNameExtracted ].(string ); ok {
193+ if p , err := policy .ParseConfig (strings .NewReader (sp )); err == nil {
194+ sets = append (sets , []policy.Policy {* p })
150195 }
151196 }
152- return merged
153197 }
154- combos := [][]map [string ]string {nil }
155- for _ , fromSet := range allows {
156- // Many statements ask for the same tags.
198+ return sets
199+ }
200+
201+ // combineExistingObjectTags combines alternatives from each base/session
202+ // Allow set and each tag-dependent Deny. A set may also contribute no tags:
203+ // another statement or another set may already supply what it needs. First
204+ // values win conflicts; the normal authorization decides whether they work.
205+ func combineExistingObjectTags (sets [][]map [string ]string ) []string {
206+ combos := []map [string ]string {{}}
207+ for _ , fromSet := range sets {
208+ fromSet = append (uniqueExistingObjectTagMaps (fromSet ), nil )
209+ var next []map [string ]string
157210 seen := set .NewStringSet ()
158- fromSet = slices .DeleteFunc (slices .Clone (fromSet ), func (tagMap map [string ]string ) bool {
159- t , err := tags .MapToObjectTags (tagMap )
160- if err != nil || seen .Contains (t .String ()) {
161- return true
162- }
163- seen .Add (t .String ())
164- return false
165- })
166- next := make ([][]map [string ]string , 0 , len (combos )* (len (fromSet )+ 1 ))
211+ combinations:
167212 for _ , combo := range combos {
168- next = append (next , combo )
169- for _ , tagMap := range fromSet {
170- next = append (next , append (slices .Clip (combo ), tagMap ))
213+ for _ , addition := range fromSet {
214+ merged := maps .Clone (combo )
215+ for k , v := range addition {
216+ if _ , ok := merged [k ]; ! ok {
217+ merged [k ] = v
218+ }
219+ }
220+ t , err := tags .MapToObjectTags (merged )
221+ if err != nil || seen .Contains (t .String ()) {
222+ continue
223+ }
224+ seen .Add (t .String ())
225+ next = append (next , merged )
226+ if len (next ) == maxExistingTagCandidates {
227+ break combinations
228+ }
171229 }
172230 }
173- combos = next [: min ( len ( next ), maxExistingTagCandidates )]
231+ combos = next
174232 }
175- deny := merge (denies ... )
176- found := set .NewStringSet ()
177233 var candidates []string
178234 for _ , combo := range combos {
179- for _ , tagMap := range []map [string ]string {merge (combo ... ), merge (append (slices .Clip (combo ), deny )... )} {
180- t , err := tags .MapToObjectTags (tagMap )
181- if len (tagMap ) == 0 || err != nil || found .Contains (t .String ()) {
182- continue
183- }
184- found .Add (t .String ())
185- if candidates = append (candidates , t .String ()); len (candidates ) == maxExistingTagCandidates {
186- return candidates
187- }
235+ if len (combo ) != 0 { // empty tags were already authorized
236+ t , _ := tags .MapToObjectTags (combo )
237+ candidates = append (candidates , t .String ())
188238 }
189239 }
190240 return candidates
191241}
192242
193- // existingObjectTagsFor returns tags under which the s3:ExistingObjectTag tests
194- // of conditions all pass, for an Allow statement, or one of them fails, for a
195- // Deny statement; a tag left out is absent. It returns false if the conditions
196- // test no tag, or if no such tags were found among those tried for each tag:
197- // none, the values the tests name, with policy variables taken from values and
198- // wildcards filled, and the fillers "" and "_". The policy engine itself
199- // evaluates the tests for each of them .
200- func existingObjectTagsFor (effect policy.Effect , conditions condition.Functions , values map [string ][]string ) ( map [string ]string , bool ) {
243+ // existingObjectTagsCandidatesFor returns tag sets under which the
244+ // s3:ExistingObjectTag tests of conditions all pass, for an Allow statement,
245+ // or one of them fails, for a Deny statement; a tag left out is absent. For an
246+ // Allow, values for different tag keys are combined, while a Deny needs only
247+ // one failing key. It tries the values named in the conditions, with policy
248+ // variables resolved, wildcard fillers, and absence, then lets the policy
249+ // engine decide whether each candidate really satisfies the conditions .
250+ func existingObjectTagsCandidatesFor (effect policy.Effect , conditions condition.Functions , values map [string ][]string ) [] map [string ]string {
201251 // Each function tests one key, so the tests of one tag pass or fail
202252 // whatever the other tags are.
203253 byKey := make (map [string ]condition.Functions )
@@ -209,47 +259,74 @@ func existingObjectTagsFor(effect policy.Effect, conditions condition.Functions,
209259 }
210260 }
211261 if len (byKey ) == 0 {
212- return nil , false
262+ return nil
213263 }
214- tagMap := make (map [string ]string )
215- for _ , key := range slices .Sorted (maps .Keys (byKey )) {
216- tagKey := strings .TrimPrefix (key , condition .ExistingObjectTag .Name ()+ "/" )
217- value , present , found := existingObjectTagValue (key , tagKey , byKey [key ], values , effect == policy .Allow )
218- switch {
219- case effect == policy .Deny && found :
220- if present {
221- tagMap [tagKey ] = value
264+
265+ keys := slices .Sorted (maps .Keys (byKey ))
266+ if effect == policy .Deny {
267+ var candidates []map [string ]string
268+ for _ , key := range keys {
269+ candidates = append (candidates , existingObjectTagValues (key , strings .TrimPrefix (key , condition .ExistingObjectTag .Name ()+ "/" ), byKey [key ], values , false )... )
270+ }
271+ return uniqueExistingObjectTagMaps (candidates )
272+ }
273+
274+ candidates := []map [string ]string {{}}
275+ for _ , key := range keys {
276+ keyCandidates := existingObjectTagValues (key , strings .TrimPrefix (key , condition .ExistingObjectTag .Name ()+ "/" ), byKey [key ], values , true )
277+ if len (keyCandidates ) == 0 {
278+ return nil
279+ }
280+ next := make ([]map [string ]string , 0 , min (maxExistingTagCandidates , len (candidates )* len (keyCandidates )))
281+ for _ , base := range candidates {
282+ for _ , keyCandidate := range keyCandidates {
283+ merged := maps .Clone (base )
284+ maps .Copy (merged , keyCandidate )
285+ next = append (next , merged )
286+ if len (next ) == maxExistingTagCandidates {
287+ break
288+ }
289+ }
290+ if len (next ) == maxExistingTagCandidates {
291+ break
222292 }
223- return tagMap , true
224- case effect == policy .Allow && ! found :
225- return nil , false
226- case present :
227- tagMap [tagKey ] = value
228293 }
294+ candidates = next
229295 }
230- if effect == policy .Deny {
231- return nil , false
296+ return uniqueExistingObjectTagMaps (candidates )
297+ }
298+
299+ func uniqueExistingObjectTagMaps (candidates []map [string ]string ) []map [string ]string {
300+ seen := set .NewStringSet ()
301+ unique := make ([]map [string ]string , 0 , len (candidates ))
302+ for _ , candidate := range candidates {
303+ t , err := tags .MapToObjectTags (candidate )
304+ if err != nil || seen .Contains (t .String ()) {
305+ continue
306+ }
307+ seen .Add (t .String ())
308+ unique = append (unique , candidate )
232309 }
233- return tagMap , true
310+ return unique
234311}
235312
236- // existingObjectTagValue looks for a value of tag tagKey, or its absence,
237- // under which tests, the functions that test the condition key key, evaluate
238- // to want.
239- func existingObjectTagValue (key , tagKey string , tests condition.Functions , values map [string ][]string , want bool ) (value string , present , found bool ) {
313+ // existingObjectTagValues looks for values of tag tagKey, or its absence,
314+ // under which the functions that test the condition key key evaluate to want.
315+ func existingObjectTagValues (key , tagKey string , tests condition.Functions , values map [string ][]string , want bool ) []map [string ]string {
240316 defer delete (values , key )
241317 delete (values , key )
318+ var candidates []map [string ]string
242319 if tests .Evaluate (values ) == want {
243- return "" , false , true
320+ candidates = append ( candidates , map [ string ] string {})
244321 }
245322 // The functions keep their values unexported; JSON exposes them.
246323 buf , err := json .Marshal (tests )
247324 if err != nil {
248- return "" , false , false
325+ return candidates
249326 }
250327 var byName map [string ]map [string ]condition.ValueSet
251328 if err = json .Unmarshal (buf , & byName ); err != nil {
252- return "" , false , false
329+ return candidates
253330 }
254331 var tried []string
255332 for name , byKey := range byName {
@@ -270,10 +347,13 @@ func existingObjectTagValue(key, tagKey string, tests condition.Functions, value
270347 }
271348 values [key ] = []string {v }
272349 if tests .Evaluate (values ) == want {
273- return v , true , true
350+ candidates = append (candidates , map [string ]string {tagKey : v })
351+ if len (candidates ) == maxExistingTagCandidates {
352+ break
353+ }
274354 }
275355 }
276- return "" , false , false
356+ return uniqueExistingObjectTagMaps ( candidates )
277357}
278358
279359// substituteConditionVariables resolves the policy variables in v from the
0 commit comments