forked from gravitational/teleport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
srv.go
471 lines (420 loc) · 12.8 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
/*
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 (
"fmt"
"net"
"net/http"
"strings"
"sync"
"sync/atomic"
"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"
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
// localAuth points to the cluster's auth server API
localAuth 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
// newAccessPoint returns new caching access point
newAccessPoint state.NewCachingAccessPoint
}
// ServerOption sets reverse tunnel server options
type ServerOption func(s *server) error
// DirectSite instructs server to proxy access to this site not using
// reverse tunnel
func DirectSite(domainName string, clt auth.ClientI) ServerOption {
return func(s *server) error {
site, err := newlocalSite(s, domainName, clt)
if err != nil {
return trace.Wrap(err)
}
s.localSites = append(s.localSites, site)
return nil
}
}
// SetLimiter sets rate limiter for reverse tunnel
func SetLimiter(limiter *limiter.Limiter) ServerOption {
return func(s *server) error {
s.limiter = limiter
return nil
}
}
// NewServer creates and returns a reverse tunnel server which is fully
// initialized but hasn't been started yet
func NewServer(addr utils.NetAddr, hostSigners []ssh.Signer,
authAPI auth.AccessPoint, fn state.NewCachingAccessPoint, opts ...ServerOption) (Server, error) {
srv := &server{
localSites: []*localSite{},
remoteSites: []*remoteSite{},
localAuth: authAPI,
newAccessPoint: fn,
}
var err error
srv.limiter, err = limiter.NewLimiter(limiter.LimiterConfig{})
if err != nil {
return nil, trace.Wrap(err)
}
for _, o := range opts {
if err := o(srv); err != nil {
return nil, trace.Wrap(err)
}
}
s, err := sshutils.NewServer(
teleport.ComponentReverseTunnel,
addr,
srv,
hostSigners,
sshutils.AuthMethods{
PublicKey: srv.keyAuth,
},
sshutils.SetLimiter(srv.limiter),
)
if err != nil {
return nil, err
}
srv.hostCertChecker = ssh.CertChecker{IsAuthority: srv.isHostAuthority}
srv.userCertChecker = ssh.CertChecker{IsAuthority: srv.isUserAuthority}
srv.srv = s
return srv, nil
}
func (s *server) Wait() {
s.srv.Wait()
}
func (s *server) Start() error {
return s.srv.Start()
}
func (s *server) Close() error {
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?"
}
log.Warningf(msg)
nch.Reject(ssh.ConnectionFailed, msg)
return
}
log.Debugf("[TUNNEL] new tunnel from %s", sconn.RemoteAddr())
if sconn.Permissions.Extensions[extCertType] != extCertTypeHost {
log.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 {
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
}
// 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 {
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 *server) getTrustedCAKeys(CertType services.CertAuthType) ([]ssh.PublicKey, error) {
cas, err := s.localAuth.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.localAuth.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 := log.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)
}
log.Infof("[TUNNEL] site %v connected from %v. sites: %d",
domainName, conn.RemoteAddr(), len(s.remoteSites))
return site, remoteConn, nil
}
func (s *server) GetSites() []RemoteSite {
s.RLock()
defer s.RUnlock()
out := make([]RemoteSite, 0, len(s.remoteSites)+len(s.localSites))
for i := range s.localSites {
out = append(out, s.localSites[i])
}
for i := range s.remoteSites {
out = append(out, s.remoteSites[i])
}
return out
}
func (s *server) GetSite(domainName string) (RemoteSite, error) {
s.RLock()
defer s.RUnlock()
for i := range s.remoteSites {
if s.remoteSites[i].domainName == domainName {
return s.remoteSites[i], nil
}
}
for i := range s.localSites {
if s.localSites[i].domainName == domainName {
return s.localSites[i], nil
}
}
return nil, trace.NotFound("site '%v' not found", domainName)
}
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("site '%v' not found", domainName)
}
type remoteConn struct {
sshConn ssh.Conn
conn net.Conn
invalid int32
log *log.Entry
counter int32
}
func (rc *remoteConn) String() string {
return fmt.Sprintf("remoteConn(remoteAddr=%v)", rc.conn.RemoteAddr())
}
func (rc *remoteConn) Close() error {
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) {
remoteSite := &remoteSite{
srv: srv,
domainName: domainName,
log: log.WithFields(log.Fields{
teleport.Component: teleport.ComponentReverseTunnel,
teleport.ComponentFields: map[string]string{
"domainName": domainName,
"side": "server",
},
}),
}
// transport uses connection do dial out to the remote address
remoteSite.transport = &http.Transport{
Dial: remoteSite.dialAccessPoint,
}
clt, err := auth.NewClient("http://stub:0", remoteSite.dialAccessPoint)
if err != nil {
return nil, trace.Wrap(err)
}
remoteSite.clt = clt
accessPoint, err := srv.newAccessPoint(clt, []string{"reverse", domainName})
if err != nil {
return nil, trace.Wrap(err)
}
remoteSite.accessPoint = accessPoint
return remoteSite, nil
}
const (
extHost = "host@teleport"
extCertType = "certtype@teleport"
extAuthority = "auth@teleport"
extCertTypeHost = "host"
extCertTypeUser = "user"
)