mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-04 18:17:16 +01:00
* security: verify JWTs against an optional previous signing key DecodeJwt, the admin token checks and the volume expiry classifier take previous keys to try after the current one, and Guard keeps a previous write and read key next to the current ones. Signing still uses only the current key. Callers pass no previous key yet. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * volume, filer, s3, admin: accept tokens signed with jwt.*.previous_key jwt.signing.previous_key, jwt.signing.read.previous_key, jwt.filer_signing.previous_key and jwt.filer_signing.read.previous_key (WEED_JWT_*_PREVIOUS_KEY from the environment) name a key the verifiers still accept next to the current one, and SIGHUP reloads them. Keys can then rotate without an error window: add the new key as previous everywhere, swap current and previous, then remove previous. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * scaffold: document jwt previous_key for key rotation Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * volume(rs): accept tokens signed with jwt.signing previous_key Mirrors the Go volume server: [jwt.signing] and [jwt.signing.read] take previous_key (WEED_JWT_SIGNING_PREVIOUS_KEY, WEED_JWT_SIGNING_READ_PREVIOUS_KEY), the guard verifies against it after the current key, and SIGHUP reloads it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * volume, filer, s3: drop a removed previous_key on SIGHUP LoadConfiguration merges, so a previous_key line deleted from security.toml kept verifying until a restart. Read the previous keys without the merge on reload, as the Rust volume server already does. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * volume, filer, s3: keep previous keys when a reload cannot read the file A SIGHUP that found security.toml unreadable or malformed cleared the previous keys while the merged current keys stayed. Fall back to the merged configuration then, so the keys in effect stay in effect. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * filer: IAM and metadata subscription admin checks follow the filer guard The IAM gRPC service checked the jwt.filer_signing keys read at startup, and the metadata subscription read them from the merged configuration, so adding or removing previous_key with SIGHUP did not reach them. Both now read the filer guard that Reload updates. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * security: reload jwt keys in one place, keeping the guard's previous keys When security.toml could not be read, the reloads fell back to the merged configuration, which still holds a previous_key an earlier reload removed. Guard.ReloadSigningKeys keeps the previous keys the guard already has instead, and the volume, filer and S3 reloads share it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * security: take reloaded current keys from the same read as previous keys The current keys came from the merged configuration and the previous keys from a second read of the file, so a file swapped between the two reads could publish the same key as both. Take every key the fresh read sets from it; a current key the file no longer sets keeps its merged value. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * admin: worker streams read the filer admin keys as security.toml sets them Both worker stream handlers read jwt.filer_signing keys from the merged configuration. Under weed mini a reload merges the file, which keeps a previous_key the operator removed, so new streams still accepted it; a standalone admin never picked up a rotation at all. Read the keys the file sets now, falling back to the merged values. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * scaffold: wait for old-key tokens to expire before clearing previous_key Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * volume(rs): keep the keys in effect when SIGHUP cannot read security.toml parse_security_config falls back to defaults when the file is missing or unreadable, so a SIGHUP then cleared every key and turned JWT checks off. Skip the key and whitelist update in that case, as the Go reload keeps its merged configuration and the guard's previous keys. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * security: keep every key in effect when a reload cannot read security.toml A failed read took the current keys from the merged configuration while keeping the active previous keys, so after a swapped pair the guard could publish the old key twice and reject tokens signed with the new one. The reload now leaves the keys untouched, as the Rust volume server does. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * security: filer admin keys fall back to the last pair read, not merged values When security.toml stopped being readable, FilerAdminKeys returned the merged configuration, which still holds a previous_key the file had already dropped, so tokens signed with the retired key verified again. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * filer: STS store gates check the admin token against one key snapshot The OIDC provider and role RPCs read the reloadable admin key twice, in checkAdminAuth and in the store gate. A reload that set the key between the two reads let a call without a token pass both. The gate now takes one snapshot for the empty-key refusal and the token check. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * security: read and cache filer admin keys under one lock Concurrent callers could store their reads out of order, so an older pair that still had previous_key could overwrite a newer one and come back when a later read of security.toml failed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * security: unlock the filer admin key mutex on every exit of its test Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
170 lines
6.2 KiB
Go
170 lines
6.2 KiB
Go
package security
|
|
|
|
import (
|
|
"context"
|
|
"net"
|
|
"strings"
|
|
"sync"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/credentials"
|
|
"google.golang.org/grpc/metadata"
|
|
"google.golang.org/grpc/peer"
|
|
"google.golang.org/grpc/status"
|
|
)
|
|
|
|
// CheckGrpcAdminPeer authenticates the peer of an admin-plane gRPC handler:
|
|
// a client certificate verified by the server's TLS config, or a Bearer token
|
|
// signed by signingKey (jwt.filer_signing.key) or one of previousKeys
|
|
// (jwt.filer_signing.previous_key). With no key configured remote
|
|
// callers cannot be told apart, so only local clients (unix socket, loopback,
|
|
// or an address assigned to this host) are allowed.
|
|
func CheckGrpcAdminPeer(ctx context.Context, signingKey SigningKey, previousKeys ...SigningKey) error {
|
|
if isMtlsVerifiedPeer(ctx) {
|
|
return nil
|
|
}
|
|
if len(signingKey) == 0 {
|
|
return CheckLocalGrpcPeer(ctx)
|
|
}
|
|
return CheckGrpcAdminToken(ctx, signingKey, previousKeys...)
|
|
}
|
|
|
|
var (
|
|
filerAdminKeysMu sync.Mutex
|
|
lastFilerAdminKeys *[2]SigningKey
|
|
)
|
|
|
|
// FilerAdminKeys returns jwt.filer_signing.key and previous_key as
|
|
// security.toml sets them now, for admin-plane handlers in processes that do
|
|
// not reload: they follow a rotation, and a previous_key removed from the file
|
|
// stops verifying. When the file cannot be read it returns the last pair read,
|
|
// else the merged keys.
|
|
func FilerAdminKeys() (signingKey, previousKey SigningKey) {
|
|
filerAdminKeysMu.Lock()
|
|
defer filerAdminKeysMu.Unlock()
|
|
if fresh, used := util.ReadConfiguration("security", ""); used != "" {
|
|
lastFilerAdminKeys = &[2]SigningKey{SigningKey(freshKey(fresh, "jwt.filer_signing.key")), SigningKey(fresh.GetString("jwt.filer_signing.previous_key"))}
|
|
return lastFilerAdminKeys[0], lastFilerAdminKeys[1]
|
|
}
|
|
if last := lastFilerAdminKeys; last != nil {
|
|
return last[0], last[1]
|
|
}
|
|
v := util.GetViper()
|
|
return SigningKey(v.GetString("jwt.filer_signing.key")), SigningKey(v.GetString("jwt.filer_signing.previous_key"))
|
|
}
|
|
|
|
// maxGrpcAdminAuthValues bounds the JWT parses one request can trigger; a
|
|
// caller sends its own token plus at most one forwarded from its context.
|
|
const maxGrpcAdminAuthValues = 4
|
|
|
|
// CheckGrpcAdminToken accepts the call when one of the first
|
|
// maxGrpcAdminAuthValues authorization values is a Bearer admin token signed
|
|
// by signingKey or one of previousKeys, so a fresh token still counts when the
|
|
// caller's context also forwards an older one.
|
|
func CheckGrpcAdminToken(ctx context.Context, signingKey SigningKey, previousKeys ...SigningKey) error {
|
|
md, ok := metadata.FromIncomingContext(ctx)
|
|
if !ok {
|
|
return status.Error(codes.Unauthenticated, "missing metadata")
|
|
}
|
|
authHeaders := md.Get("authorization")
|
|
if len(authHeaders) == 0 {
|
|
return status.Error(codes.Unauthenticated, "missing authorization metadata")
|
|
}
|
|
if len(authHeaders) > maxGrpcAdminAuthValues {
|
|
authHeaders = authHeaders[:maxGrpcAdminAuthValues]
|
|
}
|
|
err := status.Error(codes.Unauthenticated, "authorization header must use Bearer scheme")
|
|
for _, raw := range authHeaders {
|
|
parts := strings.Fields(raw)
|
|
if len(parts) != 2 || !strings.EqualFold(parts[0], "Bearer") || parts[1] == "" {
|
|
continue
|
|
}
|
|
if IsFilerAdminToken(signingKey, EncodedJwt(parts[1]), previousKeys...) {
|
|
return nil
|
|
}
|
|
err = status.Error(codes.Unauthenticated, "invalid admin token")
|
|
}
|
|
return err
|
|
}
|
|
|
|
// IsFilerAdminToken reports whether token is an admin token signed by
|
|
// signingKey or one of previousKeys. An STS session token, or a filer token
|
|
// scoped to paths or methods, signed with the same key does not count.
|
|
func IsFilerAdminToken(signingKey SigningKey, token EncodedJwt, previousKeys ...SigningKey) bool {
|
|
if len(signingKey) == 0 || token == "" {
|
|
return false
|
|
}
|
|
parsed, err := DecodeJwt(signingKey, token, &SeaweedFilerAdminClaims{}, previousKeys...)
|
|
if err != nil || parsed == nil || !parsed.Valid {
|
|
return false
|
|
}
|
|
claims, ok := parsed.Claims.(*SeaweedFilerAdminClaims)
|
|
return ok && claims.SessionId == "" && len(claims.AllowedPrefixes) == 0 && len(claims.AllowedMethods) == 0
|
|
}
|
|
|
|
// GrpcAdminPeerCredentials returns outgoing metadata carrying a Bearer token
|
|
// for admin-plane gRPC handlers, or nil when no signing key is configured.
|
|
func GrpcAdminPeerCredentials(signingKey SigningKey, expiresAfterSec int) metadata.MD {
|
|
token := GenJwtForFilerAdmin(signingKey, expiresAfterSec)
|
|
if token == "" {
|
|
return nil
|
|
}
|
|
return metadata.Pairs("authorization", "Bearer "+string(token))
|
|
}
|
|
|
|
func isMtlsVerifiedPeer(ctx context.Context) bool {
|
|
pr, ok := peer.FromContext(ctx)
|
|
if !ok {
|
|
return false
|
|
}
|
|
tlsInfo, ok := pr.AuthInfo.(credentials.TLSInfo)
|
|
return ok && len(tlsInfo.State.VerifiedChains) > 0
|
|
}
|
|
|
|
// CheckLocalGrpcPeer admits only clients on this host: unix sockets, loopback,
|
|
// or an address assigned to a local interface.
|
|
func CheckLocalGrpcPeer(ctx context.Context) error {
|
|
if ctx == nil {
|
|
return status.Error(codes.Unauthenticated, "admin gRPC calls require jwt.filer_signing.key or a local client")
|
|
}
|
|
pr, ok := peer.FromContext(ctx)
|
|
if !ok {
|
|
return status.Error(codes.Unauthenticated, "admin gRPC calls require jwt.filer_signing.key or a local client")
|
|
}
|
|
if _, isUnix := pr.Addr.(*net.UnixAddr); isUnix {
|
|
return nil
|
|
}
|
|
var ip net.IP
|
|
if tcpAddr, ok := pr.Addr.(*net.TCPAddr); ok {
|
|
ip = tcpAddr.IP
|
|
} else if h, _, err := net.SplitHostPort(pr.Addr.String()); err == nil {
|
|
ip = net.ParseIP(h)
|
|
} else {
|
|
ip = net.ParseIP(pr.Addr.String())
|
|
}
|
|
if ip != nil && (ip.IsLoopback() || isLocalAddress(ip)) {
|
|
return nil
|
|
}
|
|
glog.V(1).Infof("rejected unauthenticated admin gRPC call from %s: no jwt.filer_signing.key configured", pr.Addr)
|
|
return status.Error(codes.Unauthenticated, "admin gRPC calls require jwt.filer_signing.key or a local client")
|
|
}
|
|
|
|
// isLocalAddress enumerates interfaces per call: only reachable for
|
|
// non-loopback TCP peers on the no-key admin path, which is low-volume —
|
|
// and a cached set would keep trusting an address after it is removed from
|
|
// the host and reassigned.
|
|
func isLocalAddress(ip net.IP) bool {
|
|
addrs, err := net.InterfaceAddrs()
|
|
if err != nil {
|
|
return false
|
|
}
|
|
for _, a := range addrs {
|
|
if ipNet, ok := a.(*net.IPNet); ok && ipNet.IP.Equal(ip) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|