forked from crosbymichael/slex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathssh.go
159 lines (132 loc) · 3.7 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
package main
import (
"errors"
"io/ioutil"
"net"
"os"
"os/user"
"path/filepath"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/agent"
)
// sshSession stores the open session and connection to execute a command.
type sshSession struct {
// conn is the ssh client that started the session.
conn *ssh.Client
*ssh.Session
}
// Close closses the open ssh session and connection.
func (s *sshSession) Close() {
s.Session.Close()
s.conn.Close()
}
// sshClientConfig stores the configuration
// and the ssh agent to forward authentication requests
type sshClientConfig struct {
// agent is the connection to the ssh agent
agent agent.Agent
*ssh.ClientConfig
}
// newSshClientConfig initializes the ssh configuration.
// It connects with the ssh agent when agent forwarding is enabled.
func newSshClientConfig(userName, identity string, agentForwarding bool) (*sshClientConfig, error) {
if agentForwarding {
return newSshAgentConfig(userName)
}
return newSshDefaultConfig(userName, identity)
}
// newSshAgentConfig initializes the configuration to talk with an ssh agent.
func newSshAgentConfig(userName string) (*sshClientConfig, error) {
agent, err := newAgent()
if err != nil {
return nil, err
}
config, err := sshAgentConfig(userName, agent)
if err != nil {
return nil, err
}
return &sshClientConfig{
agent: agent,
ClientConfig: config,
}, nil
}
// newSshDefaultConfig initializes the configuration to use an ideitity file.
func newSshDefaultConfig(userName, identity string) (*sshClientConfig, error) {
config, err := sshDefaultConfig(userName, identity)
if err != nil {
return nil, err
}
return &sshClientConfig{ClientConfig: config}, nil
}
// NewSession creates a new ssh session with the host.
// It forwards authentication to the agent when it's configured.
func (s *sshClientConfig) NewSession(host string) (*sshSession, error) {
conn, err := ssh.Dial("tcp", host, s.ClientConfig)
if err != nil {
return nil, err
}
if s.agent != nil {
if err := agent.ForwardToAgent(conn, s.agent); err != nil {
return nil, err
}
}
session, err := conn.NewSession()
if s.agent != nil {
err = agent.RequestAgentForwarding(session)
}
return &sshSession{
conn: conn,
Session: session,
}, err
}
// newAgent connects with the SSH agent in the to forward authentication requests.
func newAgent() (agent.Agent, error) {
sock := os.Getenv("SSH_AUTH_SOCK")
if sock == "" {
return nil, errors.New("Unable to connect to the ssh agent. Please, check that SSH_AUTH_SOCK is set and the ssh agent is running")
}
conn, err := net.Dial("unix", sock)
if err != nil {
return nil, err
}
return agent.NewClient(conn), nil
}
// sshAgentConfig creates a new configuration for the ssh client
// with the signatures from the ssh agent.
func sshAgentConfig(userName string, a agent.Agent) (*ssh.ClientConfig, error) {
signers, err := a.Signers()
if err != nil {
return nil, err
}
return &ssh.ClientConfig{
User: userName,
Auth: []ssh.AuthMethod{
ssh.PublicKeys(signers...),
},
}, nil
}
// sshDefaultConfig returns the SSH client config for the connection
func sshDefaultConfig(userName, identity string) (*ssh.ClientConfig, error) {
contents, err := loadDefaultIdentity(userName, identity)
if err != nil {
return nil, err
}
signer, err := ssh.ParsePrivateKey(contents)
if err != nil {
return nil, err
}
return &ssh.ClientConfig{
User: userName,
Auth: []ssh.AuthMethod{
ssh.PublicKeys(signer),
},
}, nil
}
// loadDefaultIdentity returns the private key file's contents
func loadDefaultIdentity(userName, identity string) ([]byte, error) {
u, err := user.Current()
if err != nil {
return nil, err
}
return ioutil.ReadFile(filepath.Join(u.HomeDir, ".ssh", identity))
}