Skip to content

Commit d2b587b

Browse files
committed
use default credential provider chain if access key and secret key are not provided
1 parent 540fabe commit d2b587b

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1613,6 +1613,16 @@ const variables: Record<string, (...args: any) => any> = {
16131613
]
16141614
),
16151615

1616+
duckdbS3UseCredentialChain: ({
1617+
dataSource
1618+
}: {
1619+
dataSource: string,
1620+
}) => (
1621+
process.env[
1622+
keyByDataSource('CUBEJS_DB_DUCKDB_S3_USE_CREDENTIAL_CHAIN', dataSource)
1623+
]
1624+
),
1625+
16161626
/**
16171627
* Presto catalog.
16181628
*/

packages/cubejs-duckdb-driver/src/DuckDBDriver.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export type DuckDBDriverConfiguration = {
2020
dataSource?: string,
2121
initSql?: string,
2222
schema?: string,
23+
duckdbS3UseCredentialChain?: boolean,
2324
};
2425

2526
type InitPromise = {
@@ -145,7 +146,7 @@ export class DuckDBDriver extends BaseDriver implements DriverInterface {
145146
value: getEnv('duckdbS3SessionToken', this.config),
146147
}
147148
];
148-
149+
149150
for (const { key, value } of configuration) {
150151
if (value) {
151152
try {
@@ -159,6 +160,17 @@ export class DuckDBDriver extends BaseDriver implements DriverInterface {
159160
}
160161
}
161162
}
163+
const useCredentialChain = getEnv('duckdbS3UseCredentialChain', this.config);
164+
if (useCredentialChain === 'true' || useCredentialChain === true) {
165+
try {
166+
await execAsync('CREATE SECRET (TYPE S3, PROVIDER \'CREDENTIAL_CHAIN\')');
167+
} catch (e) {
168+
if (this.logger) {
169+
console.error('DuckDB - error on creating S3 credential chain secret', { e });
170+
}
171+
throw e;
172+
}
173+
}
162174

163175
if (this.config.initSql) {
164176
try {

packages/cubejs-duckdb-driver/test/DuckDBDriver.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,23 @@ describe('DuckDBDriver', () => {
7373
{ id: '3', created: '2020-03-03T03:03:03.333Z', created_date: '2020-03-03T00:00:00.000Z', price: '300' }
7474
]);
7575
});
76+
77+
test('should execute CREATE SECRET when duckdbS3UseCredentialChain is set', async () => {
78+
process.env.duckdbS3UseCredentialChain = 'true';
79+
80+
// Create a new driver instance to pick up the environment variable
81+
const driverWithCredentialChain = new DuckDBDriver({});
82+
83+
// Mock the execAsync method to spy on it
84+
const execAsyncSpy = jest.spyOn((driverWithCredentialChain as any), 'execAsync');
85+
86+
await driverWithCredentialChain.testConnection();
87+
88+
expect(execAsyncSpy).toHaveBeenCalledWith(`CREATE SECRET (TYPE S3, PROVIDER 'CREDENTIAL_CHAIN')`);
89+
90+
// Clean up
91+
delete process.env.duckdbS3UseCredentialChain;
92+
execAsyncSpy.mockRestore();
93+
await driverWithCredentialChain.release();
94+
});
7695
});

0 commit comments

Comments
 (0)