Files
seaweedfs/weed/s3api/auth_credentials_kms_test.go
T
7c41ed0635 kms: swap a reload's providers in as a whole, after S3 parses its identities (#13344)
* kms: build every provider before a reload swaps them in

A reload replaced the providers one at a time, closing each old instance
as its replacement went in, and kept the providers and bucket mappings
the config no longer listed. Between closing the old default instance and
setting the new global provider, S3 could get the closed instance. A
reload now builds every provider first and swaps the whole set in at
once, along with the default, the bucket mappings and the global
provider. The default provider failing still fails the reload, and then
nothing changes. Any other provider that fails keeps its previous
instance. Providers the config no longer lists are dropped, and a bucket
mapped to one falls back to the default. A manager other than the global
one no longer sets the global provider. The manager no longer keeps the
provider configurations, which nothing read. Each load health-checks
every provider once, after the swap, for filer.toml as for the identities
file.

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

* s3api: parse the identities before loading KMS

Loading the S3 config loaded KMS before parsing the identities, so a file
whose identities didn't parse still replaced the KMS providers. A file
without a kms block left the previous providers in place, and one with a
number out of float64 range, which protojson accepts, skipped KMS without
an error.

The identities are now parsed first, and a file that fails there changes
neither. A file without a kms block drops the providers, so a reload that
took the block out turns SSE-KMS off. A file encoding/json can't read
fails the KMS load with an error that doesn't quote it, as it could quote
key material. A KMS load that fails is logged as an error, not a warning.

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

* kms: skip a bucket mapping with an empty bucket name

SetBucketKMSProvider refused an empty bucket name, which the rebuilt
mappings no longer went through.

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

* kms: check a rejected load leaves the installed provider open

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:34:45 -08:00

83 lines
2.7 KiB
Go

package s3api
import (
"testing"
"github.com/seaweedfs/seaweedfs/weed/kms"
"github.com/seaweedfs/seaweedfs/weed/pb/iam_pb"
)
// resetKMS drops the providers a test loaded.
func resetKMS(t *testing.T) {
t.Cleanup(func() {
if err := kms.LoadKMSFromConfig(nil); err != nil {
t.Error(err)
}
})
}
func TestKMSSectionRemovedDropsTheProviders(t *testing.T) {
resetKMS(t)
iam := &IdentityAccessManagement{}
if err := iam.initializeKMSFromConfig([]byte(`{"kms": {"providers": {"removed-test": {"type": "local"}}}}`)); err != nil {
t.Fatal(err)
}
if err := iam.initializeKMSFromConfig([]byte(`{"kms": `)); err == nil {
t.Error("a configuration that isn't JSON was reported as loaded")
}
if kms.GetGlobalKMS() == nil {
t.Fatal("a configuration that isn't JSON dropped the KMS providers")
}
if err := iam.initializeKMSFromConfig([]byte(`{"identities": []}`)); err != nil {
t.Fatalf("a configuration without KMS: %v", err)
}
if kms.GetGlobalKMS() != nil {
t.Fatal("a reload that took the kms section out kept its providers")
}
}
func TestIdentitiesThatDontParseLeaveKMSAlone(t *testing.T) {
resetKMS(t)
s3a := newTestS3ApiServerWithMemoryIAM(t, []*iam_pb.Identity{})
identity := func(name string) string {
return `{"name": "` + name + `", "credentials": [{"accessKey": "AKIADUPLICATE", "secretKey": "c2VjcmV0"}]}`
}
if kms.GetGlobalKMS() != nil {
t.Fatal("a KMS provider is already set")
}
if err := s3a.iam.loadS3ApiConfigurationFromFile(writeTempIamConfig(t, `{"kms": {"providers": {"identities-test": {"type": "local"}}}, "identities": [`+identity("a")+`]}`)); err != nil {
t.Fatal(err)
}
loaded := kms.GetGlobalKMS()
if loaded == nil {
t.Fatal("the KMS providers didn't load")
}
duplicates := `"identities": [` + identity("a") + `, ` + identity("b") + `]`
for name, config := range map[string]string{
"other providers": `{"kms": {"providers": {"identities-other": {"type": "local"}}}, ` + duplicates + `}`,
"no kms section": `{` + duplicates + `}`,
} {
if err := s3a.iam.loadS3ApiConfigurationFromFile(writeTempIamConfig(t, config)); err == nil {
t.Fatalf("%s: identities sharing an access key were accepted", name)
}
if kms.GetGlobalKMS() != loaded {
t.Fatalf("%s: a file whose identities don't parse changed the KMS providers", name)
}
}
}
func TestKMSConfigWithANumberOutOfRangeKeepsItOutOfTheError(t *testing.T) {
resetKMS(t)
iam := &IdentityAccessManagement{}
const number = "12345678901234567890e400"
err := iam.initializeKMSFromConfig([]byte(`{"identities": [{"name": "admin"}], "kms": {"providers": {"p": {"type": "local", "note": ` + number + `}}}}`))
if want := "failed to parse S3 config: a number in it is out of float64 range"; err == nil || err.Error() != want {
t.Fatalf("got %v, want %s", err, want)
}
}