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>
223 lines
9.1 KiB
Go
223 lines
9.1 KiB
Go
package weed_server
|
|
|
|
import (
|
|
"context"
|
|
"crypto/tls"
|
|
"crypto/x509"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
|
"github.com/seaweedfs/seaweedfs/weed/security"
|
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
|
"github.com/spf13/viper"
|
|
"google.golang.org/grpc"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/credentials"
|
|
"google.golang.org/grpc/peer"
|
|
"google.golang.org/grpc/status"
|
|
)
|
|
|
|
func tokenlessVerifiedPeerContext() context.Context {
|
|
return peer.NewContext(context.Background(), &peer.Peer{
|
|
AuthInfo: credentials.TLSInfo{State: tls.ConnectionState{VerifiedChains: [][]*x509.Certificate{{}}}},
|
|
})
|
|
}
|
|
|
|
func TestFilerGrpcRequireTokenConfigDefaultsOffAndReloads(t *testing.T) {
|
|
fs := &FilerServer{filerGuard: security.NewGuard(nil, "http-key", 10, "", 0, "", "")}
|
|
config := util.NewViperProxy(viper.New())
|
|
config.Set("jwt.filer_signing.grpc_key", scopeTestKey)
|
|
|
|
fs.loadGrpcScopeConfig(config)
|
|
if got := fs.grpcScopeSettings.Load().requireToken; got {
|
|
t.Fatal("grpc_require_token defaulted on")
|
|
}
|
|
|
|
config.Set("jwt.filer_signing.grpc_require_token", true)
|
|
fs.loadGrpcScopeConfig(config)
|
|
if got := fs.grpcScopeSettings.Load().requireToken; !got {
|
|
t.Fatal("grpc_require_token reload did not enable require-mode")
|
|
}
|
|
}
|
|
|
|
func TestFilerGrpcRequireTokenRefusesTokenlessUnaryAndStreamingCalls(t *testing.T) {
|
|
h := newScopeHarness(t)
|
|
h.scope.cfg.requireToken = func() bool { return true }
|
|
c := h.client("")
|
|
|
|
beforeUnary := len(h.fake.received("LookupDirectoryEntry"))
|
|
_, err := c.LookupDirectoryEntry(scopeCtx(t), &filer_pb.LookupDirectoryEntryRequest{Directory: "/", Name: "file"})
|
|
if status.Code(err) != codes.Unauthenticated {
|
|
t.Fatalf("tokenless unary error=%v, want Unauthenticated", err)
|
|
}
|
|
if got := len(h.fake.received("LookupDirectoryEntry")); got != beforeUnary {
|
|
t.Fatalf("tokenless unary reached handler: %d calls before=%d", got, beforeUnary)
|
|
}
|
|
|
|
beforeStream := len(h.fake.received("ListEntries"))
|
|
stream, err := c.ListEntries(scopeCtx(t), &filer_pb.ListEntriesRequest{Directory: "/"})
|
|
if err == nil {
|
|
_, err = stream.Recv()
|
|
}
|
|
if status.Code(err) != codes.Unauthenticated {
|
|
t.Fatalf("tokenless stream error=%v, want Unauthenticated", err)
|
|
}
|
|
if got := len(h.fake.received("ListEntries")); got != beforeStream {
|
|
t.Fatalf("tokenless stream reached handler: %d calls before=%d", got, beforeStream)
|
|
}
|
|
}
|
|
|
|
func TestFilerGrpcRequireTokenRefusesTokenlessVerifiedMTLSUnaryAndStreaming(t *testing.T) {
|
|
scope := newFilerGrpcScope(scopeConfig{
|
|
verificationKeys: func() (security.SigningKey, security.SigningKey) {
|
|
return security.SigningKey(scopeTestKey), nil
|
|
},
|
|
requireToken: func() bool { return true },
|
|
}, newScopeFakeFiler())
|
|
ctx := tokenlessVerifiedPeerContext()
|
|
|
|
calledUnary := false
|
|
_, err := scope.unary(ctx, &filer_pb.PingRequest{}, &grpc.UnaryServerInfo{
|
|
FullMethod: filer_pb.SeaweedFiler_Ping_FullMethodName,
|
|
}, func(context.Context, any) (any, error) {
|
|
calledUnary = true
|
|
return &filer_pb.PingResponse{}, nil
|
|
})
|
|
if status.Code(err) != codes.Unauthenticated || calledUnary {
|
|
t.Fatalf("tokenless verified mTLS unary error=%v called=%v, want Unauthenticated before dispatch", err, calledUnary)
|
|
}
|
|
|
|
calledStream := false
|
|
err = scope.stream(nil, &scopeExpiryServerStream{ctx: ctx}, &grpc.StreamServerInfo{
|
|
FullMethod: filer_pb.SeaweedFiler_ListEntries_FullMethodName,
|
|
}, func(any, grpc.ServerStream) error {
|
|
calledStream = true
|
|
return nil
|
|
})
|
|
if status.Code(err) != codes.Unauthenticated || calledStream {
|
|
t.Fatalf("tokenless verified mTLS stream error=%v called=%v, want Unauthenticated before dispatch", err, calledStream)
|
|
}
|
|
}
|
|
|
|
func TestFilerGrpcRequireTokenFailsClosedWithoutVerificationKey(t *testing.T) {
|
|
scope := newFilerGrpcScope(scopeConfig{
|
|
verificationKeys: func() (security.SigningKey, security.SigningKey) { return nil, nil },
|
|
requireToken: func() bool { return true },
|
|
}, newScopeFakeFiler())
|
|
called := false
|
|
_, err := scope.unary(context.Background(), &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("missing-key require-mode error=%v called=%v, want Unauthenticated before dispatch", err, called)
|
|
}
|
|
}
|
|
|
|
func TestFilerGrpcRequireTokenDoesNotBypassTokenlessMTLSSubscription(t *testing.T) {
|
|
scope := subscriptionScopeServer(true)
|
|
scope.cfg.requireToken = func() bool { return true }
|
|
called := false
|
|
err := callSubscriptionStream(t, scope, security.MethodSubscribeMetadata, tokenlessVerifiedPeerContext(), func() {
|
|
called = true
|
|
})
|
|
if status.Code(err) != codes.Unauthenticated || called {
|
|
t.Fatalf("tokenless verified mTLS subscription error=%v called=%v, want Unauthenticated before dispatch", err, called)
|
|
}
|
|
}
|
|
|
|
func TestFilerGrpcRequireTokenPreservesDedicatedSubscriptionTokenFallback(t *testing.T) {
|
|
scope := subscriptionScopeServer(true)
|
|
scope.cfg.requireToken = func() bool { return true }
|
|
token := string(security.GenJwtForMetadataSubscription(security.SigningKey(subscriptionTokenKey), 60, []string{aliceWS}))
|
|
for _, method := range []string{security.MethodSubscribeMetadata, security.MethodSubscribeLocalMetadata} {
|
|
for _, transport := range []struct {
|
|
name string
|
|
ctx func(string) context.Context
|
|
}{
|
|
{"plain", subscriptionStreamContext},
|
|
{"mtls", verifiedSubscriptionContext},
|
|
} {
|
|
for _, bearer := range []struct {
|
|
name, token string
|
|
allowed bool
|
|
}{
|
|
{"valid", token, true},
|
|
{"invalid", "invalid", false},
|
|
{"missing", "", false},
|
|
} {
|
|
t.Run(method+"/"+transport.name+"/"+bearer.name, func(t *testing.T) {
|
|
ctx := transport.ctx(bearer.token)
|
|
called := false
|
|
err := scope.stream(nil, &scopeExpiryServerStream{ctx: ctx}, &grpc.StreamServerInfo{
|
|
FullMethod: scopeFilerMethodPrefix + method,
|
|
}, func(_ any, stream grpc.ServerStream) error {
|
|
called = true
|
|
want, _ := peer.FromContext(ctx)
|
|
if got, _ := peer.FromContext(stream.Context()); got != want {
|
|
t.Fatal("subscription admission replaced the handler's transport peer")
|
|
}
|
|
return nil
|
|
})
|
|
if bearer.allowed {
|
|
if err != nil || !called {
|
|
t.Fatalf("valid subscription bearer error=%v called=%v, want dispatch", err, called)
|
|
}
|
|
} else if status.Code(err) != codes.Unauthenticated || called {
|
|
t.Fatalf("invalid or missing subscription bearer error=%v called=%v, want rejection", err, called)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestFilerGrpcRequireTokenRejectsInvalidBearerWithPermissiveSubscriptionVerifier(t *testing.T) {
|
|
scope := subscriptionScopeServerWithKeys(true, security.SigningKey(subscriptionFilerKey), nil, nil)
|
|
scope.cfg.requireToken = func() bool { return true }
|
|
called := false
|
|
err := callSubscriptionStream(t, scope, security.MethodSubscribeMetadata, subscriptionStreamContext("invalid"), func() {
|
|
called = true
|
|
})
|
|
if status.Code(err) != codes.Unauthenticated || called {
|
|
t.Fatalf("invalid bearer with permissive subscription verifier error=%v called=%v, want Unauthenticated before dispatch", err, called)
|
|
}
|
|
}
|
|
|
|
func TestFilerGrpcRequireTokenSubscriptionKeepsTokenScopeOverMtls(t *testing.T) {
|
|
v := util.GetViper()
|
|
v.Set("jwt.filer_signing.subscribe.key", subscriptionTokenKey)
|
|
t.Cleanup(func() { v.Set("jwt.filer_signing.subscribe.key", "") })
|
|
fs := &FilerServer{}
|
|
fs.grpcScopeSettings.Store(&grpcScopeSettings{requireToken: true})
|
|
scope := subscriptionScopeServer(true)
|
|
scope.cfg.subscriptionScope = fs.metadataSubscriptionScope
|
|
scope.cfg.requireToken = func() bool { return true }
|
|
token := string(security.GenJwtForMetadataSubscription(security.SigningKey(subscriptionTokenKey), 60, []string{aliceWS}))
|
|
for _, method := range []string{security.MethodSubscribeMetadata, security.MethodSubscribeLocalMetadata} {
|
|
var got *security.MetadataSubscriptionScope
|
|
err := scope.stream(nil, &scopeExpiryServerStream{ctx: verifiedSubscriptionContext(token)}, &grpc.StreamServerInfo{
|
|
FullMethod: scopeFilerMethodPrefix + method,
|
|
}, func(_ any, stream grpc.ServerStream) (err error) {
|
|
got, err = fs.metadataSubscriptionScope(stream.Context(), method)
|
|
return err
|
|
})
|
|
if err != nil || got == nil || len(got.Prefixes) != 1 || got.Prefixes[0] != aliceWS {
|
|
t.Fatalf("%s over mTLS in require-mode: scope=%+v err=%v, want the token's prefixes", method, got, err)
|
|
}
|
|
// The token can expire before the handler reads the request; the
|
|
// certificate must not then widen the stream.
|
|
admitted := context.WithValue(verifiedSubscriptionContext("expired-subscription-token"), scopeSubscriptionContextKey{},
|
|
&security.MetadataSubscriptionScope{Prefixes: []string{aliceWS}, ExpiresAt: time.Now().Add(-time.Second)})
|
|
if got, err := fs.metadataSubscriptionScope(admitted, method); status.Code(err) != codes.Unauthenticated {
|
|
t.Fatalf("%s with an expired admitted token over mTLS: scope=%+v err=%v, want Unauthenticated", method, got, err)
|
|
}
|
|
if got, err := fs.metadataSubscriptionScope(verifiedSubscriptionContext("not-a-subscription-token"), method); err != nil || got != nil {
|
|
t.Fatalf("%s over mTLS with another bearer: scope=%+v err=%v, want the mTLS full stream", method, got, err)
|
|
}
|
|
}
|
|
}
|