-
Notifications
You must be signed in to change notification settings - Fork 0
/
tun.go
1090 lines (987 loc) · 32.6 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
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 auth
import (
"context"
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"sort"
"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"
"github.com/gravitational/trace"
"github.com/mailgun/ttlmap"
log "github.com/sirupsen/logrus"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/agent"
"github.com/tstranex/u2f"
)
// dialRetryInterval specifies the time interval tun client waits to retry
// dialing the same auth server
const dialRetryInterval = 100 * time.Millisecond
// AuthTunnel listens on TCP/IP socket and accepts SSH connections. It then establishes
// an SSH tunnel which HTTP requests travel over. In other words, the Auth Service API
// runs on HTTP-via-SSH-tunnel.
//
// Use auth.TunClient to connect to AuthTunnel
type AuthTunnel struct {
// authServer implements the "beef" of the Auth service
authServer *AuthServer
config *APIConfig
// 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
}
// 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
// embed auth API HTTP client
Client
user string
// static auth servers are CAs set via configuration (--auth flag) and
// they do not change
staticAuthServers []utils.NetAddr
// discoveredAuthServers are CAs that get discovered at runtime
discoveredAuthServers []utils.NetAddr
authMethods []ssh.AuthMethod
refreshTicker *time.Ticker
closeC chan struct{}
closeOnce sync.Once
addrStorage utils.AddrStorage
// purpose is used for more informative logging. it explains _why_ this
// client was created
purpose string
// throttler is used to throttle auth servers that we have failed to dial
// for some period of time
throttler *ttlmap.TtlMap
}
// 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.
// This is how "site API" (aka "auth API") is served: by creating
// an "tunnel server" which serves HTTP via SSH.
func NewTunnel(addr utils.NetAddr,
hostSigner ssh.Signer,
apiConf *APIConfig,
opts ...ServerOption) (tunnel *AuthTunnel, err error) {
tunnel = &AuthTunnel{
authServer: apiConf.AuthServer,
config: apiConf,
}
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(
teleport.ComponentAuth,
addr,
tunnel,
[]ssh.Signer{hostSigner},
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.Debugf("[AUTH] new channel request for %v from %v", nch.ChannelType(), sconn.RemoteAddr())
cht := nch.ChannelType()
switch cht {
// New connection to the Auth API via SSH:
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.onAPIConnection(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)
case "session":
nch.Reject(ssh.UnknownChannelType,
"Cannot open new SSH session on the auth server. Are you connecting to the right port?")
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 {
domainName, err := s.authServer.GetDomainName()
if err != nil {
return false
}
key, err := s.authServer.GetCertAuthority(services.CertAuthID{DomainName: 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
}
// findUserAuthority finds matching user CA based on its public key
func (s *AuthTunnel) findUserAuthority(auth ssh.PublicKey) (services.CertAuthority, error) {
cas, err := s.authServer.GetCertAuthorities(services.UserCA, false)
if err != nil {
return nil, trace.Wrap(err)
}
for _, ca := range cas {
checkers, err := ca.Checkers()
if err != nil {
return nil, trace.Wrap(err)
}
for _, checker := range checkers {
if sshutils.KeysEqual(checker, auth) {
return ca, nil
}
}
}
return nil, trace.NotFound("no matching certificate authority found")
}
// 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 {
_, err := s.findUserAuthority(auth)
if err != nil {
if !trace.IsNotFound(err) {
// something bad happened, need to log
log.Error(err)
}
return false
}
return true
}
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[ExtOrigin] != string(teleport.RoleWeb) {
log.Errorf("role %v doesn't have permission to request agent",
sconn.Permissions.Extensions[ExtOrigin])
return
}
ws, err := s.authServer.GetWebSession(sconn.User(), sconn.Permissions.Extensions[ExtWebSession])
if err != nil {
log.Errorf("session error: %v", trace.DebugReport(err))
return
}
priv, err := ssh.ParseRawPrivateKey(ws.GetPriv())
if err != nil {
log.Errorf("session error: %v", trace.DebugReport(err))
return
}
pub, _, _, _, err := ssh.ParseAuthorizedKey(ws.GetPub())
if err != nil {
log.Errorf("session error: %v", trace.DebugReport(err))
return
}
cert, ok := pub.(*ssh.Certificate)
if !ok {
log.Errorf("session error, not a certificate: %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", trace.DebugReport(err))
return
}
if err := agent.ServeAgent(newKeyAgent, ch); err != nil && err != io.EOF {
log.Errorf("Serve agent err: %v", trace.DebugReport(err))
}
}
// onAPIConnection 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) onAPIConnection(sconn *ssh.ServerConn, sshChan ssh.Channel, req *sshutils.DirectTCPIPReq) {
defer sconn.Close()
var user interface{} = nil
if extRole, ok := sconn.Permissions.Extensions[ExtRole]; ok {
// retreive the role from thsi connection's permissions (make sure it's a valid role)
systemRole := teleport.Role(extRole)
if err := systemRole.Check(); err != nil {
log.Error(err.Error())
return
}
user = teleport.BuiltinRole{Role: systemRole}
} else if clusterName, ok := sconn.Permissions.Extensions[utils.CertTeleportUserCA]; ok {
// we got user signed by remote certificate authority
var remoteRoles []string
var err error
data, ok := sconn.Permissions.Extensions[teleport.CertExtensionTeleportRoles]
if ok {
remoteRoles, err = services.UnmarshalCertRoles(data)
if err != nil {
log.Error(err.Error())
return
}
}
user = teleport.RemoteUser{
ClusterName: clusterName,
Username: sconn.Permissions.Extensions[utils.CertTeleportUser],
RemoteRoles: remoteRoles,
}
} else if teleportUser, ok := sconn.Permissions.Extensions[utils.CertTeleportUser]; ok {
// we got user signed by local certificate authority
user = teleport.LocalUser{
Username: teleportUser,
}
} else {
log.Errorf("expected %v or %v extensions for %v, found none in %v", ExtRole, utils.CertTeleportUser, sconn.User(), sconn.Permissions.Extensions)
return
}
api := NewAPIServer(s.config)
socket := fakeSocket{
closed: make(chan int),
connections: make(chan net.Conn),
}
go func() {
connection := &FakeSSHConnection{
remoteAddr: sconn.RemoteAddr(),
sshChan: sshChan,
closed: make(chan int),
}
// fakesocket.Accept() will pick it up:
socket.connections <- connection
// wait for the connection wrapper to close, so we'll close
// the fake socket, causing http.Serve() below to stop
<-connection.closed
socket.Close()
}()
// serve HTTP API via this SSH connection until it gets closed:
http.Serve(&socket, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// take SSH client name and pass it to HTTP API via HTTP Auth
api.ServeHTTP(w, r.WithContext(context.WithValue(context.TODO(), teleport.ContextUser, user)))
}))
}
func (s *AuthTunnel) keyAuth(
conn ssh.ConnMetadata, key ssh.PublicKey) (*ssh.Permissions, error) {
log.Infof("[AUTH] keyAuth: %v->%v, user=%v", conn.RemoteAddr(), conn.LocalAddr(), conn.User())
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)
}
ca, err := s.findUserAuthority(cert.SignatureKey)
if 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)
}
clusterName, err := s.authServer.GetDomainName()
if 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)
}
// if this is local CA, we assume that local user exists
if clusterName == ca.GetClusterName() {
// we are not using cert extensions for User certificates because of OpenSSH bug
// https://bugzilla.mindrot.org/show_bug.cgi?id=2387
return &ssh.Permissions{
Extensions: map[string]string{
ExtHost: conn.User(),
utils.CertTeleportUser: cert.KeyId,
},
}, nil
}
// otherwise we return this as a remote CA
permissions := &ssh.Permissions{
Extensions: map[string]string{
ExtHost: conn.User(),
utils.CertTeleportUserCA: ca.GetID().DomainName,
utils.CertTeleportUser: cert.KeyId,
},
}
extensions, ok := cert.Permissions.Extensions[teleport.CertExtensionTeleportRoles]
if ok {
permissions.Extensions[teleport.CertExtensionTeleportRoles] = extensions
}
return permissions, nil
}
// passwordAuth is called to authenticate an incoming SSH connection
// to the auth server. Such connections are usually created using a
// TunClient object
//
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("[AUTH] login attempt: user %q type %q", conn.User(), ab.Type)
switch ab.Type {
// user is trying to get in using their password+otp
case AuthWebPassword:
if err := s.authServer.CheckPassword(conn.User(), ab.Pass, ab.OTPToken); err != nil {
return nil, trace.Wrap(err)
}
perms := &ssh.Permissions{
Extensions: map[string]string{
ExtWebPassword: "<password>",
utils.CertTeleportUser: conn.User(),
},
}
log.Infof("[AUTH] password+otp authenticated user: %q", conn.User())
return perms, nil
// user is trying to get in using their password only
case AuthWebPasswordWithoutOTP:
if err := s.authServer.CheckPasswordWOToken(conn.User(), ab.Pass); err != nil {
return nil, trace.Wrap(err)
}
perms := &ssh.Permissions{
Extensions: map[string]string{
ExtWebPassword: "<password>",
utils.CertTeleportUser: conn.User(),
},
}
log.Infof("[AUTH] password authenticated user: %q", conn.User())
return perms, nil
case AuthWebU2FSign:
if err := s.authServer.CheckPasswordWOToken(conn.User(), ab.Pass); err != nil {
return nil, trace.Wrap(err)
}
// notice RoleNop here - it can literally call to nothing except one
// method that everyone is authorized to do - request a sign in
perms := &ssh.Permissions{
Extensions: map[string]string{
ExtWebPassword: "<password>",
ExtRole: string(teleport.RoleNop),
},
}
log.Infof("[AUTH] u2f sign authenticated user: '%v'", conn.User())
return perms, nil
case AuthWebU2F:
if err := s.authServer.CheckU2FSignResponse(conn.User(), &ab.U2FSignResponse); err != nil {
return nil, trace.Wrap(err)
}
perms := &ssh.Permissions{
Extensions: map[string]string{
ExtWebU2F: "<u2f-sign-response>",
utils.CertTeleportUser: 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),
// Origin is used to mark this connection as
// originated with web, as some features
// like agent request are only available
// for web users
ExtOrigin: string(teleport.RoleWeb),
utils.CertTeleportUser: conn.User(),
},
}
if _, err := s.authServer.GetWebSession(conn.User(), string(ab.Pass)); err != nil {
return nil, trace.AccessDenied("session resume error: %v", err)
}
log.Infof("[AUTH] session authenticated user: '%v'", conn.User())
return perms, nil
// when a new server tries to use the auth API to register in the cluster,
// it will use the token as a passowrd (happens only once during registration):
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),
}}
log.Infof("[AUTH] Successfully accepted token for %v", 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("[AUTH] session authenticated prov. token: '%v'", conn.User())
return perms, nil
case AuthValidateTrustedCluster:
err := s.authServer.validateTrustedClusterToken(string(ab.Pass))
if err != nil {
return nil, trace.AccessDenied("trusted cluster token validation error: %v", err)
}
perms := &ssh.Permissions{
Extensions: map[string]string{
ExtToken: string(password),
ExtRole: string(teleport.RoleNop),
}}
log.Debugf("[AUTH] session authenticated validate trusted cluster; token: %q", conn.User())
return perms, nil
default:
return nil, trace.AccessDenied("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"` // HotpToken is deprecated, use OTPToken.
OTPToken string `json:"otp_token"`
U2FSignResponse u2f.SignResponse `json:"u2fSignResponse"`
}
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, otpToken string) ([]ssh.AuthMethod, error) {
data, err := json.Marshal(authBucket{
Type: AuthWebPassword,
User: user,
Pass: password,
HotpToken: otpToken, // HotpToken is deprecated, used OTPToken.
OTPToken: otpToken,
})
if err != nil {
return nil, err
}
return []ssh.AuthMethod{ssh.Password(string(data))}, nil
}
func NewWebPasswordWithoutOTPAuth(user string, password []byte) ([]ssh.AuthMethod, error) {
data, err := json.Marshal(authBucket{
Type: AuthWebPasswordWithoutOTP,
User: user,
Pass: password,
})
if err != nil {
return nil, err
}
return []ssh.AuthMethod{ssh.Password(string(data))}, nil
}
// NewWebPasswordU2FSignAuth is for getting a U2F sign challenge
func NewWebPasswordU2FSignAuth(user string, password []byte) ([]ssh.AuthMethod, error) {
data, err := json.Marshal(authBucket{
Type: AuthWebU2FSign,
User: user,
Pass: password,
})
if err != nil {
return nil, err
}
return []ssh.AuthMethod{ssh.Password(string(data))}, nil
}
// NewWebU2FSignResponseAuth is for signing in with a U2F sign response
func NewWebU2FSignResponseAuth(user string, u2fSignResponse *u2f.SignResponse) ([]ssh.AuthMethod, error) {
data, err := json.Marshal(authBucket{
Type: AuthWebU2F,
User: user,
U2FSignResponse: *u2fSignResponse,
})
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 NewValidateTrustedClusterAuth(token string) ([]ssh.AuthMethod, error) {
data, err := json.Marshal(authBucket{
Type: AuthValidateTrustedCluster,
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
}
}
// 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
// - purpose is mostly for debuggin, like "web client" or "reverse tunnel client"
// - authServers: list of auth servers in this cluster (they are supposed to be in sync)
// - authMethods: how to authenticate (via cert, web passwowrd, etc)
// - opts : functional arguments for further extending
func NewTunClient(purpose string,
authServers []utils.NetAddr,
user string,
authMethods []ssh.AuthMethod,
opts ...TunClientOption) (*TunClient, error) {
if user == "" {
return nil, trace.BadParameter("SSH connection requires a valid username")
}
throttler, err := ttlmap.NewMap(16)
if err != nil {
return nil, trace.Wrap(err)
}
tc := &TunClient{
purpose: purpose,
user: user,
staticAuthServers: authServers,
authMethods: authMethods,
closeC: make(chan struct{}),
throttler: throttler,
}
for _, o := range opts {
o(tc)
}
log.Debugf("NewTunClient(%v) with auth: %v", purpose, authServers)
clt, err := NewClient("http://stub:0", tc.Dial)
if err != nil {
return nil, trace.Wrap(err)
}
tc.Client = *clt
// use local information about auth servers if it's available
if tc.addrStorage != nil {
cachedAuthServers, err := tc.addrStorage.GetAddresses()
if err != nil {
if !trace.IsNotFound(err) {
log.Warnf("unable to load the auth server cache: %s", err.Error())
}
} else {
tc.setAuthServers(cachedAuthServers)
}
}
return tc, nil
}
func (c *TunClient) throttleAuthServer(addr string) {
c.Lock()
defer c.Unlock()
c.throttler.Set(addr, "ok", int(defaults.DefaultThrottleTimeout/time.Second))
}
func (c *TunClient) isAuthServerThrottled(addr string) bool {
c.Lock()
defer c.Unlock()
_, ok := c.throttler.Get(addr)
return ok
}
func (c *TunClient) String() string {
return fmt.Sprintf("TunClient[%s]", c.purpose)
}
// Close releases all the resources allocated for this client
func (c *TunClient) Close() error {
if c != nil {
log.Debugf("%v.Close()", c)
c.GetTransport().CloseIdleConnections()
c.closeOnce.Do(func() {
close(c.closeC)
})
}
return nil
}
// GetDialer returns dialer that will connect to auth server API
func (c *TunClient) GetDialer() AccessPointDialer {
addrNetwork := c.staticAuthServers[0].AddrNetwork
const dialRetryTimes = 3
return func() (conn net.Conn, err error) {
for attempt := 0; attempt < dialRetryTimes; attempt++ {
conn, err = c.Dial(addrNetwork, "accesspoint:0")
if err == nil {
return conn, nil
}
time.Sleep(4 * time.Duration(attempt) * dialRetryInterval)
}
log.Errorf("%v: ", err)
return nil, trace.Wrap(err)
}
}
// GetAgent creates an SSH key agent (similar object to what CLI uses), this
// key agent fetches user keys directly from the auth server using a custom channel
// created via "ReqWebSessionAgent" reguest
func (c *TunClient) GetAgent() (AgentCloser, error) {
client, err := c.getClient() // we need an established connection first
if err != nil {
return nil, trace.Wrap(err)
}
// create a special SSH channel into the auth server, which will be used to
// serve user keys to a web-based terminal client (which will be using the
// returned SSH agent)
ch, _, err := client.OpenChannel(ReqWebSessionAgent, nil)
if err != nil {
return nil, trace.ConnectionProblem(err, "failed to connect to remote API")
}
ta := &tunAgent{client: client}
ta.Agent = agent.NewClient(ch)
return ta, nil
}
// Dial dials to Auth server's HTTP API over SSH tunnel.
func (c *TunClient) Dial(network, address string) (net.Conn, error) {
log.Debugf("TunClient[%s].Dial()", c.purpose)
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, "can't connect to auth API")
}
// dialed & authenticated? lets start synchronizing the
// list of auth servers:
if c.refreshTicker == nil {
c.refreshTicker = time.NewTicker(defaults.AuthServersRefreshPeriod)
go c.authServersSyncLoop()
}
return &tunConn{client: client, Conn: conn}, 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
}
// authServersSyncLoop continuously refreshes the list of available auth servers
// for this client
func (c *TunClient) authServersSyncLoop() {
log.Debugf("%v: authServersSyncLoop() started", c)
defer c.refreshTicker.Stop()
// initial fetch for quick start-ups
c.fetchAndSync()
for {
select {
// timer-based refresh:
case <-c.refreshTicker.C:
c.fetchAndSync()
// received a signal to quit?
case <-c.closeC:
log.Debugf("%v: authServersSyncLoop() 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.GetAddr())
if err != nil {
return nil, trace.Wrap(err)
}
if !serverAddr.IsLocal() {
authServers = append(authServers, *serverAddr)
}
}
return authServers, nil
}
// getAuthServers returns a sorted list of auth servers
func (c *TunClient) getAuthServers() (out []utils.NetAddr) {
c.Lock()
defer c.Unlock()
// return static auth servers followed by discovered ones. this guarantees
// that the client will try statically configured ones first
out = make([]utils.NetAddr, 0, len(c.staticAuthServers)+len(c.discoveredAuthServers))
out = append(out, c.staticAuthServers...)
out = append(out, c.discoveredAuthServers...)
return out
}
// byAddress allows to sort slices of addresses by implementing sort.Interface
type byAddress []utils.NetAddr
func (a byAddress) Len() int { return len(a) }
func (a byAddress) Less(i, j int) bool { return a[i].Addr < a[j].Addr }
func (a byAddress) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
// setAuthServers assigns a new list of auth servers (CAs) who all together
// control the cluster (site)
//
// it keeps the list of auth servers sorted
func (c *TunClient) setAuthServers(servers []utils.NetAddr) {
sort.Sort(byAddress(servers))
c.Lock()
defer c.Unlock()
c.discoveredAuthServers = servers
}
// getClient returns an established SSH connection to one of the auth servers (CAs)
// for the cluster.
func (c *TunClient) getClient() (client *ssh.Client, err error) {
// see if we have any auth servers online:
authServers := c.getAuthServers()
if len(authServers) == 0 {
return nil, trace.ConnectionProblem(nil, "all auth servers are offline")
}
log.Debugf("%v.authServers: %v", c, authServers)
// try to connect to the 1st one who will pick up:
for _, authServer := range authServers {
if c.isAuthServerThrottled(authServer.String()) {
continue
}
client, err = c.dialAuthServer(authServer)
if err == nil {
return client, nil
}
// if it's an auth failure, fail right away because all auth servers are backed
// by the same data store and result does not depend on which auth server you hit.
if trace.IsAccessDenied(err) {
return nil, trace.Wrap(err)
}
log.Errorf("%v.getClient() error while connecting to auth server %v: %v: throttling", c, authServer, err)