@@ -3,7 +3,7 @@ package main
33import (
44 "bytes"
55 "context"
6- "crypto/md5 "
6+ "crypto/sha1 "
77 "encoding/hex"
88 "encoding/json"
99 "fmt"
@@ -22,25 +22,25 @@ import (
2222)
2323
2424type 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"`
2929}
3030
3131type 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"`
3737}
3838
3939type Profile struct {
4040 SSOAccountID string
41- SSORegion string
42- SSORoleName string
43- SSOStartUrl string
41+ SSORegion string
42+ SSORoleName string
43+ SSOStartUrl string
4444}
4545
4646type AWSTime struct {
@@ -60,8 +60,7 @@ func (it AWSTime) MarshalJSON() ([]byte, error) {
6060 return []byte (fmt .Sprintf ("\" %sZ\" " , it .Time .UTC ().Format ("2006-01-02T15:04:05" ))), nil
6161}
6262
63-
64- func main (){
63+ func main () {
6564 zerolog .SetGlobalLevel (zerolog .InfoLevel )
6665 _ , ok := os .LookupEnv ("DEBUG" )
6766 if ok {
@@ -144,14 +143,9 @@ func writeCachedFile(awsSsoCachePath, awsSSOProfileName string, credentialProces
144143func getCachedFile (awsSsoCachePath , awsSSOProfileName string ) (* CredentialProcessJson , error ) {
145144 cachedFileName := getCachedFileName (awsSSOProfileName )
146145 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- }
152146 var credentialProcessJson CredentialProcessJson
153147
154- bytes , err := readJsonFile ( awsSsoCachePath , fInfo )
148+ bytes , err := ioutil . ReadFile ( cachedFilePath )
155149 if err != nil {
156150 return nil , err
157151 }
@@ -168,8 +162,8 @@ func getCachedFile(awsSsoCachePath, awsSSOProfileName string) (*CredentialProces
168162}
169163
170164func 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 [:]))
173167}
174168
175169func printProfile (credentialProcessJson CredentialProcessJson ) {
@@ -220,68 +214,25 @@ func getSsoRoleCredentials(profile Profile, awsSSOCredential AWSSSOCredential) (
220214func getSsoCachedLogin (profile Profile , ssoCachePath string ) (AWSSSOCredential , error ) {
221215 var awsSSOCredential AWSSSOCredential
222216
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 )
224221 if err != nil {
225222 return awsSSOCredential , err
226223 }
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- }
271224
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 )
274226 if err != nil {
275- return nil , err
227+ return awsSSOCredential , err
276228 }
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
285236}
286237
287238func parseProfile (section * ini.Section ) (Profile , error ) {
@@ -294,7 +245,7 @@ func parseProfile(section *ini.Section) (Profile, error) {
294245 log .Debug ().Str ("id" , profileAccountId .String ()).Msg ("found account id" )
295246 profile .SSOAccountID = profileAccountId .String ()
296247
297- profileRegionKey , err := section .GetKey ("sso_region" )
248+ profileRegionKey , err := section .GetKey ("sso_region" )
298249 if err != nil {
299250 return profile , fmt .Errorf ("error getting sso_region from profile: %w" , err )
300251 }
0 commit comments