Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fail to load config if configured profile doesn't exist #2309

Merged
merged 2 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

am i missing something here? the behavior looks the same: its still using the loadSharedConfigIgnoreNotExist which swallows the error (i.e. doesnt return an error message).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless the check on 204 is true though (which means a profile was specified). That's the change here.

}

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