-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsshdb.go
More file actions
276 lines (248 loc) · 8.21 KB
/
sshdb.go
File metadata and controls
276 lines (248 loc) · 8.21 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// Copyright 2021 James Cote
// All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package sshdb provides database connections to tunnel
// through an ssh connection to a remove server
package sshdb
import (
"context"
"database/sql/driver"
"errors"
"fmt"
"net"
"strings"
"sync"
"time"
"golang.org/x/crypto/ssh"
)
// Driver creates a Connector for a specific database type
// using the dialer passed from a Tunnel and a dsn string that
// defines a connection from the remote server to the desired
// database.
//
// This package includes drivers for mysql, mssql, postgress (v3 and v4).
type Driver interface {
OpenConnector(dialer Dialer, dsn string) (driver.Connector, error)
Name() string
}
// Dialer creates a net.Conn via the tunnel's ssh client
type Dialer interface {
DialContext(context.Context, string, string) (net.Conn, error)
}
// DialerFunc allows a func to fulfill the Dialer interface.
type DialerFunc func(context.Context, string, string) (net.Conn, error)
// DialContext calls the underlying dialerfunc.
func (d DialerFunc) DialContext(ctx context.Context, net, addr string) (net.Conn, error) {
return d(ctx, net, addr)
}
// New returns a Tunnel based upon the ssh clientConfig for creating new connectors/connections
// via an ssh client connection. The tunnel can host multiple db connections to different
// database servers. the remoteHostPort defines the remote ssh server address and must be in
// the form "host:port", "host%zone:port", [host]:port" or "[host%zone]:port". See func net.Dial
// for a more detailed description of the hostport format.
func New(clientConfig *ssh.ClientConfig, remoteHostPort string) (*Tunnel, error) {
if clientConfig == nil {
return nil, errors.New("clientConfig may not be nil")
}
if strings.Trim(remoteHostPort, " ") == "" {
return nil, errors.New("remoteAddr may not be empty")
}
if _, _, err := net.SplitHostPort(remoteHostPort); err != nil {
return nil, fmt.Errorf("invalid address - %w", err)
}
resetChan := make(chan struct{})
close(resetChan) // close to prevent reset calls prior to client initialization
return &Tunnel{
cfg: clientConfig,
addr: remoteHostPort,
connectors: make(map[string]driver.Connector),
sshconns: make(map[*sshConn]bool),
resetChan: resetChan,
}, nil
}
// Tunnel manages an ssh client connections and
// creates and tracks db connections made through the client
type Tunnel struct {
cfg *ssh.ClientConfig
addr string // format <hostname>:<port>
connectors map[string]driver.Connector // map of dsn to connector
ignoreSetDeadlineRequest bool
mConn sync.Mutex // protects connectors and ignoreDeadlineError
sshconns map[*sshConn]bool // initialized on dialcontext
client *ssh.Client
resetChan chan struct{} // closed at reset
m sync.Mutex //protects sshconns, client and resetChan
}
// IgnoreSetDeadlineRequest exists because the ssh client package does not support
// deadlines and returns an error if attempting to set a deadline. If existing
// code contains setdeadline calls, pass true to this functions, and the tunnel
// ignore deadline requests.
func (tun *Tunnel) IgnoreSetDeadlineRequest(val bool) {
tun.mConn.Lock()
tun.ignoreSetDeadlineRequest = val
tun.mConn.Unlock()
}
// OpenConnector fulfills the driver DriverContext interface and returns a new
// db connection via the ssh client connection. The dataSourceName should follow
// rules of the base database and must create the connection as if connecting from
// the remote ssh server.
func (tun *Tunnel) OpenConnector(tunnelDriver Driver, dataSourceName string) (driver.Connector, error) {
tun.mConn.Lock()
defer tun.mConn.Unlock()
connectorName := tunnelDriver.Name() + ":" + dataSourceName
if connector, ok := tun.connectors[connectorName]; ok {
return connector, nil
}
dbconnector, err := tunnelDriver.OpenConnector(DialerFunc(tun.DialContext), dataSourceName)
if err != nil {
return nil, err
}
tun.connectors[connectorName] = dbconnector
return dbconnector, nil
}
// Close safely resets the tunnel. If calling func has already
// locked tunnel.m, it should call reset directly.
func (tun *Tunnel) Close() error {
tun.m.Lock()
err := tun.reset()
tun.m.Unlock()
return err
}
// DialContext creates an ssh client connection to the addr. sshdb drivers must use this
// func when creating driver.Connectors. You may use this func to establish "raw" connections
// to a remote service.
func (tun *Tunnel) DialContext(ctx context.Context, _, addr string) (net.Conn, error) {
// lock sd for the duration
tun.m.Lock()
defer tun.m.Unlock()
ctxchan := ctx.Done()
select {
// check for timeout or cancel of ctx
case <-ctxchan:
return nil, ctx.Err()
// if tunnel is not open create new tunnel
case <-tun.resetChan:
// create tunnel ssh client connection
cl, err := ssh.Dial("tcp", tun.addr, tun.cfg)
if err != nil {
return nil, err
}
select {
case <-ctxchan:
cl.Close() // if context cancelled, close new client connection
return nil, ctx.Err()
default:
}
tun.client = cl
clientResetChannel := make(chan struct{})
tun.resetChan = clientResetChannel
go func() {
// if client connection close (network error)
// reset channel to close all db connections
_ = cl.Wait()
select {
case <-clientResetChannel:
return
default:
tun.Close()
}
}()
default:
}
// make connection
return tun.getNetConn(addr)
}
// reset closes the tunnel's client connection and closes
// all existing db connections. Routines must obtain a lock
// on tunnel.m prior to calling. After reset, the tunnel can
// still create new connections and existing connectors are
// valid.
func (tun *Tunnel) reset() error {
select {
case <-tun.resetChan:
// do nothing
default:
// close channel to prevent duplicate resets
close(tun.resetChan)
for k := range tun.sshconns {
k.Conn.Close()
}
tun.sshconns = make(map[*sshConn]bool)
if tun.client != nil {
return tun.client.Close()
}
}
return nil
}
// getNetConn create a client connection through the tunnel
func (tun *Tunnel) getNetConn(addr string) (net.Conn, error) {
network := "tcp"
if len(addr) > 0 && addr[0] == '/' {
network = "unix"
}
conn, err := tun.client.Dial(network, addr)
if err != nil {
return nil, err
}
sshconn := &sshConn{
tunnel: tun,
Conn: conn,
}
tun.sshconns[sshconn] = true
return sshconn, nil
}
// ConnCount returns number of active db connections
// managed by the tunnel
func (tun *Tunnel) ConnCount() int {
tun.m.Lock()
cnt := len(tun.sshconns)
tun.m.Unlock()
return cnt
}
// handleDeadline uses the ignoreDeadlineError to determine whether to
// handle a setdeadline call on a sshConn
func (tun *Tunnel) handleDeadline(tm time.Time, setDeadline func(time.Time) error) error {
var err error
tun.mConn.Lock()
if !tun.ignoreSetDeadlineRequest {
err = setDeadline(tm)
}
tun.mConn.Unlock()
return err
}
type sshConn struct {
tunnel *Tunnel
net.Conn
}
// Close resets tunnel if last connection other wise
// closes connection and updates tunnel ssh connections
// map.
func (sc *sshConn) Close() error {
tunnel := sc.tunnel
tunnel.m.Lock()
defer tunnel.m.Unlock()
if len(tunnel.sshconns) > 1 {
delete(tunnel.sshconns, sc)
return sc.Conn.Close()
}
return tunnel.reset()
}
// SetDeadline is not implemented by the ssh tcp connection. If
// the tunnel Driver implements ingnoreDeadlineError then a nil is
// returned rather than a not implemented error.
func (sc *sshConn) SetDeadline(tm time.Time) error {
return sc.tunnel.handleDeadline(tm, sc.Conn.SetDeadline)
}
// SetReadDeadline is not implemented by the ssh tcp connection. If
// the tunnel Driver implements ingnoreDeadlineError then a nil is
// returned rather than a not implemented error.
func (sc *sshConn) SetReadDeadline(tm time.Time) error {
return sc.tunnel.handleDeadline(tm, sc.Conn.SetReadDeadline)
}
// SetDeadline is not implemented by the ssh tcp connection. If
// the tunnel Driver implements ingnoreDeadlineError then a nil is
// returned rather than a not implemented error.
func (sc *sshConn) SetWriteDeadline(tm time.Time) error {
return sc.tunnel.handleDeadline(tm, sc.Conn.SetWriteDeadline)
}