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

Fix deletion of backup vault access policies that contain restrictive policy #21

Merged
merged 1 commit into from
Nov 14, 2023

Conversation

gsoria
Copy link

@gsoria gsoria commented Nov 10, 2023

This commit fixes the following error when trying to delete backup vault access policies for vaults (aws/efs/automatic-backup-vault) automatically created when EFS backup is enabled.

time="2023-10-05T15:37:07Z" level=error msg="AccessDeniedException: User: arn:aws:sts::X:assumed-role/XRole/SAAssumedRoleSession is not authorized to perform: backup:DeleteBackupVaultAccessPolicy on resource: arn:aws:backup:us-east-1:X:backup-vault:aws/efs/automatic-backup-vault with an explicit deny in a resource-based policy

The module before attempting to delete the backup vault access policy, sets a permissive policy to ensure the backup:DeleteBackupVaultAccessPolicy is allowed.

The operation to put a policy to allow backup:DeleteBackupVaultAccessPolicy was silently failing due to an error:

The specified policy cannot be added to the vault due to cross-account sharing restrictions.
Amend the policy or the vault's settings, then retry request

This commit updates the policy, to use the default as a template, but excluding delete actions.

Testing

# https://docs.aws.amazon.com/efs/latest/ug/awsbackup.html
#
# create efs file system
aws efs create-file-system --creation-token MyEfsFileSystem --tags Key=Name,Value=MyEfsFileSystem

# enable backup policy
aws efs put-backup-policy --file-system-id $(aws efs describe-file-systems | jq -r .FileSystems[].FileSystemId) --backup-policy Status="ENABLED"

# verify if backup policy is enabled
aws efs describe-backup-policy --file-system-id $(aws efs describe-file-systems | jq -r .FileSystems[].FileSystemId)

Cleanup the account, specifying the resource AWSBackupVaultAccessPolicy.

You should see an output similar to this:

us-east-1 - AWSBackupVaultAccessPolicy - aws/efs/automatic-backup-vault - triggered remove
Removal requested: 1 waiting, 0 failed, 1 skipped, 0 finished
us-east-1 - AWSBackupVaultAccessPolicy - aws/efs/automatic-backup-vault - waiting
Removal requested: 1 waiting, 0 failed, 1 skipped, 0 finished
us-east-1 - AWSBackupVaultAccessPolicy - aws/efs/automatic-backup-vault - removed
Removal requested: 0 waiting, 0 failed, 1 skipped, 1 finished
Nuke complete: 0 failed, 1 skipped, 1 finished.

…ses to prevent their deletion

This commit fixes the following error when trying to delete backup vault access policies for vaults (`aws/efs/automatic-backup-vault`)
automatically created when EFS backup is enabled.

```
time="2023-10-05T15:37:07Z" level=error msg="AccessDeniedException: User: arn:aws:sts::X:assumed-role/XRole/SAAssumedRoleSession is not authorized to perform: backup:DeleteBackupVaultAccessPolicy on resource: arn:aws:backup:us-east-1:X:backup-vault:aws/efs/automatic-backup-vault with an explicit deny in a resource-based policy
```

The module before attempting to delete the backup vault access policy, sets a permissive policy
to ensure the `backup:DeleteBackupVaultAccessPolicy` is allowed.

The operation to put a policy to allow `backup:DeleteBackupVaultAccessPolicy` was silently failing due to an
error:

```
The specified policy cannot be added to the vault due to cross-account sharing restrictions.
Amend the policy or the vault's settings, then retry request
```

This commit updates the policy, to use the default as a template, but excluding delete actions.

Signed-off-by: Gabriela S. Soria <[email protected]>
Copy link
Member

@danarbaugh danarbaugh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I followed the testing steps to confirm I received the expected AccessDeniedException error on the account with a version built from main then a subsequent run with the same configuration using a build from this branch resulted in
us-east-1 - AWSBackupVaultAccessPolicy - aws/efs/automatic-backup-vault - removed as expected. Thanks Gaby. This is a great bugfix that I am sure will be appreciated upstream! 👏

Copy link

@corybekk corybekk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everything worked for me! thank you for including the commands for creating the resources.

Testing output

Do you really want to nuke these resources on the account with the ID 299719426765 and the alias 'alias-299719426765'?
Waiting 3s before continuing.
us-east-1 - AWSBackupVaultAccessPolicy - aws/efs/automatic-backup-vault - triggered remove

Removal requested: 1 waiting, 0 failed, 1 skipped, 0 finished

us-east-1 - AWSBackupVaultAccessPolicy - aws/efs/automatic-backup-vault - waiting

Removal requested: 1 waiting, 0 failed, 1 skipped, 0 finished

us-east-1 - AWSBackupVaultAccessPolicy - aws/efs/automatic-backup-vault - removed

Removal requested: 0 waiting, 0 failed, 1 skipped, 1 finished

Nuke complete: 0 failed, 1 skipped, 1 finished.

@gsoria gsoria merged commit 8294c3a into oreilly-main Nov 14, 2023
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants