diff --git a/weed/server/filer_server_handlers_copy.go b/weed/server/filer_server_handlers_copy.go index cf059530e0..251df3958b 100644 --- a/weed/server/filer_server_handlers_copy.go +++ b/weed/server/filer_server_handlers_copy.go @@ -851,6 +851,11 @@ func (fs *FilerServer) streamCopyChunk(ctx context.Context, srcChunk *filer_pb.F Size: srcChunk.Size, ETag: srcChunk.ETag, ModifiedTsNs: srcChunk.ModifiedTsNs, + CipherKey: srcChunk.CipherKey, + // a plain gzip needle arrives decompressed; ciphertext is copied as stored + IsCompressed: srcChunk.IsCompressed && len(srcChunk.CipherKey) > 0, + SseType: srcChunk.SseType, + SseMetadata: srcChunk.SseMetadata, } glog.V(4).InfofCtx(ctx, "FilerServer.streamCopyChunk: successfully streamed %d bytes", srcChunk.Size) @@ -899,7 +904,11 @@ func (fs *FilerServer) performStreamCopy(ctx context.Context, srcUrl, srcAuth, d if err != nil { return fmt.Errorf("failed to create destination request: %v", err) } + // A ciphertext needle is longer than the chunk's clear size. dstReq.ContentLength = int64(expectedSize) + if resp.ContentLength >= 0 { + dstReq.ContentLength = resp.ContentLength + } // Set authorization header if provided if auth != "" { diff --git a/weed/server/filer_server_handlers_copy_manifest_test.go b/weed/server/filer_server_handlers_copy_manifest_test.go index ea50565de4..30356da021 100644 --- a/weed/server/filer_server_handlers_copy_manifest_test.go +++ b/weed/server/filer_server_handlers_copy_manifest_test.go @@ -59,19 +59,13 @@ func (m *chunkCopyMaster) LookupVolume(_ context.Context, req *master_pb.LookupV return resp, nil } -func TestCopyChunkManifestPreservesOverwrites(t *testing.T) { +// startChunkCopyFixture serves chunks from a fake volume server, keeps what is +// written to it, and returns a filer server whose store holds nothing yet. +func startChunkCopyFixture(t *testing.T, ctx context.Context, chunks map[string][]byte) (*FilerServer, *renameTestStore, *wdclient.MasterClient) { + t.Helper() operation.InvalidateVolumeIdLocationCache("1") t.Cleanup(func() { operation.InvalidateVolumeIdLocationCache("1") }) - ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) - defer cancel() - old := &filer_pb.FileChunk{FileId: "1,0100000001", Size: 7, ModifiedTsNs: 1} - filer_pb.BeforeEntrySerialization([]*filer_pb.FileChunk{old}) - manifest, err := proto.Marshal(&filer_pb.FileChunkManifest{Chunks: []*filer_pb.FileChunk{old}}) - if err != nil { - t.Fatal(err) - } var mu sync.Mutex - chunks := map[string][]byte{"/1,0100000001": []byte("OLD old"), "/1,0200000001": []byte("NEW"), "/1,0300000001": manifest} volume := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { mu.Lock() defer mu.Unlock() @@ -103,8 +97,38 @@ func TestCopyChunkManifestPreservesOverwrites(t *testing.T) { if ctx.Err() != nil { t.Fatal(ctx.Err()) } - store := newRenameTestStore() + fs := &FilerServer{filer: newRenameTestFiler(t, store), option: &FilerOption{}, grpcDialOption: dialOption, + entryLockTable: util.NewLockTable[util.FullPath]()} + fs.filer.MasterClient = mc + return fs, store, mc +} + +func copyEntryThroughHandler(t *testing.T, ctx context.Context, fs *FilerServer, src, dst string) *filer.Entry { + t.Helper() + rec := httptest.NewRecorder() + fs.PostHandler(rec, httptest.NewRequest(http.MethodPost, dst+"?cp.from="+src, http.NoBody).WithContext(ctx), 0) + if rec.Code != http.StatusNoContent { + t.Fatalf("copy: status %d, body %s", rec.Code, rec.Body.String()) + } + entry, err := fs.filer.FindEntry(ctx, util.FullPath(dst)) + if err != nil { + t.Fatal(err) + } + return entry +} + +func TestCopyChunkManifestPreservesOverwrites(t *testing.T) { + ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) + defer cancel() + old := &filer_pb.FileChunk{FileId: "1,0100000001", Size: 7, ModifiedTsNs: 1} + filer_pb.BeforeEntrySerialization([]*filer_pb.FileChunk{old}) + manifest, err := proto.Marshal(&filer_pb.FileChunkManifest{Chunks: []*filer_pb.FileChunk{old}}) + if err != nil { + t.Fatal(err) + } + fs, store, mc := startChunkCopyFixture(t, ctx, map[string][]byte{"/1,0100000001": []byte("OLD old"), "/1,0200000001": []byte("NEW"), "/1,0300000001": manifest}) + src := newFileEntry("/src", 1) src.Chunks = []*filer_pb.FileChunk{ {FileId: "1,0300000001", Size: old.Size, ModifiedTsNs: 1, IsChunkManifest: true}, @@ -113,9 +137,6 @@ func TestCopyChunkManifestPreservesOverwrites(t *testing.T) { filer_pb.BeforeEntrySerialization(src.Chunks) filer_pb.AfterEntryDeserialization(src.Chunks) store.entries["/src"] = src - fs := &FilerServer{filer: newRenameTestFiler(t, store), option: &FilerOption{}, grpcDialOption: dialOption, - entryLockTable: util.NewLockTable[util.FullPath]()} - fs.filer.MasterClient = mc read := func(entry *filer.Entry) string { t.Helper() var buf bytes.Buffer @@ -127,16 +148,45 @@ func TestCopyChunkManifestPreservesOverwrites(t *testing.T) { if got := read(src); got != "NEW old" { t.Fatalf("source reads %q, want NEW old", got) } - rec := httptest.NewRecorder() - fs.PostHandler(rec, httptest.NewRequest(http.MethodPost, "/dst?cp.from=/src", http.NoBody).WithContext(ctx), 0) - if rec.Code != http.StatusNoContent { - t.Fatalf("copy: status %d, body %s", rec.Code, rec.Body.String()) - } - dst, err := fs.filer.FindEntry(ctx, "/dst") - if err != nil { - t.Fatal(err) - } - if got := read(dst); got != "NEW old" { + if got := read(copyEntryThroughHandler(t, ctx, fs, "/src", "/dst")); got != "NEW old" { t.Fatalf("copied file reads %q, want NEW old", got) } } + +func TestCopyChunkKeepsEncryption(t *testing.T) { + ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) + defer cancel() + plain := []byte(strings.Repeat("encrypted chunk data ", 20)) + compressed, err := util.GzipData(plain) + if err != nil { + t.Fatal(err) + } + cipherKey := util.GenCipherKey() + ciphertext, err := util.Encrypt(compressed, cipherKey) + if err != nil { + t.Fatal(err) + } + fs, store, mc := startChunkCopyFixture(t, ctx, map[string][]byte{"/1,0100000001": ciphertext}) + + src := newFileEntry("/src", 1) + src.Chunks = []*filer_pb.FileChunk{{FileId: "1,0100000001", Size: uint64(len(plain)), ModifiedTsNs: 1, + CipherKey: cipherKey, IsCompressed: true, SseType: filer_pb.SSEType_SSE_S3, SseMetadata: []byte("per-chunk")}} + filer_pb.BeforeEntrySerialization(src.Chunks) + filer_pb.AfterEntryDeserialization(src.Chunks) + store.entries["/src"] = src + + dst := copyEntryThroughHandler(t, ctx, fs, "/src", "/dst") + if len(dst.Chunks) != 1 { + t.Fatalf("copied %d chunks, want 1", len(dst.Chunks)) + } + if got := dst.Chunks[0]; got.SseType != filer_pb.SSEType_SSE_S3 || string(got.SseMetadata) != "per-chunk" { + t.Fatalf("copied chunk SSE %v %q, want SSE_S3 per-chunk", got.SseType, got.SseMetadata) + } + var buf bytes.Buffer + if err := filer.StreamContent(mc, &buf, dst.Chunks, 0, int64(len(plain))); err != nil { + t.Fatal(err) + } + if !bytes.Equal(buf.Bytes(), plain) { + t.Fatalf("copied file reads %q, want %q", buf.Bytes(), plain) + } +}