-
-
Notifications
You must be signed in to change notification settings - Fork 63
/
easyssh_test.go
498 lines (430 loc) · 12.2 KB
/
easyssh_test.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
package easyssh
import (
"os"
"os/user"
"path"
"testing"
"time"
"github.com/stretchr/testify/assert"
"golang.org/x/crypto/ssh"
)
func getHostPublicKeyFile(keypath string) (ssh.PublicKey, error) {
var pubkey ssh.PublicKey
var err error
buf, err := os.ReadFile(keypath)
if err != nil {
return nil, err
}
pubkey, _, _, _, err = ssh.ParseAuthorizedKey(buf)
if err != nil {
return nil, err
}
return pubkey, nil
}
func TestGetKeyFile(t *testing.T) {
// missing file
_, err := getKeyFile("abc", "")
assert.Error(t, err)
assert.Equal(t, "open abc: no such file or directory", err.Error())
// wrong format
_, err = getKeyFile("./tests/.ssh/id_rsa.pub", "")
assert.Error(t, err)
assert.Equal(t, "ssh: no key found", err.Error())
_, err = getKeyFile("./tests/.ssh/id_rsa", "")
assert.NoError(t, err)
_, err = getKeyFile("./tests/.ssh/test", "1234")
assert.NoError(t, err)
}
func TestRunCommandWithFingerprint(t *testing.T) {
// wrong fingerprint
sshConf := &MakeConfig{
Server: "localhost",
User: "drone-scp",
Port: "22",
KeyPath: "./tests/.ssh/id_rsa",
Fingerprint: "wrong",
}
outStr, errStr, isTimeout, err := sshConf.Run("whoami", 10)
assert.Equal(t, "", outStr)
assert.Equal(t, "", errStr)
assert.False(t, isTimeout)
assert.Error(t, err)
hostKey, err := getHostPublicKeyFile("/etc/ssh/ssh_host_rsa_key.pub")
assert.NoError(t, err)
sshConf = &MakeConfig{
Server: "localhost",
User: "drone-scp",
Port: "22",
KeyPath: "./tests/.ssh/id_rsa",
Fingerprint: ssh.FingerprintSHA256(hostKey),
}
outStr, errStr, isTimeout, err = sshConf.Run("whoami")
assert.Equal(t, "drone-scp\n", outStr)
assert.Equal(t, "", errStr)
assert.True(t, isTimeout)
assert.NoError(t, err)
}
func TestPrivateKeyAndPassword(t *testing.T) {
// provide password and ssh private key
ssh := &MakeConfig{
Server: "localhost",
User: "drone-scp",
Port: "22",
Password: "1234",
KeyPath: "./tests/.ssh/id_rsa",
}
outStr, errStr, isTimeout, err := ssh.Run("whoami")
assert.Equal(t, "drone-scp\n", outStr)
assert.Equal(t, "", errStr)
assert.True(t, isTimeout)
assert.NoError(t, err)
// provide correct password and wrong private key
ssh = &MakeConfig{
Server: "localhost",
User: "drone-scp",
Port: "22",
Password: "1234",
KeyPath: "./tests/.ssh/id_rsa.pub",
}
outStr, errStr, isTimeout, err = ssh.Run("whoami")
assert.Equal(t, "drone-scp\n", outStr)
assert.Equal(t, "", errStr)
assert.True(t, isTimeout)
assert.NoError(t, err)
// provide wrong password and correct private key
ssh = &MakeConfig{
Server: "localhost",
User: "drone-scp",
Port: "22",
Password: "123456",
KeyPath: "./tests/.ssh/id_rsa",
}
outStr, errStr, isTimeout, err = ssh.Run("whoami")
assert.Equal(t, "drone-scp\n", outStr)
assert.Equal(t, "", errStr)
assert.True(t, isTimeout)
assert.NoError(t, err)
}
func TestRunCommand(t *testing.T) {
// wrong key
ssh := &MakeConfig{
Server: "localhost",
User: "drone-scp",
Port: "22",
KeyPath: "./tests/.ssh/id_rsa.pub",
}
outStr, errStr, isTimeout, err := ssh.Run("whoami", 10)
assert.Equal(t, "", outStr)
assert.Equal(t, "", errStr)
assert.False(t, isTimeout)
assert.Error(t, err)
ssh = &MakeConfig{
Server: "localhost",
User: "drone-scp",
Port: "22",
KeyPath: "./tests/.ssh/id_rsa",
}
outStr, errStr, isTimeout, err = ssh.Run("whoami")
assert.Equal(t, "drone-scp\n", outStr)
assert.Equal(t, "", errStr)
assert.True(t, isTimeout)
assert.NoError(t, err)
// error message: not found
outStr, errStr, isTimeout, err = ssh.Run("whoami1234")
assert.Equal(t, "", outStr)
assert.Equal(t, "sh: whoami1234: not found\n", errStr)
assert.True(t, isTimeout)
// Process exited with status 127
assert.Error(t, err)
// error message: Run Command Timeout
outStr, errStr, isTimeout, err = ssh.Run("sleep 2", 1*time.Second)
assert.Equal(t, "", outStr)
assert.Equal(t, "", errStr)
assert.False(t, isTimeout)
assert.Error(t, err)
assert.Equal(t, "Run Command Timeout", err.Error())
// test exit code
outStr, errStr, isTimeout, err = ssh.Run("exit 1")
assert.Equal(t, "", outStr)
assert.Equal(t, "", errStr)
assert.True(t, isTimeout)
// Process exited with status 1
assert.Error(t, err)
}
func TestSCPCommand(t *testing.T) {
// wrong key
ssh := &MakeConfig{
Server: "localhost",
User: "drone-scp",
Port: "22",
KeyPath: "./tests/.ssh/id_rsa.pub",
}
err := ssh.Scp("./tests/a.txt", "a.txt")
assert.Error(t, err)
ssh = &MakeConfig{
Server: "localhost",
User: "drone-scp",
Port: "22",
KeyPath: "./tests/.ssh/id_rsa",
}
err = ssh.Scp("./tests/a.txt", "a.txt")
assert.NoError(t, err)
u, err := user.Lookup("drone-scp")
if err != nil {
t.Fatalf("Lookup: %v", err)
}
// check file exist
if _, err := os.Stat(path.Join(u.HomeDir, "a.txt")); os.IsNotExist(err) {
t.Fatalf("SCP-error: %v", err)
}
}
func TestSCPCommandWithKey(t *testing.T) {
ssh := &MakeConfig{
Server: "localhost",
User: "drone-scp",
Port: "22",
Key: `-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEA4e2D/qPN08pzTac+a8ZmlP1ziJOXk45CynMPtva0rtK/RB26
VbfAF0hIJji7ltvnYnqCU9oFfvEM33cTn7T96+od8ib/Vz25YU8ZbstqtIskPuwC
bv3K0mAHgsviJyRD7yM+QKTbBQEgbGuW6gtbMKhiYfiIB4Dyj7AdS/fk3v26wDgz
7SHI5OBqu9bv1KhxQYdFEnU3PAtAqeccgzNpbH3eYLyGzuUxEIJlhpZ/uU2G9ppj
/cSrONVPiI8Ahi4RrlZjmP5l57/sq1ClGulyLpFcMw68kP5FikyqHpHJHRBNgU57
1y0Ph33SjBbs0haCIAcmreWEhGe+/OXnJe6VUQIDAQABAoIBAH97emORIm9DaVSD
7mD6DqA7c5m5Tmpgd6eszU08YC/Vkz9oVuBPUwDQNIX8tT0m0KVs42VVPIyoj874
bgZMJoucC1G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ur7Bt4XH1
7XC9wlRna4b3Ln8ew3q1ZcBjXwD4ppbTlmwAfQIaZTGJUgQbdsO9YA==
-----END RSA PRIVATE KEY-----
`,
}
// source file not found
err := ssh.Scp("./tests/test.txt", "a.txt")
assert.Error(t, err)
// target file not found ex: appleboy folder not found
err = ssh.Scp("./tests/a.txt", "/appleboy/a.txt")
assert.Error(t, err)
err = ssh.Scp("./tests/a.txt", "a.txt")
assert.NoError(t, err)
u, err := user.Lookup("drone-scp")
if err != nil {
t.Fatalf("Lookup: %v", err)
}
// check file exist
if _, err := os.Stat(path.Join(u.HomeDir, "a.txt")); os.IsNotExist(err) {
t.Fatalf("SCP-error: %v", err)
}
}
func TestProxyClient(t *testing.T) {
ssh := &MakeConfig{
Server: "localhost",
User: "drone-scp",
Port: "22",
Password: "1234",
Proxy: DefaultConfig{
User: "drone-scp",
Server: "localhost",
Port: "22",
Password: "123456",
},
}
// password of proxy client is incorrect.
// can't connect proxy server
session, client, err := ssh.Connect()
assert.Nil(t, session)
assert.Nil(t, client)
assert.Error(t, err)
ssh = &MakeConfig{
Server: "www.che.ccu.edu.tw",
User: "drone-scp",
Port: "228",
Password: "123456",
Proxy: DefaultConfig{
User: "drone-scp",
Server: "localhost",
Port: "22",
KeyPath: "./tests/.ssh/id_rsa",
},
}
// proxy client can't dial to target server
session, client, err = ssh.Connect()
assert.Nil(t, session)
assert.Nil(t, client)
assert.Error(t, err)
ssh = &MakeConfig{
Server: "localhost",
User: "drone-scp",
Port: "22",
Password: "123456",
Proxy: DefaultConfig{
User: "drone-scp",
Server: "localhost",
Port: "22",
KeyPath: "./tests/.ssh/id_rsa",
},
}
// proxy client can't create new client connection of target
session, client, err = ssh.Connect()
assert.Nil(t, session)
assert.Nil(t, client)
assert.Error(t, err)
ssh = &MakeConfig{
User: "drone-scp",
Server: "localhost",
Port: "22",
KeyPath: "./tests/.ssh/id_rsa",
Proxy: DefaultConfig{
User: "drone-scp",
Server: "localhost",
Port: "22",
KeyPath: "./tests/.ssh/id_rsa",
},
}
session, client, err = ssh.Connect()
assert.NotNil(t, session)
assert.NotNil(t, client)
assert.NoError(t, err)
}
func TestProxyClientSSHCommand(t *testing.T) {
ssh := &MakeConfig{
User: "drone-scp",
Server: "localhost",
Port: "22",
KeyPath: "./tests/.ssh/id_rsa",
Proxy: DefaultConfig{
User: "drone-scp",
Server: "localhost",
Port: "22",
KeyPath: "./tests/.ssh/id_rsa",
},
}
outStr, errStr, isTimeout, err := ssh.Run("whoami")
assert.Equal(t, "drone-scp\n", outStr)
assert.Equal(t, "", errStr)
assert.True(t, isTimeout)
assert.NoError(t, err)
}
func TestSCPCommandWithPassword(t *testing.T) {
ssh := &MakeConfig{
Server: "localhost",
User: "drone-scp",
Port: "22",
Password: "1234",
Timeout: 60 * time.Second,
}
err := ssh.Scp("./tests/b.txt", "b.txt")
assert.NoError(t, err)
u, err := user.Lookup("drone-scp")
if err != nil {
t.Fatalf("Lookup: %v", err)
}
// check file exist
if _, err := os.Stat(path.Join(u.HomeDir, "b.txt")); os.IsNotExist(err) {
t.Fatalf("SCP-error: %v", err)
}
}
func TestWrongRawKey(t *testing.T) {
// wrong key
ssh := &MakeConfig{
Server: "localhost",
User: "drone-scp",
Port: "22",
Key: "appleboy",
}
outStr, errStr, isTimeout, err := ssh.Run("whoami")
assert.Equal(t, "", outStr)
assert.Equal(t, "", errStr)
assert.False(t, isTimeout)
assert.Error(t, err)
}
func TestExitCode(t *testing.T) {
ssh := &MakeConfig{
Server: "localhost",
User: "drone-scp",
Port: "22",
KeyPath: "./tests/.ssh/id_rsa",
Timeout: 60 * time.Second,
}
outStr, errStr, isTimeout, err := ssh.Run("set -e;echo 1; mkdir a;mkdir a;echo 2")
assert.Equal(t, "1\n", outStr)
assert.Equal(t, "mkdir: can't create directory 'a': File exists\n", errStr)
assert.True(t, isTimeout)
assert.Error(t, err)
}
func TestSSHWithPassphrase(t *testing.T) {
ssh := &MakeConfig{
Server: "localhost",
User: "drone-scp",
Port: "22",
KeyPath: "./tests/.ssh/test",
Passphrase: "1234",
Timeout: 60 * time.Second,
}
outStr, errStr, isTimeout, err := ssh.Run("set -e;echo 1; mkdir test1234;mkdir test1234;echo 2")
assert.Equal(t, "1\n", outStr)
assert.Equal(t, "mkdir: can't create directory 'test1234': File exists\n", errStr)
assert.True(t, isTimeout)
assert.Error(t, err)
}
func TestSCPCommandUseInsecureCipher(t *testing.T) {
ssh := &MakeConfig{
Server: "localhost",
User: "drone-scp",
Port: "22",
KeyPath: "./tests/.ssh/id_rsa",
UseInsecureCipher: true,
}
err := ssh.Scp("./tests/a.txt", "a.txt")
assert.NoError(t, err)
u, err := user.Lookup("drone-scp")
if err != nil {
t.Fatalf("Lookup: %v", err)
}
// check file exist
if _, err := os.Stat(path.Join(u.HomeDir, "a.txt")); os.IsNotExist(err) {
t.Fatalf("SCP-error: %v", err)
}
}
// TestRootAccount test root account
func TestRootAccount(t *testing.T) {
ssh := &MakeConfig{
Server: "localhost",
User: "root",
Port: "22",
KeyPath: "./tests/.ssh/id_rsa",
}
outStr, errStr, isTimeout, err := ssh.Run("whoami")
assert.Equal(t, "root\n", outStr)
assert.Equal(t, "", errStr)
assert.True(t, isTimeout)
assert.NoError(t, err)
}
// TestSudoCommand
func TestSudoCommand(t *testing.T) {
ssh := &MakeConfig{
Server: "localhost",
User: "drone-scp",
Port: "22",
KeyPath: "./tests/.ssh/id_rsa",
RequestPty: true,
}
outStr, errStr, isTimeout, err := ssh.Run(`sudo su - -c "whoami"`)
assert.Equal(t, "root\r\n", outStr)
assert.Equal(t, "", errStr)
assert.True(t, isTimeout)
assert.NoError(t, err)
}