mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-04 18:17:16 +01:00
* kms/local: load master keys from the provider config The local provider generated its master keys in memory, so a restart or a reload lost them, and with them every object they encrypted. It now reads a keys list from its config. Each entry has a key_id, 32 bytes of key_material in hex or base64, and optional aliases and description. A reload reads the list again, so objects encrypted under a listed key stay readable. A name that's already taken, key material that isn't 32 bytes, or enableOnDemandCreate together with keys fails the provider, and the keys it loaded before the error are cleared. No default key is generated when keys are listed, since whatever it wrapped would be lost on a restart. CreateKey and CreateKeyWithID refuse a name that's taken instead of replacing that key. GetStringMap copies a list of tables like the other nested values it hands out. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * kms/local: give a racing on-demand request the key the first one created CreateKeyWithID refuses a key ID that's taken now, and the on-demand path looked the key up and created it under separate locks. Two requests for the same new key could both miss it, and the second then failed with KMSInternalFailure. The on-demand path checks for the key under the lock it stores it with, and returns the one stored in the meantime, so the first request's data key stays readable. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * kms/local: serve requests that name no key with the configured aws/s3 key With keys configured there is no generated default key, so an SSE-KMS request without a key ID failed even when the bucket-default path (which already resolves to alias/aws/s3) would work. Resolve an empty key ID to alias/aws/s3, as AWS does, so an operator picks the default by giving a configured key that alias instead of a new setting. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * kms/local: refuse a master key that Close cleared mid-request A reload replaces the provider and closes the old one, which zeroes its key material in place. A request that looked up a key before the close could then build its cipher from zeros and store a data key no provider can unwrap. Build the cipher under the provider lock and refuse a key that is no longer registered. 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>
208 lines
6.8 KiB
Go
208 lines
6.8 KiB
Go
package util
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
|
|
"github.com/spf13/viper"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
// `weed server` starts the volume server (SetDefault writers) and the master
|
|
// (GetStringMap readers) back to back. Reads and writes must share the proxy
|
|
// lock — a race-enabled build flags the interleaving the issue reported.
|
|
func TestViperProxySerializesMapReadsAgainstWrites(t *testing.T) {
|
|
p := NewViperProxy(viper.New())
|
|
p.SetDefault("storage.backend.a.enabled", true)
|
|
|
|
var wg sync.WaitGroup
|
|
for i := 0; i < 4; i++ {
|
|
wg.Add(1)
|
|
go func(i int) {
|
|
defer wg.Done()
|
|
for j := 0; j < 200; j++ {
|
|
p.SetDefault(fmt.Sprintf("storage.backend.d%d.enabled", j), i)
|
|
}
|
|
}(i)
|
|
}
|
|
wg.Add(1)
|
|
go func() {
|
|
defer wg.Done()
|
|
for j := 0; j < 200; j++ {
|
|
if got := p.GetStringMap("storage.backend"); len(got) == 0 {
|
|
t.Error("GetStringMap returned no keys")
|
|
return
|
|
}
|
|
}
|
|
}()
|
|
wg.Wait()
|
|
|
|
got := p.GetStringMap("storage.backend")
|
|
assert.Contains(t, got, "a")
|
|
}
|
|
|
|
// A list of tables set from code, rather than read from a file, must come back as a copy too.
|
|
func TestGetStringMapCopiesListsOfTables(t *testing.T) {
|
|
p := NewViperProxy(viper.New())
|
|
p.Set("kms.keys", []map[string]interface{}{{"key_id": "a"}})
|
|
|
|
keys, ok := p.GetStringMap("kms")["keys"].([]map[string]interface{})
|
|
if !assert.True(t, ok) {
|
|
return
|
|
}
|
|
keys[0]["key_id"] = "b"
|
|
|
|
again := p.GetStringMap("kms")["keys"].([]map[string]interface{})
|
|
assert.Equal(t, "a", again[0]["key_id"])
|
|
}
|
|
|
|
// A proxy over its own viper serves the wrapped instance, for configuration
|
|
// loaded from a source other than the shared one.
|
|
func TestNewViperProxyWrapsGivenViper(t *testing.T) {
|
|
v := viper.New()
|
|
v.Set("x.y", "z")
|
|
p := NewViperProxy(v)
|
|
assert.Equal(t, "z", p.GetString("x.y"))
|
|
assert.Equal(t, map[string]interface{}{"y": "z"}, p.GetStringMap("x"))
|
|
}
|
|
|
|
func TestReadConfigurationDropsKeysTheFileNoLongerSets(t *testing.T) {
|
|
dir := t.TempDir()
|
|
saved := ConfigurationFileDirectory
|
|
ConfigurationFileDirectory = DirectoryValueType(dir)
|
|
t.Cleanup(func() { ConfigurationFileDirectory = saved })
|
|
t.Setenv("WEED_READCONF_ENV", "from-env")
|
|
write := func(content string) {
|
|
if err := os.WriteFile(filepath.Join(dir, "readconf_test.toml"), []byte(content), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
v, used, _ := ReadConfiguration("readconf_test", "")
|
|
assert.Empty(t, used, "missing file")
|
|
assert.Equal(t, "from-env", v.GetString("readconf.env"))
|
|
|
|
write("[readconf]\nkey = \"v1\"\n")
|
|
v, used, _ = ReadConfiguration("readconf_test", "")
|
|
assert.Equal(t, filepath.Join(dir, "readconf_test.toml"), used)
|
|
assert.Equal(t, "v1", v.GetString("readconf.key"))
|
|
|
|
write("[readconf]\n")
|
|
v, used, _ = ReadConfiguration("readconf_test", "")
|
|
assert.NotEmpty(t, used)
|
|
assert.Empty(t, v.GetString("readconf.key"))
|
|
assert.Equal(t, "from-env", v.GetString("readconf.env"))
|
|
|
|
write("[readconf\n")
|
|
v, used, _ = ReadConfiguration("readconf_test", "")
|
|
assert.Empty(t, used, "unparsable file")
|
|
assert.Equal(t, "from-env", v.GetString("readconf.env"))
|
|
}
|
|
|
|
func TestReadConfigurationReadsTheGivenFile(t *testing.T) {
|
|
searched, given := t.TempDir(), filepath.Join(t.TempDir(), "readconf_test.toml")
|
|
saved := ConfigurationFileDirectory
|
|
ConfigurationFileDirectory = DirectoryValueType(searched)
|
|
t.Cleanup(func() { ConfigurationFileDirectory = saved })
|
|
if err := os.WriteFile(filepath.Join(searched, "readconf_test.toml"), []byte("[readconf]\nkey = \"searched\"\n"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
_, used, _ := ReadConfiguration("readconf_test", given)
|
|
assert.Empty(t, used, "missing given file must not fall back to a search")
|
|
|
|
if err := os.WriteFile(given, []byte("[readconf]\nkey = \"given\"\n"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
v, used, _ := ReadConfiguration("readconf_test", given)
|
|
assert.Equal(t, given, used)
|
|
assert.Equal(t, "given", v.GetString("readconf.key"))
|
|
}
|
|
|
|
func TestLoadSecurityConfigurationOrErrorRefusesAnUnreadableFile(t *testing.T) {
|
|
dir := t.TempDir()
|
|
t.Setenv("HOME", t.TempDir())
|
|
saved := ConfigurationFileDirectory
|
|
ConfigurationFileDirectory = DirectoryValueType(dir)
|
|
t.Cleanup(func() { ConfigurationFileDirectory = saved })
|
|
|
|
if err := LoadSecurityConfigurationOrError(); err != nil {
|
|
t.Fatalf("missing security.toml: %v", err)
|
|
}
|
|
const secret = "parser-must-not-echo-this"
|
|
if err := os.WriteFile(filepath.Join(dir, "security.toml"), []byte("[grpc.client\nkey = \""+secret+"\"\n"), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
err := LoadSecurityConfigurationOrError()
|
|
if err == nil {
|
|
t.Fatal("malformed security.toml was accepted")
|
|
}
|
|
if strings.Contains(err.Error(), secret) {
|
|
t.Fatal("the error quotes the configuration")
|
|
}
|
|
|
|
// A readable file is merged by the same, single read.
|
|
if err := os.WriteFile(filepath.Join(dir, "security.toml"), []byte("[load_security_test]\nmarker = \"merged\"\n"), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := LoadSecurityConfigurationOrError(); err != nil {
|
|
t.Fatalf("readable security.toml: %v", err)
|
|
}
|
|
if got := GetViper().GetString("load_security_test.marker"); got != "merged" {
|
|
t.Fatalf("settings of the checked file were not merged: %q", got)
|
|
}
|
|
}
|
|
|
|
func TestLoadSecurityConfigurationOrErrorRefusesADirectoryItCannotSearch(t *testing.T) {
|
|
if os.Geteuid() == 0 {
|
|
t.Skip("root can search any directory")
|
|
}
|
|
dir := t.TempDir()
|
|
t.Setenv("HOME", t.TempDir())
|
|
saved := ConfigurationFileDirectory
|
|
ConfigurationFileDirectory = DirectoryValueType(dir)
|
|
t.Cleanup(func() { ConfigurationFileDirectory = saved })
|
|
if err := os.WriteFile(filepath.Join(dir, "security.toml"), []byte("[grpc]\n"), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.Chmod(dir, 0); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { os.Chmod(dir, 0700) })
|
|
if err := LoadSecurityConfigurationOrError(); err == nil {
|
|
t.Fatal("a security.toml in a directory the process cannot search was treated as absent")
|
|
}
|
|
}
|
|
|
|
func TestLoadSecurityConfigurationOrErrorRefusesACandidateItCannotCheck(t *testing.T) {
|
|
dir := t.TempDir()
|
|
t.Setenv("HOME", t.TempDir())
|
|
saved := ConfigurationFileDirectory
|
|
ConfigurationFileDirectory = DirectoryValueType(dir)
|
|
t.Cleanup(func() { ConfigurationFileDirectory = saved })
|
|
// A symlink loop: stat fails with ELOOP, which is not absence.
|
|
loop := filepath.Join(dir, "security.toml")
|
|
if err := os.Symlink(loop, loop); err != nil {
|
|
t.Skipf("symlinks unavailable: %v", err)
|
|
}
|
|
if err := LoadSecurityConfigurationOrError(); err == nil {
|
|
t.Fatal("a security.toml that cannot be checked was treated as absent")
|
|
}
|
|
// A plain file where a configuration directory is expected is absence.
|
|
if err := os.Remove(loop); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
notDir := filepath.Join(dir, "not-a-directory")
|
|
if err := os.WriteFile(notDir, nil, 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ConfigurationFileDirectory = DirectoryValueType(notDir)
|
|
if err := LoadSecurityConfigurationOrError(); err != nil {
|
|
t.Fatalf("configuration directory that is a file: %v", err)
|
|
}
|
|
}
|