forked from gravitational/teleport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.go
643 lines (587 loc) · 18 KB
/
helpers.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
/*
Copyright 2017 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 (
"crypto/tls"
"crypto/x509"
"net"
"os"
"path/filepath"
"time"
"github.com/gravitational/teleport"
authority "github.com/gravitational/teleport/lib/auth/testauthority"
"github.com/gravitational/teleport/lib/backend"
"github.com/gravitational/teleport/lib/backend/boltbk"
"github.com/gravitational/teleport/lib/defaults"
"github.com/gravitational/teleport/lib/events"
"github.com/gravitational/teleport/lib/limiter"
"github.com/gravitational/teleport/lib/services"
"github.com/gravitational/teleport/lib/services/local"
"github.com/gravitational/teleport/lib/services/suite"
"github.com/gravitational/teleport/lib/session"
"github.com/gravitational/teleport/lib/utils"
"github.com/gravitational/trace"
"github.com/jonboulle/clockwork"
)
// TestAuthServerConfig is auth server test config
type TestAuthServerConfig struct {
// ClusterName is cluster name
ClusterName string
// Dir is directory for local backend
Dir string
}
// CheckAndSetDefaults checks and sets defaults
func (cfg *TestAuthServerConfig) CheckAndSetDefaults() error {
if cfg.ClusterName == "" {
cfg.ClusterName = "localhost"
}
if cfg.Dir == "" {
return trace.BadParameter("missing parameter Dir")
}
return nil
}
// CreateUploaderDir creates directory for file uploader service
func CreateUploaderDir(dir string) error {
return os.MkdirAll(filepath.Join(dir, teleport.LogsDir, teleport.ComponentUpload,
events.SessionLogsDir, defaults.Namespace), teleport.SharedDirMode)
}
// TestAuthServer is auth server using local filesystem backend
// and test certificate authority key generation that speeds up
// keygen by using the same private key
type TestAuthServer struct {
// TestAuthServer config is configuration used for auth server setup
TestAuthServerConfig
// AuthServer is an auth server
AuthServer *AuthServer
// AuditLog is an event audit log
AuditLog events.IAuditLog
// SessionLogger is a session logger
SessionServer session.Service
// Backend is a backend for auth server
Backend backend.Backend
// Authorizer is an authorizer used in tests
Authorizer Authorizer
}
// NewTestAuthServer returns new instances of Auth server
func NewTestAuthServer(cfg TestAuthServerConfig) (*TestAuthServer, error) {
if err := cfg.CheckAndSetDefaults(); err != nil {
return nil, trace.Wrap(err)
}
srv := &TestAuthServer{
TestAuthServerConfig: cfg,
}
var err error
srv.Backend, err = boltbk.New(backend.Params{"path": cfg.Dir})
if err != nil {
return nil, trace.Wrap(err)
}
srv.AuditLog, err = events.NewAuditLog(events.AuditLogConfig{
DataDir: cfg.Dir,
RecordSessions: true,
ServerID: cfg.ClusterName,
})
if err != nil {
return nil, trace.Wrap(err)
}
srv.SessionServer, err = session.New(srv.Backend)
if err != nil {
return nil, trace.Wrap(err)
}
access := local.NewAccessService(srv.Backend)
identity := local.NewIdentityService(srv.Backend)
clusterName, err := services.NewClusterName(services.ClusterNameSpecV2{
ClusterName: cfg.ClusterName,
})
if err != nil {
return nil, trace.Wrap(err)
}
srv.AuthServer, err = NewAuthServer(&InitConfig{
ClusterName: clusterName,
Backend: srv.Backend,
Authority: authority.New(),
Access: access,
Identity: identity,
AuditLog: srv.AuditLog,
SkipPeriodicOperations: true,
})
if err != nil {
return nil, trace.Wrap(err)
}
// set cluster config
clusterConfig, err := services.NewClusterConfig(services.ClusterConfigSpecV3{
SessionRecording: services.RecordAtNode,
})
if err != nil {
return nil, trace.Wrap(err)
}
err = srv.AuthServer.SetClusterConfig(clusterConfig)
if err != nil {
return nil, trace.Wrap(err)
}
// set cluster name in the backend
err = srv.AuthServer.SetClusterName(clusterName)
if err != nil {
return nil, trace.Wrap(err)
}
authPreference, err := services.NewAuthPreference(services.AuthPreferenceSpecV2{
Type: teleport.Local,
SecondFactor: teleport.OFF,
})
if err != nil {
return nil, trace.Wrap(err)
}
err = srv.AuthServer.SetAuthPreference(authPreference)
if err != nil {
return nil, trace.Wrap(err)
}
// set static tokens
staticTokens, err := services.NewStaticTokens(services.StaticTokensSpecV2{
StaticTokens: []services.ProvisionToken{},
})
if err != nil {
return nil, trace.Wrap(err)
}
err = srv.AuthServer.SetStaticTokens(staticTokens)
if err != nil {
return nil, trace.Wrap(err)
}
// create the default role
err = srv.AuthServer.UpsertRole(services.NewAdminRole(), backend.Forever)
if err != nil {
return nil, trace.Wrap(err)
}
// set up host private key and certificate
srv.AuthServer.UpsertCertAuthority(suite.NewTestCA(services.HostCA, srv.ClusterName))
if err != nil {
return nil, trace.Wrap(err)
}
srv.AuthServer.UpsertCertAuthority(suite.NewTestCA(services.UserCA, srv.ClusterName))
if err != nil {
return nil, trace.Wrap(err)
}
srv.Authorizer, err = NewAuthorizer(srv.AuthServer.Access, srv.AuthServer.Identity, srv.AuthServer.Trust)
if err != nil {
return nil, trace.Wrap(err)
}
return srv, nil
}
// GenerateUserCert takes the public key in the OpenSSH `authorized_keys`
// plain text format, signs it using User Certificate Authority signing key and returns the
// resulting certificate.
func (a *TestAuthServer) GenerateUserCert(key []byte, username string, ttl time.Duration, compatibility string) ([]byte, error) {
user, err := a.AuthServer.GetUser(username)
if err != nil {
return nil, trace.Wrap(err)
}
checker, err := services.FetchRoles(user.GetRoles(), a.AuthServer, user.GetTraits())
if err != nil {
return nil, trace.Wrap(err)
}
certs, err := a.AuthServer.generateUserCert(certRequest{
user: user,
roles: checker,
ttl: ttl,
compatibility: compatibility,
publicKey: key,
})
if err != nil {
return nil, trace.Wrap(err)
}
return certs.ssh, nil
}
// NewCertificate returns new TLS credentials generated by test auth server
func (a *TestAuthServer) NewCertificate(identity TestIdentity) (*tls.Certificate, error) {
switch id := identity.I.(type) {
case LocalUser:
user, err := a.AuthServer.GetUser(id.Username)
if err != nil {
return nil, trace.Wrap(err)
}
roles, err := services.FetchRoles(user.GetRoles(), a.AuthServer, user.GetTraits())
if err != nil {
return nil, trace.Wrap(err)
}
if identity.TTL == 0 {
identity.TTL = time.Hour
}
priv, pub, err := a.AuthServer.GenerateKeyPair("")
if err != nil {
return nil, trace.Wrap(err)
}
certs, err := a.AuthServer.generateUserCert(certRequest{
publicKey: pub,
user: user,
roles: roles,
ttl: identity.TTL,
})
if err != nil {
return nil, trace.Wrap(err)
}
cert, err := tls.X509KeyPair(certs.tls, priv)
if err != nil {
return nil, trace.Wrap(err)
}
return &cert, nil
case BuiltinRole:
keys, err := a.AuthServer.GenerateServerKeys(GenerateServerKeysRequest{
HostID: id.Username,
NodeName: id.Username,
Roles: teleport.Roles{id.Role},
})
if err != nil {
return nil, trace.Wrap(err)
}
cert, err := tls.X509KeyPair(keys.TLSCert, keys.Key)
if err != nil {
return nil, trace.Wrap(err)
}
return &cert, nil
default:
return nil, trace.BadParameter("identity of unknown type %T is unsupported", identity)
}
}
// Clock returns clock used by auth server
func (a *TestAuthServer) Clock() clockwork.Clock {
return a.AuthServer.GetClock()
}
// Trust adds other server host certificate authority as trusted
func (a *TestAuthServer) Trust(remote *TestAuthServer, roleMap services.RoleMap) error {
remoteCA, err := remote.AuthServer.GetCertAuthority(services.CertAuthID{
Type: services.HostCA,
DomainName: remote.ClusterName,
}, false)
if err != nil {
return trace.Wrap(err)
}
err = a.AuthServer.UpsertCertAuthority(remoteCA)
if err != nil {
return trace.Wrap(err)
}
remoteCA, err = remote.AuthServer.GetCertAuthority(services.CertAuthID{
Type: services.UserCA,
DomainName: remote.ClusterName,
}, false)
if err != nil {
return trace.Wrap(err)
}
remoteCA.SetRoleMap(roleMap)
err = a.AuthServer.UpsertCertAuthority(remoteCA)
if err != nil {
return trace.Wrap(err)
}
return nil
}
// NewTestTLSServer returns new test TLS server
func (a *TestAuthServer) NewTestTLSServer() (*TestTLSServer, error) {
apiConfig := &APIConfig{
AuthServer: a.AuthServer,
Authorizer: a.Authorizer,
SessionService: a.SessionServer,
AuditLog: a.AuditLog,
}
srv, err := NewTestTLSServer(TestTLSServerConfig{
APIConfig: apiConfig,
AuthServer: a,
})
if err != nil {
return nil, trace.Wrap(err)
}
return srv, nil
}
// NewRemoteClient creates new client to the remote server using identity
// generated for this certificate authority
func (a *TestAuthServer) NewRemoteClient(identity TestIdentity, addr net.Addr, pool *x509.CertPool) (*Client, error) {
tlsConfig := utils.TLSConfig()
cert, err := a.NewCertificate(identity)
if err != nil {
return nil, trace.Wrap(err)
}
tlsConfig.Certificates = []tls.Certificate{*cert}
tlsConfig.RootCAs = pool
addrs := []utils.NetAddr{{
AddrNetwork: addr.Network(),
Addr: addr.String()}}
return NewTLSClient(addrs, tlsConfig)
}
// TestTLSServerConfig is a configuration for test TLS server
type TestTLSServerConfig struct {
// APIConfig is a configuration of API server
APIConfig *APIConfig
// AuthServer is a test auth server used to serve requests
AuthServer *TestAuthServer
// Limiter is a connection and request limiter
Limiter *limiter.LimiterConfig
// Listener is a listener to serve requests on
Listener net.Listener
}
// Auth returns auth server used by this TLS server
func (t *TestTLSServer) Auth() *AuthServer {
return t.AuthServer.AuthServer
}
// TestTLSServer is a test TLS server
type TestTLSServer struct {
// TestTLSServerConfig is a configuration for TLS server
TestTLSServerConfig
// Identity is a generated TLS/SSH identity used to answer in TLS
Identity *Identity
// TLSServer is a configured TLS server
TLSServer *TLSServer
}
// ClusterName returns name of test TLS server cluster
func (t *TestTLSServer) ClusterName() string {
return t.AuthServer.ClusterName
}
// Clock returns clock used by auth server
func (t *TestTLSServer) Clock() clockwork.Clock {
return t.AuthServer.Clock()
}
// CheckAndSetDefaults checks and sets limiter defaults
func (cfg *TestTLSServerConfig) CheckAndSetDefaults() error {
if cfg.APIConfig == nil {
return trace.BadParameter("missing parameter APIConfig")
}
if cfg.AuthServer == nil {
return trace.BadParameter("missing parameter AuthServer")
}
// use very permissive limiter configuration by default
if cfg.Limiter == nil {
cfg.Limiter = &limiter.LimiterConfig{
MaxConnections: 1000,
MaxNumberOfUsers: 1000,
}
}
return nil
}
// NewTestTLSServer returns new test TLS server that is started and is listening
// on 127.0.0.1 loopback on any available port
func NewTestTLSServer(cfg TestTLSServerConfig) (*TestTLSServer, error) {
err := cfg.CheckAndSetDefaults()
if err != nil {
return nil, trace.Wrap(err)
}
srv := &TestTLSServer{
TestTLSServerConfig: cfg,
}
srv.Identity, err = NewServerIdentity(srv.AuthServer.AuthServer, "test-tls-server", teleport.RoleAuth)
if err != nil {
return nil, trace.Wrap(err)
}
// Register TLS endpoint of the auth service
tlsConfig, err := srv.Identity.TLSConfig()
if err != nil {
return nil, trace.Wrap(err)
}
accessPoint, err := NewAdminAuthServer(srv.AuthServer.AuthServer, srv.AuthServer.SessionServer, srv.AuthServer.AuditLog)
if err != nil {
return nil, trace.Wrap(err)
}
srv.TLSServer, err = NewTLSServer(TLSServerConfig{
AccessPoint: accessPoint,
TLS: tlsConfig,
APIConfig: *srv.APIConfig,
LimiterConfig: *srv.Limiter,
})
if err != nil {
return nil, trace.Wrap(err)
}
if err := srv.Start(); err != nil {
return nil, trace.Wrap(err)
}
return srv, nil
}
// TestIdentity is test identity spec used to generate identities in tests
type TestIdentity struct {
I interface{}
TTL time.Duration
}
// TestUser returns TestIdentity for local user
func TestUser(username string) TestIdentity {
return TestIdentity{
I: LocalUser{
Username: username,
},
}
}
// TestNop returns "Nop" - unauthenticated identity
func TestNop() TestIdentity {
return TestIdentity{
I: nil,
}
}
// TestAdmin returns TestIdentity for admin user
func TestAdmin() TestIdentity {
return TestBuiltin(teleport.RoleAdmin)
}
// TestBuiltin returns TestIdentity for builtin user
func TestBuiltin(role teleport.Role) TestIdentity {
return TestIdentity{
I: BuiltinRole{
Role: role,
Username: string(role),
},
}
}
// NewClientFromWebSession returns new authenticated client from web session
func (t *TestTLSServer) NewClientFromWebSession(sess services.WebSession) (*Client, error) {
tlsConfig, err := t.Identity.TLSConfig()
if err != nil {
return nil, trace.Wrap(err)
}
tlsCert, err := tls.X509KeyPair(sess.GetTLSCert(), sess.GetPriv())
if err != nil {
return nil, trace.Wrap(err, "failed to parse TLS cert and key")
}
tlsConfig.Certificates = []tls.Certificate{tlsCert}
addrs := []utils.NetAddr{utils.FromAddr(t.Listener.Addr())}
return NewTLSClient(addrs, tlsConfig)
}
// CertPool returns cert pool that auth server represents
func (t *TestTLSServer) CertPool() (*x509.CertPool, error) {
tlsConfig, err := t.Identity.TLSConfig()
if err != nil {
return nil, trace.Wrap(err)
}
return tlsConfig.RootCAs, nil
}
// ClientTLSConfig returns client TLS config based on the identity
func (t *TestTLSServer) ClientTLSConfig(identity TestIdentity) (*tls.Config, error) {
tlsConfig, err := t.Identity.TLSConfig()
if err != nil {
return nil, trace.Wrap(err)
}
if identity.I != nil {
cert, err := t.AuthServer.NewCertificate(identity)
if err != nil {
return nil, trace.Wrap(err)
}
tlsConfig.Certificates = []tls.Certificate{*cert}
} else {
// this client is not authenticated, which means that auth
// server should apply Nop builtin role
tlsConfig.Certificates = nil
}
return tlsConfig, nil
}
// CloneClient uses the same credentials as the passed client
// but forces the client to be recreated
func (t *TestTLSServer) CloneClient(clt *Client) *Client {
addr := []utils.NetAddr{{Addr: t.Addr().String(), AddrNetwork: t.Addr().Network()}}
newClient, err := NewTLSClient(addr, clt.TLSConfig())
if err != nil {
panic(err)
}
return newClient
}
// NewClient returns new client to test server authenticated with identity
func (t *TestTLSServer) NewClient(identity TestIdentity) (*Client, error) {
tlsConfig, err := t.ClientTLSConfig(identity)
if err != nil {
return nil, trace.Wrap(err)
}
addrs := []utils.NetAddr{utils.FromAddr(t.Listener.Addr())}
return NewTLSClient(addrs, tlsConfig)
}
// Addr returns address of TLS server
func (t *TestTLSServer) Addr() net.Addr {
return t.Listener.Addr()
}
// Start starts TLS server on loopback address on the first lisenting socket
func (t *TestTLSServer) Start() error {
var err error
if t.Listener == nil {
t.Listener, err = net.Listen("tcp", "127.0.0.1:0")
if err != nil {
return trace.Wrap(err)
}
}
go t.TLSServer.Serve(t.Listener)
return nil
}
// Close closes the listener and HTTP server
func (t *TestTLSServer) Close() error {
err := t.TLSServer.Close()
if t.Listener != nil {
t.Listener.Close()
}
if t.AuthServer.Backend != nil {
t.AuthServer.Backend.Close()
}
return err
}
// Stop stops listening server, but does not close the auth backend
func (t *TestTLSServer) Stop() error {
err := t.TLSServer.Close()
if t.Listener != nil {
t.Listener.Close()
}
return err
}
// NewServerIdentity generates new server identity, used in tests
func NewServerIdentity(clt *AuthServer, hostID string, role teleport.Role) (*Identity, error) {
keys, err := clt.GenerateServerKeys(GenerateServerKeysRequest{
HostID: hostID,
NodeName: hostID,
Roles: teleport.Roles{teleport.RoleAuth},
})
if err != nil {
return nil, trace.Wrap(err)
}
return ReadIdentityFromKeyPair(keys.Key, keys.Cert, keys.TLSCert, keys.TLSCACerts)
}
// clt limits required interface to the necessary methods
// used to pass different clients in tests
type clt interface {
UpsertRole(services.Role, time.Duration) error
UpsertUser(services.User) error
}
// CreateUserAndRole creates user and role and assignes role to a user, used in tests
func CreateUserAndRole(clt clt, username string, allowedLogins []string) (services.User, services.Role, error) {
user, err := services.NewUser(username)
if err != nil {
return nil, nil, trace.Wrap(err)
}
role := services.RoleForUser(user)
role.SetLogins(services.Allow, []string{user.GetName()})
err = clt.UpsertRole(role, backend.Forever)
if err != nil {
return nil, nil, trace.Wrap(err)
}
user.AddRole(role.GetName())
err = clt.UpsertUser(user)
if err != nil {
return nil, nil, trace.Wrap(err)
}
return user, role, nil
}
// CreateUserAndRoleWithoutRoles creates user and role, but does not assign user to a role, used in tests
func CreateUserAndRoleWithoutRoles(clt clt, username string, allowedLogins []string) (services.User, services.Role, error) {
user, err := services.NewUser(username)
if err != nil {
return nil, nil, trace.Wrap(err)
}
role := services.RoleForUser(user)
set := services.MakeRuleSet(role.GetRules(services.Allow))
delete(set, services.KindRole)
role.SetRules(services.Allow, set.Slice())
role.SetLogins(services.Allow, []string{user.GetName()})
err = clt.UpsertRole(role, backend.Forever)
if err != nil {
return nil, nil, trace.Wrap(err)
}
user.AddRole(role.GetName())
err = clt.UpsertUser(user)
if err != nil {
return nil, nil, trace.Wrap(err)
}
return user, role, nil
}