Files
seaweedfs/weed/server/filer_grpc_scope_ancestors_test.go
T
430ca2b585 filer: path- and method-scoped gRPC tokens (scope enforcement, fail closed) (#12904)
* filer: path- and method-scoped tokens on the filesystem gRPC service

* filer: close scoped admission and compatibility gaps

* filer: test subscription key ambiguity and retained write success

* filer: assert verified peer chains survive subscription fallback

* filer: keep a hard link's count and chunks together under a scoped token

A scoped write may keep a hard link the target already has, but the link
count it sends was stored as is, and its chunks could stay on the name
with the link dropped. Either let a scoped token delete its own name and
take the chunks of a name outside its grants with it: the forged count
reaches zero, or the detached name owns the shared chunks.

The handler recheck now refuses a scoped write that changes a stored
link's count or keeps its chunks without the link. Dropping the link
together with the data, and appending to a linked file, are unchanged.

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

* filer: leave subscriptions with a non-scope bearer to the existing verifier

The stream interceptor refused a SubscribeMetadata or SubscribeLocalMetadata
call whose bearer does not verify under the filer key unless the bearer was
a subscription token, and it checked that token with the peer stripped. With
no subscription key, or on an mTLS peer, the existing verifier admits such a
call, so filer.sync sending another cluster's admin token stopped.

When the bearer is no filer scope, the subscription verifier now decides on
the original context, as before scoped tokens. A scoped token still reaches
the refusal for scoped subscriptions, and malformed metadata is still refused.

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

* filer: refuse scoped writes that would make ancestors outside the grant

A scoped create, append or rename checked only its target. The filer then
creates the target's missing ancestors up to the first one it finds, and
turns that one into a directory if it is a file, dropping its ttl. A token
granted /data/proj/ws could so promote the file /data/proj, or create /q and
/q/r for a grant /q/r/ws.

The scope now walks the same ancestors before admitting the write and
refuses one outside the write grants that is missing or not a directory. A
create with skip_check_parent_directory makes no ancestors and is not
walked.

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

* filer: require write grants for scoped lock mutations

* filer: document scoped signatures and read-only token upgrades

* filer: enforce WORM retention for scoped mutations

* filer: refuse scoped writes through shared hard links

* filer: drop series notes from scope comments

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

* filer: refuse scoped subtree mutations across WORM rules

---------

Co-authored-by: Chris Lu <chrislusf@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-23 08:45:34 -08:00

89 lines
3.9 KiB
Go

package weed_server
import (
"context"
"os"
"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"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
)
func scopedCreate(t *testing.T, fs *FilerServer, scope *filerGrpcScope, grant string, req *filer_pb.CreateEntryRequest) error {
t.Helper()
ctx := metadata.NewIncomingContext(context.Background(), metadata.Pairs(
"authorization", "Bearer "+scopedToken("ancestors", time.Hour, []string{grant}, nil)))
_, err := scope.unary(ctx, req, &grpc.UnaryServerInfo{
FullMethod: scopeFilerMethodPrefix + "CreateEntry",
}, func(handlerCtx context.Context, handlerReq any) (any, error) {
return fs.CreateEntry(handlerCtx, handlerReq.(*filer_pb.CreateEntryRequest))
})
return err
}
func insertScopeTestEntry(t *testing.T, fs *FilerServer, p string, mode os.FileMode, ttlSec int32) {
t.Helper()
now := time.Now()
e := &filer.Entry{FullPath: util.FullPath(p), Attr: filer.Attr{Mode: mode, TtlSec: ttlSec, Crtime: now, Mtime: now}}
if err := fs.filer.Store.InsertEntry(context.Background(), e); err != nil {
t.Fatalf("insert %s: %v", p, err)
}
}
func TestScopeCreateCannotMakeAncestorsOutsideGrant(t *testing.T) {
t.Run("file ancestor is not promoted", func(t *testing.T) {
fs, scope := newScopeRetentionServer(t, newRenameTestStore())
insertScopeTestEntry(t, fs, "/data", os.ModeDir|0755, 0)
insertScopeTestEntry(t, fs, "/data/proj", 0644, 3600)
err := scopedCreate(t, fs, scope, "/data/proj/ws", &filer_pb.CreateEntryRequest{Directory: "/data/proj/ws", Entry: &filer_pb.Entry{Name: "x"}})
if status.Code(err) != codes.PermissionDenied {
t.Fatalf("create under a file ancestor err=%v, want PermissionDenied", err)
}
got, findErr := fs.filer.FindEntry(context.Background(), "/data/proj")
if findErr != nil || got.IsDirectory() || got.TtlSec != 3600 {
t.Fatalf("/data/proj = %+v err=%v, want the file with ttl 3600", got, findErr)
}
})
t.Run("missing ancestors are not created", func(t *testing.T) {
fs, scope := newScopeRetentionServer(t, newRenameTestStore())
err := scopedCreate(t, fs, scope, "/q/r/ws", &filer_pb.CreateEntryRequest{Directory: "/q/r/ws", Entry: &filer_pb.Entry{Name: "x"}})
if status.Code(err) != codes.PermissionDenied {
t.Fatalf("create under missing ancestors err=%v, want PermissionDenied", err)
}
if _, findErr := fs.filer.FindEntry(context.Background(), "/q"); findErr != filer_pb.ErrNotFound {
t.Fatalf("/q lookup err=%v, want not found", findErr)
}
})
t.Run("missing ancestors inside the grant are created", func(t *testing.T) {
fs, scope := newScopeRetentionServer(t, newRenameTestStore())
insertScopeTestEntry(t, fs, "/data", os.ModeDir|0755, 0)
insertScopeTestEntry(t, fs, "/data/proj", os.ModeDir|0755, 0)
err := scopedCreate(t, fs, scope, "/data/proj/ws", &filer_pb.CreateEntryRequest{Directory: "/data/proj/ws/a", Entry: &filer_pb.Entry{Name: "x"}})
if err != nil {
t.Fatalf("create with in-grant ancestors missing: %v", err)
}
if got, findErr := fs.filer.FindEntry(context.Background(), "/data/proj/ws/a"); findErr != nil || !got.IsDirectory() {
t.Fatalf("/data/proj/ws/a = %+v err=%v, want a directory", got, findErr)
}
})
}
func TestScopeRenameCannotMakeAncestorsOutsideGrant(t *testing.T) {
h := newScopeHarness(t)
c := h.client(scopedToken("alice", time.Hour, []string{aliceWS, "/q/r/ws"}, nil))
h.fake.putDir(aliceWS)
_, err := c.AtomicRenameEntry(scopeCtx(t), &filer_pb.AtomicRenameEntryRequest{
OldDirectory: aliceWS, OldName: "old", NewDirectory: "/q/r/ws", NewName: "new",
})
expectScopeCode(t, "rename under missing ancestors", err, codes.PermissionDenied)
if got := len(h.fake.received("AtomicRenameEntry")); got != 0 {
t.Fatalf("refused rename reached the handler %d times", got)
}
}