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

dengzh pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/hive.git


The following commit(s) were added to refs/heads/master by this push:
     new 0f83959036d HIVE-28805: Re-parse the password from the original jdbc 
url (#5682)
0f83959036d is described below

commit 0f83959036d546edc7f8f9f3ea1e83fef6d2326f
Author: Hazel Jiang <[email protected]>
AuthorDate: Fri Mar 14 01:20:33 2025 -0700

    HIVE-28805: Re-parse the password from the original jdbc url (#5682)
---
 jdbc/src/java/org/apache/hive/jdbc/Utils.java      |  7 ++
 .../org/apache/hive/jdbc/TestHiveConnection.java   | 77 ++++++++++++++++++++++
 2 files changed, 84 insertions(+)

diff --git a/jdbc/src/java/org/apache/hive/jdbc/Utils.java 
b/jdbc/src/java/org/apache/hive/jdbc/Utils.java
index 01fdc9de56f..46a2c582ba3 100644
--- a/jdbc/src/java/org/apache/hive/jdbc/Utils.java
+++ b/jdbc/src/java/org/apache/hive/jdbc/Utils.java
@@ -540,6 +540,13 @@ public static JdbcConnectionParams 
extractURLComponents(String uri, Properties i
       }
     }
 
+    Pattern fullPasswordPattern = 
Pattern.compile("(?i)(?<=;|^)password=([^;]+)");
+    Matcher fullPwdMatcher = fullPasswordPattern.matcher(uri);
+    if (fullPwdMatcher.find()) {
+        String fullPassword = fullPwdMatcher.group(1);
+        connParams.getSessionVars().put(JdbcConnectionParams.AUTH_PASSWD, 
fullPassword);
+    }
+
     // parse hive conf settings
     String confStr = jdbcURI.getQuery();
     if (confStr != null) {
diff --git a/jdbc/src/test/org/apache/hive/jdbc/TestHiveConnection.java 
b/jdbc/src/test/org/apache/hive/jdbc/TestHiveConnection.java
index 9bb2f60a243..a9e54167eeb 100644
--- a/jdbc/src/test/org/apache/hive/jdbc/TestHiveConnection.java
+++ b/jdbc/src/test/org/apache/hive/jdbc/TestHiveConnection.java
@@ -77,4 +77,81 @@ public void testHiveConnectionParameters() throws 
SQLException, ZooKeeperHiveCli
     JdbcConnectionParams nonPortParams = 
Utils.parseURL("jdbc:hive2://hello.host/default");
     Assert.assertEquals(Integer.parseInt(Utils.DEFAULT_PORT), 
nonPortParams.getPort());
   }
+
+  @Test
+  public void testPasswordExtractionFromUrl() throws SQLException, 
ZooKeeperHiveClientException {
+    // Test with password in the URL
+    JdbcConnectionParams params = Utils.parseURL(
+        
"jdbc:hive2://hello.host:10002/default;password=mySecretPassword;transportMode=http");
+
+    Assert.assertEquals("mySecretPassword",
+        params.getSessionVars().get(JdbcConnectionParams.AUTH_PASSWD));
+
+    // Test with password in different case (uppercase)
+    params = Utils.parseURL(
+        
"jdbc:hive2://hello.host:10002/default;PASSWORD=upperCasePassword;httpPath=cliservice");
+
+    Assert.assertEquals("upperCasePassword",
+        params.getSessionVars().get(JdbcConnectionParams.AUTH_PASSWD));
+
+    // Test with password at the beginning of parameters
+    params = Utils.parseURL(
+        
"jdbc:hive2://hello.host:10002/default;password=firstParam;httpPath=cliservice");
+
+    Assert.assertEquals("firstParam",
+        params.getSessionVars().get(JdbcConnectionParams.AUTH_PASSWD));
+
+    // Test with password at the end of parameters
+    params = Utils.parseURL(
+        
"jdbc:hive2://hello.host:10002/default;transportMode=http;password=lastParam");
+
+    Assert.assertEquals("lastParam",
+        params.getSessionVars().get(JdbcConnectionParams.AUTH_PASSWD));
+  }
+
+  @Test
+  public void testPasswordWithSpecialCharacters() throws SQLException, 
ZooKeeperHiveClientException {
+    // Test with password containing special characters
+    JdbcConnectionParams params = Utils.parseURL(
+        
"jdbc:hive2://hello.host:10002/default;password=Pass@123!#$;httpPath=cliservice");
+
+    Assert.assertEquals("Pass@123!#$",
+        params.getSessionVars().get(JdbcConnectionParams.AUTH_PASSWD));
+
+    // Test with password containing equals sign
+    params = Utils.parseURL(
+        
"jdbc:hive2://hello.host:10002/default;password=user=admin123;transportMode=http");
+
+    Assert.assertEquals("user=admin123",
+        params.getSessionVars().get(JdbcConnectionParams.AUTH_PASSWD));
+  }
+
+  @Test
+  public void testNoPasswordInUrl() throws SQLException, 
ZooKeeperHiveClientException {
+    // Test URL without password parameter
+    JdbcConnectionParams params = Utils.parseURL(
+        
"jdbc:hive2://hello.host:10002/default;transportMode=http;httpPath=cliservice");
+
+    
Assert.assertNull(params.getSessionVars().get(JdbcConnectionParams.AUTH_PASSWD));
+  }
+
+  @Test
+  public void testEmptyPassword() throws SQLException, 
ZooKeeperHiveClientException {
+    // Test with empty password
+    JdbcConnectionParams params = Utils.parseURL(
+        "jdbc:hive2://hello.host:10002/default;password=;transportMode=http");
+
+    Assert.assertEquals("",
+        params.getSessionVars().get(JdbcConnectionParams.AUTH_PASSWD));
+  }
+
+  @Test
+  public void testPasswordWithSemicolon() throws SQLException, 
ZooKeeperHiveClientException {
+    // Test URL with semicolon in password (this should extract up to the 
semicolon)
+    JdbcConnectionParams params = Utils.parseURL(
+        
"jdbc:hive2://hello.host:10002/default;password=part1;part2;transportMode=http");
+
+    Assert.assertEquals("part1",
+        params.getSessionVars().get(JdbcConnectionParams.AUTH_PASSWD));
+  }
 }

Reply via email to