-
Notifications
You must be signed in to change notification settings - Fork 190
Add Impersonate Service Account argument #2015
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
Open
wintermi
wants to merge
1
commit into
dataform-co:main
Choose a base branch
from
Conundrm:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+192
−124
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ import Long from "long"; | |
| import { PromisePoolExecutor } from "promise-pool-executor"; | ||
|
|
||
| import { BigQuery, GetTablesResponse, TableField, TableMetadata } from "@google-cloud/bigquery"; | ||
| import { GoogleAuth, Impersonated } from "google-auth-library"; | ||
| import { collectEvaluationQueries, QueryOrAction } from "df/cli/api/dbadapters/execution_sql"; | ||
| import { IBigQueryError, IDbAdapter, IDbClient, IExecutionResult, OnCancel } from "df/cli/api/dbadapters/index"; | ||
| import { parseBigqueryEvalError } from "df/cli/api/utils/error_parsing"; | ||
|
|
@@ -102,8 +103,8 @@ export class BigQueryDbAdapter implements IDbAdapter { | |
| try { | ||
| await this.pool | ||
| .addSingleTask({ | ||
| generator: () => | ||
| this.getClient().query({ | ||
| generator: async () => | ||
| (await this.getClient()).query({ | ||
| useLegacySql: false, | ||
| query, | ||
| dryRun: true | ||
|
|
@@ -128,7 +129,7 @@ export class BigQueryDbAdapter implements IDbAdapter { | |
| } | ||
|
|
||
| public async tables(): Promise<dataform.ITarget[]> { | ||
| const datasets = await this.getClient().getDatasets({ autoPaginate: true, maxResults: 1000 }); | ||
| const datasets = await (await this.getClient()).getDatasets({ autoPaginate: true, maxResults: 1000 }); | ||
| const tables = await Promise.all( | ||
| datasets[0].map(dataset => dataset.getTables({ autoPaginate: true, maxResults: 1000 })) | ||
| ); | ||
|
|
@@ -218,7 +219,7 @@ export class BigQueryDbAdapter implements IDbAdapter { | |
| } | ||
|
|
||
| public async schemas(database: string): Promise<string[]> { | ||
| const data = await this.getClient(database).getDatasets(); | ||
| const data = await (await this.getClient(database)).getDatasets(); | ||
| return data[0].map(dataset => dataset.id); | ||
| } | ||
|
|
||
|
|
@@ -238,7 +239,7 @@ export class BigQueryDbAdapter implements IDbAdapter { | |
| metadata.schema.fields | ||
| ); | ||
|
|
||
| await this.getClient(target.database) | ||
| await (await this.getClient(target.database)) | ||
| .dataset(target.schema) | ||
| .table(target.name) | ||
| .setMetadata({ | ||
|
|
@@ -250,7 +251,7 @@ export class BigQueryDbAdapter implements IDbAdapter { | |
|
|
||
| private async getMetadata(target: dataform.ITarget): Promise<TableMetadata> { | ||
| try { | ||
| const table = await this.getClient(target.database) | ||
| const table = await (await this.getClient(target.database)) | ||
| .dataset(target.schema) | ||
| .table(target.name) | ||
| .getMetadata(); | ||
|
|
@@ -265,19 +266,36 @@ export class BigQueryDbAdapter implements IDbAdapter { | |
| } | ||
| } | ||
|
|
||
| private getClient(projectId?: string) { | ||
| private async getClient(projectId?: string) { | ||
| projectId = projectId || this.bigQueryCredentials.projectId; | ||
| if (!this.clients.has(projectId)) { | ||
| this.clients.set( | ||
| let clientConfig: any = { | ||
| projectId, | ||
| new BigQuery({ | ||
| projectId, | ||
| scopes: EXTRA_GOOGLE_SCOPES, | ||
| location: this.bigQueryCredentials.location, | ||
| credentials: | ||
| this.bigQueryCredentials.credentials && JSON.parse(this.bigQueryCredentials.credentials) | ||
| scopes: EXTRA_GOOGLE_SCOPES, | ||
| location: this.bigQueryCredentials.location | ||
| }; | ||
|
|
||
| if (this.bigQueryCredentials.impersonateServiceAccount) { | ||
| // For impersonation, create an Impersonated credential directly | ||
| const sourceAuth = new GoogleAuth({ | ||
| scopes: ['https://www.googleapis.com/auth/cloud-platform'], | ||
| projectId: projectId, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we need to explicitly set |
||
| credentials: this.bigQueryCredentials.credentials && JSON.parse(this.bigQueryCredentials.credentials), | ||
| }); | ||
|
|
||
| const authClient = await sourceAuth.getClient(); | ||
|
|
||
| clientConfig.authClient = new Impersonated({ | ||
| sourceClient: authClient, | ||
| targetPrincipal: this.bigQueryCredentials.impersonateServiceAccount, | ||
| targetScopes: ['https://www.googleapis.com/auth/cloud-platform'] | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| }) | ||
| ); | ||
| } else { | ||
| clientConfig.credentials = | ||
| this.bigQueryCredentials.credentials && JSON.parse(this.bigQueryCredentials.credentials); | ||
| } | ||
|
|
||
| this.clients.set(projectId, new BigQuery(clientConfig)); | ||
| } | ||
| return this.clients.get(projectId); | ||
| } | ||
|
|
@@ -289,12 +307,12 @@ export class BigQueryDbAdapter implements IDbAdapter { | |
| byteLimit?: number, | ||
| location?: string | ||
| ) { | ||
| const results = await new Promise<any[]>((resolve, reject) => { | ||
| const results = await new Promise<any[]>(async (resolve, reject) => { | ||
| const allRows = new LimitedResultSet({ | ||
| rowLimit, | ||
| byteLimit | ||
| }); | ||
| const stream = this.getClient().createQueryStream({ | ||
| const stream = (await this.getClient()).createQueryStream({ | ||
| query, | ||
| params, | ||
| location | ||
|
|
@@ -330,7 +348,7 @@ export class BigQueryDbAdapter implements IDbAdapter { | |
| return retry( | ||
| async () => { | ||
| try { | ||
| const job = await this.getClient().createQueryJob({ | ||
| const job = await (await this.getClient()).createQueryJob({ | ||
| useLegacySql: false, | ||
| jobPrefix: "dataform-" + (jobPrefix ? `${jobPrefix}-` : ""), | ||
| query, | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's split
getClientinto a separate code line for better readability in all such places.