Skip to content

Commit fcdda34

Browse files
committed
fixed selecting default user when password is empty or null
1 parent 13fcafa commit fcdda34

File tree

3 files changed

+8
-7
lines changed

3 files changed

+8
-7
lines changed

clickhouse-http-client/src/main/java/com/clickhouse/client/http/ClickHouseHttpConnection.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,9 +243,10 @@ protected static Map<String, String> createDefaultHeaders(ClickHouseConfig confi
243243
if (config.isSsl() && !ClickHouseChecker.isNullOrEmpty(config.getSslCert())) {
244244
map.put("x-clickhouse-user", credentials.getUserName());
245245
map.put("x-clickhouse-ssl-certificate-auth", "on");
246-
} else if (!ClickHouseChecker.isNullOrEmpty(credentials.getPassword())) {
246+
} else {
247+
String password = credentials.getPassword() == null ? "" : credentials.getPassword();
247248
map.put(HttpHeaders.AUTHORIZATION, "Basic " + Base64.getEncoder()
248-
.encodeToString((credentials.getUserName() + ":" + credentials.getPassword()).getBytes(StandardCharsets.UTF_8)));
249+
.encodeToString((credentials.getUserName() + ":" + password).getBytes(StandardCharsets.UTF_8)));
249250
}
250251
}
251252

clickhouse-jdbc/src/test/java/com/clickhouse/jdbc/AccessManagementTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,9 @@ public void testPasswordAuthentication(String identifyWith, String identifyBy) t
195195

196196
try (Connection connection = dataSource.getConnection("some_user", identifyBy)) {
197197
Statement st = connection.createStatement();
198-
ResultSet rs = st.executeQuery("SELECT 1");
198+
ResultSet rs = st.executeQuery("SELECT user() AS user_name");
199199
Assert.assertTrue(rs.next());
200-
Assert.assertEquals(rs.getInt(1), 1);
200+
Assert.assertEquals(rs.getString(1), "some_user");
201201
} catch (Exception e) {
202202
Assert.fail("Failed to authenticate", e);
203203
}
@@ -207,6 +207,7 @@ public void testPasswordAuthentication(String identifyWith, String identifyBy) t
207207
private static Object[][] passwordAuthMethods() {
208208
return new Object[][] {
209209
{ "plaintext_password", "password" },
210+
{ "plaintext_password", "" },
210211
{ "plaintext_password", "S3Cr=?t"},
211212
{ "plaintext_password", "123§" },
212213
{ "sha256_password", "password" },

client-v2/src/test/java/com/clickhouse/client/HttpTransportTests.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -517,9 +517,7 @@ public void testPasswordAuthentication(String identifyWith, String identifyBy) t
517517
.setPassword(identifyBy)
518518
.build()) {
519519

520-
try (QueryResponse resp = client.query("SELECT 1").get()) {
521-
Assert.assertEquals(resp.getReadRows(), 1);
522-
}
520+
Assert.assertEquals(client.queryAll("SELECT user()").get(0).getString(1), "some_user");
523521
} catch (Exception e) {
524522
Assert.fail("Failed to authenticate", e);
525523
}
@@ -529,6 +527,7 @@ public void testPasswordAuthentication(String identifyWith, String identifyBy) t
529527
public static Object[][] testPasswordAuthenticationProvider() {
530528
return new Object[][] {
531529
{ "plaintext_password", "password" },
530+
{ "plaintext_password", "" },
532531
{ "plaintext_password", "S3Cr=?t"},
533532
{ "plaintext_password", "123§" },
534533
{ "sha256_password", "password" },

0 commit comments

Comments
 (0)