diff --git a/.changelog/bde54e56c1d44544baef8c6afdfd68d0.json b/.changelog/bde54e56c1d44544baef8c6afdfd68d0.json new file mode 100644 index 00000000000..32bac3b39fd --- /dev/null +++ b/.changelog/bde54e56c1d44544baef8c6afdfd68d0.json @@ -0,0 +1,8 @@ +{ + "id": "bde54e56-c1d4-4544-baef-8c6afdfd68d0", + "type": "bugfix", + "description": "Fail to load config if an explicitly provided profile doesn't exist.", + "modules": [ + "config" + ] +} \ No newline at end of file diff --git a/config/config.go b/config/config.go index 138f8e76da7..bf26eab9a9a 100644 --- a/config/config.go +++ b/config/config.go @@ -2,18 +2,11 @@ package config import ( "context" + "os" "github.com/aws/aws-sdk-go-v2/aws" ) -// defaultLoaders are a slice of functions that will read external configuration -// sources for configuration values. These values are read by the AWSConfigResolvers -// using interfaces to extract specific information from the external configuration. -var defaultLoaders = []loader{ - loadEnvConfig, - loadSharedConfigIgnoreNotExist, -} - // defaultAWSConfigResolvers are a slice of functions that will resolve external // configuration values into AWS configuration values. // @@ -190,7 +183,7 @@ func LoadDefaultConfig(ctx context.Context, optFns ...func(*LoadOptions) error) // assign Load Options to configs var cfgCpy = configs{options} - cfgCpy, err = cfgCpy.AppendFromLoaders(ctx, defaultLoaders) + cfgCpy, err = cfgCpy.AppendFromLoaders(ctx, resolveConfigLoaders(&options)) if err != nil { return aws.Config{}, err } @@ -202,3 +195,17 @@ func LoadDefaultConfig(ctx context.Context, optFns ...func(*LoadOptions) error) return cfg, nil } + +func resolveConfigLoaders(options *LoadOptions) []loader { + loaders := make([]loader, 2) + loaders[0] = loadEnvConfig + + // specification of a profile should cause a load failure if it doesn't exist + if os.Getenv(awsProfileEnvVar) != "" || options.SharedConfigProfile != "" { + loaders[1] = loadSharedConfig + } else { + loaders[1] = loadSharedConfigIgnoreNotExist + } + + return loaders +} diff --git a/config/resolve_credentials_test.go b/config/resolve_credentials_test.go index 1b155572c93..4288967e3cd 100644 --- a/config/resolve_credentials_test.go +++ b/config/resolve_credentials_test.go @@ -577,8 +577,6 @@ func TestResolveCredentialsIMDSClient(t *testing.T) { opts := []func(*LoadOptions) error{ WithRetryer(func() aws.Retryer { return aws.NopRetryer{} }), WithHTTPClient(httpClient), - // separate from the local config, should it exist - the default loader will ignore nonexistent profiles - WithSharedConfigProfile(" "), WithSharedConfigFiles([]string{}), }