Skip to content

Commit 9495e9b

Browse files
committedAug 2, 2020
sign-in: make it work with all kinds of profiles
1 parent 806b38b commit 9495e9b

File tree

3 files changed

+26
-108
lines changed

3 files changed

+26
-108
lines changed
 

‎cmd/aws-sign-in/main.go

+11-106
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
11
package main
22

33
import (
4-
"bytes"
4+
"context"
55
"encoding/json"
66
"fmt"
77
"io/ioutil"
88
"log"
99
"net/http"
1010
"net/url"
11-
"os"
12-
"os/exec"
1311

12+
"github.com/aws/aws-sdk-go-v2/aws"
13+
"github.com/aws/aws-sdk-go-v2/aws/external"
1414
"github.com/docopt/docopt-go"
15-
"github.com/mattn/go-shellwords"
16-
"github.com/mbrtargeting/aws-cli-oidc/internal"
1715
"github.com/pkg/browser"
18-
"gopkg.in/ini.v1"
1916
)
2017

2118
func main() {
@@ -24,7 +21,7 @@ func main() {
2421
usage := `aws-sign-in.
2522
2623
Usage:
27-
aws-sign-in [<profile>]
24+
aws-sign-in
2825
aws-sign-in -h | --help
2926
3027
Options:
@@ -36,25 +33,20 @@ Options:
3633
}
3734

3835
var conf struct {
39-
Profile string `docopt:"<profile>"`
4036
}
4137
if err := arguments.Bind(&conf); err != nil {
4238
log.Fatalf("%v\n", err)
4339
}
4440

45-
profileName := conf.Profile
46-
if profileName == "" {
47-
profileName = os.Getenv("AWS_PROFILE")
48-
if profileName == "" {
49-
profileName = "default"
50-
}
41+
cfg, err := external.LoadDefaultAWSConfig()
42+
if err != nil {
43+
fmt.Printf("unable to load SDK config: %v\n", err)
5144
}
45+
cfg.Region = "eu-central-1"
5246

53-
configPath, credentialsPath := getAWSConfigPaths()
54-
55-
creds, err := runCredentialProcess(configPath, credentialsPath, profileName)
47+
creds, err := cfg.Credentials.Retrieve(context.Background())
5648
if err != nil {
57-
log.Fatalf("Error running the credential process: %v\n", err)
49+
log.Fatalf("Error retrieving credentials: %v\n", err)
5850
}
5951

6052
signinToken, err := getSignInToken(creds)
@@ -70,94 +62,7 @@ Options:
7062
fmt.Printf("If the browser didn't open, please visit the following url to sign in to the AWS console: %v\n", loginURL)
7163
}
7264

73-
func getAWSConfigPaths() (string, string) {
74-
var credentialsPath string
75-
var configPath string
76-
77-
awsConfigFile := os.Getenv("AWS_CONFIG_FILE")
78-
if awsConfigFile != "" {
79-
configPath = awsConfigFile
80-
}
81-
82-
awsCredentialsFile := os.Getenv("AWS_SHARED_CREDENTIALS_FILE")
83-
if awsCredentialsFile != "" {
84-
credentialsPath = awsCredentialsFile
85-
}
86-
87-
home, err := os.UserHomeDir()
88-
if err == nil {
89-
// only overwrite the paths if the respective env var was not set
90-
if configPath == "" {
91-
configPath = home + "/.aws/config"
92-
}
93-
if credentialsPath == "" {
94-
credentialsPath = home + "/.aws/credentials"
95-
}
96-
}
97-
98-
return configPath, credentialsPath
99-
}
100-
101-
func tryRunCredentialProcess(credentialProcess string) (*internal.AWSCredentialsJSON, error) {
102-
words, err := shellwords.Parse(credentialProcess)
103-
if err != nil || len(words) == 0 {
104-
return nil, fmt.Errorf("invalid credential_process entry")
105-
}
106-
cmd := exec.Command(words[0], words[1:]...)
107-
var out bytes.Buffer
108-
cmd.Stdout = &out
109-
if err := cmd.Run(); err != nil {
110-
return nil, fmt.Errorf("failed to run the credential process: %v", err)
111-
}
112-
113-
var creds internal.AWSCredentialsJSON
114-
err = json.Unmarshal(out.Bytes(), &creds)
115-
if err != nil {
116-
return nil, fmt.Errorf("error parsing credential process output: %v", err)
117-
}
118-
119-
return &creds, nil
120-
}
121-
122-
func findCredentialProcess(path string, profile string) (string, error) {
123-
cfg, err := ini.Load(path)
124-
if err != nil {
125-
return "", fmt.Errorf("failed to read file: %v", err)
126-
}
127-
128-
section, err := cfg.GetSection(profile)
129-
if err != nil {
130-
return "", fmt.Errorf("failed to read section: %v", err)
131-
}
132-
133-
key, err := section.GetKey("credential_process")
134-
if err != nil {
135-
return "", fmt.Errorf("failed to find credential_process entry: %v", err)
136-
}
137-
138-
return key.String(), nil
139-
}
140-
141-
func runCredentialProcess(configPath string, credentialsPath string, profile string) (*internal.AWSCredentialsJSON, error) {
142-
credentialProcess, err := findCredentialProcess(credentialsPath, profile)
143-
if err == nil {
144-
return tryRunCredentialProcess(credentialProcess)
145-
}
146-
147-
// yes, the ~/.aws/config has a different naming scheme for profile names (must be prefixed with "profile")
148-
configProfileName := profile
149-
if profile != "default" {
150-
configProfileName = fmt.Sprintf("profile %s", profile)
151-
}
152-
credentialProcess, err = findCredentialProcess(configPath, configProfileName)
153-
if err == nil {
154-
return tryRunCredentialProcess(credentialProcess)
155-
}
156-
157-
return nil, fmt.Errorf("not able to find a valid credential_process")
158-
}
159-
160-
func getSignInToken(creds *internal.AWSCredentialsJSON) (string, error) {
65+
func getSignInToken(creds aws.Credentials) (string, error) {
16166
reqJSON := fmt.Sprintf(`{"sessionId":"%s","sessionKey":"%s","sessionToken":"%s"}`,
16267
creds.AccessKeyID,
16368
creds.SecretAccessKey,

‎go.mod

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module github.com/mbrtargeting/aws-cli-oidc
22

33
require (
44
github.com/aws/aws-sdk-go v1.15.50
5+
github.com/aws/aws-sdk-go-v2 v0.24.0
56
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815
67
github.com/mattn/go-shellwords v1.0.10
78
github.com/natsukagami/go-input v0.0.0-20180603034138-38bb793e9754
@@ -10,9 +11,9 @@ require (
1011
github.com/stretchr/testify v1.5.1 // indirect
1112
github.com/zalando/go-keyring v0.0.0-20200121091418-667557018717
1213
golang.org/x/crypto v0.0.0-20200210222208-86ce3cb69678 // indirect
13-
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3
14+
golang.org/x/net v0.0.0-20200202094626-16171245cfb2
1415
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d
15-
gopkg.in/ini.v1 v1.52.0
16+
gopkg.in/ini.v1 v1.52.0 // indirect
1617
gopkg.in/yaml.v2 v2.2.2
1718
)
1819

‎go.sum

+12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
22
github.com/aws/aws-sdk-go v1.15.50 h1:3QZIQeiRXEMTs+w+BQ2c/3Fi9/Qz9KnipT/M7YI1ub4=
33
github.com/aws/aws-sdk-go v1.15.50/go.mod h1:mFuSZ37Z9YOHbQEwBWztmVzqXrEkub65tZoCYDt7FT0=
4+
github.com/aws/aws-sdk-go-v2 v0.24.0 h1:R0lL0krk9EyTI1vmO1ycoeceGZotSzCKO51LbPGq3rU=
5+
github.com/aws/aws-sdk-go-v2 v0.24.0/go.mod h1:2LhT7UgHOXK3UXONKI5OMgIyoQL6zTAw/jwIeX6yqzw=
46
github.com/danieljoos/wincred v1.0.2 h1:zf4bhty2iLuwgjgpraD2E9UbvO+fe54XXGJbOwe23fU=
57
github.com/danieljoos/wincred v1.0.2/go.mod h1:SnuYRW9lp1oJrZX/dXJqr0cPK5gYXqx3EJbmjhLdK9U=
68
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
@@ -9,14 +11,19 @@ github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815 h1:bWDMxwH3px2JBh
911
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815/go.mod h1:WwZ+bS3ebgob9U8Nd0kOddGdZWjyMGR8Wziv+TBNwSE=
1012
github.com/go-ini/ini v1.25.4 h1:Mujh4R/dH6YL8bxuISne3xX2+qcQ9p0IxKAP6ExWoUo=
1113
github.com/go-ini/ini v1.25.4/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8=
14+
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
1215
github.com/godbus/dbus v4.1.0+incompatible h1:WqqLRTsQic3apZUK9qC5sGNfXthmPXzUZ7nQPrNITa4=
1316
github.com/godbus/dbus v4.1.0+incompatible/go.mod h1:/YcGZj5zSblfDWMMoOzV4fas9FZnQYTkDnsGvmh2Grw=
1417
github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
1518
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
19+
github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4=
20+
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
1621
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8=
1722
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
1823
github.com/jmespath/go-jmespath v0.0.0-20160202185014-0b12d6b521d8 h1:12VvqtR6Aowv3l/EQUlocDHW2Cp4G9WJVH7uyH8QFJE=
1924
github.com/jmespath/go-jmespath v0.0.0-20160202185014-0b12d6b521d8/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
25+
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af h1:pmfjZENx5imkbgOkpRUYLnmbU7UEFbjtDA2hxJ1ichM=
26+
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
2027
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
2128
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
2229
github.com/mattn/go-shellwords v1.0.10 h1:Y7Xqm8piKOO3v10Thp7Z36h4FYFjt5xB//6XvOrs2Gw=
@@ -31,6 +38,7 @@ github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykE
3138
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
3239
github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s=
3340
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
41+
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
3442
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
3543
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
3644
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
@@ -45,6 +53,8 @@ golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73r
4553
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
4654
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3 h1:0GoQqolDA55aaLxZyTzK/Y2ePZzZTUrRacwib7cNsYQ=
4755
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
56+
golang.org/x/net v0.0.0-20200202094626-16171245cfb2 h1:CCH4IOTTfewWjGOlSp+zGcjutRKlBEZQ6wTn8ozI/nI=
57+
golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
4858
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d h1:TzXSXBo42m9gQenoE3b9BGiEpg5IG2JkU5FkPIawgtw=
4959
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
5060
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4 h1:YUO/7uOKsKeq9UokNS62b8FYywz3ker1l1vDZRCRefw=
@@ -55,6 +65,8 @@ golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7w
5565
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
5666
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
5767
golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
68+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
69+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
5870
google.golang.org/appengine v1.4.0 h1:/wp5JvzpHIxhs/dumFmF7BXTf3Z+dd4uXta4kVyO508=
5971
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
6072
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=

0 commit comments

Comments
 (0)
Please sign in to comment.