Skip to content

Commit

Permalink
SharedConfigState (work in progress) (#86)
Browse files Browse the repository at this point in the history
* #74 using SharedConfigState to enable credential profiles and regions set in session config

* Using session region if noMetadata is set to true

* Reverting import formatting and !noMetadata log.Errorf

Co-authored-by: Lei Wang <[email protected]>
  • Loading branch information
mousedownmike and wangzlei authored Dec 29, 2020
1 parent 5d4c5ab commit 7494caf
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions pkg/conn/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func getRegionFromECSMetadata() string {
log.Errorf("Unable to open ECS metadata file: %v\n", err)
} else {
if err := json.Unmarshal(metadataFile, &dat); err != nil {
log.Errorf("Unable to read ECS metadatafile contents: %v", err)
log.Errorf("Unable to read ECS metadata file contents: %v", err)
} else {
taskArn = strings.Split(dat["TaskARN"].(string), ":")
region = taskArn[3]
Expand Down Expand Up @@ -156,9 +156,14 @@ func GetAWSConfigSession(cn connAttr, c *cfg.Config, roleArn string, region stri
log.Debugf("Fetch region %v from ec2 metadata", awsRegion)
}
}
} else {
es := getDefaultSession()
awsRegion = *es.Config.Region
log.Debugf("Fetched region %v from session config", awsRegion)

}
if awsRegion == "" {
log.Errorf("Cannot fetch region variable from config file, environment variables, ecs metadata, or ec2 metadata.")
log.Errorf("Cannot fetch region variable from config file, environment variables, ecs metadata, or ec2 metadata. Use local-mode to use the local session region.")
os.Exit(1)
}
s = cn.newAWSSession(roleArn, awsRegion)
Expand Down Expand Up @@ -286,7 +291,7 @@ func getSTSRegionalEndpoint(r string) string {
}

func getDefaultSession() *session.Session {
result, serr := session.NewSession()
result, serr := session.NewSessionWithOptions(session.Options{SharedConfigState: session.SharedConfigEnable})
if serr != nil {
log.Errorf("Error in creating session object : %v\n.", serr)
os.Exit(1)
Expand Down

0 comments on commit 7494caf

Please sign in to comment.