Skip to content

Commit 0de1978

Browse files
committed
feat: init
1 parent c9bd438 commit 0de1978

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1413,6 +1413,19 @@ const variables: Record<string, (...args: any) => any> = {
14131413
]
14141414
),
14151415

1416+
/**
1417+
* Snowflake OAuth token path.
1418+
*/
1419+
snowflakeOAuthTokenPath: ({
1420+
dataSource
1421+
}: {
1422+
dataSource: string,
1423+
}) => (
1424+
process.env[
1425+
keyByDataSource('CUBEJS_DB_SNOWFLAKE_OAUTH_TOKEN_PATH', dataSource)
1426+
]
1427+
),
1428+
14161429
/**
14171430
* Snowflake private key.
14181431
*/

packages/cubejs-snowflake-driver/src/SnowflakeDriver.ts

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
DriverCapabilities,
2525
} from '@cubejs-backend/base-driver';
2626
import { formatToTimeZone } from 'date-fns-timezone';
27+
import fs from 'fs/promises';
2728
import { HydrationMap, HydrationStream } from './HydrationStream';
2829

2930
// eslint-disable-next-line import/order
@@ -163,6 +164,7 @@ interface SnowflakeDriverOptions {
163164
clientSessionKeepAlive?: boolean,
164165
database?: string,
165166
authenticator?: string,
167+
token?: string,
166168
privateKeyPath?: string,
167169
privateKeyPass?: string,
168170
privateKey?: string,
@@ -207,7 +209,8 @@ export class SnowflakeDriver extends BaseDriver implements DriverInterface {
207209
'CUBEJS_DB_SNOWFLAKE_CLIENT_SESSION_KEEP_ALIVE',
208210
'CUBEJS_DB_SNOWFLAKE_AUTHENTICATOR',
209211
'CUBEJS_DB_SNOWFLAKE_PRIVATE_KEY_PATH',
210-
'CUBEJS_DB_SNOWFLAKE_PRIVATE_KEY_PASS'
212+
'CUBEJS_DB_SNOWFLAKE_PRIVATE_KEY_PASS',
213+
'CUBEJS_DB_SNOWFLAKE_OAUTH_TOKEN_PATH',
211214
];
212215
}
213216

@@ -388,11 +391,28 @@ export class SnowflakeDriver extends BaseDriver implements DriverInterface {
388391
return undefined;
389392
}
390393

394+
private async readOAuthToken(dataSource: string) {
395+
const tokenPath = getEnv('snowflakeOAuthTokenPath', { dataSource });
396+
const token = await fs.readFile(tokenPath, 'utf8');
397+
return token.trim();
398+
}
399+
400+
private async createConnection() {
401+
if (this.config.authenticator?.toUpperCase() === 'OAUTH') {
402+
this.config.token = await this.readOAuthToken(this.config.dataSource);
403+
}
404+
405+
const connection = snowflake.createConnection(this.config);
406+
407+
return connection;
408+
}
409+
391410
/**
392411
* Test driver's connection.
393412
*/
394413
public async testConnection() {
395-
const connection = snowflake.createConnection(this.config);
414+
const connection = await this.createConnection();
415+
396416
await new Promise(
397417
(resolve, reject) => connection.connect((err, conn) => (err ? reject(err) : resolve(conn)))
398418
);
@@ -411,7 +431,8 @@ export class SnowflakeDriver extends BaseDriver implements DriverInterface {
411431
*/
412432
protected async initConnection() {
413433
try {
414-
const connection = snowflake.createConnection(this.config);
434+
const connection = await this.createConnection();
435+
415436
await new Promise(
416437
(resolve, reject) => connection.connect((err, conn) => (err ? reject(err) : resolve(conn)))
417438
);

0 commit comments

Comments
 (0)