Skip to content

Commit 2ea4e97

Browse files
KSDaemonmarianore-muttdata
authored andcommitted
feat(prestodb-driver, trino-driver): Support custom auth headers (JWT) (cube-js#9660)
1 parent 2b01582 commit 2ea4e97

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
@@ -1804,7 +1804,7 @@ const variables: Record<string, (...args: any) => any> = {
18041804
return [];
18051805
},
18061806
/** ***************************************************************
1807-
* Presto Driver *
1807+
* Presto/Trino Driver *
18081808
**************************************************************** */
18091809

18101810
/**
@@ -1820,12 +1820,25 @@ const variables: Record<string, (...args: any) => any> = {
18201820
]
18211821
),
18221822

1823+
/**
1824+
* Presto/Trino Auth Token
1825+
*/
1826+
prestoAuthToken: ({
1827+
dataSource,
1828+
}: {
1829+
dataSource: string,
1830+
}) => (
1831+
process.env[
1832+
keyByDataSource('CUBEJS_DB_PRESTO_AUTH_TOKEN', dataSource)
1833+
]
1834+
),
1835+
18231836
/** ***************************************************************
18241837
* Pinot Driver *
18251838
**************************************************************** */
18261839

18271840
/**
1828-
* Pinot / Startree Auth Token
1841+
* Pinot/Startree Auth Token
18291842
*/
18301843
pinotAuthToken: ({
18311844
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)