forked from awsdocs/aws-doc-sdk-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IamListAdmins.go
208 lines (168 loc) · 5.71 KB
/
IamListAdmins.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// snippet-comment:[These are tags for the AWS doc team's sample catalog. Do not remove.]
// snippet-sourceauthor:[Doug-AWS]
// snippet-sourcedescription:[Lists the IAM users that have adminstrator privileges.]
// snippet-keyword:[AWS Identity and Access Management]
// snippet-keyword:[ListGroupPolicies function]
// snippet-keyword:[ListAttachedGroupPolicies function]
// snippet-keyword:[ListGroupsForUser function]
// snippet-keyword:[GetAccountAuthorizationDetails function]
// snippet-keyword:[Go]
// snippet-sourcesyntax:[go]
// snippet-service:[iam]
// snippet-keyword:[Code Sample]
// snippet-sourcetype:[full-example]
// snippet-sourcedate:[2018-03-16]
/*
Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
This file is licensed under the Apache License, Version 2.0 (the "License").
You may not use this file except in compliance with the License. A copy of
the License is located at
http://aws.amazon.com/apache2.0/
This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
*/
package main
import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/iam"
"fmt"
"os"
)
func UserPolicyHasAdmin(user *iam.UserDetail, admin string) bool {
for _, policy := range user.UserPolicyList {
if *policy.PolicyName == admin {
return true
}
}
return false
}
func AttachedUserPolicyHasAdmin(user *iam.UserDetail, admin string) bool {
for _, policy := range user.AttachedManagedPolicies {
if *policy.PolicyName == admin {
return true
}
}
return false
}
func GroupPolicyHasAdmin(svc *iam.IAM, group *iam.Group, admin string) bool {
input := &iam.ListGroupPoliciesInput{
GroupName: group.GroupName,
}
result, err := svc.ListGroupPolicies(input)
if err != nil {
fmt.Println("Got error calling ListGroupPolicies for group", group.GroupName)
}
// Wade through policies
for _, policyName := range result.PolicyNames {
if
*policyName == admin {
return true
}
}
return false
}
func AttachedGroupPolicyHasAdmin(svc *iam.IAM, group *iam.Group, admin string) bool {
input := &iam.ListAttachedGroupPoliciesInput{GroupName: group.GroupName}
result, err := svc.ListAttachedGroupPolicies(input)
if err != nil {
fmt.Println("Got error getting attached group policies:")
fmt.Println(err.Error())
os.Exit(1)
}
for _, policy := range result.AttachedPolicies {
if *policy.PolicyName == admin {
return true
}
}
return false
}
func UsersGroupsHaveAdmin(svc *iam.IAM, user *iam.UserDetail, admin string) bool {
input := &iam.ListGroupsForUserInput{UserName: user.UserName}
result, err := svc.ListGroupsForUser(input)
if err != nil {
fmt.Println("Got error getting groups for user:")
fmt.Println(err.Error())
os.Exit(1)
}
for _, group := range result.Groups {
groupPolicyHasAdmin := GroupPolicyHasAdmin(svc, group, admin)
if groupPolicyHasAdmin {
return true
}
attachedGroupPolicyHasAdmin := AttachedGroupPolicyHasAdmin(svc, group, admin)
if attachedGroupPolicyHasAdmin {
return true
}
}
return false
}
func IsUserAdmin(svc *iam.IAM, user *iam.UserDetail, admin string) bool {
// Check policy, attached policy, and groups (policy and attached policy)
policyHasAdmin := UserPolicyHasAdmin(user, admin)
if policyHasAdmin {
return true
}
attachedPolicyHasAdmin := AttachedUserPolicyHasAdmin(user, admin)
if attachedPolicyHasAdmin {
return true
}
userGroupsHaveAdmin := UsersGroupsHaveAdmin(svc, user, admin)
if userGroupsHaveAdmin {
return true
}
return false
}
func main() {
sess, err := session.NewSession()
if err != nil {
fmt.Println("Got error creating new session")
fmt.Println(err.Error())
os.Exit(1)
}
svc := iam.New(sess, &aws.Config{Region: aws.String("us-west-2")})
numUsers := 0
numAdmins := 0
// Get list of users
user := "User"
input := &iam.GetAccountAuthorizationDetailsInput{Filter: []*string{&user}}
resp, err := svc.GetAccountAuthorizationDetails(input)
if err != nil {
fmt.Println("Got error getting account details")
fmt.Println(err.Error())
os.Exit(1)
}
// The policy name that indicates administrator access
adminName := "AdministratorAccess"
// Wade through resulting users
for _, user := range resp.UserDetailList {
numUsers += 1
isAdmin := IsUserAdmin(svc, user, adminName)
if isAdmin {
fmt.Println(*user.UserName)
numAdmins += 1
}
}
// Are there more?
for *resp.IsTruncated {
input := &iam.GetAccountAuthorizationDetailsInput{Filter: []*string{&user}, Marker: resp.Marker}
resp, err = svc.GetAccountAuthorizationDetails(input)
if err != nil {
fmt.Println("Got error getting account details")
fmt.Println(err.Error())
os.Exit(1)
}
// Wade through resulting users
for _, user := range resp.UserDetailList {
numUsers += 1
isAdmin := IsUserAdmin(svc, user, adminName)
if isAdmin {
fmt.Println(*user.UserName)
numAdmins += 1
}
}
}
fmt.Println("")
fmt.Println("Found", numAdmins, "admin(s) out of", numUsers, "user(s).")
}