Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
11484: Add tests for awskms secret manager r=Frassle a=Frassle <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description <!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. --> Start of tests as part of looking into #11482. I need to add another test to check that assumed roles work, but I haven't yet worked out how to get aws to let me do an assume role from the sdk. But these are a start and worth adding in while still looking into adding more. ## Checklist <!--- Please provide details if the checkbox below is to be left unchecked. --> - [x] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [ ] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Service, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Service API version <!-- `@Pulumi` employees: If yes, you must submit corresponding changes in the service repo. --> Co-authored-by: Fraser Waters <fraser@pulumi.com>
- Loading branch information
Showing
5 changed files
with
159 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
// Copyright 2016-2022, Pulumi Corporation. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package cloud | ||
|
||
import ( | ||
"context" | ||
"crypto/rand" | ||
"math/big" | ||
"testing" | ||
|
||
"github.com/aws/aws-sdk-go-v2/aws" | ||
"github.com/aws/aws-sdk-go-v2/config" | ||
"github.com/aws/aws-sdk-go-v2/service/kms" | ||
"github.com/aws/aws-sdk-go-v2/service/sts" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func assertNoError(t *testing.T, err error) { | ||
if !assert.NoError(t, err) { | ||
t.FailNow() | ||
} | ||
} | ||
|
||
// the main testing function, takes a kms url and tries to make a new secret manager out of it and encrypt and | ||
// decrypt data | ||
func testURL(ctx context.Context, t *testing.T, url string) { | ||
dataKey, err := GenerateNewDataKey(url) | ||
assertNoError(t, err) | ||
|
||
manager, err := NewCloudSecretsManager(url, dataKey) | ||
assertNoError(t, err) | ||
|
||
enc, err := manager.Encrypter() | ||
assertNoError(t, err) | ||
|
||
dec, err := manager.Decrypter() | ||
assertNoError(t, err) | ||
|
||
ciphertext, err := enc.EncryptValue(ctx, "plaintext") | ||
assertNoError(t, err) | ||
|
||
plaintext, err := dec.DecryptValue(ctx, ciphertext) | ||
assertNoError(t, err) | ||
assert.Equal(t, "plaintext", plaintext) | ||
} | ||
|
||
func randomName(t *testing.T) string { | ||
name := "" | ||
letters := "abcdefghijklmnopqrstuvwxyz" | ||
for i := 0; i < 32; i++ { | ||
j, err := rand.Int(rand.Reader, big.NewInt(int64(len(letters)))) | ||
assertNoError(t, err) | ||
|
||
char := letters[j.Int64()] | ||
name = name + string(char) | ||
} | ||
return name | ||
} | ||
|
||
func getAwsCaller(t *testing.T) (context.Context, aws.Config, *sts.GetCallerIdentityOutput) { | ||
ctx := context.Background() | ||
cfg, err := config.LoadDefaultConfig(ctx) | ||
if err != nil { | ||
t.Logf("Skipping, could not load aws config: %s", err) | ||
t.SkipNow() | ||
} | ||
|
||
stsClient := sts.NewFromConfig(cfg) | ||
caller, err := stsClient.GetCallerIdentity(ctx, &sts.GetCallerIdentityInput{}) | ||
if err != nil { | ||
t.Logf("Skipping, couldn't use aws credentials to query identity: %s", err) | ||
t.SkipNow() | ||
} | ||
|
||
return ctx, cfg, caller | ||
} | ||
|
||
func createKey(ctx context.Context, t *testing.T, cfg aws.Config) *kms.CreateKeyOutput { | ||
kmsClient := kms.NewFromConfig(cfg) | ||
keyName := "test-key-" + randomName(t) | ||
key, err := kmsClient.CreateKey(ctx, &kms.CreateKeyInput{Description: &keyName}) | ||
assertNoError(t, err) | ||
t.Cleanup(func() { | ||
_, err := kmsClient.ScheduleKeyDeletion(ctx, &kms.ScheduleKeyDeletionInput{ | ||
KeyId: key.KeyMetadata.KeyId, | ||
}) | ||
assert.NoError(t, err) | ||
}) | ||
|
||
return key | ||
} | ||
|
||
//nolint:paralleltest // mutates environment variables | ||
func TestAWSCloudManager(t *testing.T) { | ||
t.Setenv("AWS_REGION", "us-west-2") | ||
ctx, cfg, _ := getAwsCaller(t) | ||
|
||
key := createKey(ctx, t, cfg) | ||
url := "awskms://" + *key.KeyMetadata.KeyId + "?awssdk=v2" | ||
|
||
testURL(ctx, t, url) | ||
} | ||
|
||
//nolint:paralleltest // mutates environment variables | ||
func TestAWSCloudManager_SessionToken(t *testing.T) { | ||
t.Setenv("AWS_REGION", "us-west-2") | ||
ctx, cfg, _ := getAwsCaller(t) | ||
|
||
key := createKey(ctx, t, cfg) | ||
url := "awskms://" + *key.KeyMetadata.KeyId + "?awssdk=v2" | ||
|
||
creds, err := cfg.Credentials.Retrieve(ctx) | ||
assertNoError(t, err) | ||
|
||
t.Setenv("AWS_PROFILE", "") | ||
t.Setenv("AWS_ACCESS_KEY_ID", creds.AccessKeyID) | ||
t.Setenv("AWS_SECRET_ACCESS_KEY", creds.SecretAccessKey) | ||
t.Setenv("AWS_SESSION_TOKEN", creds.SessionToken) | ||
testURL(ctx, t, url) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters