Skip to content

Commit 97fa19f

Browse files
committed
Simplified logic.
1 parent acecc8b commit 97fa19f

2 files changed

Lines changed: 50 additions & 22 deletions

File tree

core/packages/google-auth-library-nodejs/src/auth/computeclient.ts

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ export class Compute extends OAuth2Client {
4242
private static readonly EMAIL_REGEX = /^[^@]+@[^@]+\.[^@]+$/;
4343
readonly serviceAccountEmail: string;
4444
scopes: string[];
45-
private rabLookupSkippedWarningLogged = false;
46-
private resolvedServiceAccountEmail?: string;
45+
private isNonEmailAccount = false;
4746

4847
/**
4948
* Google Compute Engine service account credentials.
@@ -145,21 +144,18 @@ export class Compute extends OAuth2Client {
145144

146145
/**
147146
* Returns the regional access boundary lookup URL for the GCE instance.
148-
* This implementation resolves the default service account email of the GCE
149-
* instance to construct the lookup endpoint.
147+
* This implementation resolves the service account email of the GCE
148+
* instance to construct the lookup endpoint. If the resolved email is invalid
149+
* or not found, it returns `null` to skip the regional access boundary check.
150150
*
151-
* @return The regional access boundary URL string.
151+
* @return The regional access boundary URL string, or null if regional access
152+
* boundary checks should be skipped.
152153
* @internal
153154
*/
154155
public async getRegionalAccessBoundaryUrl(): Promise<string | null> {
155156
const email = await this.resolveServiceAccountEmail();
156-
if (!email || !Compute.EMAIL_REGEX.test(email)) {
157-
if (!this.rabLookupSkippedWarningLogged) {
158-
AuthClient.log.info(
159-
`RegionalAccessBoundary: Service account email "${email}" is not in a valid email format. Skipping regional access boundary lookup.`,
160-
);
161-
this.rabLookupSkippedWarningLogged = true;
162-
}
157+
if (email === null) {
158+
// This credential corresponds to a non-email account; skip RAB lookup.
163159
return null;
164160
}
165161
const regionalAccessBoundaryUrl = SERVICE_ACCOUNT_LOOKUP_ENDPOINT.replace(
@@ -172,24 +168,34 @@ export class Compute extends OAuth2Client {
172168
/**
173169
* Resolves the service account email. If the email is set to 'default',
174170
* it fetches the email from the GCE metadata server.
175-
* @returns A promise that resolves with the service account email.
171+
* @returns A promise that resolves with the service account email,
172+
* or null if MDS returns an invalid email format
176173
*/
177-
private async resolveServiceAccountEmail(): Promise<string> {
174+
private async resolveServiceAccountEmail(): Promise<string | null> {
175+
if (this.isNonEmailAccount) {
176+
return null;
177+
}
178+
178179
if (this.serviceAccountEmail !== 'default') {
179180
// If a specific email is provided, return it directly.
180181
return this.serviceAccountEmail;
181182
}
182183

183-
if (this.resolvedServiceAccountEmail !== undefined) {
184-
return this.resolvedServiceAccountEmail;
185-
}
186-
187184
// Otherwise, fetch the default email from the metadata server.
188185
try {
189186
const email = await gcpMetadata.instance<string>(
190187
'service-accounts/default/email',
191188
);
192-
this.resolvedServiceAccountEmail = email;
189+
190+
// If the metadata server returned an non-email format, log a warning only once.
191+
if (!email || !Compute.EMAIL_REGEX.test(email)) {
192+
AuthClient.log.info(
193+
`RegionalAccessBoundary: Service account email "${email}" is not in a valid email format. Skipping regional access boundary lookup.`,
194+
);
195+
this.isNonEmailAccount = true;
196+
return null;
197+
}
198+
193199
return email;
194200
} catch (e) {
195201
throw new Error(

core/packages/google-auth-library-nodejs/test/test.compute.ts

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -384,10 +384,32 @@ describe('compute', () => {
384384
assert.strictEqual(url, null);
385385
});
386386

387-
it('should return null from getRegionalAccessBoundaryUrl if custom serviceAccountEmail is not a valid email format', async () => {
388-
const compute = new Compute({serviceAccountEmail: 'not-an-email'});
387+
it('should return valid URL from getRegionalAccessBoundaryUrl if custom serviceAccountEmail is set', async () => {
388+
const email = 'custom-sa@example.com';
389+
const compute = new Compute({serviceAccountEmail: email});
389390
const url = await compute.getRegionalAccessBoundaryUrl();
390-
assert.strictEqual(url, null);
391+
const expectedUrl = SERVICE_ACCOUNT_LOOKUP_ENDPOINT.replace(
392+
'{service_account_email}',
393+
encodeURIComponent(email),
394+
);
395+
assert.strictEqual(url, expectedUrl);
396+
});
397+
398+
it('should return valid URL from getRegionalAccessBoundaryUrl when MDS returns a valid default service account email', async () => {
399+
const compute = new Compute();
400+
const fakeEmail = 'fake-default-sa@developer.gserviceaccount.com';
401+
const metadataStub = sandbox.stub(gcpMetadata, 'instance');
402+
metadataStub.callThrough();
403+
metadataStub
404+
.withArgs('service-accounts/default/email')
405+
.resolves(fakeEmail);
406+
407+
const url = await compute.getRegionalAccessBoundaryUrl();
408+
const expectedUrl = SERVICE_ACCOUNT_LOOKUP_ENDPOINT.replace(
409+
'{service_account_email}',
410+
encodeURIComponent(fakeEmail),
411+
);
412+
assert.strictEqual(url, expectedUrl);
391413
});
392414

393415
it('should NOT trigger asynchronous RAB refresh and NOT attach RAB header if email from metadata server is not a valid email format', async () => {

0 commit comments

Comments
 (0)