Files
seaweedfs/weed/server/filer_grpc_server_sub_meta_scope.go
T
39675b2faf filer: hide remote rename source paths from scoped responses (#13010)
* filer: redact remote rename source from scoped responses

* Preserve scoped remote rename markers

* Consolidate scoped marker replacement coverage

* Refuse scoped whole-entry guards during remote rename

* Keep coded create refusals for guarded scoped writes

* Make scoped subscription batch coverage deterministic

* filer: refuse scoped conditions that compare the remote rename source

IF_EXTENDED_NOT_EQUAL clauses and ExpectedExtended on Create, Update
and DeleteEntry could still compare a guess against the hidden marker,
so a scoped client could confirm the source path it redacts.

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

* filer: refuse scoped condition gates on the remote rename source

An IF_EXTENDED_TIME_ELAPSED gate applies its time check only when the
gate value matches, so a gate on the marker also confirms a guess.

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

* filer: keep a pending rename source out of remote copy errors

A failed copy of a pending remote-only rename named the marker's source
location, which the backend error repeats, and rename RPCs return that error
to scoped callers. Log the full error and return one naming only the paths
the caller supplied.

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

* filer: let a scoped local overwrite end a pending remote rename

Scoped replacements kept the hidden rename source marker even though a
scoped entry can never stay remote-only, so the marker was stale on the
local copy and every later scoped IF_ENTRY_EQUAL write was refused. Drop the
preservation and let the overwrite clear the marker as other local writes do.

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

* codespell: ignore poligon only inside URLs

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>
2026-11-26 12:31:02 -08:00

215 lines
7.2 KiB
Go

package weed_server
import (
"context"
"fmt"
"math"
"math/rand/v2"
"time"
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
"github.com/seaweedfs/seaweedfs/weed/security"
"github.com/seaweedfs/seaweedfs/weed/util"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
func (fs *FilerServer) metadataSubscriptionScope(ctx context.Context, method string) (*security.MetadataSubscriptionScope, error) {
switch sc := ctx.Value(scopeSubscriptionContextKey{}).(type) {
case *scope:
if sc.expired() {
return nil, scopeExpiredError(sc)
}
prefixes := make([]string, 0, len(sc.grants))
for _, grant := range sc.grants {
prefixes = append(prefixes, grant.path)
}
return &security.MetadataSubscriptionScope{Prefixes: prefixes, ExpiresAt: sc.exp}, nil
case *security.MetadataSubscriptionScope:
if !time.Now().Before(sc.ExpiresAt) {
return nil, errMetadataSubscriptionExpired
}
return sc, nil
}
var adminKey, previousAdminKey security.SigningKey
if fs.filerGuard != nil {
adminKey, previousAdminKey = fs.filerGuard.VerificationKeys(true)
}
subscribeKey := security.SigningKey(util.GetViper().GetString("jwt.filer_signing.subscribe.key"))
subscription, err := security.CheckGrpcMetadataSubscription(ctx, subscribeKey, adminKey, previousAdminKey, method)
if settings := fs.grpcScopeSettings.Load(); err != nil && settings != nil && len(settings.key) > 0 {
// An unscoped grpc_key token already has full filer gRPC access, and
// peers without the shared key sign their subscriptions with it.
if grpcSubscription, grpcErr := security.CheckGrpcMetadataSubscription(ctx, subscribeKey, settings.key, settings.previousKey, method); grpcErr == nil {
return grpcSubscription, nil
}
}
return subscription, err
}
// scopeMetadataResponse keeps only what a scoped subscriber may see: events
// whose every path is in scope, so a rename across the boundary never shows
// the far side, and no log file refs. Returns nil when nothing is left.
func scopeMetadataResponse(resp *filer_pb.SubscribeMetadataResponse, prefixes []string) *filer_pb.SubscribeMetadataResponse {
if resp.EventNotification == nil && len(resp.LogFileRefs) > 0 {
return nil
}
var kept []*filer_pb.SubscribeMetadataResponse
for _, e := range resp.Events {
if metadataEventInScope(e, prefixes) {
kept = append(kept, e)
}
}
headInScope := metadataEventInScope(resp, prefixes)
if !headInScope && len(kept) == 0 {
return nil
}
if headInScope && len(kept) == len(resp.Events) && len(resp.LogFileRefs) == 0 {
return resp
}
scoped := &filer_pb.SubscribeMetadataResponse{
TsNs: resp.TsNs,
Events: kept,
FlushedTsNs: resp.FlushedTsNs,
}
if headInScope {
scoped.Directory = resp.Directory
scoped.EventNotification = resp.EventNotification
scoped.MutationTsNs = resp.MutationTsNs
} else {
// the batch head keeps its position as an empty marker
scoped.EventNotification = &filer_pb.EventNotification{}
}
return scoped
}
func metadataEventInScope(event *filer_pb.SubscribeMetadataResponse, prefixes []string) bool {
if event.EventNotification == nil || filer_pb.IsEmpty(event) {
return true
}
return security.MetadataPathInScope(filer_pb.MetadataEventSourceFullPath(event), prefixes) &&
security.MetadataPathInScope(filer_pb.MetadataEventTargetFullPath(event), prefixes)
}
var errMetadataSubscriptionExpired = status.Error(codes.Unauthenticated, "metadata subscription token expired")
// scopedMetadataStream ends its context when the token expires, so an open
// subscription stops there.
type scopedMetadataStream struct {
ctx context.Context
scope *security.MetadataSubscriptionScope
withheld *int64
filerScope *scope
}
// scopedClientEpoch is below every other epoch, so a scoped registration
// never replaces another one, even if its id gets taken after it was chosen.
const scopedClientEpoch = math.MinInt32
// unusedClientId gives a scoped caller a registration id of its own, so it
// cannot replace or squat on another subscriber's registration.
func (fs *FilerServer) unusedClientId() int32 {
fs.knownListenersLock.Lock()
defer fs.knownListenersLock.Unlock()
for {
id := rand.Int32N(1<<31-1) + 1
_, found := fs.knownListeners[id]
_, foundLocal := fs.knownListeners[-id]
if !found && !foundLocal {
return id
}
}
}
func newScopedMetadataStream(parent context.Context, subscription *security.MetadataSubscriptionScope) (scopedMetadataStream, context.CancelFunc) {
ctx, cancel := context.WithDeadline(parent, subscription.ExpiresAt)
filerScope, _ := parent.Value(scopeSubscriptionContextKey{}).(*scope)
return scopedMetadataStream{ctx: ctx, scope: subscription, withheld: new(int64), filerScope: filerScope}, cancel
}
func (s scopedMetadataStream) Context() context.Context { return s.ctx }
func (s scopedMetadataStream) send(resp *filer_pb.SubscribeMetadataResponse, send func(*filer_pb.SubscribeMetadataResponse) error) error {
if !time.Now().Before(s.scope.ExpiresAt) {
return errMetadataSubscriptionExpired
}
var scoped *filer_pb.SubscribeMetadataResponse
if s.filerScope != nil {
scoped = s.filerScope.filterEvent(resp)
} else {
scoped = scopeMetadataResponse(resp, s.scope.Prefixes)
}
if scoped != nil {
scoped = scopeRedactMetadataResponse(scoped)
if err := send(scoped); err != nil {
return err
}
*s.withheld = 0
return nil
}
if resp.EventNotification == nil {
return nil
}
// withheld events still move the subscriber's resume position
*s.withheld += int64(1 + len(resp.Events))
if *s.withheld <= MaxUnsyncedEvents {
return nil
}
tsNs := resp.TsNs
for _, e := range resp.Events {
tsNs = max(tsNs, e.TsNs)
}
if err := send(&filer_pb.SubscribeMetadataResponse{EventNotification: &filer_pb.EventNotification{}, TsNs: tsNs}); err != nil {
return err
}
*s.withheld = 0
return nil
}
// sendMsg filters SendMsg too, so the embedded stream never sends unfiltered.
func (s scopedMetadataStream) sendMsg(m any, send func(*filer_pb.SubscribeMetadataResponse) error) error {
resp, ok := m.(*filer_pb.SubscribeMetadataResponse)
if !ok {
return fmt.Errorf("scoped metadata stream: unexpected message %T", m)
}
return s.send(resp, send)
}
// endErr reports an expired token in place of whatever ended the stream.
func (s scopedMetadataStream) endErr(err error) error {
if !time.Now().Before(s.scope.ExpiresAt) {
return errMetadataSubscriptionExpired
}
return err
}
type scopedSubscribeStream struct {
filer_pb.SeaweedFiler_SubscribeMetadataServer
scopedMetadataStream
}
func (s *scopedSubscribeStream) Context() context.Context { return s.ctx }
func (s *scopedSubscribeStream) Send(resp *filer_pb.SubscribeMetadataResponse) error {
return s.send(resp, s.SeaweedFiler_SubscribeMetadataServer.Send)
}
func (s *scopedSubscribeStream) SendMsg(m any) error {
return s.sendMsg(m, s.SeaweedFiler_SubscribeMetadataServer.Send)
}
type scopedSubscribeLocalStream struct {
filer_pb.SeaweedFiler_SubscribeLocalMetadataServer
scopedMetadataStream
}
func (s *scopedSubscribeLocalStream) Context() context.Context { return s.ctx }
func (s *scopedSubscribeLocalStream) Send(resp *filer_pb.SubscribeMetadataResponse) error {
return s.send(resp, s.SeaweedFiler_SubscribeLocalMetadataServer.Send)
}
func (s *scopedSubscribeLocalStream) SendMsg(m any) error {
return s.sendMsg(m, s.SeaweedFiler_SubscribeLocalMetadataServer.Send)
}