Skip to content

Commit

Permalink
Introduce support to save and load the CA journal from the datastore (#…
Browse files Browse the repository at this point in the history
…4690)

* Save and load the CA journal from datastore

Signed-off-by: Agustín Martínez Fayó <amartinezfayo@gmail.com>
  • Loading branch information
amartinezfayo authored Jan 3, 2024
1 parent 54897d1 commit 1c8dc49
Show file tree
Hide file tree
Showing 20 changed files with 1,341 additions and 386 deletions.
6 changes: 6 additions & 0 deletions pkg/common/telemetry/names.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,12 @@ const (
// BySelectors tags selectors used when filtering
BySelectors = "by_selectors"

// CAJournal is a CA journal record
CAJournal = "ca_journal"

// CAJournalID tags a CA journal ID
CAJournalID = "ca_journal_id"

// CallerAddr labels an API caller address
CallerAddr = "caller_addr"

Expand Down
29 changes: 29 additions & 0 deletions pkg/common/telemetry/server/datastore/ca_journal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package datastore

import (
"github.com/spiffe/spire/pkg/common/telemetry"
)

// StartSetCAJournal return metric for server's datastore, on setting a CA
// journal.
func StartSetCAJournal(m telemetry.Metrics) *telemetry.CallCounter {
return telemetry.StartCall(m, telemetry.Datastore, telemetry.CAJournal, telemetry.Set)
}

// StartFetchCAJournal return metric
// for server's datastore, on fetching a CA journal.
func StartFetchCAJournal(m telemetry.Metrics) *telemetry.CallCounter {
return telemetry.StartCall(m, telemetry.Datastore, telemetry.CAJournal, telemetry.Fetch)
}

// StartPruneCAJournalsCall return metric for server's datastore, on pruning CA
// journals.
func StartPruneCAJournalsCall(m telemetry.Metrics) *telemetry.CallCounter {
return telemetry.StartCall(m, telemetry.Datastore, telemetry.CAJournal, telemetry.Prune)
}

// StartListCAJournalsForTesting return metric
// for server's datastore, on listing CA journals for testing.
func StartListCAJournalsForTesting(m telemetry.Metrics) *telemetry.CallCounter {
return telemetry.StartCall(m, telemetry.Datastore, telemetry.CAJournal, telemetry.List)
}
24 changes: 24 additions & 0 deletions pkg/common/telemetry/server/datastore/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,3 +293,27 @@ func (w metricsWrapper) UpdateFederationRelationship(ctx context.Context, fr *da
defer callCounter.Done(&err)
return w.ds.UpdateFederationRelationship(ctx, fr, mask)
}

func (w metricsWrapper) SetCAJournal(ctx context.Context, caJournal *datastore.CAJournal) (_ *datastore.CAJournal, err error) {
callCounter := StartSetCAJournal(w.m)
defer callCounter.Done(&err)
return w.ds.SetCAJournal(ctx, caJournal)
}

func (w metricsWrapper) FetchCAJournal(ctx context.Context, activeX509AuthorityID string) (_ *datastore.CAJournal, err error) {
callCounter := StartFetchCAJournal(w.m)
defer callCounter.Done(&err)
return w.ds.FetchCAJournal(ctx, activeX509AuthorityID)
}

func (w metricsWrapper) ListCAJournalsForTesting(ctx context.Context) (_ []*datastore.CAJournal, err error) {
callCounter := StartListCAJournalsForTesting(w.m)
defer callCounter.Done(&err)
return w.ds.ListCAJournalsForTesting(ctx)
}

func (w metricsWrapper) PruneCAJournals(ctx context.Context, allCAsExpireBefore int64) (err error) {
callCounter := StartPruneCAJournalsCall(w.m)
defer callCounter.Done(&err)
return w.ds.PruneCAJournals(ctx, allCAsExpireBefore)
}
32 changes: 32 additions & 0 deletions pkg/common/telemetry/server/datastore/wrapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,22 @@ func TestWithMetrics(t *testing.T) {
key: "datastore.registration_entry.update",
methodName: "UpdateRegistrationEntry",
},
{
key: "datastore.ca_journal.set",
methodName: "SetCAJournal",
},
{
key: "datastore.ca_journal.fetch",
methodName: "FetchCAJournal",
},
{
key: "datastore.ca_journal.prune",
methodName: "PruneCAJournals",
},
{
key: "datastore.ca_journal.list",
methodName: "ListCAJournalsForTesting",
},
} {
tt := tt
methodType, ok := wt.MethodByName(tt.methodName)
Expand Down Expand Up @@ -477,3 +493,19 @@ func (ds *fakeDataStore) UpdateRegistrationEntry(context.Context, *common.Regist
func (ds *fakeDataStore) UpdateFederationRelationship(context.Context, *datastore.FederationRelationship, *types.FederationRelationshipMask) (*datastore.FederationRelationship, error) {
return &datastore.FederationRelationship{}, ds.err
}

func (ds *fakeDataStore) SetCAJournal(context.Context, *datastore.CAJournal) (*datastore.CAJournal, error) {
return &datastore.CAJournal{}, ds.err
}

func (ds *fakeDataStore) FetchCAJournal(context.Context, string) (*datastore.CAJournal, error) {
return &datastore.CAJournal{}, ds.err
}

func (ds *fakeDataStore) ListCAJournalsForTesting(context.Context) ([]*datastore.CAJournal, error) {
return []*datastore.CAJournal{}, ds.err
}

func (ds *fakeDataStore) PruneCAJournals(context.Context, int64) error {
return ds.err
}
8 changes: 4 additions & 4 deletions pkg/server/api/localauthority/v1/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ type CAManager interface {
GetCurrentJWTKeySlot() manager.Slot
GetNextJWTKeySlot() manager.Slot
PrepareJWTKey(ctx context.Context) error
RotateJWTKey()
RotateJWTKey(ctx context.Context)

// X509
GetCurrentX509CASlot() manager.Slot
GetNextX509CASlot() manager.Slot
PrepareX509CA(ctx context.Context) error
RotateX509CA()
RotateX509CA(ctx context.Context)
}

// Config is the service configuration
Expand Down Expand Up @@ -140,7 +140,7 @@ func (s *Service) ActivateJWTAuthority(ctx context.Context, req *localauthorityv
return nil, api.MakeErr(log, codes.Internal, "only Prepared authorities can be activated", fmt.Errorf("unsupported local authority status: %v", nextSlot.Status()))
}

s.ca.RotateJWTKey()
s.ca.RotateJWTKey(ctx)

current := s.ca.GetCurrentJWTKeySlot()
state := &localauthorityv1.AuthorityState{
Expand Down Expand Up @@ -308,7 +308,7 @@ func (s *Service) ActivateX509Authority(ctx context.Context, req *localauthority
}

// Move next into current and reset next to clean CA
s.ca.RotateX509CA()
s.ca.RotateX509CA(ctx)

current := s.ca.GetCurrentX509CASlot()
state := &localauthorityv1.AuthorityState{
Expand Down
4 changes: 2 additions & 2 deletions pkg/server/api/localauthority/v1/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1865,7 +1865,7 @@ func (m *fakeCAManager) PrepareJWTKey(context.Context) error {
return m.prepareJWTKeyErr
}

func (m *fakeCAManager) RotateJWTKey() {
func (m *fakeCAManager) RotateJWTKey(context.Context) {
m.rotateJWTKeyCalled = true
}

Expand All @@ -1881,7 +1881,7 @@ func (m *fakeCAManager) PrepareX509CA(context.Context) error {
return m.prepareX509CAErr
}

func (m *fakeCAManager) RotateX509CA() {
func (m *fakeCAManager) RotateX509CA(context.Context) {
m.rotateX509CACalled = true
}

Expand Down
Loading

0 comments on commit 1c8dc49

Please sign in to comment.