forked from crosbymichael/slex
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathssh.go
More file actions
204 lines (172 loc) · 4.71 KB
/
ssh.go
File metadata and controls
204 lines (172 loc) · 4.71 KB
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
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
// host to connect to
host string
*ssh.ClientConfig
}
// updateFromSshConfigFile updates the host, username and agentforwarding parameters
// from the ~/.ssh/config if there is a matching section
func updateFromSshConfigFile(section *SshConfigFileSection, host, userName *string, agentForwarding *bool) {
hostName, port, err := net.SplitHostPort(*host)
if err != nil {
return
}
if section.ForwardAgent == "yes" {
*agentForwarding = true
} else if section.ForwardAgent == "no" {
*agentForwarding = false
}
if section.User != "" {
*userName = section.User
}
if section.HostName != "" {
hostName = section.HostName
}
if section.Port != "" {
port = section.Port
}
*host = net.JoinHostPort(hostName, port)
}
// newSshClientConfig initializes the ssh configuration.
// It connects with the ssh agent when agent forwarding is enabled.
func newSshClientConfig(host string, section *SshConfigFileSection, userName, identity string, agentForwarding bool) (*sshClientConfig, error) {
var (
config *sshClientConfig
err error
)
if section != nil {
updateFromSshConfigFile(section, &host, &userName, &agentForwarding)
}
if agentForwarding {
config, err = newSshAgentConfig(userName)
} else {
config, err = newSshDefaultConfig(userName, identity)
}
if config != nil {
config.host = host
}
return config, err
}
// 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 := loadIdentity(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
}
// loadIdentity returns the private key file's contents
func loadIdentity(userName, identity string) ([]byte, error) {
if filepath.Dir(identity) == "." {
u, err := user.Current()
if err != nil {
return nil, err
}
identity = filepath.Join(u.HomeDir, ".ssh", identity)
}
return ioutil.ReadFile(identity)
}