-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
123 lines (105 loc) · 2.78 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package main
import (
"context"
"encoding/json"
"fmt"
"os"
"os/exec"
"os/signal"
"strings"
"syscall"
"github.com/AlecAivazis/survey/v2"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/ec2"
"github.com/aws/aws-sdk-go-v2/service/ssm"
"github.com/aws/aws-sdk-go/aws"
)
type Instance struct {
Name string
Id string
Ip string
}
var options []string
func check(err error) {
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func main() {
cfg, err := config.LoadDefaultConfig(context.TODO())
check(err)
clientEC2 := ec2.NewFromConfig(cfg)
res, err := clientEC2.DescribeInstances(context.TODO(), &ec2.DescribeInstancesInput{})
check(err)
var instances []Instance
for _, reservation := range res.Reservations {
for _, instance := range reservation.Instances {
if instance.State.Name == "terminated" || instance.State.Name == "stopped" {
continue
}
var i Instance
for _, tag := range instance.Tags {
if *tag.Key == "Name" {
i.Name = *tag.Value
}
}
i.Id = *instance.InstanceId
i.Ip = *instance.PrivateIpAddress
instances = append(instances, i)
options = append(options, fmt.Sprintf("%-28s %-24s%s", i.Name, i.Id, i.Ip))
}
}
answers := struct{ Instance string }{}
err = survey.Ask([]*survey.Question{
{
Name: "instance",
Prompt: &survey.Select{
Message: "Which instance would you like to connect to:",
Options: options,
},
},
}, &answers)
check(err)
var target string
for _, i := range instances {
if strings.Contains(answers.Instance, i.Id) {
target = i.Id
}
}
clientSSM := ssm.NewFromConfig(cfg)
input := &ssm.StartSessionInput{
Target: aws.String(target),
}
ses, err := clientSSM.StartSession(context.TODO(), input)
check(err)
fmt.Println("started session", string(*ses.SessionId))
// I followed the example for the rest this
// https://github.com/mmmorris1975/aws-runas/blob/master/cli/ssm_cmd.go
var inJ, outJ []byte
outJ, err = json.Marshal(ses)
check(err)
inJ, err = json.Marshal(input)
check(err)
ep, err := ssm.NewDefaultEndpointResolver().ResolveEndpoint(cfg.Region, ssm.EndpointResolverOptions{})
check(err)
client := ssm.NewFromConfig(cfg)
c := exec.Command("session-manager-plugin", string(outJ), cfg.Region, "StartSession", "", string(inJ), ep.URL)
c.Stdin = os.Stdin
c.Stdout = os.Stdout
c.Stderr = os.Stderr
// ignore ctrl + c
signalCh := make(chan os.Signal, 1)
signal.Notify(signalCh, os.Interrupt, syscall.SIGINT)
go func() {
for range signalCh {
fmt.Println("Ctrl+C is disabled during the SSM session. Use 'exit' to terminate the session.")
}
}()
c.Run()
term, err := client.TerminateSession(context.TODO(), &ssm.TerminateSessionInput{
SessionId: ses.SessionId,
})
check(err)
fmt.Println("terminated session", string(*term.SessionId))
}