Skip to content

Commit f5d22f1

Browse files
committed
Adding support for profiles in credential files.
1 parent 933b750 commit f5d22f1

2 files changed

Lines changed: 41 additions & 1 deletion

File tree

awsSession/awsSession.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,47 @@
11
package awsSession
22

33
import (
4+
"fmt"
45
"os"
56

7+
"github.com/aws/aws-sdk-go/aws"
8+
"github.com/aws/aws-sdk-go/aws/credentials"
69
"github.com/aws/aws-sdk-go/aws/session"
710
)
811

12+
var (
13+
credsPath = ".aws/credentials"
14+
)
15+
916
// New will create and return an AWS Session
1017
func New() (*session.Session, error) {
1118
return session.NewSession()
1219
}
1320

21+
// NewWithOptions will create and return an AWS Session with supplied options
22+
func NewWithOptions(profile, credsFile string) (*session.Session, error) {
23+
24+
filePath := credsFile
25+
if filePath == "" {
26+
home, ok := os.LookupEnv("HOME")
27+
if !ok {
28+
return nil, fmt.Errorf("Can't find home environment variable. Looking for credentials file")
29+
}
30+
filePath = home + "/" + credsPath
31+
_, err := os.Stat(filePath)
32+
if err != nil {
33+
return nil, fmt.Errorf("Failed to lookup credentials file. Error: %s", err)
34+
}
35+
}
36+
37+
creds := credentials.NewSharedCredentials(filePath, profile)
38+
config := aws.NewConfig().WithCredentials(creds)
39+
return session.NewSessionWithOptions(session.Options{
40+
Config: *config,
41+
SharedConfigState: session.SharedConfigEnable,
42+
})
43+
}
44+
1445
// SetRegion publish the supplied region if there is one given
1546
func SetRegion(region string) error {
1647
// Set what was passed in

main.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"fmt"
88
"log"
99

10+
"github.com/aws/aws-sdk-go/aws/session"
1011
"github.com/morfien101/go-parameterStoreReader/awsSession"
1112
parameterstore "github.com/morfien101/go-parameterStoreReader/parameterStore"
1213
)
@@ -24,6 +25,8 @@ var (
2425
flagDecrypt = flag.Bool("decrypt", false, "Request decrypted keys")
2526
flagAccessKey = flag.String("access-key", "", "Access key for AWS API")
2627
flagSecretKey = flag.String("secret-key", "", "Secret key for AWS API")
28+
flagProfile = flag.String("profile", "", "AWS Profile to use")
29+
flagCredsFile = flag.String("config-file", "", "AWS Config file override, only valid with -profile")
2730
flagRegion = flag.String("region", "", "Region for AWS API")
2831
flagHelp = flag.Bool("h", false, "Help menu")
2932
flagVersion = flag.Bool("v", false, "Show Version")
@@ -56,7 +59,13 @@ func main() {
5659
log.Fatal("Failed to set environment variable AWS_REGION for access to AWS")
5760
}
5861

59-
session, err := awsSession.New()
62+
var session *session.Session
63+
var err error
64+
if *flagProfile == "" {
65+
session, err = awsSession.New()
66+
} else {
67+
session, err = awsSession.NewWithOptions(*flagProfile, *flagCredsFile)
68+
}
6069
if err != nil {
6170
log.Fatalf("Failed to create AWS Session. Error: %s", err)
6271
}

0 commit comments

Comments
 (0)