1
1
package main
2
2
3
3
import (
4
- "bytes "
4
+ "context "
5
5
"encoding/json"
6
6
"fmt"
7
7
"io/ioutil"
8
8
"log"
9
9
"net/http"
10
10
"net/url"
11
- "os"
12
- "os/exec"
13
11
12
+ "github.com/aws/aws-sdk-go-v2/aws"
13
+ "github.com/aws/aws-sdk-go-v2/aws/external"
14
14
"github.com/docopt/docopt-go"
15
- "github.com/mattn/go-shellwords"
16
- "github.com/mbrtargeting/aws-cli-oidc/internal"
17
15
"github.com/pkg/browser"
18
- "gopkg.in/ini.v1"
19
16
)
20
17
21
18
func main () {
@@ -24,7 +21,7 @@ func main() {
24
21
usage := `aws-sign-in.
25
22
26
23
Usage:
27
- aws-sign-in [<profile>]
24
+ aws-sign-in
28
25
aws-sign-in -h | --help
29
26
30
27
Options:
@@ -36,25 +33,20 @@ Options:
36
33
}
37
34
38
35
var conf struct {
39
- Profile string `docopt:"<profile>"`
40
36
}
41
37
if err := arguments .Bind (& conf ); err != nil {
42
38
log .Fatalf ("%v\n " , err )
43
39
}
44
40
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 )
51
44
}
45
+ cfg .Region = "eu-central-1"
52
46
53
- configPath , credentialsPath := getAWSConfigPaths ()
54
-
55
- creds , err := runCredentialProcess (configPath , credentialsPath , profileName )
47
+ creds , err := cfg .Credentials .Retrieve (context .Background ())
56
48
if err != nil {
57
- log .Fatalf ("Error running the credential process : %v\n " , err )
49
+ log .Fatalf ("Error retrieving credentials : %v\n " , err )
58
50
}
59
51
60
52
signinToken , err := getSignInToken (creds )
@@ -70,94 +62,7 @@ Options:
70
62
fmt .Printf ("If the browser didn't open, please visit the following url to sign in to the AWS console: %v\n " , loginURL )
71
63
}
72
64
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 ) {
161
66
reqJSON := fmt .Sprintf (`{"sessionId":"%s","sessionKey":"%s","sessionToken":"%s"}` ,
162
67
creds .AccessKeyID ,
163
68
creds .SecretAccessKey ,
0 commit comments