Skip to content

Commit

Permalink
fix: fail to load config if configured profile doesn't exist (#2309)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucix-aws authored Oct 11, 2023
1 parent 5de4674 commit bebc232
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
8 changes: 8 additions & 0 deletions .changelog/bde54e56c1d44544baef8c6afdfd68d0.json
Original file line number Diff line number Diff line change
@@ -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"
]
}
25 changes: 16 additions & 9 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
//
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
2 changes: 0 additions & 2 deletions config/resolve_credentials_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}),
}

Expand Down

0 comments on commit bebc232

Please sign in to comment.