Skip to content

Commit 7992cae

Browse files
committed
feat(prestodb-driver, trino-driver): Support custom auth headers (JWT)
1 parent 71c1022 commit 7992cae

File tree

3 files changed

+32
-11
lines changed

3 files changed

+32
-11
lines changed

packages/cubejs-backend-shared/src/env.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1798,7 +1798,7 @@ const variables: Record<string, (...args: any) => any> = {
17981798
return [];
17991799
},
18001800
/** ***************************************************************
1801-
* Presto Driver *
1801+
* Presto/Trino Driver *
18021802
**************************************************************** */
18031803

18041804
/**
@@ -1814,12 +1814,25 @@ const variables: Record<string, (...args: any) => any> = {
18141814
]
18151815
),
18161816

1817+
/**
1818+
* Presto/Trino Auth Token
1819+
*/
1820+
prestoAuthToken: ({
1821+
dataSource,
1822+
}: {
1823+
dataSource: string,
1824+
}) => (
1825+
process.env[
1826+
keyByDataSource('CUBEJS_DB_PRESTO_AUTH_TOKEN', dataSource)
1827+
]
1828+
),
1829+
18171830
/** ***************************************************************
18181831
* Pinot Driver *
18191832
**************************************************************** */
18201833

18211834
/**
1822-
* Pinot / Startree Auth Token
1835+
* Pinot/Startree Auth Token
18231836
*/
18241837
pinotAuthToken: ({
18251838
dataSource,

packages/cubejs-prestodb-driver/src/PrestoDriver.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ export type PrestoDriverConfiguration = PrestoDriverExportBucket & {
4242
schema?: string;
4343
user?: string;
4444
// eslint-disable-next-line camelcase
45+
custom_auth?: string;
46+
// eslint-disable-next-line camelcase
4547
basic_auth?: { user: string, password: string };
4648
ssl?: string | TLSConnectionOptions;
4749
dataSource?: string;
@@ -76,6 +78,14 @@ export class PrestoDriver extends BaseDriver implements DriverInterface {
7678
config.dataSource ||
7779
assertDataSource('default');
7880

81+
const dbUser = getEnv('dbUser', { dataSource });
82+
const dbPassword = getEnv('dbPass', { dataSource });
83+
const authToken = getEnv('prestoAuthToken', { dataSource });
84+
85+
if (authToken && dbPassword) {
86+
throw new Error('Both user/password and auth token are set. Please remove password or token.');
87+
}
88+
7989
this.config = {
8090
host: getEnv('dbHost', { dataSource }),
8191
port: getEnv('dbPort', { dataSource }),
@@ -85,13 +95,9 @@ export class PrestoDriver extends BaseDriver implements DriverInterface {
8595
schema:
8696
getEnv('dbName', { dataSource }) ||
8797
getEnv('dbSchema', { dataSource }),
88-
user: getEnv('dbUser', { dataSource }),
89-
basic_auth: getEnv('dbPass', { dataSource })
90-
? {
91-
user: getEnv('dbUser', { dataSource }),
92-
password: getEnv('dbPass', { dataSource }),
93-
}
94-
: undefined,
98+
user: dbUser,
99+
...(authToken ? { custom_auth: `Bearer ${authToken}` } : {}),
100+
...(dbPassword ? { basic_auth: { user: dbUser, password: dbPassword } } : {}),
95101
ssl: this.getSslOptions(dataSource),
96102
bucketType: getEnv('dbExportBucketType', { supported: ['gcs'], dataSource }),
97103
exportBucket: getEnv('dbExportBucket', { dataSource }),

packages/cubejs-trino-driver/src/TrinoDriver.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ export class TrinoDriver extends PrestoDriver {
1212
}
1313

1414
public override async testConnection(): Promise<void> {
15-
const { host, port, ssl, basic_auth: basicAuth } = this.config;
15+
const { host, port, ssl, basic_auth: basicAuth, custom_auth: customAuth } = this.config;
1616
const protocol = ssl ? 'https' : 'http';
1717
const url = `${protocol}://${host}:${port}/v1/info`;
1818
const headers: Record<string, string> = {};
1919

20-
if (basicAuth) {
20+
if (customAuth) {
21+
headers.Authorization = customAuth;
22+
} else if (basicAuth) {
2123
const { user, password } = basicAuth;
2224
const encoded = Buffer.from(`${user}:${password}`).toString('base64');
2325
headers.Authorization = `Basic ${encoded}`;

0 commit comments

Comments
 (0)