Skip to content

Commit a40b678

Browse files
authored
Search all environments for AWS credential defaults (#1044)
1 parent 52566ff commit a40b678

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

cli/cmd/lib_cli_config.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,25 @@ func getEnvConfigDefaults(envName string) cliconfig.Environment {
651651
if defaults.AWSRegion == nil && os.Getenv("AWS_REGION") != "" {
652652
defaults.AWSRegion = pointer.String(os.Getenv("AWS_REGION"))
653653
}
654+
655+
if defaults.AWSAccessKeyID == nil && defaults.AWSSecretAccessKey == nil {
656+
// search other envs for credentials (favoring the env named "aws", or the last entry in the list)
657+
regionWasNil := defaults.AWSRegion == nil
658+
cliConfig, _ := readCLIConfig()
659+
for _, env := range cliConfig.Environments {
660+
if env.AWSAccessKeyID != nil && env.AWSSecretAccessKey != nil {
661+
defaults.AWSAccessKeyID = env.AWSAccessKeyID
662+
defaults.AWSSecretAccessKey = env.AWSSecretAccessKey
663+
}
664+
if regionWasNil && env.AWSRegion != nil {
665+
defaults.AWSRegion = env.AWSRegion
666+
}
667+
if env.Name == "aws" {
668+
break // favor the env named "aws"
669+
}
670+
}
671+
}
672+
654673
if defaults.AWSRegion == nil {
655674
defaults.AWSRegion = pointer.String("us-east-1")
656675
}
@@ -695,6 +714,7 @@ func configureEnv(envName string, fieldsToSkipPrompt cliconfig.Environment) (cli
695714
}
696715

697716
defaults := getEnvConfigDefaults(env.Name)
717+
698718
switch env.Provider {
699719
case types.LocalProviderType:
700720
err := promptLocalEnv(&env, defaults)

0 commit comments

Comments
 (0)