Files
seaweedfs/weed/util/fips140_keys.go
T
a9fda6b50f filer clients: authenticate filer gRPC calls with per-call admin tokens (#13039)
* 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>
2026-11-27 18:53:59 -08:00

92 lines
4.0 KiB
Go

package util
import (
"fmt"
"strings"
"github.com/seaweedfs/seaweedfs/weed/glog"
)
// Fips140MinHmacKeyBytes is the shortest key crypto/hmac accepts under
// GODEBUG=fips140=only: hmac.New panics there on keys under 112 bits.
const Fips140MinHmacKeyBytes = 14
// JwtSigningKeysWiki documents switching JWT signing keys without downtime.
const JwtSigningKeysWiki = "https://github.com/seaweedfs/seaweedfs/wiki/Cryptography-and-FIPS-Compliance#jwt-signing-keys"
// jwtSigningKeySettings are the security.toml settings that key HS256 JWTs,
// for signing or for verifying.
var jwtSigningKeySettings = []string{
"jwt.signing.key",
"jwt.signing.previous_key",
"jwt.signing.read.key",
"jwt.signing.read.previous_key",
"jwt.filer_signing.key",
"jwt.filer_signing.previous_key",
"jwt.filer_signing.read.key",
"jwt.filer_signing.read.previous_key",
"jwt.filer_signing.grpc_key",
"jwt.filer_signing.grpc_previous_key",
"jwt.filer_signing.subscribe.key",
}
// CheckFips140HmacKey returns an error naming setting when GODEBUG=fips140=only
// is in effect and key is set but too short for hmac.New, so a short key is
// refused where it is configured instead of panicking where it is first used.
func CheckFips140HmacKey(setting string, key []byte) error {
if len(key) == 0 || len(key) >= Fips140MinHmacKeyBytes || !fips140Enforced() {
return nil
}
return fmt.Errorf("%s is %d bytes; GODEBUG=fips140=only requires HMAC keys of at least %d bytes", setting, len(key), Fips140MinHmacKeyBytes)
}
// CheckFips140HmacKeys is CheckFips140HmacKey for several keys, settings[i]
// naming keys[i]. Its error names every key that is too short, so one startup
// shows them all: "jwt.signing.key (10 characters) and jwt.filer_signing.key
// (8 characters) are shorter than the 14 characters FIPS 140 needs for HMAC
// keys".
func CheckFips140HmacKeys(settings []string, keys []string) error {
var short []string
for i, setting := range settings {
if CheckFips140HmacKey(setting, []byte(keys[i])) != nil {
short = append(short, fmt.Sprintf("%s (%d characters)", setting, len(keys[i])))
}
}
switch len(short) {
case 0:
return nil
case 1:
return fmt.Errorf("%s is shorter than the %d characters FIPS 140 needs for HMAC keys", short[0], Fips140MinHmacKeyBytes)
}
list := strings.Join(short[:len(short)-1], ", ") + " and " + short[len(short)-1]
return fmt.Errorf("%s are shorter than the %d characters FIPS 140 needs for HMAC keys", list, Fips140MinHmacKeyBytes)
}
// CheckFips140SigningKeys runs CheckFips140HmacKeys on every JWT signing key
// setting in v, read from file. Outside fips140=only, where a short key works,
// it logs shortJwtKeyWarning for each one instead.
func CheckFips140SigningKeys(file string, v interface{ GetString(string) string }) error {
keys := make([]string, len(jwtSigningKeySettings))
for i, setting := range jwtSigningKeySettings {
keys[i] = v.GetString(setting)
if warning := shortJwtKeyWarning(file, setting, []byte(keys[i])); warning != "" {
glog.Warning(warning)
}
}
return CheckFips140HmacKeys(jwtSigningKeySettings, keys)
}
// shortJwtKeyWarning returns the warning for a JWT key in file that works
// outside GODEBUG=fips140=only but is under Fips140MinHmacKeyBytes, or "" when
// there is nothing to warn about. The warning asks for a switch to a longer key
// while the short one still works. A previous_key gets none: it holds the old
// key during exactly that switch, and under fips140=only the startup refusal
// names it.
func shortJwtKeyWarning(file, setting string, key []byte) string {
if len(key) == 0 || len(key) >= Fips140MinHmacKeyBytes || fips140Enforced() || strings.HasSuffix(setting, "previous_key") {
return ""
}
return fmt.Sprintf("%s: this process does not run with GODEBUG=fips140=only, so %s works, but under GODEBUG=fips140=only this server would not start: the key is shorter than the %d characters FIPS 140 needs for HMAC keys (it has %d); to replace it on every server that uses it without downtime, see %s",
file, setting, Fips140MinHmacKeyBytes, len(key), JwtSigningKeysWiki)
}