mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-05 10:37:23 +01:00
s3: keep a bucket policy change from aborting its own request (#13034)
* s3: keep a bucket policy change from aborting its own request The bucket metadata event re-checks every request in flight on the bucket, including the PutBucketPolicy, DeleteBucketPolicy or DeleteBucket request that made the change while it is still answering. A deny-all policy, or the removal of the policy that granted the requester access, then closed that request's connection after the change was stored; the client saw a reset and its retry was denied. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * s3: keep re-checking a policy change until it stores it Exempting the policy-changing actions at admission let a request whose key was revoked while it read its body still replace or delete the policy. The request stays tracked now and untracks itself right before it stores its change; if it was aborted by then, it stores nothing. DeleteBucket untracks itself before the delete, which also covers the identity re-check its bucket-scoped action pruning triggers. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
01b88ad2f9
commit
7738ea7f45
4 files changed
+69
-2
No files matched your search
@@ -121,6 +121,7 @@ func (iam *IdentityAccessManagement) abortWhenRevoked(f http.HandlerFunc, identi
|
||||
req.body = r.Body
|
||||
req.stream = http.NewResponseController(w)
|
||||
}
|
||||
r = r.WithContext(context.WithValue(ctx, inFlightRequestKey{}, req))
|
||||
tracked.add(req)
|
||||
defer tracked.remove(req)
|
||||
if tracked.generation.Load() != since {
|
||||
@@ -130,6 +131,20 @@ func (iam *IdentityAccessManagement) abortWhenRevoked(f http.HandlerFunc, identi
|
||||
}
|
||||
}
|
||||
|
||||
type inFlightRequestKey struct{}
|
||||
|
||||
// untrackInFlight stops re-checking r before it stores a change to access,
|
||||
// which would otherwise judge r by its own change and abort it after the
|
||||
// change is stored. It reports false when r was aborted already, so the change
|
||||
// must not be stored.
|
||||
func (iam *IdentityAccessManagement) untrackInFlight(r *http.Request) bool {
|
||||
req, ok := r.Context().Value(inFlightRequestKey{}).(*inFlightRequest)
|
||||
if !ok || iam == nil || iam.inFlight == nil {
|
||||
return true
|
||||
}
|
||||
return iam.inFlight.remove(req)
|
||||
}
|
||||
|
||||
// identitySource tells from how the request authenticated how a re-check finds
|
||||
// its identity again, and the credential it was signed with.
|
||||
func (iam *IdentityAccessManagement) identitySource(r *http.Request, identity *Identity) (identitySource, *Credential) {
|
||||
@@ -251,10 +266,13 @@ func (t *inFlightRequests) add(req *inFlightRequest) {
|
||||
t.mu.Unlock()
|
||||
}
|
||||
|
||||
func (t *inFlightRequests) remove(req *inFlightRequest) {
|
||||
// remove reports whether req was still tracked, that is not aborted.
|
||||
func (t *inFlightRequests) remove(req *inFlightRequest) bool {
|
||||
t.mu.Lock()
|
||||
defer t.mu.Unlock()
|
||||
_, tracked := t.requests[req]
|
||||
delete(t.requests, req)
|
||||
t.mu.Unlock()
|
||||
return tracked
|
||||
}
|
||||
|
||||
// pending takes the requests waiting for a re-check.
|
||||
|
||||
@@ -286,6 +286,46 @@ func TestAbortRequestsDeniedByBucketPolicy(t *testing.T) {
|
||||
assert.NoError(t, downloadResult(t, other, 5*time.Second), "no policy covers other")
|
||||
}
|
||||
|
||||
// A request that untracks itself before storing its change to access keeps
|
||||
// running when that change denies it, while a read the change denies is
|
||||
// aborted. A request aborted before it untracks itself must not store it.
|
||||
func TestUntrackInFlightBeforeOwnChange(t *testing.T) {
|
||||
iam := newRevokeIAM(t, revokeIdentity("bob", revokeKeyB, revokeSecretB, "Admin"))
|
||||
iam.policyEngine = NewBucketPolicyEngine()
|
||||
started := make(chan struct{}, 2)
|
||||
read := download(t, serveRevokeS3(t, started, authRead(iam))+"/bkt/object", revokeKeyB, revokeSecretB)
|
||||
change := download(t, serveRevokeS3(t, started, func(f http.HandlerFunc) http.HandlerFunc {
|
||||
return iam.Auth(func(w http.ResponseWriter, r *http.Request) {
|
||||
if iam.untrackInFlight(r) {
|
||||
f(w, r)
|
||||
}
|
||||
}, s3_constants.ACTION_PUT_BUCKET_POLICY)
|
||||
})+"/bkt/object", revokeKeyB, revokeSecretB)
|
||||
waitStarted(t, started, 2)
|
||||
|
||||
require.NoError(t, iam.policyEngine.engine.SetBucketPolicy("bkt",
|
||||
`{"Version":"2012-10-17","Statement":[{"Effect":"Deny","Principal":"*","Action":"s3:*","Resource":["arn:aws:s3:::bkt","arn:aws:s3:::bkt/*"]}]}`))
|
||||
iam.revalidateBucketInFlight("bkt")
|
||||
|
||||
assert.Error(t, downloadResult(t, read, time.Second), "the bucket policy denies the read")
|
||||
assert.NoError(t, downloadResult(t, change, 5*time.Second), "the change untracked itself first")
|
||||
|
||||
r := mux.SetURLVars(httptest.NewRequest(http.MethodPut, "/bkt?policy", nil), map[string]string{"bucket": "bkt"})
|
||||
r.Header.Set(s3_constants.AmzAuthType, "SigV4")
|
||||
r = r.WithContext(s3_constants.SetAccessKeyIdInContext(r.Context(), revokeKeyB))
|
||||
stored := true
|
||||
iam.abortWhenRevoked(func(w http.ResponseWriter, r *http.Request) {
|
||||
iam.revalidateBucketInFlight("bkt")
|
||||
select {
|
||||
case <-r.Context().Done():
|
||||
case <-time.After(5 * time.Second):
|
||||
t.Fatal("the denied request was not aborted")
|
||||
}
|
||||
stored = iam.untrackInFlight(r)
|
||||
}, iam.lookupByIdentityName("bob"), s3_constants.ACTION_PUT_BUCKET_POLICY, iam.inFlightGeneration())(httptest.NewRecorder(), r)
|
||||
assert.False(t, stored, "an aborted request must not store its change")
|
||||
}
|
||||
|
||||
// A re-check judges time conditions as of when the request was admitted, so a
|
||||
// grant that expires while the request runs does not abort it.
|
||||
func TestAbortRevokedRequestsJudgesTimeAsOfAdmission(t *testing.T) {
|
||||
|
||||
@@ -504,6 +504,9 @@ func (s3a *S3ApiServer) DeleteBucketHandler(w http.ResponseWriter, r *http.Reque
|
||||
}
|
||||
}
|
||||
|
||||
if !s3a.iam.untrackInFlight(r) {
|
||||
return
|
||||
}
|
||||
// The filer resolves and drops the bucket's collection inside the delete;
|
||||
// it keeps a shared one rather than risk another bucket's volumes.
|
||||
err := s3a.rm(r.Context(), s3a.option.BucketsPath, bucket, false, true)
|
||||
|
||||
@@ -115,6 +115,9 @@ func (s3a *S3ApiServer) PutBucketPolicyHandler(w http.ResponseWriter, r *http.Re
|
||||
return
|
||||
}
|
||||
|
||||
if !s3a.iam.untrackInFlight(r) {
|
||||
return
|
||||
}
|
||||
// Store bucket policy
|
||||
if err := s3a.setBucketPolicy(bucket, &policyDoc); err != nil {
|
||||
if errors.Is(err, ErrBucketNotFound) {
|
||||
@@ -178,6 +181,9 @@ func (s3a *S3ApiServer) DeleteBucketPolicyHandler(w http.ResponseWriter, r *http
|
||||
return
|
||||
}
|
||||
|
||||
if !s3a.iam.untrackInFlight(r) {
|
||||
return
|
||||
}
|
||||
// Delete bucket policy
|
||||
if err := s3a.deleteBucketPolicy(bucket); err != nil {
|
||||
if errors.Is(err, ErrBucketNotFound) {
|
||||
|
||||
Reference in new issue
Block a user