-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
iam.go
512 lines (440 loc) · 14.3 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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
/*
Copyright 2019 The Kubernetes Authors.
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 awsmodel
import (
"fmt"
"sort"
"strings"
"github.com/aws/aws-sdk-go/aws/endpoints"
awsIam "github.com/aws/aws-sdk-go/service/iam"
"k8s.io/apimachinery/pkg/types"
"k8s.io/klog/v2"
"k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/pkg/model"
"k8s.io/kops/pkg/model/iam"
"k8s.io/kops/pkg/util/stringorslice"
"k8s.io/kops/upup/pkg/fi"
"k8s.io/kops/upup/pkg/fi/cloudup/awstasks"
"k8s.io/kops/upup/pkg/fi/cloudup/awsup"
)
// IAMModelBuilder configures IAM objects
type IAMModelBuilder struct {
*AWSModelContext
Lifecycle fi.Lifecycle
Cluster *kops.Cluster
}
var (
_ fi.CloudupModelBuilder = &IAMModelBuilder{}
_ fi.HasDeletions = &IAMModelBuilder{}
)
const NodeRolePolicyTemplate = `{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": { "Service": "{{ IAMServiceEC2 }}"},
"Action": "sts:AssumeRole"
}
]
}`
func (b *IAMModelBuilder) Build(c *fi.CloudupModelBuilderContext) error {
// Collect managed Instance Group roles
managedRoles := make(map[kops.InstanceGroupRole]bool)
// Collect Instance Profile ARNs and their associated Instance Group roles
sharedProfileARNsToIGRole := make(map[string]kops.InstanceGroupRole)
for _, ig := range b.InstanceGroups {
if ig.Spec.IAM != nil && ig.Spec.IAM.Profile != nil {
specProfile := fi.ValueOf(ig.Spec.IAM.Profile)
if matchingRole, ok := sharedProfileARNsToIGRole[specProfile]; ok {
if matchingRole != ig.Spec.Role {
return fmt.Errorf("found IAM instance profile assigned to multiple Instance Group roles %v and %v: %v",
ig.Spec.Role, sharedProfileARNsToIGRole[specProfile], specProfile)
}
} else {
sharedProfileARNsToIGRole[specProfile] = ig.Spec.Role
}
} else {
managedRoles[ig.Spec.Role] = true
}
}
// Generate IAM tasks for each shared role
for profileARN, igRole := range sharedProfileARNsToIGRole {
lchPermissions := false
defaultWarmPool := b.Cluster.Spec.CloudProvider.AWS.WarmPool
for _, ig := range b.InstanceGroups {
warmPool := defaultWarmPool.ResolveDefaults(ig)
if ig.Spec.Role == igRole && warmPool.IsEnabled() && warmPool.EnableLifecycleHook {
lchPermissions = true
break
}
}
role, err := iam.BuildNodeRoleSubject(igRole, lchPermissions)
if err != nil {
return err
}
iamName, err := model.FindCustomAuthNameFromArn(profileARN)
if err != nil {
return fmt.Errorf("unable to parse instance profile name from arn %q: %v", profileARN, err)
}
err = b.buildIAMTasks(role, iamName, c, true)
if err != nil {
return err
}
}
// Generate IAM tasks for each managed role
defaultWarmPool := b.Cluster.Spec.CloudProvider.AWS.WarmPool
for igRole := range managedRoles {
haveWarmPool := false
for _, ig := range b.InstanceGroups {
warmPool := defaultWarmPool.ResolveDefaults(ig)
if ig.Spec.Role == igRole && warmPool.IsEnabled() && warmPool.EnableLifecycleHook {
haveWarmPool = true
break
}
}
role, err := iam.BuildNodeRoleSubject(igRole, haveWarmPool)
if err != nil {
return err
}
iamName := b.IAMName(igRole)
if err := b.buildIAMTasks(role, iamName, c, false); err != nil {
return err
}
}
iamSpec := b.Cluster.Spec.IAM
if iamSpec != nil {
for _, sa := range iamSpec.ServiceAccountExternalPermissions {
var p *iam.Policy
aws := sa.AWS
if aws.InlinePolicy != "" {
bp, err := b.buildPolicy(aws.InlinePolicy)
p = bp
if err != nil {
return fmt.Errorf("error inline policy: %w", err)
}
}
serviceAccount := &iam.GenericServiceAccount{
NamespacedName: types.NamespacedName{
Name: sa.Name,
Namespace: sa.Namespace,
},
Policy: p,
}
iamRole, err := b.BuildServiceAccountRoleTasks(serviceAccount, c)
if err != nil {
return fmt.Errorf("error building service account role tasks: %w", err)
}
if len(aws.PolicyARNs) > 0 {
name := "external-" + fi.ValueOf(iamRole.Name)
externalPolicies := aws.PolicyARNs
c.AddTask(&awstasks.IAMRolePolicy{
Name: fi.PtrTo(name),
ExternalPolicies: &externalPolicies,
Managed: true,
Role: iamRole,
Lifecycle: b.Lifecycle,
})
}
}
}
return nil
}
// BuildServiceAccountRoleTasks build tasks specifically for the ServiceAccount role.
func (b *IAMModelBuilder) BuildServiceAccountRoleTasks(role iam.Subject, c *fi.CloudupModelBuilderContext) (*awstasks.IAMRole, error) {
iamName, err := b.IAMNameForServiceAccountRole(role)
if err != nil {
return nil, err
}
iamRole, err := b.buildIAMRole(role, iamName, c)
if err != nil {
return nil, err
}
if err := b.buildIAMRolePolicy(role, iamName, iamRole, c); err != nil {
return nil, err
}
return iamRole, nil
}
func (b *IAMModelBuilder) buildIAMRole(role iam.Subject, iamName string, c *fi.CloudupModelBuilderContext) (*awstasks.IAMRole, error) {
roleKey, isServiceAccount := b.roleKey(role)
rolePolicy, err := b.buildAWSIAMRolePolicy(role)
if err != nil {
return nil, err
}
iamRole := &awstasks.IAMRole{
Name: fi.PtrTo(iamName),
Lifecycle: b.Lifecycle,
RolePolicyDocument: rolePolicy,
}
if isServiceAccount {
// e.g. kube-system-dns-controller
iamRole.ExportWithID = fi.PtrTo(roleKey)
sa, ok := role.ServiceAccount()
if ok {
iamRole.Tags = b.CloudTagsForServiceAccount(iamName, sa)
}
} else {
// e.g. nodes
iamRole.ExportWithID = fi.PtrTo(roleKey + "s")
iamRole.Tags = b.CloudTags(iamName, false)
}
if b.Cluster.Spec.IAM != nil && b.Cluster.Spec.IAM.PermissionsBoundary != nil {
iamRole.PermissionsBoundary = b.Cluster.Spec.IAM.PermissionsBoundary
}
c.AddTask(iamRole)
return iamRole, nil
}
func (b *IAMModelBuilder) buildIAMRolePolicy(role iam.Subject, iamName string, iamRole *awstasks.IAMRole, c *fi.CloudupModelBuilderContext) error {
iamPolicy := &iam.PolicyResource{
Builder: &iam.PolicyBuilder{
Cluster: b.Cluster,
Role: role,
Region: b.Region,
Partition: b.AWSPartition,
UseServiceAccountExternalPermisssions: b.UseServiceAccountExternalPermissions(),
},
}
if b.Cluster.PublishesDNSRecords() {
// This is slightly tricky; we need to know the hosted zone id,
// but we might be creating the hosted zone dynamically.
// We create a stub-reference which will be combined by the execution engine.
iamPolicy.DNSZone = &awstasks.DNSZone{
Name: fi.PtrTo(b.NameForDNSZone()),
}
}
t := &awstasks.IAMRolePolicy{
Name: fi.PtrTo(iamName),
Lifecycle: b.Lifecycle,
Role: iamRole,
PolicyDocument: iamPolicy,
}
c.AddTask(t)
return nil
}
// roleKey builds a string to represent the role uniquely. It returns true if this is a service account role.
func (b *IAMModelBuilder) roleKey(role iam.Subject) (string, bool) {
serviceAccount, ok := role.ServiceAccount()
if ok {
return strings.ToLower(strings.ReplaceAll(serviceAccount.Namespace+"-"+serviceAccount.Name, "*", "wildcard")), true
}
// This isn't great, but we have to be backwards compatible with the old names.
switch role.(type) {
case *iam.NodeRoleMaster:
return "master", false
case *iam.NodeRoleAPIServer:
return strings.ToLower(string(kops.InstanceGroupRoleAPIServer)), false
case *iam.NodeRoleNode:
return strings.ToLower(string(kops.InstanceGroupRoleNode)), false
case *iam.NodeRoleBastion:
return strings.ToLower(string(kops.InstanceGroupRoleBastion)), false
default:
klog.Fatalf("unknown node role type: %T", role)
return "", false
}
}
func (b *IAMModelBuilder) buildIAMTasks(role iam.Subject, iamName string, c *fi.CloudupModelBuilderContext, shared bool) error {
roleKey, _ := b.roleKey(role)
{
// To minimize diff for easier code review
var iamInstanceProfile *awstasks.IAMInstanceProfile
{
iamInstanceProfile = &awstasks.IAMInstanceProfile{
Name: fi.PtrTo(iamName),
Lifecycle: b.Lifecycle,
Shared: fi.PtrTo(shared),
Tags: b.CloudTags(iamName, shared),
}
c.AddTask(iamInstanceProfile)
}
if !shared {
// Create External Policy tasks
iamRole, err := b.buildIAMRole(role, iamName, c)
if err != nil {
return err
}
{
if err := b.buildIAMRolePolicy(role, iamName, iamRole, c); err != nil {
return err
}
{
iamInstanceProfileRole := &awstasks.IAMInstanceProfileRole{
Name: fi.PtrTo(iamName),
Lifecycle: b.Lifecycle,
InstanceProfile: iamInstanceProfile,
Role: iamRole,
}
c.AddTask(iamInstanceProfileRole)
}
var externalPolicies []string
if b.Cluster.Spec.ExternalPolicies != nil {
key := roleKey
if key == "master" {
key = "control-plane"
}
externalPolicies = append(externalPolicies, b.Cluster.Spec.ExternalPolicies[key]...)
}
sort.Strings(externalPolicies)
name := fmt.Sprintf("%s-policyoverride", roleKey)
t := &awstasks.IAMRolePolicy{
Name: fi.PtrTo(name),
Lifecycle: b.Lifecycle,
Role: iamRole,
Managed: true,
ExternalPolicies: &externalPolicies,
}
c.AddTask(t)
}
// Generate additional policies if needed, and attach to existing role
{
additionalPolicy := ""
if b.Cluster.Spec.AdditionalPolicies != nil {
key := roleKey
if key == "master" {
key = "control-plane"
}
additionalPolicy = b.Cluster.Spec.AdditionalPolicies[key]
}
additionalPolicyName := "additional." + iamName
t := &awstasks.IAMRolePolicy{
Name: fi.PtrTo(additionalPolicyName),
Lifecycle: b.Lifecycle,
Role: iamRole,
}
if additionalPolicy != "" {
p, err := b.buildPolicy(additionalPolicy)
if err != nil {
return fmt.Errorf("additionalPolicy %q is invalid: %v", roleKey, err)
}
policy, err := p.AsJSON()
if err != nil {
return fmt.Errorf("error building IAM policy: %w", err)
}
t.PolicyDocument = fi.NewStringResource(policy)
} else {
t.PolicyDocument = fi.NewStringResource("")
}
c.AddTask(t)
}
}
}
return nil
}
func (b *IAMModelBuilder) buildPolicy(policyString string) (*iam.Policy, error) {
p := &iam.Policy{
Version: iam.PolicyDefaultVersion,
}
statements, err := iam.ParseStatements(policyString)
if err != nil {
return nil, err
}
p.Statement = append(p.Statement, statements...)
return p, nil
}
// IAMServiceEC2 returns the name of the IAM service for EC2 in the current region.
// It is ec2.amazonaws.com in the default aws partition, but different in other isolated/custom partitions
func IAMServiceEC2(region string) string {
partitions := endpoints.DefaultPartitions()
for _, p := range partitions {
if _, ok := p.Regions()[region]; ok {
ep := "ec2." + p.DNSSuffix()
return ep
}
}
return "ec2.amazonaws.com"
}
func formatAWSIAMStatement(accountId, partition, oidcProvider, namespace, name string) (*iam.Statement, error) {
// disallow wildcard in the service account name
if strings.Contains(name, "*") {
return nil, fmt.Errorf("service account name cannot contain a wildcard %s", name)
}
// if the namespace contains a wildcard, use StringLike condition instead of StringEquals
condition := "StringEquals"
if strings.Contains(namespace, "*") {
condition = "StringLike"
}
return &iam.Statement{
Effect: "Allow",
Principal: iam.Principal{
Federated: "arn:" + partition + ":iam::" + accountId + ":oidc-provider/" + oidcProvider,
},
Action: stringorslice.String("sts:AssumeRoleWithWebIdentity"),
Condition: map[string]interface{}{
condition: map[string]interface{}{
oidcProvider + ":sub": "system:serviceaccount:" + namespace + ":" + name,
},
},
},
nil
}
// buildAWSIAMRolePolicy produces the AWS IAM role policy for the given role.
func (b *IAMModelBuilder) buildAWSIAMRolePolicy(role iam.Subject) (fi.Resource, error) {
var policy string
serviceAccount, ok := role.ServiceAccount()
if ok {
oidcProvider := strings.TrimPrefix(*b.Cluster.Spec.KubeAPIServer.ServiceAccountIssuer, "https://")
statement, err := formatAWSIAMStatement(b.AWSAccountID, b.AWSPartition, oidcProvider, serviceAccount.Namespace, serviceAccount.Name)
if err != nil {
return nil, err
}
iamPolicy := &iam.Policy{
Version: iam.PolicyDefaultVersion,
Statement: []*iam.Statement{statement},
}
s, err := iamPolicy.AsJSON()
if err != nil {
return nil, err
}
policy = s
} else {
// We don't generate using json.Marshal here, it would create whitespace changes in the policy for existing clusters.
policy = strings.ReplaceAll(NodeRolePolicyTemplate, "{{ IAMServiceEC2 }}", IAMServiceEC2(b.Region))
}
return fi.NewStringResource(policy), nil
}
func (b *IAMModelBuilder) FindDeletions(context *fi.CloudupModelBuilderContext, cloud fi.Cloud) error {
iamapi := cloud.(awsup.AWSCloud).IAM()
ownershipTag := "kubernetes.io/cluster/" + b.Cluster.ObjectMeta.Name
request := &awsIam.ListRolesInput{}
var getRoleErr error
err := iamapi.ListRolesPages(request, func(p *awsIam.ListRolesOutput, lastPage bool) bool {
for _, role := range p.Roles {
if !strings.HasSuffix(fi.ValueOf(role.RoleName), "."+b.Cluster.ObjectMeta.Name) {
continue
}
getRequest := &awsIam.GetRoleInput{RoleName: role.RoleName}
roleOutput, err := iamapi.GetRole(getRequest)
if err != nil {
getRoleErr = fmt.Errorf("calling IAM GetRole on %s: %w", fi.ValueOf(role.RoleName), err)
return false
}
for _, tag := range roleOutput.Role.Tags {
if fi.ValueOf(tag.Key) == ownershipTag && fi.ValueOf(tag.Value) == "owned" {
if _, ok := context.Tasks["IAMRole/"+fi.ValueOf(role.RoleName)]; !ok {
context.AddTask(&awstasks.IAMRole{
ID: role.RoleId,
Name: role.RoleName,
Lifecycle: b.Lifecycle,
})
}
}
}
}
return true
})
if getRoleErr != nil {
return getRoleErr
}
if err != nil {
return fmt.Errorf("listing IAM roles: %w", err)
}
return nil
}