This is an automated email from the ASF dual-hosted git repository.

roryqi pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/gravitino.git


The following commit(s) were added to refs/heads/main by this push:
     new 8bca65935b [#15056] fix(secret): Reject empty binding plaintext and 
fail fast on missing refs (#15057)
8bca65935b is described below

commit 8bca65935b6663ea024d4c6e7c007474221f5e2d
Author: MaSai <[email protected]>
AuthorDate: Wed Nov 18 19:01:55 2026 +0800

    [#15056] fix(secret): Reject empty binding plaintext and fail fast on 
missing refs (#15057)
    
    ### What changes were proposed in this pull request?
    
    - Reject blank `secretBindings` plaintext in `SecretBinding` (and alter
    `setSecretBinding` validation).
    - On catalog / schema / fileset create, call `toPlaintextProperties`
    after staging secrets and before the backend probe, so unreadable
    `secretReferences` fail with `IllegalArgumentException` instead of a
    later connection failure.
    
    ### Why are the changes needed?
    
    Fixes #15056
    
    Empty binding plaintext was accepted into the write-through path.
    Missing external references were masked during create-time catalog init,
    so JDBC-like catalogs often failed with `ConnectionFailedException`
    instead of a clear secret-resolution error.
    
    ### Does this PR introduce _any_ user-facing change?
    
    Yes. Create/alter with empty `secretBindings.plaintext` is rejected.
    Create with an unreadable `secretReferences` target fails earlier with
    `IllegalArgumentException`.
    
    ### How was this patch tested?
    
    - `./gradlew :api:test --tests
    org.apache.gravitino.secret.TestSecretBinding :core:test --tests
    org.apache.gravitino.secret.TestSecretManager --tests
    'org.apache.gravitino.catalog.TestCatalogManager.testCreateRejects*'
    --tests org.apache.gravitino.catalog.TestCatalogManager.testSecrets
    -PskipITs -PskipDockerTests=true`
    
    Made with [Cursor](https://cursor.com)
    
    ---------
    
    Co-authored-by: Cursor <[email protected]>
---
 .../org/apache/gravitino/secret/SecretBinding.java |  4 +-
 .../apache/gravitino/secret/TestSecretBinding.java |  1 +
 .../apache/gravitino/catalog/CatalogManager.java   |  1 +
 .../catalog/FilesetOperationDispatcher.java        |  1 +
 .../catalog/SchemaOperationDispatcher.java         |  1 +
 .../gravitino/secret/SecretAlterChanges.java       | 41 +++++++++------
 .../org/apache/gravitino/secret/SecretManager.java | 49 +++++++++++++++--
 .../gravitino/secret/SecretPropertyUtils.java      |  2 +-
 .../gravitino/catalog/TestCatalogManager.java      | 61 ++++++++++++++++++++++
 .../apache/gravitino/secret/TestSecretManager.java | 45 ++++++++++++++++
 10 files changed, 183 insertions(+), 23 deletions(-)

diff --git a/api/src/main/java/org/apache/gravitino/secret/SecretBinding.java 
b/api/src/main/java/org/apache/gravitino/secret/SecretBinding.java
index 01c6e2be05..43441cf84a 100644
--- a/api/src/main/java/org/apache/gravitino/secret/SecretBinding.java
+++ b/api/src/main/java/org/apache/gravitino/secret/SecretBinding.java
@@ -38,11 +38,11 @@ public final class SecretBinding {
    * Creates a write-through binding.
    *
    * @param provider registered provider instance name
-   * @param plaintext plaintext secret to write through
+   * @param plaintext non-blank plaintext secret to write through
    */
   public SecretBinding(String provider, String plaintext) {
     Preconditions.checkArgument(StringUtils.isNotBlank(provider), "provider 
must not be blank");
-    Preconditions.checkArgument(plaintext != null, "plaintext must not be 
null");
+    Preconditions.checkArgument(StringUtils.isNotBlank(plaintext), "plaintext 
must not be blank");
     this.provider = provider;
     this.plaintext = plaintext;
   }
diff --git 
a/api/src/test/java/org/apache/gravitino/secret/TestSecretBinding.java 
b/api/src/test/java/org/apache/gravitino/secret/TestSecretBinding.java
index 497b24dd8c..13aa5ac4e4 100644
--- a/api/src/test/java/org/apache/gravitino/secret/TestSecretBinding.java
+++ b/api/src/test/java/org/apache/gravitino/secret/TestSecretBinding.java
@@ -32,6 +32,7 @@ public class TestSecretBinding {
     Assertions.assertFalse(binding.toString().contains("s3cr3t"));
 
     Assertions.assertThrows(IllegalArgumentException.class, () -> new 
SecretBinding("", "x"));
+    Assertions.assertThrows(IllegalArgumentException.class, () -> new 
SecretBinding("memory", ""));
     Assertions.assertThrows(
         IllegalArgumentException.class, () -> new SecretBinding("memory", 
null));
   }
diff --git 
a/core/src/main/java/org/apache/gravitino/catalog/CatalogManager.java 
b/core/src/main/java/org/apache/gravitino/catalog/CatalogManager.java
index 772c0160a7..d48b633c57 100644
--- a/core/src/main/java/org/apache/gravitino/catalog/CatalogManager.java
+++ b/core/src/main/java/org/apache/gravitino/catalog/CatalogManager.java
@@ -888,6 +888,7 @@ public class CatalogManager implements CatalogDispatcher, 
Closeable {
             checkOpen();
             checkMetalake(metalakeIdent, store);
             // Initialization resolves secret URNs, but must not publish the 
entity yet.
+            secretManager.validateProposedSecrets(mergedConfig, 
secretPlaintexts);
             SecretVersionChanges stagedSecrets =
                 secretManager.stageSecrets(null, mergedConfig, "catalog", uid, 
secretPlaintexts);
             AtomicBoolean entityStored = new AtomicBoolean(false);
diff --git 
a/core/src/main/java/org/apache/gravitino/catalog/FilesetOperationDispatcher.java
 
b/core/src/main/java/org/apache/gravitino/catalog/FilesetOperationDispatcher.java
index 25190deb85..2553048b48 100644
--- 
a/core/src/main/java/org/apache/gravitino/catalog/FilesetOperationDispatcher.java
+++ 
b/core/src/main/java/org/apache/gravitino/catalog/FilesetOperationDispatcher.java
@@ -202,6 +202,7 @@ public class FilesetOperationDispatcher extends 
OperationDispatcher implements F
     // Write secrets before create: paths that resolve URNs (e.g. 
mergeUpLevelConfigurations for FS
     // mkdir, catalog createBaseCatalog) require secrets to exist first. Roll 
back only if the
     // catalog create fails; once it returns, the fileset references these 
secrets.
+    secretManager.validateProposedSecrets(entityProperties, secretPlaintexts);
     SecretVersionChanges stagedSecrets =
         secretManager.stageSecrets(null, entityProperties, "fileset", uid, 
secretPlaintexts);
     AtomicBoolean createdInCatalog = new AtomicBoolean();
diff --git 
a/core/src/main/java/org/apache/gravitino/catalog/SchemaOperationDispatcher.java
 
b/core/src/main/java/org/apache/gravitino/catalog/SchemaOperationDispatcher.java
index 5325f6dab9..fea51975d4 100644
--- 
a/core/src/main/java/org/apache/gravitino/catalog/SchemaOperationDispatcher.java
+++ 
b/core/src/main/java/org/apache/gravitino/catalog/SchemaOperationDispatcher.java
@@ -199,6 +199,7 @@ public class SchemaOperationDispatcher extends 
OperationDispatcher implements Sc
     // mergeUpLevelConfigurations, catalog createBaseCatalog) require secrets 
to exist first.
     // Roll back secrets only if the catalog has not created the schema; once 
it has, the schema may
     // reference them even if a later step fails.
+    secretManager.validateProposedSecrets(entityProperties, secretPlaintexts);
     SecretVersionChanges stagedSecrets =
         secretManager.stageSecrets(null, entityProperties, "schema", uid, 
secretPlaintexts);
     AtomicBoolean createdInCatalog = new AtomicBoolean();
diff --git 
a/core/src/main/java/org/apache/gravitino/secret/SecretAlterChanges.java 
b/core/src/main/java/org/apache/gravitino/secret/SecretAlterChanges.java
index 6b604deb6a..e839488fa6 100644
--- a/core/src/main/java/org/apache/gravitino/secret/SecretAlterChanges.java
+++ b/core/src/main/java/org/apache/gravitino/secret/SecretAlterChanges.java
@@ -98,11 +98,15 @@ public final class SecretAlterChanges {
       }
     }
     // Without an id the checks above let no change write or release a 
write-through secret.
-    SecretVersionChanges staged =
-        entityId == null
-            ? SecretVersionChanges.EMPTY
-            : secretManager.stageSecrets(
-                currentProperties, properties, "catalog", entityId, 
plaintexts);
+    SecretVersionChanges staged;
+    if (entityId == null) {
+      staged = SecretVersionChanges.EMPTY;
+    } else {
+      secretManager.validateProposedSecrets(properties, plaintexts);
+      staged =
+          secretManager.stageSecrets(
+              currentProperties, properties, "catalog", entityId, plaintexts);
+    }
     return Pair.of(out.toArray(new CatalogChange[0]), staged);
   }
 
@@ -213,11 +217,14 @@ public final class SecretAlterChanges {
       }
     }
     // Without an id the checks above let no change write or release a 
write-through secret.
-    SecretVersionChanges staged =
-        entityId == null
-            ? SecretVersionChanges.EMPTY
-            : secretManager.stageSecrets(
-                currentProperties, properties, "schema", entityId, plaintexts);
+    SecretVersionChanges staged;
+    if (entityId == null) {
+      staged = SecretVersionChanges.EMPTY;
+    } else {
+      secretManager.validateProposedSecrets(properties, plaintexts);
+      staged =
+          secretManager.stageSecrets(currentProperties, properties, "schema", 
entityId, plaintexts);
+    }
     return Pair.of(out.toArray(new SchemaChange[0]), staged);
   }
 
@@ -277,11 +284,15 @@ public final class SecretAlterChanges {
       }
     }
     // Without an id the checks above let no change write or release a 
write-through secret.
-    SecretVersionChanges staged =
-        entityId == null
-            ? SecretVersionChanges.EMPTY
-            : secretManager.stageSecrets(
-                currentProperties, properties, "fileset", entityId, 
plaintexts);
+    SecretVersionChanges staged;
+    if (entityId == null) {
+      staged = SecretVersionChanges.EMPTY;
+    } else {
+      secretManager.validateProposedSecrets(properties, plaintexts);
+      staged =
+          secretManager.stageSecrets(
+              currentProperties, properties, "fileset", entityId, plaintexts);
+    }
     return Pair.of(out.toArray(new FilesetChange[0]), staged);
   }
 }
diff --git a/core/src/main/java/org/apache/gravitino/secret/SecretManager.java 
b/core/src/main/java/org/apache/gravitino/secret/SecretManager.java
index 15140de45d..34a12b9e78 100644
--- a/core/src/main/java/org/apache/gravitino/secret/SecretManager.java
+++ b/core/src/main/java/org/apache/gravitino/secret/SecretManager.java
@@ -126,11 +126,12 @@ public class SecretManager implements Closeable {
    * Checks secret keys and puts reference / write-through URN strings into 
{@code
    * targetProperties}.
    *
-   * <p>Callers typically validate {@code targetProperties}, then pass the 
returned plaintexts to
-   * {@link #stageSecrets}, and call {@link #commitSecrets} after the entity 
is created or {@link
-   * #rollbackSecrets(SecretVersionChanges)} on failure. {@code properties} is 
used only for key
-   * uniqueness checks (e.g. the original request map); {@code 
targetProperties} is the mutable map
-   * that will be stored (may already contain merged catalog conf).
+   * <p>Callers typically invoke {@link #validateProposedSecrets} on {@code 
targetProperties} and
+   * the returned plaintexts, then {@link #stageSecrets}, and call {@link 
#commitSecrets} after the
+   * entity is created or {@link #rollbackSecrets(SecretVersionChanges)} on 
failure. {@code
+   * properties} is used only for key uniqueness checks (e.g. the original 
request map); {@code
+   * targetProperties} is the mutable map that will be stored (may already 
contain merged catalog
+   * conf).
    *
    * @param properties properties used for key uniqueness checks (may be null)
    * @param targetProperties mutable properties that receive URN values (may 
be null only when both
@@ -359,6 +360,37 @@ public class SecretManager implements Closeable {
     deleteSecretVersions(versions);
   }
 
+  /**
+   * Validates the proposed secret property view for a create or alter batch.
+   *
+   * <p>Checks each write-through plaintext (non-blank, not the masked 
placeholder), then overlays
+   * {@code plaintexts} onto a copy of {@code finalProperties} and resolves 
that view with {@link
+   * #toPlaintextProperties(Map)}. New bindings are therefore checked as 
plaintext (same idea as
+   * {@link SecretAlterChanges#prepareCatalogChangesForTest}) while references 
and any other secret
+   * URNs are read through their providers. Callers should invoke this before 
{@link #stageSecrets}
+   * so a blank binding or missing reference fails without writing secret 
versions.
+   *
+   * @param finalProperties entity properties after the create or alter (URNs 
for secrets; may be
+   *     null when there are no secrets)
+   * @param plaintexts property key → plaintext that {@link #stageSecrets} 
will write
+   * @throws IllegalArgumentException if a plaintext is blank or masked, or a 
secret cannot be read
+   */
+  public void validateProposedSecrets(
+      @Nullable Map<String, String> finalProperties, Map<String, String> 
plaintexts) {
+    Preconditions.checkArgument(plaintexts != null, "plaintexts must not be 
null");
+    if ((finalProperties == null || finalProperties.isEmpty()) && 
plaintexts.isEmpty()) {
+      return;
+    }
+    Preconditions.checkArgument(
+        finalProperties != null, "finalProperties must not be null when 
secrets are present");
+    for (String plaintext : plaintexts.values()) {
+      SecretPropertyUtils.validateAlterSecretBindingPlaintext(plaintext);
+    }
+    Map<String, String> view = new HashMap<>(finalProperties);
+    view.putAll(plaintexts);
+    toPlaintextProperties(view);
+  }
+
   /**
    * Stages an entity create or alter batch: writes its plaintexts as new 
versions and finds the
    * versions it releases.
@@ -367,6 +399,9 @@ public class SecretManager implements Closeable {
    * several times is treated by its net effect. A URN that has a current 
version keeps it readable
    * until {@link #commitSecrets}. If a write fails, the versions already 
written are deleted.
    *
+   * <p>Callers must invoke {@link #validateProposedSecrets} first. This 
method only writes and
+   * records versions.
+   *
    * @param startProperties entity properties before the alter, or {@code 
null} for a create
    * @param finalProperties entity properties after the create or alter
    * @param entityType {@code catalog}, {@code schema}, or {@code fileset}
@@ -674,6 +709,10 @@ public class SecretManager implements Closeable {
     return resolvePlaintextProperties(properties, false);
   }
 
+  /**
+   * Replaces secret URN values with plaintext, or with {@link 
HiddenPropertyMaskUtils#MASKED_VALUE}
+   * when {@code failOnMissing} is false and a secret cannot be read.
+   */
   private Map<String, String> resolvePlaintextProperties(
       @Nullable Map<String, String> properties, boolean failOnMissing) {
     if (properties == null || properties.isEmpty()) {
diff --git 
a/core/src/main/java/org/apache/gravitino/secret/SecretPropertyUtils.java 
b/core/src/main/java/org/apache/gravitino/secret/SecretPropertyUtils.java
index f691c935c4..a9dc4415f1 100644
--- a/core/src/main/java/org/apache/gravitino/secret/SecretPropertyUtils.java
+++ b/core/src/main/java/org/apache/gravitino/secret/SecretPropertyUtils.java
@@ -294,7 +294,7 @@ public final class SecretPropertyUtils {
    * @param plaintext the plaintext from the binding
    */
   public static void validateAlterSecretBindingPlaintext(String plaintext) {
-    Preconditions.checkArgument(plaintext != null, "plaintext must not be 
null");
+    Preconditions.checkArgument(StringUtils.isNotBlank(plaintext), "plaintext 
must not be blank");
     Preconditions.checkArgument(
         !"******".equals(plaintext),
         "setSecretBinding plaintext must not be the masked placeholder 
******");
diff --git 
a/core/src/test/java/org/apache/gravitino/catalog/TestCatalogManager.java 
b/core/src/test/java/org/apache/gravitino/catalog/TestCatalogManager.java
index 066e87358d..8f69819389 100644
--- a/core/src/test/java/org/apache/gravitino/catalog/TestCatalogManager.java
+++ b/core/src/test/java/org/apache/gravitino/catalog/TestCatalogManager.java
@@ -180,6 +180,19 @@ public class TestCatalogManager {
     public void close() {}
   }
 
+  /** Reference provider that can build URNs but always fails on read (missing 
secret). */
+  public static class TestUnreadableReferenceSecretsProvider extends 
TestReferenceSecretsProvider {
+    @Override
+    public String type() {
+      return "test-unreadable-reference";
+    }
+
+    @Override
+    public String readSecret(SecretUrn urn) {
+      throw new IllegalArgumentException("Secret not found for URN: " + urn);
+    }
+  }
+
   private static CatalogManager catalogManager;
 
   private static EntityStore entityStore;
@@ -2918,6 +2931,36 @@ public class TestCatalogManager {
     }
   }
 
+  @Test
+  void testCreateRejectsEmptySecretBindingPlaintext() {
+    Assertions.assertThrows(IllegalArgumentException.class, () -> new 
SecretBinding("memory", ""));
+  }
+
+  @Test
+  void testCreateRejectsUnreadableSecretReference() throws Exception {
+    try (SecretManager secrets = unreadableReferenceSecretManager();
+        CatalogManager manager =
+            new CatalogManager(config, entityStore, new RandomIdGenerator(), 
secrets)) {
+      NameIdentifier ident = NameIdentifier.of("metalake", 
"secret_missing_ref");
+      IllegalArgumentException ex =
+          Assertions.assertThrows(
+              IllegalArgumentException.class,
+              () ->
+                  manager.createCatalog(
+                      ident,
+                      Catalog.Type.RELATIONAL,
+                      provider,
+                      "comment",
+                      catalogProps(),
+                      Map.of(),
+                      Map.of(
+                          PROPERTY_KEY4,
+                          new SecretReference("unreadable", Map.of("path", 
"does-not-exist")))));
+      Assertions.assertTrue(ex.getMessage().contains("Secret not found"), 
ex.getMessage());
+      Assertions.assertFalse(manager.catalogExists(ident));
+    }
+  }
+
   @Test
   void testCatalogManageableWhenSecretMissing() throws Exception {
     try (SecretManager secrets = memorySecretManager();
@@ -3075,6 +3118,24 @@ public class TestCatalogManager {
     return new SecretManager(c);
   }
 
+  private static SecretManager unreadableReferenceSecretManager() {
+    Config c = new Config(false) {};
+    Properties p = new Properties();
+    p.setProperty(SecretProviderRegistry.GRAVITINO_SECRET_PROVIDERS, 
"memory,unreadable");
+    p.setProperty(
+        SecretProviderRegistry.GRAVITINO_SECRET_PROVIDER_PREFIX
+            + "memory."
+            + SecretProviderRegistry.CLASS_NAME,
+        InMemorySecretsProvider.class.getName());
+    p.setProperty(
+        SecretProviderRegistry.GRAVITINO_SECRET_PROVIDER_PREFIX
+            + "unreadable."
+            + SecretProviderRegistry.CLASS_NAME,
+        TestUnreadableReferenceSecretsProvider.class.getName());
+    c.loadFromProperties(p);
+    return new SecretManager(c);
+  }
+
   /** Catalog operations whose backend always holds one user-created schema. */
   public static class SingleSchemaCatalogOperations extends 
TestCatalogOperations {
     static final String SCHEMA_NAME = "existing_schema";
diff --git 
a/core/src/test/java/org/apache/gravitino/secret/TestSecretManager.java 
b/core/src/test/java/org/apache/gravitino/secret/TestSecretManager.java
index 7c8beea6fe..5c50a41648 100644
--- a/core/src/test/java/org/apache/gravitino/secret/TestSecretManager.java
+++ b/core/src/test/java/org/apache/gravitino/secret/TestSecretManager.java
@@ -97,6 +97,51 @@ public class TestSecretManager {
     }
   }
 
+  @Test
+  void testValidateProposedSecretsRejectsBlankPlaintext() {
+    try (SecretManager sm = memorySecretManager()) {
+      Map<String, String> props = new HashMap<>();
+      List<SecretUrn> urns = sm.buildSecretBindingUrns("catalog", 42L, 
BINDINGS);
+      SecretPropertyUtils.putSecretUrns(props, urns);
+      IllegalArgumentException ex =
+          Assertions.assertThrows(
+              IllegalArgumentException.class,
+              () -> sm.validateProposedSecrets(props, Map.of("jdbc-password", 
"")));
+      Assertions.assertTrue(ex.getMessage().contains("plaintext must not be 
blank"));
+      Assertions.assertThrows(IllegalArgumentException.class, () -> 
sm.readSecret(urns.get(0)));
+    }
+  }
+
+  @Test
+  void testValidateProposedSecretsRejectsMissingReference() {
+    try (SecretManager sm = memorySecretManager()) {
+      // memory provider cannot build reference URNs; use a write-through URN 
that was never
+      // written.
+      Map<String, String> props = new HashMap<>();
+      List<SecretUrn> urns = sm.buildSecretBindingUrns("catalog", 42L, 
BINDINGS);
+      SecretPropertyUtils.putSecretUrns(props, urns);
+      IllegalArgumentException ex =
+          Assertions.assertThrows(
+              IllegalArgumentException.class, () -> 
sm.validateProposedSecrets(props, Map.of()));
+      Assertions.assertTrue(
+          ex.getMessage().contains("Secret not found") || 
ex.getMessage().contains("not found"),
+          ex.getMessage());
+    }
+  }
+
+  @Test
+  void testValidateProposedSecretsAcceptsBindingOverlay() {
+    try (SecretManager sm = memorySecretManager()) {
+      Map<String, String> props = new HashMap<>();
+      Map<String, String> plaintexts =
+          sm.assembleSecretPlaintexts(Map.of(), props, "catalog", 42L, 
BINDINGS, Map.of());
+      Assertions.assertDoesNotThrow(() -> sm.validateProposedSecrets(props, 
plaintexts));
+      Assertions.assertThrows(
+          IllegalArgumentException.class,
+          () -> sm.readSecret(SecretUrn.parse(props.get("jdbc-password"))));
+    }
+  }
+
   @Test
   void testToPlaintextProperties() {
     try (SecretManager sm = memorySecretManager()) {

Reply via email to