Files
seaweedfs/weed/server/filer_grpc_forward_auth.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

64 lines
2.4 KiB
Go

package weed_server
import (
"context"
"net/http"
"github.com/seaweedfs/seaweedfs/weed/security"
"google.golang.org/grpc/metadata"
)
// withCallerAuthContext carries the caller's bearer to the next filer hop.
// It deliberately copies only authorization; filer-owned calls use
// peerContext instead and therefore never inherit a caller's authority.
func withCallerAuthContext(ctx context.Context) context.Context {
incoming, _ := metadata.FromIncomingContext(ctx)
return withAuthorizationValues(ctx, incoming.Get("authorization"))
}
// withHTTPBearerContext adapts the bearer already authenticated on an HTTP
// request to the metadata form used by the outgoing filer RPC. An absent
// bearer remains absent, preserving tokenless compatibility when require-mode
// is disabled and allowing require-mode to reject it at the receiver.
func withHTTPBearerContext(ctx context.Context, token security.EncodedJwt) context.Context {
if token == "" {
return withAuthorizationValues(ctx, nil)
}
return withAuthorizationValues(ctx, []string{security.BearerPrefix + string(token)})
}
// withHTTPForwardAuthContext carries an HTTP caller's bearer only when
// require-mode is enabled. In the compatibility mode,
// explicitly remove authorization so a stale token from an outer call cannot
// change the request's existing tokenless behavior.
func (fs *FilerServer) withHTTPForwardAuthContext(ctx context.Context, r *http.Request) context.Context {
var token security.EncodedJwt
if settings := fs.grpcScopeSettings.Load(); settings != nil && settings.requireToken {
token = security.GetJwt(r)
}
return withHTTPBearerContext(ctx, token)
}
func withAuthorizationValues(ctx context.Context, values []string) context.Context {
outgoing, ok := metadata.FromOutgoingContext(ctx)
if len(values) == 0 {
if !ok || len(outgoing.Get("authorization")) == 0 {
return ctx
}
outgoing = outgoing.Copy()
delete(outgoing, "authorization")
return metadata.NewOutgoingContext(ctx, outgoing)
}
if outgoing == nil {
outgoing = metadata.MD{}
} else {
outgoing = outgoing.Copy()
}
// A context may already carry a metadata value from a caller or an earlier
// forwarding helper. Replace it so the next hop sees exactly the caller's
// original authorization values, never a duplicate or an admin token.
delete(outgoing, "authorization")
outgoing.Set("authorization", values...)
return metadata.NewOutgoingContext(ctx, outgoing)
}