-
-
Notifications
You must be signed in to change notification settings - Fork 153
Expand file tree
/
Copy pathaws_utils.go
More file actions
179 lines (147 loc) · 5.38 KB
/
aws_utils.go
File metadata and controls
179 lines (147 loc) · 5.38 KB
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
package aws_utils
import (
"context"
"fmt"
"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/stscreds"
"github.com/aws/aws-sdk-go-v2/service/sts"
errUtils "github.com/cloudposse/atmos/errors"
log "github.com/cloudposse/atmos/pkg/logger"
"github.com/cloudposse/atmos/pkg/perf"
"github.com/cloudposse/atmos/pkg/schema"
)
// LoadAWSConfigWithAuth loads AWS config, preferring auth context if available.
/*
When authContext is provided, it uses the Atmos-managed credentials files and profile.
Otherwise, it falls back to standard AWS SDK credential resolution.
Standard AWS SDK credential resolution order:
Environment variables:
AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY
AWS_SESSION_TOKEN (optional, for temporary credentials)
Shared credentials file:
Typically at ~/.aws/credentials
Controlled by:
AWS_PROFILE (defaults to default)
AWS_SHARED_CREDENTIALS_FILE
Shared config file:
Typically at ~/.aws/config
Also supports named profiles and region settings
Amazon EC2 Instance Metadata Service (IMDS):
If running on EC2 or ECS
Uses IAM roles attached to the instance/task
Web Identity Token credentials:
When AWS_WEB_IDENTITY_TOKEN_FILE and AWS_ROLE_ARN are set (e.g., in EKS)
SSO credentials (if configured)
Custom credential sources:
Provided programmatically using config.WithCredentialsProvider(...)
*/
func LoadAWSConfigWithAuth(
ctx context.Context,
region string,
roleArn string,
assumeRoleDuration time.Duration,
authContext *schema.AWSAuthContext,
) (aws.Config, error) {
defer perf.Track(nil, "aws_utils.LoadAWSConfigWithAuth")()
var cfgOpts []func(*config.LoadOptions) error
// If auth context is provided, use Atmos-managed credentials.
if authContext != nil {
log.Debug("Using Atmos auth context for AWS SDK",
"profile", authContext.Profile,
"credentials", authContext.CredentialsFile,
"config", authContext.ConfigFile,
)
// Set custom credential and config file paths.
// This overrides the default ~/.aws/credentials and ~/.aws/config.
cfgOpts = append(cfgOpts,
config.WithSharedCredentialsFiles([]string{authContext.CredentialsFile}),
config.WithSharedConfigFiles([]string{authContext.ConfigFile}),
config.WithSharedConfigProfile(authContext.Profile),
)
// Use region from auth context if not explicitly provided.
if region == "" && authContext.Region != "" {
region = authContext.Region
}
} else {
log.Debug("Using standard AWS SDK credential resolution (no auth context provided)")
}
// Set region if provided.
if region != "" {
log.Debug("Using explicit region", "region", region)
cfgOpts = append(cfgOpts, config.WithRegion(region))
}
// Load base config.
log.Debug("Loading AWS SDK config", "num_options", len(cfgOpts))
baseCfg, err := config.LoadDefaultConfig(ctx, cfgOpts...)
if err != nil {
log.Debug("Failed to load AWS config", "error", err)
return aws.Config{}, fmt.Errorf("%w: %w", errUtils.ErrLoadAwsConfig, err)
}
log.Debug("Successfully loaded AWS SDK config", "region", baseCfg.Region)
// Conditionally assume role if specified.
if roleArn != "" {
log.Debug("Assuming role", "ARN", roleArn)
stsClient := sts.NewFromConfig(baseCfg)
creds := stscreds.NewAssumeRoleProvider(stsClient, roleArn, func(o *stscreds.AssumeRoleOptions) {
o.Duration = assumeRoleDuration
})
cfgOpts = append(cfgOpts, config.WithCredentialsProvider(aws.NewCredentialsCache(creds)))
// Reload full config with assumed role credentials.
return config.LoadDefaultConfig(ctx, cfgOpts...)
}
return baseCfg, nil
}
// LoadAWSConfig loads AWS config using standard AWS SDK credential resolution.
// This is a wrapper around LoadAWSConfigWithAuth for backward compatibility.
// For new code that needs Atmos auth support, use LoadAWSConfigWithAuth instead.
func LoadAWSConfig(ctx context.Context, region string, roleArn string, assumeRoleDuration time.Duration) (aws.Config, error) {
defer perf.Track(nil, "aws_utils.LoadAWSConfig")()
return LoadAWSConfigWithAuth(ctx, region, roleArn, assumeRoleDuration, nil)
}
// AWSCallerIdentityResult holds the result of GetAWSCallerIdentity.
type AWSCallerIdentityResult struct {
Account string
Arn string
UserID string
Region string
}
// GetAWSCallerIdentity retrieves AWS caller identity using STS GetCallerIdentity API.
// Returns account ID, ARN, user ID, and region.
// This function keeps AWS SDK STS imports contained within aws_utils package.
func GetAWSCallerIdentity(
ctx context.Context,
region string,
roleArn string,
assumeRoleDuration time.Duration,
authContext *schema.AWSAuthContext,
) (*AWSCallerIdentityResult, error) {
defer perf.Track(nil, "aws_utils.GetAWSCallerIdentity")()
// Load AWS config.
cfg, err := LoadAWSConfigWithAuth(ctx, region, roleArn, assumeRoleDuration, authContext)
if err != nil {
return nil, err
}
// Create STS client and get caller identity.
stsClient := sts.NewFromConfig(cfg)
output, err := stsClient.GetCallerIdentity(ctx, &sts.GetCallerIdentityInput{})
if err != nil {
return nil, fmt.Errorf("%w: %w", errUtils.ErrAwsGetCallerIdentity, err)
}
result := &AWSCallerIdentityResult{
Region: cfg.Region,
}
// Extract values from pointers.
if output.Account != nil {
result.Account = *output.Account
}
if output.Arn != nil {
result.Arn = *output.Arn
}
if output.UserId != nil {
result.UserID = *output.UserId
}
return result, nil
}