filer: keep chunk encryption on server-side copy (#13392)

* filer: reproduce overwritten bytes lost by server-side copy

Signed-off-by: Perfloop Agent <agent@perfloop.ai>

* filer: retain chunk modification time during server-side copy

Signed-off-by: Perfloop Agent <agent@perfloop.ai>

* filer: isolate the copy regression volume-location cache

Signed-off-by: Perfloop Agent <agent@perfloop.ai>

* filer: reuse fake master in chunk copy regression

Signed-off-by: Perfloop Agent <agent@perfloop.ai>

* filer: share the chunk copy test fixture

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

* filer: keep chunk encryption on server-side copy

The copy sent each needle with the chunk's clear size as its length, so a
chunk encrypted by the filer (ciphertext is longer) failed to copy. Chunks
also lost their cipher key, compression flag and S3 SSE metadata, which a
reader needs to decode the copied bytes. The upload now takes the stored
length and the copied chunk keeps those fields.

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

---------

Signed-off-by: Perfloop Agent <agent@perfloop.ai>
Co-authored-by: Perfloop Agent <agent@perfloop.ai>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
authored and GitHub committed 2026-12-04 11:30:21 -08:00
1 parent e9ea21340b
commit 56e95e5ebd
2 files changed
+83 -24

No files matched your search

@@ -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 != "" {
@@ -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)
}
}