Skip to content

Node trusted key pruning - #945

Merged
aojea merged 3 commits into
google:mainfrom
aojea:node-trusted-key-pruning
Nov 25, 2026
Merged

aojea merged 3 commits into
google:mainfrom
aojea:node-trusted-key-pruning

Conversation

@aojea

@aojea aojea commented Nov 25, 2026

Copy link
Copy Markdown
Collaborator

No description provided.

p1c2u and others added 3 commits November 25, 2026 15:34
A trusted key's age counted from when the node first received it, and a
/keys sync kept that time. Pruning keeps only the newest key past the
grace period, so a key the control plane still lists in its rotation grace
period was dropped a day after the node first saw it, even when it signed
the node's own identity. The next start then failed the role check
("biscuit: invalid signature") on a valid identity, and if the control
plane could not be reached at that moment, both recoveries failed too.

- mergeTrustedKeys: every key a /keys answer lists counts as vouched for
  at that sync, so pruning only drops keys the control plane stopped
  listing, or ones the node could not confirm for longer than the grace
  period.
- pruneTrustedKeys: keeps every key with the latest time, since a /keys
  answer vouches for all its keys at once and its order says nothing about
  which is current, and never drops the key that signed the node's current
  identity.
- NewAgentMeshNode: the configured key re-added next to persisted keys gets
  a fixed old time (the zero time is not stored and read back as now), so
  it cannot outrank the control plane's newer keys after a restart.
- Start returns *StaleIdentityError when the stored identity fails the role
  check and neither recovery renews it. It unwraps to the role, refresh and
  recovery errors, so callers can tell a refusal (*RefreshError with its
  status) from a control plane that never answered.
Use mustGenerateKey instead of ignoring ed25519.GenerateKey's error, and
report a failed store close (errcheck).
Apply the review of google#917:

- Remove mergeTrustedKeys; syncTrustedKeys gives each key of the /keys
  answer the time of the sync.
- Log each key that pruning drops, with its last vouched time and the
  grace period.
- Remove the keep parameter, signedOwnIdentity, neverVouched and
  StaleIdentityError, and their tests: google#916 does not need them.
- Correct the meaning of receive_time in api/agentmesh.proto and of
  --key-grace-period in the node reference.
- Test the sequence of the issue (sync, then prune) and add a row for
  the keys of one sync to TestPruneTrustedKeys.

@gemini-code-assist gemini-code-assist Bot left a comment

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.

Code Review

This pull request updates the control plane key pruning logic so that keys are aged based on when the control plane last vouched for them (i.e., when they were last seen in a /keys sync or rotation event) rather than when they were first learned. It removes the key-merging logic, updates documentation, and adds corresponding tests. The review feedback suggests optimizing slice allocation in syncTrustedKeys and updating test assertions in controlplane_test.go to comply with the repository style guide by explicitly stating both observed and expected values.

Comment on lines +146 to +149
synced := make([]TrustedKey, 0, len(keys))
for _, key := range keys {
synced = append(synced, TrustedKey{Key: key, ReceivedAt: now})
}

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.

medium

Since the size of keys is exactly known, we can pre-allocate the slice with the correct length and assign elements directly by index instead of using append. This avoids slice growth checks and is more efficient.

Suggested change
synced := make([]TrustedKey, 0, len(keys))
for _, key := range keys {
synced = append(synced, TrustedKey{Key: key, ReceivedAt: now})
}
synced := make([]TrustedKey, len(keys))
for i, key := range keys {
synced[i] = TrustedKey{Key: key, ReceivedAt: now}
}

Comment on lines +141 to +143
if containsTrustedKey(n.trustedKeys, expiredPub) {
t.Error("a key /keys does not list stayed in the trust set")
}

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.

medium

According to Section 3, Rule 8 of the Repository Style Guide, assertion messages must state both the observed and the expected value. Let's update the assertion message to explicitly show what was observed and what was expected.

Suggested change
if containsTrustedKey(n.trustedKeys, expiredPub) {
t.Error("a key /keys does not list stayed in the trust set")
}
if containsTrustedKey(n.trustedKeys, expiredPub) {
t.Errorf("syncTrustedKeys kept expired key %x, want it removed", expiredPub)
}
References
  1. Section 3, Rule 8 of the Repository Style Guide requires assertion messages to state both the observed and the expected value. (link)

Comment on lines +145 to +147
if len(got) != 2 || !containsTrustedKey(got, retiredPub) || !containsTrustedKey(got, currentPub) {
t.Errorf("pruning kept %d keys, want the retired and the current key that /keys lists", len(got))
}

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.

medium

According to Section 3, Rule 8 of the Repository Style Guide, assertion messages must state both the observed and the expected value. Let's update this assertion message to explicitly show the observed number of keys and the expected keys.

Suggested change
if len(got) != 2 || !containsTrustedKey(got, retiredPub) || !containsTrustedKey(got, currentPub) {
t.Errorf("pruning kept %d keys, want the retired and the current key that /keys lists", len(got))
}
if len(got) != 2 || !containsTrustedKey(got, retiredPub) || !containsTrustedKey(got, currentPub) {
t.Errorf("pruning kept %d keys (%v), want 2 keys: retired (%x) and current (%x)", len(got), got, retiredPub, currentPub)
}
References
  1. Section 3, Rule 8 of the Repository Style Guide requires assertion messages to state both the observed and the expected value. (link)

@aojea
aojea merged commit 23a1c9c into google:main Nov 25, 2026
22 checks passed
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.

2 participants