Skip to content

World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.

[#8386] fix(common): Add precodition to JdbcUrlUtils.java to check driver and url not null (#8405)

Lord of Abyss16f12b4143e5
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));
+ }
}