Files
seaweedfs/weed/server/filer_grpc_scope_methods_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

195 lines
6.7 KiB
Go

package weed_server
import (
"context"
"testing"
"time"
jwt "github.com/golang-jwt/jwt/v5"
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
"github.com/seaweedfs/seaweedfs/weed/security"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
)
func TestScopeAllowedMethodsRestrictCallsBeforeDispatch(t *testing.T) {
h := newScopeHarness(t)
token := methodScopedToken(t, "alice", []string{aliceWS}, []string{"LookupDirectoryEntry"})
c := h.client(token)
expectScopeForwarded(t, h, "LookupDirectoryEntry", "the listed method", func() error {
_, err := c.LookupDirectoryEntry(scopeCtx(t), &filer_pb.LookupDirectoryEntryRequest{Directory: aliceWS, Name: "file"})
return err
})
expectScopeDenied(t, h, "CreateEntry", "an unlisted method", func() error {
_, err := c.CreateEntry(scopeCtx(t), &filer_pb.CreateEntryRequest{Directory: aliceWS, Entry: entryWith("file")})
return err
})
}
func TestScopeMethodOnlyTokenAllowsListedMethodWithoutPathGrant(t *testing.T) {
h := newScopeHarness(t)
c := h.client(methodScopedToken(t, "alice", nil, []string{"Ping"}))
expectScopeForwarded(t, h, "Ping", "method-only Ping", func() error {
_, err := c.Ping(scopeCtx(t), &filer_pb.PingRequest{})
return err
})
expectScopeDenied(t, h, "LookupDirectoryEntry", "method-only lookup without a path grant", func() error {
_, err := c.LookupDirectoryEntry(scopeCtx(t), &filer_pb.LookupDirectoryEntryRequest{Directory: aliceWS, Name: "file"})
return err
})
}
func TestScopeRejectsInvalidScopedClaimsBeforeDispatch(t *testing.T) {
h := newScopeHarness(t)
future := jwt.NewNumericDate(time.Now().Add(time.Hour))
cases := []struct {
name string
token string
wantCode codes.Code
}{
{
name: "missing jti",
token: signedScopeClaims(t, scopeTestKey, security.SeaweedFilerClaims{
AllowedPrefixes: []string{aliceWS},
RegisteredClaims: jwt.RegisteredClaims{ExpiresAt: future},
}),
wantCode: codes.PermissionDenied,
},
{
name: "missing expiry",
token: signedScopeClaims(t, scopeTestKey, security.SeaweedFilerClaims{
AllowedPrefixes: []string{aliceWS},
RegisteredClaims: jwt.RegisteredClaims{ID: "lease-1"},
}),
wantCode: codes.PermissionDenied,
},
{
name: "expired",
token: signedScopeClaims(t, scopeTestKey, security.SeaweedFilerClaims{
AllowedPrefixes: []string{aliceWS},
RegisteredClaims: jwt.RegisteredClaims{ID: "lease-1", ExpiresAt: jwt.NewNumericDate(time.Now().Add(-time.Minute))},
}),
wantCode: codes.Unauthenticated,
},
{
name: "wrong signature",
token: signedScopeClaims(t, "wrong-scope-key", security.SeaweedFilerClaims{
AllowedPrefixes: []string{aliceWS},
RegisteredClaims: jwt.RegisteredClaims{ID: "lease-1", ExpiresAt: future},
}),
wantCode: codes.Unauthenticated,
},
{
name: "STS session",
token: signedScopeClaims(t, scopeTestKey, security.SeaweedFilerClaims{
AllowedPrefixes: []string{aliceWS},
SessionId: "session-1",
RegisteredClaims: jwt.RegisteredClaims{
ID: "lease-1",
ExpiresAt: future,
},
}),
wantCode: codes.Unauthenticated,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
before := len(h.fake.received("LookupDirectoryEntry"))
_, err := h.client(tc.token).LookupDirectoryEntry(scopeCtx(t), &filer_pb.LookupDirectoryEntryRequest{Directory: aliceWS, Name: "file"})
expectScopeCode(t, tc.name, err, tc.wantCode)
if got := len(h.fake.received("LookupDirectoryEntry")); got != before {
t.Fatalf("invalid token reached handler: %d calls before=%d", got, before)
}
})
}
}
func TestScopeAssignVolumeAndMountRegisterAreRefused(t *testing.T) {
h := newScopeHarness(t)
c := h.client(scopedToken("alice", time.Hour, []string{aliceWS}, nil))
expectScopeDenied(t, h, "AssignVolume", "AssignVolume for an in-scope path", func() error {
_, err := c.AssignVolume(scopeCtx(t), &filer_pb.AssignVolumeRequest{Path: aliceWS + "/new"})
return err
})
expectScopeDenied(t, h, "MountRegister", "MountRegister", func() error {
_, err := c.MountRegister(scopeCtx(t), &filer_pb.MountRegisterRequest{PeerAddr: "127.0.0.1:1"})
return err
})
if _, err := h.client(unscopedToken()).MountRegister(scopeCtx(t), &filer_pb.MountRegisterRequest{PeerAddr: "127.0.0.1:1"}); err != nil {
t.Fatalf("unscoped MountRegister: %v", err)
}
if got := len(h.fake.received("MountRegister")); got != 1 {
t.Fatalf("unscoped MountRegister reached the handler %d times, want 1", got)
}
}
func TestScopeSubscriptionMethodsAreRefused(t *testing.T) {
h := newScopeHarness(t)
c := h.client(scopedToken("alice", time.Hour, []string{aliceWS}, nil))
for _, tc := range []struct {
name string
method string
call func() error
}{
{
name: "SubscribeMetadata",
method: "SubscribeMetadata",
call: func() error {
stream, err := c.SubscribeMetadata(scopeCtx(t), &filer_pb.SubscribeMetadataRequest{PathPrefix: aliceWS})
if err == nil {
_, err = stream.Recv()
}
return err
},
},
{
name: "SubscribeLocalMetadata",
method: "SubscribeLocalMetadata",
call: func() error {
stream, err := c.SubscribeLocalMetadata(scopeCtx(t), &filer_pb.SubscribeMetadataRequest{PathPrefix: aliceWS})
if err == nil {
_, err = stream.Recv()
}
return err
},
},
} {
t.Run(tc.name, func(t *testing.T) {
expectScopeDenied(t, h, tc.method, tc.name, tc.call)
})
}
}
func TestScopeUnknownMethodAliasIsRefusedEvenForKnownRequestType(t *testing.T) {
h := newScopeHarness(t)
ctx := metadata.NewIncomingContext(context.Background(), metadata.Pairs("authorization", "Bearer "+scopedToken("alice", time.Hour, []string{aliceWS}, nil)))
called := false
_, err := h.scope.unary(ctx, &filer_pb.LookupDirectoryEntryRequest{Directory: aliceWS, Name: "file"}, &grpc.UnaryServerInfo{FullMethod: scopeFilerMethodPrefix + "LookupDirectoryEntryAlias"}, func(context.Context, any) (any, error) {
called = true
return &filer_pb.LookupDirectoryEntryResponse{}, nil
})
if status.Code(err) != codes.PermissionDenied || called {
t.Fatalf("unknown method alias got err=%v called=%v, want PermissionDenied before dispatch", err, called)
}
}
func TestScopeInterceptorsPassNonFilerMethodsThrough(t *testing.T) {
h := newScopeHarness(t)
called := false
ctx := metadata.NewIncomingContext(context.Background(), metadata.Pairs("authorization", "Bearer not-a-filer-token"))
resp, err := h.scope.unary(ctx, nil, &grpc.UnaryServerInfo{FullMethod: "/iam_pb.SeaweedIdentityAccessManagement/GetS3Config"}, func(context.Context, any) (any, error) {
called = true
return "ok", nil
})
if err != nil || resp != "ok" || !called {
t.Fatalf("non-filer method was not passed through: response=%v error=%v called=%v", resp, err, called)
}
}