-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
srv.go
1068 lines (955 loc) · 30.5 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
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
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"
"crypto/tls"
"fmt"
"net"
"strings"
"sync"
"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/sshca"
"github.com/gravitational/teleport/lib/sshutils"
"github.com/gravitational/teleport/lib/utils"
"github.com/gravitational/trace"
"github.com/jonboulle/clockwork"
"github.com/prometheus/client_golang/prometheus"
log "github.com/sirupsen/logrus"
"golang.org/x/crypto/ssh"
)
var (
remoteClustersStats = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "remote_clusters",
Help: "Number inbound connections from remote clusters and clusters stats",
},
[]string{"cluster"},
)
trustedClustersStats = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "trusted_clusters",
Help: "Number of tunnels per state",
},
[]string{"cluster", "state"},
)
)
func init() {
// Metrics have to be registered to be exposed:
prometheus.MustRegister(remoteClustersStats)
prometheus.MustRegister(trustedClustersStats)
}
// 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
// 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 auth.NewCachingAccessPoint
// cancel function will cancel the
cancel context.CancelFunc
// ctx is a context used for signalling and broadcast
ctx context.Context
*log.Entry
// proxyWatcher monitors changes to the proxies
// and broadcasts updates
proxyWatcher *services.ProxyWatcher
// offlineThreshold is how long to wait for a keep alive message before
// marking a reverse tunnel connection as invalid.
offlineThreshold time.Duration
}
// 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
// ClusterName is a name of this cluster
ClusterName string
// ClientTLS is a TLS config associated with this proxy
// used to connect to remote auth servers on remote clusters
ClientTLS *tls.Config
// Listener is a listener address for reverse tunnel server
Listener net.Listener
// 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 auth.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 sshca.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
// DataDir is a local server data directory
DataDir string
// PollingPeriod specifies polling period for internal sync
// goroutines, used to speed up sync-ups in tests.
PollingPeriod time.Duration
// Component is a component used in logs
Component string
// FIPS means Teleport was started in a FedRAMP/FIPS 140-2 compliant
// configuration.
FIPS bool
}
// CheckAndSetDefaults checks parameters and sets default values
func (cfg *Config) CheckAndSetDefaults() error {
if cfg.ID == "" {
return trace.BadParameter("missing parameter ID")
}
if cfg.ClusterName == "" {
return trace.BadParameter("missing parameter ClusterName")
}
if cfg.ClientTLS == nil {
return trace.BadParameter("missing parameter ClientTLS")
}
if cfg.Listener == nil {
return trace.BadParameter("missing parameter Listener")
}
if cfg.DataDir == "" {
return trace.BadParameter("missing parameter DataDir")
}
if cfg.Context == nil {
cfg.Context = context.TODO()
}
if cfg.PollingPeriod == 0 {
cfg.PollingPeriod = defaults.HighResPollingPeriod
}
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()
}
if cfg.Component == "" {
cfg.Component = teleport.Component(teleport.ComponentProxy, teleport.ComponentServer)
}
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)
}
clusterConfig, err := cfg.LocalAccessPoint.GetClusterConfig()
if err != nil {
return nil, trace.Wrap(err)
}
offlineThreshold := time.Duration(clusterConfig.GetKeepAliveCountMax()) * clusterConfig.GetKeepAliveInterval()
ctx, cancel := context.WithCancel(cfg.Context)
entry := log.WithFields(log.Fields{
trace.Component: cfg.Component,
})
proxyWatcher, err := services.NewProxyWatcher(services.ProxyWatcherConfig{
Context: ctx,
Component: cfg.Component,
Client: cfg.LocalAuthClient,
Entry: entry,
ProxiesC: make(chan []services.Server, 10),
})
if err != nil {
cancel()
return nil, trace.Wrap(err)
}
srv := &server{
Config: cfg,
localSites: []*localSite{},
remoteSites: []*remoteSite{},
localAuthClient: cfg.LocalAuthClient,
localAccessPoint: cfg.LocalAccessPoint,
newAccessPoint: cfg.NewCachingAccessPoint,
limiter: cfg.Limiter,
ctx: ctx,
cancel: cancel,
proxyWatcher: proxyWatcher,
clusterPeers: make(map[string]*clusterPeers),
Entry: entry,
offlineThreshold: offlineThreshold,
}
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)
}
s, err := sshutils.NewServer(
teleport.ComponentReverseTunnelServer,
// TODO(klizhentas): improve interface, use struct instead of parameter list
// this address is not used
utils.NetAddr{Addr: "127.0.0.1:1", AddrNetwork: "tcp"},
srv,
cfg.HostSigners,
sshutils.AuthMethods{
PublicKey: srv.keyAuth,
},
sshutils.SetLimiter(cfg.Limiter),
sshutils.SetCiphers(cfg.Ciphers),
sshutils.SetKEXAlgorithms(cfg.KEXAlgorithms),
sshutils.SetMACAlgorithms(cfg.MACAlgorithms),
sshutils.SetFIPS(cfg.FIPS),
)
if err != nil {
return nil, err
}
srv.srv = s
go srv.periodicFunctions()
return srv, nil
}
func remoteClustersMap(rc []services.RemoteCluster) map[string]services.RemoteCluster {
out := make(map[string]services.RemoteCluster)
for i := range rc {
out[rc[i].GetName()] = rc[i]
}
return out
}
// disconnectClusters disconnects reverse tunnel connections from remote clusters
// that were deleted from the the local cluster side and cleans up in memory objects.
// In this case all local trust has been deleted, so all the tunnel connections have to be dropped.
func (s *server) disconnectClusters() error {
connectedRemoteClusters := s.getRemoteClusters()
if len(connectedRemoteClusters) == 0 {
return nil
}
remoteClusters, err := s.localAuthClient.GetRemoteClusters()
if err != nil {
return trace.Wrap(err)
}
remoteMap := remoteClustersMap(remoteClusters)
for _, cluster := range connectedRemoteClusters {
if _, ok := remoteMap[cluster.GetName()]; !ok {
s.Infof("Remote cluster %q has been deleted. Disconnecting it from the proxy.", cluster.GetName())
s.RemoveSite(cluster.GetName())
err := cluster.Close()
if err != nil {
s.Debugf("Failure closing cluster %q: %v.", cluster.GetName(), err)
}
}
}
return nil
}
func (s *server) periodicFunctions() {
ticker := time.NewTicker(defaults.ResyncInterval)
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
// Proxies have been updated, notify connected agents about the update.
case proxies := <-s.proxyWatcher.ProxiesC:
s.fanOutProxies(proxies)
case <-ticker.C:
err := s.fetchClusterPeers()
if err != nil {
s.Warningf("Failed to fetch cluster peers: %v.", err)
}
err = s.disconnectClusters()
if err != nil {
s.Warningf("Failed to disconnect clusters: %v.", err)
}
err = s.reportClusterStats()
if err != nil {
s.Warningf("Failed to report cluster stats: %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 node tunnels.
if newConn.GetType() == services.NodeTunnel {
continue
}
// Filter out peer records for 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) reportClusterStats() error {
defer func() {
if r := recover(); r != nil {
s.Warningf("Recovered from panic: %v.", r)
}
}()
clusters := s.GetSites()
for _, cluster := range clusters {
gauge, err := remoteClustersStats.GetMetricWithLabelValues(cluster.GetName())
if err != nil {
return trace.Wrap(err)
}
gauge.Set(float64(cluster.GetTunnelsCount()))
}
return nil
}
func (s *server) addClusterPeers(conns map[string]services.TunnelConnection) error {
for key := range conns {
connInfo := conns[key]
peer, err := newClusterPeer(s, connInfo, s.offlineThreshold)
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(context.TODO())
}
func (s *server) Start() error {
go s.srv.Serve(s.Listener)
return nil
}
func (s *server) Close() error {
s.cancel()
return s.srv.Close()
}
func (s *server) Shutdown(ctx context.Context) error {
s.cancel()
return s.srv.Shutdown(ctx)
}
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,
s.offlineThreshold,
"reverse tunnel server")
channelType := nch.ChannelType()
switch channelType {
// Heartbeats can come from nodes or proxies.
case chanHeartbeat:
s.handleHeartbeat(conn, sconn, nch)
// Transport requests come from nodes requesting a connection to the Auth
// Server through the reverse tunnel.
case chanTransport:
s.handleTransport(sconn, nch)
default:
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. Give them an
// explicit hint.
if channelType == "session" {
msg = "Cannot open new SSH session on reverse tunnel. Are you connecting to the right port?"
}
s.Warn(msg)
nch.Reject(ssh.ConnectionFailed, msg)
return
}
}
func (s *server) handleTransport(sconn *ssh.ServerConn, nch ssh.NewChannel) {
s.Debugf("Transport request: %v.", nch.ChannelType())
channel, requestCh, err := nch.Accept()
if err != nil {
sconn.Close()
s.Warnf("Failed to accept request: %v.", err)
return
}
t := &transport{
log: s.Entry,
closeContext: s.ctx,
authClient: s.LocalAccessPoint,
channel: channel,
requestCh: requestCh,
component: teleport.ComponentReverseTunnelServer,
localClusterName: s.ClusterName,
}
go t.start()
}
func (s *server) handleHeartbeat(conn net.Conn, sconn *ssh.ServerConn, nch ssh.NewChannel) {
s.Debugf("New tunnel from %v.", sconn.RemoteAddr())
if sconn.Permissions.Extensions[extCertType] != extCertTypeHost {
s.Error(trace.BadParameter("can't retrieve certificate type in certType"))
return
}
// Extract the role. For proxies, it's another cluster asking to join, for
// nodes it's a node dialing back.
val, ok := sconn.Permissions.Extensions[extCertRole]
if !ok {
log.Errorf("Failed to accept connection, unknown role: %v.", val)
nch.Reject(ssh.ConnectionFailed, "unknown role")
}
switch {
// Node is dialing back.
case val == string(teleport.RoleNode):
s.handleNewNode(conn, sconn, nch)
// Proxy is dialing back.
case val == string(teleport.RoleProxy):
s.handleNewCluster(conn, sconn, nch)
}
}
func (s *server) handleNewNode(conn net.Conn, sconn *ssh.ServerConn, nch ssh.NewChannel) {
cluster, rconn, err := s.upsertNode(conn, sconn)
if err != nil {
log.Errorf("Failed to upsert node: %v.", err)
sconn.Close()
return
}
ch, req, err := nch.Accept()
if err != nil {
log.Errorf("Failed to accept on channel: %v.", err)
sconn.Close()
return
}
go cluster.handleHeartbeat(rconn, ch, req)
}
func (s *server) handleNewCluster(conn net.Conn, sshConn *ssh.ServerConn, nch ssh.NewChannel) {
// add the incoming site (cluster) to the list of active connections:
site, remoteConn, err := s.upsertRemoteCluster(conn, sshConn)
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))
sshConn.Close()
return
}
go site.handleHeartbeat(remoteConn, ch, req)
}
func (s *server) findLocalCluster(sconn *ssh.ServerConn) (*localSite, error) {
// Cluster name was extracted from certificate and packed into extensions.
clusterName := sconn.Permissions.Extensions[extAuthority]
if strings.TrimSpace(clusterName) == "" {
return nil, trace.BadParameter("empty cluster name")
}
for _, ls := range s.localSites {
if ls.domainName == clusterName {
return ls, nil
}
}
return nil, trace.BadParameter("local cluster %v not found", clusterName)
}
// 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, address string) 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) getTrustedCAKeysByID(id services.CertAuthID) ([]ssh.PublicKey, error) {
ca, err := s.localAccessPoint.GetCertAuthority(id, false, services.SkipValidation())
if err != nil {
return nil, trace.Wrap(err)
}
return ca.Checkers()
}
func (s *server) getTrustedCAKeys(certType services.CertAuthType) ([]ssh.PublicKey, error) {
cas, err := s.localAccessPoint.GetCertAuthorities(certType, false, services.SkipValidation())
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) 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.Warnf("Failed to authenticate host, err: %v.", err)
return nil, trace.Wrap(err)
}
certRole, ok := cert.Extensions[utils.CertExtensionRole]
if !ok || certRole == "" {
err := trace.BadParameter("certificate missing role")
logger.Warnf("Failed to authenticate host, err: %v.", err)
return nil, trace.Wrap(err)
}
err := s.checkHostCert(logger, conn.User(), authDomain, cert)
if err != nil {
logger.Warnf("Failed to authenticate host, err: %v.", err)
return nil, trace.Wrap(err)
}
return &ssh.Permissions{
Extensions: map[string]string{
extHost: conn.User(),
extCertType: extCertTypeHost,
extCertRole: certRole,
extAuthority: authDomain,
},
}, nil
default:
return nil, trace.BadParameter("unsupported cert type: %v", cert.CertType)
}
}
// checkHostCert verifies that host certificate is signed
// by the recognized certificate authority
func (s *server) checkHostCert(logger *log.Entry, user string, clusterName string, cert *ssh.Certificate) error {
if cert.CertType != ssh.HostCert {
return trace.BadParameter("expected host cert, got wrong cert type: %d", cert.CertType)
}
// fetch keys of the certificate authority to check
// if there is a match
keys, err := s.getTrustedCAKeysByID(services.CertAuthID{
Type: services.HostCA,
DomainName: clusterName,
})
if err != nil {
return trace.Wrap(err)
}
// match key of the certificate authority with the signature key
var match bool
for _, k := range keys {
if sshutils.KeysEqual(k, cert.SignatureKey) {
match = true
break
}
}
if !match {
return trace.NotFound("cluster %v has no matching CA keys", clusterName)
}
checker := utils.CertChecker{
FIPS: s.FIPS,
}
if err := checker.CheckCert(user, cert); err != nil {
return trace.BadParameter(err.Error())
}
return nil
}
func (s *server) upsertNode(conn net.Conn, sconn *ssh.ServerConn) (*localSite, *remoteConn, error) {
s.Lock()
defer s.Unlock()
cluster, err := s.findLocalCluster(sconn)
if err != nil {
return nil, nil, trace.Wrap(err)
}
nodeID, ok := sconn.Permissions.Extensions[extHost]
if !ok {
return nil, nil, trace.BadParameter("host id not found")
}
rconn, err := cluster.addConn(nodeID, conn, sconn)
if err != nil {
return nil, nil, trace.Wrap(err)
}
return cluster, rconn, nil
}
func (s *server) upsertRemoteCluster(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 cluster 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, sshConn.Conn)
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
}
func (s *server) getRemoteClusters() []*remoteSite {
s.RLock()
defer s.RUnlock()
out := make([]*remoteSite, len(s.remoteSites))
copy(out, s.remoteSites)
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)
}
// fanOutProxies is a non-blocking call that updated the watches proxies
// list and notifies all clusters about the proxy list change
func (s *server) fanOutProxies(proxies []services.Server) {
s.Lock()
defer s.Unlock()
for _, cluster := range s.localSites {
cluster.fanOutProxies(proxies)
}
for _, cluster := range s.remoteSites {
cluster.fanOutProxies(proxies)
}
}
// newRemoteSite helper creates and initializes 'remoteSite' instance
func newRemoteSite(srv *server, domainName string, sconn ssh.Conn) (*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)
}
closeContext, cancel := context.WithCancel(srv.ctx)
remoteSite := &remoteSite{
srv: srv,
domainName: domainName,
connInfo: connInfo,
Entry: log.WithFields(log.Fields{
trace.Component: teleport.ComponentReverseTunnelServer,
trace.ComponentFields: log.Fields{
"cluster": domainName,
},
}),
ctx: closeContext,
cancel: cancel,
clock: srv.Clock,
offlineThreshold: srv.offlineThreshold,
}
// 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
clt, _, err := remoteSite.getRemoteClient()
if err != nil {
return nil, trace.Wrap(err)
}
remoteSite.remoteClient = clt
// DELETE IN: 4.1.0
//
// Try to get the version of Teleport the remote cluster is running. Older
// versions of Teleport don't support the Watch endpoints needed for the
// cache, for those clusters, don't use a cache.
version, err := sendVersionRequest(sconn, closeContext)
if err != nil {
remoteSite.Debugf("Connected to legacy cluster %v, not using cache.", domainName)
remoteSite.remoteAccessPoint = clt
} else {