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

137 lines
4.7 KiB
Go

package weed_server
import (
"context"
"testing"
"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 authMetadataContext(values ...string) context.Context {
if len(values) == 0 {
return context.Background()
}
md := metadata.MD{}
for _, value := range values {
md.Append("authorization", value)
}
return metadata.NewIncomingContext(context.Background(), md)
}
func TestScopeRejectsMalformedOrDuplicateAuthorizationWithSigningKey(t *testing.T) {
scope := newFilerGrpcScope(scopeConfig{
signingKey: func() security.SigningKey { return security.SigningKey(scopeTestKey) },
}, newScopeFakeFiler())
cases := []struct {
name string
ctx context.Context
}{
{name: "malformed scheme", ctx: authMetadataContext("Basic opaque")},
{name: "bare bearer", ctx: authMetadataContext("Bearer")},
{name: "empty bearer", ctx: authMetadataContext("Bearer ")},
{name: "duplicate valid", ctx: authMetadataContext("Bearer "+unscopedToken(), "Bearer "+unscopedToken())},
{name: "malformed plus valid", ctx: authMetadataContext("Basic opaque", "Bearer "+unscopedToken())},
}
for _, tc := range cases {
t.Run(tc.name+" unary", func(t *testing.T) {
called := false
_, err := scope.unary(tc.ctx, &filer_pb.PingRequest{}, &grpc.UnaryServerInfo{
FullMethod: filer_pb.SeaweedFiler_Ping_FullMethodName,
}, func(context.Context, any) (any, error) {
called = true
return &filer_pb.PingResponse{}, nil
})
if status.Code(err) != codes.Unauthenticated || called {
t.Fatalf("unary err=%v called=%v, want Unauthenticated before dispatch", err, called)
}
})
t.Run(tc.name+" stream", func(t *testing.T) {
called := false
stream := &scopeExpiryServerStream{ctx: tc.ctx}
err := scope.stream(nil, stream, &grpc.StreamServerInfo{
FullMethod: filer_pb.SeaweedFiler_ListEntries_FullMethodName,
}, func(any, grpc.ServerStream) error {
called = true
return nil
})
if status.Code(err) != codes.Unauthenticated || called {
t.Fatalf("stream err=%v called=%v, want Unauthenticated before dispatch", err, called)
}
})
}
}
func TestScopeAcceptsAbsentAuthorizationWithSigningKey(t *testing.T) {
scope := newFilerGrpcScope(scopeConfig{
signingKey: func() security.SigningKey { return security.SigningKey(scopeTestKey) },
}, newScopeFakeFiler())
calledUnary := false
if _, err := scope.unary(context.Background(), &filer_pb.PingRequest{}, &grpc.UnaryServerInfo{
FullMethod: filer_pb.SeaweedFiler_Ping_FullMethodName,
}, func(context.Context, any) (any, error) {
calledUnary = true
return &filer_pb.PingResponse{}, nil
}); err != nil || !calledUnary {
t.Fatalf("absent unary authorization err=%v called=%v, want passthrough", err, calledUnary)
}
calledStream := false
err := scope.stream(nil, &scopeExpiryServerStream{ctx: context.Background()}, &grpc.StreamServerInfo{
FullMethod: filer_pb.SeaweedFiler_ListEntries_FullMethodName,
}, func(any, grpc.ServerStream) error {
calledStream = true
return nil
})
if err != nil || !calledStream {
t.Fatalf("absent stream authorization err=%v called=%v, want passthrough", err, calledStream)
}
}
func TestScopeKeylessSigningBypassesAuthorizationParsing(t *testing.T) {
scope := newFilerGrpcScope(scopeConfig{
signingKey: func() security.SigningKey { return nil },
}, newScopeFakeFiler())
cases := []struct {
name string
ctx context.Context
}{
{name: "absent", ctx: context.Background()},
{name: "malformed", ctx: authMetadataContext("Basic opaque")},
{name: "bare bearer", ctx: authMetadataContext("Bearer")},
{name: "empty bearer", ctx: authMetadataContext("Bearer ")},
{name: "duplicate", ctx: authMetadataContext("Bearer one", "Bearer two")},
}
for _, tc := range cases {
t.Run(tc.name+" unary", func(t *testing.T) {
called := false
_, err := scope.unary(tc.ctx, &filer_pb.PingRequest{}, &grpc.UnaryServerInfo{
FullMethod: filer_pb.SeaweedFiler_Ping_FullMethodName,
}, func(context.Context, any) (any, error) {
called = true
return &filer_pb.PingResponse{}, nil
})
if err != nil || !called {
t.Fatalf("keyless unary err=%v called=%v, want passthrough", err, called)
}
})
t.Run(tc.name+" stream", func(t *testing.T) {
called := false
err := scope.stream(nil, &scopeExpiryServerStream{ctx: tc.ctx}, &grpc.StreamServerInfo{
FullMethod: filer_pb.SeaweedFiler_ListEntries_FullMethodName,
}, func(any, grpc.ServerStream) error {
called = true
return nil
})
if err != nil || !called {
t.Fatalf("keyless stream err=%v called=%v, want passthrough", err, called)
}
})
}
}