-
Notifications
You must be signed in to change notification settings - Fork 21
/
iam.go
718 lines (634 loc) · 21.6 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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
package awsapi
//go:generate mockgen -destination=mocks/mock_iamiface.go -package=mock_awsapi github.com/aws/aws-sdk-go/service/iam/iamiface IAMAPI
////go:generate mockgen -destination=mocks/mock_iam.go -package=mock_awsapi github.com/keikoproj/iam-manager/pkg/awsapi IAMIface
import (
"context"
"fmt"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/iam"
"github.com/aws/aws-sdk-go/service/iam/iamiface"
"github.com/keikoproj/iam-manager/pkg/log"
)
//IAMIface defines interface methods
type IAMIface interface {
CreateRole(ctx context.Context, req IAMRoleRequest)
UpdateRole(ctx context.Context, req IAMRoleRequest)
DeleteRole(ctx context.Context, roleName string)
AttachInlineRolePolicy(ctx context.Context, req IAMRoleRequest)
AddPermissionBoundary(ctx context.Context, req IAMRoleRequest) error
GetRolePolicy(ctx context.Context, req IAMRoleRequest) bool
}
const (
RoleAlreadyExistsError = "Please choose a different name"
)
//IAMRoleRequest struct
type IAMRoleRequest struct {
Name string
PolicyName string
Description string
SessionDuration int64
TrustPolicy string
PermissionPolicy string
ManagedPermissionBoundaryPolicy string
ManagedPolicies []string
Tags map[string]string
}
type IAMRoleResponse struct {
RoleARN string
RoleID string
}
type IAM struct {
Client iamiface.IAMAPI
}
func NewIAM(region string) *IAM {
sess, err := session.NewSession(&aws.Config{Region: aws.String(region)})
if err != nil {
panic(err)
}
return &IAM{
Client: iam.New(sess),
}
}
// CreateRole creates/updates the role
func (i *IAM) CreateRole(ctx context.Context, req IAMRoleRequest) (*IAMRoleResponse, error) {
log := log.Logger(ctx, "awsapi", "iam", "CreateRole")
log = log.WithValues("roleName", req.Name)
log.V(1).Info("Initiating api call")
input := &iam.CreateRoleInput{
AssumeRolePolicyDocument: aws.String(req.TrustPolicy),
RoleName: aws.String(req.Name),
Description: aws.String(req.Description),
MaxSessionDuration: aws.Int64(req.SessionDuration),
PermissionsBoundary: aws.String(req.ManagedPermissionBoundaryPolicy),
}
if err := input.Validate(); err != nil {
log.Error(err, "input validation failed")
return nil, err
}
roleAlreadyExists := false
iResp, err := i.Client.CreateRole(input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
// Update the role to the latest spec if it is existed already
case iam.ErrCodeEntityAlreadyExistsException:
roleAlreadyExists = true
log.Info(iam.ErrCodeEntityAlreadyExistsException)
case iam.ErrCodeLimitExceededException:
log.Error(err, iam.ErrCodeLimitExceededException)
case iam.ErrCodeNoSuchEntityException:
log.Error(err, iam.ErrCodeNoSuchEntityException)
case iam.ErrCodeServiceFailureException:
log.Error(err, iam.ErrCodeServiceFailureException)
default:
log.Error(err, aerr.Error())
}
}
if !roleAlreadyExists {
return nil, err
}
}
resp := &IAMRoleResponse{}
if !roleAlreadyExists {
resp.RoleARN = aws.StringValue(iResp.Role.Arn)
resp.RoleID = aws.StringValue(iResp.Role.RoleId)
}
//Verify tags
log.V(1).Info("Verifying Tags")
_, err = i.VerifyTags(ctx, req)
if err != nil {
return &IAMRoleResponse{}, err
}
//Attach a tag
log.V(1).Info("Attaching Tag")
_, err = i.TagRole(ctx, req)
if err != nil {
return &IAMRoleResponse{}, err
}
//Add permission boundary
log.V(1).Info("Attaching Permission Boundary")
err = i.AddPermissionBoundary(ctx, req)
if err != nil {
return &IAMRoleResponse{}, err
}
//Attach managed role policy
log.V(1).Info("Attaching Managed policies")
for _, policy := range req.ManagedPolicies {
if policy != "" {
err = i.AttachManagedRolePolicy(ctx, policy, req.Name)
if err != nil {
log.Error(err, "Error while attaching managed policy", "policy", policy)
return &IAMRoleResponse{}, err
}
}
}
log.V(1).Info("Attaching Inline role policies")
_, err = i.UpdateRole(ctx, req)
if err != nil {
log.Error(err, "Error while updating role")
return &IAMRoleResponse{}, err
}
return resp, nil
}
//VerifyTags function verifies the tags attached to the role
func (i *IAM) VerifyTags(ctx context.Context, req IAMRoleRequest) (*IAMRoleResponse, error) {
log := log.Logger(ctx, "awsapi", "iam", "VerifyTags")
log = log.WithValues("roleName", req.Name)
log.V(1).Info("Initiating api call")
//Lets first list the tags and look for namespace and cluster tags
listTags, err := i.Client.ListRoleTags(&iam.ListRoleTagsInput{
RoleName: aws.String(req.Name),
})
if err != nil {
return nil, err
}
flag := false
for _, tag := range listTags.Tags {
if aws.StringValue(tag.Key) == "Namespace" {
if aws.StringValue(tag.Value) != req.Tags["Namespace"] {
flag = true
break
}
}
if aws.StringValue(tag.Key) == "Cluster" {
if aws.StringValue(tag.Value) != req.Tags["Cluster"] {
flag = true
break
}
}
}
if flag {
return nil, fmt.Errorf("role name %s in AWS is not available. %s", req.Name, RoleAlreadyExistsError)
}
return &IAMRoleResponse{}, nil
}
//TagRole tags role with appropriate tags
func (i *IAM) TagRole(ctx context.Context, req IAMRoleRequest) (*IAMRoleResponse, error) {
log := log.Logger(ctx, "awsapi", "iam", "TagRole")
log = log.WithValues("roleName", req.Name)
log.V(1).Info("Initiating api call")
//Attach the tags
var tags []*iam.Tag
for k, v := range req.Tags {
tag := &iam.Tag{
Key: aws.String(k),
Value: aws.String(v),
}
tags = append(tags, tag)
}
input := &iam.TagRoleInput{
RoleName: aws.String(req.Name),
Tags: tags,
}
_, err := i.Client.TagRole(input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case iam.ErrCodeNoSuchEntityException:
log.Error(err, iam.ErrCodeNoSuchEntityException)
case iam.ErrCodeLimitExceededException:
log.Error(err, iam.ErrCodeLimitExceededException)
case iam.ErrCodeInvalidInputException:
log.Error(err, iam.ErrCodeInvalidInputException)
case iam.ErrCodeServiceFailureException:
log.Error(err, iam.ErrCodeServiceFailureException)
default:
log.Error(err, aerr.Error())
}
} else {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
log.Error(err, err.Error())
}
return &IAMRoleResponse{}, err
}
log.V(1).Info("Successfully completed TagRole call")
return &IAMRoleResponse{}, nil
}
//AddPermissionBoundary adds permission boundary to the existing roles
func (i *IAM) AddPermissionBoundary(ctx context.Context, req IAMRoleRequest) error {
log := log.Logger(ctx, "awsapi", "iam", "AddPermissionBoundary")
log = log.WithValues("roleName", req.Name)
log.V(1).Info("Initiating api call")
input := &iam.PutRolePermissionsBoundaryInput{
RoleName: aws.String(req.Name),
PermissionsBoundary: aws.String(req.ManagedPermissionBoundaryPolicy),
}
if err := input.Validate(); err != nil {
log.Error(err, "input validation failed")
return err
}
_, err := i.Client.PutRolePermissionsBoundary(input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case iam.ErrCodeNoSuchEntityException:
log.Error(err, iam.ErrCodeNoSuchEntityException)
case iam.ErrCodeLimitExceededException:
log.Error(err, iam.ErrCodeLimitExceededException)
case iam.ErrCodeInvalidInputException:
log.Error(err, iam.ErrCodeInvalidInputException)
case iam.ErrCodeUnmodifiableEntityException:
log.Error(err, iam.ErrCodeUnmodifiableEntityException)
case iam.ErrCodePolicyNotAttachableException:
log.Error(err, iam.ErrCodePolicyNotAttachableException)
case iam.ErrCodeServiceFailureException:
log.Error(err, iam.ErrCodeServiceFailureException)
default:
log.Error(err, aerr.Error())
}
} else {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
log.Error(err, err.Error())
}
return err
}
log.V(1).Info("Successfuly added permission boundary")
return nil
}
//UpdateRole updates role
func (i *IAM) UpdateRole(ctx context.Context, req IAMRoleRequest) (*IAMRoleResponse, error) {
log := log.Logger(ctx, "awsapi", "iam", "UpdateRole")
log = log.WithValues("roleName", req.Name)
log.V(1).Info("Initiating api call")
input := &iam.UpdateRoleInput{
RoleName: aws.String(req.Name),
MaxSessionDuration: aws.Int64(req.SessionDuration),
Description: aws.String(req.Description),
}
if err := input.Validate(); err != nil {
log.Error(err, "input validation failed")
return nil, err
}
_, err := i.Client.UpdateRole(input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case iam.ErrCodeLimitExceededException:
log.Error(err, iam.ErrCodeLimitExceededException)
case iam.ErrCodeNoSuchEntityException:
log.Error(err, iam.ErrCodeNoSuchEntityException)
case iam.ErrCodeServiceFailureException:
log.Error(err, iam.ErrCodeServiceFailureException)
default:
log.Error(err, "error in update roles")
}
} else {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
log.Error(err, "error in update role")
//If access denied, one use case would be it is an existing role and we need to first attach permission boundary
}
return nil, err
}
//If it is already here means update role is successful, lets move on to Update Assume role policy
//Lets double check this -- do we want to do this for every update?
log.V(1).Info("Initiating api call", "api", "UpdateAssumeRolePolicy")
inputPolicy := &iam.UpdateAssumeRolePolicyInput{
RoleName: aws.String(req.Name),
PolicyDocument: aws.String(req.TrustPolicy),
}
if err := inputPolicy.Validate(); err != nil {
log.Error(err, "input validation failed")
return nil, err
}
_, err = i.Client.UpdateAssumeRolePolicy(inputPolicy)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case iam.ErrCodeLimitExceededException:
log.Error(err, iam.ErrCodeLimitExceededException)
case iam.ErrCodeNoSuchEntityException:
log.Error(err, iam.ErrCodeNoSuchEntityException)
case iam.ErrCodeServiceFailureException:
log.Error(err, iam.ErrCodeServiceFailureException)
default:
log.Error(err, aerr.Error())
}
} else {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
log.Error(err, err.Error())
}
return nil, err
}
//Attach the Inline policy
log.V(1).Info("AssumeRole Policy is successfully updated")
return i.AttachInlineRolePolicy(ctx, req)
}
//AttachInlineRolePolicy function attaches inline policy to the role
func (i *IAM) AttachInlineRolePolicy(ctx context.Context, req IAMRoleRequest) (*IAMRoleResponse, error) {
log := log.Logger(ctx, "awsapi", "iam", "AttachInlineRolePolicy")
log = log.WithValues("roleName", req.Name)
log.V(1).Info("Initiating api call")
input := &iam.PutRolePolicyInput{
RoleName: aws.String(req.Name),
PolicyName: aws.String(req.PolicyName),
PolicyDocument: aws.String(req.PermissionPolicy),
}
if err := input.Validate(); err != nil {
log.Error(err, "input validation failed")
return nil, err
}
_, err := i.Client.PutRolePolicy(input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case iam.ErrCodeMalformedPolicyDocumentException:
log.Error(err, iam.ErrCodeMalformedPolicyDocumentException)
case iam.ErrCodeUnmodifiableEntityException:
log.Error(err, iam.ErrCodeUnmodifiableEntityException)
case iam.ErrCodeLimitExceededException:
log.Error(err, iam.ErrCodeLimitExceededException)
case iam.ErrCodeNoSuchEntityException:
log.Error(err, iam.ErrCodeNoSuchEntityException)
case iam.ErrCodeServiceFailureException:
log.Error(err, iam.ErrCodeServiceFailureException)
default:
log.Error(err, aerr.Error())
}
}
return nil, err
}
log.V(1).Info("Successfully completed attaching InlineRolePolicy")
return &IAMRoleResponse{}, nil
}
//GetRole gets the role from aws iam
func (i *IAM) GetRole(ctx context.Context, req IAMRoleRequest) (*iam.GetRoleOutput, error) {
log := log.Logger(ctx, "awsapi", "iam", "GetRole")
log = log.WithValues("roleName", req.Name)
log.V(1).Info("Initiating api call")
// First get the iam role policy on the AWS IAM side
input := &iam.GetRoleInput{
RoleName: aws.String(req.Name),
}
if err := input.Validate(); err != nil {
log.Error(err, "input validation failed")
//should log the error
return nil, err
}
resp, err := i.Client.GetRole(input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case iam.ErrCodeNoSuchEntityException:
log.Error(err, iam.ErrCodeNoSuchEntityException)
case iam.ErrCodeServiceFailureException:
log.Error(err, iam.ErrCodeServiceFailureException)
default:
log.Error(err, aerr.Error())
}
}
return nil, err
}
log.V(1).Info("Successfully able to get the role")
return resp, nil
}
//GetRolePolicy gets the role from aws iam
func (i *IAM) GetRolePolicy(ctx context.Context, req IAMRoleRequest) (*string, error) {
log := log.Logger(ctx, "awsapi", "iam", "GetRolePolicy")
log = log.WithValues("roleName", req.Name)
log.V(1).Info("Initiating api call")
// First get the iam role policy on the AWS IAM side
input := &iam.GetRolePolicyInput{
PolicyName: aws.String(req.PolicyName),
RoleName: aws.String(req.Name),
}
if err := input.Validate(); err != nil {
log.Error(err, "input validation failed")
//should log the error
return nil, err
}
resp, err := i.Client.GetRolePolicy(input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case iam.ErrCodeNoSuchEntityException:
log.Error(err, iam.ErrCodeNoSuchEntityException)
case iam.ErrCodeServiceFailureException:
log.Error(err, iam.ErrCodeServiceFailureException)
default:
log.Error(err, aerr.Error())
}
}
return nil, err
}
log.V(1).Info("Successfully able to get the policy")
return resp.PolicyDocument, nil
}
// AttachManagedRolePolicy function attaches managed policy to the role
func (i *IAM) AttachManagedRolePolicy(ctx context.Context, policyArn string, roleName string) error {
log := log.Logger(ctx, "awsapi", "iam", "AttachManagedRolePolicy")
log = log.WithValues("roleName", roleName, "policyName", policyArn)
log.V(1).Info("Initiating api call")
_, err := i.Client.AttachRolePolicy(&iam.AttachRolePolicyInput{
RoleName: aws.String(roleName),
PolicyArn: aws.String(policyArn),
})
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case iam.ErrCodeNoSuchEntityException:
log.Error(err, iam.ErrCodeNoSuchEntityException)
case iam.ErrCodeLimitExceededException:
log.Error(err, iam.ErrCodeLimitExceededException)
case iam.ErrCodeInvalidInputException:
log.Error(err, iam.ErrCodeInvalidInputException)
case iam.ErrCodeUnmodifiableEntityException:
log.Error(err, iam.ErrCodeUnmodifiableEntityException)
case iam.ErrCodePolicyNotAttachableException:
log.Error(err, iam.ErrCodePolicyNotAttachableException)
case iam.ErrCodeServiceFailureException:
log.Error(err, iam.ErrCodeServiceFailureException)
default:
log.Error(err, aerr.Error())
}
}
return err
}
log.V(1).Info("Successfully attached managed policies")
return nil
}
//DeleteRole function deletes the role in the account
func (i *IAM) DeleteRole(ctx context.Context, roleName string) error {
log := log.Logger(ctx, "awsapi", "iam", "DeleteRole")
log = log.WithValues("roleName", roleName)
log.V(1).Info("Initiating api call")
//Check if role exists
managedPolicyList, err := i.Client.ListAttachedRolePolicies(&iam.ListAttachedRolePoliciesInput{
RoleName: aws.String(roleName),
})
if err != nil {
if strings.Contains(err.Error(), "NoSuchEntity") {
log.Info("Role doesn't exist in the target account", "role_name", roleName)
return nil
} else {
log.Error(err, "Unable to list attached managed policies for role")
return err
}
}
log.V(1).Info("listing attached policies", "policyList", managedPolicyList.AttachedPolicies)
// Detach managed policies
for _, policy := range managedPolicyList.AttachedPolicies {
if err := i.DetachRolePolicy(ctx, aws.StringValue(policy.PolicyArn), roleName); err != nil {
log.Error(err, "Unable to delete the policy", "policyName", aws.StringValue(policy.PolicyName))
return err
}
}
inlinePolicies, err := i.Client.ListRolePolicies(&iam.ListRolePoliciesInput{
RoleName: aws.String(roleName),
})
if err != nil {
log.Error(err, "Unable to list inline policies for role")
return err
}
// Delete inline policies
for _, inlinePolicy := range inlinePolicies.PolicyNames {
if err := i.DeleteInlinePolicy(ctx, aws.StringValue(inlinePolicy), roleName); err != nil {
log.Error(err, "Unable to delete the policy", "policyName", aws.StringValue(inlinePolicy))
return err
}
}
input := &iam.DeleteRoleInput{
RoleName: aws.String(roleName),
}
_, err = i.Client.DeleteRole(input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case iam.ErrCodeLimitExceededException:
log.Error(err, iam.ErrCodeLimitExceededException)
case iam.ErrCodeNoSuchEntityException:
//This is ok
err = nil
log.V(1).Info(iam.ErrCodeNoSuchEntityException)
case iam.ErrCodeServiceFailureException:
log.Error(err, iam.ErrCodeServiceFailureException)
default:
log.Error(err, aerr.Error())
}
}
return err
}
log.V(1).Info("Successfully deleted the role")
return nil
}
//DeleteInlinePolicy function deletes inline policy
func (i *IAM) DeleteInlinePolicy(ctx context.Context, policyName string, roleName string) error {
log := log.Logger(ctx, "awsapi", "iam", "DeleteInlinePolicy")
log = log.WithValues("roleName", roleName, "policyName", policyName)
log.V(1).Info("Initiating api call")
input := &iam.DeleteRolePolicyInput{
PolicyName: aws.String(policyName),
RoleName: aws.String(roleName),
}
_, err := i.Client.DeleteRolePolicy(input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case iam.ErrCodeNoSuchEntityException:
log.V(1).Info(iam.ErrCodeNoSuchEntityException)
// This is ok
return nil
case iam.ErrCodeLimitExceededException:
log.Error(err, iam.ErrCodeLimitExceededException)
case iam.ErrCodeUnmodifiableEntityException:
log.Error(err, iam.ErrCodeUnmodifiableEntityException)
case iam.ErrCodeServiceFailureException:
log.Error(err, iam.ErrCodeServiceFailureException)
default:
log.Error(err, aerr.Error())
}
} else {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
log.Error(err, err.Error())
}
return err
}
log.V(1).Info("Successfully deleted inline policy")
return nil
}
// DetachRolePolicy detaches a policy from role
func (i *IAM) DetachRolePolicy(ctx context.Context, policyArn string, roleName string) error {
log := log.Logger(ctx, "awsapi", "iam", "DetachRolePolicy")
log = log.WithValues("roleName", roleName, "policyArn", policyArn)
log.V(1).Info("Initiating api call")
_, err := i.Client.DetachRolePolicy(&iam.DetachRolePolicyInput{
PolicyArn: aws.String(policyArn),
RoleName: aws.String(roleName),
})
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case iam.ErrCodeNoSuchEntityException:
log.V(1).Info(iam.ErrCodeNoSuchEntityException)
// This is ok
return nil
case iam.ErrCodeLimitExceededException:
log.Error(err, iam.ErrCodeLimitExceededException)
case iam.ErrCodeUnmodifiableEntityException:
log.Error(err, iam.ErrCodeUnmodifiableEntityException)
case iam.ErrCodeServiceFailureException:
log.Error(err, iam.ErrCodeServiceFailureException)
default:
log.Error(err, aerr.Error())
}
} else {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
log.Error(err, err.Error())
}
return err
}
log.V(1).Info("Successfully detached policy")
return nil
}
//CreateOIDCProvider creates OIDC IDP provider with AWS IAM
func (i *IAM) CreateOIDCProvider(ctx context.Context, url string, aud string, certThumpPrint string) error {
log := log.Logger(ctx, "awsapi.iam", "CreateOIDCProvider")
log = log.WithValues("url", url, "aud", aud)
log.V(1).Info("Creating OIDC Provider with AWS IAM")
input := &iam.CreateOpenIDConnectProviderInput{
ClientIDList: []*string{aws.String(aud)},
ThumbprintList: []*string{aws.String(certThumpPrint)},
Url: aws.String(url),
}
result, err := i.Client.CreateOpenIDConnectProvider(input)
idpAlreadyExists := false
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case iam.ErrCodeInvalidInputException:
log.Error(err, iam.ErrCodeInvalidInputException)
case iam.ErrCodeEntityAlreadyExistsException:
log.Info("OIDC Provider already exists with this url. This should be okay")
idpAlreadyExists = true
case iam.ErrCodeLimitExceededException:
log.Error(err, iam.ErrCodeLimitExceededException)
case iam.ErrCodeServiceFailureException:
log.Error(err, iam.ErrCodeServiceFailureException)
default:
log.Error(err, aerr.Error())
}
} else {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
log.Error(err, err.Error())
}
if !idpAlreadyExists {
return err
}
}
if idpAlreadyExists {
log.Info("OIDC Provider already exists. skipping")
return nil
}
//Print the ARN very first time
log.Info("OIDC Provider created successfully", "Arn", aws.StringValue(result.OpenIDConnectProviderArn))
return nil
}