Skip to content

Commit 263c9eb

Browse files
committed
added JWT test with cloud
1 parent 6ec4d4a commit 263c9eb

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

.github/workflows/build.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,13 @@ jobs:
247247
EOF
248248
- name: Install Java client
249249
run: mvn --also-make --batch-mode --no-transfer-progress -DskipTests install
250+
- name: Generate JWT
251+
env:
252+
JWT_K_PEM: ${{ secrets.JWT_K_PEM }}
253+
run: |
254+
echo "{environment_variable_name}={value}" >> "$GITHUB_ENV"
255+
npm install jsonwebtoken -g &&
256+
echo "CLIENT_JWT=$(JWT_K_PEM=$(cat k.pem) node jwt.js)" >> "$GITHUB_ENV"
250257
- name: Test http client
251258
env:
252259
CLICKHOUSE_CLOUD_HOST: ${{ secrets.CLOUD_INSTANCE_HOST }}

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import com.clickhouse.client.api.enums.ProxyType;
1313
import com.clickhouse.client.api.insert.InsertResponse;
1414
import com.clickhouse.client.api.insert.InsertSettings;
15+
import com.clickhouse.client.api.internal.ServerSettings;
1516
import com.clickhouse.client.api.query.GenericRecord;
1617
import com.clickhouse.client.api.query.QueryResponse;
1718
import com.clickhouse.client.api.query.QuerySettings;
@@ -909,4 +910,31 @@ public void testBearerTokenAuth() throws Exception {
909910
mockServer.stop();
910911
}
911912
}
913+
914+
@Test
915+
public void testJWTWithCloud() throws Exception {
916+
String jwt = System.getenv("CLIENT_JWT");
917+
try (Client client = newClient().useBearerTokenAuth(jwt).build()) {
918+
try {
919+
List<GenericRecord> response = client.queryAll("SELECT user(), now()");
920+
System.out.println("response: " + response.get(0).getString(1) + " time: " + response.get(0).getString(2));
921+
} catch (Exception e) {
922+
e.printStackTrace();
923+
throw e;
924+
}
925+
}
926+
}
927+
928+
protected Client.Builder newClient() {
929+
ClickHouseNode node = getServer(ClickHouseProtocol.HTTP);
930+
boolean isSecure = isCloud();
931+
return new Client.Builder()
932+
.addEndpoint(Protocol.HTTP, node.getHost(), node.getPort(), isSecure)
933+
// .setUsername("default")
934+
// .setPassword(ClickHouseServerForTest.getPassword())
935+
.compressClientRequest(false)
936+
.setDefaultDatabase(ClickHouseServerForTest.getDatabase())
937+
.serverSetting(ServerSettings.WAIT_END_OF_QUERY, "1")
938+
.useNewImplementation(System.getProperty("client.tests.useNewImplementation", "true").equals("true"));
939+
}
912940
}

jwt-gen.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const jwt = require('jsonwebtoken');
2+
const secret = process.env['JWT_K_PEM']
3+
if (secret === undefined) {
4+
throw new Error(
5+
'Environment variable JWT_K_PEM is not set',
6+
)
7+
}
8+
const payload = {
9+
iss: 'ClickHouse',
10+
sub: 'CI_Test',
11+
aud: '1f7f78b8-da67-480b-8913-726fdd31d2fc',
12+
'clickhouse:roles': ['default'],
13+
'clickhouse:grants': [],
14+
};
15+
16+
const signed = jwt.sign(payload, secret, { expiresIn: '15m', algorithm: 'RS256' });
17+
18+
console.log(signed);

0 commit comments

Comments
 (0)