-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
iam.go
382 lines (324 loc) · 13.4 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
373
374
375
376
377
378
379
380
381
382
package builder
import (
"context"
"fmt"
"github.com/aws/aws-sdk-go-v2/service/cloudformation/types"
"github.com/kris-nova/logger"
"github.com/weaveworks/eksctl/pkg/iam"
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"
iamPolicyAmazonEKSLocalOutpostClusterPolicy = "AmazonEKSLocalOutpostClusterPolicy"
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...),
})
}
func (c *resourceSet) attachAllowPolicyDocument(name string, refRole *gfnt.Value, document api.InlineDocument) {
c.newResource(name, &gfniam.Policy{
PolicyName: makeName(name),
Roles: gfnt.NewSlice(refRole),
PolicyDocument: document,
})
}
// 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
var role *gfniam.Role
if c.spec.IsControlPlaneOnOutposts() {
role = &gfniam.Role{
AssumeRolePolicyDocument: cft.MakeAssumeRolePolicyDocumentForServices(
MakeServiceRef("EC2"),
),
ManagedPolicyArns: gfnt.NewSlice(makePolicyARNs(iamPolicyAmazonEKSLocalOutpostClusterPolicy)...),
}
} else {
managedPolicyARNs := []string{iamPolicyAmazonEKSClusterPolicy}
if !api.IsDisabled(c.spec.IAM.VPCResourceControllerPolicy) {
managedPolicyARNs = append(managedPolicyARNs, iamPolicyAmazonEKSVPCResourceController)
}
role = &gfniam.Role{
AssumeRolePolicyDocument: cft.MakeAssumeRolePolicyDocumentForServices(
MakeServiceRef("EKS"),
),
ManagedPolicyArns: gfnt.NewSlice(makePolicyARNs(managedPolicyARNs...)...),
}
}
if api.IsSetAndNonEmptyString(c.spec.IAM.ServiceRolePermissionsBoundary) {
role.PermissionsBoundary = gfnt.NewString(*c.spec.IAM.ServiceRolePermissionsBoundary)
}
c.newResource("ServiceRole", role)
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(ctx context.Context) error {
nodeGroupIAM := n.options.NodeGroup.IAM
if nodeGroupIAM.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(nodeGroupIAM.InstanceProfileARN)
if nodeGroupIAM.InstanceRoleARN != "" {
n.rs.defineOutputWithoutCollector(outputs.NodeGroupInstanceProfileARN, nodeGroupIAM.InstanceProfileARN, true)
n.rs.defineOutputWithoutCollector(outputs.NodeGroupInstanceRoleARN, nodeGroupIAM.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, nodeGroupIAM.InstanceProfileARN, true, func(v string) error {
return iam.ImportInstanceRoleFromProfileARN(ctx, n.iamAPI, n.options.NodeGroup, v)
})
return nil
}
n.rs.withIAM = true
if nodeGroupIAM.InstanceRoleARN != "" {
roleARN := NormalizeARN(nodeGroupIAM.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 {
nodeGroupIAM.InstanceProfileARN = v
return nil
})
n.rs.defineOutputWithoutCollector(outputs.NodeGroupInstanceRoleARN, roleARN, true)
return nil
}
// if neither role nor profile is given - create both
if nodeGroupIAM.InstanceRoleName != "" {
// setting role name requires additional capabilities
n.rs.withNamedIAM = true
}
if err := createRole(n.rs, n.options.ClusterConfig.IAM, nodeGroupIAM, false, n.options.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 {
nodeGroupIAM.InstanceProfileARN = v
return nil
})
n.rs.defineOutputFromAtt(outputs.NodeGroupInstanceRoleARN, cfnIAMInstanceRoleName, "Arn", true, func(v string) error {
nodeGroupIAM.InstanceRoleARN = v
return nil
})
return nil
}
func NewIAMRoleResourceSetForServiceAccount(spec *api.ClusterIAMServiceAccount, oidc *iamoidc.OpenIDConnectManager) *IAMRoleResourceSet {
return &IAMRoleResourceSet{
template: cft.NewTemplate(),
attachPolicy: spec.AttachPolicy,
attachPolicyARNs: spec.AttachPolicyARNs,
serviceAccount: spec.Name,
namespace: spec.Namespace,
wellKnownPolicies: spec.WellKnownPolicies,
roleName: spec.RoleName,
permissionsBoundary: spec.PermissionsBoundary,
description: fmt.Sprintf(
"IAM role for serviceaccount %q %s",
spec.NameString(),
templateDescriptionSuffix,
),
oidc: oidc,
roleNameCollector: func(v string) error {
spec.Status = &api.ClusterIAMServiceAccountStatus{
RoleARN: &v,
}
return nil
},
}
}
func NewIAMRoleResourceSetForPodIdentityWithTrustStatements(spec *api.PodIdentityAssociation, trustStatements []api.IAMStatement) *IAMRoleResourceSet {
rs := NewIAMRoleResourceSetForPodIdentity(spec)
rs.trustStatements = trustStatements
return rs
}
func NewIAMRoleResourceSetForPodIdentity(spec *api.PodIdentityAssociation) *IAMRoleResourceSet {
return &IAMRoleResourceSet{
template: cft.NewTemplate(),
attachPolicy: spec.PermissionPolicy,
attachPolicyARNs: spec.PermissionPolicyARNs,
serviceAccount: spec.ServiceAccountName,
namespace: spec.Namespace,
wellKnownPolicies: spec.WellKnownPolicies,
roleName: spec.RoleName,
permissionsBoundary: spec.PermissionsBoundaryARN,
description: fmt.Sprintf(
"IAM role for pod identity association %s",
templateDescriptionSuffix,
),
roleNameCollector: func(v string) error {
spec.RoleARN = v
return nil
},
}
}
// IAMRoleResourceSet holds IAM Role stack build-time information
type IAMRoleResourceSet struct {
template *cft.Template
oidc *iamoidc.OpenIDConnectManager
outputs *outputs.CollectorSet
roleName string
wellKnownPolicies api.WellKnownPolicies
attachPolicyARNs []string
attachPolicy api.InlineDocument
trustStatements []api.IAMStatement
roleNameCollector func(string) error
OutputRole string
serviceAccount string
namespace string
permissionsBoundary string
description string
}
// NewIAMRoleResourceSetWithAttachPolicyARNs builds IAM Role stack from the give spec
func NewIAMRoleResourceSetWithAttachPolicyARNs(name, namespace, serviceAccount, permissionsBoundary string, attachPolicyARNs []string, oidc *iamoidc.OpenIDConnectManager) *IAMRoleResourceSet {
return newIAMRoleResourceSet(name, namespace, serviceAccount, permissionsBoundary, nil, attachPolicyARNs, api.WellKnownPolicies{}, oidc)
}
// NewIAMRoleResourceSetWithAttachPolicy builds IAM Role stack from the give spec
func NewIAMRoleResourceSetWithAttachPolicy(name, namespace, serviceAccount, permissionsBoundary string, attachPolicy api.InlineDocument, oidc *iamoidc.OpenIDConnectManager) *IAMRoleResourceSet {
return newIAMRoleResourceSet(name, namespace, serviceAccount, permissionsBoundary, attachPolicy, nil, api.WellKnownPolicies{}, oidc)
}
// NewIAMRoleResourceSetWithAttachPolicyARNs builds IAM Role stack from the give spec
func NewIAMRoleResourceSetWithWellKnownPolicies(name, namespace, serviceAccount, permissionsBoundary string, wellKnownPolicies api.WellKnownPolicies, oidc *iamoidc.OpenIDConnectManager) *IAMRoleResourceSet {
return newIAMRoleResourceSet(name, namespace, serviceAccount, permissionsBoundary, nil, nil, wellKnownPolicies, oidc)
}
// NewIAMRoleResourceSetForServiceAccount builds IAM Role stack from the give spec
func newIAMRoleResourceSet(name, namespace, serviceAccount, permissionsBoundary string, attachPolicy api.InlineDocument, attachPolicyARNs []string, wellKnownPolicies api.WellKnownPolicies, oidc *iamoidc.OpenIDConnectManager) *IAMRoleResourceSet {
rs := &IAMRoleResourceSet{
template: cft.NewTemplate(),
attachPolicyARNs: attachPolicyARNs,
attachPolicy: attachPolicy,
oidc: oidc,
serviceAccount: serviceAccount,
namespace: namespace,
permissionsBoundary: permissionsBoundary,
description: fmt.Sprintf(
"IAM role for %q %s",
name,
templateDescriptionSuffix,
),
wellKnownPolicies: wellKnownPolicies,
}
rs.roleNameCollector = func(v string) error {
rs.OutputRole = v
return nil
}
return rs
}
// WithIAM returns true
func (*IAMRoleResourceSet) WithIAM() bool { return true }
// WithNamedIAM returns false
func (rs *IAMRoleResourceSet) WithNamedIAM() bool { return rs.roleName != "" }
// AddAllResources adds all resources for the stack
func (rs *IAMRoleResourceSet) AddAllResources() error {
rs.template.Description = rs.description
role := &cft.IAMRole{
AssumeRolePolicyDocument: rs.makeAssumeRolePolicyDocument(),
PermissionsBoundary: rs.permissionsBoundary,
RoleName: rs.roleName,
}
for _, arn := range rs.attachPolicyARNs {
role.ManagedPolicyArns = append(role.ManagedPolicyArns, arn)
}
managedPolicies, customPolicies := createWellKnownPolicies(rs.wellKnownPolicies)
for _, p := range managedPolicies {
role.ManagedPolicyArns = append(role.ManagedPolicyArns, makePolicyARN(p.name))
}
roleRef := rs.template.NewResource(outputs.IAMServiceAccountRoleName, role)
for _, p := range customPolicies {
doc := cft.MakePolicyDocument(p.Statements...)
rs.template.AttachPolicy(p.Name, roleRef, doc)
}
rs.template.Outputs[outputs.IAMServiceAccountRoleName] = cft.Output{
Value: cft.MakeFnGetAttString("Role1.Arn"),
}
rs.outputs = outputs.NewCollectorSet(map[string]outputs.Collector{
outputs.IAMServiceAccountRoleName: rs.roleNameCollector,
})
if len(rs.attachPolicy) != 0 {
rs.template.AttachPolicy("Policy1", roleRef, rs.attachPolicy)
}
return nil
}
func (rs *IAMRoleResourceSet) makeAssumeRolePolicyDocument() cft.MapOfInterfaces {
if len(rs.trustStatements) > 0 {
return cft.MakePolicyDocument(toMapOfInterfaces(rs.trustStatements)...)
}
if rs.oidc == nil {
return cft.MakeAssumeRolePolicyDocumentForPodIdentity()
}
if rs.serviceAccount != "" && rs.namespace != "" {
logger.Debug("service account location provided: %s/%s, adding sub condition", api.AWSNodeMeta.Namespace, api.AWSNodeMeta.Name)
return rs.oidc.MakeAssumeRolePolicyDocumentWithServiceAccountConditions(rs.namespace, rs.serviceAccount)
}
return rs.oidc.MakeAssumeRolePolicyDocument()
}
func toMapOfInterfaces(old []api.IAMStatement) []cft.MapOfInterfaces {
new := []cft.MapOfInterfaces{}
for _, s := range old {
new = append(new, s.ToMapOfInterfaces())
}
return new
}
// 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 types.Stack) error {
return rs.outputs.MustCollect(stack)
}