mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-04 18:17:16 +01:00
* 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>
147 lines
6.1 KiB
Go
147 lines
6.1 KiB
Go
package security
|
|
|
|
import (
|
|
"context"
|
|
"crypto/tls"
|
|
"crypto/x509"
|
|
"net"
|
|
"slices"
|
|
"testing"
|
|
"time"
|
|
|
|
jwt "github.com/golang-jwt/jwt/v5"
|
|
"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"
|
|
)
|
|
|
|
func peerCtx(addr net.Addr) context.Context {
|
|
return peer.NewContext(context.Background(), &peer.Peer{Addr: addr})
|
|
}
|
|
|
|
func bearerCtx(addr net.Addr, signingKey SigningKey) context.Context {
|
|
ctx := peerCtx(addr)
|
|
token := GenJwtForFilerAdmin(signingKey, 0)
|
|
return metadata.NewIncomingContext(ctx, metadata.Pairs("authorization", "Bearer "+string(token)))
|
|
}
|
|
|
|
func TestCheckGrpcAdminPeer(t *testing.T) {
|
|
key := SigningKey("test-signing-key-0123456789abcdef")
|
|
loopback := &net.TCPAddr{IP: net.ParseIP("127.0.0.1"), Port: 5000}
|
|
remote := &net.TCPAddr{IP: net.ParseIP("203.0.113.9"), Port: 5000}
|
|
|
|
for name, tc := range map[string]struct {
|
|
ctx context.Context
|
|
signingKey SigningKey
|
|
wantCode codes.Code
|
|
}{
|
|
"no peer info, no key": {context.Background(), nil, codes.Unauthenticated},
|
|
"loopback, no key": {peerCtx(loopback), nil, codes.OK},
|
|
"remote, no key": {peerCtx(remote), nil, codes.Unauthenticated},
|
|
"remote + valid token": {bearerCtx(remote, key), key, codes.OK},
|
|
"remote + no metadata, key set": {peerCtx(remote), key, codes.Unauthenticated},
|
|
"remote + bad token": {metadata.NewIncomingContext(peerCtx(remote), metadata.Pairs("authorization", "Bearer wrong")), key, codes.Unauthenticated},
|
|
"unix socket, no key": {peerCtx(&net.UnixAddr{Name: "/tmp/s.sock"}), nil, codes.OK},
|
|
"mtls verified peer": {peer.NewContext(context.Background(), &peer.Peer{
|
|
Addr: remote,
|
|
AuthInfo: credentials.TLSInfo{State: tls.ConnectionState{VerifiedChains: [][]*x509.Certificate{{}}}},
|
|
}), nil, codes.OK},
|
|
} {
|
|
err := CheckGrpcAdminPeer(tc.ctx, tc.signingKey)
|
|
got := codes.OK
|
|
if err != nil {
|
|
got = status.Code(err)
|
|
}
|
|
if got != tc.wantCode {
|
|
t.Errorf("%s: got %v want %v (err=%v)", name, got, tc.wantCode, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestCheckGrpcAdminPeerRefusesReadOnlyFilerToken(t *testing.T) {
|
|
key := SigningKey("test-signing-key-0123456789abcdef")
|
|
remote := &net.TCPAddr{IP: net.ParseIP("203.0.113.9"), Port: 5000}
|
|
token := GenJwtForFilerScope(key, time.Hour, "lease-1", nil, []string{"/buckets/alice"})
|
|
ctx := metadata.NewIncomingContext(peerCtx(remote), metadata.Pairs("authorization", "Bearer "+string(token)))
|
|
if err := CheckGrpcAdminPeer(ctx, key); status.Code(err) != codes.Unauthenticated {
|
|
t.Fatalf("read-only scoped token accepted as admin peer: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestGrpcAdminPeerCredentials(t *testing.T) {
|
|
if md := GrpcAdminPeerCredentials(nil, 0); len(md) != 0 {
|
|
t.Errorf("expected nil metadata without signing key, got %v", md)
|
|
}
|
|
md := GrpcAdminPeerCredentials(SigningKey("key"), 60)
|
|
if len(md.Get("authorization")) != 1 {
|
|
t.Fatalf("expected one authorization entry, got %v", md)
|
|
}
|
|
}
|
|
|
|
func TestCheckGrpcAdminToken(t *testing.T) {
|
|
key := SigningKey("test-signing-key-0123456789abcdef")
|
|
fresh := "Bearer " + string(GenJwtForFilerAdmin(key, 60))
|
|
stale := "Bearer " + string(GenJwtForFilerAdmin(SigningKey("another-key"), 60))
|
|
expired, signErr := jwt.NewWithClaims(jwt.SigningMethodHS256, SeaweedFilerAdminClaims{
|
|
RegisteredClaims: jwt.RegisteredClaims{ExpiresAt: jwt.NewNumericDate(time.Now().Add(-time.Hour))},
|
|
}).SignedString([]byte(key))
|
|
if signErr != nil {
|
|
t.Fatalf("sign expired token: %v", signErr)
|
|
}
|
|
session, signErr := jwt.NewWithClaims(jwt.SigningMethodHS256, SeaweedFilerAdminClaims{
|
|
SessionId: "session",
|
|
RegisteredClaims: jwt.RegisteredClaims{ExpiresAt: jwt.NewNumericDate(time.Now().Add(time.Hour))},
|
|
}).SignedString([]byte(key))
|
|
if signErr != nil {
|
|
t.Fatalf("sign session token: %v", signErr)
|
|
}
|
|
scoped := func(claims SeaweedFilerClaims) string {
|
|
claims.ExpiresAt = jwt.NewNumericDate(time.Now().Add(time.Hour))
|
|
signed, err := jwt.NewWithClaims(jwt.SigningMethodHS256, claims).SignedString([]byte(key))
|
|
if err != nil {
|
|
t.Fatalf("sign scoped token: %v", err)
|
|
}
|
|
return "Bearer " + signed
|
|
}
|
|
pathScoped := scoped(SeaweedFilerClaims{AllowedPrefixes: []string{"/tenants/t1"}})
|
|
methodScoped := scoped(SeaweedFilerClaims{AllowedMethods: []string{"GET"}})
|
|
readOnlyScoped := scoped(SeaweedFilerClaims{ReadPrefixes: []string{"/tenants/t1"}})
|
|
unscoped := scoped(SeaweedFilerClaims{})
|
|
|
|
for name, tc := range map[string]struct {
|
|
values []string
|
|
wantErr string
|
|
}{
|
|
"stale then fresh": {[]string{stale, fresh}, ""},
|
|
"fresh then garbage": {[]string{fresh, "garbage"}, ""},
|
|
"expired then fresh": {[]string{"Bearer " + expired, fresh}, ""},
|
|
"only expired": {[]string{"Bearer " + expired}, "invalid admin token"},
|
|
"only stale": {[]string{stale}, "invalid admin token"},
|
|
"same-key session": {[]string{"Bearer " + session}, "invalid admin token"},
|
|
"path-scoped": {[]string{pathScoped}, "invalid admin token"},
|
|
"method-scoped": {[]string{methodScoped}, "invalid admin token"},
|
|
"read-only-scoped": {[]string{readOnlyScoped}, "invalid admin token"},
|
|
"read-only then fresh": {[]string{readOnlyScoped, fresh}, ""},
|
|
"scoped then fresh": {[]string{pathScoped, fresh}, ""},
|
|
"unscoped filer": {[]string{unscoped}, ""},
|
|
"basic then stale": {[]string{"Basic dXNlcjpwYXNz", stale}, "invalid admin token"},
|
|
"no bearer value": {[]string{"Basic dXNlcjpwYXNz", "garbage"}, "authorization header must use Bearer scheme"},
|
|
"lowercase scheme ok": {[]string{"bearer " + fresh[len("Bearer "):]}, ""},
|
|
"fresh past the cap": {append(slices.Repeat([]string{stale}, maxGrpcAdminAuthValues), fresh), "invalid admin token"},
|
|
} {
|
|
ctx := metadata.NewIncomingContext(context.Background(), metadata.MD{"authorization": tc.values})
|
|
err := CheckGrpcAdminToken(ctx, key)
|
|
if tc.wantErr == "" {
|
|
if err != nil {
|
|
t.Errorf("%s: unexpected error %v", name, err)
|
|
}
|
|
continue
|
|
}
|
|
if status.Code(err) != codes.Unauthenticated || status.Convert(err).Message() != tc.wantErr {
|
|
t.Errorf("%s: got %v, want Unauthenticated %q", name, err, tc.wantErr)
|
|
}
|
|
}
|
|
}
|