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

yuqi4733 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 16f12b4143 [#8386] fix(common): Add precodition to JdbcUrlUtils.java 
to check driver and url not null (#8405)
16f12b4143 is described below

commit 16f12b4143e5a0a7af32e7aeb358e91ee97b0c2f
Author: Lord of Abyss <[email protected]>
AuthorDate: Wed Sep 10 11:36:54 2025 +0800

    [#8386] fix(common): Add precodition to JdbcUrlUtils.java to check driver 
and url not null (#8405)
    
    ### What changes were proposed in this pull request?
    
    Add precodition to JdbcUrlUtils.java to check driver and url not null
    
    ### Why are the changes needed?
    
    Fix: #8386
    
    ### Does this PR introduce _any_ user-facing change?
    
    No.
    
    ### How was this patch tested?
    
    local test.
    
    ---------
    
    Co-authored-by: Justin Mclean <[email protected]>
---
 .../org/apache/gravitino/utils/JdbcUrlUtils.java   | 19 ++++++++-------
 .../apache/gravitino/utils/TestJdbcUrlUtils.java   | 27 +++++++++++++++++++++-
 2 files changed, 37 insertions(+), 9 deletions(-)

diff --git a/common/src/main/java/org/apache/gravitino/utils/JdbcUrlUtils.java 
b/common/src/main/java/org/apache/gravitino/utils/JdbcUrlUtils.java
index eec8c44ec0..04dab3e1dc 100644
--- a/common/src/main/java/org/apache/gravitino/utils/JdbcUrlUtils.java
+++ b/common/src/main/java/org/apache/gravitino/utils/JdbcUrlUtils.java
@@ -19,10 +19,12 @@
 
 package org.apache.gravitino.utils;
 
+import com.google.common.base.Preconditions;
 import java.net.URLDecoder;
 import java.util.Arrays;
 import java.util.List;
 import java.util.Map;
+import org.apache.commons.lang3.StringUtils;
 import org.apache.gravitino.exceptions.GravitinoRuntimeException;
 
 /**
@@ -68,18 +70,19 @@ public class JdbcUrlUtils {
    * @param all the JDBC configuration properties
    */
   public static void validateJdbcConfig(String driver, String url, Map<String, 
String> all) {
+    Preconditions.checkArgument(StringUtils.isNotBlank(driver), "Driver class 
name can't be blank");
+    Preconditions.checkArgument(StringUtils.isNotBlank(url), "JDBC URL can't 
be blank");
+
     String lowerUrl = url.toLowerCase();
     String decodedUrl = recursiveDecode(lowerUrl);
 
     // As H2 is only used for testing, we do not check unsafe parameters for 
H2.
-    if (driver != null) {
-      if (decodedUrl.startsWith("jdbc:mysql")) {
-        checkUnsafeParameters(decodedUrl, all, UNSAFE_MYSQL_PARAMETERS, 
"MySQL");
-      } else if (decodedUrl.startsWith("jdbc:mariadb")) {
-        checkUnsafeParameters(decodedUrl, all, UNSAFE_MYSQL_PARAMETERS, 
"MariaDB");
-      } else if (decodedUrl.startsWith("jdbc:postgresql")) {
-        checkUnsafeParameters(decodedUrl, all, UNSAFE_POSTGRES_PARAMETERS, 
"PostgreSQL");
-      }
+    if (decodedUrl.startsWith("jdbc:mysql")) {
+      checkUnsafeParameters(decodedUrl, all, UNSAFE_MYSQL_PARAMETERS, "MySQL");
+    } else if (decodedUrl.startsWith("jdbc:mariadb")) {
+      checkUnsafeParameters(decodedUrl, all, UNSAFE_MYSQL_PARAMETERS, 
"MariaDB");
+    } else if (decodedUrl.startsWith("jdbc:postgresql")) {
+      checkUnsafeParameters(decodedUrl, all, UNSAFE_POSTGRES_PARAMETERS, 
"PostgreSQL");
     }
   }
 
diff --git 
a/common/src/test/java/org/apache/gravitino/utils/TestJdbcUrlUtils.java 
b/common/src/test/java/org/apache/gravitino/utils/TestJdbcUrlUtils.java
index bcd4810595..24acf4314c 100644
--- a/common/src/test/java/org/apache/gravitino/utils/TestJdbcUrlUtils.java
+++ b/common/src/test/java/org/apache/gravitino/utils/TestJdbcUrlUtils.java
@@ -39,7 +39,8 @@ public class TestJdbcUrlUtils {
 
   @Test
   public void testValidateJdbcConfigWhenDriverClassNameIsNull() {
-    Assertions.assertDoesNotThrow(
+    Assertions.assertThrowsExactly(
+        IllegalArgumentException.class,
         () ->
             JdbcUrlUtils.validateJdbcConfig(
                 null, "jdbc:mysql://localhost:0000/test", Map.of("test", 
"test")));
@@ -125,4 +126,28 @@ public class TestJdbcUrlUtils {
     Assertions.assertEquals(
         "Unsafe PostgreSQL parameter 'socketFactory' detected in JDBC URL", 
gre.getMessage());
   }
+
+  @Test
+  void testValidateNullArguments() {
+    Assertions.assertThrowsExactly(
+        IllegalArgumentException.class,
+        () ->
+            JdbcUrlUtils.validateJdbcConfig(
+                null, "jdbc:postgresql://localhost:0000/test", Map.of("test", 
"test")));
+
+    Assertions.assertThrowsExactly(
+        IllegalArgumentException.class,
+        () -> JdbcUrlUtils.validateJdbcConfig("testDriver", null, 
Map.of("test", "test")));
+
+    Assertions.assertThrowsExactly(
+        IllegalArgumentException.class, () -> 
JdbcUrlUtils.validateJdbcConfig(null, null, null));
+
+    Assertions.assertThrowsExactly(
+        IllegalArgumentException.class,
+        () -> JdbcUrlUtils.validateJdbcConfig("", 
"jdbc:postgresql://localhost:0000/test", null));
+
+    Assertions.assertThrowsExactly(
+        IllegalArgumentException.class,
+        () -> JdbcUrlUtils.validateJdbcConfig("testDriver", "", null));
+  }
 }

Reply via email to