-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.go
140 lines (109 loc) · 3.07 KB
/
session.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package main
import (
"errors"
"fmt"
"os"
"os/exec"
"github.com/aws/aws-sdk-go-v2/service/ec2/types"
"github.com/ivoronin/ec2ssh/awsutil"
)
type Session struct {
options Options
instance types.Instance
destinationAddr string
privateKeyPath string
publicKey string
proxyCommand string
}
func (s *Session) buildSSHArgs() []string {
var sshArgs []string
appendIfSet := func(option, value string) {
if value != "" {
sshArgs = append(sshArgs, fmt.Sprintf(option, value))
}
}
appendIfSet("-oProxyCommand=%s", s.proxyCommand)
appendIfSet("-l%s", s.options.Login)
appendIfSet("-p%s", s.options.Port)
appendIfSet("-i%s", s.privateKeyPath)
sshArgs = append(sshArgs, fmt.Sprintf("-oHostKeyAlias=%s", *s.instance.InstanceId))
sshArgs = append(sshArgs, s.options.SSHArgs...)
sshArgs = append(sshArgs, s.destinationAddr)
if len(s.options.CommandWithArgs) > 0 {
sshArgs = append(sshArgs, "--")
sshArgs = append(sshArgs, s.options.CommandWithArgs...)
}
return sshArgs
}
func NewSession(options Options, tmpDir string) (*Session, error) {
instance, err := GetInstance(options.DstType, options.Destination)
if err != nil {
return nil, fmt.Errorf("unable to get instance: %w", err)
}
session := &Session{
options: options,
instance: instance,
}
err = session.setupDestinationAddr()
if err != nil {
return nil, err
}
err = session.setupSSHKeys(tmpDir)
if err != nil {
return nil, err
}
return session, nil
}
func (s *Session) setupDestinationAddr() error {
var err error
if s.options.UseEICE {
s.destinationAddr = *s.instance.InstanceId
s.proxyCommand = fmt.Sprintf("%s --wscat", os.Args[0])
} else {
s.destinationAddr, err = GetInstanceAddr(s.instance, s.options.AddrType)
}
return err
}
func (s *Session) setupSSHKeys(tmpDir string) error {
var err error
if s.options.IdentityFile == "" {
s.privateKeyPath, s.publicKey, err = GenerateSSHKeypair(tmpDir)
} else {
s.privateKeyPath = s.options.IdentityFile
s.publicKey, err = GetSSHPublicKey(s.options.IdentityFile)
}
return err
}
func (s *Session) Run() error {
var err error
if !s.options.NoSendKeys {
err = awsutil.SendSSHPublicKey(s.instance, s.options.Login, s.publicKey)
if err != nil {
return fmt.Errorf("unable to send SSH public key: %w", err)
}
}
sshArgs := s.buildSSHArgs()
cmd := exec.Command("ssh", sshArgs...)
cmd.Env = os.Environ()
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if s.options.UseEICE {
tunnelURI, err := awsutil.CreateEICETunnelURI(s.instance, s.options.Port, s.options.EICEID)
if err != nil {
return fmt.Errorf("unable to setup EICE tunnel: %w", err)
}
cmd.Env = append(cmd.Env, fmt.Sprintf("EC2SSH_TUNNEL_URI=%s", tunnelURI))
}
DebugLogger.Printf("running ssh with args: %v", sshArgs)
exitCode := 0
if err := cmd.Run(); err != nil {
var exitError *exec.ExitError
if !errors.As(err, &exitError) { /* Don't print error message if ssh exits with non-zero exit code */
return err
}
exitCode = exitError.ExitCode()
}
DebugLogger.Printf("ssh exited with code %d", exitCode)
return nil
}