forked from eksctl-io/eksctl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iam.go
372 lines (311 loc) · 12.5 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
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
package builder
import (
"fmt"
"github.com/kris-nova/logger"
"github.com/weaveworks/eksctl/pkg/iam"
cfn "github.com/aws/aws-sdk-go/service/cloudformation"
gfniam "github.com/weaveworks/goformation/v4/cloudformation/iam"
gfnt "github.com/weaveworks/goformation/v4/cloudformation/types"
api "github.com/weaveworks/eksctl/pkg/apis/eksctl.io/v1alpha5"
"github.com/weaveworks/eksctl/pkg/cfn/outputs"
cft "github.com/weaveworks/eksctl/pkg/cfn/template"
iamoidc "github.com/weaveworks/eksctl/pkg/iam/oidc"
)
const (
iamPolicyAmazonEKSClusterPolicy = "AmazonEKSClusterPolicy"
iamPolicyAmazonEKSVPCResourceController = "AmazonEKSVPCResourceController"
iamPolicyAmazonEKSWorkerNodePolicy = "AmazonEKSWorkerNodePolicy"
iamPolicyAmazonEKSCNIPolicy = "AmazonEKS_CNI_Policy"
iamPolicyAmazonEC2ContainerRegistryPowerUser = "AmazonEC2ContainerRegistryPowerUser"
iamPolicyAmazonEC2ContainerRegistryReadOnly = "AmazonEC2ContainerRegistryReadOnly"
iamPolicyCloudWatchAgentServerPolicy = "CloudWatchAgentServerPolicy"
iamPolicyAmazonSSMManagedInstanceCore = "AmazonSSMManagedInstanceCore"
iamPolicyAmazonEKSFargatePodExecutionRolePolicy = "AmazonEKSFargatePodExecutionRolePolicy"
)
const (
cfnIAMInstanceRoleName = "NodeInstanceRole"
cfnIAMInstanceProfileName = "NodeInstanceProfile"
)
var (
iamDefaultNodePolicies = []string{
iamPolicyAmazonEKSWorkerNodePolicy,
}
)
func (c *resourceSet) attachAllowPolicy(name string, refRole *gfnt.Value, statements []cft.MapOfInterfaces) {
c.newResource(name, &gfniam.Policy{
PolicyName: makeName(name),
Roles: gfnt.NewSlice(refRole),
PolicyDocument: cft.MakePolicyDocument(statements...),
})
}
// WithIAM states, if IAM roles will be created or not
func (c *ClusterResourceSet) WithIAM() bool {
return c.rs.withIAM
}
// WithNamedIAM states, if specifically named IAM roles will be created or not
func (c *ClusterResourceSet) WithNamedIAM() bool {
return c.rs.withNamedIAM
}
func (c *ClusterResourceSet) addResourcesForIAM() {
c.rs.withNamedIAM = false
if api.IsSetAndNonEmptyString(c.spec.IAM.ServiceRoleARN) {
c.rs.withIAM = false
c.rs.defineOutputWithoutCollector(outputs.ClusterServiceRoleARN, c.spec.IAM.ServiceRoleARN, true)
return
}
c.rs.withIAM = true
managedPolicyArns := []string{
iamPolicyAmazonEKSClusterPolicy,
}
if !api.IsDisabled(c.spec.IAM.VPCResourceControllerPolicy) {
managedPolicyArns = append(managedPolicyArns, iamPolicyAmazonEKSVPCResourceController)
}
role := &gfniam.Role{
AssumeRolePolicyDocument: cft.MakeAssumeRolePolicyDocumentForServices(
MakeServiceRef("EKS"),
// Ensure that EKS can schedule pods onto Fargate, should the user
// define so-called "Fargate profiles" in order to do so:
MakeServiceRef("EKSFargatePods"),
),
ManagedPolicyArns: gfnt.NewSlice(makePolicyARNs(managedPolicyArns...)...),
}
if api.IsSetAndNonEmptyString(c.spec.IAM.ServiceRolePermissionsBoundary) {
role.PermissionsBoundary = gfnt.NewString(*c.spec.IAM.ServiceRolePermissionsBoundary)
}
refSR := c.newResource("ServiceRole", role)
c.rs.attachAllowPolicy("PolicyCloudWatchMetrics", refSR, cloudWatchMetricsStatements())
// These are potentially required for creating load balancers but aren't included in the
// AmazonEKSClusterPolicy
// See https://docs.aws.amazon.com/elasticloadbalancing/latest/userguide/elb-api-permissions.html#required-permissions-v2
// and weaveworks/eksctl#2488
c.rs.attachAllowPolicy("PolicyELBPermissions", refSR, elbStatements())
c.rs.defineOutputFromAtt(outputs.ClusterServiceRoleARN, "ServiceRole", "Arn", true, func(v string) error {
c.spec.IAM.ServiceRoleARN = &v
return nil
})
}
// WithIAM states, if IAM roles will be created or not
func (n *NodeGroupResourceSet) WithIAM() bool {
return n.rs.withIAM
}
// WithNamedIAM states, if specifically named IAM roles will be created or not
func (n *NodeGroupResourceSet) WithNamedIAM() bool {
return n.rs.withNamedIAM
}
func (n *NodeGroupResourceSet) addResourcesForIAM() error {
if n.spec.IAM.InstanceProfileARN != "" {
n.rs.withIAM = false
n.rs.withNamedIAM = false
// if instance profile is given, as well as the role, we simply use both and export the role
// (which is needed in order to authorise the nodegroup)
n.instanceProfileARN = gfnt.NewString(n.spec.IAM.InstanceProfileARN)
if n.spec.IAM.InstanceRoleARN != "" {
n.rs.defineOutputWithoutCollector(outputs.NodeGroupInstanceProfileARN, n.spec.IAM.InstanceProfileARN, true)
n.rs.defineOutputWithoutCollector(outputs.NodeGroupInstanceRoleARN, n.spec.IAM.InstanceRoleARN, true)
return nil
}
// if instance role is not given, export profile and use the getter to call importer function
n.rs.defineOutput(outputs.NodeGroupInstanceProfileARN, n.spec.IAM.InstanceProfileARN, true, func(v string) error {
return iam.ImportInstanceRoleFromProfileARN(n.provider, n.spec, v)
})
return nil
}
n.rs.withIAM = true
if n.spec.IAM.InstanceRoleARN != "" {
roleARN := NormalizeARN(n.spec.IAM.InstanceRoleARN)
// if role is set, but profile isn't - create profile
n.newResource(cfnIAMInstanceProfileName, &gfniam.InstanceProfile{
Path: gfnt.NewString("/"),
Roles: gfnt.NewStringSlice(AbstractRoleNameFromARN(roleARN)),
})
n.instanceProfileARN = gfnt.MakeFnGetAttString(cfnIAMInstanceProfileName, "Arn")
n.rs.defineOutputFromAtt(outputs.NodeGroupInstanceProfileARN, cfnIAMInstanceProfileName, "Arn", true, func(v string) error {
n.spec.IAM.InstanceProfileARN = v
return nil
})
n.rs.defineOutputWithoutCollector(outputs.NodeGroupInstanceRoleARN, roleARN, true)
return nil
}
// if neither role nor profile is given - create both
if n.spec.IAM.InstanceRoleName != "" {
// setting role name requires additional capabilities
n.rs.withNamedIAM = true
}
enableSSM := n.spec.SSH != nil && api.IsEnabled(n.spec.SSH.EnableSSM)
if err := createRole(n.rs, n.clusterSpec.IAM, n.spec.IAM, false, enableSSM, n.forceAddCNIPolicy); err != nil {
return err
}
n.newResource(cfnIAMInstanceProfileName, &gfniam.InstanceProfile{
Path: gfnt.NewString("/"),
Roles: gfnt.NewSlice(gfnt.MakeRef(cfnIAMInstanceRoleName)),
})
n.instanceProfileARN = gfnt.MakeFnGetAttString(cfnIAMInstanceProfileName, "Arn")
n.rs.defineOutputFromAtt(outputs.NodeGroupInstanceProfileARN, cfnIAMInstanceProfileName, "Arn", true, func(v string) error {
n.spec.IAM.InstanceProfileARN = v
return nil
})
n.rs.defineOutputFromAtt(outputs.NodeGroupInstanceRoleARN, cfnIAMInstanceRoleName, "Arn", true, func(v string) error {
n.spec.IAM.InstanceRoleARN = v
return nil
})
return nil
}
// IAMServiceAccountResourceSet holds iamserviceaccount stack build-time information
type IAMServiceAccountResourceSet struct {
template *cft.Template
spec *api.ClusterIAMServiceAccount
oidc *iamoidc.OpenIDConnectManager
outputs *outputs.CollectorSet
}
// NewIAMServiceAccountResourceSet builds iamserviceaccount stack from the give spec
func NewIAMServiceAccountResourceSet(spec *api.ClusterIAMServiceAccount, oidc *iamoidc.OpenIDConnectManager) *IAMServiceAccountResourceSet {
return &IAMServiceAccountResourceSet{
template: cft.NewTemplate(),
spec: spec,
oidc: oidc,
}
}
// WithIAM returns true
func (*IAMServiceAccountResourceSet) WithIAM() bool { return true }
// WithNamedIAM implements the ResourceSet interface
func (rs *IAMServiceAccountResourceSet) WithNamedIAM() bool {
return rs.spec.RoleName != ""
}
// AddAllResources adds all resources for the stack
func (rs *IAMServiceAccountResourceSet) AddAllResources() error {
rs.template.Description = fmt.Sprintf(
"IAM role for serviceaccount %q %s",
rs.spec.NameString(),
templateDescriptionSuffix,
)
// we use a single stack for each service account, but there maybe a few roles in it
// so will need to give them unique names
// we will need to consider using a large stack for all the roles, but that needs some
// testing and potentially a better stack mutation strategy
role := &cft.IAMRole{
AssumeRolePolicyDocument: rs.oidc.MakeAssumeRolePolicyDocumentWithServiceAccountConditions(rs.spec.Namespace, rs.spec.Name),
PermissionsBoundary: rs.spec.PermissionsBoundary,
RoleName: rs.spec.RoleName,
}
for _, arn := range rs.spec.AttachPolicyARNs {
role.ManagedPolicyArns = append(role.ManagedPolicyArns, arn)
}
wellKnownPolicies := createWellKnownPolicies(rs.spec.WellKnownPolicies)
for _, p := range wellKnownPolicies {
for _, np := range p.NamedPolicies {
role.ManagedPolicyArns = append(role.ManagedPolicyArns, makePolicyARN(np))
}
}
roleRef := rs.template.NewResource("Role1", role)
for _, p := range wellKnownPolicies {
if p.Name != "" {
doc := cft.MakePolicyDocument(p.Statements...)
rs.template.AttachPolicy(p.Name, roleRef, doc)
}
}
// TODO: declare output collector automatically when all stack builders migrated to our template package
rs.template.Outputs["Role1"] = cft.Output{
Value: cft.MakeFnGetAttString("Role1.Arn"),
}
rs.outputs = outputs.NewCollectorSet(map[string]outputs.Collector{
"Role1": func(v string) error {
rs.spec.Status = &api.ClusterIAMServiceAccountStatus{
RoleARN: &v,
}
return nil
},
})
if len(rs.spec.AttachPolicy) != 0 {
rs.template.AttachPolicy("Policy1", roleRef, rs.spec.AttachPolicy)
}
return nil
}
// RenderJSON will render iamserviceaccount stack as JSON
func (rs *IAMServiceAccountResourceSet) RenderJSON() ([]byte, error) {
return rs.template.RenderJSON()
}
// GetAllOutputs will get all outputs from iamserviceaccount stack
func (rs *IAMServiceAccountResourceSet) GetAllOutputs(stack cfn.Stack) error {
return rs.outputs.MustCollect(stack)
}
// IAMRoleResourceSet holds IAM Role stack build-time information
type IAMRoleResourceSet struct {
template *cft.Template
oidc *iamoidc.OpenIDConnectManager
outputs *outputs.CollectorSet
name string
attachPolicyARNs []string
attachPolicy api.InlineDocument
OutputRole string
serviceAccount string
namespace string
}
// NewIAMServiceAccountResourceSet builds IAM Role stack from the give spec
func NewIAMRoleResourceSetWithAttachPolicyARNs(name, namespace, serviceAccount string, attachPolicyARNs []string, oidc *iamoidc.OpenIDConnectManager) *IAMRoleResourceSet {
return &IAMRoleResourceSet{
template: cft.NewTemplate(),
attachPolicyARNs: attachPolicyARNs,
name: name,
oidc: oidc,
serviceAccount: serviceAccount,
namespace: namespace,
}
}
// NewIAMServiceAccountResourceSet builds IAM Role stack from the give spec
func NewIAMRoleResourceSetWithAttachPolicy(name, namespace, serviceAccount string, attachPolicy api.InlineDocument, oidc *iamoidc.OpenIDConnectManager) *IAMRoleResourceSet {
return &IAMRoleResourceSet{
template: cft.NewTemplate(),
attachPolicy: attachPolicy,
name: name,
oidc: oidc,
serviceAccount: serviceAccount,
namespace: namespace,
}
}
// WithIAM returns true
func (*IAMRoleResourceSet) WithIAM() bool { return true }
// WithNamedIAM returns false
func (*IAMRoleResourceSet) WithNamedIAM() bool { return false }
// AddAllResources adds all resources for the stack
func (rs *IAMRoleResourceSet) AddAllResources() error {
rs.template.Description = fmt.Sprintf(
"IAM role for %q %s",
rs.name,
templateDescriptionSuffix,
)
var assumeRolePolicyDocument cft.MapOfInterfaces
if rs.serviceAccount != "" && rs.namespace != "" {
logger.Debug("service account location provided: %s/%s, adding sub condition", api.AWSNodeMeta.Namespace, api.AWSNodeMeta.Name)
assumeRolePolicyDocument = rs.oidc.MakeAssumeRolePolicyDocumentWithServiceAccountConditions(rs.namespace, rs.serviceAccount)
} else {
assumeRolePolicyDocument = rs.oidc.MakeAssumeRolePolicyDocument()
}
role := &cft.IAMRole{
AssumeRolePolicyDocument: assumeRolePolicyDocument,
}
for _, arn := range rs.attachPolicyARNs {
role.ManagedPolicyArns = append(role.ManagedPolicyArns, arn)
}
roleRef := rs.template.NewResource("Role1", role)
rs.template.Outputs["Role1"] = cft.Output{
Value: cft.MakeFnGetAttString("Role1.Arn"),
}
rs.outputs = outputs.NewCollectorSet(map[string]outputs.Collector{
"Role1": func(v string) error {
rs.OutputRole = v
return nil
},
})
if len(rs.attachPolicy) != 0 {
rs.template.AttachPolicy("Policy1", roleRef, rs.attachPolicy)
}
return nil
}
// RenderJSON will render iamserviceaccount stack as JSON
func (rs *IAMRoleResourceSet) RenderJSON() ([]byte, error) {
return rs.template.RenderJSON()
}
// GetAllOutputs will get all outputs from iamserviceaccount stack
func (rs *IAMRoleResourceSet) GetAllOutputs(stack cfn.Stack) error {
return rs.outputs.MustCollect(stack)
}