|
1 | 1 | package com.clickhouse.jdbc; |
2 | 2 |
|
| 3 | +import java.nio.ByteBuffer; |
| 4 | +import java.nio.charset.StandardCharsets; |
3 | 5 | import java.sql.*; |
| 6 | +import java.util.Arrays; |
| 7 | +import java.util.Base64; |
| 8 | +import java.util.List; |
4 | 9 | import java.util.Properties; |
5 | 10 |
|
6 | 11 | import java.util.Properties; |
| 12 | +import java.util.concurrent.TimeUnit; |
7 | 13 |
|
8 | 14 | import com.clickhouse.client.ClickHouseNode; |
9 | 15 | import com.clickhouse.client.ClickHouseProtocol; |
| 16 | +import com.clickhouse.client.api.Client; |
10 | 17 | import com.clickhouse.client.api.ClientConfigProperties; |
11 | 18 | import com.clickhouse.client.api.ServerException; |
| 19 | +import com.clickhouse.client.api.enums.Protocol; |
12 | 20 | import com.clickhouse.client.api.internal.ServerSettings; |
| 21 | +import com.clickhouse.client.api.query.GenericRecord; |
| 22 | +import com.clickhouse.client.api.query.QueryResponse; |
13 | 23 | import com.clickhouse.jdbc.internal.ClientInfoProperties; |
14 | 24 | import com.clickhouse.jdbc.internal.DriverProperties; |
| 25 | +import com.github.tomakehurst.wiremock.WireMockServer; |
| 26 | +import com.github.tomakehurst.wiremock.client.WireMock; |
| 27 | +import com.github.tomakehurst.wiremock.common.ConsoleNotifier; |
| 28 | +import com.github.tomakehurst.wiremock.core.WireMockConfiguration; |
| 29 | +import org.apache.hc.core5.http.HttpStatus; |
15 | 30 | import org.testng.Assert; |
16 | 31 | import org.testng.annotations.Test; |
17 | 32 |
|
@@ -407,4 +422,61 @@ public void testUnwrapping() throws Exception { |
407 | 422 | Assert.assertEquals(conn.unwrap(JdbcV2Wrapper.class), conn); |
408 | 423 | assertThrows(SQLException.class, () -> conn.unwrap(ResultSet.class)); |
409 | 424 | } |
| 425 | + |
| 426 | + @Test(groups = { "integration" }) |
| 427 | + public void testBearerTokenAuth() throws Exception { |
| 428 | + if (isCloud()) { |
| 429 | + return; // mocked server |
| 430 | + } |
| 431 | + |
| 432 | + WireMockServer mockServer = new WireMockServer( WireMockConfiguration |
| 433 | + .options().port(9090).notifier(new ConsoleNotifier(false))); |
| 434 | + mockServer.start(); |
| 435 | + |
| 436 | + try { |
| 437 | + String jwtToken1 = Arrays.stream( |
| 438 | + new String[]{"header", "payload", "signature"}) |
| 439 | + .map(s -> Base64.getEncoder().encodeToString(s.getBytes(StandardCharsets.UTF_8))) |
| 440 | + .reduce((s1, s2) -> s1 + "." + s2).get(); |
| 441 | + |
| 442 | + // WIP |
| 443 | + ByteBuffer buffer = ByteBuffer.allocate(4); |
| 444 | + buffer.put((byte) 1); |
| 445 | + |
| 446 | + mockServer.addStubMapping(WireMock.post(WireMock.anyUrl()) |
| 447 | + .withHeader("Authorization", WireMock.equalTo("Bearer " + jwtToken1)) |
| 448 | + .willReturn( |
| 449 | + WireMock.ok(buffer.toString()) |
| 450 | + .withHeader("X-ClickHouse-Summary", |
| 451 | + "{ \"read_bytes\": \"10\", \"read_rows\": \"1\"}")).build()); |
| 452 | + |
| 453 | + Properties properties = new Properties(); |
| 454 | + properties.put("access_token", jwtToken1); |
| 455 | + properties.put("compress", "false"); |
| 456 | + String jdbcUrl = "jdbc:clickhouse://" + "localhost" + ":" + mockServer.port(); |
| 457 | + try (Connection conn = new ConnectionImpl(jdbcUrl, properties); |
| 458 | + Statement stmt = conn.createStatement(); |
| 459 | + ResultSet rs = stmt.executeQuery("SELECT 1")) { |
| 460 | + |
| 461 | + } |
| 462 | + } finally { |
| 463 | + mockServer.stop(); |
| 464 | + } |
| 465 | + } |
| 466 | + @Test(groups = { "integration" }) |
| 467 | + public void testJWTWithCloud() throws Exception { |
| 468 | + if (!isCloud()) { |
| 469 | + return; // only for cloud |
| 470 | + } |
| 471 | + |
| 472 | + String jwt = System.getenv("CLIENT_JWT"); |
| 473 | + Properties properties = new Properties(); |
| 474 | + properties.put("access_token", jwt); |
| 475 | + try (Connection conn = getJdbcConnection(properties); |
| 476 | + Statement stmt = conn.createStatement(); |
| 477 | + ResultSet rs = stmt.executeQuery("SELECT 1")) { |
| 478 | + Assert.assertTrue(rs.next()); |
| 479 | + } |
| 480 | + } |
| 481 | + |
410 | 482 | } |
0 commit comments