From 046abf83f283a02fb42ecb498c8af6c86262a116 Mon Sep 17 00:00:00 2001 From: AWS SDK for Go v2 automation user Date: Fri, 6 Oct 2023 18:17:55 +0000 Subject: [PATCH 1/2] fix: fail to load config if configured profile doesn't exist --- .../bde54e56c1d44544baef8c6afdfd68d0.json | 8 ++++++++ config/config.go | 17 ++++++++++++++++- config/resolve_credentials_test.go | 2 -- 3 files changed, 24 insertions(+), 3 deletions(-) create mode 100644 .changelog/bde54e56c1d44544baef8c6afdfd68d0.json 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..b5a4c5d6de7 100644 --- a/config/config.go +++ b/config/config.go @@ -2,6 +2,7 @@ package config import ( "context" + "os" "github.com/aws/aws-sdk-go-v2/aws" ) @@ -190,7 +191,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 +203,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{}), } From 73f249b3cc3c244f35070a9b51a58831c3c0d93d Mon Sep 17 00:00:00 2001 From: Luc Talatinian Date: Tue, 10 Oct 2023 15:27:15 -0400 Subject: [PATCH 2/2] remove now-unused defaultLoaders --- config/config.go | 8 -------- 1 file changed, 8 deletions(-) diff --git a/config/config.go b/config/config.go index b5a4c5d6de7..bf26eab9a9a 100644 --- a/config/config.go +++ b/config/config.go @@ -7,14 +7,6 @@ import ( "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. //