mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-05 18:47:20 +01:00
* kms: close replaced providers after a grace period A reload closed each provider it replaced or removed right away, while S3 requests that had fetched it just before could still be using it. Those requests failed. A replaced or removed provider now stays open for a grace period of a minute and is then closed once, however often it was retired. The manager's Close closes the ones still waiting. The global provider is now the manager's default one, looked up on each use, so a reload can't leave it pointing at a closed instance. A provider set with SetGlobalKMSProvider still takes its place until the next load, which clears it. Passing nil hands it back to the manager's default, where before it turned KMS off until the next load. While a retired provider is global, its retirement is held with no timer, and it starts again once it isn't. SetGlobalKMSProvider no longer closes the provider it replaces, since the caller owns what it passes. A provider InitializeGlobalKMS created belongs to the package, so it's closed once another global provider replaces it. The manager's Close clears the global provider if it's one the manager closes, so it isn't used closed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * kms: read the global override and the default provider under one lock Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * kms: check the replaced global instance is held before unsetting it Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * kms/openbao: close the reload test's providers through the manager 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>
86 lines
2.9 KiB
Go
86 lines
2.9 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 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)
|
|
}
|
|
}
|