-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathservice_spiffe_svid_output.go
408 lines (366 loc) · 11.3 KB
/
service_spiffe_svid_output.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
/*
* Teleport
* Copyright (C) 2024 Gravitational, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package tbot
import (
"cmp"
"context"
"crypto"
"crypto/x509"
"encoding/pem"
"fmt"
"log/slog"
"math"
"time"
"github.com/gravitational/trace"
"google.golang.org/protobuf/types/known/durationpb"
machineidv1pb "github.com/gravitational/teleport/api/gen/proto/go/teleport/machineid/v1"
"github.com/gravitational/teleport/api/utils/retryutils"
"github.com/gravitational/teleport/lib/auth/authclient"
"github.com/gravitational/teleport/lib/cryptosuites"
"github.com/gravitational/teleport/lib/reversetunnelclient"
"github.com/gravitational/teleport/lib/tbot/config"
"github.com/gravitational/teleport/lib/tbot/identity"
"github.com/gravitational/teleport/lib/tbot/workloadidentity"
)
const (
// pemPrivateKey is the PEM block type for a PKCS 8 encoded private key.
pemPrivateKey = "PRIVATE KEY"
// pemCertificate is the PEM block type for a DER encoded certificate.
pemCertificate = "CERTIFICATE"
)
// SPIFFESVIDOutputService is a service that generates and writes X509 SPIFFE
// SVIDs to a destination. It produces an output compatible with the
// `spiffe-helper` tool.
type SPIFFESVIDOutputService struct {
botAuthClient *authclient.Client
botCfg *config.BotConfig
cfg *config.SPIFFESVIDOutput
getBotIdentity getBotIdentityFn
log *slog.Logger
resolver reversetunnelclient.Resolver
// trustBundleCache is the cache of trust bundles. It only needs to be
// provided when running in daemon mode.
trustBundleCache *workloadidentity.TrustBundleCache
}
func (s *SPIFFESVIDOutputService) String() string {
return fmt.Sprintf("spiffe-svid-output (%s)", s.cfg.Destination.String())
}
func (s *SPIFFESVIDOutputService) OneShot(ctx context.Context) error {
res, privateKey, jwtSVIDs, err := s.requestSVID(ctx)
if err != nil {
return trace.Wrap(err, "requesting SVID")
}
bundleSet, err := workloadidentity.FetchInitialBundleSet(
ctx,
s.log,
s.botAuthClient.SPIFFEFederationServiceClient(),
s.botAuthClient.TrustClient(),
s.cfg.IncludeFederatedTrustBundles,
s.getBotIdentity().ClusterName,
)
if err != nil {
return trace.Wrap(err, "fetching trust bundle set")
}
return s.render(ctx, bundleSet, res, privateKey, jwtSVIDs)
}
func (s *SPIFFESVIDOutputService) Run(ctx context.Context) error {
bundleSet, err := s.trustBundleCache.GetBundleSet(ctx)
if err != nil {
return trace.Wrap(err, "getting trust bundle set")
}
jitter := retryutils.DefaultJitter
var res *machineidv1pb.SignX509SVIDsResponse
var privateKey crypto.Signer
var jwtSVIDs map[string]string
var failures int
firstRun := make(chan struct{}, 1)
firstRun <- struct{}{}
for {
var retryAfter <-chan time.Time
if failures > 0 {
backoffTime := time.Second * time.Duration(math.Pow(2, float64(failures-1)))
if backoffTime > time.Minute {
backoffTime = time.Minute
}
backoffTime = jitter(backoffTime)
s.log.WarnContext(
ctx,
"Last attempt to generate output failed, will retry",
"retry_after", backoffTime,
"failures", failures,
)
retryAfter = time.After(time.Duration(failures) * time.Second)
}
select {
case <-ctx.Done():
return nil
case <-retryAfter:
s.log.InfoContext(ctx, "Retrying")
case <-bundleSet.Stale():
newBundleSet, err := s.trustBundleCache.GetBundleSet(ctx)
if err != nil {
return trace.Wrap(err, "getting trust bundle set")
}
s.log.InfoContext(ctx, "Trust bundle set has been updated")
if !newBundleSet.Local.Equal(bundleSet.Local) {
// If the local trust domain CA has changed, we need to reissue
// the SVID.
res = nil
privateKey = nil
}
bundleSet = newBundleSet
case <-time.After(cmp.Or(s.cfg.CredentialLifetime, s.botCfg.CredentialLifetime).RenewalInterval):
s.log.InfoContext(ctx, "Renewal interval reached, renewing SVIDs")
res = nil
privateKey = nil
case <-firstRun:
}
if res == nil || privateKey == nil {
var err error
res, privateKey, jwtSVIDs, err = s.requestSVID(ctx)
if err != nil {
s.log.ErrorContext(ctx, "Failed to request SVID", "error", err)
failures++
continue
}
}
if err := s.render(ctx, bundleSet, res, privateKey, jwtSVIDs); err != nil {
s.log.ErrorContext(ctx, "Failed to render output", "error", err)
failures++
continue
}
failures = 0
}
}
func (s *SPIFFESVIDOutputService) requestSVID(
ctx context.Context,
) (
*machineidv1pb.SignX509SVIDsResponse,
crypto.Signer,
map[string]string,
error,
) {
ctx, span := tracer.Start(
ctx,
"SPIFFESVIDOutputService/requestSVID",
)
defer span.End()
roles, err := fetchDefaultRoles(ctx, s.botAuthClient, s.getBotIdentity())
if err != nil {
return nil, nil, nil, trace.Wrap(err, "fetching roles")
}
effectiveLifetime := cmp.Or(s.cfg.CredentialLifetime, s.botCfg.CredentialLifetime)
id, err := generateIdentity(
ctx,
s.botAuthClient,
s.getBotIdentity(),
roles,
effectiveLifetime.TTL,
nil,
)
if err != nil {
return nil, nil, nil, trace.Wrap(err, "generating identity")
}
warnOnEarlyExpiration(ctx, s.log.With("output", s), id, effectiveLifetime)
// create a client that uses the impersonated identity, so that when we
// fetch information, we can ensure access rights are enforced.
facade := identity.NewFacade(s.botCfg.FIPS, s.botCfg.Insecure, id)
impersonatedClient, err := clientForFacade(ctx, s.log, s.botCfg, facade, s.resolver)
if err != nil {
return nil, nil, nil, trace.Wrap(err)
}
defer impersonatedClient.Close()
res, privateKey, err := generateSVID(
ctx,
impersonatedClient,
[]config.SVIDRequest{s.cfg.SVID},
cmp.Or(s.cfg.CredentialLifetime, s.botCfg.CredentialLifetime).TTL,
)
if err != nil {
return nil, nil, nil, trace.Wrap(err, "generating X509 SVID")
}
jwtSvids, err := generateJWTSVIDs(
ctx,
impersonatedClient,
s.cfg.SVID,
s.cfg.JWTs,
cmp.Or(s.cfg.CredentialLifetime, s.botCfg.CredentialLifetime).TTL)
if err != nil {
return nil, nil, nil, trace.Wrap(err, "generating JWT SVIDs")
}
return res, privateKey, jwtSvids, nil
}
func (s *SPIFFESVIDOutputService) render(
ctx context.Context,
bundleSet *workloadidentity.BundleSet,
res *machineidv1pb.SignX509SVIDsResponse,
privateKey crypto.Signer,
jwtSVIDs map[string]string,
) error {
ctx, span := tracer.Start(
ctx,
"SPIFFESVIDOutputService/render",
)
defer span.End()
s.log.InfoContext(ctx, "Rendering output")
// Check the ACLs. We can't fix them, but we can warn if they're
// misconfigured. We'll need to precompute a list of keys to check.
// Note: This may only log a warning, depending on configuration.
if err := s.cfg.Destination.Verify(identity.ListKeys(identity.DestinationKinds()...)); err != nil {
return trace.Wrap(err)
}
// Ensure this destination is also writable. This is a hard fail if
// ACLs are misconfigured, regardless of configuration.
if err := identity.VerifyWrite(ctx, s.cfg.Destination); err != nil {
return trace.Wrap(err, "verifying destination")
}
privBytes, err := x509.MarshalPKCS8PrivateKey(privateKey)
if err != nil {
return trace.Wrap(err)
}
privPEM := pem.EncodeToMemory(&pem.Block{
Type: pemPrivateKey,
Bytes: privBytes,
})
if len(res.Svids) != 1 {
return trace.BadParameter("expected 1 SVID, got %d", len(res.Svids))
}
svid := res.Svids[0]
if err := s.cfg.Destination.Write(ctx, config.SVIDKeyPEMPath, privPEM); err != nil {
return trace.Wrap(err, "writing svid key")
}
certPEM := pem.EncodeToMemory(&pem.Block{
Type: pemCertificate,
Bytes: svid.Certificate,
})
if err := s.cfg.Destination.Write(ctx, config.SVIDPEMPath, certPEM); err != nil {
return trace.Wrap(err, "writing svid certificate")
}
trustBundleBytes, err := bundleSet.Local.X509Bundle().Marshal()
if err != nil {
return trace.Wrap(err, "marshaling local trust bundle")
}
if s.cfg.IncludeFederatedTrustBundles {
for _, federatedBundle := range bundleSet.Federated {
federatedBundleBytes, err := federatedBundle.X509Bundle().Marshal()
if err != nil {
return trace.Wrap(err, "marshaling federated trust bundle (%s)", federatedBundle.TrustDomain().Name())
}
trustBundleBytes = append(trustBundleBytes, federatedBundleBytes...)
}
}
if err := s.cfg.Destination.Write(
ctx, config.SVIDTrustBundlePEMPath, trustBundleBytes,
); err != nil {
return trace.Wrap(err, "writing svid trust bundle")
}
for fileName, jwt := range jwtSVIDs {
if err := s.cfg.Destination.Write(ctx, fileName, []byte(jwt)); err != nil {
return trace.Wrap(err, "writing JWT SVID")
}
}
return nil
}
func generateJWTSVIDs(
ctx context.Context,
clt *authclient.Client,
svid config.SVIDRequest,
reqs []config.JWTSVID,
ttl time.Duration,
) (map[string]string, error) {
ctx, span := tracer.Start(
ctx,
"generateJWTSVIDs",
)
defer span.End()
requestedAudiences := map[string]bool{}
for _, jwt := range reqs {
requestedAudiences[jwt.Audience] = true
}
jwtReqs := make([]*machineidv1pb.JWTSVIDRequest, 0, len(requestedAudiences))
for audience := range requestedAudiences {
jwtReqs = append(jwtReqs, &machineidv1pb.JWTSVIDRequest{
Audiences: []string{audience},
Ttl: durationpb.New(ttl),
SpiffeIdPath: svid.Path,
})
}
if len(jwtReqs) == 0 {
return nil, nil
}
jwtRes, err := clt.WorkloadIdentityServiceClient().SignJWTSVIDs(ctx, &machineidv1pb.SignJWTSVIDsRequest{
Svids: jwtReqs,
})
if err != nil {
return nil, trace.Wrap(err, "requesting JWT SVIDs")
}
jwtFiles := map[string]string{}
for _, req := range reqs {
for _, jwtSVID := range jwtRes.Svids {
if len(jwtSVID.Audiences) == 1 && jwtSVID.Audiences[0] == req.Audience {
jwtFiles[req.FileName] = jwtSVID.Jwt
break
}
}
}
return jwtFiles, nil
}
// generateSVID generates the pre-requisites and makes a SVID generation RPC
// call.
func generateSVID(
ctx context.Context,
clt *authclient.Client,
reqs []config.SVIDRequest,
ttl time.Duration,
) (*machineidv1pb.SignX509SVIDsResponse, crypto.Signer, error) {
ctx, span := tracer.Start(
ctx,
"generateSVID",
)
defer span.End()
privateKey, err := cryptosuites.GenerateKey(ctx,
cryptosuites.GetCurrentSuiteFromAuthPreference(clt),
cryptosuites.BotSVID)
if err != nil {
return nil, nil, trace.Wrap(err)
}
pubBytes, err := x509.MarshalPKIXPublicKey(privateKey.Public())
if err != nil {
return nil, nil, trace.Wrap(err)
}
svids := make([]*machineidv1pb.SVIDRequest, 0, len(reqs))
for _, req := range reqs {
svids = append(svids, &machineidv1pb.SVIDRequest{
PublicKey: pubBytes,
SpiffeIdPath: req.Path,
DnsSans: req.SANS.DNS,
IpSans: req.SANS.IP,
Hint: req.Hint,
Ttl: durationpb.New(ttl),
})
}
res, err := clt.WorkloadIdentityServiceClient().SignX509SVIDs(ctx,
&machineidv1pb.SignX509SVIDsRequest{
Svids: svids,
},
)
if err != nil {
return nil, nil, trace.Wrap(err)
}
return res, privateKey, nil
}