forked from gravitational/teleport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.go
647 lines (543 loc) · 18.5 KB
/
init.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
/*
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 (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"
"github.com/gravitational/teleport"
"github.com/gravitational/teleport/lib/backend"
"github.com/gravitational/teleport/lib/defaults"
"github.com/gravitational/teleport/lib/services"
"github.com/gravitational/teleport/lib/utils"
log "github.com/Sirupsen/logrus"
"github.com/gravitational/trace"
"golang.org/x/crypto/ssh"
)
// InitConfig is auth server init config
type InitConfig struct {
// Backend is auth backend to use
Backend backend.Backend
// Authority is key generator that we use
Authority Authority
// HostUUID is a UUID of this host
HostUUID string
// NodeName is the DNS name of the node
NodeName string
// DomainName stores the FQDN of the signing CA (its certificate will have this
// name embedded). It is usually set to the GUID of the host the Auth service runs on
DomainName string
// Authorities is a list of pre-configured authorities to supply on first start
Authorities []services.CertAuthority
// AuthServiceName is a human-readable name of this CA. If several Auth services are running
// (managing multiple teleport clusters) this field is used to tell them apart in UIs
// It usually defaults to the hostname of the machine the Auth service runs on.
AuthServiceName string
// DataDir is the full path to the directory where keys, events and logs are kept
DataDir string
// ReverseTunnels is a list of reverse tunnels statically supplied
// in configuration, so auth server will init the tunnels on the first start
ReverseTunnels []services.ReverseTunnel
// OIDCConnectors is a list of trusted OpenID Connect identity providers
// in configuration, so auth server will init the tunnels on the first start
OIDCConnectors []services.OIDCConnector
// Trust is a service that manages users and credentials
Trust services.Trust
// Presence service is a discovery and hearbeat tracker
Presence services.Presence
// Provisioner is a service that keeps track of provisioning tokens
Provisioner services.Provisioner
// Identity is a service that manages users and credentials
Identity services.Identity
// Access is service controlling access to resources
Access services.Access
// ClusterAuthPreferenceService is a service to get and set authentication preferences.
ClusterAuthPreferenceService services.ClusterAuthPreference
// UniversalSecondFactorService is a service to get and set universal second factor settings.
UniversalSecondFactorService services.UniversalSecondFactorSettings
// Roles is a set of roles to create
Roles []services.Role
// StaticTokens are pre-defined host provisioning tokens supplied via config file for
// environments where paranoid security is not needed
StaticTokens []services.ProvisionToken
// AuthPreference defines the authentication type (local, oidc) and second
// factor (off, otp, u2f) passed in from a configuration file.
AuthPreference services.AuthPreference
// U2F defines U2F application ID and any facets passed in from a configuration file.
U2F services.UniversalSecondFactor
// DeveloperMode should only be used during development as it does several
// unsafe things like log sensitive information to console as well as
// not verify certificates.
DeveloperMode bool
}
// Init instantiates and configures an instance of AuthServer
func Init(cfg InitConfig, dynamicConfig bool) (*AuthServer, *Identity, error) {
if cfg.DataDir == "" {
return nil, nil, trace.BadParameter("DataDir: data dir can not be empty")
}
if cfg.HostUUID == "" {
return nil, nil, trace.BadParameter("HostUUID: host UUID can not be empty")
}
err := cfg.Backend.AcquireLock(cfg.DomainName, 30*time.Second)
if err != nil {
return nil, nil, trace.Wrap(err)
}
defer cfg.Backend.ReleaseLock(cfg.DomainName)
// check that user CA and host CA are present and set the certs if needed
asrv := NewAuthServer(&cfg)
// we determine if it's the first start by checking if the CA's are set
firstStart, err := isFirstStart(asrv, cfg)
if err != nil {
return nil, nil, trace.Wrap(err)
}
// the logic for when to upload resources to the backend is as follows:
//
// if dynamicConfig is false: upload resources
// if dynamicConfig is true AND firstStart: upload resources
// if dynamicConfig is true AND NOT firstStart: don't upload resources
uploadResources := true
if dynamicConfig == true && firstStart == false {
uploadResources = false
}
if uploadResources {
err = asrv.SetClusterAuthPreference(cfg.AuthPreference)
if err != nil {
return nil, nil, trace.Wrap(err)
}
log.Infof("[INIT] Set Cluster Authentication Preference: %v", cfg.AuthPreference)
if cfg.U2F != nil {
err = asrv.SetUniversalSecondFactor(cfg.U2F)
if err != nil {
return nil, nil, trace.Wrap(err)
}
log.Infof("[INIT] Set Universal Second Factor Settings: %v", cfg.U2F)
}
if cfg.OIDCConnectors != nil && len(cfg.OIDCConnectors) > 0 {
for _, connector := range cfg.OIDCConnectors {
if err := asrv.UpsertOIDCConnector(connector, 0); err != nil {
return nil, nil, trace.Wrap(err)
}
log.Infof("[INIT] Created ODIC Connector: %q", connector.GetName())
}
}
for _, role := range cfg.Roles {
if err := asrv.UpsertRole(role, backend.Forever); err != nil {
return nil, nil, trace.Wrap(err)
}
log.Infof("[INIT] Created Role: %v", role)
}
for i := range cfg.Authorities {
ca := cfg.Authorities[i]
ca, err = services.GetCertAuthorityMarshaler().GenerateCertAuthority(ca)
if err != nil {
return nil, nil, trace.Wrap(err)
}
if err := asrv.Trust.UpsertCertAuthority(ca, backend.Forever); err != nil {
return nil, nil, trace.Wrap(err)
}
log.Infof("[INIT] Created Trusted Certificate Authority: %q, type: %q", ca.GetName(), ca.GetType())
}
for _, tunnel := range cfg.ReverseTunnels {
if err := asrv.UpsertReverseTunnel(tunnel, 0); err != nil {
return nil, nil, trace.Wrap(err)
}
log.Infof("[INIT] Created Reverse Tunnel: %v", tunnel)
}
}
// always create the default namespace
err = asrv.UpsertNamespace(services.NewNamespace(defaults.Namespace))
if err != nil {
return nil, nil, trace.Wrap(err)
}
log.Infof("[INIT] Created Namespace: %q", defaults.Namespace)
// generate a user certificate authority if it doesn't exist
if _, err := asrv.GetCertAuthority(services.CertAuthID{DomainName: cfg.DomainName, Type: services.UserCA}, false); err != nil {
if !trace.IsNotFound(err) {
return nil, nil, trace.Wrap(err)
}
log.Infof("[FIRST START]: Generating user certificate authority (CA)")
priv, pub, err := asrv.GenerateKeyPair("")
if err != nil {
return nil, nil, trace.Wrap(err)
}
userCA := &services.CertAuthorityV2{
Kind: services.KindCertAuthority,
Version: services.V2,
Metadata: services.Metadata{
Name: cfg.DomainName,
Namespace: defaults.Namespace,
},
Spec: services.CertAuthoritySpecV2{
ClusterName: cfg.DomainName,
Type: services.UserCA,
SigningKeys: [][]byte{priv},
CheckingKeys: [][]byte{pub},
},
}
if err := asrv.Trust.UpsertCertAuthority(userCA, backend.Forever); err != nil {
return nil, nil, trace.Wrap(err)
}
}
// generate a host certificate authority if it doesn't exist
if _, err := asrv.GetCertAuthority(services.CertAuthID{DomainName: cfg.DomainName, Type: services.HostCA}, false); err != nil {
if !trace.IsNotFound(err) {
return nil, nil, trace.Wrap(err)
}
log.Infof("[FIRST START]: Generating host certificate authority (CA)")
priv, pub, err := asrv.GenerateKeyPair("")
if err != nil {
return nil, nil, trace.Wrap(err)
}
hostCA := &services.CertAuthorityV2{
Kind: services.KindCertAuthority,
Version: services.V2,
Metadata: services.Metadata{
Name: cfg.DomainName,
Namespace: defaults.Namespace,
},
Spec: services.CertAuthoritySpecV2{
ClusterName: cfg.DomainName,
Type: services.HostCA,
SigningKeys: [][]byte{priv},
CheckingKeys: [][]byte{pub},
},
}
if err := asrv.Trust.UpsertCertAuthority(hostCA, backend.Forever); err != nil {
return nil, nil, trace.Wrap(err)
}
}
if cfg.DeveloperMode {
log.Warn("[INIT] Starting Teleport in developer mode. This is dangerous! Sensitive information will be logged to console and certificates will not be verified. Proceed with caution!")
}
// read host keys from disk or create them if they don't exist
iid := IdentityID{
HostUUID: cfg.HostUUID,
NodeName: cfg.NodeName,
Role: teleport.RoleAdmin,
}
identity, err := initKeys(asrv, cfg.DataDir, iid)
if err != nil {
return nil, nil, trace.Wrap(err)
}
// migrate any legacy resources to new format
err = migrateLegacyResources(cfg, asrv)
if err != nil {
return nil, nil, trace.Wrap(err)
}
return asrv, identity, nil
}
func migrateLegacyResources(cfg InitConfig, asrv *AuthServer) error {
err := migrateUsers(asrv)
if err != nil {
return trace.Wrap(err)
}
err = migrateCertAuthority(asrv)
if err != nil {
return trace.Wrap(err)
}
err = migrateAuthPreference(cfg, asrv)
if err != nil {
return trace.Wrap(err)
}
return nil
}
func migrateUsers(asrv *AuthServer) error {
users, err := asrv.GetUsers()
if err != nil {
return trace.Wrap(err)
}
for i := range users {
user := users[i]
raw, ok := (user.GetRawObject()).(services.UserV1)
if !ok {
continue
}
log.Infof("[MIGRATION] Legacy User: %v", user.GetName())
// create role for user and upsert to backend
role := services.RoleForUser(user)
role.SetLogins(raw.AllowedLogins)
err = asrv.UpsertRole(role, backend.Forever)
if err != nil {
return trace.Wrap(err)
}
// upsert new user to backend
user.AddRole(role.GetName())
if err := asrv.UpsertUser(user); err != nil {
return trace.Wrap(err)
}
}
return nil
}
func migrateCertAuthority(asrv *AuthServer) error {
cas, err := asrv.GetCertAuthorities(services.UserCA, true)
if err != nil {
return trace.Wrap(err)
}
for i := range cas {
ca := cas[i]
raw, ok := (ca.GetRawObject()).(services.CertAuthorityV1)
if !ok {
continue
}
_, err := asrv.GetRole(services.RoleNameForCertAuthority(ca.GetClusterName()))
if err == nil {
continue
}
if !trace.IsNotFound(err) {
return trace.Wrap(err)
}
log.Infof("[MIGRATION] Legacy Certificate Authority: %v", ca.GetName())
// create role for certificate authority and upsert to backend
newCA, role := services.ConvertV1CertAuthority(&raw)
err = asrv.UpsertRole(role, backend.Forever)
if err != nil {
return trace.Wrap(err)
}
// upsert new certificate authority to backend
if err := asrv.UpsertCertAuthority(newCA, 0); err != nil {
return trace.Wrap(err)
}
}
return nil
}
func migrateAuthPreference(cfg InitConfig, asrv *AuthServer) error {
// if no cluster auth preferences exist, upload them from file config
_, err := asrv.GetClusterAuthPreference()
if err != nil {
if trace.IsNotFound(err) {
err = asrv.SetClusterAuthPreference(cfg.AuthPreference)
if err != nil {
return trace.Wrap(err)
}
log.Infof("[MIGRATION] Set Cluster Authentication Preference: %v", cfg.AuthPreference)
} else {
return trace.Wrap(err)
}
}
// if no u2f settings exist, upload from file config
if cfg.U2F != nil {
_, err = asrv.GetUniversalSecondFactor()
if err != nil {
if trace.IsNotFound(err) {
err = asrv.SetUniversalSecondFactor(cfg.U2F)
if err != nil {
return trace.Wrap(err)
}
log.Infof("[MIGRATION] Set Universal Second Factor Settings: %v", cfg.U2F)
} else {
return trace.Wrap(err)
}
}
}
return nil
}
// isFirstStart returns 'true' if the auth server is starting for the 1st time
// on this server.
func isFirstStart(authServer *AuthServer, cfg InitConfig) (bool, error) {
// check if the CA exists?
_, err := authServer.GetCertAuthority(
services.CertAuthID{
DomainName: cfg.DomainName,
Type: services.HostCA,
}, false)
if err != nil {
if !trace.IsNotFound(err) {
return false, trace.Wrap(err)
}
// CA not found? --> first start!
return true, nil
}
return false, nil
}
// initKeys initializes a nodes host certificate. If the certificate does not exist, a request
// is made to the certificate authority to generate a host certificate and it's written to disk.
// If a certificate exists on disk, it is read in and returned.
func initKeys(a *AuthServer, dataDir string, id IdentityID) (*Identity, error) {
kp, cp := keysPath(dataDir, id)
keyExists, err := pathExists(kp)
if err != nil {
return nil, trace.Wrap(err)
}
certExists, err := pathExists(cp)
if err != nil {
return nil, trace.Wrap(err)
}
if !keyExists || !certExists {
packedKeys, err := a.GenerateServerKeys(id.HostUUID, id.NodeName, teleport.Roles{id.Role})
if err != nil {
return nil, trace.Wrap(err)
}
err = writeKeys(dataDir, id, packedKeys.Key, packedKeys.Cert)
if err != nil {
return nil, trace.Wrap(err)
}
}
i, err := ReadIdentity(dataDir, id)
if err != nil {
return nil, trace.Wrap(err)
}
return i, nil
}
// writeKeys saves the key/cert pair for a given domain onto disk. This usually means the
// domain trusts us (signed our public key)
func writeKeys(dataDir string, id IdentityID, key []byte, cert []byte) error {
kp, cp := keysPath(dataDir, id)
log.Debugf("[INIT] Writing keys to disk: Key: %q, Cert: %q", kp, cp)
if err := ioutil.WriteFile(kp, key, 0600); err != nil {
return err
}
if err := ioutil.WriteFile(cp, cert, 0600); err != nil {
return err
}
return nil
}
// Identity is a collection of certificates and signers that represent identity
type Identity struct {
ID IdentityID
KeyBytes []byte
CertBytes []byte
KeySigner ssh.Signer
Cert *ssh.Certificate
AuthorityDomain string
}
// IdentityID is a combination of role, host UUID, and node name.
type IdentityID struct {
Role teleport.Role
HostUUID string
NodeName string
}
// Equals returns true if two identities are equal
func (id *IdentityID) Equals(other IdentityID) bool {
return id.Role == other.Role && id.HostUUID == other.HostUUID
}
// String returns debug friendly representation of this identity
func (id *IdentityID) String() string {
return fmt.Sprintf("Identity(hostuuid=%v, role=%v)", id.HostUUID, id.Role)
}
// ReadIdentityFromKeyPair reads identity from initialized keypair
func ReadIdentityFromKeyPair(keyBytes, certBytes []byte) (*Identity, error) {
if len(keyBytes) == 0 {
return nil, trace.BadParameter("PrivateKey: missing private key")
}
if len(certBytes) == 0 {
return nil, trace.BadParameter("Cert: missing parameter")
}
pubKey, _, _, _, err := ssh.ParseAuthorizedKey(certBytes)
if err != nil {
return nil, trace.BadParameter("failed to parse server certificate: %v", err)
}
cert, ok := pubKey.(*ssh.Certificate)
if !ok {
return nil, trace.BadParameter("expected ssh.Certificate, got %v", pubKey)
}
signer, err := ssh.ParsePrivateKey(keyBytes)
if err != nil {
return nil, trace.BadParameter("failed to parse private key: %v", err)
}
// this signer authenticates using certificate signed by the cert authority
// not only by the public key
certSigner, err := ssh.NewCertSigner(cert, signer)
if err != nil {
return nil, trace.BadParameter("unsupported private key: %v", err)
}
// check principals on certificate
if len(cert.ValidPrincipals) < 1 {
return nil, trace.BadParameter("valid principals: at least one valid principal is required")
}
for _, validPrincipal := range cert.ValidPrincipals {
if validPrincipal == "" {
return nil, trace.BadParameter("valid principal can not be empty: %q", cert.ValidPrincipals)
}
}
// check permissions on certificate
if len(cert.Permissions.Extensions) == 0 {
return nil, trace.BadParameter("extensions: misssing needed extensions for host roles")
}
roleString := cert.Permissions.Extensions[utils.CertExtensionRole]
if roleString == "" {
return nil, trace.BadParameter("misssing cert extension %v", utils.CertExtensionRole)
}
roles, err := teleport.ParseRoles(roleString)
if err != nil {
return nil, trace.Wrap(err)
}
foundRoles := len(roles)
if foundRoles != 1 {
return nil, trace.Errorf("expected one role per certificate. found %d: '%s'",
foundRoles, roles.String())
}
role := roles[0]
authorityDomain := cert.Permissions.Extensions[utils.CertExtensionAuthority]
if authorityDomain == "" {
return nil, trace.BadParameter("misssing cert extension %v", utils.CertExtensionAuthority)
}
return &Identity{
ID: IdentityID{HostUUID: cert.ValidPrincipals[0], Role: role},
AuthorityDomain: authorityDomain,
KeyBytes: keyBytes,
CertBytes: certBytes,
KeySigner: certSigner,
Cert: cert,
}, nil
}
// ReadIdentity reads, parses and returns the given pub/pri key + cert from the
// key storage (dataDir).
func ReadIdentity(dataDir string, id IdentityID) (i *Identity, err error) {
kp, cp := keysPath(dataDir, id)
log.Debugf("[INIT] Reading keys from disk: Key: %q, Cert: %q", kp, cp)
keyBytes, err := utils.ReadPath(kp)
if err != nil {
return nil, trace.Wrap(err)
}
certBytes, err := utils.ReadPath(cp)
if err != nil {
return nil, trace.Wrap(err)
}
return ReadIdentityFromKeyPair(keyBytes, certBytes)
}
// WriteIdentity writes identity keypair to disk
func WriteIdentity(dataDir string, identity *Identity) error {
return trace.Wrap(
writeKeys(dataDir, identity.ID, identity.KeyBytes, identity.CertBytes))
}
// HaveHostKeys checks either the host keys are in place
func HaveHostKeys(dataDir string, id IdentityID) (bool, error) {
kp, cp := keysPath(dataDir, id)
exists, err := pathExists(kp)
if !exists || err != nil {
return exists, err
}
exists, err = pathExists(cp)
if !exists || err != nil {
return exists, err
}
return true, nil
}
// keysPath returns two full file paths: to the host.key and host.cert
func keysPath(dataDir string, id IdentityID) (key string, cert string) {
return filepath.Join(dataDir, fmt.Sprintf("%s.key", strings.ToLower(string(id.Role)))),
filepath.Join(dataDir, fmt.Sprintf("%s.cert", strings.ToLower(string(id.Role))))
}
func pathExists(path string) (bool, error) {
_, err := os.Stat(path)
if err != nil {
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
return true, nil
}