Files
seaweedfs/weed/server/filer_grpc_scope_entry_equal_test.go
T
39675b2faf filer: hide remote rename source paths from scoped responses (#13010)
* filer: redact remote rename source from scoped responses

* Preserve scoped remote rename markers

* Consolidate scoped marker replacement coverage

* Refuse scoped whole-entry guards during remote rename

* Keep coded create refusals for guarded scoped writes

* Make scoped subscription batch coverage deterministic

* filer: refuse scoped conditions that compare the remote rename source

IF_EXTENDED_NOT_EQUAL clauses and ExpectedExtended on Create, Update
and DeleteEntry could still compare a guess against the hidden marker,
so a scoped client could confirm the source path it redacts.

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

* filer: refuse scoped condition gates on the remote rename source

An IF_EXTENDED_TIME_ELAPSED gate applies its time check only when the
gate value matches, so a gate on the marker also confirms a guess.

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

* filer: keep a pending rename source out of remote copy errors

A failed copy of a pending remote-only rename named the marker's source
location, which the backend error repeats, and rename RPCs return that error
to scoped callers. Log the full error and return one naming only the paths
the caller supplied.

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

* filer: let a scoped local overwrite end a pending remote rename

Scoped replacements kept the hidden rename source marker even though a
scoped entry can never stay remote-only, so the marker was stale on the
local copy and every later scoped IF_ENTRY_EQUAL write was refused. Drop the
preservation and let the overwrite clear the marker as other local writes do.

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

* codespell: ignore poligon only inside URLs

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

---------

Co-authored-by: Chris Lu <chrislusf@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-26 12:31:02 -08:00

384 lines
15 KiB
Go

package weed_server
import (
"context"
"errors"
"fmt"
"net"
"strings"
"testing"
"time"
"github.com/seaweedfs/seaweedfs/weed/filer"
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
"github.com/seaweedfs/seaweedfs/weed/security"
"github.com/seaweedfs/seaweedfs/weed/util"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/proto"
)
func scopedEntryEqualCondition(expected *filer_pb.Entry, equalFirst bool) *filer_pb.WriteCondition {
equal := &filer_pb.WriteCondition_Clause{
Kind: filer_pb.WriteCondition_IF_ENTRY_EQUAL,
ExpectedEntry: proto.Clone(expected).(*filer_pb.Entry),
}
exists := &filer_pb.WriteCondition_Clause{Kind: filer_pb.WriteCondition_IF_EXISTS}
clauses := []*filer_pb.WriteCondition_Clause{exists, equal}
if equalFirst {
clauses[0], clauses[1] = clauses[1], clauses[0]
}
return &filer_pb.WriteCondition{Clauses: clauses}
}
func runScopedEntryEqualCondition(t *testing.T, method string, ctx context.Context, initial *filer.Entry, request *filer_pb.Entry, condition *filer_pb.WriteCondition, beforeHandler ...func(*filer.Entry)) (error, bool, *filer.Entry, int) {
t.Helper()
store := newRenameTestStore()
path := string(initial.FullPath)
store.entries[path] = initial
queue := &captureQueue{}
swapNotificationQueue(t, queue)
fs, scope := newScopeRetentionServer(t, store)
var req any
switch method {
case "CreateEntry":
req = &filer_pb.CreateEntryRequest{
Directory: aliceWS,
Entry: request,
Condition: condition,
SkipCheckParentDirectory: true,
}
case "UpdateEntry":
req = &filer_pb.UpdateEntryRequest{
Directory: aliceWS,
Entry: request,
Condition: condition,
}
default:
t.Fatalf("unexpected method %q", method)
}
called := false
response, err := scope.unary(ctx, req, &grpc.UnaryServerInfo{FullMethod: scopeFilerMethodPrefix + method}, func(handlerCtx context.Context, handlerReq any) (any, error) {
called = true
for _, update := range beforeHandler {
update(store.entries[path])
}
switch method {
case "CreateEntry":
return fs.CreateEntry(handlerCtx, handlerReq.(*filer_pb.CreateEntryRequest))
case "UpdateEntry":
return fs.UpdateEntry(handlerCtx, handlerReq.(*filer_pb.UpdateEntryRequest))
default:
t.Fatalf("unexpected method %q", method)
return nil, nil
}
})
if err == nil && method == "CreateEntry" {
cr := response.(*filer_pb.CreateEntryResponse)
if cr.GetErrorCode() != filer_pb.FilerError_OK {
if cr.GetErrorCode() != filer_pb.FilerError_PRECONDITION_FAILED {
t.Fatalf("unexpected create refusal: %v", cr)
}
err = fmt.Errorf("%s: %w", cr.GetError(), filer_pb.ErrPreconditionFailed)
}
}
got, findErr := store.FindEntry(context.Background(), util.FullPath(path))
if findErr != nil {
t.Fatalf("find entry after %s: %v", method, findErr)
}
fs.filer.FileIdDeletionQueue.Consume(func(ids []string) {
if len(ids) != 0 {
t.Errorf("%s queued unexpected chunk deletions: %v", method, ids)
}
})
return err, called, got, len(queue.snapshot())
}
func pendingRemoteRenameEntry(name string) *filer.Entry {
entry := newFileEntry(scopeJoin(aliceWS, name), 1)
entry.Remote = &filer_pb.RemoteEntry{
StorageName: "s3",
LastLocalSyncTsNs: 123,
RemoteETag: "etag",
RemoteMtime: 456,
RemoteSize: 789,
}
entry.Extended = map[string][]byte{
"visible-tag": []byte("kept"),
filer.ExtRemoteRenameSourceKey: []byte("/buckets/t1/source"),
}
return entry
}
func TestScopedEntryEqualRefusesHiddenRemoteRenameMarker(t *testing.T) {
for _, method := range []string{"CreateEntry", "UpdateEntry"} {
for _, equalFirst := range []bool{true, false} {
label := "second"
if equalFirst {
label = "first"
}
t.Run(method+"/equal-"+label, func(t *testing.T) {
initial := pendingRemoteRenameEntry("conditional-" + method)
before := proto.Clone(initial.ToProtoEntry()).(*filer_pb.Entry)
request := proto.Clone(before).(*filer_pb.Entry)
delete(request.Extended, filer.ExtRemoteRenameSourceKey)
request.RemoteEntry = nil
err, called, got, events := runScopedEntryEqualCondition(t, method, scopedRetentionContext(), initial, request, scopedEntryEqualCondition(request, equalFirst))
if (method == "CreateEntry" && !errors.Is(err, filer_pb.ErrPreconditionFailed)) || (method == "UpdateEntry" && status.Code(err) != codes.FailedPrecondition) {
t.Fatalf("%s error = %v, want FailedPrecondition", method, err)
}
if !strings.Contains(err.Error(), "scoped IF_ENTRY_EQUAL") {
t.Fatalf("%s error = %v, want a clear scoped IF_ENTRY_EQUAL refusal", method, err)
}
if strings.Contains(err.Error(), "/buckets/t1/source") {
t.Fatalf("%s error exposed the hidden source path: %v", method, err)
}
if called != (method == "CreateEntry") {
t.Fatalf("%s handler called=%v, want Create refusal under lock and Update admission refusal", method, called)
}
if !proto.Equal(got.ToProtoEntry(), before) {
t.Fatalf("%s changed the stored entry: got=%v want=%v", method, got.ToProtoEntry(), before)
}
if events != 0 {
t.Fatalf("%s emitted %d metadata events on refusal", method, events)
}
})
}
}
}
func TestScopedEntryEqualWithoutHiddenMarkerIsAllowed(t *testing.T) {
for _, method := range []string{"CreateEntry", "UpdateEntry"} {
t.Run(method, func(t *testing.T) {
initial := newFileEntry(scopeJoin(aliceWS, "conditional-"+method), 1)
request := proto.Clone(initial.ToProtoEntry()).(*filer_pb.Entry)
err, called, _, _ := runScopedEntryEqualCondition(t, method, scopedRetentionContext(), initial, request, scopedEntryEqualCondition(request, true))
if err != nil || !called {
t.Fatalf("%s error=%v called=%v, want allowed scoped conditional write", method, err, called)
}
})
}
}
func TestUnscopedEntryEqualWithHiddenMarkerIsUnchanged(t *testing.T) {
contexts := []struct {
name string
ctx context.Context
}{
{name: "tokenless", ctx: context.Background()},
{name: "unscoped filer token", ctx: metadata.NewIncomingContext(context.Background(), metadata.Pairs(
"authorization", "Bearer "+unscopedToken(),
))},
}
for _, method := range []string{"CreateEntry", "UpdateEntry"} {
for _, tc := range contexts {
t.Run(method+"/"+tc.name, func(t *testing.T) {
initial := pendingRemoteRenameEntry(tc.name + "-" + method)
expected := initial.ToProtoEntry()
err, called, _, _ := runScopedEntryEqualCondition(t, method, tc.ctx, initial, expected, scopedEntryEqualCondition(expected, true))
if err != nil || !called {
t.Fatalf("%s error=%v called=%v, want unscoped conditional write unchanged", method, err, called)
}
})
}
}
}
func TestScopedEntryEqualRechecksPendingRenameUnderLock(t *testing.T) {
for _, method := range []string{"CreateEntry", "UpdateEntry"} {
t.Run(method, func(t *testing.T) {
initial := pendingRemoteRenameEntry("late-" + method)
expected := proto.Clone(initial.ToProtoEntry()).(*filer_pb.Entry)
delete(initial.Extended, filer.ExtRemoteRenameSourceKey)
request := proto.Clone(initial.ToProtoEntry()).(*filer_pb.Entry)
request.RemoteEntry = nil
request.Content = []byte("replacement")
err, called, got, events := runScopedEntryEqualCondition(t, method, scopedRetentionContext(), initial, request, scopedEntryEqualCondition(expected, true), func(current *filer.Entry) {
current.Extended[filer.ExtRemoteRenameSourceKey] = []byte("/buckets/t1/source")
})
if !called || (method == "CreateEntry" && !errors.Is(err, filer_pb.ErrPreconditionFailed)) || (method == "UpdateEntry" && status.Code(err) != codes.FailedPrecondition) || !strings.Contains(err.Error(), "scoped IF_ENTRY_EQUAL") {
t.Fatalf("%s error=%v called=%v, want locked FailedPrecondition refusal", method, err, called)
}
if !proto.Equal(got.ToProtoEntry(), expected) {
t.Fatalf("%s changed stored entry on locked refusal: got=%v want=%v", method, got.ToProtoEntry(), expected)
}
if events != 0 {
t.Fatalf("%s emitted %d metadata events on locked refusal", method, events)
}
})
}
}
type pendingRenameCreateServer struct {
*FilerServer
beforeCreate func()
}
func (s *pendingRenameCreateServer) CreateEntry(ctx context.Context, req *filer_pb.CreateEntryRequest) (*filer_pb.CreateEntryResponse, error) {
if s.beforeCreate != nil {
s.beforeCreate()
}
return s.FilerServer.CreateEntry(ctx, req)
}
func TestScopedEntryEqualCreateClientRecognizesDefiniteRefusal(t *testing.T) {
for _, late := range []bool{false, true} {
name := "stored marker"
if late {
name = "marker after admission"
}
t.Run(name, func(t *testing.T) {
initial := pendingRemoteRenameEntry("client-refusal")
expected := proto.Clone(initial.ToProtoEntry()).(*filer_pb.Entry)
if late {
delete(initial.Extended, filer.ExtRemoteRenameSourceKey)
}
request := proto.Clone(initial.ToProtoEntry()).(*filer_pb.Entry)
delete(request.Extended, filer.ExtRemoteRenameSourceKey)
request.RemoteEntry = nil
store := newRenameTestStore()
store.entries[string(initial.FullPath)] = initial
queue := &captureQueue{}
swapNotificationQueue(t, queue)
fs, scope := newScopeRetentionServer(t, store)
impl := &pendingRenameCreateServer{FilerServer: fs}
if late {
impl.beforeCreate = func() { initial.Extended[filer.ExtRemoteRenameSourceKey] = []byte("/buckets/t1/source") }
}
listener, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
server := grpc.NewServer(scope.ServerOptions()...)
filer_pb.RegisterSeaweedFilerServer(server, impl)
go func() { _ = server.Serve(listener) }()
t.Cleanup(func() { server.Stop(); _ = listener.Close() })
conn, err := grpc.NewClient("passthrough:///"+listener.Addr().String(), grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = conn.Close() })
auth, _ := metadata.FromIncomingContext(scopedRetentionContext())
ctx, cancel := context.WithTimeout(metadata.NewOutgoingContext(context.Background(), auth.Copy()), 5*time.Second)
defer cancel()
client := filer_pb.NewSeaweedFilerClient(conn)
req := &filer_pb.CreateEntryRequest{Directory: aliceWS, Entry: request, Condition: scopedEntryEqualCondition(expected, true), SkipCheckParentDirectory: true}
_, err = filer_pb.CreateEntryWithResponse(ctx, client, req)
if !errors.Is(err, filer_pb.ErrPreconditionFailed) || !filer_pb.IsFilerVerdict(err) {
t.Fatalf("create client error = %v, want definite precondition refusal", err)
}
raw, err := client.CreateEntry(ctx, req)
if err != nil || raw.GetErrorCode() != filer_pb.FilerError_PRECONDITION_FAILED || !strings.Contains(raw.GetError(), "scoped IF_ENTRY_EQUAL") {
t.Fatalf("raw create response=%v error=%v, want clear coded precondition refusal", raw, err)
}
stream, err := client.StreamMutateEntry(ctx)
if err != nil {
t.Fatal(err)
}
if err := stream.Send(&filer_pb.StreamMutateEntryRequest{RequestId: 9, Request: &filer_pb.StreamMutateEntryRequest_CreateRequest{CreateRequest: req}}); err != nil {
t.Fatal(err)
}
if err := stream.CloseSend(); err != nil {
t.Fatal(err)
}
streamed, err := stream.Recv()
if err != nil || streamed.GetRequestId() != 9 || streamed.GetCreateResponse().GetErrorCode() != filer_pb.FilerError_PRECONDITION_FAILED || !strings.Contains(streamed.GetCreateResponse().GetError(), "scoped IF_ENTRY_EQUAL") {
t.Fatalf("streamed create response=%v error=%v, want nested coded refusal", streamed, err)
}
got, err := store.FindEntry(context.Background(), initial.FullPath)
if err != nil || !proto.Equal(got.ToProtoEntry(), expected) {
t.Fatalf("stored entry changed: got=%v error=%v", got, err)
}
if events := len(queue.snapshot()); events != 0 {
t.Fatalf("refusal emitted %d events", events)
}
})
}
}
func TestScopedLocalOverwriteEndsPendingRemoteRename(t *testing.T) {
const dir, name = "/buckets/b/dst", "a.jpg"
for _, method := range []string{"CreateEntry", "UpdateEntry"} {
t.Run(method, func(t *testing.T) {
client := copyingRenameRemoteClient{&renameRemoteClient{copyErr: errors.New("copy refused")}}
server, store := newRenameRemoteTestServer(t, client)
store.entries["/buckets/b/src/a.jpg"] = remoteOnlyRenameEntry("/buckets/b/src/a.jpg", 101)
store.entries[dir+"/"+name] = remoteOnlyRenameEntry(dir+"/"+name, 202)
if err := renameFile(server, "/buckets/b/src/a.jpg", dir+"/"+name); err != nil {
t.Fatalf("rename: %v", err)
}
pending, err := store.FindEntry(context.Background(), dir+"/"+name)
if err != nil || string(pending.Extended[filer.ExtRemoteRenameSourceKey]) != "/buckets/b/src/a.jpg" {
t.Fatalf("pending rename entry=%v err=%v, want the source marker", pending, err)
}
scope := newFilerGrpcScope(scopeConfig{
signingKey: func() security.SigningKey { return security.SigningKey(scopeTestKey) },
}, server)
ctx := metadata.NewIncomingContext(context.Background(), metadata.Pairs(
"authorization", "Bearer "+scopedToken("overwrite", time.Hour, []string{dir}, nil),
))
call := func(method string, req any) (any, error) {
return scope.unary(ctx, req, &grpc.UnaryServerInfo{FullMethod: scopeFilerMethodPrefix + method}, func(handlerCtx context.Context, handlerReq any) (any, error) {
switch r := handlerReq.(type) {
case *filer_pb.CreateEntryRequest:
return server.CreateEntry(handlerCtx, r)
case *filer_pb.UpdateEntryRequest:
return server.UpdateEntry(handlerCtx, r)
case *filer_pb.LookupDirectoryEntryRequest:
return server.LookupDirectoryEntry(handlerCtx, r)
}
return nil, fmt.Errorf("unexpected request %T", handlerReq)
})
}
local := &filer_pb.Entry{Name: name, Attributes: &filer_pb.FuseAttributes{FileMode: 0644, FileSize: 5}, Content: []byte("local")}
if method == "CreateEntry" {
resp, err := call(method, &filer_pb.CreateEntryRequest{Directory: dir, Entry: local})
if err != nil || resp.(*filer_pb.CreateEntryResponse).GetError() != "" {
t.Fatalf("local overwrite: resp=%v err=%v", resp, err)
}
} else if _, err := call(method, &filer_pb.UpdateEntryRequest{Directory: dir, Entry: local}); err != nil {
t.Fatalf("local overwrite: %v", err)
}
overwritten, err := store.FindEntry(context.Background(), dir+"/"+name)
if err != nil {
t.Fatalf("find overwritten entry: %v", err)
}
if _, marked := overwritten.Extended[filer.ExtRemoteRenameSourceKey]; marked {
t.Fatalf("local overwrite kept the pending rename marker: %v", overwritten.ToProtoEntry())
}
looked, err := call("LookupDirectoryEntry", &filer_pb.LookupDirectoryEntryRequest{Directory: dir, Name: name})
if err != nil {
t.Fatalf("scoped lookup: %v", err)
}
current := looked.(*filer_pb.LookupDirectoryEntryResponse).GetEntry()
next := proto.Clone(current).(*filer_pb.Entry)
next.Content = []byte("again")
condition := &filer_pb.WriteCondition{Clauses: []*filer_pb.WriteCondition_Clause{{
Kind: filer_pb.WriteCondition_IF_ENTRY_EQUAL,
ExpectedEntry: current,
}}}
if _, err := call("UpdateEntry", &filer_pb.UpdateEntryRequest{Directory: dir, Entry: next, Condition: condition}); err != nil {
t.Fatalf("guarded write after the local overwrite: %v", err)
}
got, err := store.FindEntry(context.Background(), dir+"/"+name)
if err != nil {
t.Fatalf("find entry after guarded write: %v", err)
}
if string(got.Content) != "again" {
t.Fatalf("after guarded write content=%q, want again", got.Content)
}
})
}
}