-
Notifications
You must be signed in to change notification settings - Fork 1
/
ssh.go
239 lines (209 loc) · 5.2 KB
/
ssh.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package main
import (
"crypto/x509"
"encoding/pem"
"errors"
"fmt"
"io/ioutil"
"log"
"net"
"os"
"os/exec"
"github.com/evan-forbes/devnet/config"
"golang.org/x/crypto/ssh"
)
type SSHManager struct {
Conns map[string]Connection
}
func NewSSHManager(drops map[string]config.Droplet, sshPass string) (*SSHManager, error) {
conns := make(map[string]Connection)
for name, drop := range drops {
conn, err := NewConnection(drop, sshPass)
if err != nil {
return nil, err
}
conns[name] = conn
}
return &SSHManager{
Conns: conns,
}, nil
}
// CloseAll closes each established ssh session
func (s *SSHManager) CloseAll() {
for name, c := range s.Conns {
err := c.Close()
if err != nil {
log.Println(
fmt.Errorf(
"failure to close ssh session for %s: %w", name, err,
),
)
}
}
}
type Connection struct {
client *ssh.Client
drop config.Droplet
output *os.File
}
func NewConnection(drop config.Droplet, sshPass string) (Connection, error) {
home, err := os.UserHomeDir()
if err != nil {
return Connection{}, err
}
host, err := drop.Drop.PublicIPv4()
if err != nil {
return Connection{}, err
}
sshConfig, err := newSshClientConfig("root", host, 22, home+"/.ssh/id_rsa", sshPass)
if err != nil {
return Connection{}, err
}
// connect to the server via ssh
client, err := ssh.Dial("tcp", fmt.Sprintf("%s:%d", host, 22), sshConfig)
if err != nil {
return Connection{}, err
}
output, err := os.OpenFile(drop.Output, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0700)
if err != nil {
return Connection{}, err
}
return Connection{
client: client,
drop: drop,
output: output,
}, nil
}
func (c Connection) DeliverPayload() error {
ipv4, err := c.drop.Drop.PublicIPv4()
if err != nil {
return err
}
cmd := exec.Command(
"scp",
"-r",
"-o StrictHostKeyChecking=no",
"-o UserKnownHostsFile=/dev/null",
c.drop.Payload,
fmt.Sprintf("root@%s:/root/", ipv4),
)
err = cmd.Run()
if err != nil {
return fmt.Errorf("failure to execute command: %w", err)
}
return nil
}
// Run runs a command on the ssh server and forwards the Stdout and
// Stderr of the server to the local client's output file
func (c Connection) Run(command string) error {
sesh, err := c.NewSession()
if err != nil {
return err
}
defer sesh.Close()
sesh.Stdout = c.output
sesh.Stderr = c.output
return sesh.Run(command)
}
func (c Connection) NewSession() (*ssh.Session, error) {
return c.client.NewSession()
}
func (c Connection) Close() error {
err := c.client.Close()
if err != nil {
return err
}
return c.output.Close()
}
func newSshClientConfig(user string, host string, port int, privateKeyPath string, privateKeyPassword string) (*ssh.ClientConfig, error) {
// read private key file
pemBytes, err := ioutil.ReadFile(privateKeyPath)
if err != nil {
return nil, fmt.Errorf("Reading private key file failed %v", err)
}
// create signer
signer, err := signerFromPem(pemBytes, []byte(privateKeyPassword))
if err != nil {
return nil, err
}
// build SSH client config
config := &ssh.ClientConfig{
User: user,
Auth: []ssh.AuthMethod{
ssh.PublicKeys(signer),
},
HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
// use OpenSSH's known_hosts file if you care about host validation
return nil
},
}
return config, nil
}
func signerFromPem(pemBytes []byte, password []byte) (ssh.Signer, error) {
// read pem block
err := errors.New("Pem decode failed, no key found")
pemBlock, _ := pem.Decode(pemBytes)
if pemBlock == nil {
return nil, err
}
// handle encrypted key
if x509.IsEncryptedPEMBlock(pemBlock) {
// decrypt PEM
pemBlock.Bytes, err = x509.DecryptPEMBlock(pemBlock, []byte(password))
if err != nil {
return nil, fmt.Errorf("Decrypting PEM block failed %v", err)
}
// get RSA, EC or DSA key
key, err := parsePemBlock(pemBlock)
if err != nil {
return nil, err
}
// generate signer instance from key
signer, err := ssh.NewSignerFromKey(key)
if err != nil {
return nil, fmt.Errorf("Creating signer from encrypted key failed %v", err)
}
return signer, nil
} else {
// generate signer instance from plain key
signer, err := ssh.ParsePrivateKey(pemBytes)
if err != nil {
return nil, fmt.Errorf("Parsing plain private key failed %v", err)
}
return signer, nil
}
}
func parsePemBlock(block *pem.Block) (interface{}, error) {
switch block.Type {
case "RSA PRIVATE KEY":
key, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
return nil, fmt.Errorf("Parsing PKCS private key failed %v", err)
} else {
return key, nil
}
case "EC PRIVATE KEY":
key, err := x509.ParseECPrivateKey(block.Bytes)
if err != nil {
return nil, fmt.Errorf("Parsing EC private key failed %v", err)
} else {
return key, nil
}
case "DSA PRIVATE KEY":
key, err := ssh.ParseDSAPrivateKey(block.Bytes)
if err != nil {
return nil, fmt.Errorf("Parsing DSA private key failed %v", err)
} else {
return key, nil
}
default:
return nil, fmt.Errorf("Parsing private key failed, unsupported key type %q", block.Type)
}
}
func reverse(s string) string {
runes := []rune(s)
for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
runes[i], runes[j] = runes[j], runes[i]
}
return string(runes)
}