Repository navigation
Fixes #28299: move a list secret to its element's own id when the element is renamed or moved - #38235
Khairajani wants to merge 2 commits into
Conversation
…d or moved A save kept an existing secret reference, so a renamed MCP server still read its old name's secret and a server later added under that name overwrote it. Before a list is written, a reference to another element's secret is read back into its value and stored under the element's own id. A reference that cannot be read is kept.
| if (obj instanceof Collection<?> | ||
| && store | ||
| && persistExternalSecrets | ||
| && storesSecretsByReference()) { | ||
| resolveReferencesInCollection(obj, secretId, collectionField); |
There was a problem hiding this comment.
Failed renames overwrite working keys. The new move writes over existing secrets before the whole save succeeds. For example, rename
alpha to beta while retaining its reference, then rename the existing beta to alpha with apiKey set to the reserved string "null". The first element overwrites beta’s key; the second throws a 400. The database keeps the original service, but its beta now reads alpha’s key. A later provider write failure causes the same problem during an ordinary swap.
Make these moves recoverable, or write to fresh ids that cannot overwrite keys used by the saved service. Reading all sources first protects successful swaps, not failed saves.
A move read every source first, which protects a save that succeeds; one that failed part way could leave a server reading another's key. The value at the element's own id is now read as well and written back if the save fails, and a refused value is rejected before any key moves.
| private void encryptCollection( | ||
| final Object obj, | ||
| final CollectionSecretId collection, | ||
| final boolean store, | ||
| final boolean persistExternalSecrets) { | ||
| final boolean isMovable = | ||
| obj instanceof Collection<?> | ||
| && store | ||
| && persistExternalSecrets | ||
| && storesSecretsByReference(); | ||
| final List<MovedSecret> moves = | ||
| isMovable | ||
| ? resolveReferencesInCollection(obj, collection.secretId(), collection.field()) | ||
| : List.of(); | ||
| try { |
There was a problem hiding this comment.
⚠️ Bug: Moved secrets aren't restored when the save fails after encryption
restoreMovedSecrets only runs when encryptCollection itself throws. In ServiceEntityRepository.store, encryptConnection(service) runs inside writeWithSecretOwnership, and after it come super.store(service, update, expectedVersion) and ownership.recordWrite, all in the same DB transaction. If any of those fail (a version conflict, a DB error, an ownership failure), the transaction rolls back but the vault writes stay. Take a swap of alpha/beta: alpha's id now holds beta-key and the reverse, while the stored entity still has the old config, so each server sends the other's credential. updateMany has the same problem: if encrypting a later service throws, the moves already done for earlier services in the batch are never undone. The amendment says "a save that fails part way writes it back", but that only holds for failures during encryption, and the caveat lists only dropped servers.
Fix: have the encrypt step return the MovedSecret list, or record it on a per-save context. writeWithSecretOwnership can then restore it when write.run() or the transaction fails, for example from a rollback hook or a try/catch around executeInTransaction. Add a test where super.store throws after a swap.
Was this helpful? React with 👍 / 👎
Code Review
|
| Compact |
|
Was this helpful? React with 👍 / 👎 | Powered by Gitar — free for open source
| protected void restoreMovedSecret(final MovedSecret moved) { | ||
| moved | ||
| .previous() | ||
| .ifPresentOrElse( | ||
| value -> upsertSecret(moved.secretId(), value), () -> deleteSecret(moved.secretId())); |
There was a problem hiding this comment.
Failed renames block retries. When a rename creates a new destination secret and a later list write fails, this rollback deletes the new secret. Azure Key Vault keeps deleted names reserved, so retrying the rename fails when
storeValue tries to write the same id. The reserved-name fallback in storeOwnedValue does not cover this path.
Make rollback and retry work with Azure's soft deletion, rather than leaving the destination name unusable until recovery or the retention period ends.
|
Closing: moving the secret on save kept trading one bug for another. The fix will be redesigned in #38241, deciding from the stored config's references instead of the vault's contents. |
Fixes #28299
Follow-up to #38117, from a review finding on its 2.0 backport #38118.
secret:reference as it was. An MCP server renamed fromalphatobeta(or one keyed by position and moved) still readalpha's secret, and a server later saved asalphaoverwrote it, so both servers sent the new key.ADR:2026-11-11-mcp-service-server-names-are-unique-and-masks-need-a-stored-value.ReflectionUtil.secretKeyand drops itselse if.Tests
SecretMapEntriesExternalStoreTestcases: rename then reuse the old name (same save and later save), swap names, reorder position-keyed servers; each failed before the fix.ListSecretReferenceEdgeCasesTest: missing secret, unreadable secret,-separator, database secrets manager, a swap whose second write fails, and a refused value.McpServiceResourceITandSecretsManagerUpdateSoftDeletedITpass.🤖 Generated with Claude Code
Not safe to merge yet: failed saves can still change working keys, and Azure retries can get stuck.
Findings
Summary
Moves list secrets to each element's current id after a rename or reorder, and adds recovery for failed writes.
Reviews (2) · Last reviewed commit: "Put back a secret a move overwrote when ..." · Reviewed by Greptile