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


##########
core/src/main/java/org/apache/gravitino/catalog/SchemaOperationDispatcher.java:
##########
@@ -551,10 +552,11 @@ private Pair<Schema, SchemaChange[]> 
alterExternalSchemaUnderLock(
               c -> c.doWithSchemaOps(s -> s.alterSchema(ident, 
effectiveChanges)),
               NoSuchSchemaException.class);
       alterCommitted = true;
+      writtenSecretMaterials.commit(secretManager);

Review Comment:
   On the external schema path `commit()` runs right after the external 
`alterSchema`, but the SchemaEntity is only updated afterwards, in 
`alterSchema` through `operateOnEntity`, which swallows failures. If that 
update fails after a remove or a switch to a reference, the alter still 
succeeds and the SchemaEntity keeps a URN whose version was just deleted, and 
this path reads URNs from the SchemaEntity first.
   
   Returning the holder to `alterSchema` and running the deletes only after 
`operateOnEntity` succeeded (or when there is no entity) would close it. The 
promote can stay where it is.



##########
core/src/test/java/org/apache/gravitino/secret/TestSecretManagerAlter.java:
##########
@@ -35,47 +37,201 @@ void testAlterSetSecretBindingWritesAndReturnsUrn() {
     try (SecretManager secretManager = memorySecretManager()) {
       Map<String, String> props = new HashMap<>(Map.of("jdbc-user", "root"));
       List<SecretMaterial> written = new ArrayList<>();
+      List<SecretVersion> pending = new ArrayList<>();
+      List<SecretVersion> replacedVersions = new ArrayList<>();
+      List<SecretUrn> replacedUrns = new ArrayList<>();
       String urn =
           secretManager.alterSetSecretBinding(
               props,
               "catalog",
               42L,
               "jdbc-password",
               new SecretBinding("memory", "s3cr3t"),
-              written);
+              written,
+              pending,
+              replacedVersions,
+              replacedUrns);
 
       Assertions.assertTrue(
           SecretPropertyUtils.isWriteThroughForEntity("jdbc-password", urn, 
"catalog", 42L));
       Assertions.assertEquals(urn, props.get("jdbc-password"));
       Assertions.assertEquals(1, written.size());
+      Assertions.assertTrue(pending.isEmpty());
       Assertions.assertEquals(
           "s3cr3t",
           
secretManager.getRegistry().getProvider("memory").readSecret(written.get(0).urn()));
     }
   }
 
   @Test
-  void testAlterRemovePropertyDeletesWriteThroughSecret() {
+  void testAlterRemovePropertyDefersWriteThroughSecretDeletion() {
     try (SecretManager secretManager = memorySecretManager()) {
       Map<String, String> props = new HashMap<>();
       List<SecretMaterial> written = new ArrayList<>();
+      List<SecretVersion> pending = new ArrayList<>();
+      List<SecretVersion> replacedVersions = new ArrayList<>();
+      List<SecretUrn> replacedUrns = new ArrayList<>();
       String urn =
           secretManager.alterSetSecretBinding(
-              props, "catalog", 7L, "jdbc-password", new 
SecretBinding("memory", "old"), written);
+              props,
+              "catalog",
+              7L,
+              "jdbc-password",
+              new SecretBinding("memory", "old"),
+              written,
+              pending,
+              replacedVersions,
+              replacedUrns);
 
-      secretManager.alterRemoveProperty(props, "catalog", 7L, "jdbc-password");
+      List<SecretVersion> toDelete = new ArrayList<>();
+      secretManager.alterRemoveProperty(props, "catalog", 7L, "jdbc-password", 
toDelete);
 
       Assertions.assertFalse(props.containsKey("jdbc-password"));
+      Assertions.assertEquals(1, toDelete.size());
+      Assertions.assertEquals(
+          "old",
+          
secretManager.getRegistry().getProvider("memory").readSecret(SecretUrn.parse(urn)));
+
+      SecretMaterialsHolder holder = new SecretMaterialsHolder();
+      holder.setReplacedVersions(toDelete);
+      holder.commit(secretManager);
       Assertions.assertThrows(
           IllegalArgumentException.class,
           () -> 
secretManager.getRegistry().getProvider("memory").readSecret(SecretUrn.parse(urn)));
     }
   }
 
+  @Test
+  void testFailedSameProviderRotationKeepsOldValue() {
+    try (SecretManager secretManager = memorySecretManager()) {
+      Map<String, String> props = new HashMap<>();
+      Pair<CatalogChange[], SecretMaterialsHolder> first =
+          SecretAlterChanges.prepareCatalogChanges(
+              secretManager,
+              props,
+              3L,
+              CatalogChange.setSecretBinding("jdbc-password", new 
SecretBinding("memory", "old")));
+      first.getRight().commit(secretManager);
+      String urn =
+          first.getLeft()[0] instanceof CatalogChange.SetProperty
+              ? ((CatalogChange.SetProperty) first.getLeft()[0]).getValue()
+              : null;
+      Assertions.assertNotNull(urn);
+      props.put("jdbc-password", urn);
+
+      Assertions.assertThrows(
+          IllegalArgumentException.class,
+          () ->
+              SecretAlterChanges.prepareCatalogChanges(
+                  secretManager,
+                  props,
+                  3L,
+                  CatalogChange.setSecretBinding(
+                      "jdbc-password", new SecretBinding("memory", "new")),
+                  CatalogChange.setProperty("jdbc-password", "******")));
+
+      Assertions.assertEquals(
+          "old",
+          
secretManager.getRegistry().getProvider("memory").readSecret(SecretUrn.parse(urn)));
+    }
+  }
+
+  @Test
+  void testFailedPrepareKeepsReplacedSecretReadable() {
+    try (SecretManager secretManager = memorySecretManager()) {
+      Map<String, String> props = new HashMap<>();
+      Pair<CatalogChange[], SecretMaterialsHolder> first =
+          SecretAlterChanges.prepareCatalogChanges(
+              secretManager,
+              props,
+              5L,
+              CatalogChange.setSecretBinding("jdbc-password", new 
SecretBinding("memory", "keep")));
+      first.getRight().commit(secretManager);
+      String urn = ((CatalogChange.SetProperty) first.getLeft()[0]).getValue();
+      props.put("jdbc-password", urn);
+
+      Assertions.assertThrows(
+          IllegalArgumentException.class,
+          () ->
+              SecretAlterChanges.prepareCatalogChanges(
+                  secretManager,
+                  props,
+                  5L,
+                  CatalogChange.removeProperty("jdbc-password"),
+                  CatalogChange.setProperty("other", "******")));
+
+      Assertions.assertEquals(
+          "keep",
+          
secretManager.getRegistry().getProvider("memory").readSecret(SecretUrn.parse(urn)));
+    }
+  }
+
+  @Test
+  void testRotationWritesPendingVersionAndPromotesOnCommit() {
+    try (SecretManager secretManager = memorySecretManager()) {
+      Map<String, String> props = new HashMap<>();
+      Pair<CatalogChange[], SecretMaterialsHolder> create =
+          SecretAlterChanges.prepareCatalogChanges(
+              secretManager,
+              props,
+              9L,
+              CatalogChange.setSecretBinding("jdbc-password", new 
SecretBinding("memory", "old")));
+      create.getRight().commit(secretManager);
+      String urn = ((CatalogChange.SetProperty) 
create.getLeft()[0]).getValue();
+      props.put("jdbc-password", urn);
+
+      Pair<CatalogChange[], SecretMaterialsHolder> rotate =
+          SecretAlterChanges.prepareCatalogChanges(
+              secretManager,
+              props,
+              9L,
+              CatalogChange.setSecretBinding("jdbc-password", new 
SecretBinding("memory", "new")));
+      Assertions.assertEquals(
+          "old",
+          
secretManager.getRegistry().getProvider("memory").readSecret(SecretUrn.parse(urn)));
+      Assertions.assertEquals(1, 
rotate.getRight().getPendingVersions().size());
+
+      rotate.getRight().commit(secretManager);
+      Assertions.assertEquals(
+          "new",
+          
secretManager.getRegistry().getProvider("memory").readSecret(SecretUrn.parse(urn)));
+      Assertions.assertEquals(urn, ((CatalogChange.SetProperty) 
rotate.getLeft()[0]).getValue());
+    }
+  }
+
+  @Test
+  void testRemoveAndRebindKeepsRewrittenSecretAfterCommit() {
+    try (SecretManager secretManager = memorySecretManager()) {
+      Map<String, String> props = new HashMap<>();
+      Pair<CatalogChange[], SecretMaterialsHolder> create =
+          SecretAlterChanges.prepareCatalogChanges(
+              secretManager,
+              props,
+              11L,
+              CatalogChange.setSecretBinding("jdbc-password", new 
SecretBinding("memory", "old")));
+      create.getRight().commit(secretManager);
+      String urn = ((CatalogChange.SetProperty) 
create.getLeft()[0]).getValue();
+      props.put("jdbc-password", urn);
+
+      Pair<CatalogChange[], SecretMaterialsHolder> rebind =
+          SecretAlterChanges.prepareCatalogChanges(
+              secretManager,
+              props,
+              11L,
+              CatalogChange.removeProperty("jdbc-password"),
+              CatalogChange.setSecretBinding(
+                  "jdbc-password", new SecretBinding("memory", "rebound")));
+      rebind.getRight().commit(secretManager);

Review Comment:
   The new tests call `holder.commit()` by hand and fake the entity commit with 
`props.put`, and every failure case fails inside prepare on a `"******"` value. 
So nothing covers `rollback()`, `copyFrom()`, the five call sites, a store 
update failing after prepare, a failed promote, or the "a later alter on the 
same URN is not wiped" claim, and the remove-and-rebind test only checks the 
success path.
   
   Could you add dispatcher-level cases, for example through 
`CatalogManager.alterCatalog` with a store update that throws after prepare, 
for a rotation, a remove, and a remove plus rebind?



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