LuciferYang commented on code in PR #14926:
URL: https://github.com/apache/gravitino/pull/14926#discussion_r4546643826


##########
core/src/main/java/org/apache/gravitino/secret/SecretMaterialsHolder.java:
##########
@@ -39,4 +46,85 @@ public List<SecretMaterial> get() {
   public void set(List<SecretMaterial> materials) {
     this.materials = materials;
   }
+
+  /**
+   * Returns pending versions written without changing the current readable 
value. Promoted after
+   * commit; deleted on rollback.
+   */
+  public List<SecretVersion> getPendingVersions() {
+    return pendingVersions;
+  }
+
+  /** Records pending secret versions. */
+  public void setPendingVersions(List<SecretVersion> pendingVersions) {
+    this.pendingVersions = pendingVersions;
+  }
+
+  /**
+   * Returns versions replaced or removed while the URN may be rewritten in 
the same or a later
+   * alter. Deleted with {@code deleteSecretVersion} after commit so a newer 
current version is
+   * kept.
+   */
+  public List<SecretVersion> getReplacedVersions() {
+    return replacedVersions;
+  }
+
+  /** Records replaced secret versions for post-commit deletion. */
+  public void setReplacedVersions(List<SecretVersion> replacedVersions) {
+    this.replacedVersions = replacedVersions;
+  }
+
+  /**
+   * Returns write-through URNs replaced by a different URN (for example a 
cross-provider bind).
+   * Deleted with {@code deleteSecret} after commit.
+   */
+  public List<SecretUrn> getReplacedUrns() {
+    return replacedUrns;
+  }
+
+  /** Records replaced write-through URNs for post-commit deletion. */
+  public void setReplacedUrns(List<SecretUrn> replacedUrns) {
+    this.replacedUrns = replacedUrns;
+  }
+
+  /**
+   * Copies bookkeeping from a prepare result into this holder (for use inside 
{@code store.update}
+   * lambdas).
+   *
+   * @param prepared holder returned by prepare
+   */
+  public void copyFrom(SecretMaterialsHolder prepared) {
+    this.materials = prepared.materials;
+    this.pendingVersions = prepared.pendingVersions;
+    this.replacedVersions = prepared.replacedVersions;
+    this.replacedUrns = prepared.replacedUrns;
+  }
+
+  /**
+   * After a successful alter commit: promotes pending versions to current, 
then deletes replaced
+   * versions and URNs. Best effort; must not fail the already-committed alter.
+   *
+   * @param secretManager secret manager
+   */
+  public void commit(SecretManager secretManager) {
+    secretManager.promoteSecretVersions(pendingVersions);

Review Comment:
   For a same-URN rotation the entity property doesn't change, so the real 
switch is the promote in `commit()`. That promote is best effort, and the next 
line deletes the replaced version anyway. If the promote fails, the alter still 
returns success and the old version, which is still current, is deleted, so the 
entity points at a secret that can't be read. The entity version check also 
can't order promotes across servers, so an earlier alter that promotes late 
overwrites a later one.
   
   At minimum, delete a replaced version only after the promote for its URN 
succeeded. A sturdier option is to store the version id with the URN and read 
by version, which Vault KV v2 (`?version=N`), AWS (`VersionId`) and GCP 
(`versions/N`) support natively. The entity commit then becomes the switch, its 
version check orders concurrent rotations, and no promote is needed.



##########
common/src/main/java/org/apache/gravitino/secret/SecretProvider.java:
##########
@@ -53,12 +53,67 @@ public interface SecretProvider {
    * SecretConstants#ATTR_ENTITY_TYPE}, {@link 
SecretConstants#ATTR_ENTITY_ID}, and {@link
    * SecretConstants#ATTR_PROPERTY_KEY}.
    *
+   * <p>For versioned providers this creates a new version and makes it the 
current readable value
+   * (create path). Prefer {@link #writeSecretVersion} when an alter must keep 
the previous current
+   * value readable until commit.
+   *
    * @param plaintext the secret plaintext
    * @param attributes provider-specific write attributes
    * @return the secret URN
    */
   SecretUrn writeSecret(String plaintext, Map<String, String> attributes);
 
+  /**
+   * Writes a new secret version under the URN built from {@code attributes} 
without changing the
+   * currently readable value when material already exists.
+   *
+   * <p>Aligns with multi-version secret stores (Vault KV v2, AWS Secrets 
Manager, GCP Secret
+   * Manager): the logical name / URN stays fixed; each write allocates a 
version id. When the URN
+   * has no material yet, the new version becomes current.
+   *
+   * @param plaintext the secret plaintext
+   * @param attributes provider-specific write attributes
+   * @return the URN and the new version id
+   * @throws UnsupportedOperationException if this provider does not support 
versions
+   */
+  default SecretVersion writeSecretVersion(String plaintext, Map<String, 
String> attributes) {

Review Comment:
   The four version methods default to `UnsupportedOperationException`, but 
every same-provider rotation, write-through `setProperty` and `removeProperty` 
calls them, so a provider that implements only the original SPI can no longer 
rotate or remove the secrets it wrote. In practice the methods are mandatory.
   
   The pending/promote model also maps natively only to AWS. In Vault KV v2 
every write becomes the version an unversioned read returns, and GCP's `latest` 
is always the newest version, so both would need a provider-side pointer. 
Reading a pinned version is native in all three.
   
   Since the SPI isn't released yet, could the contract be made explicit now: 
abstract methods, or a capability flag that keeps the old overwrite/delete path 
for providers without versions? The "aligns with Vault KV v2, AWS Secrets 
Manager, GCP Secret Manager" wording in the Javadoc and the design doc needs 
the same correction.



##########
core/src/main/java/org/apache/gravitino/secret/SecretMaterialsHolder.java:
##########
@@ -39,4 +46,85 @@ public List<SecretMaterial> get() {
   public void set(List<SecretMaterial> materials) {
     this.materials = materials;
   }
+
+  /**
+   * Returns pending versions written without changing the current readable 
value. Promoted after
+   * commit; deleted on rollback.
+   */
+  public List<SecretVersion> getPendingVersions() {
+    return pendingVersions;
+  }
+
+  /** Records pending secret versions. */
+  public void setPendingVersions(List<SecretVersion> pendingVersions) {
+    this.pendingVersions = pendingVersions;
+  }
+
+  /**
+   * Returns versions replaced or removed while the URN may be rewritten in 
the same or a later
+   * alter. Deleted with {@code deleteSecretVersion} after commit so a newer 
current version is
+   * kept.
+   */
+  public List<SecretVersion> getReplacedVersions() {
+    return replacedVersions;
+  }
+
+  /** Records replaced secret versions for post-commit deletion. */
+  public void setReplacedVersions(List<SecretVersion> replacedVersions) {
+    this.replacedVersions = replacedVersions;
+  }
+
+  /**
+   * Returns write-through URNs replaced by a different URN (for example a 
cross-provider bind).
+   * Deleted with {@code deleteSecret} after commit.
+   */
+  public List<SecretUrn> getReplacedUrns() {
+    return replacedUrns;
+  }
+
+  /** Records replaced write-through URNs for post-commit deletion. */
+  public void setReplacedUrns(List<SecretUrn> replacedUrns) {
+    this.replacedUrns = replacedUrns;
+  }
+
+  /**
+   * Copies bookkeeping from a prepare result into this holder (for use inside 
{@code store.update}
+   * lambdas).
+   *
+   * @param prepared holder returned by prepare
+   */
+  public void copyFrom(SecretMaterialsHolder prepared) {
+    this.materials = prepared.materials;
+    this.pendingVersions = prepared.pendingVersions;
+    this.replacedVersions = prepared.replacedVersions;
+    this.replacedUrns = prepared.replacedUrns;
+  }
+
+  /**
+   * After a successful alter commit: promotes pending versions to current, 
then deletes replaced
+   * versions and URNs. Best effort; must not fail the already-committed alter.
+   *
+   * @param secretManager secret manager
+   */
+  public void commit(SecretManager secretManager) {
+    secretManager.promoteSecretVersions(pendingVersions);
+    secretManager.rollbackSecretVersions(replacedVersions);
+    for (SecretUrn urn : replacedUrns) {
+      try {
+        
secretManager.getRegistry().getProvider(urn.providerName()).deleteSecret(urn);

Review Comment:
   Replaced versions are now deleted by id so that a later alter writing the 
same URN keeps its value, but `replacedUrns` still goes through `deleteSecret`, 
which drops every version. yuqi1129's interleaving from #13440 still works on 
these paths: A switches P to a reference and commits, B binds P back to the 
same provider (writing into the same URN) and commits, then A's commit deletes 
the URN and B's entity references nothing.
   
   Collecting the current version of the replaced URN during prepare and 
deleting that version, as the rotation path already does, would close it.



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