Skip to content

Commit

Permalink
Refreshing role expired role credentials on 'creds select' and 'creds…
Browse files Browse the repository at this point in the history
… last-used'
  • Loading branch information
null93 committed Jun 20, 2024
1 parent 4d164a5 commit 340337f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
18 changes: 18 additions & 0 deletions internal/creds-last-used.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,24 @@ var credsLastUsedCmd = &cobra.Command{
if err != nil {
ExitWithError(1, "failed to get last used role", err)
}
if role.Credentials.IsExpired() {
sessions, err := credentials.GetSessions()
if err != nil {
ExitWithError(2, "failed to parse sso sessions", err)
}
session := sessions.FindByName(role.SessionName)
if session == nil {
ExitWithError(3, "failed to find sso session " + role.SessionName, err)
}
err = session.RefreshRoleCredentials(&role)
if err != nil {
ExitWithError(4, "failed to get credentials", err)
}
err = role.Credentials.Save(session.Name, role.CacheKey())
if err != nil {
ExitWithError(5, "failed to save credentials", err)
}
}
serialized, err := role.Credentials.ToJSON()
if err != nil {
ExitWithError(2, "failed to serialize role credentials", err)
Expand Down
18 changes: 18 additions & 0 deletions internal/creds-select.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,24 @@ var credsSelectCmd = &cobra.Command{
ExitWithError(3, "failed to pick role credentials", err)
}
selectedRole := selection.Value.(credentials.Role)
if selectedRole.Credentials.IsExpired() {
sessions, err := credentials.GetSessions()
if err != nil {
ExitWithError(2, "failed to parse sso sessions", err)
}
session := sessions.FindByName(selectedRole.SessionName)
if session == nil {
ExitWithError(3, "failed to find sso session " + selectedRole.SessionName, err)
}
err = session.RefreshRoleCredentials(&selectedRole)
if err != nil {
ExitWithError(4, "failed to get credentials", err)
}
err = selectedRole.Credentials.Save(session.Name, selectedRole.CacheKey())
if err != nil {
ExitWithError(5, "failed to save credentials", err)
}
}
serialized, err := selectedRole.Credentials.ToJSON()
if err != nil {
ExitWithError(4, "failed to serialize role credentials", err)
Expand Down

0 comments on commit 340337f

Please sign in to comment.