-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
579 lines (516 loc) · 14.1 KB
/
client.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
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
/*
Copyright 2015 Gravitational, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package client
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/gravitational/teleport"
"github.com/gravitational/teleport/lib/auth"
"github.com/gravitational/teleport/lib/defaults"
"github.com/gravitational/teleport/lib/services"
"github.com/gravitational/teleport/lib/session"
"github.com/gravitational/teleport/lib/sshutils"
"github.com/gravitational/teleport/lib/sshutils/scp"
"github.com/gravitational/teleport/lib/utils"
log "github.com/Sirupsen/logrus"
"github.com/docker/docker/pkg/term"
"github.com/gravitational/roundtrip"
"github.com/gravitational/trace"
"golang.org/x/crypto/ssh"
)
// ProxyClient implements ssh client to a teleport proxy
// It can provide list of nodes or connect to nodes
type ProxyClient struct {
Client *ssh.Client
hostLogin string
proxyAddress string
hostKeyCallback utils.HostKeyCallback
authMethods []ssh.AuthMethod
siteName string
}
// NodeClient implements ssh client to a ssh node (teleport or any regular ssh node)
// NodeClient can run shell and commands or upload and download files.
type NodeClient struct {
Client *ssh.Client
Proxy *ProxyClient
}
func (proxy *ProxyClient) getSite() (*services.Site, error) {
sites, err := proxy.GetSites()
if err != nil {
return nil, trace.Wrap(err)
}
if len(sites) == 0 {
return nil, trace.NotFound("no sites registered")
}
if proxy.siteName == "" {
return &sites[0], nil
}
for _, site := range sites {
if site.Name == proxy.siteName {
return &site, nil
}
}
return nil, trace.NotFound("site %v not found", proxy.siteName)
}
// GetSites returns list of the "sites" (AKA teleport clusters) connected to the proxy
// Each site is returned as an instance of its auth server
//
func (proxy *ProxyClient) GetSites() ([]services.Site, error) {
proxySession, err := proxy.Client.NewSession()
if err != nil {
return nil, trace.Wrap(err)
}
stdout := &bytes.Buffer{}
reader, err := proxySession.StdoutPipe()
done := make(chan struct{})
go func() {
io.Copy(stdout, reader)
close(done)
}()
if err := proxySession.RequestSubsystem("proxysites"); err != nil {
return nil, trace.Wrap(err)
}
select {
case <-done:
case <-time.After(teleport.DefaultTimeout):
return nil, trace.Errorf("timeout")
}
log.Infof("proxyClient.GetSites() returned: %v", stdout.String())
var sites []services.Site
if err := json.Unmarshal(stdout.Bytes(), &sites); err != nil {
return nil, trace.Wrap(err)
}
return sites, nil
}
// FindServersByLabels returns list of the nodes which have labels exactly matching
// the given label set.
//
// A server is matched when ALL labels match.
// If no labels are passed, ALL nodes are returned.
func (proxy *ProxyClient) FindServersByLabels(labels map[string]string) ([]services.Server, error) {
nodes := make([]services.Server, 0)
siteInfo, err := proxy.getSite()
if err != nil {
return nil, trace.Wrap(err)
}
site, err := proxy.ConnectToSite(siteInfo.Name, proxy.hostLogin)
if err != nil {
return nil, trace.Wrap(err)
}
siteNodes, err := site.GetNodes()
if err != nil {
return nil, trace.Wrap(err)
}
// look at every node on this site and see which ones match:
for _, node := range siteNodes {
if node.MatchAgainst(labels) {
nodes = append(nodes, node)
}
}
return nodes, nil
}
// ConnectToSite connects to the auth server of the given site via proxy.
// It returns connected and authenticated auth server client
func (proxy *ProxyClient) ConnectToSite(siteName string, user string) (auth.ClientI, error) {
// this connects us to a node which is an auth server for this site
// note the addres we're using: "@sitename", which in practice looks like "@{site-global-id}"
// the Teleport proxy interprets such address as a request to connec to the active auth server
// of the named site
nodeClient, err := proxy.ConnectToNode("@"+siteName, user)
if err != nil {
log.Error(err)
return nil, trace.Wrap(err)
}
clt, err := auth.NewClient(
"http://stub:0",
roundtrip.HTTPClient(&http.Client{
Transport: &http.Transport{
Dial: func(network, addr string) (net.Conn, error) {
return nodeClient.Client.Dial(network, addr)
},
},
}))
if err != nil {
return nil, trace.Wrap(err)
}
return clt, nil
}
// ConnectToNode connects to the ssh server via Proxy.
// It returns connected and authenticated NodeClient
func (proxy *ProxyClient) ConnectToNode(nodeAddress string, user string) (*NodeClient, error) {
log.Infof("connecting to node: %s", nodeAddress)
e := trace.Errorf("unknown Error")
// parse destination first:
localAddr, err := utils.ParseAddr("tcp://" + proxy.proxyAddress)
if err != nil {
return nil, trace.Wrap(err)
}
fakeAddr, err := utils.ParseAddr("tcp://" + nodeAddress)
if err != nil {
return nil, trace.Wrap(err)
}
// we have to try every auth method separatedly,
// because go SSH will try only one (fist) auth method
// of a given type, so if you have 2 different public keys
// you have to try each one differently
for _, authMethod := range proxy.authMethods {
proxySession, err := proxy.Client.NewSession()
if err != nil {
return nil, trace.Wrap(err)
}
proxyWriter, err := proxySession.StdinPipe()
if err != nil {
return nil, trace.Wrap(err)
}
proxyReader, err := proxySession.StdoutPipe()
if err != nil {
return nil, trace.Wrap(err)
}
proxyErr, err := proxySession.StderrPipe()
if err != nil {
return nil, trace.Wrap(err)
}
printErrors := func() {
buf := &bytes.Buffer{}
io.Copy(buf, proxyErr)
if buf.String() != "" {
fmt.Println("ERROR: " + buf.String())
}
}
err = proxySession.RequestSubsystem("proxy:" + nodeAddress)
if err != nil {
defer printErrors()
return nil, trace.Wrap(err)
}
pipeNetConn := utils.NewPipeNetConn(
proxyReader,
proxyWriter,
proxySession,
localAddr,
fakeAddr,
)
sshConfig := &ssh.ClientConfig{
User: user,
Auth: []ssh.AuthMethod{authMethod},
HostKeyCallback: proxy.hostKeyCallback,
}
conn, chans, reqs, err := ssh.NewClientConn(pipeNetConn,
nodeAddress, sshConfig)
if err != nil {
if utils.IsHandshakeFailedError(err) {
e = trace.Wrap(err)
proxySession.Close()
continue
}
return nil, trace.Wrap(err)
}
client := ssh.NewClient(conn, chans, reqs)
if err != nil {
return nil, trace.Wrap(err)
}
return &NodeClient{Client: client, Proxy: proxy}, nil
}
if utils.IsHandshakeFailedError(e) {
// remoe the name of the site from the node address:
parts := strings.Split(nodeAddress, "@")
return nil, trace.Errorf(`access denied to login "%v" when connecting to %v`, user, parts[0])
}
return nil, e
}
func (proxy *ProxyClient) Close() error {
return proxy.Client.Close()
}
// Shell returns remote shell as io.ReadWriterCloser object
func (client *NodeClient) Shell(width, height int, sessionID session.ID) (io.ReadWriteCloser, error) {
if sessionID == "" {
// initiate a new session if not passed
sessionID = session.NewID()
}
siteInfo, err := client.Proxy.getSite()
if err != nil {
return nil, trace.Wrap(err)
}
siteClient, err := client.Proxy.ConnectToSite(siteInfo.Name, client.Proxy.hostLogin)
if err != nil {
return nil, trace.Wrap(err)
}
clientSession, err := client.Client.NewSession()
if err != nil {
return nil, trace.Wrap(err)
}
// ask the server to drop us into the existing session:
if len(sessionID) > 0 {
err = clientSession.Setenv(sshutils.SessionEnvVar, string(sessionID))
if err != nil {
return nil, trace.Wrap(err)
}
}
// pass language info into the remote session.
// TODO: in the future support passing of arbitrary environment variables
evarsToPass := []string{"LANG", "LANGUAGE"}
for _, evar := range evarsToPass {
if value := os.Getenv(evar); value != "" {
err = clientSession.Setenv(evar, value)
if err != nil {
log.Warn(err)
}
}
}
terminalModes := ssh.TerminalModes{}
err = clientSession.RequestPty("xterm", height, width, terminalModes)
if err != nil {
return nil, trace.Wrap(err)
}
writer, err := clientSession.StdinPipe()
if err != nil {
return nil, trace.Wrap(err)
}
reader, err := clientSession.StdoutPipe()
if err != nil {
return nil, trace.Wrap(err)
}
stderr, err := clientSession.StderrPipe()
if err != nil {
return nil, trace.Wrap(err)
}
broadcastClose := utils.NewCloseBroadcaster()
sigC := make(chan os.Signal, 1)
signal.Notify(sigC, syscall.SIGWINCH)
go func() {
for {
select {
case sig := <-sigC:
if sig == nil {
return
}
winSize, err := term.GetWinsize(0)
if err != nil {
log.Infof("error getting size: %s", err)
continue
}
_, err = clientSession.SendRequest(
sshutils.WindowChangeReq, false,
ssh.Marshal(sshutils.WinChangeReqParams{
W: uint32(winSize.Width),
H: uint32(winSize.Height),
}))
if err != nil {
log.Infof("failed to send window change reqest: %v", err)
}
case <-broadcastClose.C:
log.Infof("detected close")
return
}
}
}()
tick := time.NewTicker(defaults.SessionRefreshPeriod)
// detect changes of the session's terminal
go func() error {
defer tick.Stop()
var prevSess *session.Session
for {
select {
case <-tick.C:
sess, err := siteClient.GetSession(sessionID)
if err != nil {
continue
}
// no previous session
if prevSess == nil {
prevSess = sess
continue
}
// nothing changed
if prevSess.TerminalParams.W == sess.TerminalParams.W && prevSess.TerminalParams.H == sess.TerminalParams.H {
continue
}
// ok, something have changed, let's resize to the new parameters
err = term.SetWinsize(0, sess.TerminalParams.Winsize())
if err != nil {
log.Infof("error setting size: %s", err)
}
prevSess = sess
case <-broadcastClose.C:
return nil
}
}
}()
go func() {
buf := &bytes.Buffer{}
io.Copy(buf, stderr)
if buf.String() != "" {
fmt.Println("ERROR: " + buf.String())
}
}()
err = clientSession.Shell()
if err != nil {
return nil, trace.Wrap(err)
}
return utils.NewPipeNetConn(
reader,
writer,
utils.MultiCloser(writer, clientSession, broadcastClose),
&net.IPAddr{},
&net.IPAddr{},
), nil
}
// Run executes command on the remote server and writes its stdout to
// the 'output' argument
func (client *NodeClient) Run(cmd []string, output io.Writer) error {
session, err := client.Client.NewSession()
if err != nil {
return trace.Wrap(err)
}
session.Stdout = output
if err := session.Run(strings.Join(cmd, " ")); err != nil {
return trace.Wrap(err)
}
return nil
}
// Upload uploads file or dir to the remote server
func (client *NodeClient) Upload(localSourcePath, remoteDestinationPath string) error {
file, err := os.Open(localSourcePath)
if err != nil {
return trace.Wrap(err)
}
fileInfo, err := file.Stat()
if err != nil {
return trace.Wrap(err)
}
file.Close()
scpConf := scp.Command{
Source: true,
TargetIsDir: fileInfo.IsDir(),
Recursive: fileInfo.IsDir(),
Target: localSourcePath,
}
// "impersonate" scp to a server
shellCmd := "/usr/bin/scp -t"
if fileInfo.IsDir() {
shellCmd += " -r"
}
shellCmd += " " + remoteDestinationPath
return client.scp(scpConf, shellCmd)
}
// Download downloads file or dir from the remote server
func (client *NodeClient) Download(remoteSourcePath, localDestinationPath string, isDir bool) error {
scpConf := scp.Command{
Sink: true,
TargetIsDir: isDir,
Recursive: isDir,
Target: localDestinationPath,
}
// "impersonate" scp to a server
shellCmd := "/usr/bin/scp -f"
if isDir {
shellCmd += " -r"
}
shellCmd += " " + remoteSourcePath
return client.scp(scpConf, shellCmd)
}
// scp runs remote scp command(shellCmd) on the remote server and
// runs local scp handler using scpConf
func (client *NodeClient) scp(scpCommand scp.Command, shellCmd string) error {
session, err := client.Client.NewSession()
if err != nil {
return trace.Wrap(err)
}
defer session.Close()
stdin, err := session.StdinPipe()
if err != nil {
return trace.Wrap(err)
}
stdout, err := session.StdoutPipe()
if err != nil {
return trace.Wrap(err)
}
stderr, err := session.StderrPipe()
if err != nil {
return trace.Wrap(err)
}
serverErrors := make(chan error, 2)
go func() {
var errMsg bytes.Buffer
io.Copy(&errMsg, stderr)
if len(errMsg.Bytes()) > 0 {
serverErrors <- trace.Errorf(errMsg.String())
} else {
close(serverErrors)
}
}()
ch := utils.NewPipeNetConn(
stdout,
stdin,
utils.MultiCloser(),
&net.IPAddr{},
&net.IPAddr{},
)
go func() {
err := scpCommand.Execute(ch)
if err != nil {
log.Errorf(err.Error())
}
stdin.Close()
}()
err = session.Run(shellCmd)
select {
case serverError := <-serverErrors:
return trace.Wrap(serverError)
}
}
// listenAndForward listens on a given socket and forwards all incoming connections
// to the given remote address via
func (client *NodeClient) listenAndForward(socket net.Listener, remoteAddr string) {
defer socket.Close()
defer client.Close()
for {
incoming, err := socket.Accept()
if err != nil {
log.Error(err)
break
}
go func() {
defer incoming.Close()
log.Infof("forwarding connection from %v to %v", incoming.RemoteAddr(), remoteAddr)
conn, err := client.Client.Dial("tcp", remoteAddr)
if err != nil {
log.Error(err)
}
defer conn.Close()
doneC := make(chan interface{}, 2)
go func() {
io.Copy(incoming, conn)
doneC <- true
}()
go func() {
io.Copy(conn, incoming)
doneC <- true
}()
<-doneC
<-doneC
log.Infof("connection from %v to %v closed!", incoming.RemoteAddr(), remoteAddr)
}()
}
}
func (client *NodeClient) Close() error {
return client.Client.Close()
}