-
Notifications
You must be signed in to change notification settings - Fork 9.2k
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
r/secretsmanager_secret_policy - new resource #14468
Merged
bill-rich
merged 14 commits into
hashicorp:master
from
DrFaust92:r/secretsmanager_secret_policy
Nov 2, 2020
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
ae23823
add secret manager secret policy
DrFaust92 24e7b38
add docs
DrFaust92 ff741a4
ammend sweeper
DrFaust92 ac9da3f
docs(?)
DrFaust92 f1a409e
docs(!)
DrFaust92 5c662b6
lint
DrFaust92 c514777
use iam waiter
DrFaust92 8dc4783
lint
DrFaust92 bed65cd
lint
DrFaust92 29602ce
use v2 + validation
DrFaust92 9a62e61
add `block_public_policy`
DrFaust92 91e4655
add `block_public_policy`
DrFaust92 f800d11
fmt
DrFaust92 5de4024
fmt
DrFaust92 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,172 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/secretsmanager" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/structure" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" | ||
iamwaiter "github.com/terraform-providers/terraform-provider-aws/aws/internal/service/iam/waiter" | ||
) | ||
|
||
func resourceAwsSecretsManagerSecretPolicy() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceAwsSecretsManagerSecretPolicyCreate, | ||
Read: resourceAwsSecretsManagerSecretPolicyRead, | ||
Update: resourceAwsSecretsManagerSecretPolicyUpdate, | ||
Delete: resourceAwsSecretsManagerSecretPolicyDelete, | ||
Importer: &schema.ResourceImporter{ | ||
State: schema.ImportStatePassthrough, | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"secret_arn": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: validateArn, | ||
}, | ||
"policy": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: validation.StringIsJSON, | ||
DiffSuppressFunc: suppressEquivalentAwsPolicyDiffs, | ||
}, | ||
"block_public_policy": { | ||
Type: schema.TypeBool, | ||
Optional: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceAwsSecretsManagerSecretPolicyCreate(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).secretsmanagerconn | ||
|
||
input := &secretsmanager.PutResourcePolicyInput{ | ||
ResourcePolicy: aws.String(d.Get("policy").(string)), | ||
SecretId: aws.String(d.Get("secret_arn").(string)), | ||
} | ||
|
||
if v, ok := d.GetOk("block_public_policy"); ok { | ||
input.BlockPublicPolicy = aws.Bool(v.(bool)) | ||
} | ||
|
||
log.Printf("[DEBUG] Setting Secrets Manager Secret resource policy; %#v", input) | ||
var res *secretsmanager.PutResourcePolicyOutput | ||
|
||
err := resource.Retry(iamwaiter.PropagationTimeout, func() *resource.RetryError { | ||
var err error | ||
res, err = conn.PutResourcePolicy(input) | ||
if isAWSErr(err, secretsmanager.ErrCodeMalformedPolicyDocumentException, | ||
"This resource policy contains an unsupported principal") { | ||
return resource.RetryableError(err) | ||
} | ||
if err != nil { | ||
return resource.NonRetryableError(err) | ||
} | ||
return nil | ||
}) | ||
if isResourceTimeoutError(err) { | ||
res, err = conn.PutResourcePolicy(input) | ||
} | ||
if err != nil { | ||
return fmt.Errorf("error setting Secrets Manager Secret %q policy: %w", d.Id(), err) | ||
} | ||
|
||
d.SetId(aws.StringValue(res.ARN)) | ||
|
||
return resourceAwsSecretsManagerSecretPolicyRead(d, meta) | ||
} | ||
|
||
func resourceAwsSecretsManagerSecretPolicyRead(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).secretsmanagerconn | ||
|
||
input := &secretsmanager.GetResourcePolicyInput{ | ||
SecretId: aws.String(d.Id()), | ||
} | ||
log.Printf("[DEBUG] Reading Secrets Manager Secret Policy: %#v", input) | ||
res, err := conn.GetResourcePolicy(input) | ||
if err != nil { | ||
if isAWSErr(err, secretsmanager.ErrCodeResourceNotFoundException, "") { | ||
log.Printf("[WARN] SecretsManager Secret Policy (%s) not found, removing from state", d.Id()) | ||
d.SetId("") | ||
return nil | ||
} | ||
return fmt.Errorf("error reading Secrets Manager Secret policy: %w", err) | ||
} | ||
|
||
if res.ResourcePolicy != nil { | ||
policy, err := structure.NormalizeJsonString(aws.StringValue(res.ResourcePolicy)) | ||
if err != nil { | ||
return fmt.Errorf("policy contains an invalid JSON: %w", err) | ||
} | ||
d.Set("policy", policy) | ||
} else { | ||
d.Set("policy", "") | ||
} | ||
d.Set("secret_arn", d.Id()) | ||
|
||
return nil | ||
} | ||
|
||
func resourceAwsSecretsManagerSecretPolicyUpdate(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).secretsmanagerconn | ||
|
||
if d.HasChanges("policy", "block_public_policy") { | ||
policy, err := structure.NormalizeJsonString(d.Get("policy").(string)) | ||
if err != nil { | ||
return fmt.Errorf("policy contains an invalid JSON: %s", err) | ||
} | ||
input := &secretsmanager.PutResourcePolicyInput{ | ||
ResourcePolicy: aws.String(policy), | ||
SecretId: aws.String(d.Id()), | ||
BlockPublicPolicy: aws.Bool(d.Get("block_public_policy").(bool)), | ||
} | ||
|
||
log.Printf("[DEBUG] Setting Secrets Manager Secret resource policy; %#v", input) | ||
err = resource.Retry(iamwaiter.PropagationTimeout, func() *resource.RetryError { | ||
var err error | ||
_, err = conn.PutResourcePolicy(input) | ||
if isAWSErr(err, secretsmanager.ErrCodeMalformedPolicyDocumentException, | ||
"This resource policy contains an unsupported principal") { | ||
return resource.RetryableError(err) | ||
} | ||
if err != nil { | ||
return resource.NonRetryableError(err) | ||
} | ||
return nil | ||
}) | ||
if isResourceTimeoutError(err) { | ||
_, err = conn.PutResourcePolicy(input) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above |
||
} | ||
if err != nil { | ||
return fmt.Errorf("error setting Secrets Manager Secret %q policy: %w", d.Id(), err) | ||
} | ||
} | ||
|
||
return resourceAwsSecretsManagerSecretPolicyRead(d, meta) | ||
} | ||
|
||
func resourceAwsSecretsManagerSecretPolicyDelete(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).secretsmanagerconn | ||
|
||
input := &secretsmanager.DeleteResourcePolicyInput{ | ||
SecretId: aws.String(d.Id()), | ||
} | ||
|
||
log.Printf("[DEBUG] Removing Secrets Manager Secret policy: %#v", input) | ||
_, err := conn.DeleteResourcePolicy(input) | ||
if err != nil { | ||
if isAWSErr(err, secretsmanager.ErrCodeResourceNotFoundException, "") { | ||
return nil | ||
} | ||
return fmt.Errorf("error removing Secrets Manager Secret %q policy: %w", d.Id(), err) | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is the policy being applied again if the previous attempt timed out? Is this an expected condition?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bill-rich this is required in the AWS Provider due to how the AWS Go SDK works, see also: