-
-
Notifications
You must be signed in to change notification settings - Fork 353
/
iam_group.go
161 lines (142 loc) · 4.42 KB
/
iam_group.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package resources
import (
"context"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/iam"
"github.com/gruntwork-io/cloud-nuke/config"
"github.com/gruntwork-io/cloud-nuke/logging"
"github.com/gruntwork-io/cloud-nuke/report"
"github.com/gruntwork-io/gruntwork-cli/errors"
"github.com/hashicorp/go-multierror"
"sync"
)
func (ig *IAMGroups) getAll(c context.Context, configObj config.Config) ([]*string, error) {
var allIamGroups []*string
err := ig.Client.ListGroupsPages(
&iam.ListGroupsInput{},
func(page *iam.ListGroupsOutput, lastPage bool) bool {
for _, iamGroup := range page.Groups {
if configObj.IAMGroups.ShouldInclude(config.ResourceValue{
Time: iamGroup.CreateDate,
Name: iamGroup.GroupName,
}) {
allIamGroups = append(allIamGroups, iamGroup.GroupName)
}
}
return !lastPage
},
)
if err != nil {
return nil, errors.WithStackTrace(err)
}
return allIamGroups, nil
}
// nukeAll - delete all IAM groups. Caller is responsible for pagination (no more than 100/request)
func (ig *IAMGroups) nukeAll(groupNames []*string) error {
if len(groupNames) == 0 {
logging.Debug("No IAM Groups to nuke")
return nil
}
//Probably not required since pagination is handled by the caller
if len(groupNames) > 100 {
logging.Errorf("Nuking too many IAM Groups at once (100): Halting to avoid rate limits")
return TooManyIamGroupErr{}
}
//No bulk delete exists, do it with goroutines
logging.Debug("Deleting all IAM Groups")
wg := new(sync.WaitGroup)
wg.Add(len(groupNames))
errChans := make([]chan error, len(groupNames))
for i, groupName := range groupNames {
errChans[i] = make(chan error, 1)
go ig.deleteAsync(wg, errChans[i], groupName)
}
wg.Wait()
//Collapse the errors down to one
var allErrs *multierror.Error
for _, errChan := range errChans {
if err := <-errChan; err != nil {
allErrs = multierror.Append(allErrs, err)
logging.Errorf("[Failed] %s", err)
}
}
finalErr := allErrs.ErrorOrNil()
if finalErr != nil {
return errors.WithStackTrace(finalErr)
}
return nil
}
// deleteIamGroup - removes an IAM group from AWS, designed to run as a goroutine
func (ig *IAMGroups) deleteAsync(wg *sync.WaitGroup, errChan chan error, groupName *string) {
defer wg.Done()
var multierr *multierror.Error
//Remove any users from the group
getGroupInput := &iam.GetGroupInput{
GroupName: groupName,
}
grp, err := ig.Client.GetGroup(getGroupInput)
for _, user := range grp.Users {
unlinkUserInput := &iam.RemoveUserFromGroupInput{
UserName: user.UserName,
GroupName: groupName,
}
_, err := ig.Client.RemoveUserFromGroup(unlinkUserInput)
if err != nil {
multierr = multierror.Append(multierr, err)
}
}
//Detach any policies on the group
allPolicies := []*string{}
err = ig.Client.ListAttachedGroupPoliciesPages(&iam.ListAttachedGroupPoliciesInput{GroupName: groupName},
func(page *iam.ListAttachedGroupPoliciesOutput, lastPage bool) bool {
for _, iamPolicy := range page.AttachedPolicies {
allPolicies = append(allPolicies, iamPolicy.PolicyArn)
}
return !lastPage
},
)
for _, policy := range allPolicies {
unlinkPolicyInput := &iam.DetachGroupPolicyInput{
GroupName: groupName,
PolicyArn: policy,
}
_, err = ig.Client.DetachGroupPolicy(unlinkPolicyInput)
}
// Detach any inline policies on the group
allInlinePolicyNames := []*string{}
err = ig.Client.ListGroupPoliciesPages(&iam.ListGroupPoliciesInput{GroupName: groupName},
func(page *iam.ListGroupPoliciesOutput, lastPage bool) bool {
for _, policyName := range page.PolicyNames {
allInlinePolicyNames = append(allInlinePolicyNames, policyName)
}
return !lastPage
},
)
for _, policyName := range allInlinePolicyNames {
_, err = ig.Client.DeleteGroupPolicy(&iam.DeleteGroupPolicyInput{
GroupName: groupName,
PolicyName: policyName,
})
}
//Delete the group
_, err = ig.Client.DeleteGroup(&iam.DeleteGroupInput{
GroupName: groupName,
})
if err != nil {
multierr = multierror.Append(multierr, err)
} else {
logging.Debugf("[OK] IAM Group %s was deleted in global", aws.StringValue(groupName))
}
e := report.Entry{
Identifier: aws.StringValue(groupName),
ResourceType: "IAM Group",
Error: multierr.ErrorOrNil(),
}
report.Record(e)
errChan <- multierr.ErrorOrNil()
}
// TooManyIamGroupErr Custom Errors
type TooManyIamGroupErr struct{}
func (err TooManyIamGroupErr) Error() string {
return "Too many IAM Groups requested at once"
}