Skip to content

Commit

Permalink
Merge pull request #1466 from forjor/feat/Add_AWSSM_PutSecret_method
Browse files Browse the repository at this point in the history
feat: Add put method into AWS Secrets Manager helper interface.
  • Loading branch information
james03160927 authored Nov 20, 2024
2 parents ce008dc + 4215900 commit 4f95711
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
20 changes: 20 additions & 0 deletions modules/aws/secretsmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,26 @@ func GetSecretValueE(t testing.TestingT, awsRegion, id string) (string, error) {
return aws.StringValue(secret.SecretString), nil
}

// UpdateSecretString updates a secret in Secrets Manager to a new string value
func PutSecretString(t testing.TestingT, awsRegion, id string, secretString string) {
err := PutSecretStringE(t, awsRegion, id, secretString)
require.NoError(t, err)
}

// UpdateSecretStringE updates a secret in Secrets Manager to a new string value
func PutSecretStringE(t testing.TestingT, awsRegion, id string, secretString string) error {
logger.Default.Logf(t, "Updating secret with ID %s", id)

client := NewSecretsManagerClient(t, awsRegion)

_, err := client.PutSecretValue(&secretsmanager.PutSecretValueInput{
SecretId: aws.String(id),
SecretString: aws.String(secretString),
})

return err
}

// DeleteSecret deletes a secret. If forceDelete is true, the secret will be deleted after a short delay. If forceDelete is false, the secret will be deleted after a 30 day recovery window.
func DeleteSecret(t testing.TestingT, awsRegion, id string, forceDelete bool) {
err := DeleteSecretE(t, awsRegion, id, forceDelete)
Expand Down
12 changes: 9 additions & 3 deletions modules/aws/secretsmanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,19 @@ func TestSecretsManagerMethods(t *testing.T) {
region := GetRandomStableRegion(t, nil, nil)
name := random.UniqueId()
description := "This is just a secrets manager test description."
secretValue := "This is the secret value."
secretOriginalValue := "This is the secret value."
secretUpdatedValue := "This is the NEW secret value."

secretARN := CreateSecretStringWithDefaultKey(t, region, description, name, secretValue)
secretARN := CreateSecretStringWithDefaultKey(t, region, description, name, secretOriginalValue)
defer deleteSecret(t, region, secretARN)

storedValue := GetSecretValue(t, region, secretARN)
assert.Equal(t, secretValue, storedValue)
assert.Equal(t, secretOriginalValue, storedValue)

PutSecretString(t, region, secretARN, secretUpdatedValue)

storedValueAfterUpdate := GetSecretValue(t, region, secretARN)
assert.Equal(t, secretUpdatedValue, storedValueAfterUpdate)
}

func deleteSecret(t *testing.T, region, id string) {
Expand Down

0 comments on commit 4f95711

Please sign in to comment.