-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathssh.go
111 lines (94 loc) · 2.63 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
package massh
/*
The functions here are mostly abstractions to make things easier to adapt to new methods.
*/
import (
"fmt"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/agent"
"net"
"os"
)
const (
sshAuthSockEnv = "SSH_AUTH_SOCK"
)
// dial is ssh.Dial
func dial(network, host, port string, config *ssh.ClientConfig) (*ssh.Client, error) {
c, err := ssh.Dial(network, host+":"+port, config)
if err != nil {
return nil, err
}
return c, nil
}
func dialViaBastionClient(network string, bastionHost string, remoteHost string, port string, config *ssh.ClientConfig) (*ssh.Client, error) {
bastionClient, err := ssh.Dial(network, bastionHost+":"+port, config)
if err != nil {
return nil, fmt.Errorf("unable to connect to bastion: %s", err)
}
remoteHostConn, err := bastionClient.Dial(network, remoteHost+":"+port)
if err != nil {
return nil, fmt.Errorf("unable to connect to remote host: %s", err)
}
ncc, chans, reqs, err := ssh.NewClientConn(remoteHostConn, remoteHost+":"+port, config)
if err != nil {
return nil, fmt.Errorf("unable to create remote host ssh client through bastion: %s", err)
}
clientThroughBastion := ssh.NewClient(ncc, chans, reqs)
return clientThroughBastion, nil
}
// newClientSession is ssh.Client.NewSession
func newClientSession(client *ssh.Client) (*ssh.Session, error) {
session, err := client.NewSession()
if err != nil {
return nil, err
}
return session, nil
}
func generateSSHClientWithPotentialBastion(host string, config *Config) (*ssh.Client, error) {
if config.BastionHost != "" {
var sshConf *ssh.ClientConfig
if config.BastionHostSSHConfig != nil {
sshConf = config.BastionHostSSHConfig
} else {
sshConf = config.SSHConfig
}
client, err := dialViaBastionClient("tcp", config.BastionHost, host, sshPort, sshConf)
if err != nil {
return nil, err
}
return client, nil
}
client, err := dial("tcp", host, sshPort, config.SSHConfig)
if err != nil {
return nil, err
}
return client, nil
}
// runJob is ssh.Session.Run
func runJob(session *ssh.Session, job string) error {
if err := session.Run(job); err != nil {
return err
}
return nil
}
// startJob is ssh.Session.Start
func startJob(session *ssh.Session, job string) error {
if err := session.Start(job); err != nil {
return err
}
return nil
}
// startJob is ssh.Session.Close
func closeSession(session *ssh.Session) error {
if err := session.Close(); err != nil {
return err
}
return nil
}
func sshAuthSock() (ssh.AuthMethod, error) {
sshAgent, err := net.Dial("unix", os.Getenv(sshAuthSockEnv))
if err != nil {
return nil, err
}
return ssh.PublicKeysCallback(agent.NewClient(sshAgent).Signers), nil
}