Skip to content

Implemented periodic purging of expired agents - #6152

Merged
sorindumitru merged 2 commits into
spiffe:mainfrom
ValFadeev:periodic-purge-expired-nodes-server
Jul 16, 2025
Merged

sorindumitru merged 2 commits into
spiffe:mainfrom
ValFadeev:periodic-purge-expired-nodes-server

Conversation

@ValFadeev

Copy link
Copy Markdown
Contributor

Pull Request check list

  • Commit conforms to CONTRIBUTING.md?
  • Proper tests/regressions included?
  • Documentation updated?

Affected functionality
Adds the capability to enable periodic pruning of attested agents with certificates expired for longer than a configurable duration of time.

Description of change
Introduces new configuration options prune_attested_nodes_expired_for and prune_non_reattestable_nodes. When prune_attested_nodes_expired_for is set to a given duration value SPIRE server will start a periodic task deleting attested nodes and the corresponding selectors, where agent certificate had been expired for longer than the specified duration. Non-reattestable nodes are not pruned by default. In order to consider the latter for pruning prune_non_reattestable_nodes must be set to true. Banned nodes are not pruned by this task.

In the interests of limiting the scope of the change, and to require explicit opt-in at this stage, the implementation of the recently introduced agent purge subcommand (#3982) is left unchanged.

Which issue this PR fixes

Closes #5938

@ValFadeev
ValFadeev force-pushed the periodic-purge-expired-nodes-server branch from 4073973 to 9c11127 Compare June 24, 2025 23:16
@ValFadeev
ValFadeev marked this pull request as ready for review June 24, 2025 23:45
Comment thread doc/spire_server.md Resolved
return telemetry.StartCall(m, telemetry.Datastore, telemetry.Node, telemetry.Delete)
}

func StartPruneAttestedExpiredNodes(m telemetry.Metrics) *telemetry.CallCounter {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
func StartPruneAttestedExpiredNodes(m telemetry.Metrics) *telemetry.CallCounter {
// StartPruneAttestedExpiredNodes return metric
// for server's datastore, on pruning expired attested nodes.
func StartPruneAttestedExpiredNodes(m telemetry.Metrics) *telemetry.CallCounter {

return &common.AttestedNode{}, ds.err
}

func (ds *fakeDataStore) PruneAttestedExpiredNodes(ctx context.Context, expiredBefore time.Time, includeNonReattestable bool) error {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
func (ds *fakeDataStore) PruneAttestedExpiredNodes(ctx context.Context, expiredBefore time.Time, includeNonReattestable bool) error {
func (ds *fakeDataStore) PruneAttestedExpiredNodes(context.Context, time.Time, bool) error {

Comment on lines +1781 to +1779
if !include {
return tx.Where("can_reattest = ?", true)
} else {
return tx
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if !include {
return tx.Where("can_reattest = ?", true)
} else {
return tx
}
if !include {
return tx.Where("can_reattest = ?", true)
}
return tx

logger.WithFields(logrus.Fields{
telemetry.SPIFFEID: node.SpiffeID,
telemetry.SVIDSerialNumber: node.SerialNumber,
telemetry.SerialNumber: "CURRENT",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this required?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took pruning of registration entries as an example here

logger.WithFields(logrus.Fields{
telemetry.SPIFFEID: entry.SpiffeID,
telemetry.ParentID: entry.ParentID,
telemetry.RegistrationID: entry.EntryID,
}).Info("Pruned an expired registration")

telemetry.SPIFFEID: node.SpiffeID,
telemetry.SVIDSerialNumber: node.SerialNumber,
telemetry.SerialNumber: "CURRENT",
}).Info("Pruned an expired node")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is going to cause spam the first time, may we move to debug? or add a summary?
like logger.WithField("count", count).Info("Pruned expired agents")

Comment thread pkg/server/node/manager_test.go Outdated
CanReattest: false,
CertNotAfter: s.clock.Now().Unix(),
}
attestedNodeNonReattestable, err := s.ds.CreateAttestedNode(context.Background(), nodeNonReattestable)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you get the context from T and use it instead of context.Background() in all tests?

Suggested change
attestedNodeNonReattestable, err := s.ds.CreateAttestedNode(context.Background(), nodeNonReattestable)
ctx := s.T().Context()
attestedNodeNonReattestable, err := s.ds.CreateAttestedNode(ctx, nodeNonReattestable)

@ValFadeev
ValFadeev requested a review from MarcosDY July 5, 2025 12:11
@sorindumitru sorindumitru added this to the 1.13.0 milestone Jul 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Automated periodic purging of stale agents

3 participants