Skip to content

Commit 4d164a5

Browse files
committed
Attaching session name to cached role credentials
1 parent ca03436 commit 4d164a5

File tree

5 files changed

+54
-45
lines changed

5 files changed

+54
-45
lines changed

internal/clean.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ var cleanCmd = &cobra.Command{
2828
}
2929
for _, role := range roles {
3030
if role.Credentials.IsExpired() || cleanAll {
31-
err := role.Credentials.DeleteCache(role.CacheKey())
31+
err := role.Credentials.DeleteCache(role.SessionName, role.CacheKey())
3232
if err != nil {
3333
ExitWithError(2, "failed to delete role credentials", err)
3434
}

internal/creds-select.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ var credsSelectCmd = &cobra.Command{
2323
p.WithMaxHeight(10)
2424
p.WithEmptyMessage("No Role Credentials Found")
2525
p.WithTitle("Pick Role Credentials")
26-
p.WithHeaders("Region", "Account ID", "Role Name", "Expires In")
26+
p.WithHeaders("SSO Session", "Region", "Account ID", "Role Name", "Expires In")
2727
for _, role := range roles {
2828
expires := "-"
2929
if role.Credentials != nil && !role.Credentials.IsExpired() {
3030
expires = fmt.Sprintf("%.f mins", role.Credentials.Expiration.Sub(now).Minutes())
3131
}
32-
p.AddOption(role, role.Region, role.AccountId, role.Name, expires)
32+
p.AddOption(role, role.SessionName, role.Region, role.AccountId, role.Name, expires)
3333
}
3434
selection := p.Pick()
3535
if selection == nil {

internal/select.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ var selectCredentialsCmd = &cobra.Command{
144144
if err != nil {
145145
ExitWithError(16, "failed to get credentials", err)
146146
}
147-
err = role.Credentials.Save(role.CacheKey())
147+
err = role.Credentials.Save(session.Name, role.CacheKey())
148148
if err != nil {
149149
ExitWithError(17, "failed to save credentials", err)
150150
}

sdk/credentials/role-credentials.go

Lines changed: 45 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func findRoleCredentials(r Role) (*RoleCredentials, error) {
3737
if err != nil {
3838
return nil, err
3939
}
40-
cachePath := filepath.Join(homedir, RoleCredentialsCachePath, cacheKey+".json")
40+
cachePath := filepath.Join(homedir, RoleCredentialsCachePath, r.SessionName, cacheKey+".json")
4141
if _, err := os.Stat(cachePath); err == nil {
4242
contents, err := ioutil.ReadFile(cachePath)
4343
if err != nil {
@@ -56,28 +56,28 @@ func (r *RoleCredentials) IsExpired() bool {
5656
return r.Expiration.Before(time.Now())
5757
}
5858

59-
func (r *RoleCredentials) Save(key string) error {
59+
func (r *RoleCredentials) Save(sessionName, key string) error {
6060
homedir, err := os.UserHomeDir()
6161
if err != nil {
6262
return err
6363
}
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 {
6565
return err
6666
}
67-
cachePath := filepath.Join(homedir, RoleCredentialsCachePath, key+".json")
67+
cachePath := filepath.Join(homedir, RoleCredentialsCachePath, sessionName, key+".json")
6868
contents, err := json.Marshal(r)
6969
if err != nil {
7070
return err
7171
}
7272
return ioutil.WriteFile(cachePath, contents, 0600)
7373
}
7474

75-
func (r *RoleCredentials) DeleteCache(key string) error {
75+
func (r *RoleCredentials) DeleteCache(sessionName, key string) error {
7676
homedir, err := os.UserHomeDir()
7777
if err != nil {
7878
return err
7979
}
80-
cachePath := filepath.Join(homedir, RoleCredentialsCachePath, key+".json")
80+
cachePath := filepath.Join(homedir, RoleCredentialsCachePath, sessionName, key+".json")
8181
return os.Remove(cachePath)
8282
}
8383

@@ -90,7 +90,7 @@ func (r *Role) MarkLastUsed() error {
9090
return err
9191
}
9292
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)
9494
}
9595

9696
func GetLastUsedRole() (Role, error) {
@@ -103,17 +103,23 @@ func GetLastUsedRole() (Role, error) {
103103
if err != nil {
104104
return Role{}, err
105105
}
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], "_")
107112
if len(parts) < 3 {
108113
return Role{}, fmt.Errorf("invalid last used role")
109114
}
110115
region := parts[0]
111116
accountId := parts[1]
112117
roleName := strings.Join(parts[2:], "_")
113118
role := Role{
114-
Region: region,
115-
AccountId: accountId,
116-
Name: roleName,
119+
Region: region,
120+
AccountId: accountId,
121+
Name: roleName,
122+
SessionName: sessionName,
117123
}
118124
creds, err := findRoleCredentials(role)
119125
if err != nil {
@@ -129,37 +135,38 @@ func GetSavedRolesWithCredentials() (Roles, error) {
129135
if err != nil {
130136
return roles, err
131137
}
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)
134140
if err != nil {
135141
return roles, err
136142
}
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,
162168
}
169+
roles = append(roles, role)
163170
}
164171
return roles, nil
165172
}

sdk/credentials/session.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ type Role struct {
2222
Name string
2323
AccountId string
2424
Region string
25+
SessionName string
2526
Credentials *RoleCredentials
2627
}
2728

@@ -269,9 +270,10 @@ func (s *Session) GetRoles(accountId string) (Roles, error) {
269270
for _, details := range page.RoleList {
270271
roleName := aws.ToString(details.RoleName)
271272
role := Role{
272-
Name: roleName,
273-
AccountId: accountId,
274-
Region: s.Region,
273+
Name: roleName,
274+
AccountId: accountId,
275+
Region: s.Region,
276+
SessionName: s.Name,
275277
}
276278
creds, err := findRoleCredentials(role)
277279
if err != nil {

0 commit comments

Comments
 (0)