Skip to content

Commit ec6f9b0

Browse files
committed
Changing last-used file to be last-used.json with a serialized Role object
1 parent 3323c2c commit ec6f9b0

File tree

2 files changed

+15
-26
lines changed

2 files changed

+15
-26
lines changed

sdk/credentials/role-credentials.go

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package credentials
22

33
import (
44
"encoding/json"
5-
"fmt"
65
"io/ioutil"
76
"os"
87
"path/filepath"
@@ -89,37 +88,27 @@ func (r *Role) MarkLastUsed() error {
8988
if err := os.MkdirAll(filepath.Join(homedir, KnoxPath), 0700); err != nil {
9089
return err
9190
}
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)
9497
}
9598

9699
func GetLastUsedRole() (Role, error) {
97100
homedir, err := os.UserHomeDir()
98101
if err != nil {
99102
return Role{}, err
100103
}
101-
lastUsedPath := filepath.Join(homedir, KnoxPath, "last-used")
104+
lastUsedPath := filepath.Join(homedir, KnoxPath, "last-used.json")
102105
contents, err := ioutil.ReadFile(lastUsedPath)
103106
if err != nil {
104107
return Role{}, err
105108
}
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
123112
}
124113
creds, err := findRoleCredentials(role)
125114
if err != nil {

sdk/credentials/session.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ import (
1919
type Roles []Role
2020

2121
type Role struct {
22-
Name string
23-
AccountId string
24-
Region string
25-
SessionName string
26-
Credentials *RoleCredentials
22+
Name string `json:"name"`
23+
AccountId string `json:"accountId"`
24+
Region string `json:"region"`
25+
SessionName string `json:"sessionName"`
26+
Credentials *RoleCredentials `json:"-"`
2727
}
2828

2929
func (r *Role) CacheKey() string {

0 commit comments

Comments
 (0)