/
provider.go
1256 lines (1120 loc) · 42.6 KB
/
provider.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
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2016-2021, Pulumi Corporation.
//
// 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.
// nolint: gosec
package provider
import (
"bytes"
"compress/gzip"
"context"
"encoding/json"
"fmt"
"os"
"os/user"
"path/filepath"
"regexp"
"runtime"
"strconv"
"strings"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/aws/ratelimit"
"github.com/aws/aws-sdk-go-v2/aws/retry"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/aws/aws-sdk-go-v2/credentials/stscreds"
"github.com/aws/aws-sdk-go-v2/service/cloudcontrol"
"github.com/aws/aws-sdk-go-v2/service/cloudformation"
"github.com/aws/aws-sdk-go-v2/service/ec2"
"github.com/aws/aws-sdk-go-v2/service/ssm"
"github.com/aws/aws-sdk-go-v2/service/sts"
ststypes "github.com/aws/aws-sdk-go-v2/service/sts/types"
"github.com/aws/smithy-go/middleware"
smithyhttp "github.com/aws/smithy-go/transport/http"
"github.com/golang/glog"
pbempty "github.com/golang/protobuf/ptypes/empty"
"github.com/pkg/errors"
"github.com/pulumi/pulumi-aws-native/provider/pkg/autonaming"
"github.com/pulumi/pulumi-aws-native/provider/pkg/client"
"github.com/pulumi/pulumi-aws-native/provider/pkg/default_tags"
"github.com/pulumi/pulumi-aws-native/provider/pkg/metadata"
"github.com/pulumi/pulumi-aws-native/provider/pkg/naming"
"github.com/pulumi/pulumi-aws-native/provider/pkg/resources"
"github.com/pulumi/pulumi-aws-native/provider/pkg/schema"
"github.com/pulumi/pulumi-aws-native/provider/pkg/version"
"github.com/pulumi/pulumi-go-provider/resourcex"
"github.com/pulumi/pulumi/pkg/v3/resource/provider"
"github.com/pulumi/pulumi/sdk/v3/go/common/diag"
"github.com/pulumi/pulumi/sdk/v3/go/common/resource"
"github.com/pulumi/pulumi/sdk/v3/go/common/resource/plugin"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/rpcutil/rpcerror"
pulumirpc "github.com/pulumi/pulumi/sdk/v3/proto/go"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/emptypb"
"google.golang.org/protobuf/types/known/structpb"
)
type cancellationContext struct {
context context.Context
cancel context.CancelFunc
}
func makeCancellationContext() *cancellationContext {
ctx, cancel := context.WithCancel(context.Background())
return &cancellationContext{
context: ctx,
cancel: cancel,
}
}
// Disabled rate limiter to avoid rate limiting attempt retries
// across all attempts the retryer is being used with.
// https://pkg.go.dev/github.com/aws/aws-sdk-go-v2/aws/retry#StandardOptions.RateLimiter
type noOpRateLimiter struct{}
func (noOpRateLimiter) AddTokens(uint) error { return nil }
func (noOpRateLimiter) GetToken(context.Context, uint) (func() error, error) {
return func() error { return nil }, nil
}
type cfnProvider struct {
pulumirpc.UnimplementedResourceProviderServer
host *provider.HostClient
name string
canceler *cancellationContext
configured bool
version string
accountID string
region string
partition partition
resourceMap *metadata.CloudAPIMetadata
roleArn *string
allowedAccountIds []string
forbiddenAccountIds []string
defaultTags map[string]string
pulumiSchema []byte
cfn *cloudformation.Client
ccc client.CloudControlClient
ec2 *ec2.Client
ssm *ssm.Client
sts *sts.Client
customResources map[string]resources.CustomResource
}
var _ pulumirpc.ResourceProviderServer = (*cfnProvider)(nil)
func NewAwsNativeProvider(host *provider.HostClient, name, version string,
pulumiSchema, cloudAPIResourcesBytes []byte) (pulumirpc.ResourceProviderServer, error) {
resourceMap, err := LoadMetadata(cloudAPIResourcesBytes)
if err != nil {
return nil, err
}
return &cfnProvider{
host: host,
canceler: makeCancellationContext(),
name: name,
version: version,
resourceMap: resourceMap,
pulumiSchema: pulumiSchema,
}, nil
}
// LoadMetadata deserializes the provided compressed json byte array into a CloudAPIMetadata struct.
func LoadMetadata(metadataBytes []byte) (*metadata.CloudAPIMetadata, error) {
var resourceMap metadata.CloudAPIMetadata
uncompressed, err := gzip.NewReader(bytes.NewReader(metadataBytes))
if err != nil {
return nil, errors.Wrap(err, "expand compressed metadata")
}
if err = json.NewDecoder(uncompressed).Decode(&resourceMap); err != nil {
return nil, errors.Wrap(err, "unmarshalling resource map")
}
if err = uncompressed.Close(); err != nil {
return nil, errors.Wrap(err, "closing uncompress stream for metadata")
}
return &resourceMap, nil
}
func (p *cfnProvider) Attach(context context.Context, req *pulumirpc.PluginAttach) (*emptypb.Empty, error) {
host, err := provider.NewHostClient(req.GetAddress())
if err != nil {
return nil, err
}
p.host = host
return &pbempty.Empty{}, nil
}
func (p *cfnProvider) GetSchema(ctx context.Context, req *pulumirpc.GetSchemaRequest) (*pulumirpc.GetSchemaResponse, error) {
if v := req.GetVersion(); v != 0 {
return nil, fmt.Errorf("unsupported schema version %d", v)
}
decompressed, err := schema.DecompressSchema(p.pulumiSchema)
if err != nil {
return nil, errors.New("failure loading schema")
}
return &pulumirpc.GetSchemaResponse{Schema: string(decompressed)}, nil
}
// CheckConfig validates the configuration for this provider.
func (p *cfnProvider) CheckConfig(ctx context.Context, req *pulumirpc.CheckRequest) (*pulumirpc.CheckResponse, error) {
news, err := plugin.UnmarshalProperties(req.GetNews(), plugin.MarshalOptions{
Label: fmt.Sprintf("%s.CheckConfig.news", p.name),
KeepUnknowns: true,
})
if err != nil {
return nil, err
}
truthyValue := func(argName resource.PropertyKey, props resource.PropertyMap) bool {
if arg := props[argName]; arg.HasValue() {
switch {
case arg.IsString() && len(arg.StringValue()) > 0:
return true
case arg.IsBool() && arg.BoolValue():
return true
}
}
return false
}
// These provider values are not yet implemented, so return an error and link to the relevant issue(s).
var failures []*pulumirpc.CheckFailure
for k := range news {
switch k {
case "ignoreTags":
failures = append(failures, &pulumirpc.CheckFailure{Property: string(k),
Reason: fmt.Sprintf("not yet implemented. See https://github.com/pulumi/pulumi-aws-native/issues/110")})
case "insecure":
failures = append(failures, &pulumirpc.CheckFailure{Property: string(k),
Reason: fmt.Sprintf("not yet implemented. See https://github.com/pulumi/pulumi-aws-native/issues/111")})
case "s3ForcePathStyle":
failures = append(failures, &pulumirpc.CheckFailure{Property: string(k),
Reason: fmt.Sprintf("not yet implemented. See https://github.com/pulumi/pulumi-aws-native/issues/113")})
case "skipGetEc2Platforms":
if !truthyValue(k, news) {
failures = append(failures, &pulumirpc.CheckFailure{Property: string(k),
Reason: fmt.Sprintf("not yet implemented. See https://github.com/pulumi/pulumi-aws-native/issues/115")})
}
case "skipMetadataApiCheck":
if !truthyValue(k, news) {
failures = append(failures, &pulumirpc.CheckFailure{Property: string(k),
Reason: fmt.Sprintf("not yet implemented. See https://github.com/pulumi/pulumi-aws-native/issues/116")})
}
case "skipRegionValidation":
if !truthyValue(k, news) {
failures = append(failures, &pulumirpc.CheckFailure{Property: string(k),
Reason: fmt.Sprintf("not yet implemented. See https://github.com/pulumi/pulumi-aws-native/issues/117")})
}
case "skipRequestingAccountId":
failures = append(failures, &pulumirpc.CheckFailure{Property: string(k),
Reason: fmt.Sprintf("not yet implemented. See https://github.com/pulumi/pulumi-aws-native/issues/118")})
}
}
if failures != nil {
return &pulumirpc.CheckResponse{Failures: failures}, nil
}
return &pulumirpc.CheckResponse{Inputs: req.GetNews()}, nil
}
// DiffConfig diffs the configuration for this provider.
func (p *cfnProvider) DiffConfig(ctx context.Context, req *pulumirpc.DiffRequest) (*pulumirpc.DiffResponse, error) {
urn := resource.URN(req.GetUrn())
label := fmt.Sprintf("%s.DiffConfig(%s)", p.name, urn)
glog.V(9).Infof("%s executing", label)
olds, err := plugin.UnmarshalProperties(req.GetOlds(), plugin.MarshalOptions{
Label: fmt.Sprintf("%s.olds", label),
KeepUnknowns: true,
})
if err != nil {
return nil, err
}
news, err := plugin.UnmarshalProperties(req.GetNews(), plugin.MarshalOptions{
Label: fmt.Sprintf("%s.news", label),
KeepUnknowns: true,
RejectAssets: true,
})
if err != nil {
return nil, errors.Wrapf(err, "diffConfig failed because of malformed resource inputs")
}
diff := olds.Diff(news)
if diff == nil {
return &pulumirpc.DiffResponse{Changes: pulumirpc.DiffResponse_DIFF_NONE}, nil
}
var diffs, replaces []string
for _, k := range diff.Keys() {
diffs = append(diffs, string(k))
}
return &pulumirpc.DiffResponse{
Changes: pulumirpc.DiffResponse_DIFF_SOME,
Diffs: diffs,
Replaces: replaces,
}, nil
}
// Configure configures the resource provider with "globals" that control its behavior.
func (p *cfnProvider) Configure(ctx context.Context, req *pulumirpc.ConfigureRequest) (*pulumirpc.ConfigureResponse, error) {
vars := req.GetVariables()
// loadOptions are used to override default config loading behavior.
var loadOptions []func(*config.LoadOptions) error
if region, ok := varsOrEnv(vars, "aws-native:config:region", "AWS_REGION", "AWS_DEFAULT_REGION"); ok {
glog.V(4).Infof("using AWS region: %q", region)
loadOptions = append(loadOptions, config.WithRegion(region))
p.region, p.partition = region, getPartition(region)
} else {
return nil, errors.New("missing required configuration key \"aws-native:region\":" +
"The region where AWS operations will take place. Examples are eu-east-1, eu-west-2, etc.\n" +
"\tSet a value using the command `pulumi config set aws-native:region <value>`, or by setting the environment variables `AWS_REGION` or `AWS_DEFAULT_REGION`.")
}
if profile, ok := varsOrEnv(vars, "aws-native:config:profile", "AWS_PROFILE"); ok {
glog.V(4).Infof("using AWS profile: %q", profile)
loadOptions = append(loadOptions, config.WithSharedConfigProfile(profile))
} else {
glog.V(4).Infof(`using AWS profile: "default"`)
}
if sharedCredentialsFilePath, ok := varsOrEnv(vars, "aws-native:config:sharedCredentialsFile", "AWS_SHARED_CREDENTIALS_FILE"); ok {
glog.V(4).Infof("using AWS shared credentials file at path: %q", sharedCredentialsFilePath)
normalizedPath, err := normalizeFilePath(sharedCredentialsFilePath)
if err != nil {
return nil, fmt.Errorf("failed to load shared credentials file: %w", err)
}
loadOptions = append(loadOptions, config.WithSharedCredentialsFiles([]string{normalizedPath}))
} else {
if runtime.GOOS == "windows" {
windowsPath := `%USERPROFILE%\.aws\credentials`
glog.V(4).Infof(`using AWS shared credentials file at path: %q`, windowsPath)
} else {
glog.V(4).Info(`using AWS shared credentials file at path: "~/.aws/credentials"`)
}
}
// Environment variables are checked by the AWS SDK by default as a fallback after explicitly defined config.
// See https://github.com/pulumi/pulumi-aws-native/pull/1378
var accessKey, secretKey, token string
if v, ok := varsOrEnv(vars, "aws-native:config:accessKey"); ok {
accessKey = v
}
if v, ok := varsOrEnv(vars, "aws-native:config:secretKey"); ok {
secretKey = v
}
if v, ok := varsOrEnv(vars, "aws-native:config:token"); ok {
token = v
}
if len(accessKey) > 0 || len(secretKey) > 0 || len(token) > 0 {
// If all required values are not present/valid, the client will return an appropriate error.
credsProvider := credentials.NewStaticCredentialsProvider(accessKey, secretKey, token)
loadOptions = append(loadOptions, config.WithCredentialsProvider(credsProvider))
}
if endpointsString, ok := vars["aws-native:config:endpoints"]; ok {
var endpoints map[string]string
err := json.Unmarshal([]byte(endpointsString), &endpoints)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal 'endpoints' config: %w", err)
}
glog.V(4).Infof("using AWS endpoints: %v", endpoints)
resolver := aws.EndpointResolverWithOptionsFunc(func(service, region string, options ...interface{}) (aws.Endpoint, error) {
if endpoint, ok := endpoints[strings.ToLower(service)]; ok {
return aws.Endpoint{URL: endpoint}, nil
}
return aws.Endpoint{}, &aws.EndpointNotFoundError{}
})
loadOptions = append(loadOptions, config.WithEndpointResolverWithOptions(resolver))
}
// Attach custom middleware to the client.
loadOptions = append(loadOptions, config.WithAPIOptions(func() (v []func(stack *middleware.Stack) error) {
v = append(v, attachCustomMiddleware)
return v
}()))
if glog.V(9) {
loadOptions = append(loadOptions, config.WithClientLogMode(aws.LogRequestWithBody|aws.LogResponseWithBody))
}
// Load configuration from the environment, overriding with any config that was explicitly set on the Provider.
cfg, err := config.LoadDefaultConfig(ctx, loadOptions...)
if err != nil {
return nil, errors.Wrapf(err, "could not load AWS config")
}
if assumeRoleJson, ok := vars["aws-native:config:assumeRole"]; ok {
var assumeRole ProviderAssumeRole
err := json.Unmarshal([]byte(assumeRoleJson), &assumeRole)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal 'assumeRole' config: %w", err)
}
creds := stscreds.NewAssumeRoleProvider(sts.NewFromConfig(cfg), "roleArn",
func(o *stscreds.AssumeRoleOptions) {
if assumeRole.DurationSeconds != nil {
o.Duration = time.Duration(*(assumeRole.DurationSeconds)) * time.Second
}
o.ExternalID = assumeRole.ExternalId
o.Policy = assumeRole.Policy
for _, arn := range assumeRole.PolicyArns {
o.PolicyARNs = append(o.PolicyARNs, ststypes.PolicyDescriptorType{Arn: aws.String(arn)})
}
if assumeRole.RoleArn != nil {
o.RoleARN = *assumeRole.RoleArn
}
if assumeRole.SessionName != nil {
o.RoleSessionName = *assumeRole.SessionName
}
for k, v := range assumeRole.Tags {
o.Tags = append(o.Tags, ststypes.Tag{Key: aws.String(k), Value: aws.String(v)})
}
if assumeRole.TransitiveTagKeys != nil {
o.TransitiveTagKeys = assumeRole.TransitiveTagKeys
}
})
cfg.Credentials = aws.NewCredentialsCache(creds)
}
if roleArn, ok := vars["aws-native:config:roleArn"]; ok {
p.roleArn = &roleArn
}
maxRetries := 25
if maxRetriesConf, ok := vars["aws-native:config:maxRetries"]; ok {
maxRetries, err = strconv.Atoi(maxRetriesConf)
if err != nil {
return nil, fmt.Errorf("invalid config value for 'maxRetries': %q: %w", maxRetriesConf, err)
}
}
glog.V(4).Infof("using Max Retry Attempts: %d", maxRetries)
maxRetryRateTokens := -1
if maxRetryRateTokensConf, ok := vars["aws-native:config:maxRetryRateTokens"]; ok {
maxRetryRateTokens, err = strconv.Atoi(maxRetryRateTokensConf)
if err != nil {
return nil, fmt.Errorf("invalid config value for 'maxRetryRateTokens': %q: %w", maxRetryRateTokensConf, err)
}
glog.V(4).Infof("using Retry Token Rate Limit: %d", maxRetryRateTokens)
} else {
glog.V(4).Infof("using Retry Token Rate Limit: unlimited")
}
cfg.Retryer = func() aws.Retryer {
return retry.NewStandard(func(o *retry.StandardOptions) {
o.MaxAttempts = maxRetries
if maxRetryRateTokens > 0 {
o.RateLimiter = ratelimit.NewTokenRateLimit(uint(maxRetryRateTokens))
} else {
o.RateLimiter = noOpRateLimiter{}
}
})
}
if allowedAccountIdsJson, ok := vars["aws-native:config:allowedAccountIds"]; ok {
var allowedAccountIds []string
err := json.Unmarshal([]byte(allowedAccountIdsJson), &allowedAccountIds)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal 'allowedAccountIds' config: %w", err)
}
p.allowedAccountIds = allowedAccountIds
}
if forbiddenAccountIdsJson, ok := vars["aws-native:config:forbiddenAccountIds"]; ok {
var forbiddenAccountIds []string
err := json.Unmarshal([]byte(forbiddenAccountIdsJson), &forbiddenAccountIds)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal 'forbiddenAccountIds' config: %w", err)
}
p.forbiddenAccountIds = forbiddenAccountIds
}
skipCredentialsValidation := false
if skipCredentialsValidationVar, ok := varsOrEnv(vars, "aws-native:config:skipCredentialsValidation", "AWS_SKIP_CREDENTIALS_VALIDATION"); ok {
if skipCredentialsValidationVar == "true" {
skipCredentialsValidation = true
}
}
if defaultTagsJson, ok := vars["aws-native:config:defaultTags"]; ok {
type DefaultTags struct {
Tags map[string]string `json:"tags"`
}
var defaultTags DefaultTags
err := json.Unmarshal([]byte(defaultTagsJson), &defaultTags)
if err == nil {
p.defaultTags = defaultTags.Tags
} else {
// As a fallback, we also try to unmarshal the default tags as a simple map[string]string
// as this was originally supported when implementing defaultTags.
fallbackErr := json.Unmarshal([]byte(defaultTagsJson), &p.defaultTags)
if fallbackErr != nil {
return nil, fmt.Errorf("failed to unmarshal 'defaultTags' config: %w", err)
}
}
} else {
p.defaultTags = nil
}
p.cfn = cloudformation.NewFromConfig(cfg)
p.ccc = client.NewCloudControlClient(cloudcontrol.NewFromConfig(cfg), p.roleArn)
p.ec2 = ec2.NewFromConfig(cfg)
p.ssm = ssm.NewFromConfig(cfg)
p.sts = sts.NewFromConfig(cfg)
if !skipCredentialsValidation {
callerIdentityResp, err := p.sts.GetCallerIdentity(ctx, &sts.GetCallerIdentityInput{})
if err != nil {
return nil, errors.Wrapf(err, "could not get AWS account ID")
}
if callerIdentityResp.Account == nil {
return nil, errors.New("could not get AWS account ID: nil account")
}
p.accountID = *callerIdentityResp.Account
err = p.validateAccountId()
if err != nil {
return nil, err
}
}
p.customResources = map[string]resources.CustomResource{
metadata.ExtensionResourceToken: resources.NewExtensionResource(p.ccc),
}
p.configured = true
return &pulumirpc.ConfigureResponse{
AcceptSecrets: true,
}, nil
}
func varsOrEnv(vars map[string]string, key string, env ...string) (string, bool) {
if val, ok := vars[key]; ok {
return val, true
}
for _, e := range env {
if val, ok := os.LookupEnv(e); ok {
return val, true
}
}
return "", false
}
// ValidateAccountId returns a context-specific error if the configured account
// id is explicitly forbidden or not authorised; and nil if it is authorised.
func (p *cfnProvider) validateAccountId() error {
if len(p.allowedAccountIds) == 0 && len(p.forbiddenAccountIds) == 0 {
return nil
}
if p.forbiddenAccountIds != nil {
for _, id := range p.forbiddenAccountIds {
if id == p.accountID {
return fmt.Errorf("forbidden account ID (%s)", id)
}
}
}
if p.allowedAccountIds != nil {
for _, id := range p.allowedAccountIds {
if id == p.accountID {
return nil
}
}
return fmt.Errorf("account ID not allowed (%s)", p.accountID)
}
return nil
}
var functions = map[string]func(*cfnProvider, context.Context, resource.PropertyMap) (resource.PropertyMap, error){
"aws-native:index:getAccountId": (*cfnProvider).getAccountID,
"aws-native:index:getAzs": (*cfnProvider).getAZs,
"aws-native:index:getRegion": (*cfnProvider).getRegion,
"aws-native:index:getUrlSuffix": (*cfnProvider).getURLSuffix,
"aws-native:index:cidr": (*cfnProvider).cidr,
"aws-native:index:getSsmParameterString": (*cfnProvider).getSSMParameterString,
"aws-native:index:getSsmParameterList": (*cfnProvider).getSSMParameterList,
"aws-native:index:importValue": (*cfnProvider).importValue,
"aws-native:index:getPartition": (*cfnProvider).getPartition,
}
type invokeFunc func(p *cfnProvider, ctx context.Context, inputs resource.PropertyMap) (resource.PropertyMap, error)
// Invoke dynamically executes a built-in function in the provider.
func (p *cfnProvider) Invoke(ctx context.Context,
req *pulumirpc.InvokeRequest) (*pulumirpc.InvokeResponse, error) {
// Unmarshal arguments.
tok := req.GetTok()
inputs, err := plugin.UnmarshalProperties(req.GetArgs(), plugin.MarshalOptions{
Label: fmt.Sprintf("%s.Invoke(%s).inputs", p.name, tok),
KeepUnknowns: true,
KeepSecrets: true,
})
if err != nil {
return nil, err
}
// Process Invoke call.
var result resource.PropertyMap
fn, ok := functions[tok]
if !ok {
fn, ok = p.getInvokeFunc(ctx, tok)
}
if !ok {
return nil, fmt.Errorf("unknown function '%s'", tok)
}
result, err = fn(p, ctx, inputs)
if err != nil {
return nil, err
}
res, err := plugin.MarshalProperties(result, plugin.MarshalOptions{
Label: fmt.Sprintf("%s.Invoke(%s).outputs", p.name, tok),
KeepUnknowns: true,
KeepSecrets: true,
})
if err != nil {
return nil, err
}
return &pulumirpc.InvokeResponse{Return: res}, nil
}
func (p *cfnProvider) getInvokeFunc(ctx context.Context, tok string) (invokeFunc, bool) {
cf, ok := p.resourceMap.Functions[tok]
if !ok {
return nil, false
}
return func(p *cfnProvider, ctx context.Context, inputs resource.PropertyMap) (resource.PropertyMap, error) {
idParts := make([]string, len(cf.Identifiers))
for i, v := range cf.Identifiers {
pv, ok := inputs[resource.PropertyKey(v)]
if !ok {
return nil, errors.Errorf("missing identifier property %q", v)
}
if !pv.IsString() {
return nil, errors.Errorf("identifier property %q, expected type string, found %q", v, pv.TypeString())
}
idParts[i] = pv.StringValue()
}
identifier := strings.Join(idParts, "|")
glog.V(9).Infof("%s invoking", cf.CfType)
outputs, exists, err := p.ccc.Read(ctx, cf.CfType, identifier)
if err != nil {
return nil, err
}
if !exists {
return nil, errors.New("resource not found")
}
sdkOutput := naming.CfnToSdk(outputs)
return resource.NewPropertyMapFromMap(sdkOutput), nil
}, true
}
// StreamInvoke dynamically executes a built-in function in the provider. The result is streamed
// back as a series of messages.
func (p *cfnProvider) StreamInvoke(
req *pulumirpc.InvokeRequest, server pulumirpc.ResourceProvider_StreamInvokeServer) error {
tok := req.GetTok()
return errors.Errorf("unrecognized function (StreamInvoke): %s", tok)
}
// filterNullValues removes nested null values from the given property value. If all nested values in the property
// value are null, the property value itself is considered null. This allows for easier integration with CloudFormation
// templates, which use `AWS::NoValue` to remove values from lists, maps, etc. We use `null` to the same effect.
func filterNullValues(v resource.PropertyValue) resource.PropertyValue {
switch {
case v.IsArray():
if len(v.ArrayValue()) == 0 {
return v
}
var result []resource.PropertyValue
for _, e := range v.ArrayValue() {
e = filterNullValues(e)
if !e.IsNull() {
result = append(result, e)
}
}
return resource.NewArrayProperty(result)
case v.IsObject():
return resource.NewObjectProperty(filterNullProperties(v.ObjectValue()))
default:
return v
}
}
// filterNullValues removes nested null values from the given property map. If all nested values in the property
// map are null, the property map itself is considered null. This allows for easier integration with CloudFormation
// templates, which use `AWS::NoValue` to remove values from lists, maps, etc. We use `null` to the same effect.
func filterNullProperties(m resource.PropertyMap) resource.PropertyMap {
if len(m) == 0 {
return m
}
result := resource.PropertyMap{}
for k, v := range m {
e := filterNullValues(v)
if !e.IsNull() {
result[k] = e
}
}
if len(result) == 0 {
return nil
}
return result
}
func (p *cfnProvider) Check(ctx context.Context, req *pulumirpc.CheckRequest) (*pulumirpc.CheckResponse, error) {
urn := resource.URN(req.GetUrn())
label := fmt.Sprintf("%s.Check(%s)", p.name, urn)
glog.V(9).Infof("%s executing", label)
olds, err := plugin.UnmarshalProperties(req.GetOlds(), plugin.MarshalOptions{
Label: fmt.Sprintf("%s.olds", label), SkipNulls: true,
})
if err != nil {
return nil, err
}
// Parse inputs.
newInputs, err := plugin.UnmarshalProperties(req.GetNews(), plugin.MarshalOptions{
Label: fmt.Sprintf("%s.properties", label),
KeepUnknowns: true,
RejectAssets: true,
KeepSecrets: true,
})
if err != nil {
return nil, err
}
// Filter null properties from the inputs.
newInputs = filterNullProperties(newInputs)
var failures []resources.ValidationFailure
resourceToken := string(urn.Type())
if customResource, ok := p.customResources[resourceToken]; ok {
newInputs, failures, err = customResource.Check(ctx, urn, req.RandomSeed, newInputs, olds, p.defaultTags)
if err != nil {
return nil, fmt.Errorf("failed to check custom resource %q: %w", resourceToken, err)
}
} else {
spec, ok := p.resourceMap.Resources[resourceToken]
if !ok {
return nil, errors.Errorf("resource type %s not found", resourceToken)
}
if err := autonaming.ApplyAutoNaming(spec.AutoNamingSpec, urn, req.RandomSeed, olds, newInputs); err != nil {
return nil, fmt.Errorf("failed to apply auto-naming: %w", err)
}
// Merge default tags into the inputs if the resource supports tags and the user has not overridden them.
if len(p.defaultTags) > 0 && spec.TagsProperty != "" && spec.TagsStyle != default_tags.TagsStyleUnknown {
tagsKey := resource.PropertyKey(spec.TagsProperty)
val, err := default_tags.MergeDefaultTags(newInputs[tagsKey], p.defaultTags, spec.TagsStyle)
if err != nil {
return nil, err
}
newInputs[tagsKey] = val
}
failures, err = resources.ValidateResource(&spec, p.resourceMap.Types, newInputs)
if err != nil {
return nil, err
}
}
var checkFailures []*pulumirpc.CheckFailure
for _, f := range failures {
checkFailures = append(checkFailures, &pulumirpc.CheckFailure{Property: f.Path, Reason: f.Reason})
}
if len(failures) == 0 {
inputs, err := plugin.MarshalProperties(newInputs, plugin.MarshalOptions{
Label: fmt.Sprintf("%s.inputs", label),
KeepUnknowns: true,
KeepSecrets: true,
})
if err != nil {
return nil, err
}
return &pulumirpc.CheckResponse{Inputs: inputs}, nil
}
return &pulumirpc.CheckResponse{Failures: checkFailures}, nil
}
func (p *cfnProvider) Diff(ctx context.Context, req *pulumirpc.DiffRequest) (*pulumirpc.DiffResponse, error) {
urn := resource.URN(req.GetUrn())
label := fmt.Sprintf("%s.Diff(%s)", p.name, urn)
glog.V(9).Infof("%s executing", label)
newInputs, err := plugin.UnmarshalProperties(req.GetNews(), plugin.MarshalOptions{
Label: fmt.Sprintf("%s.properties", label),
KeepUnknowns: true,
RejectAssets: true,
KeepSecrets: true,
})
if err != nil {
return nil, errors.Wrapf(err, "failed to parse inputs for update")
}
oldState, err := plugin.UnmarshalProperties(req.GetOlds(), plugin.MarshalOptions{
Label: fmt.Sprintf("%s.oldState", label),
KeepUnknowns: true,
RejectAssets: true,
KeepSecrets: true,
})
if err != nil {
return nil, errors.Wrapf(err, "diff failed because malformed resource inputs")
}
// Extract old inputs from the `__inputs` field of the old state.
oldInputs := parseCheckpointObject(oldState)
diff := oldInputs.Diff(newInputs)
if diff == nil {
return &pulumirpc.DiffResponse{Changes: pulumirpc.DiffResponse_DIFF_NONE}, nil
}
return &pulumirpc.DiffResponse{
Changes: pulumirpc.DiffResponse_DIFF_UNKNOWN,
DeleteBeforeReplace: true,
}, nil
}
func (p *cfnProvider) Create(ctx context.Context, req *pulumirpc.CreateRequest) (*pulumirpc.CreateResponse, error) {
urn := resource.URN(req.GetUrn())
label := fmt.Sprintf("%s.Create(%s)", p.name, urn)
glog.V(9).Infof("%s executing", label)
// Deserialize RPC inputs.
inputs, err := plugin.UnmarshalProperties(req.GetProperties(), plugin.MarshalOptions{
Label: fmt.Sprintf("%s.properties", label),
KeepUnknowns: true,
RejectAssets: true,
KeepSecrets: true,
})
if err != nil {
return nil, errors.Wrapf(err, "malformed resource inputs")
}
resourceToken := string(urn.Type())
var id *string
var outputs map[string]interface{}
var createErr error
var payload map[string]interface{}
if customResource, ok := p.customResources[resourceToken]; ok {
id, outputs, createErr = customResource.Create(ctx, urn, inputs)
} else {
// Standard resource
spec, hasSpec := p.resourceMap.Resources[resourceToken]
if !hasSpec {
return nil, errors.Errorf("Resource type %s not found", resourceToken)
}
cfType := spec.CfType
// Convert SDK inputs to CFN payload.
payload, err = naming.SdkToCfn(&spec, p.resourceMap.Types, resourcex.Decode(inputs))
if err != nil {
return nil, fmt.Errorf("Failed to convert value for %s: %w", resourceToken, err)
}
// Create the resource with Cloud API.
glog.V(9).Infof("%s.CreateResource %q", label, cfType)
var resourceState map[string]interface{}
id, resourceState, createErr = p.ccc.Create(ctx, cfType, payload)
if createErr != nil && (id == nil || resourceState == nil) {
return nil, errors.Wrapf(createErr, "creating resource")
}
outputs = naming.CfnToSdk(resourceState)
// Write-only properties are not returned in the outputs, so we assume they should have the same value we sent from the inputs.
if hasSpec && len(spec.WriteOnly) > 0 {
inputsMap := inputs.Mappable()
for _, writeOnlyProp := range spec.WriteOnly {
if _, ok := outputs[writeOnlyProp]; !ok {
inputValue, ok := inputsMap[writeOnlyProp]
if ok {
outputs[writeOnlyProp] = inputValue
}
}
}
}
}
if createErr != nil {
if id == nil {
return nil, fmt.Errorf("creating resource: %w", createErr)
}
// Resource was created but failed to fully initialize.
// It has some state, so we return a partial error.
obj := checkpointObject(inputs, outputs)
checkpoint, err := plugin.MarshalProperties(
obj,
plugin.MarshalOptions{
Label: "currentResourceStateCheckpoint.checkpoint",
KeepSecrets: true,
KeepUnknowns: true,
SkipNulls: true,
},
)
if err != nil {
return nil, fmt.Errorf("marshalling currentResourceStateCheckpoint: %w", err)
}
return nil, partialError(*id, createErr, checkpoint, req.GetProperties())
}
// Store both outputs and inputs into the state.
checkpoint, err := plugin.MarshalProperties(
checkpointObject(inputs, outputs),
plugin.MarshalOptions{Label: fmt.Sprintf("%s.checkpoint", label), KeepSecrets: true, KeepUnknowns: true, SkipNulls: true},
)
if err != nil {
return nil, err
}
return &pulumirpc.CreateResponse{
Id: *id,
Properties: checkpoint,
}, nil
}
func (p *cfnProvider) Read(ctx context.Context, req *pulumirpc.ReadRequest) (*pulumirpc.ReadResponse, error) {
urn := resource.URN(req.GetUrn())
label := fmt.Sprintf("%s.Read(%s)", p.name, urn)
glog.V(9).Infof("%s executing", label)
id := req.GetId()
// Retrieve the old state.
oldState, err := plugin.UnmarshalProperties(req.GetProperties(), plugin.MarshalOptions{
Label: fmt.Sprintf("%s.olds", label), KeepUnknowns: true, SkipNulls: true, KeepSecrets: true,
})
if err != nil {
return nil, err
}
// Extract old inputs from the `__inputs` field of the old state.
inputs := parseCheckpointObject(oldState)
// Read the resource state from AWS.
resourceToken := string(urn.Type())
var newInputs resource.PropertyMap
var newState map[string]interface{}
var exists bool
if customResource, ok := p.customResources[resourceToken]; ok {
newState, newInputs, exists, err = customResource.Read(ctx, urn, id, inputs, oldState)
if err != nil {
return nil, err
}
if !exists {
// Not Exists means that the resource was deleted.
return &pulumirpc.ReadResponse{Id: ""}, nil
}
} else {
// Standard resource
spec, ok := p.resourceMap.Resources[resourceToken]
if !ok {
return nil, errors.Errorf("Resource type %s not found", resourceToken)
}
var resourceState map[string]interface{}
resourceState, exists, err = p.ccc.Read(p.canceler.context, spec.CfType, id)
if err != nil {
return nil, err
}
if !exists {
// Not Exists means that the resource was deleted.
return &pulumirpc.ReadResponse{Id: ""}, nil
}
newState = naming.CfnToSdk(resourceState)
if inputs == nil {
// There may be no old state (i.e., importing a new resource).
// Extract inputs from the response body.
newStateProps := resource.NewPropertyMapFromMap(newState)
inputs, err = schema.GetInputsFromState(&spec, newStateProps)
if err != nil {
return nil, err
}
if len(spec.WriteOnly) > 0 {
p.host.Log(ctx, diag.Warning, urn, fmt.Sprintf("Can't import write-only properties: %s", strings.Join(spec.WriteOnly, ", ")))
}
} else {
// It's hard to infer the changes in the inputs shape based on the outputs without false positives.
// The current approach is complicated but it's aimed to minimize the noise while refreshing:
// 0. We have "old" inputs and outputs before refresh and "new" outputs read from AWS.
// 1. Project old outputs to their corresponding input shape (exclude attributes).
oldInputProjection, err := schema.GetInputsFromState(&spec, oldState)
if err != nil {
return nil, err
}
oldStateMap := oldState.Mappable()
// Fill in the write-only properties from the old state as they won't included when reading.
if len(spec.WriteOnly) > 0 {
missingProps := make([]string, 0, len(spec.WriteOnly))
for _, writeOnlyProp := range spec.WriteOnly {
if _, ok := newState[writeOnlyProp]; !ok {
oldValue, ok := oldStateMap[writeOnlyProp]
missingProps = append(missingProps, writeOnlyProp)
if ok {
newState[writeOnlyProp] = oldValue
}
}
}
p.host.Log(ctx, diag.Warning, urn, fmt.Sprintf("Can't refresh write-only properties: %s", strings.Join(missingProps, ", ")))
}
// 2. Project new outputs to their corresponding input shape (exclude attributes).
newStateProps := resource.NewPropertyMapFromMap(newState)
newInputProjection, err := schema.GetInputsFromState(&spec, newStateProps)
if err != nil {
return nil, err
}
// 3. Calculate the difference between two projections. This should give us actual significant changes