mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-04 18:17:16 +01:00
* 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>
214 lines
7.2 KiB
Go
214 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 {
|
|
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)
|
|
}
|