Files
seaweedfs/weed/server/filer_grpc_scope_subscription_compat_test.go
T
a9fda6b50f filer clients: authenticate filer gRPC calls with per-call admin tokens (#13039)
* Add per-call filer authentication sources and pooled client wrappers

* Authenticate gateway filer calls with per-call admin tokens

* Authenticate shell admin and synchronization filer calls

* Authenticate workers MQ and remaining filer client paths

* Cache filer client signing keys and sign subscriptions with the shared key

Re-reading security.toml on every filer RPC cost about 125us against 8us for
signing. Keep the keys for up to a second, re-reading at once when a WEED_
override or the configuration directory changes. When the file exists but
cannot be read, keep the last keys read, else the startup keys, as
FilerAdminKeys does, so a process without a key stays tokenless. Metadata
subscriptions prefer the shared key, as filer peers do, because older filers
verify subscription admin tokens only with it.

* Authenticate admin-script, maintenance and filer.conf shell paths

Plugin worker admin scripts and master maintenance scripts build their own
shell options and carried no filer token source; fs.configure and
s3.bucket.quota.check read filer.conf without it.

* filer.cat: report a failed read; isolate key overrides in the admin-script test

filer.cat ignored the error from its filer call and exited successfully,
leaving a truncated -output file. Report the error and exit with failure.
The admin-script token test now clears the WEED_ signing-key overrides,
which take precedence over security.toml.

* security: refuse gRPC keys too short to sign; query engine uses client TLS

Under GODEBUG=fips140=only, hmac.New panics on keys under 14 bytes. The
startup check now covers jwt.filer_signing.grpc_key and grpc_previous_key,
and filer client token sources check the keys they read, so a reloaded or
per-endpoint security file with a short key is refused instead of
panicking when the next RPC signs.

The query engine's broker client dialed every master, filer and broker in
plaintext, so the admin token it now sends could cross the network in the
clear. It dials with the grpc.client TLS settings instead, and weed-sql
loads security.toml as weed-db does.

* security: per-endpoint admin tokens ignore WEED_ key overrides

FilerAdminTokenFromFile honored WEED_JWT_FILER_SIGNING_GRPC_KEY and
WEED_JWT_FILER_SIGNING_KEY. Those are process-wide, so filer.sync with
-a.security and -b.security signed both clusters' admin tokens with the
one key, handing each cluster a token valid on the other. A per-endpoint
file now supplies its own keys only.

* weed-sql, weed-db: refuse to start on an unreadable security.toml

Both only warned when security.toml was present but could not be read or
parsed. They then dialed without the grpc.client TLS settings while the
filer token source could still sign admin tokens from WEED_ environment
keys, so the tokens could travel in plaintext. They now exit with an
error that does not quote the file; a missing file is still fine.

* pb: take the filer token source in WithGrpcFilerClient and WithOneOfGrpcFilerClients

Fold WithGrpcFilerClientAuth and WithOneOfGrpcFilerClientsAuth into the
existing helpers instead of keeping two names for each. Callers without a
token source pass nil.

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

* util: return the read error from ReadConfiguration

Fold ReadConfigurationWithError into ReadConfiguration. The error is
returned and not logged, since parser errors can quote values; the
existing reload callers already warn when no file was read.

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

* security: keep the test-only filerAdminToken helper in the test

It had no production caller. The test helper now resolves keys through
filerClientKeysFrom, as the token sources do.

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

* util: read security.toml once in LoadSecurityConfigurationOrError

It validated the file, then merged it with a second read whose failure
was ignored and whose parser error was logged. It now merges with one
read and logs nothing from the parser. Viper's search also treats a
directory the process cannot search as holding no file; a candidate
that cannot be checked for lack of permission is now refused too.

* security: verify admin subscription tokens with the gRPC clock leeway

CheckGrpcMetadataSubscription checked admin tokens strictly, while unary
filer calls allow jwt.filer_signing.clock_leeway_seconds. With ten-second
client admin tokens, a subscriber whose clock trailed the filer's by more
than ten seconds was refused with a matching key. It now takes the leeway,
and the filer passes its configured one. The leeway check moves from the
server package to security.IsFilerAdminTokenWithLeeway so both use it.

* util: start without security.toml only when every candidate is confirmed absent

A candidate whose check fails for any reason other than not existing,
such as a symlink loop, is refused like one the process cannot search.

---------

Co-authored-by: Chris Lu <chrislusf@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-27 18:53:59 -08:00

341 lines
12 KiB
Go

package weed_server
import (
"context"
"crypto/tls"
"crypto/x509"
"testing"
"time"
jwt "github.com/golang-jwt/jwt/v5"
"github.com/seaweedfs/seaweedfs/weed/security"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/peer"
"google.golang.org/grpc/status"
)
const (
subscriptionFilerKey = "subscription-filer-key"
subscriptionPreviousFilerKey = "subscription-previous-filer-key"
subscriptionTokenKey = "subscription-token-key"
)
func subscriptionScopeServer(withSubscriptionVerifier bool) *filerGrpcScope {
return subscriptionScopeServerWithKeys(
withSubscriptionVerifier,
security.SigningKey(subscriptionFilerKey),
nil,
security.SigningKey(subscriptionTokenKey),
)
}
func subscriptionScopeServerWithKeys(withSubscriptionVerifier bool, filerKey, previousFilerKey, subscribeKey security.SigningKey) *filerGrpcScope {
cfg := scopeConfig{
verificationKeys: func() (security.SigningKey, security.SigningKey) {
return filerKey, previousFilerKey
},
}
if withSubscriptionVerifier {
cfg.subscriptionScope = func(ctx context.Context, method string) (*security.MetadataSubscriptionScope, error) {
return security.CheckGrpcMetadataSubscription(
ctx,
subscribeKey,
filerKey,
previousFilerKey,
method,
0,
)
}
}
return newFilerGrpcScope(cfg, newScopeFakeFiler())
}
func subscriptionStreamContext(token string) context.Context {
return metadata.NewIncomingContext(context.Background(), metadata.Pairs("authorization", "Bearer "+token))
}
func subscriptionStreamContextValues(values ...string) context.Context {
md := metadata.MD{}
for _, value := range values {
md.Append("authorization", value)
}
return metadata.NewIncomingContext(context.Background(), md)
}
func verifiedSubscriptionContext(token string) context.Context {
ctx := peer.NewContext(context.Background(), &peer.Peer{
AuthInfo: credentials.TLSInfo{State: tls.ConnectionState{VerifiedChains: [][]*x509.Certificate{{}}}},
})
return metadata.NewIncomingContext(ctx, metadata.Pairs("authorization", "Bearer "+token))
}
func callSubscriptionStream(t *testing.T, scope *filerGrpcScope, method string, ctx context.Context, handler func()) error {
t.Helper()
stream := &scopeExpiryServerStream{ctx: ctx}
return scope.stream(nil, stream, &grpc.StreamServerInfo{
FullMethod: scopeFilerMethodPrefix + method,
}, func(any, grpc.ServerStream) error {
handler()
return nil
})
}
func TestScopePreservesDedicatedSubscriptionTokensForBothMethods(t *testing.T) {
token := string(security.GenJwtForMetadataSubscription(
security.SigningKey(subscriptionTokenKey),
60,
[]string{aliceWS},
))
if token == "" {
t.Fatal("failed to mint dedicated subscription token")
}
scope := subscriptionScopeServer(true)
for _, method := range []string{security.MethodSubscribeMetadata, security.MethodSubscribeLocalMetadata} {
t.Run(method, func(t *testing.T) {
called := false
err := callSubscriptionStream(t, scope, method, subscriptionStreamContext(token), func() { called = true })
if err != nil || !called {
t.Fatalf("dedicated subscription token err=%v called=%v, want handler dispatch", err, called)
}
})
}
}
func TestScopeDedicatedSubscriptionTokenSurvivesVerifiedPeerFallback(t *testing.T) {
validToken := string(security.GenJwtForMetadataSubscription(security.SigningKey(subscriptionTokenKey), 60, []string{aliceWS}))
if validToken == "" {
t.Fatal("failed to mint dedicated subscription token")
}
wrongToken := string(security.GenJwtForMetadataSubscription(security.SigningKey("wrong-subscription-key"), 60, []string{aliceWS}))
if wrongToken == "" {
t.Fatal("failed to mint wrong-key subscription token")
}
t.Run("valid token retains original verified peer", func(t *testing.T) {
scope := subscriptionScopeServer(true)
stream := &scopeExpiryServerStream{ctx: verifiedSubscriptionContext(validToken)}
called := false
var handlerContext context.Context
err := scope.stream(nil, stream, &grpc.StreamServerInfo{
FullMethod: scopeFilerMethodPrefix + security.MethodSubscribeMetadata,
}, func(_ any, handlerStream grpc.ServerStream) error {
called = true
handlerContext = handlerStream.Context()
return nil
})
if err != nil || !called {
t.Fatalf("mTLS dedicated subscription err=%v called=%v, want handler dispatch", err, called)
}
peerInfo, ok := peer.FromContext(handlerContext)
if !ok || peerInfo == nil {
t.Fatal("handler lost the original peer context")
}
tlsInfo, ok := peerInfo.AuthInfo.(credentials.TLSInfo)
if !ok {
t.Fatalf("handler peer auth info = %T, want verified TLSInfo", peerInfo.AuthInfo)
}
if len(tlsInfo.State.VerifiedChains) == 0 {
t.Fatal("handler peer lost verified TLS chains")
}
})
t.Run("invalid token keeps the verified peer's access", func(t *testing.T) {
scope := subscriptionScopeServer(true)
called := false
err := callSubscriptionStream(t, scope, security.MethodSubscribeMetadata, verifiedSubscriptionContext(wrongToken), func() { called = true })
if err != nil || !called {
t.Fatalf("mTLS invalid dedicated subscription err=%v called=%v, want handler dispatch", err, called)
}
})
}
func TestScopeSubscriptionWithoutSubscribeKeyIgnoresForeignBearer(t *testing.T) {
foreign := string(security.GenJwtForFilerAdmin(security.SigningKey("other-cluster-filer-key"), 60))
if foreign == "" {
t.Fatal("failed to mint foreign admin token")
}
scope := subscriptionScopeServerWithKeys(true, security.SigningKey(subscriptionFilerKey), nil, nil)
for _, method := range []string{security.MethodSubscribeMetadata, security.MethodSubscribeLocalMetadata} {
t.Run(method, func(t *testing.T) {
called := false
err := callSubscriptionStream(t, scope, method, subscriptionStreamContext(foreign), func() { called = true })
if err != nil || !called {
t.Fatalf("foreign bearer without subscribe key err=%v called=%v, want handler dispatch", err, called)
}
})
}
}
func TestScopeAdmitsGeneralFilerScopesOnSubscriptionsWithOrWithoutVerifier(t *testing.T) {
token := string(security.GenJwtForFilerScope(
security.SigningKey(subscriptionFilerKey),
time.Minute,
"general-scope",
[]string{aliceWS},
nil,
))
if token == "" {
t.Fatal("failed to mint general filer scope token")
}
for _, withVerifier := range []bool{false, true} {
t.Run(map[bool]string{false: "subscription-auth-disabled", true: "subscription-auth-enabled"}[withVerifier], func(t *testing.T) {
scope := subscriptionScopeServer(withVerifier)
for _, method := range []string{security.MethodSubscribeMetadata, security.MethodSubscribeLocalMetadata} {
t.Run(method, func(t *testing.T) {
called := false
err := callSubscriptionStream(t, scope, method, subscriptionStreamContext(token), func() { called = true })
if err != nil || !called {
t.Fatalf("general scoped subscription err=%v called=%v, want scoped handler dispatch", err, called)
}
})
}
})
}
}
func subscriptionCompatibilityToken(t *testing.T, signingKey security.SigningKey, withJTI bool) string {
t.Helper()
if !withJTI {
token := string(security.GenJwtForMetadataSubscription(signingKey, 60, []string{aliceWS}))
if token == "" {
t.Fatal("failed to mint standard subscription token")
}
return token
}
claims := security.SeaweedFilerClaims{
AllowedPrefixes: []string{aliceWS},
AllowedMethods: []string{security.MethodSubscribeMetadata, security.MethodSubscribeLocalMetadata},
RegisteredClaims: jwt.RegisteredClaims{
ID: "subscription-compatibility",
IssuedAt: jwt.NewNumericDate(time.Now()),
ExpiresAt: jwt.NewNumericDate(time.Now().Add(time.Minute)),
},
}
token, err := jwt.NewWithClaims(jwt.SigningMethodHS256, claims).SignedString([]byte(signingKey))
if err != nil {
t.Fatalf("failed to mint jti subscription token: %v", err)
}
return token
}
func TestScopeSubscriptionKeyOverlapCompatibilityMatrix(t *testing.T) {
type tokenKeyCase struct {
name string
filerKey security.SigningKey
previousKey security.SigningKey
subscribeKey security.SigningKey
}
cases := []tokenKeyCase{
{
name: "separate subscription key",
filerKey: security.SigningKey(subscriptionFilerKey),
subscribeKey: security.SigningKey(subscriptionTokenKey),
},
{
name: "current filer key",
filerKey: security.SigningKey(subscriptionFilerKey),
subscribeKey: security.SigningKey(subscriptionFilerKey),
},
{
name: "previous filer key",
filerKey: security.SigningKey(subscriptionFilerKey),
previousKey: security.SigningKey(subscriptionPreviousFilerKey),
subscribeKey: security.SigningKey(subscriptionPreviousFilerKey),
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
scope := subscriptionScopeServerWithKeys(true, tc.filerKey, tc.previousKey, tc.subscribeKey)
for _, withJTI := range []bool{false, true} {
t.Run(map[bool]string{false: "without-jti", true: "with-jti"}[withJTI], func(t *testing.T) {
token := subscriptionCompatibilityToken(t, tc.subscribeKey, withJTI)
for _, method := range []string{security.MethodSubscribeMetadata, security.MethodSubscribeLocalMetadata} {
t.Run(method, func(t *testing.T) {
// The existing dedicated verifier accepts every token in this matrix,
// including overlapping keys with jti. That case now uses
// the general filer scope's bounded subscription path.
dedicatedScope, dedicatedErr := security.CheckGrpcMetadataSubscription(
subscriptionStreamContext(token),
tc.subscribeKey,
tc.filerKey,
tc.previousKey,
method,
0,
)
if dedicatedErr != nil || dedicatedScope == nil {
t.Fatalf("dedicated verifier err=%v scope=%v, want a subscription scope", dedicatedErr, dedicatedScope)
}
called := false
err := callSubscriptionStream(t, scope, method, subscriptionStreamContext(token), func() { called = true })
if err != nil || !called {
t.Fatalf("subscription token err=%v called=%v, want handler dispatch", err, called)
}
})
}
})
}
})
}
}
func TestScopeRejectsMalformedAndInvalidDedicatedSubscriptionTokens(t *testing.T) {
validToken := string(security.GenJwtForMetadataSubscription(security.SigningKey(subscriptionTokenKey), 60, []string{aliceWS}))
if validToken == "" {
t.Fatal("failed to mint valid dedicated subscription token")
}
expiredClaims := security.SeaweedFilerClaims{
AllowedPrefixes: []string{aliceWS},
AllowedMethods: []string{security.MethodSubscribeMetadata, security.MethodSubscribeLocalMetadata},
RegisteredClaims: jwt.RegisteredClaims{
ExpiresAt: jwt.NewNumericDate(time.Now().Add(-time.Minute)),
},
}
expiredToken, err := jwt.NewWithClaims(jwt.SigningMethodHS256, expiredClaims).SignedString([]byte(subscriptionTokenKey))
if err != nil {
t.Fatalf("failed to mint expired dedicated subscription token: %v", err)
}
cases := []struct {
name string
token string
ctx context.Context
}{
{
name: "malformed authorization",
ctx: metadata.NewIncomingContext(context.Background(), metadata.Pairs("authorization", "Basic opaque")),
},
{
name: "duplicate valid authorization",
ctx: subscriptionStreamContextValues("Bearer "+validToken, "Bearer "+validToken),
},
{
name: "wrong signing key",
token: string(security.GenJwtForMetadataSubscription(security.SigningKey("wrong-subscription-key"), 60, []string{aliceWS})),
},
{
name: "expired",
token: expiredToken,
},
}
scope := subscriptionScopeServer(true)
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
ctx := tc.ctx
if ctx == nil {
if tc.token == "" {
t.Fatal("test case has neither context nor token")
}
ctx = subscriptionStreamContext(tc.token)
}
called := false
err := callSubscriptionStream(t, scope, security.MethodSubscribeMetadata, ctx, func() { called = true })
if status.Code(err) != codes.Unauthenticated || called {
t.Fatalf("invalid dedicated subscription err=%v called=%v, want Unauthenticated before dispatch", err, called)
}
})
}
}