Files
seaweedfs/weed/server/filer_grpc_scope_forwarding_test.go
T
430ca2b585 filer: path- and method-scoped gRPC tokens (scope enforcement, fail closed) (#12904)
* 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>
2026-11-23 08:45:34 -08:00

165 lines
5.0 KiB
Go

package weed_server
import (
"context"
"testing"
"github.com/seaweedfs/seaweedfs/weed/pb"
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
"github.com/seaweedfs/seaweedfs/weed/util"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/proto"
)
func sharedWriteRing(c *twoFilers) {
servers := []pb.ServerAddress{c.addrA, c.addrB}
setRing(c.fsA, 0, servers)
setRing(c.fsB, 0, servers)
}
func scopedForwardCreateRequest(p util.FullPath) *filer_pb.CreateEntryRequest {
dir, name := p.DirAndName()
return &filer_pb.CreateEntryRequest{
Directory: dir,
OExcl: true,
SkipCheckParentDirectory: true,
Entry: &filer_pb.Entry{
Name: name,
Attributes: &filer_pb.FuseAttributes{
FileMode: 0644,
},
},
}
}
func scopedForwardUpdateRequest(p util.FullPath, expected *filer_pb.Entry, value string) *filer_pb.UpdateEntryRequest {
dir, _ := p.DirAndName()
next := proto.Clone(expected).(*filer_pb.Entry)
next.Extended = map[string][]byte{"v": []byte(value)}
return &filer_pb.UpdateEntryRequest{
Directory: dir,
Entry: next,
ExpectedExtended: map[string][]byte{"v": expected.Extended["v"]},
}
}
func TestScopedConditionalWritesRefuseRemoteOwner(t *testing.T) {
for _, tc := range []struct {
name string
call func(*twoFilers, util.FullPath) error
check func(*testing.T, *twoFilers, util.FullPath)
}{
{
name: "create",
call: func(c *twoFilers, p util.FullPath) error {
req := scopedForwardCreateRequest(p)
_, err := c.fsA.CreateEntry(withScopeKeepOnly(context.Background()), req)
if req.IsMoved {
return status.Error(codes.Internal, "scoped create marked itself moved before refusal")
}
return err
},
check: func(t *testing.T, c *twoFilers, p util.FullPath) {
if _, err := c.store.FindEntry(context.Background(), p); err == nil {
t.Fatal("refused scoped create changed the shared store")
}
if updates, _ := c.callsB.counts(); updates != 0 {
t.Fatalf("refused scoped create reached the owner %d time(s)", updates)
}
},
},
{
name: "update",
call: func(c *twoFilers, p util.FullPath) error {
expected := c.seed(t, p)
_, err := c.fsA.UpdateEntry(withScopeKeepOnly(context.Background()), scopedForwardUpdateRequest(p, expected, "changed"))
return err
},
check: func(t *testing.T, c *twoFilers, p util.FullPath) {
if got := c.stored(t, p); got != "seed" {
t.Fatalf("refused scoped update changed the shared store to %q", got)
}
if updates, _ := c.callsB.counts(); updates != 0 {
t.Fatalf("refused scoped update reached the owner %d time(s)", updates)
}
},
},
} {
t.Run(tc.name, func(t *testing.T) {
c := newTwoFilers(t)
sharedWriteRing(c)
p := c.keyOwnedBy(t, c.addrB, c.addrB)
err := tc.call(c, p)
if status.Code(err) != codes.PermissionDenied {
t.Fatalf("scoped remote %s error = %v, want PermissionDenied", tc.name, err)
}
tc.check(t, c, p)
})
}
}
func TestScopedConditionalWritesApplyOnLocalOwner(t *testing.T) {
for _, tc := range []struct {
name string
call func(*twoFilers, util.FullPath) error
check func(*testing.T, *twoFilers, util.FullPath)
}{
{
name: "create",
call: func(c *twoFilers, p util.FullPath) error {
_, err := c.fsA.CreateEntry(withScopeKeepOnly(context.Background()), scopedForwardCreateRequest(p))
return err
},
check: func(t *testing.T, c *twoFilers, p util.FullPath) {
if _, err := c.store.FindEntry(context.Background(), p); err != nil {
t.Fatalf("scoped local create was not stored: %v", err)
}
},
},
{
name: "update",
call: func(c *twoFilers, p util.FullPath) error {
expected := c.seed(t, p)
_, err := c.fsA.UpdateEntry(withScopeKeepOnly(context.Background()), scopedForwardUpdateRequest(p, expected, "changed"))
return err
},
check: func(t *testing.T, c *twoFilers, p util.FullPath) {
if got := c.stored(t, p); got != "changed" {
t.Fatalf("scoped local update stored %q, want changed", got)
}
},
},
} {
t.Run(tc.name, func(t *testing.T) {
c := newTwoFilers(t)
sharedWriteRing(c)
p := c.keyOwnedBy(t, c.addrA, c.addrA)
if err := tc.call(c, p); err != nil {
t.Fatalf("scoped local %s: %v", tc.name, err)
}
tc.check(t, c, p)
if updates, _ := c.callsB.counts(); updates != 0 {
t.Fatalf("scoped local %s reached the other filer %d time(s)", tc.name, updates)
}
})
}
}
func TestUnscopedConditionalUpdateStillForwards(t *testing.T) {
c := newTwoFilers(t)
sharedWriteRing(c)
p := c.keyOwnedBy(t, c.addrB, c.addrB)
expected := c.seed(t, p)
if err := c.update(c.addrA, p, expected, "forwarded"); err != nil {
t.Fatalf("unscoped conditional update: %v", err)
}
if got := c.stored(t, p); got != "forwarded" {
t.Fatalf("forwarded update stored %q, want forwarded", got)
}
if updates, moved := c.callsB.counts(); updates != 1 || moved != 1 {
t.Fatalf("owner received %d updates (%d moved), want one forwarded update", updates, moved)
}
}