vertex: read ADC files without exists preflight (#60592)

Merged via squash.

Prepared head SHA: 72f7372e97
Co-authored-by: gumadeiras <5599352+gumadeiras@users.noreply.github.com>
Co-authored-by: gumadeiras <5599352+gumadeiras@users.noreply.github.com>
Reviewed-by: @gumadeiras
This commit is contained in:
Gustavo Madeira Santana
2026-04-03 21:13:34 -04:00
committed by GitHub
parent 34cd49faa6
commit 3713b0e506
5 changed files with 143 additions and 35 deletions

View File

@@ -0,0 +1,49 @@
import { afterEach, describe, expect, it, vi } from "vitest";
const { existsSyncMock, readFileSyncMock } = vi.hoisted(() => ({
existsSyncMock: vi.fn(),
readFileSyncMock: vi.fn(),
}));
vi.mock("node:fs", async (importOriginal) => {
const actual = await importOriginal<typeof import("node:fs")>();
existsSyncMock.mockImplementation((pathname) => actual.existsSync(pathname));
readFileSyncMock.mockImplementation((pathname, options) =>
String(pathname) === "/tmp/vertex-adc.json"
? '{"project_id":"vertex-project"}'
: actual.readFileSync(pathname, options as never),
);
return {
...actual,
existsSync: existsSyncMock,
readFileSync: readFileSyncMock,
default: {
...actual,
existsSync: existsSyncMock,
readFileSync: readFileSyncMock,
},
};
});
import { hasAnthropicVertexAvailableAuth, resolveAnthropicVertexProjectId } from "./region.js";
describe("anthropic-vertex ADC reads", () => {
afterEach(() => {
existsSyncMock.mockClear();
readFileSyncMock.mockClear();
});
it("reads explicit ADC credentials without an existsSync preflight", () => {
const env = {
GOOGLE_APPLICATION_CREDENTIALS: "/tmp/vertex-adc.json",
} as NodeJS.ProcessEnv;
existsSyncMock.mockClear();
readFileSyncMock.mockClear();
expect(resolveAnthropicVertexProjectId(env)).toBe("vertex-project");
expect(hasAnthropicVertexAvailableAuth(env)).toBe(true);
expect(existsSyncMock).not.toHaveBeenCalled();
expect(readFileSyncMock).toHaveBeenCalledWith("/tmp/vertex-adc.json", "utf8");
});
});

View File

@@ -1,4 +1,4 @@
import { existsSync, readFileSync } from "node:fs";
import { readFileSync } from "node:fs";
import { homedir, platform } from "node:os";
import { join } from "node:path";
import { resolveProviderEndpoint } from "openclaw/plugin-sdk/provider-http";
@@ -77,26 +77,29 @@ function resolveAnthropicVertexDefaultAdcPath(env: NodeJS.ProcessEnv = process.e
: GCLOUD_DEFAULT_ADC_PATH;
}
function resolveAnthropicVertexAdcCredentialsPath(
function resolveAnthropicVertexAdcCredentialsPathCandidate(
env: NodeJS.ProcessEnv = process.env,
): string | undefined {
const explicitCredentialsPath = normalizeOptionalSecretInput(env.GOOGLE_APPLICATION_CREDENTIALS);
if (explicitCredentialsPath) {
return existsSync(explicitCredentialsPath) ? explicitCredentialsPath : undefined;
}
): string {
return (
normalizeOptionalSecretInput(env.GOOGLE_APPLICATION_CREDENTIALS) ??
resolveAnthropicVertexDefaultAdcPath(env)
);
}
const defaultAdcPath = resolveAnthropicVertexDefaultAdcPath(env);
return existsSync(defaultAdcPath) ? defaultAdcPath : undefined;
function canReadAnthropicVertexAdc(env: NodeJS.ProcessEnv = process.env): boolean {
const credentialsPath = resolveAnthropicVertexAdcCredentialsPathCandidate(env);
try {
readFileSync(credentialsPath, "utf8");
return true;
} catch {
return false;
}
}
function resolveAnthropicVertexProjectIdFromAdc(
env: NodeJS.ProcessEnv = process.env,
): string | undefined {
const credentialsPath = resolveAnthropicVertexAdcCredentialsPath(env);
if (!credentialsPath) {
return undefined;
}
const credentialsPath = resolveAnthropicVertexAdcCredentialsPathCandidate(env);
try {
const parsed = JSON.parse(readFileSync(credentialsPath, "utf8")) as AdcProjectFile;
return (
@@ -109,10 +112,7 @@ function resolveAnthropicVertexProjectIdFromAdc(
}
export function hasAnthropicVertexCredentials(env: NodeJS.ProcessEnv = process.env): boolean {
return (
hasAnthropicVertexMetadataServerAdc(env) ||
resolveAnthropicVertexAdcCredentialsPath(env) !== undefined
);
return hasAnthropicVertexMetadataServerAdc(env) || canReadAnthropicVertexAdc(env);
}
export function hasAnthropicVertexAvailableAuth(env: NodeJS.ProcessEnv = process.env): boolean {