Skip to content

Commit 0f83959

Browse files
authored
HIVE-28805: Re-parse the password from the original jdbc url (#5682)
1 parent 38dc6b6 commit 0f83959

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

jdbc/src/java/org/apache/hive/jdbc/Utils.java

+7
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,13 @@ public static JdbcConnectionParams extractURLComponents(String uri, Properties i
540540
}
541541
}
542542

543+
Pattern fullPasswordPattern = Pattern.compile("(?i)(?<=;|^)password=([^;]+)");
544+
Matcher fullPwdMatcher = fullPasswordPattern.matcher(uri);
545+
if (fullPwdMatcher.find()) {
546+
String fullPassword = fullPwdMatcher.group(1);
547+
connParams.getSessionVars().put(JdbcConnectionParams.AUTH_PASSWD, fullPassword);
548+
}
549+
543550
// parse hive conf settings
544551
String confStr = jdbcURI.getQuery();
545552
if (confStr != null) {

jdbc/src/test/org/apache/hive/jdbc/TestHiveConnection.java

+77
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,81 @@ public void testHiveConnectionParameters() throws SQLException, ZooKeeperHiveCli
7777
JdbcConnectionParams nonPortParams = Utils.parseURL("jdbc:hive2://hello.host/default");
7878
Assert.assertEquals(Integer.parseInt(Utils.DEFAULT_PORT), nonPortParams.getPort());
7979
}
80+
81+
@Test
82+
public void testPasswordExtractionFromUrl() throws SQLException, ZooKeeperHiveClientException {
83+
// Test with password in the URL
84+
JdbcConnectionParams params = Utils.parseURL(
85+
"jdbc:hive2://hello.host:10002/default;password=mySecretPassword;transportMode=http");
86+
87+
Assert.assertEquals("mySecretPassword",
88+
params.getSessionVars().get(JdbcConnectionParams.AUTH_PASSWD));
89+
90+
// Test with password in different case (uppercase)
91+
params = Utils.parseURL(
92+
"jdbc:hive2://hello.host:10002/default;PASSWORD=upperCasePassword;httpPath=cliservice");
93+
94+
Assert.assertEquals("upperCasePassword",
95+
params.getSessionVars().get(JdbcConnectionParams.AUTH_PASSWD));
96+
97+
// Test with password at the beginning of parameters
98+
params = Utils.parseURL(
99+
"jdbc:hive2://hello.host:10002/default;password=firstParam;httpPath=cliservice");
100+
101+
Assert.assertEquals("firstParam",
102+
params.getSessionVars().get(JdbcConnectionParams.AUTH_PASSWD));
103+
104+
// Test with password at the end of parameters
105+
params = Utils.parseURL(
106+
"jdbc:hive2://hello.host:10002/default;transportMode=http;password=lastParam");
107+
108+
Assert.assertEquals("lastParam",
109+
params.getSessionVars().get(JdbcConnectionParams.AUTH_PASSWD));
110+
}
111+
112+
@Test
113+
public void testPasswordWithSpecialCharacters() throws SQLException, ZooKeeperHiveClientException {
114+
// Test with password containing special characters
115+
JdbcConnectionParams params = Utils.parseURL(
116+
"jdbc:hive2://hello.host:10002/default;password=Pass@123!#$;httpPath=cliservice");
117+
118+
Assert.assertEquals("Pass@123!#$",
119+
params.getSessionVars().get(JdbcConnectionParams.AUTH_PASSWD));
120+
121+
// Test with password containing equals sign
122+
params = Utils.parseURL(
123+
"jdbc:hive2://hello.host:10002/default;password=user=admin123;transportMode=http");
124+
125+
Assert.assertEquals("user=admin123",
126+
params.getSessionVars().get(JdbcConnectionParams.AUTH_PASSWD));
127+
}
128+
129+
@Test
130+
public void testNoPasswordInUrl() throws SQLException, ZooKeeperHiveClientException {
131+
// Test URL without password parameter
132+
JdbcConnectionParams params = Utils.parseURL(
133+
"jdbc:hive2://hello.host:10002/default;transportMode=http;httpPath=cliservice");
134+
135+
Assert.assertNull(params.getSessionVars().get(JdbcConnectionParams.AUTH_PASSWD));
136+
}
137+
138+
@Test
139+
public void testEmptyPassword() throws SQLException, ZooKeeperHiveClientException {
140+
// Test with empty password
141+
JdbcConnectionParams params = Utils.parseURL(
142+
"jdbc:hive2://hello.host:10002/default;password=;transportMode=http");
143+
144+
Assert.assertEquals("",
145+
params.getSessionVars().get(JdbcConnectionParams.AUTH_PASSWD));
146+
}
147+
148+
@Test
149+
public void testPasswordWithSemicolon() throws SQLException, ZooKeeperHiveClientException {
150+
// Test URL with semicolon in password (this should extract up to the semicolon)
151+
JdbcConnectionParams params = Utils.parseURL(
152+
"jdbc:hive2://hello.host:10002/default;password=part1;part2;transportMode=http");
153+
154+
Assert.assertEquals("part1",
155+
params.getSessionVars().get(JdbcConnectionParams.AUTH_PASSWD));
156+
}
80157
}

0 commit comments

Comments
 (0)