@@ -2,13 +2,12 @@ package internal
2
2
3
3
import (
4
4
"fmt"
5
+ "os"
5
6
"os/exec"
6
- "time"
7
7
"syscall"
8
- "os"
9
8
10
9
"github.com/null93/aws-knox/sdk/credentials"
11
- "github.com/null93/aws-knox/sdk/picker "
10
+ "github.com/null93/aws-knox/sdk/tui "
12
11
"github.com/spf13/cobra"
13
12
)
14
13
@@ -29,134 +28,66 @@ var connectCmd = &cobra.Command{
29
28
Short : "Connect to a specific EC2 instance using AWS session-manager-plugin" ,
30
29
Args : cobra .ExactArgs (0 ),
31
30
Run : func (cmd * cobra.Command , args []string ) {
32
- now := time .Now ()
33
- sessions , err := credentials .GetSessions ()
34
- if err != nil {
31
+ var err error
32
+ var sessions credentials.Sessions
33
+ var session * credentials.Session
34
+ var roles credentials.Roles
35
+ var role * credentials.Role
36
+ var binaryPath string
37
+ if sessions , err = credentials .GetSessions (); err != nil {
35
38
ExitWithError (1 , "failed to get configured sessions" , err )
36
39
}
37
- if len (sessions ) == 0 {
38
- ExitWithError (2 , "no sso sessions found in config" , err )
39
- }
40
40
if connectSessionName == "" {
41
- p := picker .NewPicker ()
42
- p .WithMaxHeight (10 )
43
- p .WithEmptyMessage ("No SSO Sessions Found" )
44
- p .WithTitle ("Pick SSO Session" )
45
- p .WithHeaders ("SSO Session" , "Region" , "SSO Start URL" , "Expires In" )
46
- for _ , session := range sessions {
47
- expires := "-"
48
- if session .ClientToken != nil && ! session .ClientToken .IsExpired () {
49
- expires = fmt .Sprintf ("%.f mins" , session .ClientToken .ExpiresAt .Sub (now ).Minutes ())
50
- }
51
- p .AddOption (session .Name , session .Name , session .Region , session .StartUrl , expires )
41
+ if connectSessionName , err = tui .SelectSession (sessions ); err != nil {
42
+ ExitWithError (2 , "failed to pick an sso session" , err )
52
43
}
53
- selection := p .Pick ()
54
- if selection == nil {
55
- ExitWithError (3 , "failed to pick an sso session" , err )
56
- }
57
- connectSessionName = selection .Value .(string )
58
44
}
59
- session := sessions .FindByName (connectSessionName )
60
- if session == nil {
61
- ExitWithError (4 , "session with passed name not found" , err )
45
+ if session = sessions .FindByName (connectSessionName ); session == nil {
46
+ ExitWithError (3 , "session with passed name not found" , err )
62
47
}
63
48
if session .ClientToken == nil || session .ClientToken .IsExpired () {
64
- err := ClientLogin (session )
65
- if err != nil {
66
- ExitWithError (5 , "failed to authorize device login" , err )
49
+ if err = tui .ClientLogin (session ); err != nil {
50
+ ExitWithError (4 , "failed to authorize device login" , err )
67
51
}
68
52
}
69
53
if connectAccountId == "" {
70
- connectAccountIds , err := session .GetAccounts ()
71
- if err != nil {
72
- ExitWithError (6 , "failed to get account ids" , err )
73
- }
74
- if len (connectAccountIds ) == 0 {
75
- ExitWithError (7 , "no accounts found" , err )
76
- }
77
- p := picker .NewPicker ()
78
- p .WithMaxHeight (5 )
79
- p .WithEmptyMessage ("No Accounts Found" )
80
- p .WithTitle ("Pick Account" )
81
- p .WithHeaders ("Account ID" , "Name" , "Email" )
82
- for _ , account := range connectAccountIds {
83
- p .AddOption (account .Id , account .Id , account .Name , account .Email )
84
- }
85
- selection := p .Pick ()
86
- if selection == nil {
87
- ExitWithError (8 , "failed to pick an account id" , err )
54
+ if connectAccountId , err = tui .SelectAccount (session ); err != nil {
55
+ ExitWithError (5 , "failed to pick an account id" , err )
88
56
}
89
- connectAccountId = selection .Value .(string )
90
57
}
91
- roles , err := session .GetRoles (connectAccountId )
58
+ if roles , err = session .GetRoles (connectAccountId ); err != nil {
59
+ ExitWithError (6 , "failed to get roles" , err )
60
+ }
92
61
if connectRoleName == "" {
93
- if err != nil {
94
- ExitWithError (9 , "failed to get roles" , err )
95
- }
96
- p := picker .NewPicker ()
97
- p .WithMaxHeight (5 )
98
- p .WithEmptyMessage ("No Roles Found" )
99
- p .WithTitle ("Pick Role" )
100
- p .WithHeaders ("Role Name" , "Expires In" )
101
- for _ , role := range roles {
102
- expires := "-"
103
- if role .Credentials != nil && ! role .Credentials .IsExpired () {
104
- expires = fmt .Sprintf ("%.f mins" , role .Credentials .Expiration .Sub (now ).Minutes ())
105
- }
106
- p .AddOption (role .Name , role .Name , expires )
62
+ if connectRoleName , err = tui .SelectRole (roles ); err != nil {
63
+ ExitWithError (7 , "failed to pick a role" , err )
107
64
}
108
- selection := p .Pick ()
109
- if selection == nil {
110
- ExitWithError (10 , "failed to pick a role name" , err )
111
- }
112
- connectRoleName = selection .Value .(string )
113
65
}
114
- role := roles .FindByName (connectRoleName )
115
- if role == nil {
116
- ExitWithError (11 , "role with passed name not found" , err )
66
+ if role = roles .FindByName (connectRoleName ); role == nil {
67
+ ExitWithError (8 , "role with passed name not found" , err )
117
68
}
118
69
if role .Credentials == nil || role .Credentials .IsExpired () {
119
- err := session .RefreshRoleCredentials (role )
120
- if err != nil {
121
- ExitWithError (12 , "failed to get credentials" , err )
70
+ if err = session .RefreshRoleCredentials (role ); err != nil {
71
+ ExitWithError (9 , "failed to get credentials" , err )
122
72
}
123
- err = role .Credentials .Save (session .Name , role .CacheKey ())
124
- if err != nil {
125
- ExitWithError (13 , "failed to save credentials" , err )
73
+ if err = role .Credentials .Save (session .Name , role .CacheKey ()); err != nil {
74
+ ExitWithError (10 , "failed to save credentials" , err )
126
75
}
127
76
}
128
77
if err := role .MarkLastUsed (); err != nil {
129
- ExitWithError (14 , "failed to mark last used role" , err )
78
+ ExitWithError (11 , "failed to mark last used role" , err )
130
79
}
131
80
if connectInstanceId == "" {
132
- instances , err := role .GetManagedInstances ()
133
- if err != nil {
134
- ExitWithError (15 , "failed to get instances" , err )
135
- }
136
- if len (instances ) == 0 {
137
- ExitWithError (16 , "no instances found" , err )
81
+ if connectInstanceId , err = tui .SelectInstance (role ); err != nil {
82
+ ExitWithError (12 , "failed to pick an instance" , err )
138
83
}
139
- p := picker .NewPicker ()
140
- p .WithMaxHeight (10 )
141
- p .WithEmptyMessage ("No Instances Found" )
142
- p .WithTitle ("Pick EC2 Instance" )
143
- p .WithHeaders ("Instance ID" , "Instance Type" , "Private IP" , "Public IP" , "Name" )
144
- for _ , instance := range instances {
145
- p .AddOption (instance .Id , instance .Id , instance .InstanceType , instance .PrivateIpAddress , instance .PublicIpAddress , instance .Name )
146
- }
147
- selection := p .Pick ()
148
- if selection == nil {
149
- ExitWithError (17 , "failed to pick an instance id" , err )
150
- }
151
- connectInstanceId = selection .Value .(string )
152
84
}
153
85
details , err := role .StartSession (connectInstanceId , connectUid )
154
86
if err != nil {
155
- ExitWithError (18 , "failed to start ssm session" , err )
87
+ ExitWithError (13 , "failed to start ssm session" , err )
156
88
}
157
- binaryPath , err := exec .LookPath ("session-manager-plugin" )
158
- if err != nil {
159
- ExitWithError (19 , "failed to find session-manager-plugin, see " + SESSION_MANAGER_PLUGIN_URL , err )
89
+ if binaryPath , err = exec .LookPath ("session-manager-plugin" ); err != nil {
90
+ ExitWithError (14 , "failed to find session-manager-plugin, see " + SESSION_MANAGER_PLUGIN_URL , err )
160
91
}
161
92
command := exec .Command (
162
93
binaryPath ,
@@ -170,12 +101,10 @@ var connectCmd = &cobra.Command{
170
101
command .Stdin = os .Stdin
171
102
command .Stdout = os .Stdout
172
103
command .Stderr = os .Stderr
173
- command .SysProcAttr = & syscall.SysProcAttr { Setpgid : true , Foreground : true }
174
- err = command .Run ()
175
- if err != nil {
176
- ExitWithError (20 , "failed to run session-manager-plugin" , err )
104
+ command .SysProcAttr = & syscall.SysProcAttr {Setpgid : true , Foreground : true }
105
+ if err = command .Run (); err != nil {
106
+ ExitWithError (15 , "failed to run session-manager-plugin" , err )
177
107
}
178
- fmt .Println ("EXITED" )
179
108
},
180
109
}
181
110
0 commit comments