Skip to content

Commit 7ec7352

Browse files
committed
Verbose output for sync command and adding --region flag. Defaults to sso region
1 parent f0e20eb commit 7ec7352

File tree

5 files changed

+43
-22
lines changed

5 files changed

+43
-22
lines changed

internal/connect.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ import (
66
"os/exec"
77
"syscall"
88

9-
"github.com/null93/aws-knox/sdk/credentials"
10-
"github.com/null93/aws-knox/sdk/tui"
119
"github.com/null93/aws-knox/pkg/color"
10+
"github.com/null93/aws-knox/sdk/credentials"
1211
. "github.com/null93/aws-knox/sdk/style"
12+
"github.com/null93/aws-knox/sdk/tui"
1313
"github.com/spf13/cobra"
1414
)
1515

@@ -77,8 +77,11 @@ var connectCmd = &cobra.Command{
7777
continue
7878
}
7979
}
80+
if region == "" {
81+
region = role.Region
82+
}
8083
if instanceId == "" {
81-
if instanceId, action, err = tui.SelectInstance(role, searchTerm); err != nil {
84+
if instanceId, action, err = tui.SelectInstance(role, region, searchTerm); err != nil {
8285
ExitWithError(19, "failed to pick an instance", err)
8386
} else if action == "back" {
8487
goBack()
@@ -91,7 +94,7 @@ var connectCmd = &cobra.Command{
9194
title := TitleStyle.Decorator()
9295
DefaultStyle.Printfln("")
9396
DefaultStyle.Printfln("%s %s", title("SSO Session: "), gray(role.SessionName))
94-
DefaultStyle.Printfln("%s %s", title("Region: "), gray(role.Region))
97+
DefaultStyle.Printfln("%s %s", title("Region: "), gray(region))
9598
DefaultStyle.Printfln("%s %s", title("Account ID: "), gray(role.AccountId))
9699
DefaultStyle.Printfln("%s %s", title("Role Name: "), gray(role.Name))
97100
DefaultStyle.Printfln("%s %s", title("Instance ID: "), yellow(instanceId))
@@ -106,11 +109,11 @@ var connectCmd = &cobra.Command{
106109
command := exec.Command(
107110
binaryPath,
108111
fmt.Sprintf(`{"SessionId": "%s", "TokenValue": "%s", "StreamUrl": "%s"}`, *details.SessionId, *details.TokenValue, *details.StreamUrl),
109-
role.Region,
112+
region,
110113
"StartSession",
111114
"", // No Profile
112115
fmt.Sprintf(`{"Target": "%s"}`, instanceId),
113-
fmt.Sprintf("https://ssm.%s.amazonaws.com", role.Region),
116+
fmt.Sprintf("https://ssm.%s.amazonaws.com", region),
114117
)
115118
command.Stdin = os.Stdin
116119
command.Stdout = os.Stdout
@@ -131,6 +134,7 @@ func init() {
131134
connectCmd.Flags().StringVarP(&accountId, "account-id", "a", accountId, "AWS account ID")
132135
connectCmd.Flags().StringVarP(&roleName, "role-name", "r", roleName, "AWS role name")
133136
connectCmd.Flags().StringVarP(&instanceId, "instance-id", "i", instanceId, "EC2 instance ID")
137+
connectCmd.Flags().StringVar(&region, "region", region, "Region for quering instances")
134138
connectCmd.Flags().BoolVarP(&selectCachedFirst, "cached", "c", selectCachedFirst, "select from cached credentials")
135139
connectCmd.Flags().BoolVarP(&lastUsed, "last-used", "l", lastUsed, "select last used credentials")
136140
connectCmd.Flags().Uint32VarP(&connectUid, "uid", "u", connectUid, "UID on instance to 'su' to")

internal/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ var (
2323
accountId string
2424
roleName string
2525
instanceId string
26+
region string
2627
accountAliases map[string]string
2728
)
2829

internal/sync.go

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ import (
88
"strings"
99
"syscall"
1010

11+
"github.com/null93/aws-knox/pkg/color"
1112
"github.com/null93/aws-knox/sdk/credentials"
13+
. "github.com/null93/aws-knox/sdk/style"
1214
"github.com/null93/aws-knox/sdk/tui"
1315
"github.com/spf13/cobra"
1416
)
@@ -48,11 +50,11 @@ func rsyncInit(role *credentials.Role, instanceId string) {
4850
command := exec.Command(
4951
binaryPath,
5052
fmt.Sprintf(`{"SessionId": "%s", "TokenValue": "%s", "StreamUrl": "%s"}`, *details.SessionId, *details.TokenValue, *details.StreamUrl),
51-
role.Region,
53+
region,
5254
"StartSession",
5355
"", // No Profile
5456
fmt.Sprintf(`{"Target": "%s"}`, instanceId),
55-
fmt.Sprintf("https://ssm.%s.amazonaws.com", role.Region),
57+
fmt.Sprintf("https://ssm.%s.amazonaws.com", region),
5658
)
5759
command.Stdin = os.Stdin
5860
command.SysProcAttr = &syscall.SysProcAttr{Setpgid: true, Foreground: true}
@@ -85,11 +87,11 @@ func rsyncStart(role *credentials.Role, instanceId string) {
8587
command := exec.Command(
8688
binaryPath,
8789
fmt.Sprintf(`{"SessionId": "%s", "TokenValue": "%s", "StreamUrl": "%s"}`, *details.SessionId, *details.TokenValue, *details.StreamUrl),
88-
role.Region,
90+
region,
8991
"StartSession",
9092
"", // No Profile
9193
fmt.Sprintf(`{"Target": "%s"}`, instanceId),
92-
fmt.Sprintf("https://ssm.%s.amazonaws.com", role.Region),
94+
fmt.Sprintf("https://ssm.%s.amazonaws.com", region),
9395
)
9496
command.Stdin = os.Stdin
9597
command.Stdout = nil
@@ -117,11 +119,11 @@ func rsyncClean(role *credentials.Role, instanceId string) {
117119
command := exec.Command(
118120
binaryPath,
119121
fmt.Sprintf(`{"SessionId": "%s", "TokenValue": "%s", "StreamUrl": "%s"}`, *details.SessionId, *details.TokenValue, *details.StreamUrl),
120-
role.Region,
122+
region,
121123
"StartSession",
122124
"", // No Profile
123125
fmt.Sprintf(`{"Target": "%s"}`, instanceId),
124-
fmt.Sprintf("https://ssm.%s.amazonaws.com", role.Region),
126+
fmt.Sprintf("https://ssm.%s.amazonaws.com", region),
125127
)
126128
command.Stdin = os.Stdin
127129
command.SysProcAttr = &syscall.SysProcAttr{Setpgid: true, Foreground: true}
@@ -146,11 +148,11 @@ func rsyncPortForward(role *credentials.Role, instanceId string) {
146148
command := exec.Command(
147149
binaryPath,
148150
fmt.Sprintf(`{"SessionId": "%s", "TokenValue": "%s", "StreamUrl": "%s"}`, *details.SessionId, *details.TokenValue, *details.StreamUrl),
149-
role.Region,
151+
region,
150152
"StartSession",
151153
"", // No Profile
152154
fmt.Sprintf(`{"Target": "%s"}`, instanceId),
153-
fmt.Sprintf("https://ssm.%s.amazonaws.com", role.Region),
155+
fmt.Sprintf("https://ssm.%s.amazonaws.com", region),
154156
)
155157
command.Stdin = os.Stdin
156158
command.Stdout = nil
@@ -224,17 +226,30 @@ var syncCmd = &cobra.Command{
224226
continue
225227
}
226228
}
229+
if region == "" {
230+
region = role.Region
231+
}
227232
if instanceId == "" {
228-
if instanceId, action, err = tui.SelectInstance(role, searchTerm); err != nil {
233+
if instanceId, action, err = tui.SelectInstance(role, region, searchTerm); err != nil {
229234
ExitWithError(19, "failed to pick an instance", err)
230235
} else if action == "back" {
231236
goBack()
232237
continue
233238
}
234239
}
235-
fmt.Println("Remote Destination: /root/knox-sync")
236-
fmt.Printf("Example Command: rsync -P ./dump.sql ./release.tar.gz rsync://127.0.0.1:%d/sync\n", localPort)
237-
fmt.Println()
240+
241+
yellow := color.ToForeground(YellowColor).Decorator()
242+
gray := color.ToForeground(LightGrayColor).Decorator()
243+
title := TitleStyle.Decorator()
244+
DefaultStyle.Printfln("")
245+
DefaultStyle.Printfln("%s %s", title("SSO Session: "), gray(role.SessionName))
246+
DefaultStyle.Printfln("%s %s", title("Region: "), gray(region))
247+
DefaultStyle.Printfln("%s %s", title("Account ID: "), gray(role.AccountId))
248+
DefaultStyle.Printfln("%s %s", title("Role Name: "), gray(role.Name))
249+
DefaultStyle.Printfln("%s %s", title("Instance ID: "), gray(instanceId))
250+
DefaultStyle.Printfln("%s %s", title("Remote Destination: "), gray("/root/knox-sync"))
251+
DefaultStyle.Printfln("%s %s", title("Example Command: "), yellow("rsync -P ./dump.sql ./release.tar.gz rsync://127.0.0.1:%d/sync\n", localPort))
252+
238253
defer rsyncClean(role, instanceId)
239254
defer func() {
240255
fmt.Println("\nCleaning up...")
@@ -257,6 +272,7 @@ func init() {
257272
syncCmd.Flags().StringVarP(&accountId, "account-id", "a", accountId, "AWS account ID")
258273
syncCmd.Flags().StringVarP(&roleName, "role-name", "r", roleName, "AWS role name")
259274
syncCmd.Flags().StringVarP(&instanceId, "instance-id", "i", instanceId, "EC2 instance ID")
275+
syncCmd.Flags().StringVar(&region, "region", region, "Region for quering instances")
260276
syncCmd.Flags().Uint16VarP(&rsyncPort, "rsync-port", "P", rsyncPort, "rsync port")
261277
syncCmd.Flags().Uint16VarP(&localPort, "local-port", "p", localPort, "local port")
262278
syncCmd.Flags().BoolVarP(&lastUsed, "last-used", "l", lastUsed, "select last used credentials")

sdk/credentials/session.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ func (r Roles) FindByName(name string) *Role {
113113
return nil
114114
}
115115

116-
func (r *Role) GetManagedInstances() (Instances, error) {
116+
func (r *Role) GetManagedInstances(region string) (Instances, error) {
117117
instances := Instances{}
118118
if r.Credentials == nil {
119119
return instances, ErrorRoleCredentialsNil
@@ -123,7 +123,7 @@ func (r *Role) GetManagedInstances() (Instances, error) {
123123
r.Credentials.SecretAccessKey,
124124
r.Credentials.SessionToken,
125125
)
126-
options := ec2.Options{Region: r.Region, Credentials: staticProvider}
126+
options := ec2.Options{Region: region, Credentials: staticProvider}
127127
client := ec2.New(options)
128128
params := ec2.DescribeInstancesInput{
129129
Filters: []ec2types.Filter{

sdk/tui/tui.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,8 @@ func SelectRole(roles credentials.Roles) (string, string, error) {
149149
return selection.Value.(string), "", nil
150150
}
151151

152-
func SelectInstance(role *credentials.Role, initialFilter string) (string, string, error) {
153-
instances, err := role.GetManagedInstances()
152+
func SelectInstance(role *credentials.Role, region, initialFilter string) (string, string, error) {
153+
instances, err := role.GetManagedInstances(region)
154154
if err != nil {
155155
return "", "", err
156156
}

0 commit comments

Comments
 (0)