Repository navigation
s3: honor If-Match on DELETE #1245
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
47959f5
s3: honor If-Match on DELETE
bootjp 4abe41e
Merge main into fix/s3-conditional-delete
bootjp 7404490
adapter: honor S3 delete If-Match validators
bootjp 7929024
adapter: reject conditional delete for absent object
bootjp 14f505f
Merge commit '16b56f2d7a8c184e5ffba8faeb9a7df477707ff9' into HEAD
bootjp f08fb0b
Merge main into conditional S3 deletion
bootjp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,240 @@ | ||
| package adapter | ||
|
|
||
| import ( | ||
| "context" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/bootjp/elastickv/store" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| // Every case here uses one bucket and one object key; only the ETag and the | ||
| // presence of the header vary. | ||
| const ( | ||
| conditionalDeleteBucket = "retention" | ||
| conditionalDeleteKey = "payload.bin" | ||
| ) | ||
|
|
||
| // putS3TestObject stores body at the test bucket/key through the real handler | ||
| // and returns the ETag the server assigned. | ||
| func putS3TestObject(t *testing.T, server *S3Server, body string) string { | ||
| t.Helper() | ||
|
|
||
| rec := httptest.NewRecorder() | ||
| req := newS3TestRequest(http.MethodPut, | ||
| "/"+conditionalDeleteBucket+"/"+conditionalDeleteKey, strings.NewReader(body)) | ||
| req.Header.Set("Content-Type", "text/plain") | ||
| server.handle(rec, req) | ||
| require.Equal(t, http.StatusOK, rec.Code, "PUT %s/%s", conditionalDeleteBucket, conditionalDeleteKey) | ||
|
|
||
| etag := strings.Trim(rec.Header().Get("ETag"), `"`) | ||
| require.NotEmpty(t, etag, "the server must assign an ETag") | ||
| return etag | ||
| } | ||
|
|
||
| func newConditionalDeleteS3Server(t *testing.T) *S3Server { | ||
| t.Helper() | ||
|
|
||
| st := store.NewMVCCStore() | ||
| server := NewS3Server(nil, "", st, newLocalAdapterCoordinator(st), nil) | ||
| _, err := server.AdminCreateBucket(context.Background(), | ||
| fullAdminBucketsPrincipal(), conditionalDeleteBucket, s3AclPrivate) | ||
| require.NoError(t, err) | ||
| return server | ||
| } | ||
|
|
||
| func deleteS3TestObject(t *testing.T, server *S3Server, key, ifMatch string) *httptest.ResponseRecorder { | ||
| t.Helper() | ||
|
|
||
| rec := httptest.NewRecorder() | ||
| req := newS3TestRequest(http.MethodDelete, "/"+conditionalDeleteBucket+"/"+key, nil) | ||
| if ifMatch != "" { | ||
| req.Header.Set("If-Match", ifMatch) | ||
| } | ||
| server.handle(rec, req) | ||
| return rec | ||
| } | ||
|
|
||
| func s3TestObjectExists(t *testing.T, server *S3Server) bool { | ||
| t.Helper() | ||
|
|
||
| rec := httptest.NewRecorder() | ||
| server.handle(rec, newS3TestRequest(http.MethodHead, "/"+conditionalDeleteBucket+"/"+conditionalDeleteKey, nil)) | ||
| return rec.Code == http.StatusOK | ||
| } | ||
|
|
||
| // TestS3ConditionalDeleteRefusesAStaleETag is the load-bearing case. | ||
| // | ||
| // The handler loaded the manifest -- so it had the current ETag -- and then | ||
| // dispatched kv.Del without looking at If-Match. A caller that read an object, | ||
| // decided it was reclaimable, and sent a conditional delete believed the | ||
| // condition protected it from a concurrent rewrite. It did not: the object was | ||
| // removed whatever its ETag, so the rewrite was silently lost. Snapshot-offload | ||
| // retention relies on exactly this precondition and maps the 412 to | ||
| // ErrObjectModified. | ||
| func TestS3ConditionalDeleteRefusesAStaleETag(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| server := newConditionalDeleteS3Server(t) | ||
| staleETag := putS3TestObject(t, server, "first") | ||
|
|
||
| // A concurrent rewrite changes the ETag after the caller observed it. | ||
| newETag := putS3TestObject(t, server, "rewritten") | ||
| require.NotEqual(t, staleETag, newETag, "the rewrite must change the ETag") | ||
|
|
||
| rec := deleteS3TestObject(t, server, conditionalDeleteKey, staleETag) | ||
| require.Equal(t, http.StatusPreconditionFailed, rec.Code, | ||
| "a delete conditioned on a superseded ETag must fail with 412") | ||
| require.Contains(t, rec.Body.String(), "PreconditionFailed") | ||
| require.True(t, s3TestObjectExists(t, server), | ||
| "the rewritten object must survive a refused conditional delete") | ||
| } | ||
|
|
||
| // A matching ETag must still delete, or retention could never reclaim anything. | ||
| func TestS3ConditionalDeleteAcceptsAMatchingETag(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| server := newConditionalDeleteS3Server(t) | ||
| etag := putS3TestObject(t, server, "first") | ||
|
|
||
| rec := deleteS3TestObject(t, server, conditionalDeleteKey, etag) | ||
| require.Equal(t, http.StatusNoContent, rec.Code) | ||
| require.False(t, s3TestObjectExists(t, server)) | ||
| } | ||
|
|
||
| // Quoted ETags are the wire form AWS uses, so both spellings must match. | ||
| func TestS3ConditionalDeleteAcceptsAQuotedETag(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| server := newConditionalDeleteS3Server(t) | ||
| etag := putS3TestObject(t, server, "first") | ||
|
|
||
| rec := deleteS3TestObject(t, server, conditionalDeleteKey, `"`+etag+`"`) | ||
| require.Equal(t, http.StatusNoContent, rec.Code) | ||
| } | ||
|
|
||
| func TestS3ConditionalDeleteAcceptsWildcardAndETagList(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| for _, ifMatch := range []string{"*", `"stale", "PLACEHOLDER"`} { | ||
| t.Run(ifMatch, func(t *testing.T) { | ||
| t.Parallel() | ||
| server := newConditionalDeleteS3Server(t) | ||
| etag := putS3TestObject(t, server, "first") | ||
| condition := strings.ReplaceAll(ifMatch, "PLACEHOLDER", etag) | ||
|
|
||
| rec := deleteS3TestObject(t, server, conditionalDeleteKey, condition) | ||
| require.Equal(t, http.StatusNoContent, rec.Code) | ||
| require.False(t, s3TestObjectExists(t, server)) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestS3ConditionalDeleteRejectsBlankIfMatch(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| server := newConditionalDeleteS3Server(t) | ||
| putS3TestObject(t, server, "first") | ||
| rec := httptest.NewRecorder() | ||
| req := newS3TestRequest(http.MethodDelete, "/"+conditionalDeleteBucket+"/"+conditionalDeleteKey, nil) | ||
| req.Header.Set("If-Match", " ") | ||
| server.handle(rec, req) | ||
|
|
||
| require.Equal(t, http.StatusPreconditionFailed, rec.Code) | ||
| require.True(t, s3TestObjectExists(t, server)) | ||
| } | ||
|
|
||
| // An unconditional delete must be unaffected: adding the header check must not | ||
| // change the behaviour of every existing caller that does not send one. | ||
| func TestS3UnconditionalDeleteIsUnchanged(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| server := newConditionalDeleteS3Server(t) | ||
| putS3TestObject(t, server, "first") | ||
|
|
||
| rec := deleteS3TestObject(t, server, conditionalDeleteKey, "") | ||
| require.Equal(t, http.StatusNoContent, rec.Code) | ||
| require.False(t, s3TestObjectExists(t, server)) | ||
| } | ||
|
|
||
| // An unconditional delete remains idempotent, while a conditional delete | ||
| // reports that the object whose ETag was to be checked is missing. | ||
| func TestS3ConditionalDeleteOnAnAbsentKey(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| server := newConditionalDeleteS3Server(t) | ||
|
|
||
| rec := deleteS3TestObject(t, server, "never-existed", "") | ||
| require.Equal(t, http.StatusNoContent, rec.Code) | ||
|
|
||
| rec = deleteS3TestObject(t, server, "never-existed", "deadbeef") | ||
| require.Equal(t, http.StatusNotFound, rec.Code) | ||
| require.Contains(t, rec.Body.String(), "NoSuchKey") | ||
|
|
||
| rec = deleteS3TestObject(t, server, "never-existed", "*") | ||
| require.Equal(t, http.StatusNotFound, rec.Code) | ||
| require.Contains(t, rec.Body.String(), "NoSuchKey") | ||
| } | ||
|
|
||
| // TestValidateS3DeletePreconditions covers the predicate directly, including | ||
| // that If-None-Match is deliberately NOT honoured on a delete. | ||
| func TestValidateS3DeletePreconditions(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| previous := &s3ObjectManifest{ETag: "abc123"} | ||
|
|
||
| t.Run("no header passes", func(t *testing.T) { | ||
| t.Parallel() | ||
| req := newS3TestRequest(http.MethodDelete, "/b/k", nil) | ||
| require.NoError(t, validateS3DeletePreconditions(req, previous)) | ||
| }) | ||
|
|
||
| t.Run("matching etag passes", func(t *testing.T) { | ||
| t.Parallel() | ||
| req := newS3TestRequest(http.MethodDelete, "/b/k", nil) | ||
| req.Header.Set("If-Match", "abc123") | ||
| require.NoError(t, validateS3DeletePreconditions(req, previous)) | ||
| }) | ||
|
|
||
| t.Run("stale etag fails", func(t *testing.T) { | ||
| t.Parallel() | ||
| req := newS3TestRequest(http.MethodDelete, "/b/k", nil) | ||
| req.Header.Set("If-Match", "stale") | ||
| require.Error(t, validateS3DeletePreconditions(req, previous)) | ||
| }) | ||
|
|
||
| t.Run("multiple header lines match any strong etag", func(t *testing.T) { | ||
| t.Parallel() | ||
| req := newS3TestRequest(http.MethodDelete, "/b/k", nil) | ||
| req.Header.Add("If-Match", `"stale"`) | ||
| req.Header.Add("If-Match", `"abc123"`) | ||
| require.NoError(t, validateS3DeletePreconditions(req, previous)) | ||
| }) | ||
|
|
||
| t.Run("weak etag does not match", func(t *testing.T) { | ||
| t.Parallel() | ||
| req := newS3TestRequest(http.MethodDelete, "/b/k", nil) | ||
| req.Header.Set("If-Match", `W/"abc123"`) | ||
| require.Error(t, validateS3DeletePreconditions(req, previous)) | ||
| }) | ||
|
|
||
| t.Run("If-None-Match is not honoured on delete", func(t *testing.T) { | ||
| t.Parallel() | ||
| // "only if absent" is meaningless for an operation whose purpose is | ||
| // to remove something that exists; honouring it would refuse every | ||
| // delete of a present object. | ||
| req := newS3TestRequest(http.MethodDelete, "/b/k", nil) | ||
| req.Header.Set("If-None-Match", "*") | ||
| require.NoError(t, validateS3DeletePreconditions(req, previous)) | ||
| }) | ||
|
|
||
| t.Run("nil manifest fails with If-Match", func(t *testing.T) { | ||
| t.Parallel() | ||
| req := newS3TestRequest(http.MethodDelete, "/b/k", nil) | ||
| req.Header.Set("If-Match", "abc123") | ||
| require.Error(t, validateS3DeletePreconditions(req, nil)) | ||
| }) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.