Files
seaweedfs/weed/s3api/auth_credentials_kms_test.go
T
0b7bd23647 s3api: stop at startup when the -s3.config KMS block doesn't load (#13380)
* s3api: stop at startup when the -s3.config KMS block doesn't load

A kms block in the -s3.config file that failed to load was only logged,
so S3 came up healthy with SSE-KMS off. Every SSE-KMS write and every
read of an SSE-KMS object then failed with 500, and nothing at startup
pointed at the config.

loadS3ApiConfigurationFromFile now returns the KMS error once it has
applied the identities, and startup already stops on any error it
returns. A SIGHUP reload still applies the identities, so a revoked
key stops working, keeps the previous KMS providers and logs the error.

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

* s3api: shorten the KMS load-order comment

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-04 11:07:11 -08:00

99 lines
3.5 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 and closes the ones still in their grace period.
func resetKMS(t *testing.T) {
t.Cleanup(func() {
if err := kms.LoadKMSFromConfig(nil); err != nil {
t.Error(err)
}
if err := kms.GetKMSManager().Close(); 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 TestKMSThatDoesntLoadFailsTheFileAfterItsIdentities(t *testing.T) {
resetKMS(t)
s3a := newTestS3ApiServerWithMemoryIAM(t, []*iam_pb.Identity{})
err := s3a.iam.loadS3ApiConfigurationFromFile(writeTempIamConfig(t, `{"kms": {"default_provider": "missing"}, "identities": [{"name": "a", "credentials": [{"accessKey": "AKIAKMSFAILED", "secretKey": "c2VjcmV0"}]}]}`))
if err == nil {
t.Fatal("a kms section that doesn't load was reported as loaded")
}
if _, _, found := s3a.iam.lookupByAccessKey("AKIAKMSFAILED"); !found {
t.Fatal("a kms section that doesn't load held back the identities")
}
}
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)
}
}