Files
seaweedfs/weed/kms/config_loader_test.go
T
3764534dd1 kms: read filer.toml and the identities file with one parser (#13313)
* kms: read filer.toml and the identities file with one parser

The KMS settings from filer.toml and from the identities file went through
two paths: the JSON one was wrapped in an adapter so it could be read like
viper. Both are now parsed into the same settings and loaded by the same
code. Viper lowercases the provider tables in filer.toml but not the values
that name a provider, so a mixed-case default_provider or bucket provider
never matched and failed the load. Those values are now lowercased for
filer.toml. Providers load in name order, a providers or buckets section
that isn't a table fails the load instead of being ignored, and so does a
default_provider that isn't a string.

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

* kms: test the parser's section types and JSON provider names

A providers or buckets section that isn't a table and a default_provider
that isn't a string fail the parse, and a JSON config keeps the case of
the provider names it uses. The check-once test now loads through the
whole loader, which sets the global provider, so it clears it after.

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

* kms: pick the first provider by name when no default is set

ListKMSProviders iterated the provider map, so with several providers
and no default_provider the global SSE-KMS provider changed between
loads, and objects written under one could not be read after a restart.

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

* s3api: report KMS load errors from the identities file

Any failure loading the identities file's kms block was logged at V(2)
as no KMS configuration, which hid the parser's errors and a
default_provider that does not exist. Only a missing block is quiet now.

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-02 13:16:52 -08:00

48 lines
1.5 KiB
Go

package kms
import (
"strings"
"testing"
"github.com/seaweedfs/seaweedfs/weed/util"
)
func TestJSONConfigKeepsTheCaseOfProviderNames(t *testing.T) {
RegisterProvider("json-case-test", func(util.Configuration) (KMSProvider, error) {
return newCountingKMSProvider(), nil
})
defer func() {
GetKMSManager().RemoveKMSProvider("Main")
GetKMSManager().RemoveKMSProvider("Logs")
SetGlobalKMSProvider(nil)
}()
provider := map[string]interface{}{"type": "json-case-test", "cache_enabled": false}
if err := LoadKMSFromConfig(map[string]interface{}{
"default_provider": "Main",
"providers": map[string]interface{}{"Main": provider, "Logs": provider},
"buckets": map[string]interface{}{"logs": map[string]interface{}{"provider": "Logs"}},
}); err != nil {
t.Fatal(err)
}
if got := GetKMSManager().GetBucketKMSProvider("logs"); got != "Logs" {
t.Fatalf("bucket logs uses %q, want Logs", got)
}
if GetGlobalKMS() == nil {
t.Fatal("the default provider isn't global")
}
}
func TestKMSSectionsOfTheWrongTypeFailToParse(t *testing.T) {
for name, config := range map[string]map[string]interface{}{
"providers": {"providers": "vault"},
"buckets": {"buckets": []interface{}{"logs"}},
"default_provider": {"default_provider": true},
} {
_, err := parseKMSSettings(config, strings.ToLower)
if err == nil || !strings.Contains(err.Error(), name) {
t.Errorf("%s of the wrong type: got %v, want an error naming it", name, err)
}
}
}