fix: GCA creds loading order (#6498)

This commit is contained in:
Gaurav
2025-08-18 14:11:19 -07:00
committed by GitHub
parent 3960ccf781
commit 5fe4e02310
2 changed files with 86 additions and 17 deletions

View File

@@ -351,27 +351,32 @@ export function getAvailablePort(): Promise<number> {
}
async function loadCachedCredentials(client: OAuth2Client): Promise<boolean> {
try {
const keyFile =
process.env['GOOGLE_APPLICATION_CREDENTIALS'] ||
getCachedCredentialPath();
const pathsToTry = [
getCachedCredentialPath(),
process.env['GOOGLE_APPLICATION_CREDENTIALS'],
].filter((p): p is string => !!p);
const creds = await fs.readFile(keyFile, 'utf-8');
client.setCredentials(JSON.parse(creds));
for (const keyFile of pathsToTry) {
try {
const creds = await fs.readFile(keyFile, 'utf-8');
client.setCredentials(JSON.parse(creds));
// This will verify locally that the credentials look good.
const { token } = await client.getAccessToken();
if (!token) {
return false;
// This will verify locally that the credentials look good.
const { token } = await client.getAccessToken();
if (!token) {
continue;
}
// This will check with the server to see if it hasn't been revoked.
await client.getTokenInfo(token);
return true;
} catch (_) {
// Ignore and try next path.
}
// This will check with the server to see if it hasn't been revoked.
await client.getTokenInfo(token);
return true;
} catch (_) {
return false;
}
return false;
}
async function cacheCredentials(credentials: Credentials) {