Files
seaweedfs/weed/s3api/s3api_object_handlers_conditional_read_test.go
T
f9b494c576 filer, s3: give an entry without chunks the ETag of its inline content (#13106)
* filer, s3: give an entry without chunks the ETag of its inline content

A file written through a mount stores no MD5. With no chunks either (an
empty file, or a small one kept inline) S3 answered HEAD, GET and
listings with an empty ETag, and the filer's HTTP API with the chunk
form for zero chunks, "d41d8cd98f00b204e9800998ecf8427e-0". Both now
answer with the MD5 of the inline content, which for an empty file is
what AWS gives an empty object and what the same file gets once it is
written through S3.

An entry whose size reaches past its inline content (a hole that no
chunk covers) keeps the ETag it had.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

* filer: match If-Match on an entry without chunks against its inline-content ETag

The gateway now answers the MD5 of the inline bytes for an entry with no
stored MD5 and no chunks; the filer's conditional check still saw an empty
ETag, so a PUT with If-Match set to the ETag HEAD returned failed with 412.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: MapleWhiteZZZ <18653045+MapleWhiteZZZ@users.noreply.github.com>
Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
Co-authored-by: Chris Lu <chrislusf@users.noreply.github.com>
2026-11-29 10:32:38 -08:00

334 lines
13 KiB
Go

package s3api
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
"github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants"
"github.com/seaweedfs/seaweedfs/weed/s3api/s3err"
)
// A precondition can only fail against an object that exists. GET/HEAD of a missing
// key must stay a missing-key answer even when If-Match or If-Unmodified-Since is sent.
func TestValidateConditionalHeadersForReadsMissingObject(t *testing.T) {
s3a := &S3ApiServer{}
existing := &filer_pb.Entry{
Attributes: &filer_pb.FuseAttributes{Mtime: time.Now().Unix()},
Extended: map[string][]byte{s3_constants.ExtETagKey: []byte("d41d8cd98f00b204e9800998ecf8427e")},
}
deleteMarker := &filer_pb.Entry{
Attributes: &filer_pb.FuseAttributes{Mtime: time.Now().Unix()},
Extended: map[string][]byte{s3_constants.ExtDeleteMarkerKey: []byte("true")},
}
future := time.Now().Add(24 * time.Hour).UTC().Format(http.TimeFormat)
testCases := []struct {
name string
header string
value string
entry *filer_pb.Entry
want s3err.ErrorCode
}{
{"if-match on missing object", s3_constants.IfMatch, "0000", nil, s3err.ErrNoSuchKey},
{"if-match star on missing object", s3_constants.IfMatch, "*", nil, s3err.ErrNoSuchKey},
{"if-unmodified-since on missing object", s3_constants.IfUnmodifiedSince, future, nil, s3err.ErrNoSuchKey},
{"if-match on delete marker", s3_constants.IfMatch, "0000", deleteMarker, s3err.ErrNoSuchKey},
{"if-none-match on missing object", s3_constants.IfNoneMatch, "*", nil, s3err.ErrNone},
{"if-modified-since on missing object", s3_constants.IfModifiedSince, future, nil, s3err.ErrNone},
{"if-match mismatch on existing object", s3_constants.IfMatch, "0000", existing, s3err.ErrPreconditionFailed},
{"if-match hit on existing object", s3_constants.IfMatch, "d41d8cd98f00b204e9800998ecf8427e", existing, s3err.ErrNone},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
r := httptest.NewRequest(http.MethodGet, "/bucket/object", nil)
r.Header.Set(tc.header, tc.value)
headers, errCode := parseConditionalHeaders(r)
if errCode != s3err.ErrNone {
t.Fatalf("parseConditionalHeaders: %v", errCode)
}
result := s3a.validateConditionalHeadersForReads(r, headers, tc.entry, "bucket", "object")
if result.ErrorCode != tc.want {
t.Errorf("got %v, want %v", result.ErrorCode, tc.want)
}
})
}
}
// If-None-Match uses the weak comparison and If-Match the strong one (RFC 9110
// 13.1.1, 13.1.2): a W/ tag revalidates to 304 but never satisfies If-Match.
func TestValidateConditionalHeadersForReadsWeakETags(t *testing.T) {
s3a := &S3ApiServer{}
entry := &filer_pb.Entry{
Attributes: &filer_pb.FuseAttributes{Mtime: time.Now().Unix()},
Extended: map[string][]byte{s3_constants.ExtETagKey: []byte("d41d8cd98f00b204e9800998ecf8427e")},
}
testCases := []struct {
name string
header string
value string
entry *filer_pb.Entry
want s3err.ErrorCode
}{
{"if-none-match weak hit", s3_constants.IfNoneMatch, `W/"d41d8cd98f00b204e9800998ecf8427e"`, entry, s3err.ErrNotModified},
{"if-none-match weak hit in list", s3_constants.IfNoneMatch, `"0000", W/"d41d8cd98f00b204e9800998ecf8427e"`, entry, s3err.ErrNotModified},
{"if-none-match weak miss", s3_constants.IfNoneMatch, `W/"0000"`, entry, s3err.ErrNone},
{"if-none-match strong hit", s3_constants.IfNoneMatch, `"d41d8cd98f00b204e9800998ecf8427e"`, entry, s3err.ErrNotModified},
{"if-match weak tag", s3_constants.IfMatch, `W/"d41d8cd98f00b204e9800998ecf8427e"`, entry, s3err.ErrPreconditionFailed},
{"if-match strong hit after weak tag", s3_constants.IfMatch, `W/"0000", "d41d8cd98f00b204e9800998ecf8427e"`, entry, s3err.ErrNone},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
r := httptest.NewRequest(http.MethodGet, "/bucket/object", nil)
r.Header.Set(tc.header, tc.value)
headers, errCode := parseConditionalHeaders(r)
if errCode != s3err.ErrNone {
t.Fatalf("parseConditionalHeaders: %v", errCode)
}
result := s3a.validateConditionalHeadersForReads(r, headers, tc.entry, "bucket", "object")
if result.ErrorCode != tc.want {
t.Errorf("got %v, want %v", result.ErrorCode, tc.want)
}
})
}
}
func TestRemoteObjectETagAcrossCacheFill(t *testing.T) {
s3a := &S3ApiServer{}
entry := &filer_pb.Entry{
Name: "object",
Attributes: &filer_pb.FuseAttributes{FileSize: 2048, Mtime: 100},
RemoteEntry: &filer_pb.RemoteEntry{RemoteSize: 2048, RemoteMtime: 100, RemoteETag: "remote-multipart-2"},
}
head := httptest.NewRecorder()
s3a.setResponseHeaders(head, httptest.NewRequest(http.MethodHead, "/bucket/object", nil), entry, 2048)
headETag := head.Header().Get("ETag")
if headETag != `"remote-multipart-2"` {
t.Errorf("cold ETag = %q", headETag)
}
entry.Chunks = []*filer_pb.FileChunk{
{Offset: 0, Size: 1024, ETag: "1B2M2Y8AsgTpgAmY7PhCfg=="},
{Offset: 1024, Size: 1024, ETag: "1B2M2Y8AsgTpgAmY7PhCfg=="},
}
entry.RemoteEntry.LastLocalSyncTsNs = 200
if got := s3a.getObjectETag(entry); got != headETag {
t.Errorf("cached ETag = %q, cold ETag = %q", got, headETag)
}
for _, method := range []string{http.MethodGet, http.MethodHead} {
r := httptest.NewRequest(method, "/bucket/object", nil)
r.Header.Set(s3_constants.IfMatch, headETag)
r.Header.Set("Range", "bytes=1024-2047")
headers, code := parseConditionalHeaders(r)
if code != s3err.ErrNone {
t.Fatal(code)
}
result := s3a.validateConditionalHeadersForReads(r, headers, entry, "bucket", "object")
if result.ErrorCode != s3err.ErrNone {
t.Errorf("%s after cache fill: %v", method, result.ErrorCode)
}
}
entry.RemoteEntry.RemoteETag = `"remote-multipart-2"`
if got := s3a.getObjectETag(entry); got != headETag {
t.Errorf("quoted remote ETag = %q", got)
}
entry.Extended = map[string][]byte{s3_constants.ExtETagKey: []byte("local-etag")}
if got := s3a.getObjectETag(entry); got != `"local-etag"` {
t.Errorf("explicit ETag = %q", got)
}
entry.Extended = nil
entry.RemoteEntry.LastLocalSyncTsNs = 0
if got, want := s3a.getObjectETag(entry), s3a.calculateETagFromChunks(entry.Chunks); got != want {
t.Errorf("unsynced local ETag = %q, want %q", got, want)
}
}
// A metadata-only update such as utimens changes Mtime without touching the
// remote bytes, so the object must keep its remote ETag; otherwise a later
// cache fill restores it and breaks clients holding the interim value.
func TestRemoteObjectETagMetadataOnlyTouch(t *testing.T) {
s3a := &S3ApiServer{}
for _, cached := range []bool{false, true} {
entry := &filer_pb.Entry{
Name: "object",
Attributes: &filer_pb.FuseAttributes{FileSize: 2048, Mtime: 100},
RemoteEntry: &filer_pb.RemoteEntry{
RemoteSize: 2048, RemoteMtime: 100, RemoteETag: "remote-multipart-2",
},
}
if cached {
entry.Chunks = []*filer_pb.FileChunk{{Size: 2048, ETag: "1B2M2Y8AsgTpgAmY7PhCfg=="}}
entry.RemoteEntry.LastLocalSyncTsNs = 200
}
entry.Attributes.Mtime = 101
entry.Attributes.MtimeNs = 500
if got := s3a.getObjectETag(entry); got != `"remote-multipart-2"` {
t.Errorf("cached=%v touched ETag = %q", cached, got)
}
}
}
// Extending or shrinking a cached remote file through setattr changes FileSize
// while leaving its chunks and sync stamp alone; the remote ETag no longer
// describes the local bytes and conditional reads must not answer 304 for it.
func TestRemoteObjectETagLocalResize(t *testing.T) {
s3a := &S3ApiServer{}
for _, size := range []uint64{0, 1024, 4096} {
entry := &filer_pb.Entry{
Name: "object",
Attributes: &filer_pb.FuseAttributes{FileSize: 2048, Mtime: 100},
Chunks: []*filer_pb.FileChunk{{Size: 2048, ETag: "1B2M2Y8AsgTpgAmY7PhCfg=="}},
RemoteEntry: &filer_pb.RemoteEntry{
RemoteSize: 2048, RemoteMtime: 100, RemoteETag: "remote-multipart-2",
LastLocalSyncTsNs: 200,
},
}
entry.Attributes.FileSize = size
if got := s3a.getObjectETag(entry); got == `"remote-multipart-2"` {
t.Errorf("resized to %d retained remote ETag", size)
}
r := httptest.NewRequest(http.MethodHead, "/bucket/object", nil)
r.Header.Set(s3_constants.IfNoneMatch, `"remote-multipart-2"`)
headers, code := parseConditionalHeaders(r)
if code != s3err.ErrNone {
t.Fatal(code)
}
if got := s3a.validateConditionalHeadersForReads(r, headers, entry, "bucket", "object"); got.ErrorCode != s3err.ErrNone {
t.Errorf("resized to %d read: %v", size, got.ErrorCode)
}
}
}
func TestRemoteObjectETagCopyConditions(t *testing.T) {
s3a := &S3ApiServer{}
entry := &filer_pb.Entry{
Attributes: &filer_pb.FuseAttributes{FileSize: 2048, Mtime: 100},
RemoteEntry: &filer_pb.RemoteEntry{RemoteSize: 2048, RemoteMtime: 100, RemoteETag: "remote-multipart-2"},
}
for _, cached := range []bool{false, true} {
if cached {
entry.Chunks = []*filer_pb.FileChunk{{Size: 2048, ETag: "1B2M2Y8AsgTpgAmY7PhCfg=="}}
entry.RemoteEntry.LastLocalSyncTsNs = 200
}
for _, condition := range []string{s3_constants.AmzCopySourceIfMatch, s3_constants.AmzCopySourceIfNoneMatch} {
r := httptest.NewRequest(http.MethodPut, "/bucket/copy", nil)
r.Header.Set(condition, s3a.getObjectETag(entry))
want := s3err.ErrNone
if condition == s3_constants.AmzCopySourceIfNoneMatch {
want = s3err.ErrPreconditionFailed
}
if got := s3a.validateConditionalCopyHeaders(r, entry); got != want {
t.Errorf("cached=%v %s: got %v, want %v", cached, condition, got, want)
}
}
}
}
func TestRemoteObjectETagLocalChanges(t *testing.T) {
s3a := &S3ApiServer{}
for _, content := range [][]byte{nil, []byte("new local content")} {
entry := &filer_pb.Entry{
Attributes: &filer_pb.FuseAttributes{FileSize: uint64(len(content)), Mtime: 101},
Content: content,
RemoteEntry: &filer_pb.RemoteEntry{RemoteSize: 2048, RemoteMtime: 100, RemoteETag: "old-origin"},
}
if got, want := copyEntryETag(entry), strings.Trim(s3a.getObjectETag(entry), `"`); got != want {
t.Errorf("local copy ETag = %q, read ETag = %q", got, want)
}
if got := s3a.getObjectETag(entry); got == `"old-origin"` {
t.Errorf("local content %q retained remote ETag", content)
}
r := httptest.NewRequest(http.MethodHead, "/bucket/object", nil)
r.Header.Set(s3_constants.IfNoneMatch, `"old-origin"`)
headers, code := parseConditionalHeaders(r)
if code != s3err.ErrNone {
t.Fatal(code)
}
if got := s3a.validateConditionalHeadersForReads(r, headers, entry, "bucket", "object"); got.ErrorCode != s3err.ErrNone {
t.Errorf("local content %q read: %v", content, got.ErrorCode)
}
r.Header.Set(s3_constants.AmzCopySourceIfMatch, `"old-origin"`)
if got := s3a.validateConditionalCopyHeaders(r, entry); got != s3err.ErrPreconditionFailed {
t.Errorf("local content %q copy: %v", content, got)
}
}
}
func TestRemoteObjectETagFallback(t *testing.T) {
s3a := &S3ApiServer{}
entry := &filer_pb.Entry{
Attributes: &filer_pb.FuseAttributes{Md5: []byte{0xab, 0xcd}},
RemoteEntry: &filer_pb.RemoteEntry{},
}
if got := s3a.getObjectETag(entry); got != `"abcd"` {
t.Errorf("missing remote ETag = %q", got)
}
entry.RemoteEntry.RemoteETag = "empty-origin"
if got := s3a.getObjectETag(entry); got != `"empty-origin"` {
t.Errorf("empty remote object ETag = %q", got)
}
entry.RemoteEntry = nil
if got := s3a.getObjectETag(entry); got != `"abcd"` {
t.Errorf("local object ETag = %q", got)
}
}
func TestSetResponseHeadersIgnoresOverridesForAnonymous(t *testing.T) {
s3a := &S3ApiServer{}
entry := &filer_pb.Entry{
Name: "object",
Attributes: &filer_pb.FuseAttributes{FileSize: 8, Mtime: 100},
}
for name, url := range map[string]string{
"anonymous": "/bucket/object?response-content-type=text%2Fhtml&response-content-disposition=inline",
"signed": "/bucket/object?response-content-type=text%2Fhtml&response-content-disposition=inline",
"presigned": "/bucket/object?X-Amz-Credential=k&response-content-type=text%2Fhtml",
} {
r := httptest.NewRequest(http.MethodGet, url, nil)
if name == "signed" {
r.Header.Set("Authorization", "AWS4-HMAC-SHA256 Credential=k")
}
w := httptest.NewRecorder()
s3a.setResponseHeaders(w, r, entry, 8)
if name == "anonymous" {
if got := w.Header().Get("Content-Disposition"); got != "" {
t.Errorf("anonymous request must not override response headers, got Content-Disposition %q", got)
}
} else if got := w.Header().Get("Content-Type"); got != "text/html" {
t.Errorf("%s request should apply response overrides, got Content-Type %q", name, got)
}
}
}
// A file written through a mount stores no MD5. When it also has no chunks
// (it is empty, or small and kept inline) S3 answers with the MD5 of its
// bytes, as AWS does for an empty object, not with an empty ETag.
func TestObjectETagOfAnEntryWithoutChunks(t *testing.T) {
s3a := &S3ApiServer{}
tests := []struct {
name string
entry *filer_pb.Entry
want string
}{
{"empty file", &filer_pb.Entry{Attributes: &filer_pb.FuseAttributes{}}, `"d41d8cd98f00b204e9800998ecf8427e"`},
{"inline content", &filer_pb.Entry{Content: []byte("hello"), Attributes: &filer_pb.FuseAttributes{FileSize: 5}}, `"5d41402abc4b2a76b9719d911017c592"`},
{"a stored MD5 wins", &filer_pb.Entry{Attributes: &filer_pb.FuseAttributes{Md5: []byte{0xab, 0xcd}}}, `"abcd"`},
// a hole no chunk covers is not inline content: no hash to give
{"a hole only", &filer_pb.Entry{Attributes: &filer_pb.FuseAttributes{FileSize: 4096}}, `""`},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := s3a.getObjectETag(tt.entry); got != tt.want {
t.Errorf("getObjectETag() = %s, want %s", got, tt.want)
}
})
}
}