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>
112 lines
3.7 KiB
Go
112 lines
3.7 KiB
Go
package security
|
|
|
|
import (
|
|
"errors"
|
|
"testing"
|
|
"time"
|
|
|
|
jwt "github.com/golang-jwt/jwt/v5"
|
|
)
|
|
|
|
func TestGenJwtForFilerScopeCarriesReadWriteAndReadOnlyClaims(t *testing.T) {
|
|
token := GenJwtForFilerScope(SigningKey("scope-test-signing-key"), time.Hour, "lease-1", []string{"/write"}, []string{"/read"})
|
|
if token == "" {
|
|
t.Fatal("GenJwtForFilerScope returned an empty token")
|
|
}
|
|
claims := &SeaweedFilerClaims{}
|
|
parsed, err := DecodeJwt(SigningKey("scope-test-signing-key"), token, claims)
|
|
if err != nil || parsed == nil || !parsed.Valid {
|
|
t.Fatalf("decode scoped token: valid=%v err=%v", parsed != nil && parsed.Valid, err)
|
|
}
|
|
if claims.ID != "lease-1" || !claims.IsScoped() {
|
|
t.Fatalf("claims = %+v, want scoped lease-1 claims", claims)
|
|
}
|
|
if len(claims.AllowedPrefixes) != 1 || claims.AllowedPrefixes[0] != "/write" {
|
|
t.Fatalf("allowed prefixes = %v", claims.AllowedPrefixes)
|
|
}
|
|
if len(claims.ReadPrefixes) != 1 || claims.ReadPrefixes[0] != "/read" {
|
|
t.Fatalf("read prefixes = %v", claims.ReadPrefixes)
|
|
}
|
|
}
|
|
|
|
func TestGenJwtForFilerScopeRequiresKeyAndPositiveExpiry(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
name string
|
|
key SigningKey
|
|
age time.Duration
|
|
}{
|
|
{name: "missing key", age: time.Hour},
|
|
{name: "zero expiry", key: SigningKey("scope-test-signing-key")},
|
|
{name: "negative expiry", key: SigningKey("scope-test-signing-key"), age: -time.Second},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
if got := GenJwtForFilerScope(tc.key, tc.age, "lease-1", []string{"/write"}, nil); got != "" {
|
|
t.Fatal("expected no token")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestGenJwtForFilerScopeRequiresAPathGrant(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
name string
|
|
allowed []string
|
|
readOnly []string
|
|
}{
|
|
{name: "nil grants"},
|
|
{name: "empty grants", allowed: []string{}, readOnly: []string{}},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
if got := GenJwtForFilerScope(SigningKey("scope-test-signing-key"), time.Hour, "lease-1", tc.allowed, tc.readOnly); got != "" {
|
|
t.Fatal("expected no token without a path grant")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestDecodeJwtWithAcceptsCurrentAndPreviousScopeKeys(t *testing.T) {
|
|
current := SigningKey("current-scope-key")
|
|
previous := SigningKey("previous-scope-key")
|
|
other := SigningKey("other-scope-key")
|
|
for _, tc := range []struct {
|
|
name string
|
|
signedBy SigningKey
|
|
previousKey SigningKey
|
|
wantErr bool
|
|
}{
|
|
{name: "current key", signedBy: current},
|
|
{name: "previous key", signedBy: previous, previousKey: previous},
|
|
{name: "wrong previous key", signedBy: previous, previousKey: other, wantErr: true},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
token := GenJwtForFilerScope(tc.signedBy, time.Hour, "lease-1", []string{"/write"}, nil)
|
|
claims := &SeaweedFilerClaims{}
|
|
parsed, err := DecodeJwtWith(current, tc.previousKey, token, claims, 0)
|
|
if tc.wantErr {
|
|
if err == nil || parsed != nil && parsed.Valid {
|
|
t.Fatalf("wrong previous key accepted token: parsed=%v err=%v", parsed, err)
|
|
}
|
|
return
|
|
}
|
|
if err != nil || parsed == nil || !parsed.Valid {
|
|
t.Fatalf("scope key rotation decode: parsed=%v err=%v", parsed, err)
|
|
}
|
|
})
|
|
}
|
|
|
|
expiredPayload, err := jwt.NewWithClaims(jwt.SigningMethodHS256, SeaweedFilerClaims{
|
|
AllowedPrefixes: []string{"/write"},
|
|
RegisteredClaims: jwt.RegisteredClaims{
|
|
ID: "lease-1",
|
|
ExpiresAt: jwt.NewNumericDate(time.Now().Add(-time.Minute)),
|
|
},
|
|
}).SignedString([]byte(previous))
|
|
if err != nil {
|
|
t.Fatalf("sign expired token: %v", err)
|
|
}
|
|
_, err = DecodeJwtWith(current, previous, EncodedJwt(expiredPayload), &SeaweedFilerClaims{}, 0)
|
|
if !errors.Is(err, jwt.ErrTokenExpired) {
|
|
t.Fatalf("expired previous-key token error = %v, want ErrTokenExpired", err)
|
|
}
|
|
}
|