forked from ekristen/aws-nuke
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup-vaults-access-policies.go
134 lines (119 loc) · 3.83 KB
/
backup-vaults-access-policies.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package resources
import (
"context"
"github.com/ekristen/aws-nuke/v3/pkg/nuke"
"github.com/ekristen/libnuke/pkg/registry"
"github.com/ekristen/libnuke/pkg/resource"
"github.com/aws/aws-sdk-go/service/backup"
)
type BackupVaultAccessPolicy struct {
svc *backup.Backup
backupVaultName string
}
const AWSBackupVaultAccessPolicyResource = "AWSBackupVaultAccessPolicy"
func init() {
registry.Register(®istry.Registration{
Name: AWSBackupVaultAccessPolicyResource,
Scope: nuke.Account,
Lister: &AWSBackupVaultAccessPolicyLister{},
})
}
type AWSBackupVaultAccessPolicyLister struct{}
func (l *AWSBackupVaultAccessPolicyLister) List(_ context.Context, o interface{}) ([]resource.Resource, error) {
opts := o.(*nuke.ListerOpts)
svc := backup.New(opts.Session)
maxVaultsLen := int64(100)
params := &backup.ListBackupVaultsInput{
MaxResults: &maxVaultsLen, // aws default limit on number of backup vaults per account
}
resp, err := svc.ListBackupVaults(params)
if err != nil {
return nil, err
}
// Iterate over backup vaults and add vault policies that exist.
resources := make([]resource.Resource, 0)
for _, out := range resp.BackupVaultList {
// Check if the Backup Vault has an Access Policy set
resp, err := svc.GetBackupVaultAccessPolicy(&backup.GetBackupVaultAccessPolicyInput{
BackupVaultName: out.BackupVaultName,
})
// Non-existent Access Policies can come from ResourceNotFoundException or
// being nil.
if err != nil {
switch err.(type) {
case *backup.ResourceNotFoundException:
// Non-existent is OK and we skip over them
continue
default:
return nil, err
}
}
// Only delete policies that exist.
if resp.Policy != nil {
resources = append(resources, &BackupVaultAccessPolicy{
svc: svc,
backupVaultName: *out.BackupVaultName,
})
}
}
return resources, nil
}
func (b *BackupVaultAccessPolicy) Remove(_ context.Context) error {
// Set the policy to a policy that allows deletion before removal.
//
// This is required to delete the policy for the automagically created vaults
// such as "aws/efs/automatic-backup-vault" from EFS automatic backups
// which by default Deny policy deletion via backup::DeleteBackupVaultAccessPolicy
//
// Example "aws/efs/automatic-backup-vault" default policy:
//
// {
// "Version": "2012-10-17",
// "Statement": [
// {
// "Effect": "Deny",
// "Principal": {
// "AWS": "*"
// },
// "Action": [
// "backup:DeleteBackupVault",
// "backup:DeleteBackupVaultAccessPolicy",
// "backup:DeleteRecoveryPoint",
// "backup:StartCopyJob",
// "backup:StartRestoreJob",
// "backup:UpdateRecoveryPointLifecycle"
// ],
// "Resource": "*"
// }
// ]
// }
//
// While deletion is Denied, you can update the policy with one that
// doesn't deny and then delete at will.
allowDeletionPolicy := `{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"AWS": "*"
},
"Action": "backup:DeleteBackupVaultAccessPolicy",
"Resource": "*"
}
]
}`
// Ignore error from if we can't put permissive backup vault policy in for some reason, that's OK.
_, _ = b.svc.PutBackupVaultAccessPolicy(&backup.PutBackupVaultAccessPolicyInput{
BackupVaultName: &b.backupVaultName,
Policy: &allowDeletionPolicy,
})
// In the end, this is the only call we actually really care about for err.
_, err := b.svc.DeleteBackupVaultAccessPolicy(&backup.DeleteBackupVaultAccessPolicyInput{
BackupVaultName: &b.backupVaultName,
})
return err
}
func (b *BackupVaultAccessPolicy) String() string {
return b.backupVaultName
}