Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create event when entry is pruned #4860

Merged
merged 11 commits into from
Feb 14, 2024
3 changes: 3 additions & 0 deletions pkg/server/datastore/sqlstore/sqlstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -3818,6 +3818,9 @@ func pruneRegistrationEntries(tx *gorm.DB, expiresBefore time.Time, logger logru
if err := deleteRegistrationEntrySupport(tx, entry); err != nil {
return err
}
if err := createRegistrationEntryEvent(tx, entry.EntryID); err != nil {
faisal-memon marked this conversation as resolved.
Show resolved Hide resolved
return err
}
logger.WithFields(logrus.Fields{
telemetry.SPIFFEID: entry.SpiffeID,
telemetry.ParentID: entry.ParentID,
Expand Down
20 changes: 20 additions & 0 deletions pkg/server/datastore/sqlstore/sqlstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2047,6 +2047,11 @@ func (s *PluginSuite) TestPruneRegistrationEntries() {
}
prunedLogMessage := "Pruned an expired registration"

resp, err := s.ds.ListRegistrationEntriesEvents(ctx, &datastore.ListRegistrationEntriesEventsRequest{})
s.Require().NoError(err)
s.Require().Equal(1, len(resp.Events))
s.Require().Equal(createdRegistrationEntry.EntryId, resp.Events[0].EntryID)

for _, tt := range []struct {
name string
time time.Time
Expand Down Expand Up @@ -2082,12 +2087,27 @@ func (s *PluginSuite) TestPruneRegistrationEntries() {
} {
tt := tt
s.T().Run(tt.name, func(t *testing.T) {
lastEventID, err := s.ds.GetLatestRegistrationEntryEventID(ctx)
require.NoError(t, err)

err = s.ds.PruneRegistrationEntries(ctx, tt.time)
require.NoError(t, err)
fetchedRegistrationEntry, err = s.ds.FetchRegistrationEntry(ctx, createdRegistrationEntry.EntryId)
require.NoError(t, err)
assert.Equal(t, tt.expectedRegistrationEntry, fetchedRegistrationEntry)

// Verify pruning triggers event creation
resp, err := s.ds.ListRegistrationEntriesEvents(ctx, &datastore.ListRegistrationEntriesEventsRequest{
GreaterThanEventID: lastEventID,
})
require.NoError(t, err)
if tt.expectedRegistrationEntry != nil {
require.Equal(t, 0, len(resp.Events))
} else {
require.Equal(t, 1, len(resp.Events))
require.Equal(t, createdRegistrationEntry.EntryId, resp.Events[0].EntryID)
}

if tt.expectedLastLog.Message == prunedLogMessage {
spiretest.AssertLastLogs(t, s.hook.AllEntries(), []spiretest.LogEntry{tt.expectedLastLog})
} else {
Expand Down
Loading