-
Notifications
You must be signed in to change notification settings - Fork 0
/
register.go
141 lines (127 loc) · 4.68 KB
/
register.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
/*
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 (
"crypto/x509"
"fmt"
"io/ioutil"
"path/filepath"
"strings"
"github.com/gravitational/teleport"
"github.com/gravitational/teleport/lib/defaults"
"github.com/gravitational/teleport/lib/tlsca"
"github.com/gravitational/teleport/lib/utils"
"github.com/gravitational/trace"
)
// LocalRegister is used to generate host keys when a node or proxy is running within the same process
// as the auth server. This method does not need to use provisioning tokens.
func LocalRegister(id IdentityID, authServer *AuthServer, additionalPrincipals []string) (*Identity, error) {
keys, err := authServer.GenerateServerKeys(GenerateServerKeysRequest{
HostID: id.HostUUID,
NodeName: id.NodeName,
Roles: teleport.Roles{id.Role},
AdditionalPrincipals: additionalPrincipals,
})
if err != nil {
return nil, trace.Wrap(err)
}
return ReadIdentityFromKeyPair(keys.Key, keys.Cert, keys.TLSCert, keys.TLSCACerts)
}
// Register is used to generate host keys when a node or proxy are running on different hosts
// than the auth server. This method requires provisioning tokens to prove a valid auth server
// was used to issue the joining request.
func Register(dataDir, token string, id IdentityID, servers []utils.NetAddr, additionalPrincipals []string) (*Identity, error) {
tok, err := readToken(token)
if err != nil {
return nil, trace.Wrap(err)
}
tlsConfig := utils.TLSConfig()
certPath := filepath.Join(dataDir, defaults.CACertFile)
certBytes, err := utils.ReadPath(certPath)
if err != nil {
// Only support secure cluster joins in the next releases
if !trace.IsNotFound(err) {
return nil, trace.Wrap(err)
}
message := fmt.Sprintf(`Your configuration is insecure! Registering without TLS certificate authority, to fix this warning add ca.cert to %v, you can get ca.cert using 'tctl auth export --type=tls > ca.cert'`, dataDir)
log.Warning(message)
tlsConfig.InsecureSkipVerify = true
} else {
cert, err := tlsca.ParseCertificatePEM(certBytes)
if err != nil {
return nil, trace.Wrap(err, "failed to parse certificate at %v", certPath)
}
log.Infof("Joining remote cluster %v.", cert.Subject.CommonName)
certPool := x509.NewCertPool()
certPool.AddCert(cert)
tlsConfig.RootCAs = certPool
}
client, err := NewTLSClient(servers, tlsConfig)
if err != nil {
return nil, trace.Wrap(err)
}
defer client.Close()
// get the host certificate and keys
keys, err := client.RegisterUsingToken(RegisterUsingTokenRequest{
Token: tok,
HostID: id.HostUUID,
NodeName: id.NodeName,
Role: id.Role,
AdditionalPrincipals: additionalPrincipals,
})
if err != nil {
return nil, trace.Wrap(err)
}
return ReadIdentityFromKeyPair(keys.Key, keys.Cert, keys.TLSCert, keys.TLSCACerts)
}
// ReRegister renews the certificates and private keys based on the client's existing identity.
func ReRegister(clt ClientI, id IdentityID, additionalPrincipals []string) (*Identity, error) {
hostID, err := id.HostID()
if err != nil {
return nil, trace.Wrap(err)
}
keys, err := clt.GenerateServerKeys(GenerateServerKeysRequest{
HostID: hostID,
NodeName: id.NodeName,
Roles: teleport.Roles{id.Role},
AdditionalPrincipals: additionalPrincipals,
})
if err != nil {
return nil, trace.Wrap(err)
}
return ReadIdentityFromKeyPair(keys.Key, keys.Cert, keys.TLSCert, keys.TLSCACerts)
}
func readToken(token string) (string, error) {
if !strings.HasPrefix(token, "/") {
return token, nil
}
// treat it as a file
out, err := ioutil.ReadFile(token)
if err != nil {
return "", nil
}
// trim newlines as tokens in files tend to have newlines
return strings.TrimSpace(string(out)), nil
}
// PackedKeys is a collection of private key, SSH host certificate
// and TLS certificate and certificate authority issued the certificate
type PackedKeys struct {
// Key is a private key
Key []byte `json:"key"`
// Cert is an SSH host cert
Cert []byte `json:"cert"`
// TLSCert is an X509 certificate
TLSCert []byte `json:"tls_cert"`
// TLSCACerts is a list of certificate authorities
TLSCACerts [][]byte `json:"tls_ca_certs"`
}