lasdf1234 commented on code in PR #14954:
URL: https://github.com/apache/gravitino/pull/14954#discussion_r4560846839


##########
core/src/main/java/org/apache/gravitino/secret/SecretManager.java:
##########
@@ -323,6 +324,209 @@ public void rollbackSecrets(List<SecretMaterial> 
secretMaterials) {
     deleteSecrets(urns);
   }
 
+  /**
+   * Makes the versions an alter wrote current and deletes the versions it 
replaced.
+   *
+   * <p>Call only after the entity alter committed. Failures are logged and do 
not fail the
+   * committed alter. If making a written version current fails and the 
provider then reports it as
+   * current, the previous version is deleted; otherwise the written version 
is deleted. If the
+   * current version cannot be looked up, both versions are kept. Running the 
same change again
+   * writes and promotes a new version.
+   *
+   * @param prepared secrets prepared by a {@code 
SecretAlterChanges.prepare*Changes} method
+   */
+  public void commitSecrets(PreparedSecrets prepared) {
+    Preconditions.checkArgument(prepared != null, "prepared must not be null");
+    for (PreparedSecrets.WrittenVersion written : prepared.written()) {
+      // Without a previous version the write already made the new version 
current.
+      if (written.previousId() == null) {
+        continue;
+      }
+      SecretUrn urn = written.version().urn();
+      SecretProvider provider = null;
+      try {
+        provider = registry.getProvider(urn.providerName());
+        provider.promoteSecretVersion(urn, written.version().id());
+      } catch (RuntimeException e) {
+        LOG.error(
+            "Failed to make version {} of secret {} current after the alter 
committed",
+            written.version().id(),
+            urn,
+            e);
+        if (provider != null) {
+          deleteUnusedVersionAfterFailedPromote(provider, written);
+        }
+        continue;
+      }
+      try {
+        provider.deleteSecretVersion(urn, written.previousId());
+      } catch (RuntimeException e) {
+        LOG.warn("Failed to delete replaced version {} of secret {}", 
written.previousId(), urn, e);
+      }
+    }
+    deleteSecretVersions(prepared.released());
+  }
+
+  /**
+   * Deletes the version a failed promote left unused. The promote may have 
taken effect before it
+   * failed, so the version that is current now is kept; if the current 
version cannot be looked up,
+   * both versions are kept.
+   */
+  private static void deleteUnusedVersionAfterFailedPromote(
+      SecretProvider provider, PreparedSecrets.WrittenVersion written) {
+    SecretUrn urn = written.version().urn();
+    String writtenId = written.version().id();
+    String current;
+    try {
+      current = provider.currentSecretVersion(urn);
+    } catch (RuntimeException e) {
+      LOG.warn(
+          "Could not look up the current version of secret {} after a failed 
promote; keeping"
+              + " versions {} and {}",
+          urn,
+          written.previousId(),
+          writtenId,
+          e);
+      return;
+    }
+    String unused;
+    if (writtenId.equals(current)) {
+      LOG.info("Version {} of secret {} is current although its promote 
failed", writtenId, urn);
+      unused = written.previousId();
+    } else {
+      unused = writtenId;
+    }
+    try {
+      provider.deleteSecretVersion(urn, unused);
+    } catch (RuntimeException e) {
+      LOG.warn("Failed to delete unused version {} of secret {}", unused, urn, 
e);
+    }
+  }
+
+  /**
+   * Best-effort delete of the versions an alter wrote, after the entity alter 
failed.
+   *
+   * <p>The versions that were current before the alter are kept, so the 
entity's own write-through
+   * secrets keep the values it still references. A secret that {@link 
#alterSetProperty} rewrote

Review Comment:
   **Low (design-documented consistency gap) — `alterSetProperty` on a 
non-owned URN writes immediately.**
   
   This rollback note is accurate: foreign / non–entity-owned URN rewrites call 
`writeSecret` right away and are **not** undone here. Create rejects raw URNs 
via `checkSecretKeys`; alter can still mutate another entity’s secret through 
`setProperty`.
   
   For Gravitino’s “don’t poke other entities’ secrets” posture: call this out 
in user-facing docs (not only the design doc), or reject rewriting foreign 
write-through URNs the same way create rejects raw URNs.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to