@@ -3,7 +3,7 @@ package main
3
3
import (
4
4
"bytes"
5
5
"context"
6
- "crypto/md5 "
6
+ "crypto/sha1 "
7
7
"encoding/hex"
8
8
"encoding/json"
9
9
"fmt"
@@ -22,25 +22,25 @@ import (
22
22
)
23
23
24
24
type AWSSSOCredential struct {
25
- StartURL string `json:"startUrl"`
26
- Region string `json:"region"`
27
- AccessToken string `json:"accessToken"`
28
- ExpiresAt AWSTime `json:"expiresAt"`
25
+ StartURL string `json:"startUrl"`
26
+ Region string `json:"region"`
27
+ AccessToken string `json:"accessToken"`
28
+ ExpiresAt AWSTime `json:"expiresAt"`
29
29
}
30
30
31
31
type CredentialProcessJson struct {
32
- Version int `json:"Version"`
33
- AccessKeyID string `json:"AccessKeyId"`
34
- SecretAccessKey string `json:"SecretAccessKey"`
35
- SessionToken string `json:"SessionToken"`
36
- Expiration AWSTime `json:"Expiration"`
32
+ Version int `json:"Version"`
33
+ AccessKeyID string `json:"AccessKeyId"`
34
+ SecretAccessKey string `json:"SecretAccessKey"`
35
+ SessionToken string `json:"SessionToken"`
36
+ Expiration AWSTime `json:"Expiration"`
37
37
}
38
38
39
39
type Profile struct {
40
40
SSOAccountID string
41
- SSORegion string
42
- SSORoleName string
43
- SSOStartUrl string
41
+ SSORegion string
42
+ SSORoleName string
43
+ SSOStartUrl string
44
44
}
45
45
46
46
type AWSTime struct {
@@ -60,8 +60,7 @@ func (it AWSTime) MarshalJSON() ([]byte, error) {
60
60
return []byte (fmt .Sprintf ("\" %sZ\" " , it .Time .UTC ().Format ("2006-01-02T15:04:05" ))), nil
61
61
}
62
62
63
-
64
- func main (){
63
+ func main () {
65
64
zerolog .SetGlobalLevel (zerolog .InfoLevel )
66
65
_ , ok := os .LookupEnv ("DEBUG" )
67
66
if ok {
@@ -144,14 +143,9 @@ func writeCachedFile(awsSsoCachePath, awsSSOProfileName string, credentialProces
144
143
func getCachedFile (awsSsoCachePath , awsSSOProfileName string ) (* CredentialProcessJson , error ) {
145
144
cachedFileName := getCachedFileName (awsSSOProfileName )
146
145
cachedFilePath := filepath .Join (awsSsoCachePath , cachedFileName )
147
- fInfo , err := os .Stat (cachedFilePath )
148
- if os .IsNotExist (err ) {
149
- log .Debug ().Str ("path" , cachedFilePath ).Msg ("cache file doesn't exist" )
150
- return nil , nil
151
- }
152
146
var credentialProcessJson CredentialProcessJson
153
147
154
- bytes , err := readJsonFile ( awsSsoCachePath , fInfo )
148
+ bytes , err := ioutil . ReadFile ( cachedFilePath )
155
149
if err != nil {
156
150
return nil , err
157
151
}
@@ -168,8 +162,8 @@ func getCachedFile(awsSsoCachePath, awsSSOProfileName string) (*CredentialProces
168
162
}
169
163
170
164
func getCachedFileName (awsSSOProfileName string ) string {
171
- md5ProfileName := md5 .Sum ([]byte (awsSSOProfileName ))
172
- return fmt .Sprintf ("aws-sso-fetcher-%s.json" , hex .EncodeToString (md5ProfileName [:]))
165
+ profileNameSha1 := sha1 .Sum ([]byte (awsSSOProfileName ))
166
+ return fmt .Sprintf ("aws-sso-fetcher-%s.json" , hex .EncodeToString (profileNameSha1 [:]))
173
167
}
174
168
175
169
func printProfile (credentialProcessJson CredentialProcessJson ) {
@@ -220,68 +214,25 @@ func getSsoRoleCredentials(profile Profile, awsSSOCredential AWSSSOCredential) (
220
214
func getSsoCachedLogin (profile Profile , ssoCachePath string ) (AWSSSOCredential , error ) {
221
215
var awsSSOCredential AWSSSOCredential
222
216
223
- filesToInvestigate , err := ioutil .ReadDir (ssoCachePath )
217
+ bs := sha1 .Sum ([]byte (profile .SSOStartUrl ))
218
+ cachedFilePath := filepath .Join (ssoCachePath , fmt .Sprintf ("%x.json" , bs ))
219
+
220
+ bytes , err := ioutil .ReadFile (cachedFilePath )
224
221
if err != nil {
225
222
return awsSSOCredential , err
226
223
}
227
- log .Debug ().Int ("fileCount" , len (filesToInvestigate )).Msg ("found files" )
228
- for _ , f := range filesToInvestigate {
229
- log .Debug ().Str ("path" , f .Name ()).Msg ("looking at file" )
230
- if f .IsDir () {
231
- log .Debug ().Bool ("isDir" , f .IsDir ()).Msg ("found dir, not opening" )
232
- continue
233
- }
234
-
235
- bytes , err := readJsonFile (ssoCachePath , f )
236
- if err != nil {
237
- return awsSSOCredential , err
238
- }
239
-
240
- err = json .Unmarshal (bytes , & awsSSOCredential )
241
- if err != nil {
242
- return awsSSOCredential , err
243
- }
244
-
245
- if awsSSOCredential .StartURL != profile .SSOStartUrl {
246
- log .Debug ().
247
- Str ("file" , f .Name ()).
248
- Str ("JsonStartURL" , awsSSOCredential .StartURL ).
249
- Str ("SSOStartURL" , profile .SSOStartUrl ).
250
- Msg ("start urls did not match" )
251
- continue
252
- }
253
- if awsSSOCredential .Region != profile .SSORegion {
254
- log .Debug ().
255
- Str ("file" , f .Name ()).
256
- Str ("JsonRegion" , awsSSOCredential .Region ).
257
- Str ("SSORegion" , profile .SSORegion ).
258
- Msg ("regions did not match" )
259
- continue
260
- }
261
- if time .Now ().After (awsSSOCredential .ExpiresAt .Time ) {
262
- log .Debug ().Str ("ExpiresAt" , awsSSOCredential .ExpiresAt .String ()).Msg ("credential is expired" )
263
- continue
264
- }
265
-
266
- log .Debug ().Str ("file" , f .Name ()).Msg ("found a file that will work" )
267
- return awsSSOCredential , nil
268
- }
269
- return AWSSSOCredential {}, fmt .Errorf ("found no credential to use to create creds, log back into AWS SSO" )
270
- }
271
224
272
- func readJsonFile (ssoCachePath string , f os.FileInfo ) ([]byte , error ) {
273
- jsonFile , err := os .Open (filepath .Join (ssoCachePath , f .Name ()))
225
+ err = json .Unmarshal (bytes , & awsSSOCredential )
274
226
if err != nil {
275
- return nil , err
227
+ return awsSSOCredential , err
276
228
}
277
- defer func () {
278
- err := jsonFile .Close ()
279
- if err != nil {
280
- log .Error ().Err (err ).Msg ("yo I couldn't close a file, that's super scary" )
281
- }
282
- }()
283
- byteValue , _ := ioutil .ReadAll (jsonFile )
284
- return byteValue , nil
229
+
230
+ if time .Now ().After (awsSSOCredential .ExpiresAt .Time ) {
231
+ log .Debug ().Str ("ExpiresAt" , awsSSOCredential .ExpiresAt .String ()).Msg ("credential is expired" )
232
+ return awsSSOCredential , fmt .Errorf ("Credentials expired" )
233
+ }
234
+
235
+ return awsSSOCredential , nil
285
236
}
286
237
287
238
func parseProfile (section * ini.Section ) (Profile , error ) {
@@ -294,7 +245,7 @@ func parseProfile(section *ini.Section) (Profile, error) {
294
245
log .Debug ().Str ("id" , profileAccountId .String ()).Msg ("found account id" )
295
246
profile .SSOAccountID = profileAccountId .String ()
296
247
297
- profileRegionKey , err := section .GetKey ("sso_region" )
248
+ profileRegionKey , err := section .GetKey ("sso_region" )
298
249
if err != nil {
299
250
return profile , fmt .Errorf ("error getting sso_region from profile: %w" , err )
300
251
}
0 commit comments