mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-04 10:07:28 +01:00
* Add per-call filer authentication sources and pooled client wrappers * Authenticate gateway filer calls with per-call admin tokens * Authenticate shell admin and synchronization filer calls * Authenticate workers MQ and remaining filer client paths * Cache filer client signing keys and sign subscriptions with the shared key Re-reading security.toml on every filer RPC cost about 125us against 8us for signing. Keep the keys for up to a second, re-reading at once when a WEED_ override or the configuration directory changes. When the file exists but cannot be read, keep the last keys read, else the startup keys, as FilerAdminKeys does, so a process without a key stays tokenless. Metadata subscriptions prefer the shared key, as filer peers do, because older filers verify subscription admin tokens only with it. * Authenticate admin-script, maintenance and filer.conf shell paths Plugin worker admin scripts and master maintenance scripts build their own shell options and carried no filer token source; fs.configure and s3.bucket.quota.check read filer.conf without it. * filer.cat: report a failed read; isolate key overrides in the admin-script test filer.cat ignored the error from its filer call and exited successfully, leaving a truncated -output file. Report the error and exit with failure. The admin-script token test now clears the WEED_ signing-key overrides, which take precedence over security.toml. * security: refuse gRPC keys too short to sign; query engine uses client TLS Under GODEBUG=fips140=only, hmac.New panics on keys under 14 bytes. The startup check now covers jwt.filer_signing.grpc_key and grpc_previous_key, and filer client token sources check the keys they read, so a reloaded or per-endpoint security file with a short key is refused instead of panicking when the next RPC signs. The query engine's broker client dialed every master, filer and broker in plaintext, so the admin token it now sends could cross the network in the clear. It dials with the grpc.client TLS settings instead, and weed-sql loads security.toml as weed-db does. * security: per-endpoint admin tokens ignore WEED_ key overrides FilerAdminTokenFromFile honored WEED_JWT_FILER_SIGNING_GRPC_KEY and WEED_JWT_FILER_SIGNING_KEY. Those are process-wide, so filer.sync with -a.security and -b.security signed both clusters' admin tokens with the one key, handing each cluster a token valid on the other. A per-endpoint file now supplies its own keys only. * weed-sql, weed-db: refuse to start on an unreadable security.toml Both only warned when security.toml was present but could not be read or parsed. They then dialed without the grpc.client TLS settings while the filer token source could still sign admin tokens from WEED_ environment keys, so the tokens could travel in plaintext. They now exit with an error that does not quote the file; a missing file is still fine. * pb: take the filer token source in WithGrpcFilerClient and WithOneOfGrpcFilerClients Fold WithGrpcFilerClientAuth and WithOneOfGrpcFilerClientsAuth into the existing helpers instead of keeping two names for each. Callers without a token source pass nil. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * util: return the read error from ReadConfiguration Fold ReadConfigurationWithError into ReadConfiguration. The error is returned and not logged, since parser errors can quote values; the existing reload callers already warn when no file was read. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * security: keep the test-only filerAdminToken helper in the test It had no production caller. The test helper now resolves keys through filerClientKeysFrom, as the token sources do. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * util: read security.toml once in LoadSecurityConfigurationOrError It validated the file, then merged it with a second read whose failure was ignored and whose parser error was logged. It now merges with one read and logs nothing from the parser. Viper's search also treats a directory the process cannot search as holding no file; a candidate that cannot be checked for lack of permission is now refused too. * security: verify admin subscription tokens with the gRPC clock leeway CheckGrpcMetadataSubscription checked admin tokens strictly, while unary filer calls allow jwt.filer_signing.clock_leeway_seconds. With ten-second client admin tokens, a subscriber whose clock trailed the filer's by more than ten seconds was refused with a matching key. It now takes the leeway, and the filer passes its configured one. The leeway check moves from the server package to security.IsFilerAdminTokenWithLeeway so both use it. * util: start without security.toml only when every candidate is confirmed absent A candidate whose check fails for any reason other than not existing, such as a symlink loop, is refused like one the process cannot search. --------- Co-authored-by: Chris Lu <chrislusf@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
265 lines
9.3 KiB
Go
265 lines
9.3 KiB
Go
package security
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"net"
|
|
"net/http"
|
|
"strings"
|
|
"sync"
|
|
"sync/atomic"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
|
)
|
|
|
|
var (
|
|
ErrUnauthorized = errors.New("unauthorized token")
|
|
)
|
|
|
|
/*
|
|
Guard is to ensure data access security.
|
|
There are 2 ways to check access:
|
|
1. white list. It's checking request ip address.
|
|
2. JSON Web Token(JWT) generated from secretKey.
|
|
The jwt can come from:
|
|
1. url parameter jwt=...
|
|
2. request header "Authorization"
|
|
3. cookie with the name "jwt"
|
|
|
|
The white list is checked first because it is easy.
|
|
Then the JWT is checked.
|
|
|
|
The Guard will also check these claims if provided:
|
|
1. "exp" Expiration Time
|
|
2. "nbf" Not Before
|
|
|
|
Generating JWT:
|
|
1. use HS256 to sign
|
|
2. optionally set "exp", "nbf" fields, in Unix time,
|
|
the number of seconds elapsed since January 1, 1970 UTC.
|
|
|
|
Referenced:
|
|
https://github.com/pkieltyka/jwtauth/blob/master/jwtauth.go
|
|
*/
|
|
// guardState is the immutable snapshot of all hot-reloadable Guard state. The
|
|
// Update* methods build a new snapshot from the current one and swap it in
|
|
// atomically, so request-path readers (WhiteList, IsWhiteListed, the SigningKey
|
|
// accessors) always observe a consistent set of keys and whitelist — never a
|
|
// torn slice header or a mix of old and new state across a SIGHUP.
|
|
type guardState struct {
|
|
signingKey SigningKey
|
|
expiresAfterSec int
|
|
readSigningKey SigningKey
|
|
readExpiresAfterSec int
|
|
// previous keys are accepted when verifying, never used to sign
|
|
previousSigningKey SigningKey
|
|
previousReadSigningKey SigningKey
|
|
|
|
whiteListIp map[string]struct{}
|
|
whiteListCIDR map[string]*net.IPNet
|
|
isWriteActive bool
|
|
isEmptyWhiteList bool
|
|
}
|
|
|
|
type Guard struct {
|
|
// state is swapped atomically by the Update* methods. Read it via Load.
|
|
state atomic.Pointer[guardState]
|
|
// updateMu serializes the read-modify-write inside the Update* methods so
|
|
// concurrent reloads don't clobber each other; readers stay lock-free.
|
|
updateMu sync.Mutex
|
|
}
|
|
|
|
func NewGuard(whiteList []string, signingKey string, expiresAfterSec int, readSigningKey string, readExpiresAfterSec int, previousSigningKey string, previousReadSigningKey string) *Guard {
|
|
g := &Guard{}
|
|
g.state.Store(&guardState{
|
|
signingKey: SigningKey(signingKey),
|
|
expiresAfterSec: expiresAfterSec,
|
|
readSigningKey: SigningKey(readSigningKey),
|
|
readExpiresAfterSec: readExpiresAfterSec,
|
|
previousSigningKey: SigningKey(previousSigningKey),
|
|
previousReadSigningKey: SigningKey(previousReadSigningKey),
|
|
})
|
|
g.UpdateWhiteList(whiteList)
|
|
return g
|
|
}
|
|
|
|
func (g *Guard) SigningKey() SigningKey { return g.state.Load().signingKey }
|
|
func (g *Guard) ExpiresAfterSec() int { return g.state.Load().expiresAfterSec }
|
|
func (g *Guard) ReadSigningKey() SigningKey { return g.state.Load().readSigningKey }
|
|
func (g *Guard) ReadExpiresAfterSec() int { return g.state.Load().readExpiresAfterSec }
|
|
|
|
// VerificationKeys returns the write (or read) signing key and the previous
|
|
// key still accepted for it, from one snapshot.
|
|
func (g *Guard) VerificationKeys(isWrite bool) (signingKey, previousSigningKey SigningKey) {
|
|
st := g.state.Load()
|
|
if isWrite {
|
|
return st.signingKey, st.previousSigningKey
|
|
}
|
|
return st.readSigningKey, st.previousReadSigningKey
|
|
}
|
|
|
|
func (g *Guard) WhiteList(f http.HandlerFunc) http.HandlerFunc {
|
|
if !g.state.Load().isWriteActive {
|
|
//if no security needed, just skip all checking
|
|
return f
|
|
}
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
if err := g.checkWhiteList(w, r); err != nil {
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
return
|
|
}
|
|
f(w, r)
|
|
}
|
|
}
|
|
|
|
func GetActualRemoteHost(r *http.Request) string {
|
|
// For security reasons, only use RemoteAddr to determine the client's IP address.
|
|
// Do not trust headers like X-Forwarded-For, as they can be easily spoofed by clients.
|
|
host, _, err := net.SplitHostPort(r.RemoteAddr)
|
|
if err == nil {
|
|
return host
|
|
}
|
|
|
|
// If SplitHostPort fails, it may be because of a missing port.
|
|
// We try to parse RemoteAddr as a raw host (IP or hostname).
|
|
host = strings.TrimSpace(r.RemoteAddr)
|
|
// It might be an IPv6 address without a port, but with brackets.
|
|
// e.g. "[::1]"
|
|
if strings.HasPrefix(host, "[") && strings.HasSuffix(host, "]") {
|
|
host = host[1 : len(host)-1]
|
|
}
|
|
|
|
// Return the host (can be IP or hostname, just like headers)
|
|
return host
|
|
}
|
|
|
|
func (g *Guard) checkWhiteList(w http.ResponseWriter, r *http.Request) error {
|
|
host := GetActualRemoteHost(r)
|
|
if g.IsWhiteListed(host) {
|
|
return nil
|
|
}
|
|
glog.V(0).Infof("Not in whitelist: %s (original RemoteAddr: %s)", host, r.RemoteAddr)
|
|
return fmt.Errorf("Not in whitelist: %s", host)
|
|
}
|
|
|
|
// IsWhiteListed returns true if the given host IP is allowed by the guard.
|
|
// When no whitelist is configured (security inactive), all hosts are allowed.
|
|
func (g *Guard) IsWhiteListed(host string) bool {
|
|
st := g.state.Load()
|
|
if !st.isWriteActive {
|
|
return true
|
|
}
|
|
if st.isEmptyWhiteList {
|
|
return true
|
|
}
|
|
if _, ok := st.whiteListIp[host]; ok {
|
|
return true
|
|
}
|
|
remote := net.ParseIP(host)
|
|
if remote != nil {
|
|
for _, cidrnet := range st.whiteListCIDR {
|
|
if cidrnet.Contains(remote) {
|
|
return true
|
|
}
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// UpdateSigningKeys refreshes the JWT signing keys and their expirations so
|
|
// operators can rotate keys (e.g. via SIGHUP) without restarting the process.
|
|
// It swaps in a new snapshot carrying the existing whitelist, so a concurrent
|
|
// reader sees either the old keys or the new ones, never a torn slice header.
|
|
// Current keys too short for GODEBUG=fips140=only keep the keys in effect, as
|
|
// signing with them would panic; it then returns false.
|
|
func (g *Guard) UpdateSigningKeys(signingKey string, expiresAfterSec int, readSigningKey string, readExpiresAfterSec int, previousSigningKey string, previousReadSigningKey string) bool {
|
|
if err := util.CheckFips140HmacKeys([]string{"signing key", "read signing key"}, []string{signingKey, readSigningKey}); err != nil {
|
|
glog.Errorf("JWT reload: keeping the keys in effect, because %v", err)
|
|
return false
|
|
}
|
|
previousSigningKey = usablePreviousKey("previous signing key", previousSigningKey)
|
|
previousReadSigningKey = usablePreviousKey("previous read signing key", previousReadSigningKey)
|
|
g.updateMu.Lock()
|
|
defer g.updateMu.Unlock()
|
|
next := *g.state.Load()
|
|
next.signingKey = SigningKey(signingKey)
|
|
next.expiresAfterSec = expiresAfterSec
|
|
next.readSigningKey = SigningKey(readSigningKey)
|
|
next.readExpiresAfterSec = readExpiresAfterSec
|
|
next.previousSigningKey = SigningKey(previousSigningKey)
|
|
next.previousReadSigningKey = SigningKey(previousReadSigningKey)
|
|
next.isWriteActive = !next.isEmptyWhiteList || len(next.signingKey) != 0
|
|
g.state.Store(&next)
|
|
return true
|
|
}
|
|
|
|
// ReloadSigningKeys applies the [section] keys after util.LoadConfiguration
|
|
// merged security.toml again. The merge keeps a key the file no longer sets,
|
|
// so the keys are read from the file anew: a removed previous key stops
|
|
// verifying, and a swapped pair comes from one read. A file that cannot be
|
|
// read keeps the keys in effect. It reports whether the new keys are in effect.
|
|
func (g *Guard) ReloadSigningKeys(section string) bool {
|
|
fresh, used, _ := util.ReadConfiguration("security", "")
|
|
if used == "" {
|
|
glog.Warningf("reload %s: security.toml cannot be read, keeping the keys in effect", section)
|
|
return false
|
|
}
|
|
v := util.GetViper()
|
|
return g.UpdateSigningKeys(
|
|
freshKey(fresh, section+".key"),
|
|
v.GetInt(section+".expires_after_seconds"),
|
|
freshKey(fresh, section+".read.key"),
|
|
v.GetInt(section+".read.expires_after_seconds"),
|
|
fresh.GetString(section+".previous_key"),
|
|
fresh.GetString(section+".read.previous_key"),
|
|
)
|
|
}
|
|
|
|
// usablePreviousKey returns key, or "" with an error log when it is too short
|
|
// for HMAC under GODEBUG=fips140=only. Nothing in that mode can sign with such a
|
|
// key, so dropping it rejects no token; keeping the new current keys keeps a
|
|
// reload's outcome the same on servers that pass previous keys and on those
|
|
// that do not, such as the filer's volume signer.
|
|
func usablePreviousKey(name, key string) string {
|
|
if err := util.CheckFips140HmacKey(name, []byte(key)); err != nil {
|
|
glog.Errorf("JWT reload: ignoring the %s, which is shorter than the %d characters FIPS 140 needs for HMAC keys; the new keys are in effect", name, util.Fips140MinHmacKeyBytes)
|
|
return ""
|
|
}
|
|
return key
|
|
}
|
|
|
|
// freshKey returns name as the fresh read sets it, else its merged value, so
|
|
// removing a current key from the file does not turn verification off.
|
|
func freshKey(fresh *util.ViperProxy, name string) string {
|
|
if fresh.IsSet(name) {
|
|
return fresh.GetString(name)
|
|
}
|
|
return util.GetViper().GetString(name)
|
|
}
|
|
|
|
func (g *Guard) UpdateWhiteList(whiteList []string) {
|
|
whiteListIp := make(map[string]struct{})
|
|
whiteListCIDR := make(map[string]*net.IPNet)
|
|
for _, ip := range whiteList {
|
|
if strings.Contains(ip, "/") {
|
|
_, cidrnet, err := net.ParseCIDR(ip)
|
|
if err != nil {
|
|
glog.Errorf("Parse CIDR %s in whitelist failed: %v", ip, err)
|
|
continue
|
|
}
|
|
whiteListCIDR[ip] = cidrnet
|
|
} else {
|
|
whiteListIp[ip] = struct{}{}
|
|
}
|
|
}
|
|
g.updateMu.Lock()
|
|
defer g.updateMu.Unlock()
|
|
next := *g.state.Load()
|
|
next.isEmptyWhiteList = len(whiteListIp) == 0 && len(whiteListCIDR) == 0
|
|
next.isWriteActive = !next.isEmptyWhiteList || len(next.signingKey) != 0
|
|
next.whiteListIp = whiteListIp
|
|
next.whiteListCIDR = whiteListCIDR
|
|
g.state.Store(&next)
|
|
}
|