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

r/backup_vault_policy: add iam retry for eventual consistency #34671

Merged
merged 4 commits into from
Dec 4, 2023
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
6 changes: 6 additions & 0 deletions internal/service/backup/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@

package backup

import "time"

const (
iamPropagationTimeout = 2 * time.Minute
)

const (
frameworkStatusCompleted = "COMPLETED"
frameworkStatusCreationInProgress = "CREATE_IN_PROGRESS"
Expand Down
3 changes: 2 additions & 1 deletion internal/service/backup/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
package backup

const (
errCodeAccessDeniedException = "AccessDeniedException"
errCodeAccessDeniedException = "AccessDeniedException"
errCodeInvalidParameterValueException = "InvalidParameterValueException"
)
7 changes: 6 additions & 1 deletion internal/service/backup/vault_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,12 @@ func resourceVaultPolicyPut(ctx context.Context, d *schema.ResourceData, meta in
Policy: aws.String(policy),
}

_, err = conn.PutBackupVaultAccessPolicyWithContext(ctx, input)
_, err = tfresource.RetryWhenAWSErrMessageContains(ctx, iamPropagationTimeout,
func() (interface{}, error) {
return conn.PutBackupVaultAccessPolicyWithContext(ctx, input)
},
errCodeInvalidParameterValueException, "Provided principal is not valid",
)

if err != nil {
return sdkdiag.AppendErrorf(diags, "creating Backup Vault Policy (%s): %s", name, err)
Expand Down
82 changes: 82 additions & 0 deletions internal/service/backup/vault_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,28 @@ func TestAccBackupVaultPolicy_basic(t *testing.T) {
})
}

func TestAccBackupVaultPolicy_eventualConsistency(t *testing.T) {
ctx := acctest.Context(t)
var vault backup.GetBackupVaultAccessPolicyOutput
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_backup_vault_policy.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t); testAccPreCheck(ctx, t) },
ErrorCheck: acctest.ErrorCheck(t, backup.EndpointsID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckVaultPolicyDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccVaultPolicyConfig_eventualConsistency(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckVaultPolicyExists(ctx, resourceName, &vault),
resource.TestMatchResourceAttr(resourceName, "policy", regexache.MustCompile("^{\"Id\":\"default\".+"))),
},
},
})
}

func TestAccBackupVaultPolicy_disappears(t *testing.T) {
ctx := acctest.Context(t)
var vault backup.GetBackupVaultAccessPolicyOutput
Expand Down Expand Up @@ -285,3 +307,63 @@ resource "aws_backup_vault_policy" "test" {
}
`, rName)
}

func testAccVaultPolicyConfig_eventualConsistency(rName string) string {
return acctest.ConfigCompose(
fmt.Sprintf(`
data "aws_partition" "current" {}

resource "aws_iam_role" "test" {
name = %[1]q
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = "sts:AssumeRole"
Effect = "Allow"
Sid = ""
Principal = {
Service = "backup.amazonaws.com"
}
},
]
})
}

resource "aws_iam_role_policy_attachment" "test" {
role = aws_iam_role.test.name
policy_arn = "arn:${data.aws_partition.current.partition}:iam::${data.aws_partition.current.partition}:policy/service-role/AWSBackupServiceRolePolicyForBackup"
}

resource "aws_backup_vault" "test" {
name = %[1]q
}

resource "aws_backup_vault_policy" "test" {
backup_vault_name = aws_backup_vault.test.name

policy = jsonencode({
Version = "2012-10-17"
Id = "default"
Statement = [{
Sid = "default"
Effect = "Allow"
Principal = {
AWS = "${aws_iam_role.test.arn}"
}
Action = [
"backup:DescribeBackupVault",
"backup:DeleteBackupVault",
"backup:PutBackupVaultAccessPolicy",
"backup:DeleteBackupVaultAccessPolicy",
"backup:GetBackupVaultAccessPolicy",
"backup:StartBackupJob",
"backup:GetBackupVaultNotifications",
"backup:PutBackupVaultNotifications",
]
Resource = aws_backup_vault.test.arn
}]
})
}
`, rName))
}
Loading