forked from rebuy-de/aws-nuke
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiam-instance-profile-roles.go
98 lines (80 loc) · 2.23 KB
/
iam-instance-profile-roles.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
package resources
import (
"fmt"
"time"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/iam"
"github.com/rebuy-de/aws-nuke/v2/pkg/types"
"github.com/sirupsen/logrus"
)
type IAMInstanceProfileRole struct {
svc *iam.IAM
role *iam.Role
profile *iam.InstanceProfile
}
func init() {
register("IAMInstanceProfileRole", ListIAMInstanceProfileRoles)
}
func ListIAMInstanceProfileRoles(sess *session.Session) ([]Resource, error) {
svc := iam.New(sess)
params := &iam.ListInstanceProfilesInput{}
resources := make([]Resource, 0)
for {
resp, err := svc.ListInstanceProfiles(params)
if err != nil {
return nil, err
}
for _, out := range resp.InstanceProfiles {
for _, outRole := range out.Roles {
profile, err := GetIAMInstanceProfile(svc, out.InstanceProfileName)
if err != nil {
logrus.
WithError(err).
WithField("instanceProfileName", *out.InstanceProfileName).
Error("Failed to get listed instance profile")
continue
}
resources = append(resources, &IAMInstanceProfileRole{
svc: svc,
role: outRole,
profile: profile,
})
}
}
if !*resp.IsTruncated {
break
}
params.Marker = resp.Marker
}
return resources, nil
}
func (e *IAMInstanceProfileRole) Remove() error {
_, err := e.svc.RemoveRoleFromInstanceProfile(
&iam.RemoveRoleFromInstanceProfileInput{
InstanceProfileName: e.profile.InstanceProfileName,
RoleName: e.role.RoleName,
})
if err != nil {
return err
}
return nil
}
func (e *IAMInstanceProfileRole) String() string {
return fmt.Sprintf("%s -> %s", *e.profile.InstanceProfileName, e.role)
}
func (e *IAMInstanceProfileRole) Properties() types.Properties {
properties := types.NewProperties()
for _, tagValue := range e.profile.Tags {
properties.SetTag(tagValue.Key, tagValue.Value)
}
properties.
Set("InstanceProfile", e.profile.InstanceProfileName).
Set("InstanceRole", e.role.RoleName).
Set("role:Path", e.role.Path).
Set("role:CreateDate", e.role.CreateDate.Format(time.RFC3339)).
Set("role:LastUsedDate", getLastUsedDate(e.role, time.RFC3339))
for _, tagValue := range e.role.Tags {
properties.SetTagWithPrefix("role", tagValue.Key, tagValue.Value)
}
return properties
}