-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathcredentials.go
239 lines (197 loc) · 7.55 KB
/
credentials.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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package awsbase
import (
"context"
"fmt"
"os"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials/stscreds"
"github.com/aws/aws-sdk-go-v2/service/sts/types"
"github.com/hashicorp/aws-sdk-go-base/v2/diag"
"github.com/hashicorp/aws-sdk-go-base/v2/logging"
)
const (
configSourceProviderConfig = "provider"
configSourceEnvironmentVariable = "envvar"
)
func getCredentialsProvider(ctx context.Context, c *Config) (aws.CredentialsProvider, string, diag.Diagnostics) {
// This function will need to exist for any authentication methods that call STS until the providers use a reasonable default `MaxRetries`.
// Otherwise, retryable errors will cause the provider to appear to have frozen.
var diags diag.Diagnostics
logger := logging.RetrieveLogger(ctx)
loadOptions, err := commonLoadOptions(ctx, c)
if err != nil {
return nil, "", diags.AddSimpleError(err)
}
loadOptions = append(
loadOptions,
// The endpoint resolver is added here instead of in commonLoadOptions() so that it
// is not included in the aws.Config returned to the caller
config.WithEndpointResolverWithOptions(credentialsEndpointResolver(ctx, c)),
)
envConfig, err := config.NewEnvConfig()
if err != nil {
return nil, "", diags.AddSimpleError(err)
}
if c.Profile != "" && os.Getenv("AWS_ACCESS_KEY_ID") != "" && os.Getenv("AWS_SECRET_ACCESS_KEY") != "" {
diags.AddWarning("Configuration conflict detected",
`A Profile was specified along with the environment variables "AWS_ACCESS_KEY_ID" and "AWS_SECRET_ACCESS_KEY". `+
`The Profile is now used instead of the environment variable credentials. This may lead to unexpected behavior.`)
}
if profile := c.Profile; profile != "" {
logger.Debug(ctx, "Setting profile", map[string]any{
"tf_aws.profile": profile,
"tf_aws.profile.source": configSourceProviderConfig,
})
loadOptions = append(
loadOptions,
config.WithSharedConfigProfile(c.Profile),
)
} else if profile := envConfig.SharedConfigProfile; profile != "" {
logger.Debug(ctx, "Using profile", map[string]any{
"tf_aws.profile": profile,
"tf_aws.profile.source": configSourceEnvironmentVariable,
})
}
logger.Debug(ctx, "Loading configuration")
cfg, err := config.LoadDefaultConfig(ctx, loadOptions...)
if err != nil {
return nil, "", diags.AddSimpleError(err)
}
// This can probably be configured directly in commonLoadOptions() once
// https://github.com/aws/aws-sdk-go-v2/pull/1682 is merged
if c.AssumeRoleWithWebIdentity != nil {
if c.AssumeRoleWithWebIdentity.RoleARN == "" {
return nil, "", diags.AddError("Assume Role With Web Identity", "Role ARN was not set")
}
if c.AssumeRoleWithWebIdentity.WebIdentityToken == "" && c.AssumeRoleWithWebIdentity.WebIdentityTokenFile == "" {
return nil, "", diags.AddError("Assume Role With Web Identity", "One of WebIdentityToken, WebIdentityTokenFile must be set")
}
provider, d := webIdentityCredentialsProvider(ctx, cfg, c)
diags = diags.Append(d...)
if diags.HasError() {
return nil, "", diags
}
cfg.Credentials = provider
}
logger.Debug(ctx, "Retrieving credentials")
creds, err := cfg.Credentials.Retrieve(ctx)
if err != nil {
if c.Profile != "" && os.Getenv("AWS_ACCESS_KEY_ID") != "" && os.Getenv("AWS_SECRET_ACCESS_KEY") != "" {
err = fmt.Errorf(`A Profile was specified along with the environment variables "AWS_ACCESS_KEY_ID" and "AWS_SECRET_ACCESS_KEY". The Profile is now used instead of the environment variable credentials.
AWS Error: %w`, err)
}
return nil, "", diags.Append(c.NewNoValidCredentialSourcesError(err))
}
if len(c.AssumeRole) == 0 {
return cfg.Credentials, creds.Source, diags
}
logger.Info(ctx, "Retrieved initial credentials", map[string]any{
"tf_aws.credentials_source": creds.Source,
})
provider, d := assumeRoleCredentialsProvider(ctx, cfg, c)
diags = diags.Append(d...)
if diags.HasError() {
return nil, "", diags
}
return provider, creds.Source, diags
}
func webIdentityCredentialsProvider(ctx context.Context, awsConfig aws.Config, c *Config) (aws.CredentialsProvider, diag.Diagnostics) {
var diags diag.Diagnostics
logger := logging.RetrieveLogger(ctx)
ar := c.AssumeRoleWithWebIdentity
logger.Info(ctx, "Assuming IAM Role With Web Identity", map[string]any{
"tf_aws.assume_role_with_web_identity.role_arn": ar.RoleARN,
"tf_aws.assume_role_with_web_identity.session_name": ar.SessionName,
})
// awsConfig now has IMDS creds, remove them before initializing
// We probably shouldn't be resolving config before setting this up
awsConfig.Credentials = nil
client := stsClient(ctx, awsConfig, c)
appCreds := stscreds.NewWebIdentityRoleProvider(client, ar.RoleARN, ar, func(opts *stscreds.WebIdentityRoleOptions) {
opts.RoleSessionName = ar.SessionName
opts.Duration = ar.Duration
if ar.Policy != "" {
opts.Policy = aws.String(ar.Policy)
}
if len(ar.PolicyARNs) > 0 {
opts.PolicyARNs = getPolicyDescriptorTypes(ar.PolicyARNs)
}
})
if _, err := appCreds.Retrieve(ctx); err != nil {
return nil, diags.Append(c.NewCannotAssumeRoleWithWebIdentityError(err))
}
return aws.NewCredentialsCache(appCreds), diags
}
func assumeRoleCredentialsProvider(ctx context.Context, awsConfig aws.Config, c *Config) (aws.CredentialsProvider, diag.Diagnostics) {
var diags diag.Diagnostics
logger := logging.RetrieveLogger(ctx)
var creds aws.CredentialsProvider
total := len(c.AssumeRole)
for i, ar := range c.AssumeRole {
if ar.RoleARN == "" {
return nil, diags.AddError(
"Cannot assume IAM Role",
fmt.Sprintf("IAM Role ARN not set in assume role %d of %d", i+1, total),
)
}
logger.Info(ctx, "Assuming IAM Role", map[string]any{
"tf_aws.assume_role.index": i,
"tf_aws.assume_role.role_arn": ar.RoleARN,
"tf_aws.assume_role.session_name": ar.SessionName,
"tf_aws.assume_role.external_id": ar.ExternalID,
"tf_aws.assume_role.source_identity": ar.SourceIdentity,
})
// When assuming a role, we need to first authenticate the base credentials above, then assume the desired role
client := stsClient(ctx, awsConfig, c)
appCreds := stscreds.NewAssumeRoleProvider(client, ar.RoleARN, func(opts *stscreds.AssumeRoleOptions) {
opts.RoleSessionName = ar.SessionName
opts.Duration = ar.Duration
if ar.ExternalID != "" {
opts.ExternalID = aws.String(ar.ExternalID)
}
if ar.Policy != "" {
opts.Policy = aws.String(ar.Policy)
}
if len(ar.PolicyARNs) > 0 {
opts.PolicyARNs = getPolicyDescriptorTypes(ar.PolicyARNs)
}
if len(ar.Tags) > 0 {
var tags []types.Tag
for k, v := range ar.Tags {
tag := types.Tag{
Key: aws.String(k),
Value: aws.String(v),
}
tags = append(tags, tag)
}
opts.Tags = tags
}
if len(ar.TransitiveTagKeys) > 0 {
opts.TransitiveTagKeys = ar.TransitiveTagKeys
}
if ar.SourceIdentity != "" {
opts.SourceIdentity = aws.String(ar.SourceIdentity)
}
})
_, err := appCreds.Retrieve(ctx)
if err != nil {
return nil, diags.Append(newCannotAssumeRoleError(ar, err))
}
creds = aws.NewCredentialsCache(appCreds)
awsConfig.Credentials = creds
}
return creds, nil
}
func getPolicyDescriptorTypes(policyARNs []string) []types.PolicyDescriptorType {
var policyDescriptorTypes []types.PolicyDescriptorType
for _, policyARN := range policyARNs {
policyDescriptorType := types.PolicyDescriptorType{
Arn: aws.String(policyARN),
}
policyDescriptorTypes = append(policyDescriptorTypes, policyDescriptorType)
}
return policyDescriptorTypes
}