/
aws.go
840 lines (758 loc) · 26.5 KB
/
aws.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
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
/*
Copyright 2019 The Crossplane 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 aws
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/url"
"strings"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials"
stscredsv2 "github.com/aws/aws-sdk-go-v2/credentials/stscreds"
ec2type "github.com/aws/aws-sdk-go-v2/service/ec2/types"
"github.com/aws/aws-sdk-go-v2/service/sts"
awsv1 "github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
credentialsv1 "github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/credentials/stscreds"
endpointsv1 "github.com/aws/aws-sdk-go/aws/endpoints"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/smithy-go"
jsonpatch "github.com/evanphx/json-patch"
"github.com/go-ini/ini"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1"
"github.com/crossplane/crossplane-runtime/pkg/resource"
"github.com/fhopfensperger/provider-basic-aws/apis/v1beta1"
)
// DefaultSection for INI files.
const DefaultSection = ini.DefaultSection
// GlobalRegion is the region name used for AWS services that do not have a notion
// of region.
const GlobalRegion = "aws-global"
// Endpoint URL configuration types.
const (
URLConfigTypeStatic = "Static"
URLConfigTypeDynamic = "Dynamic"
)
// A FieldOption determines how common Go types are translated to the types
// required by the AWS Go SDK.
type FieldOption int
// Field options.
const (
// FieldRequired causes zero values to be converted to a pointer to the zero
// value, rather than a nil pointer. AWS Go SDK types use pointer fields,
// with a nil pointer indicating an unset field. Our ToPtr functions return
// a nil pointer for a zero values, unless FieldRequired is set.
FieldRequired FieldOption = iota
)
// GetConfig constructs an *aws.Config that can be used to authenticate to AWS
// API by the AWS clients.
func GetConfig(ctx context.Context, c client.Client, mg resource.Managed, region string) (*aws.Config, error) {
switch {
case mg.GetProviderConfigReference() != nil:
return UseProviderConfig(ctx, c, mg, region)
default:
return nil, errors.New("neither providerConfigRef nor providerRef is given")
}
}
// UseProviderConfig to produce a config that can be used to authenticate to AWS.
func UseProviderConfig(ctx context.Context, c client.Client, mg resource.Managed, region string) (*aws.Config, error) {
pc := &v1beta1.ProviderConfig{}
if err := c.Get(ctx, types.NamespacedName{Name: mg.GetProviderConfigReference().Name}, pc); err != nil {
return nil, errors.Wrap(err, "cannot get referenced Provider")
}
t := resource.NewProviderConfigUsageTracker(c, &v1beta1.ProviderConfigUsage{})
if err := t.Track(ctx, mg); err != nil {
return nil, errors.Wrap(err, "cannot track ProviderConfig usage")
}
switch s := pc.Spec.Credentials.Source; s { //nolint:exhaustive
case xpv1.CredentialsSourceInjectedIdentity:
cfg, err := UsePodServiceAccount(ctx, []byte{}, DefaultSection, region)
if err != nil {
return nil, err
}
return SetResolver(pc, cfg), nil
default:
data, err := resource.CommonCredentialExtractor(ctx, s, c, pc.Spec.Credentials.CommonCredentialSelectors)
if err != nil {
return nil, errors.Wrap(err, "cannot get credentials")
}
cfg, err := UseProviderSecret(ctx, data, DefaultSection, region)
if err != nil {
return nil, err
}
return SetResolver(pc, cfg), nil
}
}
// SetResolver parses annotations from the managed resource
// and returns a configuration accordingly.
func SetResolver(pc *v1beta1.ProviderConfig, cfg *aws.Config) *aws.Config { // nolint:gocyclo
if pc.Spec.Endpoint == nil {
return cfg
}
cfg.EndpointResolver = aws.EndpointResolverFunc(func(service, region string) (aws.Endpoint, error) {
fullURL := ""
switch pc.Spec.Endpoint.URL.Type {
case URLConfigTypeStatic:
if pc.Spec.Endpoint.URL.Static == nil {
return aws.Endpoint{}, errors.New("static type is chosen but static field does not have a value")
}
fullURL = StringValue(pc.Spec.Endpoint.URL.Static)
case URLConfigTypeDynamic:
if pc.Spec.Endpoint.URL.Dynamic == nil {
return aws.Endpoint{}, errors.New("dynamic type is chosen but dynamic configuration is not given")
}
// NOTE(muvaf): IAM does not have any region.
if service == "IAM" {
fullURL = fmt.Sprintf("%s://%s.%s", pc.Spec.Endpoint.URL.Dynamic.Protocol, strings.ToLower(service), pc.Spec.Endpoint.URL.Dynamic.Host)
} else {
fullURL = fmt.Sprintf("%s://%s.%s.%s", pc.Spec.Endpoint.URL.Dynamic.Protocol, strings.ToLower(service), region, pc.Spec.Endpoint.URL.Dynamic.Host)
}
default:
return aws.Endpoint{}, errors.New("unsupported url config type is chosen")
}
e := aws.Endpoint{
URL: fullURL,
HostnameImmutable: BoolValue(pc.Spec.Endpoint.HostnameImmutable),
PartitionID: StringValue(pc.Spec.Endpoint.PartitionID),
SigningName: StringValue(pc.Spec.Endpoint.SigningName),
SigningRegion: StringValue(LateInitializeStringPtr(pc.Spec.Endpoint.SigningRegion, ®ion)),
SigningMethod: StringValue(pc.Spec.Endpoint.SigningMethod),
}
// Only IAM does not have a region parameter and "aws-global" is used in
// SDK setup. However, signing region has to be us-east-1 and it needs
// to be set.
if region == "aws-global" {
switch StringValue(pc.Spec.Endpoint.PartitionID) {
case "aws-us-gov", "aws-cn":
e.SigningRegion = StringValue(LateInitializeStringPtr(pc.Spec.Endpoint.SigningRegion, ®ion))
default:
e.SigningRegion = "us-east-1"
}
}
if pc.Spec.Endpoint.Source != nil {
switch *pc.Spec.Endpoint.Source {
case "ServiceMetadata":
e.Source = aws.EndpointSourceServiceMetadata
case "Custom":
e.Source = aws.EndpointSourceCustom
}
}
return e, nil
})
return cfg
}
// CredentialsIDSecret retrieves AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY from the data which contains
// aws credentials under given profile
// Example:
// [default]
// aws_access_key_id = <YOUR_ACCESS_KEY_ID>
// aws_secret_access_key = <YOUR_SECRET_ACCESS_KEY>
func CredentialsIDSecret(data []byte, profile string) (aws.Credentials, error) {
config, err := ini.InsensitiveLoad(data)
if err != nil {
return aws.Credentials{}, errors.Wrap(err, "cannot parse credentials secret")
}
iniProfile, err := config.GetSection(profile)
if err != nil {
return aws.Credentials{}, errors.Wrap(err, fmt.Sprintf("cannot get %s profile in credentials secret", profile))
}
accessKeyID := iniProfile.Key("aws_access_key_id")
secretAccessKey := iniProfile.Key("aws_secret_access_key")
sessionToken := iniProfile.Key("aws_session_token")
// NOTE(muvaf): Key function implementation never returns nil but still its
// type is pointer so we check to make sure its next versions doesn't break
// that implicit contract.
if accessKeyID == nil || secretAccessKey == nil || sessionToken == nil {
return aws.Credentials{}, errors.New("returned key can be empty but cannot be nil")
}
return aws.Credentials{
AccessKeyID: accessKeyID.Value(),
SecretAccessKey: secretAccessKey.Value(),
SessionToken: sessionToken.Value(),
}, nil
}
// AuthMethod is a method of authenticating to the AWS API
type AuthMethod func(context.Context, []byte, string, string) (*aws.Config, error)
// UseProviderSecret - AWS configuration which can be used to issue requests against AWS API
func UseProviderSecret(ctx context.Context, data []byte, profile, region string) (*aws.Config, error) {
creds, err := CredentialsIDSecret(data, profile)
if err != nil {
return nil, errors.Wrap(err, "cannot parse credentials secret")
}
conf, err := config.LoadDefaultConfig(ctx, config.WithRegion(region), config.WithCredentialsProvider(credentials.StaticCredentialsProvider{
Value: creds,
}))
if err != nil {
return nil, errors.Wrap(err, "could not load default aws conf")
}
//FIXME: Reloading ini data is not performant
cfg, err := ini.InsensitiveLoad(data)
if err != nil {
return nil, errors.Wrap(err, "cannot parse credentials secret")
}
// An account profile exists, which can be assumed
for _, section := range cfg.Sections() {
if strings.EqualFold(section.Name(), DefaultSection) {
continue
}
log.Info("using stscreds ", cfg.Sections())
iniAccountProfile, err := cfg.GetSection(section.Name())
if err != nil {
return nil, errors.Wrap(err, "cannot parse ini section")
}
accountArn := iniAccountProfile.Key("role_arn")
stsSvc := sts.NewFromConfig(conf)
stsCreds := stscredsv2.NewAssumeRoleProvider(stsSvc, accountArn.Value())
conf.Credentials = stsCreds
break
}
return &conf, err
}
// UsePodServiceAccount assumes an IAM role configured via a ServiceAccount.
// https://docs.aws.amazon.com/eks/latest/userguide/iam-roles-for-service-accounts.html
func UsePodServiceAccount(ctx context.Context, _ []byte, _, region string) (*aws.Config, error) {
cfg, err := config.LoadDefaultConfig(
ctx,
config.WithRegion(region),
)
if err != nil {
return nil, errors.Wrap(err, "failed to load default AWS config")
}
return &cfg, err
}
// NOTE(muvaf): ACK-generated controllers use aws/aws-sdk-go instead of
// aws/aws-sdk-go-v2. These functions are implemented to be used by those controllers.
// GetConfigV1 constructs an *awsv1.Config that can be used to authenticate to AWS
// API by the AWSv1 clients.
func GetConfigV1(ctx context.Context, c client.Client, mg resource.Managed, region string) (*session.Session, error) { // nolint:gocyclo
if mg.GetProviderConfigReference() == nil {
return nil, errors.New("providerConfigRef cannot be empty")
}
pc := &v1beta1.ProviderConfig{}
if err := c.Get(ctx, types.NamespacedName{Name: mg.GetProviderConfigReference().Name}, pc); err != nil {
return nil, errors.Wrap(err, "cannot get referenced ProviderConfig")
}
t := resource.NewProviderConfigUsageTracker(c, &v1beta1.ProviderConfigUsage{})
if err := t.Track(ctx, mg); err != nil {
return nil, errors.Wrap(err, "cannot track ProviderConfig usage")
}
switch s := pc.Spec.Credentials.Source; s { //nolint:exhaustive
case xpv1.CredentialsSourceInjectedIdentity:
cfg, err := UsePodServiceAccountV1(ctx, []byte{}, pc, DefaultSection, region)
if err != nil {
return nil, errors.Wrap(err, "cannot use pod service account")
}
return session.NewSession(cfg)
default:
data, err := resource.CommonCredentialExtractor(ctx, s, c, pc.Spec.Credentials.CommonCredentialSelectors)
if err != nil {
return nil, errors.Wrap(err, "cannot get credentials")
}
cfg, err := UseProviderSecretV1(ctx, data, pc, DefaultSection, region)
if err != nil {
return nil, errors.Wrap(err, "cannot use secret")
}
return session.NewSession(cfg)
}
}
// UseProviderSecretV1 retrieves AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY from
// the data which contains aws credentials under given profile and produces a *awsv1.Config
// Example:
// [default]
// aws_access_key_id = <YOUR_ACCESS_KEY_ID>
// aws_secret_access_key = <YOUR_SECRET_ACCESS_KEY>
func UseProviderSecretV1(_ context.Context, data []byte, pc *v1beta1.ProviderConfig, profile, region string) (*awsv1.Config, error) {
cfg, err := ini.InsensitiveLoad(data)
if err != nil {
return nil, errors.Wrap(err, "cannot parse credentials secret")
}
iniProfile, err := cfg.GetSection(profile)
if err != nil {
return nil, errors.Wrap(err, fmt.Sprintf("cannot get %s profile in credentials secret", profile))
}
accessKeyID := iniProfile.Key("aws_access_key_id")
secretAccessKey := iniProfile.Key("aws_secret_access_key")
sessionToken := iniProfile.Key("aws_session_token")
// NOTE(muvaf): Key function implementation never returns nil but still its
// type is pointer so we check to make sure its next versions doesn't break
// that implicit contract.
if accessKeyID == nil || secretAccessKey == nil || sessionToken == nil {
return nil, errors.New("returned key can be empty but cannot be nil")
}
creds := credentialsv1.NewStaticCredentials(accessKeyID.Value(), secretAccessKey.Value(), sessionToken.Value())
// An account profile exists, which can be assumed
for _, section := range cfg.Sections() {
if strings.EqualFold(section.Name(), DefaultSection) {
continue
}
log.Info("using stscreds ", cfg.Sections())
iniAccountProfile, err := cfg.GetSection(section.Name())
if err != nil {
return nil, errors.Wrap(err, "cannot parse ini section")
}
accountArn := iniAccountProfile.Key("role_arn")
sess := session.Must(session.NewSession(awsv1.NewConfig().WithCredentials(creds).WithRegion(region)))
awsConfig := awsv1.NewConfig().WithCredentials(creds).WithRegion(region).WithCredentials(stscreds.NewCredentials(sess, accountArn.Value()))
return SetResolverV1(pc, awsConfig), nil
}
return SetResolverV1(pc, awsv1.NewConfig().WithCredentials(creds).WithRegion(region)), nil
}
// UsePodServiceAccountV1 assumes an IAM role configured via a ServiceAccount.
// https://docs.aws.amazon.com/eks/latest/userguide/iam-roles-for-service-accounts.html
func UsePodServiceAccountV1(ctx context.Context, _ []byte, pc *v1beta1.ProviderConfig, _, region string) (*awsv1.Config, error) {
cfg, err := config.LoadDefaultConfig(
ctx,
config.WithRegion(region),
)
if err != nil {
return nil, errors.Wrap(err, "failed to load default AWS config")
}
v2creds, err := cfg.Credentials.Retrieve(ctx)
if err != nil {
return nil, errors.Wrap(err, "failed to retrieve credentials")
}
v1creds := credentialsv1.NewStaticCredentials(
v2creds.AccessKeyID,
v2creds.SecretAccessKey,
v2creds.SessionToken)
return SetResolverV1(pc, awsv1.NewConfig().WithCredentials(v1creds).WithRegion(region)), nil
}
// SetResolverV1 parses annotations from the managed resource
// and returns a V1 configuration accordingly.
func SetResolverV1(pc *v1beta1.ProviderConfig, cfg *awsv1.Config) *awsv1.Config { // nolint:gocyclo
if pc.Spec.Endpoint == nil {
return cfg
}
cfg.EndpointResolver = endpointsv1.ResolverFunc(func(service, region string, optFns ...func(*endpointsv1.Options)) (endpointsv1.ResolvedEndpoint, error) {
fullURL := ""
switch pc.Spec.Endpoint.URL.Type {
case URLConfigTypeStatic:
if pc.Spec.Endpoint.URL.Static == nil {
return endpointsv1.ResolvedEndpoint{}, errors.New("static type is chosen but static field does not have a value")
}
fullURL = StringValue(pc.Spec.Endpoint.URL.Static)
case URLConfigTypeDynamic:
if pc.Spec.Endpoint.URL.Dynamic == nil {
return endpointsv1.ResolvedEndpoint{}, errors.New("dynamic type is chosen but dynamic configuration is not given")
}
// NOTE(muvaf): IAM does not have any region.
if service == "IAM" {
fullURL = fmt.Sprintf("%s://%s.%s", pc.Spec.Endpoint.URL.Dynamic.Protocol, strings.ToLower(service), pc.Spec.Endpoint.URL.Dynamic.Host)
} else {
fullURL = fmt.Sprintf("%s://%s.%s.%s", pc.Spec.Endpoint.URL.Dynamic.Protocol, strings.ToLower(service), region, pc.Spec.Endpoint.URL.Dynamic.Host)
}
default:
return endpointsv1.ResolvedEndpoint{}, errors.New("unsupported url config type is chosen")
}
e := endpointsv1.ResolvedEndpoint{
URL: fullURL,
PartitionID: StringValue(pc.Spec.Endpoint.PartitionID),
SigningName: StringValue(pc.Spec.Endpoint.SigningName),
SigningRegion: StringValue(LateInitializeStringPtr(pc.Spec.Endpoint.SigningRegion, ®ion)),
SigningMethod: StringValue(pc.Spec.Endpoint.SigningMethod),
}
// Only IAM does not have a region parameter and "aws-global" is used in
// SDK setup. However, signing region has to be us-east-1 and it needs
// to be set.
if region == "aws-global" {
switch StringValue(pc.Spec.Endpoint.PartitionID) {
case "aws-us-gov", "aws-cn":
e.SigningRegion = StringValue(LateInitializeStringPtr(pc.Spec.Endpoint.SigningRegion, ®ion))
default:
e.SigningRegion = "us-east-1"
}
}
return e, nil
})
return cfg
}
// TODO(muvaf): All the types that use CreateJSONPatch are known during
// development time. In order to avoid unnecessary panic checks, we can generate
// the code that creates a patch between two objects that share the same type.
// CreateJSONPatch creates a diff JSON object that can be applied to any other
// JSON object.
func CreateJSONPatch(source, destination interface{}) ([]byte, error) {
sourceJSON, err := json.Marshal(source)
if err != nil {
return nil, err
}
destinationJSON, err := json.Marshal(destination)
if err != nil {
return nil, err
}
patchJSON, err := jsonpatch.CreateMergePatch(sourceJSON, destinationJSON)
if err != nil {
return nil, err
}
return patchJSON, nil
}
// String converts the supplied string for use with the AWS Go SDK.
func String(v string, o ...FieldOption) *string {
for _, fo := range o {
if fo == FieldRequired && v == "" {
return aws.String(v)
}
}
if v == "" {
return nil
}
return aws.String(v)
}
// StringValue converts the supplied string pointer to a string, returning the
// empty string if the pointer is nil.
// TODO(muvaf): is this really meaningful? why not implement it?
func StringValue(v *string) string {
return aws.ToString(v)
}
// BoolValue calls underlying aws ToBool
func BoolValue(v *bool) bool {
return aws.ToBool(v)
}
// Int64Value converts the supplied int64 pointer to a int64, returning
// 0 if the pointer is nil.
func Int64Value(v *int64) int64 {
if v != nil {
return *v
}
return 0
}
// LateInitializeStringPtr returns in if it's non-nil, otherwise returns from
// which is the backup for the cases in is nil.
func LateInitializeStringPtr(in *string, from *string) *string {
if in != nil {
return in
}
return from
}
// LateInitializeString returns `from` if `in` is empty and `from` is non-nil,
// in other cases it returns `in`.
func LateInitializeString(in string, from *string) string {
if in == "" && from != nil {
return *from
}
return in
}
// LateInitializeTimePtr returns in if it's non-nil, otherwise returns from
// which is the backup for the cases in is nil.
func LateInitializeTimePtr(in *metav1.Time, from *time.Time) *metav1.Time {
if in != nil {
return in
}
if from != nil {
t := metav1.NewTime(*from)
return &t
}
return nil
}
// Int64 converts the supplied int for use with the AWS Go SDK.
func Int64(v int, o ...FieldOption) *int64 {
for _, fo := range o {
if fo == FieldRequired && v == 0 {
return aws.Int64(int64(v))
}
}
if v == 0 {
return nil
}
return aws.Int64(int64(v))
}
// Int32 converts the supplied int for use with the AWS Go SDK.
func Int32(v int, o ...FieldOption) *int32 {
for _, fo := range o {
if fo == FieldRequired && v == 0 {
return aws.Int32(int32(v))
}
}
if v == 0 {
return nil
}
return aws.Int32(int32(v))
}
// Int64Address returns the given *int in the form of *int64.
func Int64Address(i *int) *int64 {
if i == nil {
return nil
}
return aws.Int64(int64(*i))
}
// Int32Address returns the given *int in the form of *int32.
func Int32Address(i *int) *int32 {
if i == nil {
return nil
}
return aws.Int32(int32(*i))
}
// IntAddress converts the supplied int64 pointer to an int pointer, returning nil if
// the pointer is nil.
func IntAddress(i *int64) *int {
if i == nil {
return nil
}
r := int(*i)
return &r
}
// IntFrom32Address converts the supplied int32 pointer to an int pointer, returning nil if
// the pointer is nil.
func IntFrom32Address(i *int32) *int {
if i == nil {
return nil
}
r := int(*i)
return &r
}
// LateInitializeIntPtr returns in if it's non-nil, otherwise returns from
// which is the backup for the cases in is nil.
func LateInitializeIntPtr(in *int, from *int64) *int {
if in != nil {
return in
}
if from != nil {
i := int(*from)
return &i
}
return nil
}
// LateInitializeIntFrom32Ptr returns in if it's non-nil, otherwise returns from
// which is the backup for the cases in is nil.
func LateInitializeIntFrom32Ptr(in *int, from *int32) *int {
if in != nil {
return in
}
if from != nil && *from != 0 {
i := int(*from)
return &i
}
return nil
}
// LateInitializeInt32Ptr returns in if it's non-nil, otherwise returns from
// which is the backup for the cases in is nil.
func LateInitializeInt32Ptr(in *int32, from *int32) *int32 {
if in != nil {
return in
}
return from
}
// LateInitializeInt64Ptr returns in if it's non-nil, otherwise returns from
// which is the backup for the cases in is nil.
func LateInitializeInt64Ptr(in *int64, from *int64) *int64 {
if in != nil {
return in
}
return from
}
// LateInitializeInt32 returns in if it's non-zero, otherwise returns from
// which is the backup for the cases in is zero.
func LateInitializeInt32(in int32, from int32) int32 {
if in != 0 {
return in
}
return from
}
// LateInitializeInt64 returns in if it's non-zero, otherwise returns from
// which is the backup for the cases in is zero.
func LateInitializeInt64(in int64, from int64) int64 {
if in != 0 {
return in
}
return from
}
// LateInitializeStringPtrSlice returns in if it's non-nil or from is zero
// length, otherwise it returns from.
func LateInitializeStringPtrSlice(in []*string, from []*string) []*string {
if in != nil || len(from) == 0 {
return in
}
return from
}
// LateInitializeInt64PtrSlice returns in if it's non-nil or from is zero
// length, otherwise it returns from.
func LateInitializeInt64PtrSlice(in []*int64, from []*int64) []*int64 {
if in != nil || len(from) == 0 {
return in
}
return from
}
// Bool converts the supplied bool for use with the AWS Go SDK.
func Bool(v bool, o ...FieldOption) *bool {
for _, fo := range o {
if fo == FieldRequired && !v {
return aws.Bool(v)
}
}
if !v {
return nil
}
return aws.Bool(v)
}
// LateInitializeBoolPtr returns in if it's non-nil, otherwise returns from
// which is the backup for the cases in is nil.
func LateInitializeBoolPtr(in *bool, from *bool) *bool {
if in != nil {
return in
}
return from
}
// CompactAndEscapeJSON removes space characters and URL-encodes the JSON string.
func CompactAndEscapeJSON(s string) (string, error) {
buffer := new(bytes.Buffer)
if err := json.Compact(buffer, []byte(s)); err != nil {
return "", err
}
return url.QueryEscape(buffer.String()), nil
}
// DiffTags returns tags that should be added or removed.
func DiffTags(local, remote map[string]string) (add map[string]string, remove []string) {
add = make(map[string]string, len(local))
remove = []string{}
for k, v := range local {
add[k] = v
}
for k, v := range remote {
switch val, ok := local[k]; {
case ok && val != v:
remove = append(remove, k)
case !ok:
remove = append(remove, k)
delete(add, k)
default:
delete(add, k)
}
}
return
}
// DiffEC2Tags returns []ec2type.Tag that should be added or removed.
func DiffEC2Tags(local []ec2type.Tag, remote []ec2type.Tag) (add []ec2type.Tag, remove []ec2type.Tag) {
var tagsToAdd = make(map[string]string, len(local))
add = []ec2type.Tag{}
remove = []ec2type.Tag{}
for _, j := range local {
tagsToAdd[aws.ToString(j.Key)] = aws.ToString(j.Value)
}
for _, j := range remote {
switch val, ok := tagsToAdd[aws.ToString(j.Key)]; {
case ok && val == aws.ToString(j.Value):
delete(tagsToAdd, aws.ToString(j.Key))
case !ok:
remove = append(remove, ec2type.Tag{
Key: j.Key,
Value: nil,
})
}
}
for i, j := range tagsToAdd {
add = append(add, ec2type.Tag{
Key: aws.String(i),
Value: aws.String(j),
})
}
return
}
// DiffLabels returns labels that should be added, modified, or removed.
func DiffLabels(local, remote map[string]string) (addOrModify map[string]string, remove []string) {
addOrModify = make(map[string]string, len(local))
remove = []string{}
for k, v := range local {
addOrModify[k] = v
}
for k, v := range remote {
switch val, ok := local[k]; {
case ok && val != v:
// if value does not match key it will be updated by the correct
// key-value pair being present in the returned addOrModify map
continue
case !ok:
remove = append(remove, k)
delete(addOrModify, k)
default:
delete(addOrModify, k)
}
}
return
}
// IsPolicyUpToDate Marshall policies to json for a compare to get around string ordering
func IsPolicyUpToDate(local, remote *string) bool {
var localUnmarshalled interface{}
var remoteUnmarshalled interface{}
var err error
err = json.Unmarshal([]byte(*local), &localUnmarshalled)
if err != nil {
return false
}
err = json.Unmarshal([]byte(*remote), &remoteUnmarshalled)
if err != nil {
return false
}
sortSlicesOpt := cmpopts.SortSlices(func(x, y interface{}) bool {
if a, ok := x.(string); ok {
if b, ok := y.(string); ok {
return a < b
}
}
// Note: Unknown types in slices will not cause a panic, but
// may not be sorted correctly. Depending on how AWS handles
// these, it may cause constant updates - but better this than
// panicing.
return false
})
return cmp.Equal(localUnmarshalled, remoteUnmarshalled, cmpopts.EquateEmpty(), sortSlicesOpt)
}
// Wrap will remove the request-specific information from the error and only then
// wrap it.
func Wrap(err error, msg string) error {
// NOTE(muvaf): nil check is done for performance, otherwise errors.As makes
// a few reflection calls before returning false, letting awsErr be nil.
if err == nil {
return nil
}
var awsErr smithy.APIError
if errors.As(err, &awsErr) {
return errors.Wrap(awsErr, msg)
}
// AWS SDK v1 uses different interfaces than v2 and it doesn't unwrap to
// the underlying error. So, we need to strip off the unique request ID
// manually.
if v1RequestError, ok := err.(awserr.RequestFailure); ok {
return errors.Wrap(errors.New(strings.ReplaceAll(err.Error(), v1RequestError.RequestID(), "")), msg)
}
return errors.Wrap(err, msg)
}
// DiffTagsMapPtr returns which AWS Tags exist in the resource tags and which are outdated and should be removed
func DiffTagsMapPtr(spec map[string]*string, current map[string]*string) (map[string]*string, []*string) {
addMap := make(map[string]*string, len(spec))
removeTags := make([]*string, 0)
for k, v := range current {
if StringValue(spec[k]) == StringValue(v) {
continue
}
removeTags = append(removeTags, String(k))
}
for k, v := range spec {
if StringValue(current[k]) == StringValue(v) {
continue
}
addMap[k] = v
}
return addMap, removeTags
}