mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-04 10:07:28 +01:00
* 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>
291 lines
9.7 KiB
Rust
291 lines
9.7 KiB
Rust
//! The FIPS 140-3 mode a SeaweedFS process runs in.
|
|
//!
|
|
//! One setting states it for a whole deployment: `[fips] mode` in
|
|
//! security.toml, or `WEED_FIPS_MODE`, which wins over the file as viper's
|
|
//! `AutomaticEnv` does for the Go processes. Go processes take their mode from
|
|
//! `GODEBUG=fips140` and refuse to start when it differs from the setting; the
|
|
//! Rust binaries take it from the setting alone, and under [`FipsMode::On`]
|
|
//! and [`FipsMode::Strict`] restrict TLS to what Go's `crypto/tls` allows in
|
|
//! FIPS mode (see [`crate::tls`]). This restricts behaviour only: the
|
|
//! cryptography underneath is still the non-validated aws-lc-rs build.
|
|
//!
|
|
//! Each binary reads `fips.mode` with the parser it uses for the rest of
|
|
//! security.toml and hands the value to [`resolve`], so the mode and the JWT
|
|
//! keys are either both read from a file or both missed.
|
|
|
|
use std::ffi::OsString;
|
|
use std::fmt;
|
|
use std::path::{Path, PathBuf};
|
|
use std::sync::OnceLock;
|
|
|
|
/// The security.toml setting, as `section.key`.
|
|
pub const SETTING: &str = "fips.mode";
|
|
/// The environment variable that overrides [`SETTING`].
|
|
pub const ENV: &str = "WEED_FIPS_MODE";
|
|
|
|
/// The modes `fips.mode` names. Unset reads as [`FipsMode::Off`], which is
|
|
/// how every Rust binary behaved before the setting existed.
|
|
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
|
pub enum FipsMode {
|
|
/// No FIPS restrictions; Go processes need `fips140=off`.
|
|
#[default]
|
|
Off,
|
|
/// FIPS-mode TLS; Go processes need `GODEBUG=fips140=on`.
|
|
On,
|
|
/// FIPS-mode TLS; Go processes need `GODEBUG=fips140=only`, which also
|
|
/// refuses non-approved algorithms outside TLS.
|
|
Strict,
|
|
}
|
|
|
|
impl FipsMode {
|
|
/// Parses a value of `fips.mode`: `off`, `on` or `strict`, or `None` for
|
|
/// an empty value, which leaves the setting unset.
|
|
pub fn parse(value: &str) -> Result<Option<Self>, FipsModeError> {
|
|
match value {
|
|
"" => Ok(None),
|
|
"off" => Ok(Some(Self::Off)),
|
|
"on" => Ok(Some(Self::On)),
|
|
"strict" => Ok(Some(Self::Strict)),
|
|
other => Err(FipsModeError::Unknown {
|
|
source: SETTING.to_string(),
|
|
value: other.to_string(),
|
|
}),
|
|
}
|
|
}
|
|
|
|
/// Whether TLS is restricted to the FIPS 140-3 lists.
|
|
pub fn is_fips(self) -> bool {
|
|
self != Self::Off
|
|
}
|
|
|
|
pub fn as_str(self) -> &'static str {
|
|
match self {
|
|
Self::Off => "off",
|
|
Self::On => "on",
|
|
Self::Strict => "strict",
|
|
}
|
|
}
|
|
}
|
|
|
|
impl fmt::Display for FipsMode {
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
f.write_str(self.as_str())
|
|
}
|
|
}
|
|
|
|
/// Why the mode could not be resolved or recorded.
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
|
pub enum FipsModeError {
|
|
/// `source` holds a value that is not `off`, `on` or `strict`.
|
|
Unknown { source: String, value: String },
|
|
/// The process mode was already set to a different mode.
|
|
AlreadySet { set: FipsMode, wanted: FipsMode },
|
|
/// The security.toml at `path` exists but could not be read or parsed, so
|
|
/// whether it sets `fips.mode` is unknown.
|
|
File { path: PathBuf, reason: String },
|
|
}
|
|
|
|
impl fmt::Display for FipsModeError {
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
match self {
|
|
Self::Unknown { source, value } => {
|
|
write!(f, "{source} is {value:?}; want off, on or strict")
|
|
}
|
|
Self::AlreadySet { set, wanted } => {
|
|
write!(f, "FIPS mode is already {set}, cannot switch to {wanted}")
|
|
}
|
|
Self::File { path, reason } => write!(
|
|
f,
|
|
"{}: {reason}; cannot tell whether it sets {SETTING}",
|
|
path.display()
|
|
),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl std::error::Error for FipsModeError {}
|
|
|
|
/// A resolved `fips.mode`: the mode, and where it was set (`None` when
|
|
/// nothing set it and the mode is [`FipsMode::Off`]).
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
|
pub struct Setting {
|
|
pub mode: FipsMode,
|
|
pub source: Option<String>,
|
|
}
|
|
|
|
impl fmt::Display for Setting {
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
match &self.source {
|
|
Some(source) => write!(f, "FIPS mode {} ({source})", self.mode),
|
|
None => write!(f, "FIPS mode {} ({SETTING} unset)", self.mode),
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Resolves the mode of a process whose own security.toml loader read
|
|
/// `file_value` for `fips.mode` from `file` (`None` when there is no file or
|
|
/// it does not set the key): `WEED_FIPS_MODE` when set and non-empty, else
|
|
/// `file_value`, else [`FipsMode::Off`]. An unknown value is an error naming
|
|
/// where it came from.
|
|
pub fn resolve(
|
|
file: Option<&Path>,
|
|
file_value: Option<&str>,
|
|
env: &dyn Fn(&str) -> Option<OsString>,
|
|
) -> Result<Setting, FipsModeError> {
|
|
let from_env = env(ENV)
|
|
.map(|v| v.to_string_lossy().into_owned())
|
|
.filter(|v| !v.is_empty());
|
|
let from_file = || {
|
|
let source = match file {
|
|
Some(path) => format!("{}: {SETTING}", path.display()),
|
|
None => SETTING.to_string(),
|
|
};
|
|
file_value.map(|value| (source, value.to_string()))
|
|
};
|
|
let Some((source, value)) = from_env.map(|v| (ENV.to_string(), v)).or_else(from_file) else {
|
|
return Ok(Setting {
|
|
mode: FipsMode::Off,
|
|
source: None,
|
|
});
|
|
};
|
|
match FipsMode::parse(&value) {
|
|
Ok(Some(mode)) => Ok(Setting {
|
|
mode,
|
|
source: Some(source),
|
|
}),
|
|
Ok(None) => Ok(Setting {
|
|
mode: FipsMode::Off,
|
|
source: None,
|
|
}),
|
|
Err(_) => Err(FipsModeError::Unknown { source, value }),
|
|
}
|
|
}
|
|
|
|
static PROCESS_MODE: OnceLock<FipsMode> = OnceLock::new();
|
|
|
|
/// Records the mode this process runs in. A second call with the same mode is
|
|
/// a no-op; one with another mode is an error, since providers and
|
|
/// configurations built under the first would not change.
|
|
pub fn set_process_mode(mode: FipsMode) -> Result<(), FipsModeError> {
|
|
let set = *PROCESS_MODE.get_or_init(|| mode);
|
|
if set != mode {
|
|
return Err(FipsModeError::AlreadySet { set, wanted: mode });
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
/// The mode recorded by [`set_process_mode`], [`FipsMode::Off`] before.
|
|
pub fn process_mode() -> FipsMode {
|
|
PROCESS_MODE.get().copied().unwrap_or_default()
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
fn env_of(pairs: &'static [(&'static str, &'static str)]) -> impl Fn(&str) -> Option<OsString> {
|
|
move |key| {
|
|
pairs
|
|
.iter()
|
|
.find(|(k, _)| *k == key)
|
|
.map(|(_, v)| OsString::from(v))
|
|
}
|
|
}
|
|
|
|
const FILE_PATH: &str = "/etc/seaweedfs/security.toml";
|
|
|
|
#[test]
|
|
fn parse_knows_three_modes_and_empty() {
|
|
assert_eq!(FipsMode::parse(""), Ok(None));
|
|
assert_eq!(FipsMode::parse("off"), Ok(Some(FipsMode::Off)));
|
|
assert_eq!(FipsMode::parse("on"), Ok(Some(FipsMode::On)));
|
|
assert_eq!(FipsMode::parse("strict"), Ok(Some(FipsMode::Strict)));
|
|
assert!(FipsMode::parse("only").is_err());
|
|
assert!(FipsMode::parse("On").is_err());
|
|
}
|
|
|
|
#[test]
|
|
fn unset_everywhere_is_off() {
|
|
let off = Setting {
|
|
mode: FipsMode::Off,
|
|
source: None,
|
|
};
|
|
assert_eq!(resolve(None, None, &env_of(&[])), Ok(off.clone()));
|
|
assert_eq!(
|
|
resolve(Some(Path::new(FILE_PATH)), None, &env_of(&[])),
|
|
Ok(off.clone())
|
|
);
|
|
assert_eq!(
|
|
resolve(Some(Path::new(FILE_PATH)), Some(""), &env_of(&[])),
|
|
Ok(off.clone())
|
|
);
|
|
assert_eq!(off.to_string(), "FIPS mode off (fips.mode unset)");
|
|
}
|
|
|
|
#[test]
|
|
fn the_file_sets_the_mode() {
|
|
let setting = resolve(Some(Path::new(FILE_PATH)), Some("strict"), &env_of(&[])).unwrap();
|
|
assert_eq!(setting.mode, FipsMode::Strict);
|
|
assert_eq!(
|
|
setting.to_string(),
|
|
"FIPS mode strict (/etc/seaweedfs/security.toml: fips.mode)"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn the_environment_wins_over_the_file() {
|
|
let env = env_of(&[("WEED_FIPS_MODE", "on")]);
|
|
let setting = resolve(Some(Path::new(FILE_PATH)), Some("off"), &env).unwrap();
|
|
assert_eq!(setting.mode, FipsMode::On);
|
|
assert_eq!(setting.to_string(), "FIPS mode on (WEED_FIPS_MODE)");
|
|
assert_eq!(resolve(None, None, &env).unwrap().mode, FipsMode::On);
|
|
assert_eq!(
|
|
resolve(Some(Path::new(FILE_PATH)), Some("[\"strict\"]"), &env)
|
|
.unwrap()
|
|
.mode,
|
|
FipsMode::On
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn an_empty_environment_variable_counts_as_unset() {
|
|
let env = env_of(&[("WEED_FIPS_MODE", "")]);
|
|
assert_eq!(
|
|
resolve(Some(Path::new(FILE_PATH)), Some("on"), &env)
|
|
.unwrap()
|
|
.mode,
|
|
FipsMode::On
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn an_unknown_value_is_refused_naming_its_source() {
|
|
let err = resolve(None, None, &env_of(&[("WEED_FIPS_MODE", "yes")])).unwrap_err();
|
|
assert_eq!(
|
|
err.to_string(),
|
|
"WEED_FIPS_MODE is \"yes\"; want off, on or strict"
|
|
);
|
|
let err = resolve(
|
|
Some(Path::new(FILE_PATH)),
|
|
Some("[\"strict\"]"),
|
|
&env_of(&[]),
|
|
)
|
|
.unwrap_err();
|
|
assert_eq!(
|
|
err.to_string(),
|
|
"/etc/seaweedfs/security.toml: fips.mode is \"[\\\"strict\\\"]\"; want off, on or strict"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn the_process_mode_is_set_once() {
|
|
assert_eq!(set_process_mode(process_mode()), Ok(()));
|
|
let other = if process_mode() == FipsMode::On {
|
|
FipsMode::Off
|
|
} else {
|
|
FipsMode::On
|
|
};
|
|
assert!(set_process_mode(other).is_err());
|
|
}
|
|
}
|