Skip to content

Commit 842289e

Browse files
committed
feat(extension-driver-ksqldb): add http basic security to connection
1 parent c786c10 commit 842289e

6 files changed

Lines changed: 37 additions & 8 deletions

File tree

packages/extension-driver-ksqldb/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ This is the KSqlDb driver for VulcanSQL, provided by [Canner](https://cannerdata
2525
connection:
2626
# Optional: KSqlDb instance URL. Default is http://localhost:8088.
2727
host: 'www.example.com:8088'
28+
# Optional: The name of the user on whose behalf requests are made.
29+
username: '<username>',
30+
# The user's password
31+
password: '<password>'
2832
```
2933

3034
## Testing

packages/extension-driver-ksqldb/src/lib/restfulClient.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
import * as http2 from 'http2';
22

3-
const BASIC_HEADERS = {
4-
Accept: 'application/vnd.ksql.v1+json',
5-
};
63

74
const RESTFUL_API = {
85
INFO: '/info',
@@ -54,7 +51,9 @@ export type FinalMessage = {
5451
export type QueryResponse = Header | Row | FinalMessage;
5552

5653
export interface RestfulClientOptions {
57-
host: string;
54+
host?: string;
55+
username?: string;
56+
password?: string;
5857
}
5958

6059
export class RestfulClient {
@@ -71,7 +70,7 @@ export class RestfulClient {
7170
public connect() {
7271
this.startSession = () =>
7372
new Promise((resolve, reject) => {
74-
this.client = http2.connect(this.options.host);
73+
this.client = http2.connect(this.options.host || 'http://localhost:8088');
7574

7675
this.client.on('connect', () => {
7776
this.connected = true;
@@ -103,7 +102,11 @@ export class RestfulClient {
103102
const res = await this.request<KsqlInfoResponse>(RESTFUL_API.INFO, 'GET');
104103
return res.KsqlServerInfo['serverStatus'];
105104
} catch (e) {
106-
throw new Error('KsqlDb server is not ready');
105+
if(e.error_code) {
106+
throw new Error(JSON.stringify(e));
107+
} else {
108+
throw new Error('KsqlDb server is not ready');
109+
}
107110
}
108111
}
109112

@@ -168,11 +171,16 @@ export class RestfulClient {
168171
this.startSession && (await this.startSession());
169172

170173
return new Promise((resolve, reject) => {
171-
const config = {
172-
...BASIC_HEADERS,
174+
const config: any = {
175+
'content-type': 'application/vnd.ksql.v1+json',
173176
':method': method,
174177
':path': path,
175178
};
179+
// add authorization if username and password is provided
180+
if(this.options.username && this.options.password) {
181+
config['authorization'] = `Basic ${Buffer.from(`${this.options.username}:${this.options.password}`).toString('base64')}`
182+
}
183+
176184
const req = this.client!.request(config);
177185
req.setEncoding('utf-8');
178186

packages/extension-driver-ksqldb/test/docker/docker-compose.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,18 @@ services:
4646
- "8088:8088"
4747
expose:
4848
- "8088"
49+
volumes:
50+
- ./jaas_config.file:/etc/confluent/jaas_config.file
51+
- ./password-file:/etc/confluent/password-file
4952
environment:
5053
KSQL_LISTENERS: http://0.0.0.0:8088
5154
KSQL_BOOTSTRAP_SERVERS: broker:9092
5255
KSQL_KSQL_LOGGING_PROCESSING_STREAM_AUTO_CREATE: "true"
5356
KSQL_KSQL_LOGGING_PROCESSING_TOPIC_AUTO_CREATE: "true"
57+
KSQL_AUTHENTICATION_METHOD: "BASIC"
58+
KSQL_AUTHENTICATION_REALM: "KsqlServer-Props"
59+
KSQL_AUTHENTICATION_ROLES: "users"
60+
KSQL_OPTS: "-Djava.security.auth.login.config=/etc/confluent/jaas_config.file -Dauthentication.skip.paths=/healthcheck"
5461
networks:
5562
- ksqldb_default
5663

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
KsqlServer-Props {
2+
org.eclipse.jetty.jaas.spi.PropertyFileLoginModule required
3+
file="/etc/confluent/password-file"
4+
debug="true";
5+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
admin:admin123, users

packages/extension-driver-ksqldb/test/ksqlDbServer.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ export class KSqlDbServer {
1717
public readonly port = '8088';
1818
public readonly host = 'localhost';
1919
public readonly image = 'confluentinc/ksqldb-server:0.29.0';
20+
public readonly username = 'admin';
21+
public readonly password = 'admin123';
2022
public container?: Docker.Container;
2123
public client?: RestfulClient;
2224

@@ -25,6 +27,8 @@ export class KSqlDbServer {
2527
// https://github.com/apocas/dockerode/issues/647
2628
this.client = new RestfulClient({
2729
host: `http://${this.host}:${this.port}`,
30+
username: this.username,
31+
password: this.password,
2832
});
2933
await this.waitKSqlDbReady(this.client);
3034

0 commit comments

Comments
 (0)