forked from ekristen/aws-nuke
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiam-role-policy-attachments.go
144 lines (116 loc) · 3.27 KB
/
iam-role-policy-attachments.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
135
136
137
138
139
140
141
142
143
144
package resources
import (
"context"
"fmt"
"strings"
"time"
"github.com/sirupsen/logrus"
"github.com/aws/aws-sdk-go/service/iam"
"github.com/aws/aws-sdk-go/service/iam/iamiface"
"github.com/ekristen/libnuke/pkg/registry"
"github.com/ekristen/libnuke/pkg/resource"
"github.com/ekristen/libnuke/pkg/types"
"github.com/ekristen/aws-nuke/v3/pkg/nuke"
)
const IAMRolePolicyAttachmentResource = "IAMRolePolicyAttachment"
func init() {
registry.Register(®istry.Registration{
Name: IAMRolePolicyAttachmentResource,
Scope: nuke.Account,
Lister: &IAMRolePolicyAttachmentLister{},
DeprecatedAliases: []string{
"IamRolePolicyAttachement",
},
})
}
type IAMRolePolicyAttachment struct {
svc iamiface.IAMAPI
policyArn string
policyName string
role *iam.Role
}
func (e *IAMRolePolicyAttachment) Filter() error {
if strings.Contains(e.policyArn, ":iam::aws:policy/aws-service-role/") {
return fmt.Errorf("cannot detach from service roles")
}
if strings.HasPrefix(*e.role.Path, "/aws-reserved/sso.amazonaws.com/") {
return fmt.Errorf("cannot detach from SSO roles")
}
return nil
}
func (e *IAMRolePolicyAttachment) Remove(_ context.Context) error {
_, err := e.svc.DetachRolePolicy(
&iam.DetachRolePolicyInput{
PolicyArn: &e.policyArn,
RoleName: e.role.RoleName,
})
if err != nil {
return err
}
return nil
}
func (e *IAMRolePolicyAttachment) Properties() types.Properties {
properties := types.NewProperties().
Set("RoleName", e.role.RoleName).
Set("RolePath", e.role.Path).
Set("RoleLastUsed", getLastUsedDate(e.role)).
Set("RoleCreateDate", e.role.CreateDate.Format(time.RFC3339)).
Set("PolicyName", e.policyName).
Set("PolicyArn", e.policyArn)
for _, tag := range e.role.Tags {
properties.SetTagWithPrefix("role", tag.Key, tag.Value)
}
return properties
}
func (e *IAMRolePolicyAttachment) String() string {
return fmt.Sprintf("%s -> %s", *e.role.RoleName, e.policyName)
}
// -----------------------
type IAMRolePolicyAttachmentLister struct{}
func (l *IAMRolePolicyAttachmentLister) List(_ context.Context, o interface{}) ([]resource.Resource, error) {
opts := o.(*nuke.ListerOpts)
svc := iam.New(opts.Session)
roleParams := &iam.ListRolesInput{}
resources := make([]resource.Resource, 0)
for {
roleResp, err := svc.ListRoles(roleParams)
if err != nil {
return nil, err
}
for _, listedRole := range roleResp.Roles {
role, err := GetIAMRole(svc, listedRole.RoleName)
if err != nil {
logrus.Errorf("Failed to get listed role %s: %v", *listedRole.RoleName, err)
continue
}
polParams := &iam.ListAttachedRolePoliciesInput{
RoleName: role.RoleName,
}
for {
polResp, err := svc.ListAttachedRolePolicies(polParams)
if err != nil {
logrus.Errorf("failed to list attached policies for role %s: %v",
*role.RoleName, err)
break
}
for _, pol := range polResp.AttachedPolicies {
resources = append(resources, &IAMRolePolicyAttachment{
svc: svc,
policyArn: *pol.PolicyArn,
policyName: *pol.PolicyName,
role: role,
})
}
if !*polResp.IsTruncated {
break
}
polParams.Marker = polResp.Marker
}
}
if !*roleResp.IsTruncated {
break
}
roleParams.Marker = roleResp.Marker
}
return resources, nil
}