Skip to content

Fixes #28299: move a list secret to its element's own id when the element is renamed or moved - #38235

Closed
Khairajani wants to merge 2 commits into
mainfrom
secret-ref-follows-element
Closed

Khairajani wants to merge 2 commits into
mainfrom
secret-ref-follows-element

Conversation

@Khairajani

@Khairajani Khairajani commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #28299

Follow-up to #38117, from a review finding on its 2.0 backport #38118.

  • Bug: with an external secrets manager, a save kept an existing secret: reference as it was. An MCP server renamed from alpha to beta (or one keyed by position and moved) still read alpha's secret, and a server later saved as alpha overwrote it, so both servers sent the new key.
  • Fix: before a list is written, a reference to the same field of another element is read back into its value, and the save stores it under the element's own id. All reads come first, so swapped elements keep their own keys.
  • Failed saves: a move also reads what the element's own id holds, and writes it back if the save fails part way; a refused value is rejected before any key moves.
  • Kept as is: references outside the list (user-given), references whose secret is missing or can't be read (the save still succeeds), the approval/prepare path, and the database secrets manager.
  • Ownership: the secret left under the old id still belongs to the service and goes on hard delete.
  • Decision record: v4 amendment to ADR:2026-11-11-mcp-service-server-names-are-unique-and-masks-need-a-stored-value.
  • Also renames two booleans in ReflectionUtil.secretKey and drops its else if.

Tests

  • New SecretMapEntriesExternalStoreTest cases: 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.
  • Secrets, masker and converter unit tests pass; McpServiceResourceIT and SecretsManagerUpdateSoftDeletedIT pass.

🤖 Generated with Claude Code

RetriggerView in GreptileConfidence Score: 3/5

Not safe to merge yet: failed saves can still change working keys, and Azure retries can get stuck.

Findings

  1. P1 Failed renames block retries ▶
  2. P1 Failed renames overwrite working keys. ▶

Summary

Moves list secrets to each element's current id after a rename or reorder, and adds recovery for failed writes.

  • List elements keep their own secrets when names or positions change.
  • Collection secret keys keep the same names and hashes.

Reviews (2) · Last reviewed commit: "Put back a secret a move overwrote when ..." · Reviewed by Greptile

…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.
Comment on lines +668 to +672
if (obj instanceof Collection<?>
&& store
&& persistExternalSecrets
&& storesSecretsByReference()) {
resolveReferencesInCollection(obj, secretId, collectionField);

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.

P1 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.
Comment on lines +532 to +546
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 {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ 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 👍 / 👎

@gitar-bot

gitar-bot Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Code Review ⚠️ Changes requested 🔴 High risk 0 closed / 1 findings

Moving secrets between external-store IDs changes credential ownership; a mistaken move could make an MCP server use another server's key.

Moves a list secret to its element's own id when an MCP server is renamed or repositioned, so a renamed server no longer reads or overwrites another server's credential. Changes requested: moved secrets are not restored when the save fails after encryption. If super.store or ownership.recordWrite fails after a swap, the transaction rolls back but the vault writes persist, so servers can send each other's credentials. updateMany has the same gap for earlier services in a batch. Fix by tracking moved secrets per save and restoring them on failure, and add a test where super.store throws after a swap.

⚠️ Bug: Moved secrets aren't restored when the save fails after encryption

📄 openmetadata-service/src/main/java/org/openmetadata/service/secrets/SecretsManager.java:532-546 📄 docs/decisions/2026-11-11-mcp-service-server-names-are-unique-and-masks-need-a-stored-value.md:66-69

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.

🤖 Prompt for agents
Code Review: Moves a list secret to its element's own id when an MCP server is renamed or repositioned, so a renamed server no longer reads or overwrites another server's credential. Changes requested: moved secrets are not restored when the save fails after encryption. If `super.store` or `ownership.recordWrite` fails after a swap, the transaction rolls back but the vault writes persist, so servers can send each other's credentials. `updateMany` has the same gap for earlier services in a batch. Fix by tracking moved secrets per save and restoring them on failure, and add a test where `super.store` throws after a swap.

1. ⚠️ Bug: Moved secrets aren't restored when the save fails after encryption
   Files: openmetadata-service/src/main/java/org/openmetadata/service/secrets/SecretsManager.java:532-546, docs/decisions/2026-11-11-mcp-service-server-names-are-unique-and-masks-need-a-stored-value.md:66-69

   `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.

Verification

Rules No rules evaluated

Functional validation Not enabled

Set up

Auto-approval Not enabled

Set up

For more verification details, comment gitar display:verbose.

Options

Display: compact → Showing compact verification results.

Comment with these commands to change the behavior for this request:

Compact
gitar display:verbose         

Was this helpful? React with 👍 / 👎 | Powered by Gitar — free for open source

Comment on lines +155 to +159
protected void restoreMovedSecret(final MovedSecret moved) {
moved
.previous()
.ifPresentOrElse(
value -> upsertSecret(moved.secretId(), value), () -> deleteSecret(moved.secretId()));

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.

P1 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.

@Khairajani

Copy link
Copy Markdown
Contributor Author

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.

@Khairajani Khairajani closed this Nov 19, 2026
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.

Sftp Service connection details contain unmasked password

1 participant