/
iam.go
228 lines (191 loc) · 5.49 KB
/
iam.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
Copyright 2022 Cortex Labs, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License 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 aws
import (
"fmt"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/iam"
"github.com/cortexlabs/cortex/pkg/lib/errors"
)
func PartitionFromRegion(region string) string {
if strings.Contains(region, "us-gov") {
return "aws-us-gov"
}
return "aws"
}
func administratorAccessARN(region string) string {
return fmt.Sprintf("arn:%s:iam::aws:policy/AdministratorAccess", PartitionFromRegion(region))
}
func (c *Client) GetUser() (iam.User, error) {
getUserOutput, err := c.IAM().GetUser(nil)
if err != nil {
return iam.User{}, errors.WithStack(err)
}
return *getUserOutput.User, nil
}
func (c *Client) GetGroupsForUser(userName string) ([]iam.Group, error) {
input := &iam.ListGroupsForUserInput{
UserName: &userName,
}
var groups []iam.Group
err := c.IAM().ListGroupsForUserPages(input, func(page *iam.ListGroupsForUserOutput, lastPage bool) bool {
for _, group := range page.Groups {
groups = append(groups, *group)
}
return true
})
if err != nil {
return nil, errors.WithStack(err)
}
return groups, nil
}
// Note: root users don't have attached policies, but do have full access
func (c *Client) GetManagedPoliciesForUser(userName string) ([]iam.AttachedPolicy, error) {
var policies []iam.AttachedPolicy
userManagedPolicies, err := c.IAM().ListAttachedUserPolicies(&iam.ListAttachedUserPoliciesInput{
UserName: &userName,
})
if err != nil {
return nil, errors.WithStack(err)
}
for _, policy := range userManagedPolicies.AttachedPolicies {
policies = append(policies, *policy)
}
groups, err := c.GetGroupsForUser(userName)
if err != nil {
return nil, err
}
for _, group := range groups {
groupManagedPolicies, err := c.IAM().ListAttachedGroupPolicies(&iam.ListAttachedGroupPoliciesInput{
GroupName: group.GroupName,
})
if err != nil {
return nil, errors.WithStack(err)
}
for _, policy := range groupManagedPolicies.AttachedPolicies {
policies = append(policies, *policy)
}
}
return policies, nil
}
func (c *Client) isAdminUser(user iam.User) bool {
// Root users may not have a user name
if user.UserName == nil {
return true
}
// Root users may have a user name
if user.Arn == nil || strings.HasSuffix(*user.Arn, ":root") {
return true
}
policies, err := c.GetManagedPoliciesForUser(*user.UserName)
if err != nil {
return false
}
for _, policy := range policies {
if *policy.PolicyArn == administratorAccessARN(c.Region) {
return true
}
}
return false
}
func (c *Client) isRoleAdmin() bool {
identity, err := c.STS().GetCallerIdentity(nil)
if err != nil {
return false
}
arn := identity.Arn
if arn == nil {
return false
}
if !strings.Contains(*arn, ":assumed-role/") {
return false
}
// expected to be in form arn:aws:sts::account-id:assumed-role/role-name/role-session-name
arnSplit := strings.Split(*arn, "/")
if len(arnSplit) < 2 {
return false
}
roleName := arnSplit[1]
isAdmin := false
c.IAM().ListAttachedRolePoliciesPages(&iam.ListAttachedRolePoliciesInput{
RoleName: &roleName,
}, func(policies *iam.ListAttachedRolePoliciesOutput, lastPage bool) bool {
for _, policy := range policies.AttachedPolicies {
if *policy.PolicyArn == administratorAccessARN(c.Region) {
isAdmin = true
return false
}
}
return !lastPage
})
return isAdmin
}
func (c *Client) IsAdmin() bool {
user, err := c.GetUser()
if err != nil {
awsErr, ok := errors.CauseOrSelf(err).(awserr.Error)
if !ok {
return false
}
// this particular error is returned if GetUser() is invoked using credentials that are not for users
if awsErr.Code() == "ValidationError" && strings.Contains(strings.ToLower(err.Error()), strings.ToLower("calling with non-User credentials")) {
return c.isRoleAdmin()
}
return false
}
return c.isAdminUser(user)
}
// delete non default policy versions and then delete the policy (as required by aws)
func (c *Client) DeletePolicy(policyARN string) error {
policyVersionList, err := c.IAM().ListPolicyVersions(&iam.ListPolicyVersionsInput{
PolicyArn: aws.String(policyARN),
})
if err != nil {
return errors.WithStack(err)
}
for _, policy := range policyVersionList.Versions {
if !*policy.IsDefaultVersion {
_, err = c.IAM().DeletePolicyVersion(&iam.DeletePolicyVersionInput{
PolicyArn: aws.String(policyARN),
VersionId: policy.VersionId,
})
if err != nil {
return errors.WithStack(err)
}
}
}
_, err = c.IAM().DeletePolicy(&iam.DeletePolicyInput{
PolicyArn: aws.String(policyARN),
})
if err != nil {
return errors.WithStack(err)
}
return nil
}
func (c *Client) GetPolicyOrNil(policyARN string) (*iam.Policy, error) {
policyOutput, err := c.IAM().GetPolicy(&iam.GetPolicyInput{
PolicyArn: aws.String(policyARN),
})
if err != nil {
if IsErrCode(err, iam.ErrCodeNoSuchEntityException) {
return nil, nil
}
return nil, errors.WithStack(err)
}
if policyOutput != nil {
return policyOutput.Policy, nil
}
return nil, nil
}