-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
join.go
370 lines (338 loc) · 12.2 KB
/
join.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
/*
Copyright 2022 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"
"crypto/rand"
"encoding/base64"
"fmt"
"net"
"strings"
"github.com/gravitational/trace"
"golang.org/x/exp/slices"
"google.golang.org/grpc/peer"
"github.com/gravitational/teleport/api/client/proto"
"github.com/gravitational/teleport/api/types"
apievents "github.com/gravitational/teleport/api/types/events"
"github.com/gravitational/teleport/lib/authz"
"github.com/gravitational/teleport/lib/defaults"
"github.com/gravitational/teleport/lib/events"
)
// tokenJoinMethod returns the join method of the token with the given tokenName
func (a *Server) tokenJoinMethod(ctx context.Context, tokenName string) types.JoinMethod {
provisionToken, err := a.GetToken(ctx, tokenName)
if err != nil {
// could not find dynamic token, assume static token. If it does not
// exist this will be caught later.
return types.JoinMethodToken
}
return provisionToken.GetJoinMethod()
}
// checkTokenJoinRequestCommon checks all token join rules that are common to
// all join methods, including token existence, token TTL, and allowed roles.
func (a *Server) checkTokenJoinRequestCommon(ctx context.Context, req *types.RegisterUsingTokenRequest) (types.ProvisionToken, error) {
// make sure the token is valid
provisionToken, err := a.ValidateToken(ctx, req.Token)
if err != nil {
log.Warningf("%q can not join the cluster with role %s, token error: %v", req.NodeName, req.Role, err)
msg := "the token is not valid" // default to most generic message
if strings.Contains(err.Error(), TokenExpiredOrNotFound) {
// propagate ExpiredOrNotFound message so that clients can attempt
// assertion-based fallback if appropriate.
msg = TokenExpiredOrNotFound
}
return nil, trace.AccessDenied("%q can not join the cluster with role %q, %s", req.NodeName, req.Role, msg)
}
// instance certs can be requested by any agent that has at least one local service role (e.g. proxy, node, etc).
if req.Role == types.RoleInstance {
hasLocalServiceRole := false
for _, role := range provisionToken.GetRoles() {
if role.IsLocalService() {
hasLocalServiceRole = true
break
}
}
if !hasLocalServiceRole {
msg := fmt.Sprintf("%q [%v] cannot requisition instance certs (token contains no local service roles)", req.NodeName, req.HostID)
log.Warn(msg)
return nil, trace.AccessDenied(msg)
}
}
// make sure the caller is requesting a role allowed by the token
if !provisionToken.GetRoles().Include(req.Role) && req.Role != types.RoleInstance {
msg := fmt.Sprintf("node %q [%v] can not join the cluster, the token does not allow %q role", req.NodeName, req.HostID, req.Role)
log.Warn(msg)
return nil, trace.BadParameter(msg)
}
return provisionToken, nil
}
type joinAttributeSourcer interface {
// JoinAuditAttributes returns a series of attributes that can be inserted into
// audit events related to a specific join.
JoinAuditAttributes() (map[string]interface{}, error)
}
func setRemoteAddrFromContext(ctx context.Context, req *types.RegisterUsingTokenRequest) error {
var addr string
if clientIP, err := authz.ClientSrcAddrFromContext(ctx); err == nil {
addr = clientIP.String()
} else if p, ok := peer.FromContext(ctx); ok {
addr = p.Addr.String()
}
ip, _, err := net.SplitHostPort(addr)
if err != nil {
return trace.Wrap(err)
}
req.RemoteAddr = ip
return nil
}
// RegisterUsingToken returns credentials for a new node to join the Teleport
// cluster using a previously issued token.
//
// A node must also request a specific role (and the role must match one of the roles
// the token was generated for.)
//
// If a token was generated with a TTL, it gets enforced (can't register new
// nodes after TTL expires.)
//
// If the token includes a specific join method, the rules for that join method
// will be checked.
func (a *Server) RegisterUsingToken(ctx context.Context, req *types.RegisterUsingTokenRequest) (*proto.Certs, error) {
log.Infof("Node %q [%v] is trying to join with role: %v.", req.NodeName, req.HostID, req.Role)
if err := req.CheckAndSetDefaults(); err != nil {
return nil, trace.Wrap(err)
}
var joinAttributeSrc joinAttributeSourcer
switch method := a.tokenJoinMethod(ctx, req.Token); method {
case types.JoinMethodEC2:
if err := a.checkEC2JoinRequest(ctx, req); err != nil {
return nil, trace.Wrap(err)
}
case types.JoinMethodIAM, types.JoinMethodAzure:
// IAM and Azure join methods must use gRPC register methods
return nil, trace.AccessDenied("this token is only valid for the %s "+
"join method but the node has connected to the wrong endpoint, make "+
"sure your node is configured to use the %s join method", method, method)
case types.JoinMethodGitHub:
claims, err := a.checkGitHubJoinRequest(ctx, req)
if err != nil {
return nil, trace.Wrap(err)
}
joinAttributeSrc = claims
case types.JoinMethodGitLab:
claims, err := a.checkGitLabJoinRequest(ctx, req)
if err != nil {
return nil, trace.Wrap(err)
}
joinAttributeSrc = claims
case types.JoinMethodCircleCI:
claims, err := a.checkCircleCIJoinRequest(ctx, req)
if err != nil {
return nil, trace.Wrap(err)
}
joinAttributeSrc = claims
case types.JoinMethodKubernetes:
claims, err := a.checkKubernetesJoinRequest(ctx, req)
if err != nil {
return nil, trace.Wrap(err)
}
joinAttributeSrc = claims
case types.JoinMethodGCP:
claims, err := a.checkGCPJoinRequest(ctx, req)
if err != nil {
return nil, trace.Wrap(err)
}
joinAttributeSrc = claims
case types.JoinMethodSpacelift:
claims, err := a.checkSpaceliftJoinRequest(ctx, req)
if err != nil {
return nil, trace.Wrap(err)
}
joinAttributeSrc = claims
case types.JoinMethodToken:
// carry on to common token checking logic
default:
// this is a logic error, all valid join methods should be captured
// above (empty join method will be set to JoinMethodToken by
// CheckAndSetDefaults)
return nil, trace.BadParameter("unrecognized token join method")
}
// perform common token checks
provisionToken, err := a.checkTokenJoinRequestCommon(ctx, req)
if err != nil {
return nil, trace.Wrap(err)
}
// With all elements of the token validated, we can now generate & return
// certificates.
if req.Role == types.RoleBot {
certs, err := a.generateCertsBot(ctx, provisionToken, req, joinAttributeSrc)
return certs, trace.Wrap(err)
}
certs, err := a.generateCerts(ctx, provisionToken, req, joinAttributeSrc)
return certs, trace.Wrap(err)
}
func (a *Server) generateCertsBot(
ctx context.Context,
provisionToken types.ProvisionToken,
req *types.RegisterUsingTokenRequest,
joinAttributeSrc joinAttributeSourcer,
) (*proto.Certs, error) {
// bots use this endpoint but get a user cert
// botResourceName must be set, enforced in CheckAndSetDefaults
botName := provisionToken.GetBotName()
joinMethod := provisionToken.GetJoinMethod()
// Check this is a join method for bots we support.
if !slices.Contains(supportedBotJoinMethods, joinMethod) {
return nil, trace.BadParameter(
"unsupported join method %q for bot", joinMethod,
)
}
// Most join methods produce non-renewable certificates and join must be
// called again to fetch fresh certificates with a longer lifetime. These
// join methods do not delete the token after use.
renewable := false
shouldDeleteToken := false
if joinMethod == types.JoinMethodToken {
// The token join method is special and produces renewable certificates
// but the token is deleted after use.
shouldDeleteToken = true
renewable = true
}
expires := a.GetClock().Now().Add(defaults.DefaultRenewableCertTTL)
if req.Expires != nil {
expires = *req.Expires
}
certs, err := a.generateInitialBotCerts(
ctx, botName, BotResourceName(botName), req.RemoteAddr, req.PublicSSHKey, expires, renewable,
)
if err != nil {
return nil, trace.Wrap(err)
}
if shouldDeleteToken {
// delete ephemeral bot join tokens so they can't be re-used
if err := a.DeleteToken(ctx, provisionToken.GetName()); err != nil {
log.WithError(err).Warnf("Could not delete bot provision token %q after generating certs",
provisionToken.GetSafeName(),
)
}
}
// Emit audit event for bot join.
log.Infof("Bot %q has joined the cluster.", botName)
joinEvent := &apievents.BotJoin{
Metadata: apievents.Metadata{
Type: events.BotJoinEvent,
Code: events.BotJoinCode,
},
Status: apievents.Status{
Success: true,
},
BotName: botName,
Method: string(joinMethod),
TokenName: provisionToken.GetSafeName(),
}
if joinAttributeSrc != nil {
attributes, err := joinAttributeSrc.JoinAuditAttributes()
if err != nil {
log.WithError(err).Warn("Unable to fetch join attributes from join method.")
}
joinEvent.Attributes, err = apievents.EncodeMap(attributes)
if err != nil {
log.WithError(err).Warn("Unable to encode join attributes for audit event.")
}
}
if err := a.emitter.EmitAuditEvent(ctx, joinEvent); err != nil {
log.WithError(err).Warn("Failed to emit bot join event.")
}
return certs, nil
}
func (a *Server) generateCerts(
ctx context.Context,
provisionToken types.ProvisionToken,
req *types.RegisterUsingTokenRequest,
joinAttributeSrc joinAttributeSourcer,
) (*proto.Certs, error) {
if req.Expires != nil {
return nil, trace.BadParameter("'expires' cannot be set on join for non-bot certificates")
}
// instance certs include an additional field that specifies the list of
// all services authorized by the token.
var systemRoles []types.SystemRole
if req.Role == types.RoleInstance {
for _, r := range provisionToken.GetRoles() {
if r.IsLocalService() {
systemRoles = append(systemRoles, r)
} else {
log.Warnf("Omitting non-service system role from instance cert: %q", r)
}
}
}
// generate and return host certificate and keys
certs, err := a.GenerateHostCerts(ctx,
&proto.HostCertsRequest{
HostID: req.HostID,
NodeName: req.NodeName,
Role: req.Role,
AdditionalPrincipals: req.AdditionalPrincipals,
PublicTLSKey: req.PublicTLSKey,
PublicSSHKey: req.PublicSSHKey,
RemoteAddr: req.RemoteAddr,
DNSNames: req.DNSNames,
SystemRoles: systemRoles,
})
if err != nil {
return nil, trace.Wrap(err)
}
// Emit audit event
if req.Role == types.RoleInstance {
log.Infof("Instance %q [%v] has joined the cluster. role=%s, systemRoles=%+v", req.NodeName, req.HostID, req.Role, systemRoles)
} else {
log.Infof("Instance %q [%v] has joined the cluster. role=%s", req.NodeName, req.HostID, req.Role)
}
joinEvent := &apievents.InstanceJoin{
Metadata: apievents.Metadata{
Type: events.InstanceJoinEvent,
Code: events.InstanceJoinCode,
},
Status: apievents.Status{
Success: true,
},
NodeName: req.NodeName,
Role: string(req.Role),
Method: string(provisionToken.GetJoinMethod()),
TokenName: provisionToken.GetSafeName(),
TokenExpires: provisionToken.Expiry(),
HostID: req.HostID,
}
if joinAttributeSrc != nil {
attributes, err := joinAttributeSrc.JoinAuditAttributes()
if err != nil {
log.WithError(err).Warn("Unable to fetch join attributes from join method.")
}
joinEvent.Attributes, err = apievents.EncodeMap(attributes)
if err != nil {
log.WithError(err).Warn("Unable to encode join attributes for audit event.")
}
}
if err := a.emitter.EmitAuditEvent(ctx, joinEvent); err != nil {
log.WithError(err).Warn("Failed to emit instance join event.")
}
return certs, nil
}
func generateChallenge(encoding *base64.Encoding, length int) (string, error) {
// read crypto-random bytes to generate the challenge
challengeRawBytes := make([]byte, length)
if _, err := rand.Read(challengeRawBytes); err != nil {
return "", trace.Wrap(err)
}
// encode the challenge to base64 so it can be sent over HTTP
return encoding.EncodeToString(challengeRawBytes), nil
}