-
Notifications
You must be signed in to change notification settings - Fork 206
Handle GCP multi-universe #2323
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
base: main
Are you sure you want to change the base?
Changes from all commits
361779c
2f09b5c
2962ae0
8bf34e9
d99767c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| import { expect } from "chai"; | ||
| import * as fs from "fs-extra"; | ||
| import * as path from "path"; | ||
|
|
||
| import { read } from "df/cli/api/commands/credentials"; | ||
| import { suite, test } from "df/testing"; | ||
| import { TmpDirFixture } from "df/testing/fixtures"; | ||
|
|
||
| suite("credentials", ({ afterEach }) => { | ||
| const tmpDirFixture = new TmpDirFixture(afterEach); | ||
|
|
||
| function writeCredentials(contents: object): string { | ||
| const credentialsPath = path.join(tmpDirFixture.createNewTmpDir(), ".df-credentials.json"); | ||
| fs.writeFileSync(credentialsPath, JSON.stringify(contents)); | ||
| return credentialsPath; | ||
| } | ||
|
|
||
| test("read maps universeDomain when present", () => { | ||
| const credentialsPath = writeCredentials({ | ||
| projectId: "my-project", | ||
| location: "US", | ||
| universeDomain: "my-universe.example.com" | ||
| }); | ||
|
|
||
| const credentials = read(credentialsPath); | ||
|
|
||
| expect(credentials.projectId).to.equal("my-project"); | ||
| expect(credentials.location).to.equal("US"); | ||
| expect(credentials.universeDomain).to.equal("my-universe.example.com"); | ||
| }); | ||
|
|
||
| test("read leaves universeDomain unset when omitted", () => { | ||
| const credentialsPath = writeCredentials({ projectId: "my-project", location: "US" }); | ||
|
|
||
| const credentials = read(credentialsPath); | ||
|
|
||
| expect(credentials.universeDomain).to.satisfy( | ||
| (value: string) => value === "" || value === undefined | ||
| ); | ||
| }); | ||
|
|
||
| test("read rejects unknown fields", () => { | ||
| const credentialsPath = writeCredentials({ projectId: "my-project", notARealField: "x" }); | ||
|
|
||
| expect(() => read(credentialsPath)).to.throw(/notARealField/); | ||
| }); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,7 @@ import { Dataset, Table } from "@google-cloud/bigquery"; | |
| import { expect } from "chai"; | ||
| import { anything, instance, mock, verify, when } from "ts-mockito"; | ||
|
|
||
| import { BigQueryDbAdapter } from "df/cli/api/dbadapters/bigquery"; | ||
| import { BigQueryDbAdapter, createBigQueryClientProvider } from "df/cli/api/dbadapters/bigquery"; | ||
| import { dataform } from "df/protos/ts"; | ||
| import { suite, test } from "df/testing"; | ||
|
|
||
|
|
@@ -146,4 +146,26 @@ suite("BigQueryDbAdapter", () => { | |
|
|
||
| await adapter.setMetadata(action); | ||
| }); | ||
|
|
||
| suite("createBigQueryClientProvider", () => { | ||
|
Collaborator
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. Nit: this suite doesn't exercise |
||
| test("passes universeDomain to the BigQuery client when set", () => { | ||
| const credentials = dataform.BigQuery.create({ | ||
| projectId: "project1", | ||
| location: "US", | ||
| universeDomain: "my-universe.example.com" | ||
| }); | ||
|
|
||
| const client = createBigQueryClientProvider(credentials)(); | ||
|
|
||
| expect(client.universeDomain).to.equal("my-universe.example.com"); | ||
| }); | ||
|
|
||
| test("defaults to googleapis.com when universeDomain is unset", () => { | ||
| const credentials = dataform.BigQuery.create({ projectId: "project1", location: "US" }); | ||
|
|
||
| const client = createBigQueryClientProvider(credentials)(); | ||
|
|
||
| expect(client.universeDomain).to.equal("googleapis.com"); | ||
| }); | ||
| }); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -42,7 +42,10 @@ export function createLineageClientProvider( | |
| apiEndpoint: endpoint, | ||
| credentials: credentials.credentials && JSON.parse(credentials.credentials), | ||
| libName: DATAFORM_CLI_LIB_NAME, | ||
| libVersion: version | ||
| libVersion: version, | ||
| // Omit empty universeDomain: gax uses ??, so "" would skip the default. (BigQuery | ||
| // falsy-checks options.universeDomain, so this guard is not needed there.) | ||
| ...(credentials.universeDomain ? { universeDomain: credentials.universeDomain } : {}) | ||
|
Comment on lines
42
to
+48
Collaborator
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. This doesn't take effect: It's also a bit worse than a no-op: gax compares the configured universe against the credential's before each call, so setting it here makes that check pass while we carry on dialing a GDU endpoint — a loud failure becomes a silent one. Could the endpoint router become universe-aware, or this hunk drop out for now? Either way it needs coverage; |
||
| }) | ||
| ); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import { expect } from "chai"; | ||
|
|
||
| import { credentialsFromServiceAccountJson } from "df/cli/credentials"; | ||
| import { suite, test } from "df/testing"; | ||
|
|
||
| suite("credentialsFromServiceAccountJson", () => { | ||
| function keyJson(overrides: object = {}): string { | ||
| return JSON.stringify({ | ||
| type: "service_account", | ||
| project_id: "my-project", | ||
| private_key: "fake-key", | ||
| ...overrides, | ||
| }); | ||
| } | ||
|
|
||
| test("copies universe_domain from the key so BigQuery matches google-auth", () => { | ||
| const credentials = credentialsFromServiceAccountJson( | ||
| keyJson({ universe_domain: "my-universe.example.com" }), | ||
| "EU", | ||
| ); | ||
|
|
||
| expect(credentials.projectId).to.equal("my-project"); | ||
| expect(credentials.location).to.equal("EU"); | ||
| expect(credentials.universeDomain).to.equal("my-universe.example.com"); | ||
| expect(JSON.parse(credentials.credentials).universe_domain).to.equal("my-universe.example.com"); | ||
| }); | ||
|
|
||
| test("omits universeDomain when the key has no universe_domain", () => { | ||
| const credentials = credentialsFromServiceAccountJson(keyJson(), "US"); | ||
|
|
||
| expect(credentials).to.not.have.property("universeDomain"); | ||
| }); | ||
|
|
||
| test("omits universeDomain when universe_domain is blank", () => { | ||
| const credentials = credentialsFromServiceAccountJson(keyJson({ universe_domain: " " }), "US"); | ||
|
|
||
| expect(credentials).to.not.have.property("universeDomain"); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -57,6 +57,7 @@ To run the CLI integration test against your own GCP project: | |
| - `projectId`: your GCP project id | ||
| - `credentials`: the entire content of your GCP service account key JSON file as a single string (you can generate it with `jq -Rsa < path/to/key.json`). | ||
| - `location`: location to use in your project | ||
| - `universeDomain` (optional): the universe domain to connect to (e.g. `googleapis.com`). Leave unset to use the default Google Default Universe (GDU). Set this only when targeting a non-default universe such as a Trusted Partner Cloud (TPC). | ||
|
Collaborator
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. This covers the integration-test credentials only — is a matching update to the user-facing Cloud docs tracked anywhere? |
||
|
|
||
| Example: | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,10 @@ message BigQuery { | |
| string credentials = 3; | ||
| // Options are listed here: https://cloud.google.com/bigquery/docs/locations | ||
| string location = 4; | ||
| // The universe domain to connect to (e.g. "googleapis.com"). Leave unset to | ||
|
Collaborator
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. Nit: |
||
| // use the default Google Default Universe (GDU). Set this when targeting a | ||
| // Trusted Partner Cloud (TPC) or another non-default universe. | ||
| string universe_domain = 5; | ||
|
|
||
| reserved 2; | ||
| } | ||
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.
read()returns a protobufjs message, so an unset string is deterministically""— this can just beexpect(credentials.universeDomain).to.equal(""). As written it would still pass if the value becameundefined, which is the distinction the guards inbigquery.tsandemitter.tsdepend on.