forked from hashicorp/aws-sdk-go-base
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aws_config.go
267 lines (230 loc) · 8.24 KB
/
aws_config.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
package awsbase
import (
"context"
"errors"
"fmt"
"log"
"net"
"os"
"strings"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
awsmiddleware "github.com/aws/aws-sdk-go-v2/aws/middleware"
"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/ec2rolecreds"
"github.com/aws/aws-sdk-go-v2/feature/ec2/imds"
"github.com/aws/smithy-go/middleware"
"github.com/hashicorp/aws-sdk-go-base/v2/internal/awsconfig"
"github.com/hashicorp/aws-sdk-go-base/v2/internal/constants"
"github.com/hashicorp/aws-sdk-go-base/v2/internal/endpoints"
)
func GetAwsConfig(ctx context.Context, c *Config) (aws.Config, error) {
if metadataUrl := os.Getenv("AWS_METADATA_URL"); metadataUrl != "" {
log.Println(`[WARN] The environment variable "AWS_METADATA_URL" is deprecated. Use "AWS_EC2_METADATA_SERVICE_ENDPOINT" instead.`)
if ec2MetadataServiceEndpoint := os.Getenv("AWS_EC2_METADATA_SERVICE_ENDPOINT"); ec2MetadataServiceEndpoint != "" {
if ec2MetadataServiceEndpoint != metadataUrl {
log.Printf(`[WARN] The environment variable "AWS_EC2_METADATA_SERVICE_ENDPOINT" is already set to %q. Ignoring "AWS_METADATA_URL".`, ec2MetadataServiceEndpoint)
}
} else {
log.Printf(`[WARN] Setting "AWS_EC2_METADATA_SERVICE_ENDPOINT" to %q.`, metadataUrl)
os.Setenv("AWS_EC2_METADATA_SERVICE_ENDPOINT", metadataUrl)
}
}
credentialsProvider, initialSource, err := getCredentialsProvider(ctx, c)
if err != nil {
return aws.Config{}, err
}
creds, _ := credentialsProvider.Retrieve(ctx)
log.Printf("[INFO] Retrieved credentials from %q", creds.Source)
loadOptions, err := commonLoadOptions(c)
if err != nil {
return aws.Config{}, err
}
loadOptions = append(
loadOptions,
config.WithCredentialsProvider(credentialsProvider),
)
if initialSource == ec2rolecreds.ProviderName {
loadOptions = append(
loadOptions,
config.WithEC2IMDSRegion(),
)
}
awsConfig, err := config.LoadDefaultConfig(ctx, loadOptions...)
if err != nil {
return awsConfig, fmt.Errorf("loading configuration: %w", err)
}
resolveRetryer(ctx, &awsConfig)
if !c.SkipCredsValidation {
if _, _, err := getAccountIDAndPartitionFromSTSGetCallerIdentity(ctx, stsClient(awsConfig, c)); err != nil {
return awsConfig, fmt.Errorf("error validating provider credentials: %w", err)
}
}
return awsConfig, nil
}
// Adapted from the per-service-client `resolveRetryer()` functions in the AWS SDK for Go v2
// e.g. https://github.com/aws/aws-sdk-go-v2/blob/main/service/accessanalyzer/api_client.go
// Currently only supports "standard" retry mode
func resolveRetryer(ctx context.Context, awsConfig *aws.Config) {
var standardOptions []func(*retry.StandardOptions)
if v, found, _ := awsconfig.GetRetryMaxAttempts(ctx, awsConfig.ConfigSources); found && v != 0 {
standardOptions = append(standardOptions, func(so *retry.StandardOptions) {
so.MaxAttempts = v
})
}
awsConfig.Retryer = func() aws.Retryer {
return &networkErrorShortcutter{
RetryerV2: retry.NewStandard(standardOptions...),
}
}
}
// networkErrorShortcutter is used to enable networking error shortcutting
type networkErrorShortcutter struct {
aws.RetryerV2
}
// We're misusing RetryDelay here, since this is the only function that takes the attempt count
func (r *networkErrorShortcutter) RetryDelay(attempt int, err error) (time.Duration, error) {
if attempt >= constants.MaxNetworkRetryCount {
var netOpErr *net.OpError
if errors.As(err, &netOpErr) {
// It's disappointing that we have to do string matching here, rather than being able to using `errors.Is()` or even strings exported by the Go `net` package
if strings.Contains(netOpErr.Error(), "no such host") || strings.Contains(netOpErr.Error(), "connection refused") {
log.Printf("[WARN] Disabling retries after next request due to networking issue: %s", err)
return 0, &retry.MaxAttemptsError{
Attempt: attempt,
Err: err,
}
}
}
}
return r.RetryerV2.RetryDelay(attempt, err)
}
func GetAwsAccountIDAndPartition(ctx context.Context, awsConfig aws.Config, c *Config) (string, string, error) {
if !c.SkipCredsValidation {
stsClient := stsClient(awsConfig, c)
accountID, partition, err := getAccountIDAndPartitionFromSTSGetCallerIdentity(ctx, stsClient)
if err != nil {
return "", "", fmt.Errorf("error validating provider credentials: %w", err)
}
return accountID, partition, nil
}
if !c.SkipRequestingAccountId {
credentialsProviderName := ""
if credentialsValue, err := awsConfig.Credentials.Retrieve(context.Background()); err == nil {
credentialsProviderName = credentialsValue.Source
}
iamClient := iamClient(awsConfig, c)
stsClient := stsClient(awsConfig, c)
accountID, partition, err := getAccountIDAndPartition(ctx, iamClient, stsClient, credentialsProviderName)
if err == nil {
return accountID, partition, nil
}
return "", "", fmt.Errorf(
"AWS account ID not previously found and failed retrieving via all available methods. "+
"See https://www.terraform.io/docs/providers/aws/index.html#skip_requesting_account_id for workaround and implications. "+
"Errors: %w", err)
}
return "", endpoints.PartitionForRegion(awsConfig.Region), nil
}
func commonLoadOptions(c *Config) ([]func(*config.LoadOptions) error, error) {
httpClient, err := defaultHttpClient(c)
if err != nil {
return nil, err
}
apiOptions := make([]func(*middleware.Stack) error, 0)
if c.APNInfo != nil {
apiOptions = append(apiOptions, func(stack *middleware.Stack) error {
// Because the default User-Agent middleware prepends itself to the contents of the User-Agent header,
// we have to run after it and also prepend our custom User-Agent
return stack.Build.Add(apnUserAgentMiddleware(*c.APNInfo), middleware.After)
})
}
if len(c.UserAgent) > 0 {
apiOptions = append(apiOptions, awsmiddleware.AddUserAgentKey(c.UserAgent.BuildUserAgentString()))
}
if v := os.Getenv(constants.AppendUserAgentEnvVar); v != "" {
log.Printf("[DEBUG] Using additional User-Agent Info: %s", v)
apiOptions = append(apiOptions, awsmiddleware.AddUserAgentKey(v))
}
loadOptions := []func(*config.LoadOptions) error{
config.WithRegion(c.Region),
config.WithHTTPClient(httpClient),
config.WithAPIOptions(apiOptions),
config.WithEC2IMDSClientEnableState(c.EC2MetadataServiceEnableState),
}
if c.MaxRetries != 0 {
loadOptions = append(
loadOptions,
config.WithRetryMaxAttempts(c.MaxRetries),
)
}
if !c.SuppressDebugLog {
loadOptions = append(
loadOptions,
config.WithClientLogMode(aws.LogRequestWithBody|aws.LogResponseWithBody|aws.LogRetries),
config.WithLogger(debugLogger{}),
)
}
sharedCredentialsFiles, err := c.ResolveSharedCredentialsFiles()
if err != nil {
return nil, err
}
if len(sharedCredentialsFiles) > 0 {
loadOptions = append(
loadOptions,
config.WithSharedCredentialsFiles(sharedCredentialsFiles),
)
}
sharedConfigFiles, err := c.ResolveSharedConfigFiles()
if err != nil {
return nil, err
}
if len(sharedConfigFiles) > 0 {
loadOptions = append(
loadOptions,
config.WithSharedConfigFiles(sharedConfigFiles),
)
}
if c.CustomCABundle != "" {
reader, err := c.CustomCABundleReader()
if err != nil {
return nil, err
}
loadOptions = append(loadOptions,
config.WithCustomCABundle(reader),
)
}
if c.EC2MetadataServiceEndpoint != "" {
loadOptions = append(loadOptions,
config.WithEC2IMDSEndpoint(c.EC2MetadataServiceEndpoint),
)
}
if c.EC2MetadataServiceEndpointMode != "" {
var endpointMode imds.EndpointModeState
err := endpointMode.SetFromString(c.EC2MetadataServiceEndpointMode)
if err != nil {
return nil, err
}
loadOptions = append(loadOptions,
config.WithEC2IMDSEndpointMode(endpointMode),
)
}
// This should not be needed, but https://github.com/aws/aws-sdk-go-v2/issues/1398
if c.EC2MetadataServiceEnableState == imds.ClientEnabled {
os.Setenv("AWS_EC2_METADATA_DISABLED", "false")
} else if c.EC2MetadataServiceEnableState == imds.ClientDisabled {
os.Setenv("AWS_EC2_METADATA_DISABLED", "true")
}
if c.UseDualStackEndpoint {
loadOptions = append(loadOptions,
config.WithUseDualStackEndpoint(aws.DualStackEndpointStateEnabled),
)
}
if c.UseFIPSEndpoint {
loadOptions = append(loadOptions,
config.WithUseFIPSEndpoint(aws.FIPSEndpointStateEnabled),
)
}
return loadOptions, nil
}