-
Notifications
You must be signed in to change notification settings - Fork 619
/
Copy pathinstance_credentials_provider_windows.go
105 lines (93 loc) · 3.83 KB
/
instance_credentials_provider_windows.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
//go:build windows
// +build windows
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file 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 providers
import (
"context"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials/ec2rolecreds"
)
// NewInstanceCredentialsCache returns a chain of instance credentials providers wrapped in a credentials cache.
// The instance credentials chain is the default credentials chain plus the "rotating shared credentials provider",
// so credentials will be checked in this order:
//
// 1. Env vars (AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY).
//
// 2. Shared credentials file (https://docs.aws.amazon.com/ses/latest/DeveloperGuide/create-shared-credentials-file.html) (file at ~/.aws/credentials containing access key id and secret access key).
//
// 3. EC2 role credentials. This is an IAM role that the user specifies when they launch their EC2 container instance (ie ecsInstanceRole (https://docs.aws.amazon.com/AmazonECS/latest/developerguide/instance_IAM_role.html)).
//
// 4. Rotating shared credentials file located at /rotatingcreds/credentials
//
// The default credential chain provided by the SDK includes:
// * EnvProvider
// * SharedCredentialsProvider
// * RemoteCredProvider (EC2RoleProvider)
//
// In the case of ECS-A on Windows, the `SharedCredentialsProvider` takes
// precedence over the `RotatingSharedCredentialsProvider` and this results
// in the credentials not being refreshed. To mitigate this issue, we will
// reorder the credential chain and ensure that `RotatingSharedCredentialsProvider`
// takes precedence over the `SharedCredentialsProvider` for ECS-A.
func NewInstanceCredentialsCache(
isExternal bool,
rotatingSharedCreds aws.CredentialsProvider,
imdsClient ec2rolecreds.GetMetadataAPIClient,
) *InstanceCredentialsCache {
var providers []aws.CredentialsProvider
// If imdsClient is nil, the SDK will default to the EC2 IMDS client.
// Pass a non-nil imdsClient to stub it out in tests.
options := func(o *ec2rolecreds.Options) {
o.Client = imdsClient
}
if isExternal {
providers = []aws.CredentialsProvider{
envCreds,
rotatingSharedCreds,
sharedCreds,
ec2rolecreds.New(options),
}
} else {
providers = []aws.CredentialsProvider{
defaultCreds(options),
rotatingSharedCreds,
}
}
return &InstanceCredentialsCache{
providers: providers,
}
}
var envCreds = aws.CredentialsProviderFunc(func(ctx context.Context) (aws.Credentials, error) {
cfg, err := config.NewEnvConfig()
return cfg.Credentials, err
})
var sharedCreds = aws.CredentialsProviderFunc(func(ctx context.Context) (aws.Credentials, error) {
// Load the env config to get shared config values from env vars (AWS_PROFILE and AWS_SHARED_CREDENTIALS_FILE).
envCfg, err := config.NewEnvConfig()
if err != nil {
return aws.Credentials{}, err
}
// If shared config env vars are unset, use the default values.
if envCfg.SharedConfigProfile == "" {
envCfg.SharedConfigProfile = config.DefaultSharedConfigProfile
}
if envCfg.SharedCredentialsFile == "" {
envCfg.SharedCredentialsFile = config.DefaultSharedCredentialsFilename()
}
cfg, err := config.LoadSharedConfigProfile(ctx, envCfg.SharedConfigProfile, func(option *config.LoadSharedConfigOptions) {
option.CredentialsFiles = []string{envCfg.SharedCredentialsFile}
})
return cfg.Credentials, err
})