Skip to content

Commit a09c943

Browse files
committed
If last-used credentials do not exist, create them
1 parent ec6f9b0 commit a09c943

File tree

5 files changed

+73
-50
lines changed

5 files changed

+73
-50
lines changed

internal/creds-last-used.go

+10-4
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ var credsLastUsedCmd = &cobra.Command{
1616
if err != nil {
1717
ExitWithError(1, "failed to get last used role", err)
1818
}
19-
if role.Credentials.IsExpired() {
19+
if role.Credentials == nil || role.Credentials.IsExpired() {
2020
sessions, err := credentials.GetSessions()
2121
if err != nil {
2222
ExitWithError(2, "failed to parse sso sessions", err)
@@ -25,18 +25,24 @@ var credsLastUsedCmd = &cobra.Command{
2525
if session == nil {
2626
ExitWithError(3, "failed to find sso session " + role.SessionName, err)
2727
}
28+
if session.ClientToken == nil || session.ClientToken.IsExpired() {
29+
err := ClientLogin(session)
30+
if err != nil {
31+
ExitWithError(4, "failed to authorize device login", err)
32+
}
33+
}
2834
err = session.RefreshRoleCredentials(&role)
2935
if err != nil {
30-
ExitWithError(4, "failed to get credentials", err)
36+
ExitWithError(5, "failed to get credentials", err)
3137
}
3238
err = role.Credentials.Save(session.Name, role.CacheKey())
3339
if err != nil {
34-
ExitWithError(5, "failed to save credentials", err)
40+
ExitWithError(6, "failed to save credentials", err)
3541
}
3642
}
3743
serialized, err := role.Credentials.ToJSON()
3844
if err != nil {
39-
ExitWithError(2, "failed to serialize role credentials", err)
45+
ExitWithError(7, "failed to serialize role credentials", err)
4046
}
4147
fmt.Println(serialized)
4248
},

internal/creds-select.go

+6
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ var credsSelectCmd = &cobra.Command{
4545
if session == nil {
4646
ExitWithError(3, "failed to find sso session " + selectedRole.SessionName, err)
4747
}
48+
if session.ClientToken == nil || session.ClientToken.IsExpired() {
49+
err := ClientLogin(session)
50+
if err != nil {
51+
ExitWithError(4, "failed to authorize device login", err)
52+
}
53+
}
4854
err = session.RefreshRoleCredentials(&selectedRole)
4955
if err != nil {
5056
ExitWithError(4, "failed to get credentials", err)

internal/root.go

+40
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import (
55
"os"
66
"syscall"
77

8+
. "github.com/null93/aws-knox/sdk/style"
9+
"github.com/null93/aws-knox/sdk/credentials"
10+
"github.com/pkg/browser"
811
"github.com/null93/aws-knox/pkg/ansi"
912
"github.com/null93/aws-knox/pkg/color"
1013
"github.com/spf13/cobra"
@@ -29,6 +32,43 @@ func ExitWithError(code int, message string, err error) {
2932
os.Exit(code)
3033
}
3134

35+
func ClientLogin(session *credentials.Session) error {
36+
if err := session.RegisterClient(); err != nil {
37+
return err
38+
}
39+
userCode, deviceCode, url, urlFull, err := session.StartDeviceAuthorization()
40+
if err != nil {
41+
return err
42+
}
43+
yellow := color.ToForeground(YellowColor).Decorator()
44+
gray := color.ToForeground(LightGrayColor).Decorator()
45+
title := TitleStyle.Decorator()
46+
DefaultStyle.Printfln("")
47+
DefaultStyle.Printfln("%s %s", title("SSO Session: "), gray(session.Name))
48+
DefaultStyle.Printfln("%s %s", title("SSO Start URL: "), gray(session.StartUrl))
49+
DefaultStyle.Printfln("%s %s", title("Authorization URL:"), gray(url))
50+
DefaultStyle.Printfln("%s %s", title("Device Code: "), yellow(userCode))
51+
DefaultStyle.Printfln("")
52+
DefaultStyle.Printf("Waiting for authorization to complete...")
53+
err = browser.OpenURL(urlFull)
54+
if err != nil {
55+
ansi.MoveCursorUp(6)
56+
ansi.ClearDown()
57+
return err
58+
}
59+
err = session.WaitForToken(deviceCode)
60+
ansi.MoveCursorUp(6)
61+
ansi.ClearDown()
62+
if err != nil {
63+
return err
64+
}
65+
err = session.Save()
66+
if err != nil {
67+
return err
68+
}
69+
return nil
70+
}
71+
3272
func init() {
3373
RootCmd.Flags().SortFlags = true
3474
RootCmd.Root().CompletionOptions.DisableDefaultCmd = true

internal/select.go

+12-45
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,8 @@ import (
44
"fmt"
55
"time"
66

7-
"github.com/null93/aws-knox/pkg/ansi"
8-
"github.com/null93/aws-knox/pkg/color"
97
"github.com/null93/aws-knox/sdk/credentials"
108
"github.com/null93/aws-knox/sdk/picker"
11-
. "github.com/null93/aws-knox/sdk/style"
12-
"github.com/pkg/browser"
139
"github.com/spf13/cobra"
1410
)
1511

@@ -56,47 +52,18 @@ var selectCredentialsCmd = &cobra.Command{
5652
ExitWithError(4, "session with passed name not found", err)
5753
}
5854
if session.ClientToken == nil || session.ClientToken.IsExpired() {
59-
if err := session.RegisterClient(); err != nil {
60-
ExitWithError(5, "failed to register client", err)
61-
}
62-
userCode, deviceCode, url, urlFull, err := session.StartDeviceAuthorization()
63-
if err != nil {
64-
ExitWithError(6, "failed to start device authorization", err)
65-
}
66-
yellow := color.ToForeground(YellowColor).Decorator()
67-
gray := color.ToForeground(LightGrayColor).Decorator()
68-
title := TitleStyle.Decorator()
69-
DefaultStyle.Printfln("")
70-
DefaultStyle.Printfln("%s %s", title("SSO Session: "), gray(session.Name))
71-
DefaultStyle.Printfln("%s %s", title("SSO Start URL: "), gray(session.StartUrl))
72-
DefaultStyle.Printfln("%s %s", title("Authorization URL:"), gray(url))
73-
DefaultStyle.Printfln("%s %s", title("Device Code: "), yellow(userCode))
74-
DefaultStyle.Printfln("")
75-
DefaultStyle.Printf("Waiting for authorization to complete...")
76-
err = browser.OpenURL(urlFull)
77-
if err != nil {
78-
ansi.MoveCursorUp(6)
79-
ansi.ClearDown()
80-
ExitWithError(7, "failed to open url in browser", err)
81-
}
82-
err = session.WaitForToken(deviceCode)
83-
ansi.MoveCursorUp(6)
84-
ansi.ClearDown()
85-
if err != nil {
86-
ExitWithError(8, "failed to wait for token", err)
87-
}
88-
err = session.Save()
55+
err := ClientLogin(session)
8956
if err != nil {
90-
ExitWithError(9, "failed to save session", err)
57+
ExitWithError(5, "failed to authorize device login", err)
9158
}
9259
}
9360
if accountId == "" {
9461
accountIds, err := session.GetAccounts()
9562
if err != nil {
96-
ExitWithError(10, "failed to get account ids", err)
63+
ExitWithError(6, "failed to get account ids", err)
9764
}
9865
if len(accountIds) == 0 {
99-
ExitWithError(11, "no accounts found", err)
66+
ExitWithError(7, "no accounts found", err)
10067
}
10168
p := picker.NewPicker()
10269
p.WithMaxHeight(5)
@@ -108,14 +75,14 @@ var selectCredentialsCmd = &cobra.Command{
10875
}
10976
selection := p.Pick()
11077
if selection == nil {
111-
ExitWithError(12, "failed to pick an account id", err)
78+
ExitWithError(8, "failed to pick an account id", err)
11279
}
11380
accountId = selection.Value.(string)
11481
}
11582
roles, err := session.GetRoles(accountId)
11683
if roleName == "" {
11784
if err != nil {
118-
ExitWithError(13, "failed to get roles", err)
85+
ExitWithError(9, "failed to get roles", err)
11986
}
12087
p := picker.NewPicker()
12188
p.WithMaxHeight(5)
@@ -131,30 +98,30 @@ var selectCredentialsCmd = &cobra.Command{
13198
}
13299
selection := p.Pick()
133100
if selection == nil {
134-
ExitWithError(14, "failed to pick a role name", err)
101+
ExitWithError(10, "failed to pick a role name", err)
135102
}
136103
roleName = selection.Value.(string)
137104
}
138105
role := roles.FindByName(roleName)
139106
if role == nil {
140-
ExitWithError(15, "role with passed name not found", err)
107+
ExitWithError(11, "role with passed name not found", err)
141108
}
142109
if role.Credentials == nil || role.Credentials.IsExpired() {
143110
err := session.RefreshRoleCredentials(role)
144111
if err != nil {
145-
ExitWithError(16, "failed to get credentials", err)
112+
ExitWithError(12, "failed to get credentials", err)
146113
}
147114
err = role.Credentials.Save(session.Name, role.CacheKey())
148115
if err != nil {
149-
ExitWithError(17, "failed to save credentials", err)
116+
ExitWithError(13, "failed to save credentials", err)
150117
}
151118
}
152119
if err := role.MarkLastUsed(); err != nil {
153-
ExitWithError(18, "failed to mark last used role", err)
120+
ExitWithError(14, "failed to mark last used role", err)
154121
}
155122
json, err := role.Credentials.ToJSON()
156123
if err != nil {
157-
ExitWithError(19, "failed to convert credentials to json", err)
124+
ExitWithError(15, "failed to convert credentials to json", err)
158125
}
159126
fmt.Println(json)
160127
},

sdk/credentials/session.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,11 @@ func (s *Session) RefreshRoleCredentials(role *Role) error {
292292
}
293293
options := sso.Options{Region: s.Region}
294294
client := sso.New(options)
295-
params := sso.GetRoleCredentialsInput{AccessToken: &s.ClientToken.AccessToken, AccountId: &role.AccountId, RoleName: &role.Name}
295+
params := sso.GetRoleCredentialsInput{
296+
AccessToken: &s.ClientToken.AccessToken,
297+
AccountId: &role.AccountId,
298+
RoleName: &role.Name,
299+
}
296300
resp, err := client.GetRoleCredentials(context.TODO(), &params)
297301
if err != nil {
298302
return err

0 commit comments

Comments
 (0)