forked from travis-ci/worker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
package.go
153 lines (123 loc) · 3.36 KB
/
package.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
package ssh
import (
"crypto"
"crypto/x509"
"encoding/pem"
"io"
"io/ioutil"
"time"
"golang.org/x/crypto/ssh"
"github.com/pkg/errors"
"github.com/pkg/sftp"
)
type Dialer interface {
Dial(address, username string, timeout time.Duration) (Connection, error)
}
type Connection interface {
UploadFile(path string, data []byte) (bool, error)
RunCommand(command string, output io.Writer) (uint8, error)
Close() error
}
func FormatPublicKey(key interface{}) ([]byte, error) {
pubKey, err := ssh.NewPublicKey(key)
if err != nil {
return nil, errors.Wrap(err, "couldn't use public key")
}
return ssh.MarshalAuthorizedKey(pubKey), nil
}
type AuthDialer struct {
authMethods []ssh.AuthMethod
}
func NewDialerWithKey(key crypto.Signer) (*AuthDialer, error) {
signer, err := ssh.NewSignerFromKey(key)
if err != nil {
return nil, errors.Wrap(err, "couldn't create signer from SSH key")
}
return &AuthDialer{
authMethods: []ssh.AuthMethod{ssh.PublicKeys(signer)},
}, nil
}
func NewDialerWithPassword(password string) (*AuthDialer, error) {
return &AuthDialer{
authMethods: []ssh.AuthMethod{ssh.Password(password)},
}, nil
}
func NewDialer(keyPath, keyPassphrase string) (*AuthDialer, error) {
file, err := ioutil.ReadFile(keyPath)
if err != nil {
return nil, errors.Wrap(err, "couldn't read SSH key")
}
block, _ := pem.Decode(file)
if block == nil {
return nil, errors.Errorf("ssh key does not contain a valid PEM block")
}
der, err := x509.DecryptPEMBlock(block, []byte(keyPassphrase))
if err != nil {
return nil, errors.Wrap(err, "couldn't decrypt SSH key")
}
key, err := x509.ParsePKCS1PrivateKey(der)
if err != nil {
return nil, errors.Wrap(err, "couldn't parse SSH key")
}
return NewDialerWithKey(key)
}
func (d *AuthDialer) Dial(address, username string, timeout time.Duration) (Connection, error) {
client, err := ssh.Dial("tcp", address, &ssh.ClientConfig{
User: username,
Auth: d.authMethods,
Timeout: timeout,
})
if err != nil {
return nil, errors.Wrap(err, "couldn't connect to SSH server")
}
return &sshConnection{client: client}, nil
}
type sshConnection struct {
client *ssh.Client
}
func (c *sshConnection) UploadFile(path string, data []byte) (bool, error) {
sftp, err := sftp.NewClient(c.client)
if err != nil {
return false, errors.Wrap(err, "couldn't create SFTP client")
}
defer sftp.Close()
_, err = sftp.Lstat(path)
if err == nil {
return true, errors.New("file already existed")
}
f, err := sftp.Create(path)
if err != nil {
return false, errors.Wrap(err, "couldn't create file")
}
_, err = f.Write(data)
if err != nil {
return false, errors.Wrap(err, "couldn't write contents to file")
}
return false, nil
}
func (c *sshConnection) RunCommand(command string, output io.Writer) (uint8, error) {
session, err := c.client.NewSession()
if err != nil {
return 0, errors.Wrap(err, "error creating SSH session")
}
defer session.Close()
err = session.RequestPty("xterm", 40, 80, ssh.TerminalModes{})
if err != nil {
return 0, errors.Wrap(err, "error requesting PTY")
}
session.Stdout = output
session.Stderr = output
err = session.Run(command)
if err == nil {
return 0, nil
}
switch err := err.(type) {
case *ssh.ExitError:
return uint8(err.ExitStatus()), nil
default:
return 0, errors.Wrap(err, "error running script")
}
}
func (c *sshConnection) Close() error {
return c.client.Close()
}