forked from gravitational/teleport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tun.go
769 lines (700 loc) · 21.3 KB
/
tun.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
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
/*
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 auth
import (
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"os"
"sync"
"time"
"github.com/gravitational/teleport"
"github.com/gravitational/teleport/lib/defaults"
"github.com/gravitational/teleport/lib/limiter"
"github.com/gravitational/teleport/lib/services"
"github.com/gravitational/teleport/lib/sshutils"
"github.com/gravitational/teleport/lib/utils"
log "github.com/Sirupsen/logrus"
"github.com/gravitational/roundtrip"
"github.com/gravitational/trace"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/agent"
)
// AuthTunnel listens on TCP/IP socket and accepts SSH connections. It then stablishes
// an SSH tunnell which HTTP requests travel over. In other words, the Auth Service API
// runs on HTTP-via-SSH-tunnel.
type AuthTunnel struct {
// authServer implements the "beef" of the Auth service
authServer *AuthServer
// apiServer maintains a map of API servers, each assigned
// to a certain role. this allows to break Auth API into
// a set of restricted API services with well-defined permissions
apiServer *APIWithRoles
// sshServer implements the nuts & bolts of serving an SSH connection
// to create a tunnel
sshServer *sshutils.Server
hostSigner ssh.Signer
hostCertChecker ssh.CertChecker
userCertChecker ssh.CertChecker
limiter *limiter.Limiter
}
// ServerOption is the functional argument passed to the server
type ServerOption func(s *AuthTunnel) error
// SetLimiter sets rate and connection limiter for auth tunnel server
func SetLimiter(limiter *limiter.Limiter) ServerOption {
return func(s *AuthTunnel) error {
s.limiter = limiter
return nil
}
}
// NewTunnel creates a new SSH tunnel server which is not started yet
func NewTunnel(addr utils.NetAddr,
hostSigners []ssh.Signer,
apiServer *APIWithRoles,
authServer *AuthServer,
opts ...ServerOption) (tunnel *AuthTunnel, err error) {
tunnel = &AuthTunnel{
authServer: authServer,
apiServer: apiServer,
}
tunnel.limiter, err = limiter.NewLimiter(limiter.LimiterConfig{})
if err != nil {
return nil, trace.Wrap(err)
}
// apply functional options:
for _, o := range opts {
if err := o(tunnel); err != nil {
return nil, err
}
}
// create an SSH server and assign the tunnel to be it's "new SSH channel handler"
tunnel.sshServer, err = sshutils.NewServer(
addr,
tunnel,
hostSigners,
sshutils.AuthMethods{
Password: tunnel.passwordAuth,
PublicKey: tunnel.keyAuth,
},
sshutils.SetLimiter(tunnel.limiter),
)
if err != nil {
return nil, err
}
tunnel.userCertChecker = ssh.CertChecker{IsAuthority: tunnel.isUserAuthority}
tunnel.hostCertChecker = ssh.CertChecker{IsAuthority: tunnel.isHostAuthority}
return tunnel, nil
}
func (s *AuthTunnel) Addr() string {
return s.sshServer.Addr()
}
func (s *AuthTunnel) Start() error {
return s.sshServer.Start()
}
func (s *AuthTunnel) Close() error {
if s != nil && s.sshServer != nil {
return s.sshServer.Close()
}
return nil
}
// HandleNewChan implements NewChanHandler interface: it gets called every time a new SSH
// connection is established
func (s *AuthTunnel) HandleNewChan(_ net.Conn, sconn *ssh.ServerConn, nch ssh.NewChannel) {
log.Infof("[AUTH] new channel request: %v", nch.ChannelType())
cht := nch.ChannelType()
switch cht {
case ReqDirectTCPIP:
if !s.haveExt(sconn, ExtHost, ExtWebSession, ExtWebPassword) {
nch.Reject(
ssh.UnknownChannelType,
fmt.Sprintf("register clients can not TCPIP: %v", cht))
return
}
req, err := sshutils.ParseDirectTCPIPReq(nch.ExtraData())
if err != nil {
log.Errorf("[AUTH] failed to parse request data: %v, err: %v",
string(nch.ExtraData()), err)
nch.Reject(ssh.UnknownChannelType,
"failed to parse direct-tcpip request")
return
}
sshCh, _, err := nch.Accept()
if err != nil {
log.Infof("[AUTH] could not accept channel (%s)", err)
return
}
go s.handleDirectTCPIPRequest(sconn, sshCh, req)
case ReqWebSessionAgent:
// this is a protective measure, so web requests can be only done
// if have session ready
if !s.haveExt(sconn, ExtWebSession) {
nch.Reject(
ssh.UnknownChannelType,
fmt.Sprintf("don't have web session for: %v", cht))
return
}
ch, _, err := nch.Accept()
if err != nil {
log.Infof("[AUTH] could not accept channel (%s)", err)
return
}
go s.handleWebAgentRequest(sconn, ch)
default:
nch.Reject(ssh.UnknownChannelType, fmt.Sprintf(
"unknown channel type: %v", cht))
}
}
// isHostAuthority is called during checking the client key, to see if the signing
// key is the real host CA authority key.
func (s *AuthTunnel) isHostAuthority(auth ssh.PublicKey) bool {
key, err := s.authServer.GetCertAuthority(services.CertAuthID{DomainName: s.authServer.DomainName, Type: services.HostCA}, false)
if err != nil {
log.Errorf("failed to retrieve user authority key, err: %v", err)
return false
}
checkers, err := key.Checkers()
if err != nil {
log.Errorf("failed to parse CA keys: %v", err)
return false
}
for _, checker := range checkers {
if sshutils.KeysEqual(checker, auth) {
return true
}
}
return false
}
// isUserAuthority is called during checking the client key, to see if the signing
// key is the real user CA authority key.
func (s *AuthTunnel) isUserAuthority(auth ssh.PublicKey) bool {
keys, err := s.getTrustedCAKeys(services.UserCA)
if err != nil {
log.Errorf("failed to retrieve trusted keys, err: %v", err)
return false
}
for _, k := range keys {
if sshutils.KeysEqual(k, auth) {
return true
}
}
return false
}
func (s *AuthTunnel) getTrustedCAKeys(CertType services.CertAuthType) ([]ssh.PublicKey, error) {
cas, err := s.authServer.GetCertAuthorities(CertType, false)
if err != nil {
return nil, err
}
out := []ssh.PublicKey{}
for _, ca := range cas {
checkers, err := ca.Checkers()
if err != nil {
return nil, trace.Wrap(err)
}
out = append(out, checkers...)
}
return out, nil
}
func (s *AuthTunnel) haveExt(sconn *ssh.ServerConn, ext ...string) bool {
if sconn.Permissions == nil {
return false
}
for _, e := range ext {
if sconn.Permissions.Extensions[e] != "" {
return true
}
}
return true
}
func (s *AuthTunnel) handleWebAgentRequest(sconn *ssh.ServerConn, ch ssh.Channel) {
defer ch.Close()
if sconn.Permissions.Extensions[ExtRole] != string(teleport.RoleWeb) {
log.Errorf("role %v doesn't have permission to request agent",
sconn.Permissions.Extensions[ExtRole])
return
}
ws, err := s.authServer.GetWebSession(sconn.User(), sconn.Permissions.Extensions[ExtWebSession])
if err != nil {
log.Errorf("session error: %v", err)
return
}
priv, err := ssh.ParseRawPrivateKey(ws.WS.Priv)
if err != nil {
log.Errorf("session error: %v", err)
return
}
pub, _, _, _, err := ssh.ParseAuthorizedKey(ws.WS.Pub)
if err != nil {
log.Errorf("session error: %v", err)
return
}
cert, ok := pub.(*ssh.Certificate)
if !ok {
log.Errorf("session error, not a cert: %T", pub)
return
}
addedKey := agent.AddedKey{
PrivateKey: priv,
Certificate: cert,
Comment: "web-session@teleport",
LifetimeSecs: 0,
ConfirmBeforeUse: false,
}
newKeyAgent := agent.NewKeyring()
if err := newKeyAgent.Add(addedKey); err != nil {
log.Errorf("failed to add: %v", err)
return
}
if err := agent.ServeAgent(newKeyAgent, ch); err != nil {
log.Errorf("Serve agent err: %v", err)
}
}
// handleDirectTCPIPRequest accepts an incoming SSH connection via TCP/IP and forwards
// it to the local auth server which listens on local UNIX pipe
func (s *AuthTunnel) handleDirectTCPIPRequest(sconn *ssh.ServerConn, sshChannel ssh.Channel, req *sshutils.DirectTCPIPReq) {
defer sconn.Close()
// retreive the role from thsi connection's permissions (make sure it's a valid role)
role := teleport.Role(sconn.Permissions.Extensions[ExtRole])
if err := role.Check(); err != nil {
log.Errorf(err.Error())
return
}
// Forward this new SSH channel to API-with-roles server. It will try to proxy this
// connection to the API service mapped to this role:
if err := s.apiServer.HandleNewChannel(sconn.RemoteAddr(), sshChannel, role); err != nil {
log.Error(err)
return
}
}
func (s *AuthTunnel) keyAuth(
conn ssh.ConnMetadata, key ssh.PublicKey) (*ssh.Permissions, error) {
cid := fmt.Sprintf(
"conn(%v->%v, user=%v)", conn.RemoteAddr(),
conn.LocalAddr(), conn.User())
log.Infof("%v auth attempt with key %v", cid, key.Type())
cert, ok := key.(*ssh.Certificate)
if !ok {
return nil, trace.Errorf("ERROR: Server doesn't support provided key type")
}
if cert.CertType == ssh.HostCert {
err := s.hostCertChecker.CheckHostKey(conn.User(), conn.RemoteAddr(), key)
if err != nil {
log.Warningf("conn(%v->%v, user=%v) ERROR: failed auth user %v, err: %v",
conn.RemoteAddr(), conn.LocalAddr(), conn.User(), conn.User(), err)
return nil, err
}
if err := s.hostCertChecker.CheckCert(conn.User(), cert); err != nil {
log.Warningf("conn(%v->%v, user=%v) ERROR: Failed to authorize user %v, err: %v",
conn.RemoteAddr(), conn.LocalAddr(), conn.User(), conn.User(), err)
return nil, trace.Wrap(err)
}
perms := &ssh.Permissions{
Extensions: map[string]string{
ExtHost: conn.User(),
ExtRole: cert.Permissions.Extensions[utils.CertExtensionRole],
},
}
return perms, nil
}
// we are assuming that this is a user cert
if err := s.userCertChecker.CheckCert(conn.User(), cert); err != nil {
log.Warningf("conn(%v->%v, user=%v) ERROR: Failed to authorize user %v, err: %v",
conn.RemoteAddr(), conn.LocalAddr(), conn.User(), conn.User(), err)
return nil, trace.Wrap(err)
}
// we are not using cert extensions for User certificates because of OpenSSH bug
// https://bugzilla.mindrot.org/show_bug.cgi?id=2387
perms := &ssh.Permissions{
Extensions: map[string]string{
ExtHost: conn.User(),
ExtRole: string(teleport.RoleUser),
},
}
return perms, nil
}
func (s *AuthTunnel) passwordAuth(
conn ssh.ConnMetadata, password []byte) (*ssh.Permissions, error) {
var ab *authBucket
if err := json.Unmarshal(password, &ab); err != nil {
return nil, err
}
log.Infof("got authentication attempt for user '%v' type '%v'", conn.User(), ab.Type)
switch ab.Type {
case AuthWebPassword:
if err := s.authServer.CheckPassword(conn.User(), ab.Pass, ab.HotpToken); err != nil {
log.Warningf("password auth error: %#v", err)
return nil, trace.Wrap(err)
}
perms := &ssh.Permissions{
Extensions: map[string]string{
ExtWebPassword: "<password>",
ExtRole: string(teleport.RoleUser),
},
}
log.Infof("password authenticated user: '%v'", conn.User())
return perms, nil
case AuthWebSession:
// we use extra permissions mechanism to keep the connection data
// after authorization, in this case the session
perms := &ssh.Permissions{
Extensions: map[string]string{
ExtWebSession: string(ab.Pass),
ExtRole: string(teleport.RoleWeb),
},
}
if _, err := s.authServer.GetWebSession(conn.User(), string(ab.Pass)); err != nil {
return nil, trace.Errorf("session resume error: %v", trace.Wrap(err))
}
log.Infof("session authenticated user: '%v'", conn.User())
return perms, nil
case AuthToken:
_, err := s.authServer.ValidateToken(string(ab.Pass))
if err != nil {
log.Errorf("token validation error: %v", err)
return nil, trace.Wrap(err, fmt.Sprintf("invalid token for: %v", ab.User))
}
perms := &ssh.Permissions{
Extensions: map[string]string{
ExtToken: string(password),
ExtRole: string(teleport.RoleProvisionToken),
}}
utils.Consolef(os.Stdout, "[AUTH] Successfully accepted token %v for %v", string(password), conn.User())
return perms, nil
case AuthSignupToken:
_, err := s.authServer.GetSignupToken(string(ab.Pass))
if err != nil {
return nil, trace.Errorf("token validation error: %v", trace.Wrap(err))
}
perms := &ssh.Permissions{
Extensions: map[string]string{
ExtToken: string(password),
ExtRole: string(teleport.RoleSignup),
}}
log.Infof("session authenticated prov. token: '%v'", conn.User())
return perms, nil
default:
return nil, trace.Errorf("unsupported auth method: '%v'", ab.Type)
}
}
// authBucket uses password to transport app-specific user name and
// auth-type in addition to the password to support auth
type authBucket struct {
User string `json:"user"`
Type string `json:"type"`
Pass []byte `json:"pass"`
HotpToken string `json:"hotpToken"`
}
func NewTokenAuth(domainName, token string) ([]ssh.AuthMethod, error) {
data, err := json.Marshal(authBucket{
Type: AuthToken,
User: domainName,
Pass: []byte(token),
})
if err != nil {
return nil, err
}
return []ssh.AuthMethod{ssh.Password(string(data))}, nil
}
func NewWebSessionAuth(user string, session []byte) ([]ssh.AuthMethod, error) {
data, err := json.Marshal(authBucket{
Type: AuthWebSession,
User: user,
Pass: session,
})
if err != nil {
return nil, err
}
return []ssh.AuthMethod{ssh.Password(string(data))}, nil
}
func NewWebPasswordAuth(user string, password []byte, hotpToken string) ([]ssh.AuthMethod, error) {
data, err := json.Marshal(authBucket{
Type: AuthWebPassword,
User: user,
Pass: password,
HotpToken: hotpToken,
})
if err != nil {
return nil, err
}
return []ssh.AuthMethod{ssh.Password(string(data))}, nil
}
func NewSignupTokenAuth(token string) ([]ssh.AuthMethod, error) {
data, err := json.Marshal(authBucket{
Type: AuthSignupToken,
Pass: []byte(token),
})
if err != nil {
return nil, err
}
return []ssh.AuthMethod{ssh.Password(string(data))}, nil
}
func NewHostAuth(key, cert []byte) ([]ssh.AuthMethod, error) {
signer, err := sshutils.NewSigner(key, cert)
if err != nil {
return nil, err
}
return []ssh.AuthMethod{ssh.PublicKeys(signer)}, nil
}
// TunClientOption is functional option for tunnel client
type TunClientOption func(t *TunClient)
// TunClientStorage allows tun client to set local presence service
// that it will use to sync up the latest information about auth servers
func TunClientStorage(storage utils.AddrStorage) TunClientOption {
return func(t *TunClient) {
t.addrStorage = storage
}
}
// TunClient is HTTP client that works over SSH tunnel
// This is done in order to authenticate various teleport roles
// using existing SSH certificate infrastructure
type TunClient struct {
sync.Mutex
Client
user string
authServers []utils.NetAddr
authMethods []ssh.AuthMethod
refreshTicker *time.Ticker
closeC chan struct{}
closeOnce sync.Once
tr *http.Transport
addrStorage utils.AddrStorage
}
// NewTunClient returns an instance of new HTTP client to Auth server API
// exposed over SSH tunnel, so client uses SSH credentials to dial and authenticate
func NewTunClient(authServers []utils.NetAddr, user string, authMethods []ssh.AuthMethod, opts ...TunClientOption) (*TunClient, error) {
if user == "" {
return nil, trace.BadParameter("SSH connection requires a valid username")
}
tc := &TunClient{
user: user,
authServers: authServers,
authMethods: authMethods,
refreshTicker: time.NewTicker(defaults.AuthServersRefreshPeriod),
closeC: make(chan struct{}),
}
for _, o := range opts {
o(tc)
}
tr := &http.Transport{
Dial: tc.Dial,
}
clt, err := NewClient(
"http://stub:0",
roundtrip.HTTPClient(&http.Client{
Transport: tr,
}))
if err != nil {
return nil, trace.Wrap(err)
}
tc.Client = *clt
tc.tr = tr
// use local information about auth servers if it's available
if tc.addrStorage != nil {
authServers, err := tc.addrStorage.GetAddresses()
if err != nil {
if !trace.IsNotFound(err) {
return nil, trace.Wrap(err)
}
log.Infof("local storage is provided, not initialized")
} else {
log.Infof("using auth servers from local storage: %v", authServers)
tc.authServers = authServers
}
}
go tc.syncAuthServers()
return tc, nil
}
// Close releases all the resources allocated for this client
func (c *TunClient) Close() error {
log.Infof("TunClient.Close()")
if c != nil {
c.tr.CloseIdleConnections()
c.refreshTicker.Stop()
c.closeOnce.Do(func() {
close(c.closeC)
})
}
return nil
}
// GetDialer returns dialer that will connect to auth server API
func (c *TunClient) GetDialer() AccessPointDialer {
return func() (net.Conn, error) {
return c.Dial(c.authServers[0].AddrNetwork, "accesspoint:0")
}
}
// GetAgent returns SSH agent that uses ReqWebSessionAgent Auth server extension
func (c *TunClient) GetAgent() (AgentCloser, error) {
client, err := c.getClient() // we need an established connection first
if err != nil {
return nil, trace.Wrap(err)
}
ch, _, err := client.OpenChannel(ReqWebSessionAgent, nil)
if err != nil {
return nil, trace.ConnectionProblem(err, "failed to connect to remote API")
}
agentCloser := &tunAgent{client: client}
agentCloser.Agent = agent.NewClient(ch)
return agentCloser, nil
}
// Dial dials to Auth server's HTTP API over SSH tunnel
func (c *TunClient) Dial(network, address string) (net.Conn, error) {
log.Debugf("TunDialer.Dial(%v, %v)", network, address)
client, err := c.getClient()
if err != nil {
return nil, trace.Wrap(err)
}
conn, err := client.Dial(network, address)
if err != nil {
return nil, trace.ConnectionProblem(err, "failed to connect to remote API")
}
tc := &tunConn{client: client}
tc.Conn = conn
return tc, nil
}
func (c *TunClient) fetchAndSync() error {
authServers, err := c.fetchAuthServers()
if err != nil {
return trace.Wrap(err)
}
if len(authServers) == 0 {
return trace.NotFound("no auth servers with remote ips advertised")
}
// set runtime information about auth servers
c.setAuthServers(authServers)
// populate local storage if it is supplied
if c.addrStorage != nil {
if err := c.addrStorage.SetAddresses(authServers); err != nil {
return trace.Wrap(err, "failed to set local storage addresses")
}
}
return nil
}
func (c *TunClient) syncAuthServers() {
log.Infof("TunClient (%p): syncAuthServers() started", c)
for {
select {
case <-c.refreshTicker.C:
err := c.fetchAndSync()
if err != nil {
continue
}
case <-c.closeC:
log.Infof("TunClient (%p): syncAuthServers() exited", c)
return
}
}
}
func (c *TunClient) fetchAuthServers() ([]utils.NetAddr, error) {
servers, err := c.GetAuthServers()
if err != nil {
return nil, trace.Wrap(err)
}
authServers := make([]utils.NetAddr, 0, len(servers))
for _, server := range servers {
serverAddr, err := utils.ParseAddr(server.Addr)
if err != nil {
return nil, trace.Wrap(err)
}
if !serverAddr.IsLocal() {
authServers = append(authServers, *serverAddr)
}
}
return authServers, nil
}
func (c *TunClient) getAuthServers() []utils.NetAddr {
c.Lock()
defer c.Unlock()
out := make([]utils.NetAddr, len(c.authServers))
for i := range c.authServers {
out[i] = c.authServers[i]
}
return out
}
func (c *TunClient) setAuthServers(servers []utils.NetAddr) {
c.Lock()
defer c.Unlock()
c.authServers = servers
}
func (c *TunClient) getClient() (*ssh.Client, error) {
var client *ssh.Client
var err error
for _, authServer := range c.getAuthServers() {
client, err = c.dialAuthServer(authServer)
if err == nil {
return client, nil
}
}
return nil, trace.Wrap(err)
}
func (c *TunClient) dialAuthServer(authServer utils.NetAddr) (*ssh.Client, error) {
config := &ssh.ClientConfig{
User: c.user,
Auth: c.authMethods,
}
client, err := ssh.Dial(authServer.AddrNetwork, authServer.Addr, config)
log.Debugf("TunDialer.getClient(%v)", authServer.String())
if err != nil {
log.Infof("TunDialer could not ssh.Dial: %v", err)
if utils.IsHandshakeFailedError(err) {
return nil, trace.AccessDenied("access denied to '%v': bad username or credentials", c.user)
}
return nil, trace.ConvertSystemError(err)
}
return client, nil
}
type AgentCloser interface {
io.Closer
agent.Agent
}
type tunAgent struct {
agent.Agent
client *ssh.Client
}
func (ta *tunAgent) Close() error {
log.Infof("tunAgent.Close")
return ta.client.Close()
}
const (
// DialerRetryAttempts is the amount of attempts for dialer to try and
// connect to the remote destination
DialerRetryAttempts = 3
// DialerPeriodBetweenAttempts is the period between retry attempts
DialerPeriodBetweenAttempts = time.Second
)
type tunConn struct {
net.Conn
client *ssh.Client
}
func (c *tunConn) Close() error {
err := c.Conn.Close()
err = c.client.Close()
return trace.Wrap(err)
}
const (
ReqWebSessionAgent = "web-session-agent@teleport"
ReqProvision = "provision@teleport"
ReqDirectTCPIP = "direct-tcpip"
ReqNewAuth = "new-auth@teleport"
ExtWebSession = "web-session@teleport"
ExtWebPassword = "web-password@teleport"
ExtToken = "provision@teleport"
ExtHost = "host@teleport"
ExtRole = "role@teleport"
AuthWebPassword = "password"
AuthWebSession = "session"
AuthToken = "provision-token"
AuthSignupToken = "signup-token"
)
// AccessPointDialer dials to auth access point remote HTTP api
type AccessPointDialer func() (net.Conn, error)