Skip to content

Commit 6376201

Browse files
KSDaemonFrank-TXS
authored andcommitted
feat(snowflake-driver): Introduce CUBEJS_DB_SNOWFLAKE_OAUTH_TOKEN env var / config option (cube-js#9752)
* feat(snowflake-driver): Introduce CUBEJS_DB_SNOWFLAKE_OAUTH_TOKEN env var / config option * update docs
1 parent e525020 commit 6376201

File tree

3 files changed

+48
-4
lines changed

3 files changed

+48
-4
lines changed

docs/pages/product/configuration/data-sources/snowflake.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ if [dedicated infrastructure][ref-dedicated-infra] is used. Check out the
8484
| `CUBEJS_DB_SNOWFLAKE_PRIVATE_KEY` | The content of the private RSA key | Content of the private RSA key (encrypted or not) ||
8585
| `CUBEJS_DB_SNOWFLAKE_PRIVATE_KEY_PATH` | The path to the private RSA key | A valid path to the private RSA key ||
8686
| `CUBEJS_DB_SNOWFLAKE_PRIVATE_KEY_PASS` | The password for the private RSA key. Only required for encrypted keys | A valid password for the encrypted private RSA key ||
87+
| `CUBEJS_DB_SNOWFLAKE_OAUTH_TOKEN` | The OAuth token | A valid OAuth token (string) ||
8788
| `CUBEJS_DB_SNOWFLAKE_OAUTH_TOKEN_PATH` | The path to the valid oauth toket file | A valid path for the oauth token file ||
8889
| `CUBEJS_DB_SNOWFLAKE_HOST` | Host address to which the driver should connect | A valid hostname ||
8990
| `CUBEJS_DB_SNOWFLAKE_QUOTED_IDENTIFIERS_IGNORE_CASE` | Whether or not [quoted identifiers should be case insensitive][link-snowflake-quoted-identifiers]. Default is `false` | `true`, `false` ||

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

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

1572+
/**
1573+
* Snowflake OAuth token (string).
1574+
*/
1575+
snowflakeOAuthToken: ({
1576+
dataSource
1577+
}: {
1578+
dataSource: string,
1579+
}) => (
1580+
process.env[
1581+
keyByDataSource('CUBEJS_DB_SNOWFLAKE_OAUTH_TOKEN', dataSource)
1582+
]
1583+
),
1584+
15721585
/**
15731586
* Snowflake OAuth token path.
15741587
*/

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

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ interface SnowflakeDriverOptions {
153153
clientSessionKeepAlive?: boolean,
154154
database?: string,
155155
authenticator?: string,
156+
oauthToken?: string,
156157
oauthTokenPath?: string,
157158
token?: string,
158159
privateKeyPath?: string,
@@ -189,7 +190,7 @@ export class SnowflakeDriver extends BaseDriver implements DriverInterface {
189190
/**
190191
* Returns the configurable driver options
191192
* Note: It returns the unprefixed option names.
192-
* In case of using multisources options need to be prefixed manually.
193+
* In case of using multi-sources options need to be prefixed manually.
193194
*/
194195
public static driverEnvVariables() {
195196
return [
@@ -202,6 +203,7 @@ export class SnowflakeDriver extends BaseDriver implements DriverInterface {
202203
'CUBEJS_DB_SNOWFLAKE_ROLE',
203204
'CUBEJS_DB_SNOWFLAKE_CLIENT_SESSION_KEEP_ALIVE',
204205
'CUBEJS_DB_SNOWFLAKE_AUTHENTICATOR',
206+
'CUBEJS_DB_SNOWFLAKE_OAUTH_TOKEN',
205207
'CUBEJS_DB_SNOWFLAKE_OAUTH_TOKEN_PATH',
206208
'CUBEJS_DB_SNOWFLAKE_HOST',
207209
'CUBEJS_DB_SNOWFLAKE_PRIVATE_KEY',
@@ -286,6 +288,7 @@ export class SnowflakeDriver extends BaseDriver implements DriverInterface {
286288
username: getEnv('dbUser', { dataSource }),
287289
password: getEnv('dbPass', { dataSource }),
288290
authenticator: getEnv('snowflakeAuthenticator', { dataSource }),
291+
oauthToken: getEnv('snowflakeOAuthToken', { dataSource }),
289292
oauthTokenPath: getEnv('snowflakeOAuthTokenPath', { dataSource }),
290293
privateKeyPath: getEnv('snowflakePrivateKeyPath', { dataSource }),
291294
privateKeyPass: getEnv('snowflakePrivateKeyPass', { dataSource }),
@@ -423,12 +426,39 @@ export class SnowflakeDriver extends BaseDriver implements DriverInterface {
423426
return token.trim();
424427
}
425428

426-
private async createConnection() {
429+
private async prepareConnectOptions(): Promise<snowflake.ConnectionOptions> {
430+
const config: Record<string, any> = {
431+
account: this.config.account,
432+
region: this.config.region,
433+
host: this.config.host,
434+
application: this.config.application,
435+
authenticator: this.config.authenticator,
436+
clientSessionKeepAlive: this.config.clientSessionKeepAlive,
437+
database: this.config.database,
438+
warehouse: this.config.warehouse,
439+
role: this.config.role,
440+
resultPrefetch: this.config.resultPrefetch,
441+
};
442+
427443
if (this.config.authenticator?.toUpperCase() === 'OAUTH') {
428-
this.config.token = await this.readOAuthToken();
444+
config.token = this.config.oauthToken || await this.readOAuthToken();
445+
} else if (this.config.authenticator?.toUpperCase() === 'SNOWFLAKE_JWT') {
446+
config.username = this.config.username;
447+
config.privateKey = this.config.privateKey;
448+
config.privateKeyPath = this.config.privateKeyPath;
449+
config.privateKeyPass = this.config.privateKeyPass;
450+
} else {
451+
config.username = this.config.username;
452+
config.password = this.config.password;
429453
}
430454

431-
return snowflake.createConnection(this.config);
455+
return config as snowflake.ConnectionOptions;
456+
}
457+
458+
private async createConnection() {
459+
const config = await this.prepareConnectOptions();
460+
461+
return snowflake.createConnection(config);
432462
}
433463

434464
/**

0 commit comments

Comments
 (0)