@@ -37,7 +37,7 @@ func findRoleCredentials(r Role) (*RoleCredentials, error) {
37
37
if err != nil {
38
38
return nil , err
39
39
}
40
- cachePath := filepath .Join (homedir , RoleCredentialsCachePath , cacheKey + ".json" )
40
+ cachePath := filepath .Join (homedir , RoleCredentialsCachePath , r . SessionName , cacheKey + ".json" )
41
41
if _ , err := os .Stat (cachePath ); err == nil {
42
42
contents , err := ioutil .ReadFile (cachePath )
43
43
if err != nil {
@@ -56,28 +56,28 @@ func (r *RoleCredentials) IsExpired() bool {
56
56
return r .Expiration .Before (time .Now ())
57
57
}
58
58
59
- func (r * RoleCredentials ) Save (key string ) error {
59
+ func (r * RoleCredentials ) Save (sessionName , key string ) error {
60
60
homedir , err := os .UserHomeDir ()
61
61
if err != nil {
62
62
return err
63
63
}
64
- if err := os .MkdirAll (filepath .Join (homedir , RoleCredentialsCachePath ), 0700 ); err != nil {
64
+ if err := os .MkdirAll (filepath .Join (homedir , RoleCredentialsCachePath , sessionName ), 0700 ); err != nil {
65
65
return err
66
66
}
67
- cachePath := filepath .Join (homedir , RoleCredentialsCachePath , key + ".json" )
67
+ cachePath := filepath .Join (homedir , RoleCredentialsCachePath , sessionName , key + ".json" )
68
68
contents , err := json .Marshal (r )
69
69
if err != nil {
70
70
return err
71
71
}
72
72
return ioutil .WriteFile (cachePath , contents , 0600 )
73
73
}
74
74
75
- func (r * RoleCredentials ) DeleteCache (key string ) error {
75
+ func (r * RoleCredentials ) DeleteCache (sessionName , key string ) error {
76
76
homedir , err := os .UserHomeDir ()
77
77
if err != nil {
78
78
return err
79
79
}
80
- cachePath := filepath .Join (homedir , RoleCredentialsCachePath , key + ".json" )
80
+ cachePath := filepath .Join (homedir , RoleCredentialsCachePath , sessionName , key + ".json" )
81
81
return os .Remove (cachePath )
82
82
}
83
83
@@ -90,7 +90,7 @@ func (r *Role) MarkLastUsed() error {
90
90
return err
91
91
}
92
92
lastUsedPath := filepath .Join (homedir , KnoxPath , "last-used" )
93
- return ioutil .WriteFile (lastUsedPath , []byte (r .CacheKey ()), 0600 )
93
+ return ioutil .WriteFile (lastUsedPath , []byte (r .SessionName + " \n " + r . CacheKey ()), 0600 )
94
94
}
95
95
96
96
func GetLastUsedRole () (Role , error ) {
@@ -103,17 +103,23 @@ func GetLastUsedRole() (Role, error) {
103
103
if err != nil {
104
104
return Role {}, err
105
105
}
106
- parts := strings .Split (string (contents ), "_" )
106
+ lines := strings .Split (string (contents ), "\n " )
107
+ if len (lines ) < 2 {
108
+ return Role {}, fmt .Errorf ("invalid last used role" )
109
+ }
110
+ sessionName := lines [0 ]
111
+ parts := strings .Split (lines [1 ], "_" )
107
112
if len (parts ) < 3 {
108
113
return Role {}, fmt .Errorf ("invalid last used role" )
109
114
}
110
115
region := parts [0 ]
111
116
accountId := parts [1 ]
112
117
roleName := strings .Join (parts [2 :], "_" )
113
118
role := Role {
114
- Region : region ,
115
- AccountId : accountId ,
116
- Name : roleName ,
119
+ Region : region ,
120
+ AccountId : accountId ,
121
+ Name : roleName ,
122
+ SessionName : sessionName ,
117
123
}
118
124
creds , err := findRoleCredentials (role )
119
125
if err != nil {
@@ -129,37 +135,38 @@ func GetSavedRolesWithCredentials() (Roles, error) {
129
135
if err != nil {
130
136
return roles , err
131
137
}
132
- cacheDir := filepath .Join (homedir , RoleCredentialsCachePath )
133
- files , err := os . ReadDir ( cacheDir )
138
+ pattern := filepath .Join (homedir , RoleCredentialsCachePath , "*" , "*.json" )
139
+ files , err := filepath . Glob ( pattern )
134
140
if err != nil {
135
141
return roles , err
136
142
}
137
- for _ , file := range files {
138
- filename := file . Name ( )
139
- if ! file . IsDir () && filepath .Ext ( filename ) == ".json" {
140
- contents , err := os . ReadFile (filepath .Join ( cacheDir , filename ))
141
- parts := strings . Split ( filename , "_" )
142
- if len ( parts ) < 3 {
143
- continue
144
- }
145
- region := parts [ 0 ]
146
- accountId := parts [1 ]
147
- roleName := strings . TrimSuffix ( strings . Join ( parts [2 :], "_" ), ".json" )
148
- if err != nil {
149
- return nil , err
150
- }
151
- cred := RoleCredentials { }
152
- if err := json . Unmarshal ( contents , & cred ); err != nil {
153
- return nil , err
154
- }
155
- role := Role {
156
- Region : region ,
157
- AccountId : accountId ,
158
- Name : roleName ,
159
- Credentials : & cred ,
160
- }
161
- roles = append ( roles , role )
143
+ for _ , foundPath := range files {
144
+ fmt . Println ( foundPath )
145
+ fileName := filepath .Base ( foundPath )
146
+ sessionName := filepath . Base (filepath .Dir ( foundPath ))
147
+ contents , err := os . ReadFile ( foundPath )
148
+ parts := strings . Split ( fileName , "_" )
149
+ if len ( parts ) < 3 {
150
+ continue
151
+ }
152
+ region := parts [0 ]
153
+ accountId := parts [1 ]
154
+ roleName := strings . TrimSuffix ( strings . Join ( parts [ 2 :], "_" ), ".json" )
155
+ if err != nil {
156
+ return nil , err
157
+ }
158
+ cred := RoleCredentials {}
159
+ if err := json . Unmarshal ( contents , & cred ); err != nil {
160
+ return nil , err
161
+ }
162
+ role := Role {
163
+ Region : region ,
164
+ AccountId : accountId ,
165
+ Name : roleName ,
166
+ SessionName : sessionName ,
167
+ Credentials : & cred ,
162
168
}
169
+ roles = append (roles , role )
163
170
}
164
171
return roles , nil
165
172
}
0 commit comments