Files
seaweedfs/weed/util/config.go
T
397a156104 security: a fips.mode setting that Go checks against GODEBUG and the Rust binaries apply to TLS (#13029)
* util: refuse to start when fips.mode in security.toml disagrees with GODEBUG

security.toml gains [fips] mode = "off" | "on" | "strict", also settable
as WEED_FIPS_MODE: one setting that states the FIPS 140-3 mode of every
process in a deployment, so the Rust binaries, which do not run on the Go
runtime, can follow it. Go processes keep taking their behaviour from
GODEBUG=fips140 and now check it against the setting:
LoadSecurityConfiguration and `weed worker` exit when "off" runs without
fips140=off, "on" without fips140=on, or "strict" without fips140=only,
naming the setting and the GODEBUG value it needs. An unknown value is
refused; unset or empty changes nothing.

Refs #12839

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* rust: fips.mode with FIPS-mode TLS in weed-volume, weed-worker and weed-vector

seaweed-common gains a fips module: FipsMode resolved from fips.mode in
the security.toml a binary uses, with WEED_FIPS_MODE over it. Each binary
resolves it once at startup, logs it and installs the matching rustls
provider as the process default, refusing to start on an unknown value or
when another provider was installed first.

Under "on" and "strict" the provider is aws-lc-rs filtered to the lists
Go's crypto/tls applies in FIPS mode: TLS 1.3 AES-GCM suites and TLS 1.2
ECDHE AES-GCM suites (no ChaCha20-Poly1305), X25519MLKEM768,
SecP256r1MLKEM768, P-256 and P-384 (no plain X25519), Go's ten signature
schemes (no ML-DSA). Configurations SeaweedFS builds require the Extended
Master Secret in that mode, and the volume server refuses a non-FIPS suite
in tls.cipher_suites instead of falling back. sw-client installs the same
provider instead of ring. Unset or "off" keeps aws-lc-rs's default
provider unchanged.

This is behavioural parity only: the provider still runs on the
non-validated aws-lc-rs build.

Refs #12839

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* util: refuse a fips.mode that is not a string

viper's GetString turns an array or a table into "", so mode = ["strict"]
under [fips] skipped the GODEBUG check while the Rust binaries refused the
same file. RequireFipsMode now reads the raw value (ViperProxy.Get, under
its lock) and exits when it is not a string, naming its source; an absent
or empty value still means unset, and WEED_FIPS_MODE still wins.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* rust: read fips.mode with the parser that reads the JWT keys

seaweed_common::fips::resolve parsed security.toml with the toml crate,
while the volume server reads the rest of the file with its own lenient
loader. A file with [fips] mode = "on" beside an unquoted value the loader
accepts resolved to off and logged "fips.mode unset", although the JWT
keys from the same file were applied. Each binary now reads fips.mode
with the loader it uses for everything else and hands the value to
resolve: the volume server through its line parser, the worker and vector
engine through their parsed Security table, which already refuses a file
that is not TOML. A security.toml the volume server finds but cannot read
stops startup. A missing setting is still off, and WEED_FIPS_MODE still
wins. seaweed-common no longer depends on toml.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* sw-client: offer only FIPS signature schemes with insecure_skip_verify

With https.client.insecure_skip_verify, reqwest installs its own verifier,
which advertises a fixed scheme list including SHA-1 and Ed448 whatever
the process provider allows. In FIPS mode the volume transport now builds
its rustls configuration itself on the process provider: the server
certificate and host name are not checked, as the setting asks, but
handshake signatures are verified with the provider's algorithms, only its
schemes are offered, and TLS 1.2 requires the Extended Master Secret.
Outside FIPS mode the reqwest path is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* seaweed-common: sign only with Go 1.27's FIPS schemes, allow ML-DSA

The FIPS provider kept aws-lc-rs's key provider, so a FIPS-mode listener
signed handshakes with whatever scheme its key and the peer agreed on,
outside the list the provider verifies with. Keys now load through a
provider that limits each key to the FIPS signature schemes and refuses a
key that can sign none of them, such as Ed448; the volume server and the
worker's gRPC TLS name the certificate and key files in that error.

The lists follow Go 1.27.4, which go.mod now requires: its
defaults_fips140.go allows ML-DSA-44, -65 and -87 as signature schemes and
ML-DSA certificate keys (for Go's native module only), so they are allowed
here as well. Go also allows MLKEM1024 but does not enable it by default,
so it stays out of the key exchange list as in Go's defaults. "off" keeps
the default key provider.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* mount: skip the truncate manifest test on freebsd like its fake filer

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Chris Lu <chrislusf@users.noreply.github.com>
2026-11-27 12:47:44 -08:00

269 lines
7.2 KiB
Go

package util
import (
"strings"
"sync"
"time"
"github.com/spf13/viper"
"github.com/seaweedfs/seaweedfs/weed/glog"
)
var (
ConfigurationFileDirectory DirectoryValueType
loadSecurityConfigOnce sync.Once
)
type DirectoryValueType string
func (s *DirectoryValueType) Set(value string) error {
*s = DirectoryValueType(value)
return nil
}
func (s *DirectoryValueType) String() string {
return string(*s)
}
type Configuration interface {
GetString(key string) string
GetBool(key string) bool
GetInt(key string) int
GetStringSlice(key string) []string
SetDefault(key string, value interface{})
}
// securityConfigFile names the security.toml that LoadConfiguration read, or
// "security.toml" when the settings came from the environment only.
func securityConfigFile() string {
if f := viper.ConfigFileUsed(); f != "" {
return f
}
return "security.toml"
}
// LoadSecurityConfiguration merges security.toml once per process and exits
// when fips.mode does not match the fips140 GODEBUG (RequireFipsMode) or a
// JWT signing key is too short for GODEBUG=fips140=only; outside that mode it
// warns about such a key.
func LoadSecurityConfiguration() {
loadSecurityConfigOnce.Do(func() {
LoadConfiguration("security", false)
RequireFipsMode()
if err := CheckFips140SigningKeys(securityConfigFile(), GetViper()); err != nil {
glog.Fatalf("%s: this process runs with GODEBUG=fips140=only, and stops because %v; replace them on every server that uses them, with or without downtime, see %s",
securityConfigFile(), err, JwtSigningKeysWiki)
}
})
}
func LoadConfiguration(configFileName string, required bool) (loaded bool) {
// MergeInConfig mutates the shared viper that ViperProxy serializes;
// take the same lock so a merge cannot race a reader or SetDefault.
vp.Lock()
defer vp.Unlock()
setConfigurationPaths(viper.GetViper(), configFileName)
if err := viper.MergeInConfig(); err != nil { // Handle errors reading the config file
if strings.Contains(err.Error(), "Not Found") {
glog.V(1).Infof("Reading %s: %v", viper.ConfigFileUsed(), err)
} else {
// If the config is required, fail immediately
if required {
glog.Fatalf("Reading %s: %v", viper.ConfigFileUsed(), err)
}
// If the config is optional, log a warning but don't crash
glog.Warningf("Reading %s: %v. Skipping optional configuration.", viper.ConfigFileUsed(), err)
}
if required {
glog.Fatalf("Failed to load %s.toml file from current directory, or $HOME/.seaweedfs/, or /etc/seaweedfs/"+
"\n\nPlease use this command to generate the default %s.toml file\n"+
" weed scaffold -config=%s -output=.\n\n\n",
configFileName, configFileName, configFileName)
}
return false
}
glog.V(1).Infof("Reading %s.toml from %s", configFileName, viper.ConfigFileUsed())
return true
}
// ReadConfiguration reads configFileName into a new viper with the WEED_
// environment overrides. Unlike LoadConfiguration it does not merge, so a key
// the file no longer sets reads as unset. A non-empty configFile is read in
// place of searching the configuration directories. used is the file read, or
// empty when none could be; the environment still applies then.
func ReadConfiguration(configFileName, configFile string) (v *ViperProxy, used string) {
fresh := viper.New()
setEnvOverrides(fresh)
if configFile != "" {
fresh.SetConfigFile(configFile)
} else {
setConfigurationPaths(fresh, configFileName)
}
if err := fresh.ReadInConfig(); err != nil {
glog.V(1).Infof("Reading %s: %v", configFileName, err)
return NewViperProxy(fresh), ""
}
return NewViperProxy(fresh), fresh.ConfigFileUsed()
}
func setConfigurationPaths(v *viper.Viper, configFileName string) {
v.SetConfigName(configFileName)
v.AddConfigPath(ResolvePath(ConfigurationFileDirectory.String()))
v.AddConfigPath(".")
v.AddConfigPath("$HOME/.seaweedfs")
v.AddConfigPath("/usr/local/etc/seaweedfs/")
v.AddConfigPath("/etc/seaweedfs/")
}
func setEnvOverrides(v *viper.Viper) {
v.AutomaticEnv()
v.SetEnvPrefix("weed")
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
}
// ViperProxy serializes access to the global viper. The wrapped Viper is a
// named field, not embedded, so every method must be declared here under the
// mutex — a promoted method would take no lock and race, e.g. GetStringMap
// against a concurrent SetDefault during `weed server` startup.
type ViperProxy struct {
v *viper.Viper
sync.Mutex
}
var (
vp = &ViperProxy{}
)
func (vp *ViperProxy) SetDefault(key string, value interface{}) {
vp.Lock()
defer vp.Unlock()
vp.v.SetDefault(key, value)
}
func (vp *ViperProxy) AutomaticEnv() {
vp.Lock()
defer vp.Unlock()
vp.v.AutomaticEnv()
}
// Get returns the value at key as viper holds it, without converting it, so
// a caller can tell a string from an array or a table.
func (vp *ViperProxy) Get(key string) interface{} {
vp.Lock()
defer vp.Unlock()
return deepCopyValue(vp.v.Get(key))
}
func (vp *ViperProxy) GetString(key string) string {
vp.Lock()
defer vp.Unlock()
return vp.v.GetString(key)
}
func (vp *ViperProxy) GetBool(key string) bool {
vp.Lock()
defer vp.Unlock()
return vp.v.GetBool(key)
}
func (vp *ViperProxy) GetInt(key string) int {
vp.Lock()
defer vp.Unlock()
return vp.v.GetInt(key)
}
func (vp *ViperProxy) GetStringSlice(key string) []string {
vp.Lock()
defer vp.Unlock()
// a []string value comes back uncast — the same shared subtree issue
// GetStringMap has, so hand the caller a copy
return append([]string(nil), vp.v.GetStringSlice(key)...)
}
func (vp *ViperProxy) GetStringMap(key string) map[string]interface{} {
vp.Lock()
defer vp.Unlock()
// viper hands back its internal subtree, so a caller iterating it after
// the lock is released would race the next SetDefault — copy it out.
return deepCopyStringMap(vp.v.GetStringMap(key))
}
func deepCopyStringMap(m map[string]interface{}) map[string]interface{} {
out := make(map[string]interface{}, len(m))
for k, v := range m {
out[k] = deepCopyValue(v)
}
return out
}
func deepCopyValue(v interface{}) interface{} {
switch nested := v.(type) {
case map[string]interface{}:
return deepCopyStringMap(nested)
case []interface{}:
out := make([]interface{}, len(nested))
for i, item := range nested {
out[i] = deepCopyValue(item)
}
return out
default:
return v
}
}
func (vp *ViperProxy) GetUint32(key string) uint32 {
vp.Lock()
defer vp.Unlock()
return vp.v.GetUint32(key)
}
func (vp *ViperProxy) GetFloat64(key string) float64 {
vp.Lock()
defer vp.Unlock()
return vp.v.GetFloat64(key)
}
func (vp *ViperProxy) GetDuration(key string) time.Duration {
vp.Lock()
defer vp.Unlock()
return vp.v.GetDuration(key)
}
func (vp *ViperProxy) Set(key string, value interface{}) {
vp.Lock()
defer vp.Unlock()
vp.v.Set(key, value)
}
func (vp *ViperProxy) IsSet(key string) bool {
vp.Lock()
defer vp.Unlock()
return vp.v.IsSet(key)
}
func (vp *ViperProxy) AllKeys() []string {
vp.Lock()
defer vp.Unlock()
return vp.v.AllKeys()
}
// NewViperProxy wraps a specific viper — for configuration loaded from a
// source other than the shared instance GetViper returns.
func NewViperProxy(v *viper.Viper) *ViperProxy {
return &ViperProxy{v: v}
}
func GetViper() *ViperProxy {
vp.Lock()
defer vp.Unlock()
if vp.v == nil {
vp.v = viper.GetViper()
setEnvOverrides(vp.v)
}
return vp
}