/
ssh.go
239 lines (207 loc) · 5.04 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
// Copyright (c) 2018 Benjamin Borbe All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package ssh
import (
"bytes"
"context"
"fmt"
"io"
"io/ioutil"
"sync"
"github.com/bborbe/run"
"github.com/golang/glog"
"github.com/pkg/errors"
"golang.org/x/crypto/ssh"
"github.com/bborbe/world/pkg/network"
)
type Host struct {
IP network.IP
Port int
}
func (h Host) Address(ctx context.Context) (string, error) {
ip, err := h.IP.IP(ctx)
if err != nil {
return "", err
}
return fmt.Sprintf("%s:%d", ip.String(), h.Port), nil
}
func (h Host) Validate(ctx context.Context) error {
if err := h.IP.Validate(ctx); err != nil {
return err
}
if h.Port <= 0 {
return errors.New("invalid Port")
}
return nil
}
type PublicKeyPath string
func (p PublicKeyPath) Validate(ctx context.Context) error {
if p == "" {
return errors.New("PublicKeyPath missing")
}
return nil
}
func (p PublicKeyPath) String() string {
return string(p)
}
type PrivateKeyPath string
func (p PrivateKeyPath) Validate(ctx context.Context) error {
if p == "" {
return errors.New("PrivateKeyPath missing")
}
return nil
}
func (p PrivateKeyPath) String() string {
return string(p)
}
func (p PrivateKeyPath) Signer() (ssh.Signer, error) {
content, err := ioutil.ReadFile(p.String())
if err != nil {
return nil, errors.Wrap(err, "read private key failed")
}
signer, err := ssh.ParsePrivateKey(content)
if err != nil {
return nil, errors.Wrap(err, "parse private key failed")
}
return signer, nil
}
type User string
func (h User) Validate(ctx context.Context) error {
if h == "" {
return errors.New("User missing")
}
return nil
}
func (p User) String() string {
return string(p)
}
type SSH struct {
Host Host
PrivateKeyPath PrivateKeyPath
User User
mux sync.Mutex
client *ssh.Client
}
func (s *SSH) Validate(ctx context.Context) error {
if err := s.Host.Validate(ctx); err != nil {
return err
}
if s.PrivateKeyPath == "" {
return errors.New("PrivateKeyPath missing")
}
if s.User == "" {
return errors.New("User missing")
}
return nil
}
func (s *SSH) getClient(ctx context.Context) (*ssh.Client, error) {
s.mux.Lock()
defer s.mux.Unlock()
if s.client == nil {
signer, err := s.PrivateKeyPath.Signer()
if err != nil {
return nil, errors.Wrap(err, "get signer failed")
}
addr, err := s.Host.Address(ctx)
if err != nil {
return nil, errors.Wrap(err, "get addr from host failed")
}
s.client, err = ssh.Dial("tcp", addr, &ssh.ClientConfig{
User: s.User.String(),
Auth: []ssh.AuthMethod{
ssh.PublicKeys(signer),
},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
})
if err != nil {
return nil, errors.Wrap(err, "connect host failed")
}
}
return s.client, nil
}
func (s *SSH) createSession(ctx context.Context) (*ssh.Session, error) {
client, err := s.getClient(ctx)
if err != nil {
return nil, errors.Wrap(err, "get client failed")
}
session, err := client.NewSession()
if err != nil {
return nil, errors.Wrap(err, "create new ssh session failed")
}
return session, nil
}
func (s *SSH) RunCommandStdin(ctx context.Context, command string, content []byte) error {
session, err := s.createSession(ctx)
if err != nil {
return errors.Wrap(err, "create ssh session failed")
}
defer session.Close()
return run.CancelOnFirstError(
ctx,
func(ctx context.Context) error {
stdin, err := session.StdinPipe()
if err != nil {
return errors.Wrap(err, "open stdinpipe failed")
}
defer stdin.Close()
stdin.Write(content)
glog.V(2).Infof("write content to stdin complete")
return nil
},
func(ctx context.Context) error {
return runWithout(ctx, session, command)
},
)
}
func (s *SSH) RunCommandStdout(ctx context.Context, command string) ([]byte, error) {
session, err := s.createSession(ctx)
if err != nil {
return nil, errors.Wrap(err, "create ssh session failed")
}
defer session.Close()
b := &bytes.Buffer{}
err = run.CancelOnFirstError(
ctx,
func(ctx context.Context) error {
return runWithout(ctx, session, command)
},
func(ctx context.Context) error {
stdin, err := session.StdoutPipe()
if err != nil {
return err
}
_, err = io.Copy(b, stdin)
return errors.Wrap(err, "copy failed")
},
)
if err != nil {
return nil, err
}
return b.Bytes(), nil
}
func (s *SSH) RunCommand(ctx context.Context, cmd string) error {
session, err := s.createSession(ctx)
if err != nil {
return errors.Wrap(err, "create ssh session failed")
}
defer session.Close()
return runWithout(ctx, session, cmd)
}
func runWithout(ctx context.Context, session *ssh.Session, cmd string) error {
command := fmt.Sprintf("sudo sh -c 'export LANG=C; %s'", cmd)
glog.V(1).Infof("run remote command: %s", command)
select {
case <-ctx.Done():
glog.V(1).Infof("context done => send kill")
return session.Signal(ssh.SIGKILL)
default:
return errors.Wrapf(session.Run(command), "run command failed: %s", command)
}
}
func (s *SSH) Close() {
s.mux.Lock()
defer s.mux.Unlock()
s.client.Close()
s.client = nil
}