Files
seaweedfs/weed/util/fips140_mode_test.go
T
5e4b2fc42f docker: one FIPS mode for the Go processes and the Rust binaries (#13296)
* docker: one FIPS mode for the Go processes and the Rust binaries

In the image, Go processes ran with fips140=on by default (#7590) while the
Rust binaries, which take their mode from fips.mode alone, ran with
unrestricted TLS; and WEED_FIPS_MODE=off or strict made every Go process
refuse to start, since the entrypoint's fips140=on did not match.

The entrypoint now states the image's mode once, as
WEED_FIPS_MODE_DEFAULT=on, and gives Go processes the fips140 GODEBUG that
matches WEED_FIPS_MODE, else that default. The Rust binaries use the
default when neither WEED_FIPS_MODE nor fips.mode sets a mode; Go processes
check their GODEBUG against it and refuse to start on a mismatch, naming
WEED_FIPS_MODE as the way to pick another mode. Setting
WEED_FIPS_MODE_DEFAULT empty leaves every process in its own default, as
outside the image.

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

* docker: take the image's FIPS mode from a fips140 the caller sets

GODEBUG=fips140=off, the entrypoint's advice since #7590, made every Go
process exit once the Rust binaries joined the image's mode. The entrypoint
now maps the caller's fips140 to WEED_FIPS_MODE_DEFAULT, so Go and Rust run
in that mode and the old advice keeps working.

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

* docker: say fips.mode alone cannot change the image's FIPS mode

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

* docker: read an empty fips140 as off and debug as on, as Go does

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

* docker: warn when GODEBUG fips140 picks the image's FIPS mode

The entrypoint keeps translating a caller's fips140 into the mode for the
Rust binaries, so existing docker run setups keep starting, but logs that
WEED_FIPS_MODE is the setting that also covers launches without the
entrypoint.

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

* docker: set the FIPS mode after dropping privileges

Running as root, the entrypoint re-runs itself through su-exec, and the
second pass took the fips140 the first pass had exported for the caller's
own, warning on default starts and prepending fips140 twice.

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-12-03 10:29:05 -08:00

182 lines
6.0 KiB
Go

package util
import (
"strings"
"testing"
"github.com/spf13/viper"
)
// setFIPS140Godebug makes this test see the fips140 GODEBUG value godebug
// ("off", "on" or "only") without running the test binary under it.
func setFIPS140Godebug(t *testing.T, godebug string) {
t.Helper()
setFIPS140Enforced(t, godebug == "only")
saved := fips140Enabled
fips140Enabled = func() bool { return godebug != "off" }
t.Cleanup(func() { fips140Enabled = saved })
}
func TestCheckFipsMode(t *testing.T) {
cases := []struct {
mode string
godebug string
wantErr string
}{
{"", "off", ""},
{"", "on", ""},
{"", "only", ""},
{"off", "off", ""},
{"off", "on", `"off", which needs GODEBUG=fips140=off; this process runs with fips140=on`},
{"off", "only", `"off", which needs GODEBUG=fips140=off; this process runs with fips140=only`},
{"on", "on", ""},
{"on", "off", `"on", which needs GODEBUG=fips140=on; this process runs with fips140=off`},
{"on", "only", `"on", which needs GODEBUG=fips140=on; this process runs with fips140=only`},
{"strict", "only", ""},
{"strict", "off", `"strict", which needs GODEBUG=fips140=only; this process runs with fips140=off`},
{"strict", "on", `"strict", which needs GODEBUG=fips140=only; this process runs with fips140=on`},
{"only", "only", `"only"; want off, on or strict`},
{"On", "on", `"On"; want off, on or strict`},
}
for _, tc := range cases {
setFIPS140Godebug(t, tc.godebug)
err := CheckFipsMode("security.toml: fips.mode", tc.mode)
if tc.wantErr == "" {
if err != nil {
t.Errorf("mode %q, fips140=%s: got %v, want no error", tc.mode, tc.godebug, err)
}
continue
}
if err == nil {
t.Errorf("mode %q, fips140=%s: got no error, want %q", tc.mode, tc.godebug, tc.wantErr)
continue
}
if want := "security.toml: fips.mode is " + tc.wantErr; err.Error() != want {
t.Errorf("mode %q, fips140=%s: got %q, want %q", tc.mode, tc.godebug, err, want)
}
}
}
func TestFipsModeSourceNamesTheEnvironmentWhenSet(t *testing.T) {
t.Setenv(FipsModeEnv, "")
if got := fipsModeSource("/etc/seaweedfs/security.toml"); got != "/etc/seaweedfs/security.toml: fips.mode" {
t.Errorf("without %s: got %q", FipsModeEnv, got)
}
t.Setenv(FipsModeEnv, "strict")
if got := fipsModeSource("/etc/seaweedfs/security.toml"); got != FipsModeEnv {
t.Errorf("with %s: got %q", FipsModeEnv, got)
}
}
// securityToml returns a ViperProxy over toml as security.toml, with the
// WEED_ environment overrides LoadConfiguration applies.
func securityToml(t *testing.T, toml string) *ViperProxy {
t.Helper()
v := viper.New()
setEnvOverrides(v)
v.SetConfigType("toml")
if err := v.ReadConfig(strings.NewReader(toml)); err != nil {
t.Fatal(err)
}
return NewViperProxy(v)
}
func TestFipsModeValueRefusesValuesThatAreNotStrings(t *testing.T) {
t.Setenv(FipsModeEnv, "")
for _, toml := range []string{
`[fips]
mode = ["strict"]
`,
`[fips]
mode = { value = "strict" }
`,
`[fips]
mode = true
`,
} {
mode, err := FipsModeValue("security.toml: fips.mode", securityToml(t, toml))
if err == nil || !strings.HasPrefix(err.Error(), "security.toml: fips.mode is ") || !strings.Contains(err.Error(), "not a string") {
t.Errorf("%q: got mode %q, error %v; want a not-a-string error", toml, mode, err)
}
}
}
func TestFipsModeValueReadsStringsAndUnset(t *testing.T) {
t.Setenv(FipsModeEnv, "")
cases := []struct{ toml, want string }{
{"", ""},
{`[grpc]
ca = ""
`, ""},
{`[fips]
mode = ""
`, ""},
{`[fips]
mode = "strict"
`, "strict"},
{`[FIPS]
Mode = 'on'
`, "on"},
}
for _, tc := range cases {
mode, err := FipsModeValue("security.toml: fips.mode", securityToml(t, tc.toml))
if err != nil || mode != tc.want {
t.Errorf("%q: got %q, %v; want %q", tc.toml, mode, err, tc.want)
}
}
}
func TestFipsModeEnvironmentWinsOverANonStringFile(t *testing.T) {
t.Setenv(FipsModeEnv, "on")
mode, err := FipsModeValue(FipsModeEnv, securityToml(t, `[fips]
mode = ["strict"]
`))
if err != nil || mode != "on" {
t.Fatalf("got %q, %v; want %q from %s", mode, err, "on", FipsModeEnv)
}
}
func TestFipsModeIsReadFromTheEnvironment(t *testing.T) {
t.Setenv(FipsModeEnv, "strict")
setFIPS140Godebug(t, "on")
err := CheckFipsMode(fipsModeSource("security.toml"), GetViper().GetString(FipsModeSetting))
if err == nil || !strings.HasPrefix(err.Error(), FipsModeEnv+` is "strict"`) {
t.Fatalf("got %v, want the %s value refused", err, FipsModeEnv)
}
}
func TestFipsModeDefaultAppliesWhenNothingElseSetsTheMode(t *testing.T) {
t.Setenv(FipsModeEnv, "")
t.Setenv(FipsModeDefaultEnv, "on")
cases := []struct{ env, toml, wantSource, wantMode string }{
{"", "", FipsModeDefaultEnv, "on"},
{"", "[fips]\nmode = \"\"\n", FipsModeDefaultEnv, "on"},
{"", "[fips]\nmode = \"strict\"\n", "security.toml: fips.mode", "strict"},
{"off", "", FipsModeEnv, "off"},
}
for _, tc := range cases {
t.Setenv(FipsModeEnv, tc.env)
source, mode, err := resolveFipsMode("security.toml", securityToml(t, tc.toml))
if err != nil || source != tc.wantSource || mode != tc.wantMode {
t.Errorf("env %q, file %q: got %q from %q, %v; want %q from %q", tc.env, tc.toml, mode, source, err, tc.wantMode, tc.wantSource)
}
}
t.Setenv(FipsModeEnv, "")
t.Setenv(FipsModeDefaultEnv, "")
if source, mode, err := resolveFipsMode("security.toml", securityToml(t, "")); err != nil || mode != "" {
t.Errorf("an empty default: got %q from %q, %v; want no mode", mode, source, err)
}
}
func TestFipsModeDefaultMismatchSaysHowToPickAnotherMode(t *testing.T) {
setFIPS140Godebug(t, "off")
err := checkResolvedFipsMode(FipsModeDefaultEnv, "on")
want := FipsModeDefaultEnv + ` is "on", which needs GODEBUG=fips140=on; this process runs with fips140=off; to run in another mode, set WEED_FIPS_MODE (or fips.mode) instead of the fips140 GODEBUG`
if err == nil || err.Error() != want {
t.Fatalf("got %v\nwant %s", err, want)
}
if err := checkResolvedFipsMode("security.toml: fips.mode", "on"); err == nil || strings.Contains(err.Error(), "instead of") {
t.Fatalf("a mismatch with the file should not carry the hint: %v", err)
}
}