Files
seaweedfs/weed/server/filer_grpc_scope_subscription.go
T
f079b68f2b filer: grpc_require_token with authenticated filer-to-filer peer calls (#13011)
* 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>
2026-11-26 08:18:28 -08:00

143 lines
4.5 KiB
Go

package weed_server
import (
"context"
"strings"
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
"google.golang.org/grpc"
)
// Only the scope interceptor produces this marker: a *scope for a general
// filer token, or the *security.MetadataSubscriptionScope a dedicated
// subscription token was admitted with.
type scopeSubscriptionContextKey struct{}
type subscriptionStream struct {
grpc.ServerStream
ctx context.Context
}
func (s *subscriptionStream) Context() context.Context { return s.ctx }
// checkSubscribe bounds every requested selector. Event filtering also enforces
// path-segment boundaries because the subscription's prefix match is textual.
func (sc *scope) checkSubscribe(method string, req *filer_pb.SubscribeMetadataRequest) error {
req.ClientSupportsMetadataChunks = false
kept := req.PathPrefixes[:0]
for _, p := range req.PathPrefixes {
// Mounts also watch the storage-rule file, which scoped reads hide.
if p != scopeFilerConfDir+"/"+scopeFilerConfName {
kept = append(kept, p)
}
}
req.PathPrefixes = kept
prefixes := append([]string{}, req.PathPrefixes...)
if req.PathPrefix != "" {
prefixes = append(prefixes, req.PathPrefix)
}
if len(prefixes) == 0 && len(req.Directories) == 0 {
return sc.deny(method, "a subscription must name at least one path prefix or directory")
}
for _, p := range prefixes {
// Mounts use a trailing slash to subscribe below their root.
if len(p) > 1 {
p = strings.TrimSuffix(p, "/")
}
if _, err := sc.checkPath(method, p, "", false); err != nil {
return err
}
}
for _, dir := range req.Directories {
if _, err := sc.checkPath(method, dir, "", false); err != nil {
return err
}
}
return nil
}
// filterEvent checks the envelope's event as well as every batched event. A
// hidden head keeps only its position when visible tail events remain. Shared
// input messages are never modified, and log-file references never pass through.
func (sc *scope) filterEvent(resp *filer_pb.SubscribeMetadataResponse) *filer_pb.SubscribeMetadataResponse {
if resp == nil {
return nil
}
head := sc.filterSubscriptionEvent(resp)
var kept []*filer_pb.SubscribeMetadataResponse
for _, event := range resp.Events {
if out := sc.filterEvent(event); out != nil {
kept = append(kept, out)
}
}
if head == nil {
if len(kept) == 0 {
return nil
}
head = &filer_pb.SubscribeMetadataResponse{
TsNs: resp.TsNs, FlushedTsNs: resp.FlushedTsNs,
EventNotification: &filer_pb.EventNotification{},
}
}
head.Events = kept
return head
}
// filterSubscriptionEvent exposes only the visible half of a boundary-crossing
// rename. A rename from filer.conf is entirely hidden: its new side would carry
// the hidden file's data. A rename into it may still delete the visible old side.
func (sc *scope) filterSubscriptionEvent(resp *filer_pb.SubscribeMetadataResponse) *filer_pb.SubscribeMetadataResponse {
if len(resp.LogFileRefs) > 0 {
return nil
}
out := &filer_pb.SubscribeMetadataResponse{TsNs: resp.TsNs, FlushedTsNs: resp.FlushedTsNs}
ev := resp.EventNotification
if ev == nil {
return out
}
if ev.OldEntry == nil && ev.NewEntry == nil {
if resp.Directory != "" {
p, err := scopeFullPath(resp.Directory, "")
if err != nil || !scopePathAllowed(sc.grants, p, false) || isScopeFilerConf(resp.Directory, "") {
return nil
}
out.Directory = resp.Directory
}
out.EventNotification = &filer_pb.EventNotification{}
return out
}
newDir := filer_pb.MetadataEventTargetDirectory(resp)
visible := func(dir string, entry *filer_pb.Entry) (bool, error) {
if entry == nil {
return false, nil
}
if entry.Name == "" {
return false, errScopePathNotCanonical
}
p, err := scopeFullPath(dir, entry.Name)
return err == nil && scopePathAllowed(sc.grants, p, false) && !isScopeFilerConf(dir, entry.Name), err
}
oldOK, oldErr := visible(resp.Directory, ev.OldEntry)
newOK, newErr := visible(newDir, ev.NewEntry)
if oldErr != nil || newErr != nil ||
(ev.OldEntry != nil && isScopeFilerConf(resp.Directory, ev.OldEntry.Name)) ||
(!oldOK && !newOK) {
return nil
}
out.Directory, out.MutationTsNs = resp.Directory, resp.MutationTsNs
out.EventNotification = &filer_pb.EventNotification{
DeleteChunks: ev.DeleteChunks, IsFromOtherCluster: ev.IsFromOtherCluster, Signatures: ev.Signatures,
}
if oldOK {
out.EventNotification.OldEntry = ev.OldEntry
}
if newOK {
out.EventNotification.NewEntry = ev.NewEntry
out.EventNotification.NewParentPath = newDir
if !oldOK {
out.Directory = newDir
}
}
return out
}