Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use default credential provider chain for duckdb s3 access #8935

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions packages/cubejs-backend-shared/src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1613,6 +1613,16 @@ const variables: Record<string, (...args: any) => any> = {
]
),

duckdbS3UseCredentialChain: ({
dataSource
}: {
dataSource: string,
}) => (
process.env[
keyByDataSource('CUBEJS_DB_DUCKDB_S3_USE_CREDENTIAL_CHAIN', dataSource)
]
Comment on lines +1621 to +1623
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better to parse and return a boolean here. This will simplify further checks. Have a look at this example:

  snowflakeSessionKeepAlive: ({
    dataSource
  }: {
    dataSource: string,
  }) => {
    const val = process.env[
      keyByDataSource(
        'CUBEJS_DB_SNOWFLAKE_CLIENT_SESSION_KEEP_ALIVE',
        dataSource,
      )
    ];
    if (val) {
      if (val.toLocaleLowerCase() === 'true') {
        return true;
      } else if (val.toLowerCase() === 'false') {
        return false;
      } else {
        throw new TypeError(
          `The ${
            keyByDataSource(
              'CUBEJS_DB_SNOWFLAKE_CLIENT_SESSION_KEEP_ALIVE',
              dataSource,
            )
          } must be either 'true' or 'false'.`
        );
      }
    } else {
      return true;
    }
  },

This way, you can also control the default value if the env var is not set.

),

/**
* Presto catalog.
*/
Expand Down
14 changes: 13 additions & 1 deletion packages/cubejs-duckdb-driver/src/DuckDBDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export type DuckDBDriverConfiguration = {
dataSource?: string,
initSql?: string,
schema?: string,
duckdbS3UseCredentialChain?: boolean,
};

type InitPromise = {
Expand Down Expand Up @@ -145,7 +146,7 @@ export class DuckDBDriver extends BaseDriver implements DriverInterface {
value: getEnv('duckdbS3SessionToken', this.config),
}
];

for (const { key, value } of configuration) {
if (value) {
try {
Expand All @@ -159,6 +160,17 @@ export class DuckDBDriver extends BaseDriver implements DriverInterface {
}
}
}
const useCredentialChain = getEnv('duckdbS3UseCredentialChain', this.config);
if (useCredentialChain === 'true' || useCredentialChain === true) {
try {
await execAsync('CREATE SECRET (TYPE S3, PROVIDER \'CREDENTIAL_CHAIN\')');
} catch (e) {
if (this.logger) {
console.error('DuckDB - error on creating S3 credential chain secret', { e });
}
throw e;
}
}

if (this.config.initSql) {
try {
Expand Down
19 changes: 19 additions & 0 deletions packages/cubejs-duckdb-driver/test/DuckDBDriver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,23 @@ describe('DuckDBDriver', () => {
{ id: '3', created: '2020-03-03T03:03:03.333Z', created_date: '2020-03-03T00:00:00.000Z', price: '300' }
]);
});

test('should execute CREATE SECRET when duckdbS3UseCredentialChain is set', async () => {
process.env.duckdbS3UseCredentialChain = 'true';

// Create a new driver instance to pick up the environment variable
const driverWithCredentialChain = new DuckDBDriver({});

// Mock the execAsync method to spy on it
const execAsyncSpy = jest.spyOn((driverWithCredentialChain as any), 'execAsync');

await driverWithCredentialChain.testConnection();

expect(execAsyncSpy).toHaveBeenCalledWith(`CREATE SECRET (TYPE S3, PROVIDER 'CREDENTIAL_CHAIN')`);

// Clean up
delete process.env.duckdbS3UseCredentialChain;
execAsyncSpy.mockRestore();
await driverWithCredentialChain.release();
});
});
Loading