filer: run scoped UpdateEntry checks before chunk preparation (#12965)

* filer: check scoped updates before preparing chunks

(cherry picked from commit cede961372)

* filer: recheck scoped WORM policy after chunk preparation

(cherry picked from commit 9d2e6c775c)

* filer: recheck scoped update authority after chunk preparation
This commit is contained in:
Carlos Leyva authored and GitHub committed 2026-11-25 12:50:41 -08:00
1 parent d95f5e67fb
commit c0f3182046
3 files changed
+130 -4

No files matched your search

@@ -0,0 +1,61 @@
package weed_server
import (
"context"
"testing"
"time"
"github.com/seaweedfs/seaweedfs/weed/filer"
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
"github.com/seaweedfs/seaweedfs/weed/util"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/proto"
)
func TestScopedUpdateRechecksExpiryAfterPreparation(t *testing.T) {
for _, transactional := range []bool{false, true} {
t.Run(map[bool]string{false: "plain store", true: "transactional store"}[transactional], func(t *testing.T) {
store := newRenameTestStore()
fs, interceptor := newScopeRetentionServer(t, store)
if transactional {
fs.filer = newRenameTestFiler(t, conflictRefusingStore{&txnRenameStore{renameTestStore: store}})
}
queue := &captureQueue{}
swapNotificationQueue(t, queue)
path := util.NewFullPath(aliceWS, "expiry")
stored := newFileEntry(string(path), 1)
stored.Chunks = []*filer_pb.FileChunk{written(storedChunk, 1)}
store.entries[string(path)] = stored
before := proto.Clone(stored.ToProtoEntry()).(*filer_pb.Entry)
replacement := proto.Clone(before).(*filer_pb.Entry)
replacement.Attributes.Mtime++
const manifestID = "4,0d04"
preparations := 0
err := scopedWormUnary(t, interceptor, "UpdateEntry", &filer_pb.UpdateEntryRequest{
Directory: aliceWS, Entry: replacement,
}, func(ctx context.Context, request any) (any, error) {
return fs.updateEntry(ctx, request.(*filer_pb.UpdateEntryRequest), string(path),
func(ctx context.Context, _ string, _ *filer.Entry, _ *filer_pb.Entry) ([]*filer_pb.FileChunk, []*filer_pb.FileChunk, error) {
preparations++
authority := ctx.Value(scopeWriteAuthorityKey{}).(*scopeWriteAuthority)
authority.sc.exp = time.Now().Add(-authority.sc.leeway - time.Second)
manifest := written(manifestID, 2)
manifest.IsChunkManifest = true
return []*filer_pb.FileChunk{manifest}, nil, nil
})
})
if status.Code(err) != codes.Unauthenticated || preparations != 1 {
t.Fatalf("update = %v, preparations = %d, want Unauthenticated after one preparation", err, preparations)
}
got, err := store.FindEntry(context.Background(), path)
if err != nil || !proto.Equal(got.ToProtoEntry(), before) {
t.Fatalf("expired update changed stored entry: got=%v err=%v", got, err)
}
if events := queue.snapshot(); len(events) != 0 {
t.Fatalf("expired update emitted %d events", len(events))
}
checkDeletions(t, fs.filer, manifestID)
})
}
}
+58
View File
@@ -5,6 +5,7 @@ import (
"testing"
"time"
"github.com/seaweedfs/seaweedfs/weed/filer"
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
"github.com/seaweedfs/seaweedfs/weed/util"
"google.golang.org/grpc"
@@ -319,3 +320,60 @@ func TestScopeWormRefusesAppend(t *testing.T) {
t.Fatalf("append changed WORM data: entry=%v error=%v", got, err)
}
}
func TestScopedWormUpdateRechecksAfterPreparation(t *testing.T) {
for _, transactional := range []bool{false, true} {
t.Run(map[bool]string{false: "plain store", true: "transactional store"}[transactional], func(t *testing.T) {
fs, scope, store := newScopedWormServer(t)
if transactional {
conf := fs.filer.FilerConf
fs.filer = newRenameTestFiler(t, conflictRefusingStore{&txnRenameStore{renameTestStore: store}})
fs.filer.FilerConf = conf
}
queue := &captureQueue{}
swapNotificationQueue(t, queue)
path := insertScopedWormEntry(t, store, "update", time.Now())
stored := store.entries[string(path)]
stored.Content = nil
stored.Chunks = []*filer_pb.FileChunk{written(storedChunk, 1)}
before := proto.Clone(stored.ToProtoEntry()).(*filer_pb.Entry)
replacement := proto.Clone(before).(*filer_pb.Entry)
replacement.Attributes.Mtime++
setRule := func(enabled bool) {
t.Helper()
if err := fs.filer.FilerConf.SetLocationConf(&filer_pb.FilerConf_PathConf{
LocationPrefix: aliceWS,
Worm: proto.Bool(enabled),
}); err != nil {
t.Fatalf("set WORM rule: %v", err)
}
}
setRule(false)
const manifestID = "4,0d04"
preparations := 0
err := scopedWormUnary(t, scope, "UpdateEntry", &filer_pb.UpdateEntryRequest{
Directory: aliceWS, Entry: replacement,
}, func(ctx context.Context, request any) (any, error) {
return fs.updateEntry(ctx, request.(*filer_pb.UpdateEntryRequest), string(path),
func(context.Context, string, *filer.Entry, *filer_pb.Entry) ([]*filer_pb.FileChunk, []*filer_pb.FileChunk, error) {
preparations++
setRule(true)
manifest := written(manifestID, 2)
manifest.IsChunkManifest = true
return []*filer_pb.FileChunk{manifest}, nil, nil
})
})
if status.Code(err) != codes.PermissionDenied || preparations != 1 {
t.Fatalf("update = %v, preparations = %d, want PermissionDenied after one preparation", err, preparations)
}
got, err := store.FindEntry(context.Background(), path)
if err != nil || !proto.Equal(got.ToProtoEntry(), before) {
t.Fatalf("refused update changed stored entry: got=%v err=%v", got, err)
}
if events := queue.snapshot(); len(events) != 0 {
t.Fatalf("refused update emitted %d events", len(events))
}
checkDeletions(t, fs.filer, manifestID)
})
}
}
+11 -4
View File
@@ -1044,10 +1044,6 @@ func (fs *FilerServer) updateEntry(ctx context.Context, req *filer_pb.UpdateEntr
if refusal != nil {
return &filer_pb.UpdateEntryResponse{}, refusal
}
chunks, garbage, err := prepareChunks(ctx, fullpath, entry, req.Entry)
if err != nil {
return &filer_pb.UpdateEntryResponse{}, fmt.Errorf("UpdateEntry cleanupChunks %s: %w", fullpath, err)
}
if err := fs.checkScopeWorm(ctx, string(lockPath)); err != nil {
return &filer_pb.UpdateEntryResponse{}, err
}
@@ -1059,6 +1055,11 @@ func (fs *FilerServer) updateEntry(ctx context.Context, req *filer_pb.UpdateEntr
return &filer_pb.UpdateEntryResponse{}, err
}
chunks, garbage, err := prepareChunks(ctx, fullpath, entry, req.Entry)
if err != nil {
return &filer_pb.UpdateEntryResponse{}, fmt.Errorf("UpdateEntry cleanupChunks %s: %w", fullpath, err)
}
var newEntry *filer.Entry
var stamp int64
var unchanged bool
@@ -1072,6 +1073,12 @@ func (fs *FilerServer) updateEntry(ctx context.Context, req *filer_pb.UpdateEntr
return err
}
}
if err := fs.checkScopeWorm(ctx, string(lockPath)); err != nil {
return err
}
if err := checkScopeRetainedData(ctx, string(lockPath), entry, req.Entry, req.GetChunkAuth(), false); err != nil {
return err
}
newEntry = filer.FromPbEntry(req.Directory, req.Entry)
newEntry.Chunks = chunks
// Remote storage manages expiry for remote entries.