mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-04 18:17:16 +01:00
* filer: require bearer tokens for configured gRPC calls * filer: authenticate owned peer RPCs with per-call admin tokens * filer: retain scoped forwarding refusals with required authentication * filer: preserve tokenless HTTP forwarding outside require mode * filer: honor clock tolerance for gRPC admin fallback * filer: preserve caller token on lock owner forwards * filer: forward caller auth on target ping * filer: authenticate startup peer digest checks * filer: verify required subscription bearers independently of mTLS * filer: assert exact caller bearer on forwarded ping * filer: sign peer tokens with the gRPC verification key peerContext signed filer-owned RPCs with jwt.filer_signing.key only. With grpc_key as the only key and require-mode on, peer calls went out tokenless and were refused. With both keys set, peers that verify with grpc_key and lack the shared-key admin fallback refused the shared-key token even with require-mode off. Sign with the key filesystem gRPC verifies, and keep the shared key for metadata subscriptions, whose verifier accepts admin tokens signed only with it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * filer: keep a subscription token's scope over mTLS in require-mode Require-mode admitted a dedicated subscription bearer without the mTLS shortcut, but the handler re-checked it with the certificate, which granted the full stream past the token's prefixes and expiry. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * filer: delete uploaded chunks when the write owner refuses authentication A forwarded HTTP append or offset write that the owner refused as Unauthenticated kept its new chunks as if the outcome were unknown, orphaning them. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * filer: hand the admitted subscription scope to the handler The handler re-verified a dedicated subscription token after reading the request. If the token expired in between, an mTLS peer fell back to the certificate and got an unrestricted stream. The interceptor now passes the scope it admitted, and the handler refuses it once expired. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * filer: accept grpc_key admin tokens on metadata subscriptions A filer with only grpc_key signs peer subscriptions with it, but the subscription verifier checked admin tokens against the shared key only, so with a subscribe key set the aggregator's peer streams were refused. An unscoped grpc_key token already has full filer gRPC access. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * filer: delete the generated manifest when the write owner refuses authentication A forwarded HTTP append that crossed the manifest threshold left the manifest blob it had uploaded orphaned when the owner refused the call as Unauthenticated. The blob alone is deleted, since the manifest also lists committed chunks. 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>
204 lines
7.2 KiB
Go
204 lines
7.2 KiB
Go
package weed_server
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
jwt "github.com/golang-jwt/jwt/v5"
|
|
"github.com/seaweedfs/seaweedfs/weed/cluster"
|
|
"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{Target: "127.0.0.1:1", TargetType: cluster.FilerType})
|
|
return err
|
|
})
|
|
pingCalls := h.fake.received("Ping")
|
|
if len(pingCalls) != 1 {
|
|
t.Fatalf("method-only Ping reached handler %d times, want 1", len(pingCalls))
|
|
}
|
|
pingReq, ok := pingCalls[0].(*filer_pb.PingRequest)
|
|
if !ok || pingReq.GetTarget() != "" || pingReq.GetTargetType() != "" {
|
|
t.Fatalf("scoped Ping forwarded target %q/%q, want empty target", pingReq.GetTarget(), pingReq.GetTargetType())
|
|
}
|
|
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 TestScopeAssignVolumeAllowedAndMountRegisterRefused(t *testing.T) {
|
|
h := newScopeHarness(t)
|
|
c := h.client(scopedToken("alice", time.Hour, []string{aliceWS}, nil))
|
|
|
|
expectScopeForwarded(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 TestScopeSubscriptionMethodsRequireMethodGrant(t *testing.T) {
|
|
h := newScopeHarness(t)
|
|
c := h.client(methodScopedToken(t, "alice", []string{aliceWS}, []string{"ListEntries"}))
|
|
|
|
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)
|
|
}
|
|
}
|