-
Notifications
You must be signed in to change notification settings - Fork 678
feat: port GDCH credentials support to Node.js Auth SDK #8301
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
macastelaz
wants to merge
12
commits into
main
Choose a base branch
from
gdch-credentials
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.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
476f16a
feat: port GDCH credentials support to Node.js Auth SDK
macastelaz c5121cb
Make small adjustments to address gemini feedback, focusing on four t…
macastelaz d3c4af0
Fix several issues in the original implementation, identified during …
macastelaz e8080d8
Merge branch 'main' into gdch-credentials
macastelaz a8f70af
Update core/packages/google-auth-library-nodejs/src/auth/gdchclient.ts
macastelaz b2e7e41
perf(auth): cache GDCH CA cert agent to avoid redundant file reads
macastelaz 781ac2d
Use base64urldirectly instead of doing multiple replaces to account f…
macastelaz 30169d0
Merge branch 'main' into gdch-credentials
pearigee 7dfe5ae
test(auth): break up large gdch client test into smaller targeted tests
macastelaz 81be49f
feat(auth): address PR 8301 review comments for GDCH client
macastelaz a91f687
feat(auth): conditionally inject CA cert agent into requestAsync for …
macastelaz d885a6e
feat(auth): implement private key/token redaction and STS token reque…
macastelaz 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
354 changes: 354 additions & 0 deletions
354
core/packages/google-auth-library-nodejs/src/auth/gdchclient.ts
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 |
|---|---|---|
| @@ -0,0 +1,354 @@ | ||
| // Copyright 2026 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| import * as crypto from 'crypto'; | ||
| import * as fs from 'fs'; | ||
| import * as https from 'https'; | ||
| import {GaxiosOptions, GaxiosResponse} from 'gaxios'; | ||
| import { | ||
| GetTokenResponse, | ||
| OAuth2Client, | ||
| OAuth2ClientOptions, | ||
| } from './oauth2client'; | ||
| import {CredentialRequest, Credentials} from './credentials'; | ||
|
|
||
| const DEFAULT_LIFETIME_IN_SECONDS = 3600; | ||
| export const GDCH_SERVICE_ACCOUNT_TYPE = 'gdch_service_account'; | ||
|
|
||
| export interface GdchClientOptions extends OAuth2ClientOptions { | ||
| projectId?: string | null; | ||
| privateKeyId?: string; | ||
| privateKey?: string; | ||
| serviceIdentityName?: string; | ||
| tokenServerUri?: string; | ||
| caCertPath?: string; | ||
| apiAudience?: string; | ||
| lifetime?: number; | ||
| } | ||
|
|
||
| export interface GdchCredentialsInput { | ||
| type: 'gdch_service_account'; | ||
| format_version: string; | ||
| project: string; | ||
| private_key_id: string; | ||
| private_key: string; | ||
| name: string; | ||
| token_uri: string; | ||
| ca_cert_path?: string; | ||
| } | ||
|
|
||
| export class GdchClient extends OAuth2Client { | ||
| projectId?: string; | ||
| privateKeyId?: string; | ||
| privateKey?: string; | ||
| serviceIdentityName?: string; | ||
| tokenServerUri?: string; | ||
| caCertPath?: string; | ||
| apiAudience?: string; | ||
| lifetime: number; | ||
| private gdchOptions: GdchClientOptions; | ||
| private caAgentPromise?: Promise<https.Agent>; | ||
| private cachedCaCertPath?: string; | ||
| private lastCaCertReadTime = 0; | ||
| private readonly CA_CERT_TTL_MS = 5 * 60 * 1000; | ||
|
|
||
| constructor(options: GdchClientOptions = {}) { | ||
| super(options); | ||
| this.gdchOptions = options; | ||
| this.projectId = options.projectId || undefined; | ||
| this.privateKeyId = options.privateKeyId; | ||
| this.privateKey = options.privateKey; | ||
| this.serviceIdentityName = options.serviceIdentityName; | ||
| this.tokenServerUri = options.tokenServerUri; | ||
| this.caCertPath = options.caCertPath; | ||
| this.apiAudience = options.apiAudience; | ||
| this.lifetime = options.lifetime || DEFAULT_LIFETIME_IN_SECONDS; | ||
|
|
||
| // Start with an expired refresh token, which will automatically be | ||
| // refreshed before the first API call is made. | ||
| this.credentials = {refresh_token: 'gdch-placeholder', expiry_date: 1}; | ||
| } | ||
|
|
||
| createWithGdchAudience(apiAudience: string): GdchClient { | ||
| if (!apiAudience) { | ||
| throw new Error( | ||
| 'Audience cannot be null or empty for GDCH service account credentials.' | ||
| ); | ||
| } | ||
| return new GdchClient({ | ||
| ...this.gdchOptions, | ||
| projectId: this.projectId, | ||
| privateKeyId: this.privateKeyId, | ||
| privateKey: this.privateKey, | ||
| serviceIdentityName: this.serviceIdentityName, | ||
| tokenServerUri: this.tokenServerUri, | ||
| caCertPath: this.caCertPath, | ||
| lifetime: this.lifetime, | ||
| apiAudience, | ||
| }); | ||
| } | ||
|
|
||
| fromJSON(json: GdchCredentialsInput): void { | ||
| if (!json) { | ||
| throw new Error( | ||
| 'Must pass in a JSON object containing the GDCH credentials settings.' | ||
| ); | ||
| } | ||
| if (json.type !== GDCH_SERVICE_ACCOUNT_TYPE) { | ||
| throw new Error( | ||
| `The incoming JSON object does not have the "${GDCH_SERVICE_ACCOUNT_TYPE}" type` | ||
| ); | ||
| } | ||
| if (json.format_version !== '1') { | ||
| throw new Error('Only format version 1 is supported.'); | ||
| } | ||
| if (!json.project) { | ||
| throw new Error('The incoming JSON object does not contain a project field'); | ||
| } | ||
| if (!json.private_key_id) { | ||
| throw new Error( | ||
| 'The incoming JSON object does not contain a private_key_id field' | ||
| ); | ||
| } | ||
| if (!json.private_key) { | ||
| throw new Error('The incoming JSON object does not contain a private_key field'); | ||
| } | ||
| if (!json.name) { | ||
| throw new Error('The incoming JSON object does not contain a name field'); | ||
| } | ||
| if (!json.token_uri) { | ||
| throw new Error('The incoming JSON object does not contain a token_uri field'); | ||
| } | ||
|
|
||
| this.projectId = json.project; | ||
| this.privateKeyId = json.private_key_id; | ||
| this.privateKey = json.private_key; | ||
| this.serviceIdentityName = json.name; | ||
| this.tokenServerUri = json.token_uri; | ||
| this.caCertPath = json.ca_cert_path; | ||
|
|
||
| this.gdchOptions = { | ||
| ...this.gdchOptions, | ||
| projectId: json.project, | ||
| privateKeyId: json.private_key_id, | ||
| privateKey: json.private_key, | ||
| serviceIdentityName: json.name, | ||
| tokenServerUri: json.token_uri, | ||
| caCertPath: json.ca_cert_path, | ||
| }; | ||
| } | ||
|
|
||
| protected async refreshTokenNoCache(): Promise<GetTokenResponse> { | ||
| if (!this.apiAudience) { | ||
| throw new Error( | ||
| 'Audience cannot be null or empty for GDCH service account credentials. ' + | ||
| 'Specify the audience by calling createWithGdchAudience.' | ||
| ); | ||
| } | ||
| if (!this.privateKey) { | ||
| throw new Error('Private key is not configured for GDCH credentials.'); | ||
| } | ||
| if (!this.privateKeyId) { | ||
| throw new Error('Private key ID is not configured for GDCH credentials.'); | ||
| } | ||
| if (!this.projectId) { | ||
| throw new Error('Project is not configured for GDCH credentials.'); | ||
| } | ||
| if (!this.serviceIdentityName) { | ||
| throw new Error('Service identity name is not configured for GDCH credentials.'); | ||
| } | ||
| if (!this.tokenServerUri) { | ||
| throw new Error('Token server URI is not configured for GDCH credentials.'); | ||
| } | ||
|
|
||
| const assertion = this.createAssertion(); | ||
|
|
||
| const data = { | ||
| audience: this.apiAudience, | ||
| grant_type: 'urn:ietf:params:oauth:token-type:token-exchange', | ||
| requested_token_type: 'urn:ietf:params:oauth:token-type:access_token', | ||
| subject_token: assertion, | ||
| subject_token_type: 'urn:k8s:params:oauth:token-type:serviceaccount', | ||
| }; | ||
|
|
||
| const requestOpts: GaxiosOptions = { | ||
| url: this.tokenServerUri, | ||
| method: 'POST', | ||
| headers: { | ||
| 'Content-Type': 'application/json', | ||
| }, | ||
| data, | ||
| responseType: 'json', | ||
| timeout: 10000, | ||
| retry: true, | ||
| retryConfig: { | ||
| httpMethodsToRetry: ['POST'], | ||
| statusCodesToRetry: [[500, 599]], | ||
| noResponseRetries: 3, | ||
| }, | ||
| }; | ||
|
macastelaz marked this conversation as resolved.
|
||
|
|
||
| if (this.caCertPath) { | ||
| requestOpts.agent = await this.getCaAgent(); | ||
| } | ||
|
|
||
| try { | ||
| const res = await this.transporter.request<CredentialRequest>(requestOpts); | ||
| const tokenResponse = res.data; | ||
| if (!tokenResponse.access_token) { | ||
| throw new Error('Token response did not contain an access_token.'); | ||
| } | ||
| const tokens: Credentials = { | ||
| access_token: tokenResponse.access_token, | ||
| token_type: 'STS-Bearer', | ||
| }; | ||
|
|
||
| if (tokenResponse.expires_in) { | ||
| tokens.expiry_date = Date.now() + tokenResponse.expires_in * 1000; | ||
| } | ||
|
|
||
| this.emit('tokens', tokens); | ||
| return {res, tokens}; | ||
| } catch (e: any) { | ||
| if (e && e.config && e.config.data) { | ||
| try { | ||
| if (typeof e.config.data === 'string') { | ||
| const parsedData = JSON.parse(e.config.data); | ||
| if (parsedData.subject_token) { | ||
| parsedData.subject_token = '***REDACTED***'; | ||
| e.config.data = JSON.stringify(parsedData); | ||
| } | ||
| } else if (typeof e.config.data === 'object' && e.config.data.subject_token) { | ||
| e.config.data.subject_token = '***REDACTED***'; | ||
| } | ||
| } catch {} | ||
| } | ||
| if (e instanceof Error) { | ||
| e.message = `Error getting access token for GDCH service account: ${e.message}, iss: ${this.serviceIdentityName}`; | ||
| } | ||
| throw e; | ||
| } | ||
| } | ||
|
macastelaz marked this conversation as resolved.
|
||
|
|
||
| private createAssertion(): string { | ||
| const header = { | ||
| alg: 'ES256', | ||
| typ: 'JWT', | ||
| kid: this.privateKeyId, | ||
| }; | ||
|
|
||
| const issSub = `system:serviceaccount:${this.projectId}:${this.serviceIdentityName}`; | ||
| const currentTime = Math.floor(Date.now() / 1000); | ||
| const payload = { | ||
| iss: issSub, | ||
| sub: issSub, | ||
| iat: currentTime, | ||
| exp: currentTime + this.lifetime, | ||
| aud: this.tokenServerUri, | ||
| }; | ||
|
|
||
| const encodedHeader = this.base64UrlEncode(JSON.stringify(header)); | ||
| const encodedPayload = this.base64UrlEncode(JSON.stringify(payload)); | ||
| const signingInput = `${encodedHeader}.${encodedPayload}`; | ||
|
|
||
| const signature = crypto.sign( | ||
| 'sha256', | ||
| Buffer.from(signingInput), | ||
| { | ||
| key: this.privateKey!, | ||
| dsaEncoding: 'ieee-p1363', | ||
| } | ||
| ); | ||
|
|
||
| const encodedSignature = this.base64UrlEncode(signature); | ||
| return `${signingInput}.${encodedSignature}`; | ||
| } | ||
|
|
||
|
|
||
| override async requestAsync<T>( | ||
| opts: GaxiosOptions, | ||
| retry = false | ||
| ): Promise<GaxiosResponse<T>> { | ||
| if (this.caCertPath && !opts.agent) { | ||
| const url = (opts.url || '').toString(); | ||
| if (!url.includes('googleapis.com') && !url.includes('google.com')) { | ||
| opts.agent = await this.getCaAgent(); | ||
| } | ||
| } | ||
| return super.requestAsync(opts, retry); | ||
| } | ||
|
|
||
| private getCaAgent(): Promise<https.Agent> | undefined { | ||
| if (!this.caCertPath) { | ||
| this.caAgentPromise = undefined; | ||
| this.cachedCaCertPath = undefined; | ||
| this.lastCaCertReadTime = 0; | ||
| return undefined; | ||
| } | ||
|
|
||
| const now = Date.now(); | ||
| const isCacheExpired = now - this.lastCaCertReadTime > this.CA_CERT_TTL_MS; | ||
|
|
||
| if ( | ||
| this.caAgentPromise && | ||
| this.caCertPath === this.cachedCaCertPath && | ||
| !isCacheExpired | ||
| ) { | ||
| return this.caAgentPromise; | ||
| } | ||
|
|
||
| this.cachedCaCertPath = this.caCertPath; | ||
| this.lastCaCertReadTime = now; | ||
| const currentPath = this.caCertPath; | ||
| this.caAgentPromise = (async () => { | ||
| try { | ||
| const ca = await fs.promises.readFile(currentPath); | ||
| return new https.Agent({ca}); | ||
| } catch (err) { | ||
| if (this.cachedCaCertPath === currentPath) { | ||
| this.caAgentPromise = undefined; | ||
| this.cachedCaCertPath = undefined; | ||
| this.lastCaCertReadTime = 0; | ||
| } | ||
| if (err instanceof Error) { | ||
| err.message = `Error reading certificate file from CA cert path, value '${currentPath}': ${err.message}`; | ||
| } | ||
| throw err; | ||
| } | ||
| })(); | ||
|
|
||
| return this.caAgentPromise; | ||
| } | ||
|
macastelaz marked this conversation as resolved.
|
||
|
|
||
| toJSON(): Record<string, any> { | ||
| return { | ||
| ...this, | ||
| privateKey: this.privateKey ? '***REDACTED***' : undefined, | ||
| credentials: { | ||
| ...this.credentials, | ||
| access_token: this.credentials?.access_token ? '***REDACTED***' : undefined, | ||
| refresh_token: this.credentials?.refresh_token ? '***REDACTED***' : undefined, | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| [Symbol.for('nodejs.util.inspect.custom')]() { | ||
| return this.toJSON(); | ||
| } | ||
|
|
||
| private base64UrlEncode(str: string | Buffer): string { | ||
| const buffer = typeof str === 'string' ? Buffer.from(str) : str; | ||
| return buffer.toString('base64url'); | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.