-
Notifications
You must be signed in to change notification settings - Fork 0
/
srv.go
781 lines (705 loc) · 22.3 KB
/
srv.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
770
771
772
773
774
775
776
777
778
779
780
781
/*
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 reversetunnel
import (
"context"
"fmt"
"net"
"net/http"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/gravitational/teleport"
"github.com/gravitational/teleport/lib/auth"
"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/state"
"github.com/gravitational/teleport/lib/utils"
"github.com/gravitational/trace"
"github.com/jonboulle/clockwork"
log "github.com/sirupsen/logrus"
"golang.org/x/crypto/ssh"
)
// server is a "reverse tunnel server". it exposes the cluster capabilities
// (like access to a cluster's auth) to remote trusted clients
// (also known as 'reverse tunnel agents'.
type server struct {
sync.RWMutex
Config
// localAuthClient provides access to the full Auth Server API for the
// local cluster.
localAuthClient auth.ClientI
// localAccessPoint provides access to a cached subset of the Auth
// Server API.
localAccessPoint auth.AccessPoint
hostCertChecker ssh.CertChecker
userCertChecker ssh.CertChecker
// srv is the "base class" i.e. the underlying SSH server
srv *sshutils.Server
limiter *limiter.Limiter
// remoteSites is the list of conencted remote clusters
remoteSites []*remoteSite
// localSites is the list of local (our own cluster) tunnel clients,
// usually each of them is a local proxy.
localSites []*localSite
// clusterPeers is a map of clusters connected to peer proxies
// via reverse tunnels
clusterPeers map[string]*clusterPeers
// newAccessPoint returns new caching access point
newAccessPoint state.NewCachingAccessPoint
// cancel function will cancel the
cancel context.CancelFunc
// ctx is a context used for signalling and broadcast
ctx context.Context
*log.Entry
}
// DirectCluster is used to access cluster directly
type DirectCluster struct {
// Name is a cluster name
Name string
// Client is a client to the cluster
Client auth.ClientI
}
// Config is a reverse tunnel server configuration
type Config struct {
// ID is the ID of this server proxy
ID string
// ListenAddr is a listening address for reverse tunnel server
ListenAddr utils.NetAddr
// HostSigners is a list of host signers
HostSigners []ssh.Signer
// HostKeyCallback
// Limiter is optional request limiter
Limiter *limiter.Limiter
// LocalAuthClient provides access to a full AuthClient for the local cluster.
LocalAuthClient auth.ClientI
// AccessPoint provides access to a subset of AuthClient of the cluster.
// AccessPoint caches values and can still return results during connection
// problems.
LocalAccessPoint auth.AccessPoint
// NewCachingAccessPoint returns new caching access points
// per remote cluster
NewCachingAccessPoint state.NewCachingAccessPoint
// DirectClusters is a list of clusters accessed directly
DirectClusters []DirectCluster
// Context is a signalling context
Context context.Context
// Clock is a clock used in the server, set up to
// wall clock if not set
Clock clockwork.Clock
// KeyGen is a process wide key generator. It is shared to speed up
// generation of public/private keypairs.
KeyGen auth.Authority
// Ciphers is a list of ciphers that the server supports. If omitted,
// the defaults will be used.
Ciphers []string
// KEXAlgorithms is a list of key exchange (KEX) algorithms that the
// server supports. If omitted, the defaults will be used.
KEXAlgorithms []string
// MACAlgorithms is a list of message authentication codes (MAC) that
// the server supports. If omitted the defaults will be used.
MACAlgorithms []string
}
// CheckAndSetDefaults checks parameters and sets default values
func (cfg *Config) CheckAndSetDefaults() error {
if cfg.ID == "" {
return trace.BadParameter("missing parameter ID")
}
if cfg.ListenAddr.IsEmpty() {
return trace.BadParameter("missing parameter ListenAddr")
}
if cfg.Context == nil {
cfg.Context = context.TODO()
}
if cfg.Limiter == nil {
var err error
cfg.Limiter, err = limiter.NewLimiter(limiter.LimiterConfig{})
if err != nil {
return trace.Wrap(err)
}
}
if cfg.Clock == nil {
cfg.Clock = clockwork.NewRealClock()
}
return nil
}
// NewServer creates and returns a reverse tunnel server which is fully
// initialized but hasn't been started yet
func NewServer(cfg Config) (Server, error) {
if err := cfg.CheckAndSetDefaults(); err != nil {
return nil, trace.Wrap(err)
}
ctx, cancel := context.WithCancel(cfg.Context)
srv := &server{
Config: cfg,
localSites: []*localSite{},
remoteSites: []*remoteSite{},
localAuthClient: cfg.LocalAuthClient,
localAccessPoint: cfg.LocalAccessPoint,
newAccessPoint: cfg.NewCachingAccessPoint,
limiter: cfg.Limiter,
ctx: ctx,
cancel: cancel,
clusterPeers: make(map[string]*clusterPeers),
Entry: log.WithFields(log.Fields{
trace.Component: teleport.ComponentReverseTunnelServer,
}),
}
for _, clusterInfo := range cfg.DirectClusters {
cluster, err := newlocalSite(srv, clusterInfo.Name, clusterInfo.Client)
if err != nil {
return nil, trace.Wrap(err)
}
srv.localSites = append(srv.localSites, cluster)
}
var err error
s, err := sshutils.NewServer(
teleport.ComponentReverseTunnelServer,
cfg.ListenAddr,
srv,
cfg.HostSigners,
sshutils.AuthMethods{
PublicKey: srv.keyAuth,
},
sshutils.SetLimiter(cfg.Limiter),
)
if err != nil {
return nil, err
}
srv.hostCertChecker = ssh.CertChecker{IsAuthority: srv.isHostAuthority}
srv.userCertChecker = ssh.CertChecker{IsAuthority: srv.isUserAuthority}
srv.srv = s
go srv.periodicFetchClusterPeers()
return srv, nil
}
func (s *server) periodicFetchClusterPeers() {
ticker := time.NewTicker(defaults.ReverseTunnelAgentHeartbeatPeriod)
defer ticker.Stop()
if err := s.fetchClusterPeers(); err != nil {
s.Warningf("failed to fetch cluster peers: %v", err)
}
for {
select {
case <-s.ctx.Done():
s.Debugf("closing")
return
case <-ticker.C:
err := s.fetchClusterPeers()
if err != nil {
s.Warningf("failed to fetch cluster peers: %v", err)
}
}
}
}
// fetchClusterPeers pulls back all proxies that have registered themselves
// (created a services.TunnelConnection) in the backend and compares them to
// what was found in the previous iteration and updates the in-memory cluster
// peer map. This map is used later by GetSite(s) to return either local or
// remote site, or if non match, a cluster peer.
func (s *server) fetchClusterPeers() error {
conns, err := s.LocalAccessPoint.GetAllTunnelConnections()
if err != nil {
return trace.Wrap(err)
}
newConns := make(map[string]services.TunnelConnection)
for i := range conns {
newConn := conns[i]
// filter out peer records for our own proxy
if newConn.GetProxyName() == s.ID {
continue
}
newConns[newConn.GetName()] = newConn
}
existingConns := s.existingConns()
connsToAdd, connsToUpdate, connsToRemove := s.diffConns(newConns, existingConns)
s.removeClusterPeers(connsToRemove)
s.updateClusterPeers(connsToUpdate)
return s.addClusterPeers(connsToAdd)
}
func (s *server) addClusterPeers(conns map[string]services.TunnelConnection) error {
for key := range conns {
connInfo := conns[key]
peer, err := newClusterPeer(s, connInfo)
if err != nil {
return trace.Wrap(err)
}
s.addClusterPeer(peer)
}
return nil
}
func (s *server) updateClusterPeers(conns map[string]services.TunnelConnection) {
for key := range conns {
connInfo := conns[key]
s.updateClusterPeer(connInfo)
}
}
func (s *server) addClusterPeer(peer *clusterPeer) {
s.Lock()
defer s.Unlock()
clusterName := peer.connInfo.GetClusterName()
peers, ok := s.clusterPeers[clusterName]
if !ok {
peers = newClusterPeers(clusterName)
s.clusterPeers[clusterName] = peers
}
peers.addPeer(peer)
}
func (s *server) updateClusterPeer(conn services.TunnelConnection) bool {
s.Lock()
defer s.Unlock()
clusterName := conn.GetClusterName()
peers, ok := s.clusterPeers[clusterName]
if !ok {
return false
}
return peers.updatePeer(conn)
}
func (s *server) removeClusterPeers(conns []services.TunnelConnection) {
s.Lock()
defer s.Unlock()
for _, conn := range conns {
peers, ok := s.clusterPeers[conn.GetClusterName()]
if !ok {
s.Warningf("failed to remove cluster peer, not found peers for %v", conn)
continue
}
peers.removePeer(conn)
s.Debugf("removed cluster peer %v", conn)
}
}
func (s *server) existingConns() map[string]services.TunnelConnection {
s.RLock()
defer s.RUnlock()
conns := make(map[string]services.TunnelConnection)
for _, peers := range s.clusterPeers {
for _, cluster := range peers.peers {
conns[cluster.connInfo.GetName()] = cluster.connInfo
}
}
return conns
}
func (s *server) diffConns(newConns, existingConns map[string]services.TunnelConnection) (map[string]services.TunnelConnection, map[string]services.TunnelConnection, []services.TunnelConnection) {
connsToAdd := make(map[string]services.TunnelConnection)
connsToUpdate := make(map[string]services.TunnelConnection)
var connsToRemove []services.TunnelConnection
for existingKey := range existingConns {
conn := existingConns[existingKey]
if _, ok := newConns[existingKey]; !ok { // tunnel was removed
connsToRemove = append(connsToRemove, conn)
}
}
for newKey := range newConns {
conn := newConns[newKey]
if _, ok := existingConns[newKey]; !ok { // tunnel was added
connsToAdd[newKey] = conn
} else {
connsToUpdate[newKey] = conn
}
}
return connsToAdd, connsToUpdate, connsToRemove
}
func (s *server) Wait() {
s.srv.Wait()
}
func (s *server) Start() error {
return s.srv.Start()
}
func (s *server) Close() error {
s.cancel()
return s.srv.Close()
}
func (s *server) HandleNewChan(conn net.Conn, sconn *ssh.ServerConn, nch ssh.NewChannel) {
// apply read/write timeouts to the server connection
conn = utils.ObeyIdleTimeout(conn,
defaults.ReverseTunnelAgentHeartbeatPeriod*10,
"reverse tunnel server")
ct := nch.ChannelType()
if ct != chanHeartbeat {
msg := fmt.Sprintf("reversetunnel received unknown channel request %v from %v",
nch.ChannelType(), sconn)
// if someone is trying to open a new SSH session by talking to a reverse tunnel,
// they're most likely using the wrong port number. Lets give them the explicit hint:
if ct == "session" {
msg = "Cannot open new SSH session on reverse tunnel. Are you connecting to the right port?"
}
s.Warning(msg)
nch.Reject(ssh.ConnectionFailed, msg)
return
}
s.Debugf("new tunnel from %s", sconn.RemoteAddr())
if sconn.Permissions.Extensions[extCertType] != extCertTypeHost {
s.Error(trace.BadParameter("can't retrieve certificate type in certType"))
return
}
// add the incoming site (cluster) to the list of active connections:
site, remoteConn, err := s.upsertSite(conn, sconn)
if err != nil {
log.Error(trace.Wrap(err))
nch.Reject(ssh.ConnectionFailed, "failed to accept incoming cluster connection")
return
}
// accept the request and start the heartbeat on it:
ch, req, err := nch.Accept()
if err != nil {
log.Error(trace.Wrap(err))
sconn.Close()
return
}
go site.handleHeartbeat(remoteConn, ch, req)
}
// isHostAuthority is called during checking the client key, to see if the signing
// key is the real host CA authority key.
func (s *server) isHostAuthority(auth ssh.PublicKey) bool {
keys, err := s.getTrustedCAKeys(services.HostCA)
if err != nil {
s.Errorf("failed to retrieve trusted keys, err: %v", err)
return false
}
for _, k := range keys {
if sshutils.KeysEqual(k, 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 *server) isUserAuthority(auth ssh.PublicKey) bool {
keys, err := s.getTrustedCAKeys(services.UserCA)
if err != nil {
s.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 *server) getTrustedCAKeys(CertType services.CertAuthType) ([]ssh.PublicKey, error) {
cas, err := s.localAccessPoint.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 *server) checkTrustedKey(CertType services.CertAuthType, domainName string, key ssh.PublicKey) error {
cas, err := s.localAccessPoint.GetCertAuthorities(CertType, false)
if err != nil {
return trace.Wrap(err)
}
for _, ca := range cas {
if ca.GetClusterName() != domainName {
continue
}
checkers, err := ca.Checkers()
if err != nil {
return trace.Wrap(err)
}
for _, checker := range checkers {
if sshutils.KeysEqual(key, checker) {
return nil
}
}
}
return trace.NotFound("authority domain %v not found or has no mathching keys", domainName)
}
func (s *server) keyAuth(conn ssh.ConnMetadata, key ssh.PublicKey) (*ssh.Permissions, error) {
logger := s.WithFields(log.Fields{
"remote": conn.RemoteAddr(),
"user": conn.User(),
})
cert, ok := key.(*ssh.Certificate)
if !ok {
logger.Warningf("server doesn't support provided key type")
return nil, trace.BadParameter("server doesn't support provided key type")
}
switch cert.CertType {
case ssh.HostCert:
authDomain, ok := cert.Extensions[utils.CertExtensionAuthority]
if !ok || authDomain == "" {
err := trace.BadParameter("missing authority domainName parameter")
logger.Warningf("failed authenticate host, err: %v", err)
return nil, err
}
err := s.hostCertChecker.CheckHostKey(conn.User(), conn.RemoteAddr(), key)
if err != nil {
logger.Warningf("failed authenticate host, err: %v", err)
return nil, trace.Wrap(err)
}
if err := s.hostCertChecker.CheckCert(conn.User(), cert); err != nil {
logger.Warningf("failed to authenticate host err: %v", err)
return nil, trace.Wrap(err)
}
// this fixes possible injection attack
// when we have 2 trusted remote sites, and one can simply
// pose as another. so we have to check that authority
// matches by some other way (in absence of x509 chains)
if err := s.checkTrustedKey(services.HostCA, authDomain, cert.SignatureKey); err != nil {
logger.Warningf("this claims to be signed as authDomain %v, but no matching signing keys found")
return nil, trace.Wrap(err)
}
return &ssh.Permissions{
Extensions: map[string]string{
extHost: conn.User(),
extCertType: extCertTypeHost,
extAuthority: authDomain,
},
}, nil
case ssh.UserCert:
_, err := s.userCertChecker.Authenticate(conn, key)
if err != nil {
logger.Warningf("failed to authenticate user, err: %v", err)
return nil, err
}
if err := s.userCertChecker.CheckCert(conn.User(), cert); err != nil {
logger.Warningf("failed to authenticate user err: %v", err)
return nil, trace.Wrap(err)
}
return &ssh.Permissions{
Extensions: map[string]string{
extHost: conn.User(),
extCertType: extCertTypeUser,
},
}, nil
default:
return nil, trace.BadParameter("unsupported cert type: %v", cert.CertType)
}
}
func (s *server) upsertSite(conn net.Conn, sshConn *ssh.ServerConn) (*remoteSite, *remoteConn, error) {
domainName := sshConn.Permissions.Extensions[extAuthority]
if strings.TrimSpace(domainName) == "" {
return nil, nil, trace.BadParameter("Cannot create reverse tunnel: empty domain name")
}
s.Lock()
defer s.Unlock()
var site *remoteSite
for _, st := range s.remoteSites {
if st.domainName == domainName {
site = st
break
}
}
var err error
var remoteConn *remoteConn
if site != nil {
if remoteConn, err = site.addConn(conn, sshConn); err != nil {
return nil, nil, trace.Wrap(err)
}
} else {
site, err = newRemoteSite(s, domainName)
if err != nil {
return nil, nil, trace.Wrap(err)
}
if remoteConn, err = site.addConn(conn, sshConn); err != nil {
return nil, nil, trace.Wrap(err)
}
s.remoteSites = append(s.remoteSites, site)
}
site.Infof("connection <- %v, clusters: %d", conn.RemoteAddr(), len(s.remoteSites))
// treat first connection as a registered heartbeat,
// otherwise the connection information will appear after initial
// heartbeat delay
go site.registerHeartbeat(time.Now())
return site, remoteConn, nil
}
func (s *server) GetSites() []RemoteSite {
s.RLock()
defer s.RUnlock()
out := make([]RemoteSite, 0, len(s.remoteSites)+len(s.localSites)+len(s.clusterPeers))
for i := range s.localSites {
out = append(out, s.localSites[i])
}
haveLocalConnection := make(map[string]bool)
for i := range s.remoteSites {
site := s.remoteSites[i]
haveLocalConnection[site.GetName()] = true
out = append(out, site)
}
for i := range s.clusterPeers {
cluster := s.clusterPeers[i]
if _, ok := haveLocalConnection[cluster.GetName()]; !ok {
out = append(out, cluster)
}
}
return out
}
// GetSite returns a RemoteSite. The first attempt is to find and return a
// remote site and that is what is returned if a remote remote agent has
// connected to this proxy. Next we loop over local sites and try and try and
// return a local site. If that fails, we return a cluster peer. This happens
// when you hit proxy that has never had an agent connect to it. If you end up
// with a cluster peer your best bet is to wait until the agent has discovered
// all proxies behind a the load balancer. Note, the cluster peer is a
// services.TunnelConnection that was created by another proxy.
func (s *server) GetSite(name string) (RemoteSite, error) {
s.RLock()
defer s.RUnlock()
for i := range s.remoteSites {
if s.remoteSites[i].GetName() == name {
return s.remoteSites[i], nil
}
}
for i := range s.localSites {
if s.localSites[i].GetName() == name {
return s.localSites[i], nil
}
}
for i := range s.clusterPeers {
if s.clusterPeers[i].GetName() == name {
return s.clusterPeers[i], nil
}
}
return nil, trace.NotFound("cluster %q is not found", name)
}
func (s *server) RemoveSite(domainName string) error {
s.Lock()
defer s.Unlock()
for i := range s.remoteSites {
if s.remoteSites[i].domainName == domainName {
s.remoteSites = append(s.remoteSites[:i], s.remoteSites[i+1:]...)
return nil
}
}
for i := range s.localSites {
if s.localSites[i].domainName == domainName {
s.localSites = append(s.localSites[:i], s.localSites[i+1:]...)
return nil
}
}
return trace.NotFound("cluster %q is not found", domainName)
}
type remoteConn struct {
sshConn ssh.Conn
conn net.Conn
invalid int32
log *log.Entry
counter int32
discoveryC ssh.Channel
discoveryErr error
closed int32
}
func (rc *remoteConn) openDiscoveryChannel() (ssh.Channel, error) {
if rc.discoveryC != nil {
return rc.discoveryC, nil
}
if rc.discoveryErr != nil {
return nil, trace.Wrap(rc.discoveryErr)
}
discoveryC, _, err := rc.sshConn.OpenChannel(chanDiscovery, nil)
if err != nil {
rc.discoveryErr = err
return nil, trace.Wrap(err)
}
rc.discoveryC = discoveryC
return rc.discoveryC, nil
}
func (rc *remoteConn) String() string {
return fmt.Sprintf("remoteConn(remoteAddr=%v)", rc.conn.RemoteAddr())
}
func (rc *remoteConn) Close() error {
if !atomic.CompareAndSwapInt32(&rc.closed, 0, 1) {
// already closed
return nil
}
if rc.discoveryC != nil {
rc.discoveryC.Close()
rc.discoveryC = nil
}
return rc.sshConn.Close()
}
func (rc *remoteConn) markInvalid(err error) {
atomic.StoreInt32(&rc.invalid, 1)
}
func (rc *remoteConn) isInvalid() bool {
return atomic.LoadInt32(&rc.invalid) == 1
}
// newRemoteSite helper creates and initializes 'remoteSite' instance
func newRemoteSite(srv *server, domainName string) (*remoteSite, error) {
connInfo, err := services.NewTunnelConnection(
fmt.Sprintf("%v-%v", srv.ID, domainName),
services.TunnelConnectionSpecV2{
ClusterName: domainName,
ProxyName: srv.ID,
LastHeartbeat: time.Now().UTC(),
},
)
if err != nil {
return nil, trace.Wrap(err)
}
remoteSite := &remoteSite{
srv: srv,
domainName: domainName,
connInfo: connInfo,
Entry: log.WithFields(log.Fields{
trace.Component: teleport.ComponentReverseTunnelServer,
trace.ComponentFields: log.Fields{
"cluster": domainName,
},
}),
ctx: srv.ctx,
clock: srv.Clock,
}
// transport uses connection do dial out to the remote address
remoteSite.transport = &http.Transport{
Dial: remoteSite.dialAccessPoint,
}
// configure access to the full Auth Server API and the cached subset for
// the local cluster within which reversetunnel.Server is running.
remoteSite.localClient = srv.localAuthClient
remoteSite.localAccessPoint = srv.localAccessPoint
// configure access to the full Auth Server API for the remote cluster that
// this remote site provides access to.
clt, err := auth.NewClient("http://stub:0", remoteSite.dialAccessPoint)
if err != nil {
return nil, trace.Wrap(err)
}
remoteSite.remoteClient = clt
// configure access to the cached subset of the Auth Server API of the remote
// cluster this remote site provides access to.
accessPoint, err := srv.newAccessPoint(clt, []string{"reverse", domainName})
if err != nil {
return nil, trace.Wrap(err)
}
remoteSite.remoteAccessPoint = accessPoint
// instantiate a cache of host certificates for the forwarding server. the
// certificate cache is created in each site (instead of creating it in
// reversetunnel.server and passing it along) so that the host certificate
// is signed by the correct certificate authority.
certificateCache, err := NewHostCertificateCache(srv.Config.KeyGen, srv.localAuthClient)
if err != nil {
return nil, trace.Wrap(err)
}
remoteSite.certificateCache = certificateCache
go remoteSite.periodicSendDiscoveryRequests()
return remoteSite, nil
}
const (
extHost = "host@teleport"
extCertType = "certtype@teleport"
extAuthority = "auth@teleport"
extCertTypeHost = "host"
extCertTypeUser = "user"
)