-
Notifications
You must be signed in to change notification settings - Fork 82
/
role.go
333 lines (315 loc) · 11.2 KB
/
role.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
package eks
import (
"context"
"errors"
"fmt"
"os"
"strings"
"time"
awscfn "github.com/aws/aws-k8s-tester/pkg/aws/cloudformation"
awsiam "github.com/aws/aws-k8s-tester/pkg/aws/iam"
"github.com/aws/aws-k8s-tester/version"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/cloudformation"
"go.uber.org/zap"
)
// TemplateClusterRole is the CloudFormation template for EKS cluster role.
//
// e.g.
// Error creating load balancer (will retry): failed to ensure load balancer for service eks-*/hello-world-service: Error creating load balancer: "AccessDenied: User: arn:aws:sts::404174646922:assumed-role/eks-*-cluster-role/* is not authorized to perform: ec2:DescribeAccountAttributes\n\tstatus code: 403"
//
// TODO: scope down (e.g. ec2:DescribeAccountAttributes, ec2:DescribeInternetGateways)
// mng, fargate, etc. may use other roles
const TemplateClusterRole = `
---
AWSTemplateFormatVersion: '2010-09-09'
Description: 'Amazon EKS Cluster Role Basic'
Parameters:
RoleName:
Description: EKS Role name
Type: String
Default: aws-k8s-tester-eks-role
RoleServicePrincipals:
Description: EKS Role Service Principals
Type: CommaDelimitedList
Default: 'ec2.amazonaws.com,eks.amazonaws.com,eks-fargate-pods.amazonaws.com'
RoleManagedPolicyARNs:
Description: EKS Role managed policy ARNs
Type: CommaDelimitedList
Default: 'arn:aws:iam::aws:policy/AmazonEKSServicePolicy,arn:aws:iam::aws:policy/AmazonEKSClusterPolicy,arn:aws:iam::aws:policy/AmazonEKSWorkerNodePolicy,arn:aws:iam::aws:policy/AmazonEKS_CNI_Policy,arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryReadOnly,arn:aws:iam::aws:policy/ElasticLoadBalancingFullAccess'
Resources:
Role:
Type: AWS::IAM::Role
Properties:
RoleName: !Ref RoleName
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service: !Ref RoleServicePrincipals
Action:
- sts:AssumeRole
ManagedPolicyArns: !Ref RoleManagedPolicyARNs
Path: /
Policies:
# https://github.com/kubernetes-sigs/aws-alb-ingress-controller/blob/master/docs/examples/iam-policy.json
- PolicyName: !Join ['-', [!Ref RoleName, 'alb-policy']]
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- acm:DescribeCertificate
- acm:ListCertificates
- acm:GetCertificate
Resource: "*"
- Effect: Allow
Action:
- ec2:AuthorizeSecurityGroupIngress
- ec2:CreateSecurityGroup
- ec2:CreateTags
- ec2:DeleteTags
- ec2:DeleteSecurityGroup
- ec2:DescribeAccountAttributes
- ec2:DescribeAddresses
- ec2:DescribeInstances
- ec2:DescribeInstanceStatus
- ec2:DescribeInternetGateways
- ec2:DescribeNetworkInterfaces
- ec2:DescribeSecurityGroups
- ec2:DescribeSubnets
- ec2:DescribeTags
- ec2:DescribeVpcs
- ec2:ModifyInstanceAttribute
- ec2:ModifyNetworkInterfaceAttribute
- ec2:RevokeSecurityGroupIngress
Resource: "*"
- Effect: Allow
Action:
- elasticloadbalancing:AddListenerCertificates
- elasticloadbalancing:AddTags
- elasticloadbalancing:CreateListener
- elasticloadbalancing:CreateLoadBalancer
- elasticloadbalancing:CreateRule
- elasticloadbalancing:CreateTargetGroup
- elasticloadbalancing:DeleteListener
- elasticloadbalancing:DeleteLoadBalancer
- elasticloadbalancing:DeleteRule
- elasticloadbalancing:DeleteTargetGroup
- elasticloadbalancing:DeregisterTargets
- elasticloadbalancing:DescribeListenerCertificates
- elasticloadbalancing:DescribeListeners
- elasticloadbalancing:DescribeLoadBalancers
- elasticloadbalancing:DescribeLoadBalancerAttributes
- elasticloadbalancing:DescribeRules
- elasticloadbalancing:DescribeSSLPolicies
- elasticloadbalancing:DescribeTags
- elasticloadbalancing:DescribeTargetGroups
- elasticloadbalancing:DescribeTargetGroupAttributes
- elasticloadbalancing:DescribeTargetHealth
- elasticloadbalancing:ModifyListener
- elasticloadbalancing:ModifyLoadBalancerAttributes
- elasticloadbalancing:ModifyRule
- elasticloadbalancing:ModifyTargetGroup
- elasticloadbalancing:ModifyTargetGroupAttributes
- elasticloadbalancing:RegisterTargets
- elasticloadbalancing:RemoveListenerCertificates
- elasticloadbalancing:RemoveTags
- elasticloadbalancing:SetIpAddressType
- elasticloadbalancing:SetSecurityGroups
- elasticloadbalancing:SetSubnets
- elasticloadbalancing:SetWebACL
Resource: "*"
- Effect: Allow
Action:
- iam:CreateServiceLinkedRole
- iam:GetServerCertificate
- iam:ListServerCertificates
Resource: "*"
- Effect: Allow
Action:
- cognito-idp:DescribeUserPoolClient
Resource: "*"
- Effect: Allow
Action:
- waf-regional:GetWebACLForResource
- waf-regional:GetWebACL
- waf-regional:AssociateWebACL
- waf-regional:DisassociateWebACL
Resource: "*"
- Effect: Allow
Action:
- tag:GetResources
- tag:TagResources
Resource: "*"
- Effect: Allow
Action:
- waf:GetWebACL
Resource: "*"
- Effect: Allow
Action:
- shield:DescribeProtection
- shield:GetSubscriptionState
- shield:DeleteProtection
- shield:CreateProtection
- shield:DescribeSubscription
- shield:ListProtections
Resource: "*"
Outputs:
RoleARN:
Description: Role ARN that EKS uses to create AWS resources for Kubernetes
Value: !GetAtt Role.Arn
`
func (ts *Tester) createClusterRole() error {
if !ts.cfg.Parameters.RoleCreate {
ts.lg.Info("Parameters.RoleCreate false; skipping creation")
return awsiam.Validate(
ts.lg,
ts.iamAPI,
ts.cfg.Parameters.RoleName,
[]string{"eks.amazonaws.com"},
[]string{
"arn:aws:iam::aws:policy/AmazonEKSServicePolicy",
"arn:aws:iam::aws:policy/AmazonEKSClusterPolicy",
},
)
}
if ts.cfg.Parameters.RoleCFNStackID != "" &&
ts.cfg.Parameters.RoleARN != "" {
ts.lg.Info("role already created; no need to create a new one")
return nil
}
if ts.cfg.Parameters.RoleName == "" {
return errors.New("cannot create a cluster role with an empty Parameters.RoleName")
}
tmpl := TemplateClusterRole
// role ARN is empty, create a default role
// otherwise, use the existing one
ts.lg.Info("creating a new role", zap.String("cluster-role-name", ts.cfg.Parameters.RoleName))
stackInput := &cloudformation.CreateStackInput{
StackName: aws.String(ts.cfg.Parameters.RoleName),
Capabilities: aws.StringSlice([]string{"CAPABILITY_NAMED_IAM"}),
OnFailure: aws.String(cloudformation.OnFailureDelete),
TemplateBody: aws.String(tmpl),
Tags: awscfn.NewTags(map[string]string{
"Kind": "aws-k8s-tester",
"Name": ts.cfg.Name,
"aws-k8s-tester-version": version.ReleaseVersion,
}),
Parameters: []*cloudformation.Parameter{
{
ParameterKey: aws.String("RoleName"),
ParameterValue: aws.String(ts.cfg.Parameters.RoleName),
},
},
}
if len(ts.cfg.Parameters.RoleServicePrincipals) > 0 {
ts.lg.Info("creating a new cluster role with custom service principals",
zap.Strings("service-principals", ts.cfg.Parameters.RoleServicePrincipals),
)
stackInput.Parameters = append(stackInput.Parameters, &cloudformation.Parameter{
ParameterKey: aws.String("RoleServicePrincipals"),
ParameterValue: aws.String(strings.Join(ts.cfg.Parameters.RoleServicePrincipals, ",")),
})
}
if len(ts.cfg.Parameters.RoleManagedPolicyARNs) > 0 {
ts.lg.Info("creating a new cluster role with custom managed role policies",
zap.Strings("policy-arns", ts.cfg.Parameters.RoleManagedPolicyARNs),
)
stackInput.Parameters = append(stackInput.Parameters, &cloudformation.Parameter{
ParameterKey: aws.String("RoleManagedPolicyARNs"),
ParameterValue: aws.String(strings.Join(ts.cfg.Parameters.RoleManagedPolicyARNs, ",")),
})
}
stackOutput, err := ts.cfnAPI.CreateStack(stackInput)
if err != nil {
return err
}
ts.cfg.Parameters.RoleCFNStackID = aws.StringValue(stackOutput.StackId)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute)
ch := awscfn.Poll(
ctx,
ts.stopCreationCh,
ts.interruptSig,
ts.lg,
ts.cfnAPI,
ts.cfg.Parameters.RoleCFNStackID,
cloudformation.ResourceStatusCreateComplete,
25*time.Second,
10*time.Second,
)
var st awscfn.StackStatus
for st = range ch {
if st.Error != nil {
cancel()
ts.cfg.RecordStatus(fmt.Sprintf("failed to create role (%v)", st.Error))
ts.lg.Warn("polling errror", zap.Error(st.Error))
}
}
cancel()
if st.Error != nil {
return st.Error
}
// update status after creating a new IAM role
for _, o := range st.Stack.Outputs {
switch k := aws.StringValue(o.OutputKey); k {
case "RoleARN":
ts.cfg.Parameters.RoleARN = aws.StringValue(o.OutputValue)
default:
return fmt.Errorf("unexpected OutputKey %q from %q", k, ts.cfg.Parameters.RoleCFNStackID)
}
}
ts.lg.Info("created a new role",
zap.String("cluster-role-cfn-stack-id", ts.cfg.Parameters.RoleCFNStackID),
zap.String("cluster-role-arn", ts.cfg.Parameters.RoleARN),
)
return ts.cfg.Sync()
}
func (ts *Tester) deleteClusterRole() error {
if !ts.cfg.Parameters.RoleCreate {
ts.lg.Info("Parameters.RoleCreate false; skipping deletion")
return nil
}
if ts.cfg.Parameters.RoleCFNStackID == "" {
ts.lg.Info("empty role CFN stack ID; no need to delete role")
return nil
}
ts.lg.Info("deleting role CFN stack", zap.String("cluster-role-cfn-stack-id", ts.cfg.Parameters.RoleCFNStackID))
_, err := ts.cfnAPI.DeleteStack(&cloudformation.DeleteStackInput{
StackName: aws.String(ts.cfg.Parameters.RoleCFNStackID),
})
if err != nil {
return err
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute)
ch := awscfn.Poll(
ctx,
make(chan struct{}), // do not exit on stop
make(chan os.Signal), // do not exit on stop
ts.lg,
ts.cfnAPI,
ts.cfg.Parameters.RoleCFNStackID,
cloudformation.ResourceStatusDeleteComplete,
25*time.Second,
10*time.Second,
)
var st awscfn.StackStatus
for st = range ch {
if st.Error != nil {
cancel()
ts.cfg.RecordStatus(fmt.Sprintf("failed to delete role (%v)", st.Error))
ts.lg.Warn("polling errror", zap.Error(st.Error))
}
}
cancel()
if st.Error != nil {
return st.Error
}
ts.lg.Info("deleted a role",
zap.String("cluster-role-cfn-stack-id", ts.cfg.Parameters.RoleCFNStackID),
zap.String("cluster-role-arn", ts.cfg.Parameters.RoleARN),
zap.String("cluster-role-name", ts.cfg.Parameters.RoleName),
)
return ts.cfg.Sync()
}