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) } }