forked from spiffe/spire
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanager.go
1084 lines (916 loc) · 31.2 KB
/
manager.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
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package manager
import (
"bytes"
"context"
"crypto"
"crypto/rand"
"crypto/x509"
"errors"
"fmt"
"sync"
"time"
"github.com/andres-erbsen/clock"
"github.com/sirupsen/logrus"
"github.com/spiffe/go-spiffe/v2/spiffeid"
"github.com/spiffe/spire/pkg/common/backoff"
"github.com/spiffe/spire/pkg/common/coretypes/x509certificate"
"github.com/spiffe/spire/pkg/common/telemetry"
telemetry_server "github.com/spiffe/spire/pkg/common/telemetry/server"
"github.com/spiffe/spire/pkg/common/x509util"
"github.com/spiffe/spire/pkg/server/ca"
"github.com/spiffe/spire/pkg/server/catalog"
"github.com/spiffe/spire/pkg/server/credtemplate"
"github.com/spiffe/spire/pkg/server/credvalidator"
"github.com/spiffe/spire/pkg/server/datastore"
"github.com/spiffe/spire/pkg/server/plugin/keymanager"
"github.com/spiffe/spire/pkg/server/plugin/notifier"
"github.com/spiffe/spire/proto/private/server/journal"
"github.com/spiffe/spire/proto/spire/common"
"github.com/zeebo/errs"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
const (
publishJWKTimeout = 5 * time.Second
safetyThresholdBundle = 24 * time.Hour
safetyThresholdCAJournals = time.Hour * 24 * 14 // Two weeks
thirtyDays = 30 * 24 * time.Hour
preparationThresholdCap = thirtyDays
preparationThresholdDivisor = 2
sevenDays = 7 * 24 * time.Hour
activationThresholdCap = sevenDays
activationThresholdDivisor = 6
taintBackoffInterval = 5 * time.Second
taintBackoffMaxElapsedTime = 1 * time.Minute
)
type ManagedCA interface {
SetX509CA(*ca.X509CA)
SetJWTKey(*ca.JWTKey)
NotifyTaintedX509Authorities([]*x509.Certificate)
}
type JwtKeyPublisher interface {
PublishJWTKey(ctx context.Context, jwtKey *common.PublicKey) ([]*common.PublicKey, error)
}
type AuthorityManager interface {
GetCurrentJWTKeySlot() Slot
GetNextJWTKeySlot() Slot
PrepareJWTKey(ctx context.Context) error
RotateJWTKey(ctx context.Context)
GetCurrentX509CASlot() Slot
GetNextX509CASlot() Slot
PrepareX509CA(ctx context.Context) error
RotateX509CA(ctx context.Context)
IsUpstreamAuthority() bool
PublishJWTKey(ctx context.Context, jwtKey *common.PublicKey) ([]*common.PublicKey, error)
NotifyTaintedX509Authority(ctx context.Context, authorityID string) error
}
type Config struct {
CredBuilder *credtemplate.Builder
CredValidator *credvalidator.Validator
CA ManagedCA
Catalog catalog.Catalog
TrustDomain spiffeid.TrustDomain
X509CAKeyType keymanager.KeyType
JWTKeyType keymanager.KeyType
Dir string
Log logrus.FieldLogger
Metrics telemetry.Metrics
Clock clock.Clock
}
type Manager struct {
c Config
caTTL time.Duration
bundleUpdatedCh chan struct{}
taintedUpstreamAuthoritiesCh chan []*x509.Certificate
upstreamClient *ca.UpstreamClient
upstreamPluginName string
currentX509CA *x509CASlot
nextX509CA *x509CASlot
x509CAMutex sync.RWMutex
currentJWTKey *jwtKeySlot
nextJWTKey *jwtKeySlot
jwtKeyMutex sync.RWMutex
journal *Journal
// Used to log a warning only once when the UpstreamAuthority does not support JWT-SVIDs.
jwtUnimplementedWarnOnce sync.Once
// Used for testing backoff, must not be set in regular code
triggerBackOffCh chan error
}
func NewManager(ctx context.Context, c Config) (*Manager, error) {
if c.Clock == nil {
c.Clock = clock.New()
}
m := &Manager{
c: c,
caTTL: c.CredBuilder.Config().X509CATTL,
bundleUpdatedCh: make(chan struct{}, 1),
taintedUpstreamAuthoritiesCh: make(chan []*x509.Certificate, 1),
}
if upstreamAuthority, ok := c.Catalog.GetUpstreamAuthority(); ok {
m.upstreamClient = ca.NewUpstreamClient(ca.UpstreamClientConfig{
UpstreamAuthority: upstreamAuthority,
BundleUpdater: &bundleUpdater{
log: c.Log,
trustDomainID: c.TrustDomain.IDString(),
ds: c.Catalog.GetDataStore(),
updated: m.bundleUpdated,
upstreamAuthoritiesTainted: m.notifyUpstreamAuthoritiesTainted,
processedTaintedAuthorities: map[string]struct{}{},
},
})
m.upstreamPluginName = upstreamAuthority.Name()
}
loader := &SlotLoader{
TrustDomain: c.TrustDomain,
Log: c.Log,
Dir: c.Dir,
Catalog: c.Catalog,
UpstreamClient: m.upstreamClient,
}
journal, slots, err := loader.load(ctx)
if err != nil {
return nil, err
}
now := m.c.Clock.Now()
m.journal = journal
if currentX509CA, ok := slots[CurrentX509CASlot]; ok {
m.currentX509CA = currentX509CA.(*x509CASlot)
if !currentX509CA.IsEmpty() && !currentX509CA.ShouldActivateNext(now) {
// activate the X509CA immediately if it is set and not within
// activation time of the next X509CA.
m.activateX509CA(ctx)
}
}
if nextX509CA, ok := slots[NextX509CASlot]; ok {
m.nextX509CA = nextX509CA.(*x509CASlot)
}
if currentJWTKey, ok := slots[CurrentJWTKeySlot]; ok {
m.currentJWTKey = currentJWTKey.(*jwtKeySlot)
// TODO: Activation on journal depends on dates, it will need to be
// refactored to allow to set a status, because when forcing a rotation,
// we are no longer able to depend on a date.
if !currentJWTKey.IsEmpty() && !currentJWTKey.ShouldActivateNext(now) {
// activate the JWT key immediately if it is set and not within
// activation time of the next JWT key.
m.activateJWTKey(ctx)
}
}
if nextJWTKey, ok := slots[NextJWTKeySlot]; ok {
m.nextJWTKey = nextJWTKey.(*jwtKeySlot)
}
return m, nil
}
func (m *Manager) Close() {
if m.upstreamClient != nil {
_ = m.upstreamClient.Close()
}
}
func (m *Manager) NotifyTaintedX509Authority(ctx context.Context, authorityID string) error {
taintedAuthority, err := m.fetchRootCAByAuthorityID(ctx, authorityID)
if err != nil {
return err
}
m.c.CA.NotifyTaintedX509Authorities([]*x509.Certificate{taintedAuthority})
return nil
}
func (m *Manager) GetCurrentX509CASlot() Slot {
m.x509CAMutex.RLock()
defer m.x509CAMutex.RUnlock()
return m.currentX509CA
}
func (m *Manager) GetNextX509CASlot() Slot {
m.x509CAMutex.RLock()
defer m.x509CAMutex.RUnlock()
return m.nextX509CA
}
func (m *Manager) PrepareX509CA(ctx context.Context) (err error) {
counter := telemetry_server.StartServerCAManagerPrepareX509CACall(m.c.Metrics)
defer counter.Done(&err)
m.x509CAMutex.Lock()
defer m.x509CAMutex.Unlock()
// If current is not empty, prepare the next.
// If the journal has been started, we will be preparing on next.
// This is only needed when the journal has not been started.
slot := m.currentX509CA
if !slot.IsEmpty() {
slot = m.nextX509CA
}
log := m.c.Log.WithField(telemetry.Slot, slot.id)
log.Debug("Preparing X509 CA")
slot.Reset()
now := m.c.Clock.Now()
km := m.c.Catalog.GetKeyManager()
signer, err := km.GenerateKey(ctx, slot.KmKeyID(), m.c.X509CAKeyType)
if err != nil {
return err
}
var x509CA *ca.X509CA
if m.upstreamClient != nil {
x509CA, err = m.upstreamSignX509CA(ctx, signer)
if err != nil {
return err
}
} else {
x509CA, err = m.selfSignX509CA(ctx, signer)
if err != nil {
return err
}
}
slot.issuedAt = now
slot.x509CA = x509CA
slot.status = journal.Status_PREPARED
// Set key from new CA, to be able to get it after
// slot moved to old state
slot.authorityID = x509util.SubjectKeyIDToString(x509CA.Certificate.SubjectKeyId)
slot.upstreamAuthorityID = x509util.SubjectKeyIDToString(x509CA.Certificate.AuthorityKeyId)
slot.publicKey = slot.x509CA.Certificate.PublicKey
slot.notAfter = slot.x509CA.Certificate.NotAfter
if err := m.journal.AppendX509CA(ctx, slot.id, slot.issuedAt, slot.x509CA); err != nil {
log.WithError(err).Error("Unable to append X509 CA to journal")
}
m.c.Log.WithFields(logrus.Fields{
telemetry.Slot: slot.id,
telemetry.IssuedAt: slot.issuedAt,
telemetry.Expiration: slot.x509CA.Certificate.NotAfter,
telemetry.SelfSigned: m.upstreamClient == nil,
telemetry.LocalAuthorityID: slot.authorityID,
telemetry.UpstreamAuthorityID: slot.upstreamAuthorityID,
}).Info("X509 CA prepared")
return nil
}
func (m *Manager) IsUpstreamAuthority() bool {
return m.upstreamClient != nil
}
func (m *Manager) ActivateX509CA(ctx context.Context) {
m.x509CAMutex.RLock()
defer m.x509CAMutex.RUnlock()
m.activateX509CA(ctx)
}
func (m *Manager) RotateX509CA(ctx context.Context) {
m.x509CAMutex.Lock()
defer m.x509CAMutex.Unlock()
m.currentX509CA, m.nextX509CA = m.nextX509CA, m.currentX509CA
m.nextX509CA.Reset()
if err := m.journal.UpdateX509CAStatus(ctx, m.nextX509CA.AuthorityID(), journal.Status_OLD); err != nil {
m.c.Log.WithError(err).Error("Failed to update status on X509CA journal entry")
}
m.activateX509CA(ctx)
}
func (m *Manager) GetCurrentJWTKeySlot() Slot {
m.jwtKeyMutex.RLock()
defer m.jwtKeyMutex.RUnlock()
return m.currentJWTKey
}
func (m *Manager) GetNextJWTKeySlot() Slot {
m.jwtKeyMutex.RLock()
defer m.jwtKeyMutex.RUnlock()
return m.nextJWTKey
}
func (m *Manager) PrepareJWTKey(ctx context.Context) (err error) {
counter := telemetry_server.StartServerCAManagerPrepareJWTKeyCall(m.c.Metrics)
defer counter.Done(&err)
m.jwtKeyMutex.Lock()
defer m.jwtKeyMutex.Unlock()
// If current slot is not empty, use next to prepare
slot := m.currentJWTKey
if !slot.IsEmpty() {
slot = m.nextJWTKey
}
log := m.c.Log.WithField(telemetry.Slot, slot.id)
log.Debug("Preparing JWT key")
slot.Reset()
now := m.c.Clock.Now()
notAfter := now.Add(m.caTTL)
km := m.c.Catalog.GetKeyManager()
signer, err := km.GenerateKey(ctx, slot.KmKeyID(), m.c.JWTKeyType)
if err != nil {
return err
}
jwtKey, err := newJWTKey(signer, notAfter)
if err != nil {
return err
}
publicKey, err := publicKeyFromJWTKey(jwtKey)
if err != nil {
return err
}
if _, err := m.PublishJWTKey(ctx, publicKey); err != nil {
return err
}
slot.issuedAt = now
slot.jwtKey = jwtKey
slot.status = journal.Status_PREPARED
slot.authorityID = jwtKey.Kid
slot.notAfter = jwtKey.NotAfter
if err := m.journal.AppendJWTKey(ctx, slot.id, slot.issuedAt, slot.jwtKey); err != nil {
log.WithError(err).Error("Unable to append JWT key to journal")
}
m.c.Log.WithFields(logrus.Fields{
telemetry.Slot: slot.id,
telemetry.IssuedAt: slot.issuedAt,
telemetry.Expiration: slot.jwtKey.NotAfter,
telemetry.LocalAuthorityID: slot.authorityID,
}).Info("JWT key prepared")
return nil
}
func (m *Manager) ActivateJWTKey(ctx context.Context) {
m.jwtKeyMutex.RLock()
defer m.jwtKeyMutex.RUnlock()
m.activateJWTKey(ctx)
}
func (m *Manager) RotateJWTKey(ctx context.Context) {
m.jwtKeyMutex.Lock()
defer m.jwtKeyMutex.Unlock()
m.currentJWTKey, m.nextJWTKey = m.nextJWTKey, m.currentJWTKey
m.nextJWTKey.Reset()
if err := m.journal.UpdateJWTKeyStatus(ctx, m.nextJWTKey.AuthorityID(), journal.Status_OLD); err != nil {
m.c.Log.WithError(err).Error("Failed to update status on JWTKey journal entry")
}
m.activateJWTKey(ctx)
}
// PublishJWTKey publishes the passed JWK to the upstream server using the configured
// UpstreamAuthority plugin, then appends to the bundle the JWKs returned by the upstream server,
// and finally it returns the updated list of JWT keys contained in the bundle.
//
// The following cases may arise when calling this function:
//
// - The UpstreamAuthority plugin doesn't implement PublishJWTKey, in which case we receive an
// Unimplemented error from the upstream server, and hence we log a one time warning about this,
// append the passed JWK to the bundle, and return the updated list of JWT keys.
//
// - The UpstreamAuthority plugin returned an error, then we return the error.
//
// - There is no UpstreamAuthority plugin configured, then assumes we are the root server and
// just appends the passed JWK to the bundle and returns the updated list of JWT keys.
func (m *Manager) PublishJWTKey(ctx context.Context, jwtKey *common.PublicKey) ([]*common.PublicKey, error) {
if m.upstreamClient != nil {
publishCtx, cancel := context.WithTimeout(ctx, publishJWKTimeout)
defer cancel()
upstreamJWTKeys, err := m.upstreamClient.PublishJWTKey(publishCtx, jwtKey)
switch {
case status.Code(err) == codes.Unimplemented:
// JWT Key publishing is not supported by the upstream plugin.
// Issue a one-time warning and then fall through to the
// appendBundle call below as if an upstream client was not
// configured so the JWT key gets pushed into the local bundle.
m.jwtUnimplementedWarnOnce.Do(func() {
m.c.Log.WithField("plugin_name", m.upstreamPluginName).Warn("UpstreamAuthority plugin does not support JWT-SVIDs. Workloads managed " +
"by this server may have trouble communicating with workloads outside " +
"this cluster when using JWT-SVIDs.")
})
case err != nil:
return nil, err
default:
return upstreamJWTKeys, nil
}
}
bundle, err := m.appendBundle(ctx, nil, []*common.PublicKey{jwtKey})
if err != nil {
return nil, err
}
return bundle.JwtSigningKeys, nil
}
func (m *Manager) PruneBundle(ctx context.Context) (err error) {
counter := telemetry_server.StartCAManagerPruneBundleCall(m.c.Metrics)
defer counter.Done(&err)
ds := m.c.Catalog.GetDataStore()
expiresBefore := m.c.Clock.Now().Add(-safetyThresholdBundle)
changed, err := ds.PruneBundle(ctx, m.c.TrustDomain.IDString(), expiresBefore)
if err != nil {
return fmt.Errorf("unable to prune bundle: %w", err)
}
if changed {
telemetry_server.IncrManagerPrunedBundleCounter(m.c.Metrics)
m.c.Log.Debug("Expired certificates were successfully pruned from bundle")
m.bundleUpdated()
}
return nil
}
func (m *Manager) PruneCAJournals(ctx context.Context) (err error) {
counter := telemetry_server.StartCAManagerPruneBundleCall(m.c.Metrics)
defer counter.Done(&err)
ds := m.c.Catalog.GetDataStore()
expiresBefore := m.c.Clock.Now().Add(-safetyThresholdCAJournals)
err = ds.PruneCAJournals(ctx, expiresBefore.Unix())
if err != nil {
return fmt.Errorf("unable to prune CA journals: %w", err)
}
return nil
}
// ProcessBundleUpdates Notify any bundle update, or process tainted authorities
func (m *Manager) ProcessBundleUpdates(ctx context.Context) {
for {
select {
case <-m.bundleUpdatedCh:
if err := m.notifyBundleUpdated(ctx); err != nil {
m.c.Log.WithError(err).Warn("Failed to notify on bundle update")
}
case taintedAuthorities := <-m.taintedUpstreamAuthoritiesCh:
if err := m.notifyTaintedAuthorities(ctx, taintedAuthorities); err != nil {
m.c.Log.WithError(err).Error("Failed to force intermediate bundle rotation")
return
}
case <-ctx.Done():
return
}
}
}
func (m *Manager) NotifyBundleLoaded(ctx context.Context) error {
// if initialization has triggered a "bundle updated" event (e.g. server CA
// was rotated), we want to drain it now as we're about to emit the initial
// bundle loaded event. otherwise, plugins will get an immediate "bundle
// updated" event right after "bundle loaded".
m.dropBundleUpdated()
var bundle *common.Bundle
return m.notify(ctx, "bundle loaded", true,
func(ctx context.Context) (err error) {
bundle, err = m.fetchRequiredBundle(ctx)
return err
},
func(ctx context.Context, n notifier.Notifier) error {
return n.NotifyAndAdviseBundleLoaded(ctx, bundle)
},
)
}
func (m *Manager) activateJWTKey(ctx context.Context) {
log := m.c.Log.WithFields(logrus.Fields{
telemetry.Slot: m.currentJWTKey.id,
telemetry.IssuedAt: m.currentJWTKey.issuedAt,
telemetry.Expiration: m.currentJWTKey.jwtKey.NotAfter,
telemetry.LocalAuthorityID: m.currentJWTKey.authorityID,
})
log.Info("JWT key activated")
telemetry_server.IncrActivateJWTKeyManagerCounter(m.c.Metrics)
m.currentJWTKey.status = journal.Status_ACTIVE
if err := m.journal.UpdateJWTKeyStatus(ctx, m.currentJWTKey.AuthorityID(), journal.Status_ACTIVE); err != nil {
log.WithError(err).Error("Failed to update to activated status on JWTKey journal entry")
}
m.c.CA.SetJWTKey(m.currentJWTKey.jwtKey)
}
func (m *Manager) activateX509CA(ctx context.Context) {
log := m.c.Log.WithFields(logrus.Fields{
telemetry.Slot: m.currentX509CA.id,
telemetry.IssuedAt: m.currentX509CA.issuedAt,
telemetry.Expiration: m.currentX509CA.x509CA.Certificate.NotAfter,
telemetry.LocalAuthorityID: m.currentX509CA.authorityID,
telemetry.UpstreamAuthorityID: m.currentX509CA.upstreamAuthorityID,
})
log.Info("X509 CA activated")
telemetry_server.IncrActivateX509CAManagerCounter(m.c.Metrics)
m.currentX509CA.status = journal.Status_ACTIVE
if err := m.journal.UpdateX509CAStatus(ctx, m.currentX509CA.AuthorityID(), journal.Status_ACTIVE); err != nil {
log.WithError(err).Error("Failed to update to activated status on X509CA journal entry")
}
ttl := m.currentX509CA.x509CA.Certificate.NotAfter.Sub(m.c.Clock.Now())
telemetry_server.SetX509CARotateGauge(m.c.Metrics, m.c.TrustDomain.Name(), float32(ttl.Seconds()))
m.c.Log.WithFields(logrus.Fields{
telemetry.TrustDomainID: m.c.TrustDomain.IDString(),
telemetry.TTL: ttl.Seconds(),
}).Debug("Successfully rotated X.509 CA")
m.c.CA.SetX509CA(m.currentX509CA.x509CA)
}
func (m *Manager) bundleUpdated() {
select {
case m.bundleUpdatedCh <- struct{}{}:
default:
}
}
func (m *Manager) dropBundleUpdated() {
select {
case <-m.bundleUpdatedCh:
default:
}
}
func (m *Manager) notifyUpstreamAuthoritiesTainted(taintedAuthorities []*x509.Certificate) {
select {
case m.taintedUpstreamAuthoritiesCh <- taintedAuthorities:
default:
}
}
func (m *Manager) fetchRootCAByAuthorityID(ctx context.Context, authorityID string) (*x509.Certificate, error) {
bundle, err := m.fetchRequiredBundle(ctx)
if err != nil {
return nil, err
}
for _, rootCA := range bundle.RootCas {
if rootCA.TaintedKey {
cert, err := x509.ParseCertificate(rootCA.DerBytes)
if err != nil {
return nil, fmt.Errorf("failed to parse RootCA: %w", err)
}
skID := x509util.SubjectKeyIDToString(cert.SubjectKeyId)
if authorityID == skID {
return cert, nil
}
}
}
return nil, fmt.Errorf("no tainted root CA found with authority ID: %q", authorityID)
}
func (m *Manager) notifyTaintedAuthorities(ctx context.Context, taintedAuthorities []*x509.Certificate) error {
taintBackoff := backoff.NewBackoff(
m.c.Clock,
taintBackoffInterval,
backoff.WithMaxElapsedTime(taintBackoffMaxElapsedTime),
)
for {
err := m.processTaintedUpstreamAuthorities(ctx, taintedAuthorities)
if err == nil {
break
}
nextDuration := taintBackoff.NextBackOff()
if nextDuration == backoff.Stop {
return err
}
m.c.Log.WithError(err).Warn("Failed to process tainted keys on upstream authority")
if m.triggerBackOffCh != nil {
m.triggerBackOffCh <- err
}
select {
case <-ctx.Done():
return ctx.Err()
case <-m.c.Clock.After(nextDuration):
continue
}
}
return nil
}
func (m *Manager) processTaintedUpstreamAuthorities(ctx context.Context, taintedAuthorities []*x509.Certificate) error {
// Nothing to rotate if no upstream authority is used
if m.upstreamClient == nil {
return errors.New("processing of tainted upstream authorities must not be reached when not using an upstream authority; please report this bug")
}
if len(taintedAuthorities) == 0 {
// No tainted keys found
return nil
}
m.c.Log.Debug("Processing tainted keys on upstream authority")
currentSlotCA := m.currentX509CA.x509CA
if ok := isX509AuthorityTainted(currentSlotCA, taintedAuthorities); ok {
m.c.Log.Info("Current root CA is signed by a tainted upstream authority, preparing rotation")
if ok := m.shouldPrepareX509CA(taintedAuthorities); ok {
if err := m.PrepareX509CA(ctx); err != nil {
return fmt.Errorf("failed to prepare x509 authority: %w", err)
}
}
// Activate the prepared X.509 authority
m.RotateX509CA(ctx)
}
// Now that we have rotated the intermediate, we can notify about the
// tainted authorities, so agents and downstream servers can start forcing
// the rotation of their SVIDs.
ds := m.c.Catalog.GetDataStore()
for _, each := range taintedAuthorities {
skID := x509util.SubjectKeyIDToString(each.SubjectKeyId)
if err := ds.TaintX509CA(ctx, m.c.TrustDomain.IDString(), skID); err != nil {
return fmt.Errorf("could not taint X509 CA in datastore: %w", err)
}
}
// Intermediate is safe. Notify rotator to force rotation
// of tainted X.509 SVID.
m.c.CA.NotifyTaintedX509Authorities(taintedAuthorities)
return nil
}
func (m *Manager) notifyBundleUpdated(ctx context.Context) error {
var bundle *common.Bundle
return m.notify(ctx, "bundle updated", false,
func(ctx context.Context) (err error) {
bundle, err = m.fetchRequiredBundle(ctx)
return err
},
func(ctx context.Context, n notifier.Notifier) error {
return n.NotifyBundleUpdated(ctx, bundle)
},
)
}
func (m *Manager) notify(ctx context.Context, event string, advise bool, pre func(context.Context) error, do func(context.Context, notifier.Notifier) error) error {
notifiers := m.c.Catalog.GetNotifiers()
if len(notifiers) == 0 {
return nil
}
if pre != nil {
if err := pre(ctx); err != nil {
return err
}
}
errsCh := make(chan error, len(notifiers))
for _, n := range notifiers {
go func(n notifier.Notifier) {
err := do(ctx, n)
f := m.c.Log.WithFields(logrus.Fields{
telemetry.Notifier: n.Name(),
telemetry.Event: event,
})
if err == nil {
f.Debug("Notifier handled event")
} else {
f := f.WithError(err)
if advise {
f.Error("Notifier failed to handle event")
} else {
f.Warn("Notifier failed to handle event")
}
}
errsCh <- err
}(n)
}
var allErrs errs.Group
for i := 0; i < len(notifiers); i++ {
// don't select on the ctx here as we can rely on the plugins to
// respond to context cancellation and return an error.
if err := <-errsCh; err != nil {
allErrs.Add(err)
}
}
if err := allErrs.Err(); err != nil {
return errs.New("one or more notifiers returned an error: %v", err)
}
return nil
}
func (m *Manager) fetchRequiredBundle(ctx context.Context) (*common.Bundle, error) {
bundle, err := m.fetchOptionalBundle(ctx)
if err != nil {
return nil, err
}
if bundle == nil {
return nil, errs.New("trust domain bundle is missing")
}
return bundle, nil
}
func (m *Manager) fetchOptionalBundle(ctx context.Context) (*common.Bundle, error) {
ds := m.c.Catalog.GetDataStore()
bundle, err := ds.FetchBundle(ctx, m.c.TrustDomain.IDString())
if err != nil {
return nil, errs.Wrap(err)
}
return bundle, nil
}
func (m *Manager) upstreamSignX509CA(ctx context.Context, signer crypto.Signer) (*ca.X509CA, error) {
template, err := m.c.CredBuilder.BuildUpstreamSignedX509CACSR(ctx, credtemplate.UpstreamSignedX509CAParams{
PublicKey: signer.Public(),
})
if err != nil {
return nil, err
}
csr, err := x509.CreateCertificateRequest(rand.Reader, template, signer)
if err != nil {
return nil, err
}
validator := ca.X509CAValidator{
TrustDomain: m.c.TrustDomain,
CredValidator: m.c.CredValidator,
Signer: signer,
Clock: m.c.Clock,
}
caChain, err := m.upstreamClient.MintX509CA(ctx, csr, m.caTTL, validator.ValidateUpstreamX509CA)
if err != nil {
return nil, err
}
return &ca.X509CA{
Signer: signer,
Certificate: caChain[0],
UpstreamChain: caChain,
}, nil
}
func (m *Manager) selfSignX509CA(ctx context.Context, signer crypto.Signer) (*ca.X509CA, error) {
template, err := m.c.CredBuilder.BuildSelfSignedX509CATemplate(ctx, credtemplate.SelfSignedX509CAParams{
PublicKey: signer.Public(),
})
if err != nil {
return nil, err
}
cert, err := x509util.CreateCertificate(template, template, signer.Public(), signer)
if err != nil {
return nil, err
}
if err := m.c.CredValidator.ValidateX509CA(cert); err != nil {
return nil, fmt.Errorf("invalid downstream X509 CA: %w", err)
}
if _, err := m.appendBundle(ctx, []*x509.Certificate{cert}, nil); err != nil {
return nil, err
}
return &ca.X509CA{
Signer: signer,
Certificate: cert,
}, nil
}
func (m *Manager) appendBundle(ctx context.Context, caChain []*x509.Certificate, jwtSigningKeys []*common.PublicKey) (*common.Bundle, error) {
var rootCAs []*common.Certificate
for _, caCert := range caChain {
rootCAs = append(rootCAs, &common.Certificate{
DerBytes: caCert.Raw,
})
}
ds := m.c.Catalog.GetDataStore()
res, err := ds.AppendBundle(ctx, &common.Bundle{
TrustDomainId: m.c.TrustDomain.IDString(),
RootCas: rootCAs,
JwtSigningKeys: jwtSigningKeys,
})
if err != nil {
return nil, err
}
m.bundleUpdated()
return res, nil
}
func (m *Manager) shouldPrepareX509CA(taintedAuthorities []*x509.Certificate) bool {
slot := m.nextX509CA
switch {
case slot.IsEmpty():
return true
case slot.Status() == journal.Status_PREPARED:
isTainted := isX509AuthorityTainted(slot.x509CA, taintedAuthorities)
m.c.Log.Info("Next authority is tainted, prepare new X.509 authority")
return isTainted
default:
return false
}
}
// MaxSVIDTTL returns the maximum SVID lifetime that can be guaranteed to not
// be cut artificially short by a scheduled rotation.
func MaxSVIDTTL() time.Duration {
return activationThresholdCap
}
// MaxSVIDTTLForCATTL returns the maximum SVID TTL that can be guaranteed given
// a specific CA TTL. In other words, given a CA TTL, what is the largest SVID
// TTL that is guaranteed to not be cut artificially short by a scheduled
// rotation?
func MaxSVIDTTLForCATTL(caTTL time.Duration) time.Duration {
maxTTL := caTTL / activationThresholdDivisor
if maxTTL > activationThresholdCap {
maxTTL = activationThresholdCap
}
return maxTTL
}
// MinCATTLForSVIDTTL returns the minimum CA TTL necessary to guarantee an SVID
// TTL of the provided value. In other words, given an SVID TTL, what is the
// minimum CA TTL that will guarantee that the SVIDs lifetime won't be cut
// artificially short by a scheduled rotation?
func MinCATTLForSVIDTTL(svidTTL time.Duration) time.Duration {
return svidTTL * activationThresholdDivisor
}
type bundleUpdater struct {
log logrus.FieldLogger
trustDomainID string
ds datastore.DataStore
updated func()
upstreamAuthoritiesTainted func([]*x509.Certificate)
processedTaintedAuthorities map[string]struct{}
}
func (u *bundleUpdater) SyncX509Roots(ctx context.Context, roots []*x509certificate.X509Authority) error {
bundle := &common.Bundle{
TrustDomainId: u.trustDomainID,
RootCas: make([]*common.Certificate, 0, len(roots)),
}
x509Authorities, err := u.fetchX509Authorities(ctx)
if err != nil {
return err
}
newAuthorities := make(map[string]struct{}, len(roots))
var taintedAuthorities []*x509.Certificate
for _, root := range roots {
skID := x509util.SubjectKeyIDToString(root.Certificate.SubjectKeyId)
// Collect all skIDs
newAuthorities[skID] = struct{}{}
// Verify if new root ca is tainted
if root.Tainted {
// Taint x.509 authority, if required
if found, ok := x509Authorities[skID]; ok && !found.Tainted {
_, alreadyProcessed := u.processedTaintedAuthorities[skID]
if !alreadyProcessed {
u.processedTaintedAuthorities[skID] = struct{}{}
// Add to the list of new tainted authorities
taintedAuthorities = append(taintedAuthorities, found.Certificate)
u.log.WithField(telemetry.SubjectKeyID, skID).Info("X.509 authority tainted")
}
// Prevent to add tainted keys, since status is updated before
continue
}
}
bundle.RootCas = append(bundle.RootCas, &common.Certificate{
DerBytes: root.Certificate.Raw,
TaintedKey: root.Tainted,
})
}
// Notify about tainted authorities to force the rotation of
// intermediates and update the database. This is done in a separate thread
// to prevent agents and downstream servers to start the rotation before the
// current server starts the rotation of the intermediate.
if len(taintedAuthorities) > 0 {
u.upstreamAuthoritiesTainted(taintedAuthorities)
}
for skID, authority := range x509Authorities {
// Only tainted keys can ke revoked
if authority.Tainted {
// In case a stored tainted authority is not found,
// from latest bundle update, then revoke it
if _, found := newAuthorities[skID]; !found {
if err := u.ds.RevokeX509CA(ctx, u.trustDomainID, skID); err != nil {
return fmt.Errorf("failed to revoke a tainted key %q: %w", skID, err)
}
u.log.WithField(telemetry.SubjectKeyID, skID).Info("X.509 authority revoked")
}
}
}
if _, err := u.appendBundle(ctx, bundle); err != nil {
return err
}
return nil
}
func (u *bundleUpdater) AppendJWTKeys(ctx context.Context, keys []*common.PublicKey) ([]*common.PublicKey, error) {
bundle, err := u.appendBundle(ctx, &common.Bundle{
TrustDomainId: u.trustDomainID,
JwtSigningKeys: keys,
})
if err != nil {
return nil, err
}
return bundle.JwtSigningKeys, nil
}
func (u *bundleUpdater) LogError(err error, msg string) {
u.log.WithError(err).Error(msg)
}
func (u *bundleUpdater) fetchX509Authorities(ctx context.Context) (map[string]*x509certificate.X509Authority, error) {
bundle, err := u.ds.FetchBundle(ctx, u.trustDomainID)
if err != nil {
return nil, fmt.Errorf("failed to fetch bundle: %w", err)
}
// Bundle not found
if bundle == nil {
return nil, nil
}
authorities := map[string]*x509certificate.X509Authority{}
for _, eachRoot := range bundle.RootCas {
cert, err := x509.ParseCertificate(eachRoot.DerBytes)
if err != nil {
return nil, fmt.Errorf("failed to parse root certificate: %w", err)