Newer
Older
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
package libvirt
// SSH helpers used in tests.
import (
"bytes"
"crypto/ed25519"
"fmt"
"io"
"net"
"strings"
"github.com/pkg/sftp"
"go4.org/writerutil"
"golang.org/x/crypto/ssh"
)
func proxyJump(c *ssh.Client, addr string, config *ssh.ClientConfig) (cc *ssh.Client, err error) {
defer func() {
if err != nil {
err = fmt.Errorf("proxyJump %s: %w", addr, err)
}
}()
conn, err := c.Dial("tcp", net.JoinHostPort(addr, "22"))
if err != nil {
return nil, err
}
sshConn, chans, reqs, err := ssh.NewClientConn(conn, addr, config)
if err != nil {
conn.Close()
return nil, err
}
return ssh.NewClient(sshConn, chans, reqs), nil
}
func runCommand(c *ssh.Client, name string, args ...string) ([]byte, error) {
var b strings.Builder
b.WriteString(shellQuote(name))
for _, a := range args {
b.WriteByte(' ')
b.WriteString(shellQuote(a))
}
cmd := b.String()
sess, err := c.NewSession()
if err != nil {
return nil, err
}
defer sess.Close()
var stdout bytes.Buffer
stderr := &writerutil.PrefixSuffixSaver{N: 1024}
sess.Stdout = &stdout
sess.Stderr = stderr
if err := sess.Run(cmd); err != nil {
if msg := stderr.Bytes(); len(msg) > 0 {
return nil, fmt.Errorf("runCommand: %w | %s |", err, msg)
}
return nil, fmt.Errorf("runCommand: %w", err)
}
return stdout.Bytes(), nil
}
func sftpGet(conn *ssh.Client, path string) (content []byte, err error) {
c, err := sftp.NewClient(conn)
if err != nil {
return nil, err
}
defer c.Close()
fd, err := c.Open(path)
if err != nil {
return nil, err
}
defer fd.Close()
return io.ReadAll(fd)
}
func sftpPutReader(conn *ssh.Client, dstPath string, src io.Reader) (err error) {
c, err := sftp.NewClient(conn)
if err != nil {
return err
}
defer c.Close()
fd, err := c.Create(dstPath)
if err != nil {
return err
}
defer func() {
if cerr := fd.Close(); err == nil {
err = cerr
}
}()
_, err = io.Copy(fd, src)
return err
}
func sftpPut(conn *ssh.Client, dstPath string, content []byte) (err error) {
return sftpPutReader(conn, dstPath, bytes.NewReader(content))
}
func sshKeygen(rand io.Reader) (ssh.Signer, []byte, error) {
_, sk, err := ed25519.GenerateKey(rand)
if err != nil {
return nil, nil, err
}
signer, err := ssh.NewSignerFromSigner(sk)
if err != nil {
return nil, nil, err
}
sshPubKey := ssh.MarshalAuthorizedKey(signer.PublicKey())
return signer, sshPubKey, nil
}
// ShellQuote returns s in a form suitable to pass it to the shell as an
// argument. Obviously, it works for Bourne-like shells only. The way this
// works is that first the whole string is enclosed in single quotes. Now the
// only character that needs special handling is the single quote itself. We
// replace it by '\'' (the outer quotes are part of the replacement) and make
// use of the fact that the shell concatenates adjacent strings.
func shellQuote(s string) string {
t := strings.Replace(s, "'", `'\''`, -1)
return "'" + t + "'"
}