Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for policy in secrets manager datasource #6091

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions aws/data_source_aws_secretsmanager_secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/secretsmanager"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/structure"
)

func dataSourceAwsSecretsManagerSecret() *schema.Resource {
Expand All @@ -34,6 +35,10 @@ func dataSourceAwsSecretsManagerSecret() *schema.Resource {
Optional: true,
Computed: true,
},
"policy": {
Type: schema.TypeString,
Computed: true,
},
"rotation_enabled": {
Type: schema.TypeBool,
Computed: true,
Expand Down Expand Up @@ -103,6 +108,24 @@ func dataSourceAwsSecretsManagerSecretRead(d *schema.ResourceData, meta interfac
d.Set("name", output.Name)
d.Set("rotation_enabled", output.RotationEnabled)
d.Set("rotation_lambda_arn", output.RotationLambdaARN)
d.Set("policy", "")

pIn := &secretsmanager.GetResourcePolicyInput{
SecretId: aws.String(d.Id()),
}
log.Printf("[DEBUG] Reading Secrets Manager Secret policy: %s", pIn)
pOut, err := conn.GetResourcePolicy(pIn)
if err != nil {
return fmt.Errorf("error reading Secrets Manager Secret policy: %s", err)
}

bflad marked this conversation as resolved.
Show resolved Hide resolved
if pOut != nil && pOut.ResourcePolicy != nil {
policy, err := structure.NormalizeJsonString(aws.StringValue(pOut.ResourcePolicy))
if err != nil {
return fmt.Errorf("policy contains an invalid JSON: %s", err)
}
d.Set("policy", policy)
}

if err := d.Set("rotation_rules", flattenSecretsManagerRotationRules(output.RotationRules)); err != nil {
return fmt.Errorf("error setting rotation_rules: %s", err)
Expand Down
49 changes: 49 additions & 0 deletions aws/data_source_aws_secretsmanager_secret_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,25 @@ func TestAccDataSourceAwsSecretsManagerSecret_Name(t *testing.T) {
})
}

func TestAccDataSourceAwsSecretsManagerSecret_Policy(t *testing.T) {
rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_secretsmanager_secret.test"
datasourceName := "data.aws_secretsmanager_secret.test"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAwsSecretsManagerSecretConfig_Policy(rName),
Check: resource.ComposeTestCheckFunc(
testAccDataSourceAwsSecretsManagerSecretCheck(datasourceName, resourceName),
),
},
},
})
}

func testAccDataSourceAwsSecretsManagerSecretCheck(datasourceName, resourceName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
resource, ok := s.RootModule().Resources[datasourceName]
Expand All @@ -86,6 +105,7 @@ func testAccDataSourceAwsSecretsManagerSecretCheck(datasourceName, resourceName
"description",
"kms_key_id",
"name",
"policy",
"rotation_enabled",
"rotation_lambda_arn",
"rotation_rules.#",
Expand Down Expand Up @@ -148,6 +168,35 @@ data "aws_secretsmanager_secret" "test" {
`, rName)
}

func testAccDataSourceAwsSecretsManagerSecretConfig_Policy(rName string) string {
return fmt.Sprintf(`
resource "aws_secretsmanager_secret" "test" {
name = "%[1]s"

policy = <<POLICY
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "EnableAllPermissions",
"Effect": "Allow",
"Principal": {
"AWS": "*"
},
"Action": "secretsmanager:GetSecretValue",
"Resource": "*"
}
]
}
POLICY
}

data "aws_secretsmanager_secret" "test" {
name = "${aws_secretsmanager_secret.test.name}"
}
`, rName)
}

const testAccDataSourceAwsSecretsManagerSecretConfig_NonExistent = `
data "aws_secretsmanager_secret" "test" {
name = "tf-acc-test-does-not-exist"
Expand Down
1 change: 1 addition & 0 deletions website/docs/d/secretsmanager_secret.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,4 @@ data "aws_secretsmanager_secret" "by-name" {
* `rotation_lambda_arn` - Rotation Lambda function Amazon Resource Name (ARN) if rotation is enabled.
* `rotation_rules` - Rotation rules if rotation is enabled.
* `tags` - Tags of the secret.
* `policy` - The resource-based policy document that's attached to the secret.