Skip to content

Commit bebc232

Browse files
authored
fix: fail to load config if configured profile doesn't exist (#2309)
1 parent 5de4674 commit bebc232

File tree

3 files changed

+24
-11
lines changed

3 files changed

+24
-11
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"id": "bde54e56-c1d4-4544-baef-8c6afdfd68d0",
3+
"type": "bugfix",
4+
"description": "Fail to load config if an explicitly provided profile doesn't exist.",
5+
"modules": [
6+
"config"
7+
]
8+
}

config/config.go

+16-9
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,11 @@ package config
22

33
import (
44
"context"
5+
"os"
56

67
"github.com/aws/aws-sdk-go-v2/aws"
78
)
89

9-
// defaultLoaders are a slice of functions that will read external configuration
10-
// sources for configuration values. These values are read by the AWSConfigResolvers
11-
// using interfaces to extract specific information from the external configuration.
12-
var defaultLoaders = []loader{
13-
loadEnvConfig,
14-
loadSharedConfigIgnoreNotExist,
15-
}
16-
1710
// defaultAWSConfigResolvers are a slice of functions that will resolve external
1811
// configuration values into AWS configuration values.
1912
//
@@ -190,7 +183,7 @@ func LoadDefaultConfig(ctx context.Context, optFns ...func(*LoadOptions) error)
190183
// assign Load Options to configs
191184
var cfgCpy = configs{options}
192185

193-
cfgCpy, err = cfgCpy.AppendFromLoaders(ctx, defaultLoaders)
186+
cfgCpy, err = cfgCpy.AppendFromLoaders(ctx, resolveConfigLoaders(&options))
194187
if err != nil {
195188
return aws.Config{}, err
196189
}
@@ -202,3 +195,17 @@ func LoadDefaultConfig(ctx context.Context, optFns ...func(*LoadOptions) error)
202195

203196
return cfg, nil
204197
}
198+
199+
func resolveConfigLoaders(options *LoadOptions) []loader {
200+
loaders := make([]loader, 2)
201+
loaders[0] = loadEnvConfig
202+
203+
// specification of a profile should cause a load failure if it doesn't exist
204+
if os.Getenv(awsProfileEnvVar) != "" || options.SharedConfigProfile != "" {
205+
loaders[1] = loadSharedConfig
206+
} else {
207+
loaders[1] = loadSharedConfigIgnoreNotExist
208+
}
209+
210+
return loaders
211+
}

config/resolve_credentials_test.go

-2
Original file line numberDiff line numberDiff line change
@@ -577,8 +577,6 @@ func TestResolveCredentialsIMDSClient(t *testing.T) {
577577
opts := []func(*LoadOptions) error{
578578
WithRetryer(func() aws.Retryer { return aws.NopRetryer{} }),
579579
WithHTTPClient(httpClient),
580-
// separate from the local config, should it exist - the default loader will ignore nonexistent profiles
581-
WithSharedConfigProfile(" "),
582580
WithSharedConfigFiles([]string{}),
583581
}
584582

0 commit comments

Comments
 (0)