Skip to content

Commit

Permalink
Fix to code for extracting AWS config.
Browse files Browse the repository at this point in the history
  • Loading branch information
gjbae1212 committed Sep 8, 2021
1 parent e43138a commit c9732fd
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ builds:
- all=-trimpath={{.Env.GOPATH}}
ldflags:
- -s -w
- -X main.gossmVersion=v1.4.2 # version
- -X main.gossmVersion=v1.4.3 # version
ignore:
- goos: windows
goarch: arm64
Expand Down
21 changes: 8 additions & 13 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,8 @@ func initConfig() {

// if shared cred file is exist.
if sharedCredFile != "" {
profile := _credential.awsProfile
if sharedCredFile == _credentialWithMFA { // gossm mfa profile is default.
profile = _defaultProfile
}
awsConfig, err := internal.NewSharedConfig(context.Background(),
profile,
_credential.awsProfile,
[]string{config.DefaultSharedConfigFilename()},
[]string{sharedCredFile},
)
Expand All @@ -148,12 +144,8 @@ func initConfig() {
}

cred, err := awsConfig.Credentials.Retrieve(context.Background())
if err != nil {
panicRed(internal.WrapError(err))
}

// delete invalid shared credential.
if cred.Expired() || cred.AccessKeyID == "" || cred.SecretAccessKey == "" {
if err != nil || cred.Expired() || cred.AccessKeyID == "" || cred.SecretAccessKey == "" {
color.Yellow("[Expire] gossm default mfa credential file %s", sharedCredFile)
os.Unsetenv("AWS_SHARED_CREDENTIALS_FILE")
} else {
Expand Down Expand Up @@ -227,6 +219,11 @@ func initConfig() {
if temporaryCredentials.Expired() || temporaryCredentials.AccessKeyID == "" || temporaryCredentials.SecretAccessKey == "" {
panicRed(internal.WrapError(fmt.Errorf("[err] not found credentials")))
}

// extract aws region if awsRegion is empty.
if awsRegion == "" {
awsRegion = temporaryConfig.Region
}
}
}

Expand All @@ -241,9 +238,7 @@ func initConfig() {

os.Setenv("AWS_SHARED_CREDENTIALS_FILE", _credentialWithTemporary)
awsConfig, err := internal.NewSharedConfig(context.Background(),
_credential.awsProfile,
[]string{config.DefaultSharedConfigFilename()},
[]string{_credentialWithTemporary},
_credential.awsProfile, []string{}, []string{_credentialWithTemporary},
)
if err != nil {
panicRed(internal.WrapError(err))
Expand Down

0 comments on commit c9732fd

Please sign in to comment.