|
5 | 5 | import com.clickhouse.client.api.ClientException;
|
6 | 6 | import com.clickhouse.client.api.ClientFaultCause;
|
7 | 7 | import com.clickhouse.client.api.ConnectionReuseStrategy;
|
| 8 | +import com.clickhouse.client.api.command.CommandResponse; |
8 | 9 | import com.clickhouse.client.api.enums.Protocol;
|
9 | 10 | import com.clickhouse.client.api.internal.ClickHouseLZ4OutputStream;
|
10 | 11 | import com.clickhouse.client.api.metadata.DefaultColumnToMethodMatchingStrategy;
|
|
13 | 14 | import com.clickhouse.client.api.query.QuerySettings;
|
14 | 15 | import com.clickhouse.client.api.query.Records;
|
15 | 16 | import com.clickhouse.client.config.ClickHouseClientOption;
|
| 17 | +import com.clickhouse.client.query.QueryTests; |
| 18 | +import com.clickhouse.data.ClickHouseVersion; |
| 19 | +import org.apache.commons.lang3.RandomStringUtils; |
16 | 20 | import org.slf4j.Logger;
|
17 | 21 | import org.slf4j.LoggerFactory;
|
18 | 22 | import org.testng.Assert;
|
19 | 23 | import org.testng.annotations.DataProvider;
|
20 | 24 | import org.testng.annotations.Test;
|
| 25 | +import org.testng.util.Strings; |
21 | 26 |
|
22 | 27 | import java.net.ConnectException;
|
| 28 | +import java.util.Arrays; |
23 | 29 | import java.util.HashMap;
|
| 30 | +import java.util.HashSet; |
| 31 | +import java.util.List; |
24 | 32 | import java.util.Map;
|
25 | 33 | import java.util.Optional;
|
| 34 | +import java.util.Set; |
| 35 | +import java.util.UUID; |
26 | 36 | import java.util.concurrent.ExecutorService;
|
27 | 37 | import java.util.concurrent.Executors;
|
28 | 38 | import java.util.concurrent.TimeUnit;
|
@@ -310,6 +320,104 @@ public void testWithOldDefaults() {
|
310 | 320 | }
|
311 | 321 | }
|
312 | 322 |
|
| 323 | + @DataProvider(name = "sessionRoles") |
| 324 | + private static Object[][] sessionRoles() { |
| 325 | + return new Object[][]{ |
| 326 | + {new String[]{"ROL1", "ROL2"}}, |
| 327 | + {new String[]{"ROL1", "ROL2"}}, |
| 328 | + {new String[]{"ROL1", "ROL2"}}, |
| 329 | + {new String[]{"ROL1", "ROL2,☺"}}, |
| 330 | + {new String[]{"ROL1", "ROL2"}}, |
| 331 | + }; |
| 332 | + } |
| 333 | + |
| 334 | + @Test(groups = {"integration"}, dataProvider = "sessionRoles") |
| 335 | + public void testOperationCustomRoles(String[] roles) throws Exception { |
| 336 | + if (isVersionMatch("(,24.3]", newClient().build())) { |
| 337 | + return; |
| 338 | + } |
| 339 | + |
| 340 | + String password = "^1A" + RandomStringUtils.random(12, true, true) + "3b$"; |
| 341 | + final String rolesList = "\"" + Strings.join("\",\"", roles) + "\""; |
| 342 | + try (Client client = newClient().build()) { |
| 343 | + client.execute("DROP ROLE IF EXISTS " + rolesList).get().close(); |
| 344 | + client.execute("CREATE ROLE " + rolesList).get().close(); |
| 345 | + client.execute("DROP USER IF EXISTS some_user").get().close(); |
| 346 | + client.execute("CREATE USER some_user IDENTIFIED BY '" + password + "'").get().close(); |
| 347 | + client.execute("GRANT " + rolesList + " TO some_user").get().close(); |
| 348 | + } |
| 349 | + |
| 350 | + try (Client userClient = newClient().setUsername("some_user").setPassword(password).build()) { |
| 351 | + QuerySettings settings = new QuerySettings().setDBRoles(Arrays.asList(roles)); |
| 352 | + List<GenericRecord> resp = userClient.queryAll("SELECT currentRoles()", settings); |
| 353 | + Set<String> roleSet = new HashSet<>(Arrays.asList(roles)); |
| 354 | + Set<String> currentRoles = new HashSet<String> (resp.get(0).getList(1)); |
| 355 | + Assert.assertEquals(currentRoles, roleSet, "Roles " + roleSet + " not found in " + currentRoles); |
| 356 | + } |
| 357 | + } |
| 358 | + |
| 359 | + @DataProvider(name = "clientSessionRoles") |
| 360 | + private static Object[][] clientSessionRoles() { |
| 361 | + return new Object[][]{ |
| 362 | + {new String[]{"ROL1", "ROL2"}}, |
| 363 | + {new String[]{"ROL1", "ROL2,☺"}}, |
| 364 | + }; |
| 365 | + } |
| 366 | + @Test(groups = {"integration"}, dataProvider = "clientSessionRoles") |
| 367 | + public void testClientCustomRoles(String[] roles) throws Exception { |
| 368 | + if (isVersionMatch("(,24.3]", newClient().build())) { |
| 369 | + return; |
| 370 | + } |
| 371 | + |
| 372 | + String password = "^1A" + RandomStringUtils.random(12, true, true) + "3B$"; |
| 373 | + final String rolesList = "\"" + Strings.join("\",\"", roles) + "\""; |
| 374 | + try (Client client = newClient().build()) { |
| 375 | + client.execute("DROP ROLE IF EXISTS " + rolesList).get().close(); |
| 376 | + client.execute("CREATE ROLE " + rolesList).get().close(); |
| 377 | + client.execute("DROP USER IF EXISTS some_user").get().close(); |
| 378 | + client.execute("CREATE USER some_user IDENTIFIED WITH sha256_password BY '" + password + "'").get().close(); |
| 379 | + client.execute("GRANT " + rolesList + " TO some_user").get().close(); |
| 380 | + } |
| 381 | + |
| 382 | + try (Client userClient = newClient().setUsername("some_user").setPassword(password).build()) { |
| 383 | + userClient.setDBRoles(Arrays.asList(roles)); |
| 384 | + List<GenericRecord> resp = userClient.queryAll("SELECT currentRoles()"); |
| 385 | + Set<String> roleSet = new HashSet<>(Arrays.asList(roles)); |
| 386 | + Set<String> currentRoles = new HashSet<String> (resp.get(0).getList(1)); |
| 387 | + Assert.assertEquals(currentRoles, roleSet, "Roles " + roleSet + " not found in " + currentRoles); |
| 388 | + } |
| 389 | + } |
| 390 | + |
| 391 | + |
| 392 | + @Test(groups = {"integration"}) |
| 393 | + public void testLogComment() throws Exception { |
| 394 | + |
| 395 | + String logComment = "Test log comment"; |
| 396 | + QuerySettings settings = new QuerySettings() |
| 397 | + .setQueryId(UUID.randomUUID().toString()) |
| 398 | + .logComment(logComment); |
| 399 | + |
| 400 | + try (Client client = newClient().build()) { |
| 401 | + |
| 402 | + try (QueryResponse response = client.query("SELECT 1", settings).get()) { |
| 403 | + Assert.assertNotNull(response.getQueryId()); |
| 404 | + Assert.assertTrue(response.getQueryId().startsWith(settings.getQueryId())); |
| 405 | + } |
| 406 | + |
| 407 | + client.execute("SYSTEM FLUSH LOGS").get().close(); |
| 408 | + |
| 409 | + List<GenericRecord> logRecords = client.queryAll("SELECT query_id, log_comment FROM clusterAllReplicas('default', system.query_log) WHERE query_id = '" + settings.getQueryId() + "'"); |
| 410 | + Assert.assertEquals(logRecords.get(0).getString("query_id"), settings.getQueryId()); |
| 411 | + Assert.assertEquals(logRecords.get(0).getString("log_comment"), logComment); |
| 412 | + } |
| 413 | + } |
| 414 | + |
| 415 | + public boolean isVersionMatch(String versionExpression, Client client) { |
| 416 | + List<GenericRecord> serverVersion = client.queryAll("SELECT version()"); |
| 417 | + return ClickHouseVersion.of(serverVersion.get(0).getString(1)).check(versionExpression); |
| 418 | + } |
| 419 | + |
| 420 | + |
313 | 421 | protected Client.Builder newClient() {
|
314 | 422 | ClickHouseNode node = getServer(ClickHouseProtocol.HTTP);
|
315 | 423 | boolean isSecure = isCloud();
|
|
0 commit comments