@@ -2,7 +2,6 @@ package credentials
2
2
3
3
import (
4
4
"encoding/json"
5
- "fmt"
6
5
"io/ioutil"
7
6
"os"
8
7
"path/filepath"
@@ -89,37 +88,27 @@ func (r *Role) MarkLastUsed() error {
89
88
if err := os .MkdirAll (filepath .Join (homedir , KnoxPath ), 0700 ); err != nil {
90
89
return err
91
90
}
92
- lastUsedPath := filepath .Join (homedir , KnoxPath , "last-used" )
93
- return ioutil .WriteFile (lastUsedPath , []byte (r .SessionName + "\n " + r .CacheKey ()), 0600 )
91
+ serialized , err := json .MarshalIndent (r , "" , " " )
92
+ if err != nil {
93
+ return err
94
+ }
95
+ lastUsedPath := filepath .Join (homedir , KnoxPath , "last-used.json" )
96
+ return ioutil .WriteFile (lastUsedPath , serialized , 0600 )
94
97
}
95
98
96
99
func GetLastUsedRole () (Role , error ) {
97
100
homedir , err := os .UserHomeDir ()
98
101
if err != nil {
99
102
return Role {}, err
100
103
}
101
- lastUsedPath := filepath .Join (homedir , KnoxPath , "last-used" )
104
+ lastUsedPath := filepath .Join (homedir , KnoxPath , "last-used.json " )
102
105
contents , err := ioutil .ReadFile (lastUsedPath )
103
106
if err != nil {
104
107
return Role {}, err
105
108
}
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 ], "_" )
112
- if len (parts ) < 3 {
113
- return Role {}, fmt .Errorf ("invalid last used role" )
114
- }
115
- region := parts [0 ]
116
- accountId := parts [1 ]
117
- roleName := strings .Join (parts [2 :], "_" )
118
- role := Role {
119
- Region : region ,
120
- AccountId : accountId ,
121
- Name : roleName ,
122
- SessionName : sessionName ,
109
+ role := Role {}
110
+ if err := json .Unmarshal (contents , & role ); err != nil {
111
+ return Role {}, err
123
112
}
124
113
creds , err := findRoleCredentials (role )
125
114
if err != nil {
0 commit comments