package s3api import ( "context" "encoding/xml" "fmt" "io" "net/http" "net/http/httptest" "strings" "sync" "sync/atomic" "testing" "time" "github.com/gorilla/mux" "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb" "github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants" "github.com/seaweedfs/seaweedfs/weed/s3api/s3err" weed_server "github.com/seaweedfs/seaweedfs/weed/server" ) const keepAliveSrc, keepAliveDst, keepAliveObject = "ka-src", "ka-dst", "object" // keepAliveCopyServer serves a chunked source object, so CopyObject and // UploadPartCopy take their data-moving paths, with the keep-alive armed // after delay and the copy itself replaced by copy (a slow or failing volume). func keepAliveCopyServer(t *testing.T, delay time.Duration, dstVersioning string, work func() error) (*S3ApiServer, *copyACLFiler, *atomic.Int32) { t.Helper() oldDelay, oldInterval, oldObject, oldPart, oldReencrypt, oldFill := copyKeepAliveDelay, copyKeepAliveInterval, copyObjectData, copyPartData, copyPartReencrypt, copyRemoteFill t.Cleanup(func() { copyKeepAliveDelay, copyKeepAliveInterval, copyObjectData, copyPartData, copyPartReencrypt, copyRemoteFill = oldDelay, oldInterval, oldObject, oldPart, oldReencrypt, oldFill }) copyKeepAliveDelay, copyKeepAliveInterval = delay, 10*time.Millisecond calls := &atomic.Int32{} copied := []*filer_pb.FileChunk{{FileId: "3,0200000000000002", Offset: 0, Size: 4}} copyObjectData = func(*S3ApiServer, *filer_pb.Entry, *http.Request, string, string, string, string) ([]*filer_pb.FileChunk, map[string][]byte, error) { calls.Add(1) if err := work(); err != nil { return nil, nil, err } return copied, nil, nil } copyPartReencrypt = func(*S3ApiServer, *http.Request, *filer_pb.Entry, int64, int64, string, string, string, int, *filer_pb.Entry) (string, SSEResponseMetadata, s3err.ErrorCode) { calls.Add(1) if err := work(); err != nil { return "", SSEResponseMetadata{}, s3err.ErrInternalError } return "0123456789abcdef0123456789abcdef", SSEResponseMetadata{ChecksumHeaderName: "X-Amz-Checksum-Crc32", ChecksumValue: "AAAAAA=="}, s3err.ErrNone } copyPartData = func(*S3ApiServer, *filer_pb.Entry, int64, int64, string) ([]*filer_pb.FileChunk, []byte, error) { calls.Add(1) if err := work(); err != nil { return nil, nil, err } return copied, []byte("0123456789abcdef"), nil } filer := ©ACLFiler{ambiguousPutFiler: &ambiguousPutFiler{apply: true, entries: map[string]*filer_pb.Entry{}}} s3a := newPutTestServer(t, startFakeFiler(t, filer)) s3a.iam = &IdentityAccessManagement{} s3a.bucketConfigCache = NewBucketConfigCache(time.Minute) s3a.bucketConfigCache.Set(keepAliveSrc, &BucketConfig{Name: keepAliveSrc, Ownership: s3_constants.OwnershipObjectWriter}) s3a.bucketConfigCache.Set(keepAliveDst, &BucketConfig{Name: keepAliveDst, Versioning: dstVersioning, Ownership: s3_constants.OwnershipObjectWriter}) filer.entries["/buckets/"+keepAliveSrc+"/"+keepAliveObject] = &filer_pb.Entry{ Name: keepAliveObject, Attributes: &filer_pb.FuseAttributes{FileMode: defaultFileMode, FileSize: 4, Md5: []byte("0123456789abcdef")}, Chunks: []*filer_pb.FileChunk{{FileId: "3,0100000000000001", Offset: 0, Size: 4}}, } return s3a, filer, calls } func copyObjectRequest(source string) *http.Request { req := httptest.NewRequest(http.MethodPut, "/"+keepAliveDst+"/"+keepAliveObject, nil) req = mux.SetURLVars(req, map[string]string{"bucket": keepAliveDst, "object": keepAliveObject}) req.Header.Set("X-Amz-Copy-Source", source) return req } // partCopyRequest opens an upload carrying extended (SSE, checksum) and // returns an UploadPartCopy of the source into its part 1. func partCopyRequest(s3a *S3ApiServer, filer *copyACLFiler, extended map[string][]byte) *http.Request { uploadID := s3a.generateUploadID(keepAliveObject) ext := map[string][]byte{s3_constants.ExtMultipartObjectKey: []byte(keepAliveObject)} for k, v := range extended { ext[k] = v } filer.entries["/buckets/"+keepAliveDst+"/.uploads/"+uploadID] = &filer_pb.Entry{Name: uploadID, IsDirectory: true, Extended: ext} req := httptest.NewRequest(http.MethodPut, "/"+keepAliveDst+"/"+keepAliveObject+"?partNumber=1&uploadId="+uploadID, nil) req = mux.SetURLVars(req, map[string]string{"bucket": keepAliveDst, "object": keepAliveObject}) req.Header.Set("X-Amz-Copy-Source", "/"+keepAliveSrc+"/"+keepAliveObject) return req } func slowCopy() error { time.Sleep(150 * time.Millisecond); return nil } // keptAliveBody checks the shape of a kept-alive answer: 200, the XML // declaration flushed first, whitespace while the copy ran, then the // document; it returns that document. func keptAliveBody(t *testing.T, rr *httptest.ResponseRecorder) string { t.Helper() if rr.Code != http.StatusOK { t.Fatalf("status = %d, want 200; body: %q", rr.Code, rr.Body.String()) } if got := rr.Header().Get("Content-Type"); got != "application/xml" { t.Fatalf("Content-Type = %q", got) } if rr.Header().Get("Content-Length") != "" { t.Fatalf("a kept-alive answer cannot know its length up front") } body := rr.Body.String() rest, ok := strings.CutPrefix(body, xml.Header) if !ok { t.Fatalf("body does not open with the XML declaration: %q", body) } doc := strings.TrimLeft(rest, " ") if len(rest)-len(doc) < 2 { t.Fatalf("no keep-alive whitespace before the document: %q", body) } return doc } // A copy that outlasts the delay answers 200 at once and keeps the connection // alive; the result a standard decoder (and the AWS SDKs, which skip leading // whitespace) reads is the CopyObjectResult. func TestCopyObjectKeepAliveSlowCopySucceeds(t *testing.T) { s3a, filer, _ := keepAliveCopyServer(t, 20*time.Millisecond, "", slowCopy) rr := httptest.NewRecorder() s3a.CopyObjectHandler(rr, copyObjectRequest("/"+keepAliveSrc+"/"+keepAliveObject)) doc := keptAliveBody(t, rr) if !strings.HasPrefix(doc, " document with the code the handler answers it with (a read-only // volume is AccessDenied), which the SDKs raise as that error. func TestCopyObjectKeepAliveSlowCopyFails(t *testing.T) { s3a, filer, _ := keepAliveCopyServer(t, 20*time.Millisecond, "", func() error { time.Sleep(150 * time.Millisecond) return fmt.Errorf("assign volume: %w", weed_server.ErrReadOnly) }) rr := httptest.NewRecorder() s3a.CopyObjectHandler(rr, copyObjectRequest("/"+keepAliveSrc+"/"+keepAliveObject)) doc := keptAliveBody(t, rr) var e s3err.RESTErrorResponse if err := xml.NewDecoder(strings.NewReader(doc)).Decode(&e); err != nil { t.Fatalf("decode %q: %v", doc, err) } if e.Code != "AccessDenied" || e.RequestID == "" || e.Resource == "" { t.Fatalf("error document = %+v, want AccessDenied with its request id and resource", e) } if rr.Header().Get("x-amz-request-id") != e.RequestID { t.Fatalf("request id header %q differs from the document's %q", rr.Header().Get("x-amz-request-id"), e.RequestID) } if filer.entries["/buckets/"+keepAliveDst+"/"+keepAliveObject] != nil { t.Fatalf("a failed copy wrote the destination") } } // A copy that ends before the delay answers exactly as before: one document // with its length, no leading whitespace, the ETag header set. func TestCopyObjectKeepAliveFastCopyUnchanged(t *testing.T) { s3a, _, calls := keepAliveCopyServer(t, time.Second, "", func() error { return nil }) rr := httptest.NewRecorder() s3a.CopyObjectHandler(rr, copyObjectRequest("/"+keepAliveSrc+"/"+keepAliveObject)) if calls.Load() != 1 { t.Fatalf("copy ran %d times, want 1", calls.Load()) } if rr.Code != http.StatusOK { t.Fatalf("status = %d; body: %q", rr.Code, rr.Body.String()) } if !strings.HasPrefix(rr.Body.String(), xml.Header+"NoSuchKey") { t.Fatalf("status = %d, body %q; want 404 NoSuchKey", rr.Code, rr.Body.String()) } if strings.Contains(rr.Body.String(), "> <") || calls.Load() != 0 { t.Fatalf("a refused copy reached the copy step or the keep-alive") } } // UploadPartCopy keeps a slow part copy alive the same way, ending in the // CopyPartResult. func TestCopyObjectPartKeepAliveSlowCopySucceeds(t *testing.T) { s3a, filer, _ := keepAliveCopyServer(t, 20*time.Millisecond, "", slowCopy) rr := httptest.NewRecorder() s3a.CopyObjectPartHandler(rr, partCopyRequest(s3a, filer, nil)) doc := keptAliveBody(t, rr) if !strings.HasPrefix(doc, "AAAAAA==") { t.Fatalf("CopyPartResult lost the checksum: %q", doc) } } // A remote-only source is filled after the keep-alive arms: on a cell whose // disk is a cache, that fill is the slow part. func TestCopyObjectKeepAliveCoversRemoteFill(t *testing.T) { s3a, filer, calls := keepAliveCopyServer(t, 20*time.Millisecond, "", func() error { return nil }) key := "/buckets/" + keepAliveSrc + "/" + keepAliveObject filled := filer.entries[key] remote := &filer_pb.RemoteEntry{RemoteSize: 4, RemoteETag: "r", RemoteMtime: 1} filled.RemoteEntry = remote filer.entries[key] = &filer_pb.Entry{Name: keepAliveObject, Attributes: filled.Attributes, RemoteEntry: remote} copyRemoteFill = func(_ *S3ApiServer, _ context.Context, src *filer_pb.Entry, _, _, _ string) *filer_pb.Entry { if !src.IsInRemoteOnly() { t.Errorf("fill called for a local source") } time.Sleep(150 * time.Millisecond) return filled } rr := httptest.NewRecorder() s3a.CopyObjectHandler(rr, copyObjectRequest("/"+keepAliveSrc+"/"+keepAliveObject)) if doc := keptAliveBody(t, rr); !strings.HasPrefix(doc, " in it. func TestCopyObjectKeepAliveSlowLookupThenBadTagKeepsStatus(t *testing.T) { s3a, filer, calls := keepAliveCopyServer(t, 20*time.Millisecond, "", slowCopy) filer.afterLookup = func(*filer_pb.LookupDirectoryEntryRequest) { time.Sleep(200 * time.Millisecond) } req := copyObjectRequest("/" + keepAliveSrc + "/" + keepAliveObject) req.Header.Set(s3_constants.AmzObjectTaggingDirective, "REPLACE") req.Header.Set(s3_constants.AmzObjectTagging, strings.Repeat("k", 200)+"=v") rr := httptest.NewRecorder() s3a.CopyObjectHandler(rr, req) if rr.Code != http.StatusBadRequest || !strings.Contains(rr.Body.String(), "InvalidTag") { t.Fatalf("status = %d, body %q; want 400 InvalidTag", rr.Code, rr.Body.String()) } if calls.Load() != 0 { t.Fatalf("a refused copy reached the copy step") } } // The metadata-only self-copy moves no data and is never kept alive: a // source condition that fails on the snapshot read under the write lock, // after a slow wait, still answers 412. func TestCopyObjectSelfCopyConditionUnderLockKeeps412(t *testing.T) { s3a, filer, _ := keepAliveCopyServer(t, 20*time.Millisecond, "", slowCopy) key := "/buckets/" + keepAliveSrc + "/" + keepAliveObject filer.entries[key].Extended = map[string][]byte{s3_constants.ExtETagKey: []byte("aaaa")} lookups := 0 filer.afterLookup = func(req *filer_pb.LookupDirectoryEntryRequest) { if req.Directory+"/"+req.Name != key { return } if lookups++; lookups == 1 { // a concurrent writer replaces the object right after the first read filer.entries[key].Extended[s3_constants.ExtETagKey] = []byte("bbbb") return } time.Sleep(200 * time.Millisecond) } req := httptest.NewRequest(http.MethodPut, "/"+keepAliveSrc+"/"+keepAliveObject, nil) req = mux.SetURLVars(req, map[string]string{"bucket": keepAliveSrc, "object": keepAliveObject}) req.Header.Set("X-Amz-Copy-Source", "/"+keepAliveSrc+"/"+keepAliveObject) req.Header.Set(s3_constants.AmzUserMetaDirective, "REPLACE") req.Header.Set("X-Amz-Copy-Source-If-Match", `"aaaa"`) rr := httptest.NewRecorder() s3a.CopyObjectHandler(rr, req) filer.mu.Lock() defer filer.mu.Unlock() if lookups < 2 { t.Fatalf("the self-copy did not re-read its source under the lock (%d lookups)", lookups) } if rr.Code != http.StatusPreconditionFailed { t.Fatalf("status = %d, body %q; want 412", rr.Code, rr.Body.String()) } } // UploadPartCopy checks a remote-only source's range against the remote // object's size: its local attributes can be stale until the fill. func TestCopyObjectPartRangeOnRemoteOnlySourceUsesRemoteSize(t *testing.T) { s3a, filer, calls := keepAliveCopyServer(t, time.Second, "", func() error { return nil }) key := "/buckets/" + keepAliveSrc + "/" + keepAliveObject filled := filer.entries[key] remote := &filer_pb.RemoteEntry{RemoteSize: 4, RemoteETag: "r", RemoteMtime: 1} filled.RemoteEntry = remote filled.Attributes.Mtime = 2 // stale local size, and a remote mtime that does not postdate it filer.entries[key] = &filer_pb.Entry{Name: keepAliveObject, Attributes: &filer_pb.FuseAttributes{FileMode: defaultFileMode, Mtime: 2}, RemoteEntry: remote} copyRemoteFill = func(*S3ApiServer, context.Context, *filer_pb.Entry, string, string, string) *filer_pb.Entry { return filled } req := partCopyRequest(s3a, filer, nil) req.Header.Set("X-Amz-Copy-Source-Range", "bytes=0-3") rr := httptest.NewRecorder() s3a.CopyObjectPartHandler(rr, req) if rr.Code != http.StatusOK || calls.Load() != 1 { t.Fatalf("status = %d, copies %d, body %q; want the range copied", rr.Code, calls.Load(), rr.Body.String()) } } // A failing source condition on a remote-only source keeps its 412 however // long the fill takes: the fill it reads runs before the keep-alive arms. func TestCopyKeepAliveRemoteSourceConditionKeeps412(t *testing.T) { for _, part := range []bool{false, true} { s3a, filer, calls := keepAliveCopyServer(t, 20*time.Millisecond, "", func() error { return nil }) key := "/buckets/" + keepAliveSrc + "/" + keepAliveObject filled := filer.entries[key] remote := &filer_pb.RemoteEntry{RemoteSize: 4, RemoteETag: "r", RemoteMtime: 1} filled.RemoteEntry = remote filer.entries[key] = &filer_pb.Entry{Name: keepAliveObject, Attributes: filled.Attributes, RemoteEntry: remote} copyRemoteFill = func(*S3ApiServer, context.Context, *filer_pb.Entry, string, string, string) *filer_pb.Entry { time.Sleep(150 * time.Millisecond) return filled } req := copyObjectRequest("/" + keepAliveSrc + "/" + keepAliveObject) if part { req = partCopyRequest(s3a, filer, nil) } req.Header.Set(s3_constants.AmzCopySourceIfMatch, `"not-the-etag"`) rr := httptest.NewRecorder() if part { s3a.CopyObjectPartHandler(rr, req) } else { s3a.CopyObjectHandler(rr, req) } if rr.Code != http.StatusPreconditionFailed || calls.Load() != 0 { t.Fatalf("part=%v: status = %d, copies %d, body %q; want 412 and no copy", part, rr.Code, calls.Load(), rr.Body.String()) } } } // A remote-only self-copy that has to move data (here plaintext into SSE-S3) // is not metadata-only, so its fill runs under the keep-alive. func TestCopyObjectKeepAliveCoversSelfCopyRemoteFill(t *testing.T) { s3a, filer, calls := keepAliveCopyServer(t, 20*time.Millisecond, "", func() error { return nil }) key := "/buckets/" + keepAliveSrc + "/" + keepAliveObject filled := filer.entries[key] remote := &filer_pb.RemoteEntry{RemoteSize: 4, RemoteETag: "r", RemoteMtime: 1} filled.RemoteEntry = remote filer.entries[key] = &filer_pb.Entry{Name: keepAliveObject, Attributes: filled.Attributes, RemoteEntry: remote} copyRemoteFill = func(*S3ApiServer, context.Context, *filer_pb.Entry, string, string, string) *filer_pb.Entry { time.Sleep(150 * time.Millisecond) return filled } req := httptest.NewRequest(http.MethodPut, "/"+keepAliveSrc+"/"+keepAliveObject, nil) req = mux.SetURLVars(req, map[string]string{"bucket": keepAliveSrc, "object": keepAliveObject}) req.Header.Set("X-Amz-Copy-Source", "/"+keepAliveSrc+"/"+keepAliveObject) req.Header.Set(s3_constants.AmzUserMetaDirective, "REPLACE") req.Header.Set(s3_constants.AmzServerSideEncryption, "AES256") rr := httptest.NewRecorder() s3a.CopyObjectHandler(rr, req) if doc := keptAliveBody(t, rr); !strings.HasPrefix(doc, " 2*time.Second { t.Fatalf("the keep-alive reached the client only after %v", elapsed) } if want := xml.Header + strings.Repeat(" ", 2*len(copyKeepAliveSpaces)); string(head) != want { t.Fatalf("streamed %q, want %q", head, want) } releaseOnce.Do(func() { close(release) }) rest, err := io.ReadAll(resp.Body) if err != nil { t.Fatalf("read the result: %v", err) } if doc := strings.TrimLeft(string(rest), " "); !strings.HasPrefix(doc, "