Skip to content

Commit 2ae5669

Browse files
lonexrebsteipete
andauthored
fix(agents): align plugin tool auth with provider runtime (#103852)
* fix(agents): count env/config credentials in plugin tool provider-auth gating (#103828) The plugin-tool availability gate (hasAuthForProvider) consulted only the auth profile store, so tools like image analysis rejected providers whose API key arrives via the process environment (including keys injected through OPENCLAW_SERVICE_MANAGED_ENV_KEYS) or config apiKey SecretRefs - paths chat routing resolves fine via resolveApiKeyForProvider. Route the gate through the shared hasProviderAuthForTool helper the media model selection already uses, which layers runtime env, custom-provider SecretRefs, managed secrets, and the profile store. * fix(agents): preserve unprepared auth fallback Keep direct env lookup for callers without a prepared runtime auth lookup while allowing prepared plugin-tool composition to skip redundant metadata discovery. --------- Co-authored-by: Peter Steinberger <[email protected]>
1 parent 5578d01 commit 2ae5669

4 files changed

Lines changed: 224 additions & 32 deletions

File tree

‎src/agents/openclaw-plugin-tools.ts‎

Lines changed: 66 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ import type { OpenClawConfig } from "../config/types.openclaw.js";
88
import { resolvePluginTools } from "../plugins/tools.js";
99
import { resolveApiKeyForProfile, resolveAuthProfileOrder } from "./auth-profiles.js";
1010
import type { AuthProfileStore } from "./auth-profiles/types.js";
11+
import {
12+
createRuntimeProviderAuthLookup,
13+
hasRuntimeAvailableProviderAuth,
14+
resolveApiKeyForProvider as resolveProviderAuth,
15+
} from "./model-auth.js";
1116
import { createNodePluginTools } from "./node-plugin-tools.js";
1217
import {
1318
resolveOpenClawPluginToolInputs,
@@ -16,6 +21,7 @@ import {
1621
import { applyPluginToolDeliveryDefaults } from "./plugin-tool-delivery-defaults.js";
1722
import { resolveAgentRuntimeToolConfig } from "./tool-runtime-config.js";
1823
import type { AnyAgentTool } from "./tools/common.js";
24+
import { hasProviderAuthForTool } from "./tools/model-config.helpers.js";
1925

2026
type ResolveOpenClawPluginToolsOptions = OpenClawPluginToolOptions & {
2127
pluginToolAllowlist?: string[];
@@ -50,23 +56,42 @@ export function resolveOpenClawPluginToolsForOptions(params: {
5056
// while tests can still inject a fixed resolvedConfig.
5157
return resolveAgentRuntimeToolConfig(params.resolvedConfig ?? params.options?.config);
5258
};
59+
const pluginToolInputs = resolveOpenClawPluginToolInputs({
60+
options: params.options,
61+
resolvedConfig: params.resolvedConfig,
62+
runtimeConfig: resolveCurrentRuntimeConfig(),
63+
getRuntimeConfig: resolveCurrentRuntimeConfig,
64+
});
5365
const authProfileStore = params.options?.authProfileStore;
54-
const resolveAuthProfileIdsForProvider = authProfileStore
55-
? (providerId: string): string[] =>
56-
resolveAuthProfileOrder({
57-
cfg: resolveCurrentRuntimeConfig(),
58-
store: authProfileStore,
59-
provider: providerId,
60-
})
66+
const availabilityConfig = resolveCurrentRuntimeConfig();
67+
const availabilityRuntimeLookup = authProfileStore
68+
? createRuntimeProviderAuthLookup({
69+
cfg: availabilityConfig,
70+
workspaceDir: pluginToolInputs.context.workspaceDir,
71+
includePluginSyntheticAuth: false,
72+
})
6173
: undefined;
6274
const hasAuthForProvider = authProfileStore
63-
? (providerId: string) => (resolveAuthProfileIdsForProvider?.(providerId) ?? []).length > 0
75+
? (providerId: string) =>
76+
hasProviderAuthForTool({
77+
provider: providerId,
78+
cfg: availabilityConfig,
79+
workspaceDir: pluginToolInputs.context.workspaceDir,
80+
agentDir: params.options?.agentDir,
81+
authStore: authProfileStore,
82+
runtimeLookup: availabilityRuntimeLookup,
83+
})
6484
: undefined;
6585
const resolveApiKeyForProvider = authProfileStore
6686
? async (providerId: string): Promise<string | undefined> => {
67-
for (const profileId of resolveAuthProfileIdsForProvider?.(providerId) ?? []) {
87+
const cfg = resolveCurrentRuntimeConfig();
88+
for (const profileId of resolveAuthProfileOrder({
89+
cfg,
90+
store: authProfileStore,
91+
provider: providerId,
92+
})) {
6893
const resolved = await resolveApiKeyForProfile({
69-
cfg: resolveCurrentRuntimeConfig(),
94+
cfg,
7095
store: authProfileStore,
7196
profileId,
7297
agentDir: params.options?.agentDir,
@@ -75,15 +100,39 @@ export function resolveOpenClawPluginToolsForOptions(params: {
75100
return resolved.apiKey;
76101
}
77102
}
78-
return undefined;
103+
const workspaceDir = pluginToolInputs.context.workspaceDir;
104+
const runtimeLookup = createRuntimeProviderAuthLookup({
105+
cfg,
106+
workspaceDir,
107+
includePluginSyntheticAuth: false,
108+
});
109+
if (
110+
!hasRuntimeAvailableProviderAuth({
111+
provider: providerId,
112+
cfg,
113+
workspaceDir,
114+
allowPluginSyntheticAuth: false,
115+
runtimeLookup,
116+
})
117+
) {
118+
return undefined;
119+
}
120+
try {
121+
const resolved = await resolveProviderAuth({
122+
provider: providerId,
123+
cfg,
124+
store: authProfileStore,
125+
agentDir: params.options?.agentDir,
126+
workspaceDir,
127+
credentialPrecedence: "env-first",
128+
allowAuthProfileFallback: false,
129+
});
130+
return resolved.apiKey;
131+
} catch {
132+
return undefined;
133+
}
79134
}
80135
: undefined;
81-
const pluginToolInputs = resolveOpenClawPluginToolInputs({
82-
options: params.options,
83-
resolvedConfig: params.resolvedConfig,
84-
runtimeConfig: resolveCurrentRuntimeConfig(),
85-
getRuntimeConfig: resolveCurrentRuntimeConfig,
86-
});
87136
const existingToolNames = new Set(params.existingToolNames ?? []);
88137
const pluginTools = resolvePluginTools({
89138
...pluginToolInputs,

‎src/agents/openclaw-tools.browser-plugin.integration.test.ts‎

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
// Verifies OpenClaw plugin tools are resolved with browser/runtime context.
2+
import os from "node:os";
3+
import path from "node:path";
24
import { afterEach, describe, expect, it, vi } from "vitest";
35
import type { OpenClawConfig } from "../config/config.js";
46
import { resetConfigRuntimeState, setRuntimeConfigSnapshot } from "../config/config.js";
@@ -9,6 +11,7 @@ import { resolveOpenClawPluginToolsForOptions } from "./openclaw-plugin-tools.js
911
const hoisted = vi.hoisted(() => ({
1012
resolvePluginTools: vi.fn(),
1113
}));
14+
const TEST_AGENT_DIR = path.join(os.tmpdir(), "openclaw-plugin-tool-auth-test");
1215

1316
vi.mock("../plugins/tools.js", () => ({
1417
resolvePluginTools: (...args: unknown[]) => hoisted.resolvePluginTools(...args),
@@ -26,6 +29,7 @@ function firstResolvePluginToolsParams(): Record<string, unknown> {
2629
describe("createOpenClawTools browser plugin integration", () => {
2730
afterEach(() => {
2831
hoisted.resolvePluginTools.mockReset();
32+
vi.unstubAllEnvs();
2933
clearSecretsRuntimeSnapshot();
3034
resetConfigRuntimeState();
3135
});
@@ -177,6 +181,7 @@ describe("createOpenClawTools browser plugin integration", () => {
177181
resolveOpenClawPluginToolsForOptions({
178182
options: {
179183
config,
184+
agentDir: TEST_AGENT_DIR,
180185
authProfileStore: {
181186
version: 1,
182187
profiles: {
@@ -203,6 +208,118 @@ describe("createOpenClawTools browser plugin integration", () => {
203208
);
204209
});
205210

211+
it("keeps provider availability and credential resolution aligned for env-only auth", async () => {
212+
const envName = "OPENCLAW_PLUGIN_TOOL_AUTH_TEST_KEY";
213+
vi.stubEnv(envName, "env-only-key");
214+
let capturedParams:
215+
| {
216+
hasAuthForProvider?: (providerId: string) => boolean;
217+
context?: {
218+
hasAuthForProvider?: (providerId: string) => boolean;
219+
resolveApiKeyForProvider?: (providerId: string) => Promise<string | undefined>;
220+
};
221+
}
222+
| undefined;
223+
hoisted.resolvePluginTools.mockImplementation((params: unknown) => {
224+
capturedParams = params as typeof capturedParams;
225+
return [];
226+
});
227+
const config = {
228+
models: {
229+
providers: {
230+
acme: {
231+
baseUrl: "https://example.com/v1",
232+
apiKey: `\${${envName}}`,
233+
models: [],
234+
},
235+
},
236+
},
237+
plugins: { allow: ["xai"] },
238+
} as OpenClawConfig;
239+
240+
resolveOpenClawPluginToolsForOptions({
241+
options: {
242+
config,
243+
agentDir: TEST_AGENT_DIR,
244+
workspaceDir: "/workspace",
245+
authProfileStore: { version: 1, profiles: {} },
246+
},
247+
resolvedConfig: config,
248+
});
249+
250+
expect(capturedParams?.hasAuthForProvider?.("acme")).toBe(true);
251+
expect(capturedParams?.context?.hasAuthForProvider?.("acme")).toBe(true);
252+
await expect(capturedParams?.context?.resolveApiKeyForProvider?.("acme")).resolves.toBe(
253+
"env-only-key",
254+
);
255+
});
256+
257+
it("keeps ordered profile precedence when runtime auth is also available", async () => {
258+
vi.stubEnv("ACME_API_KEY", "env-key");
259+
let resolveApiKeyForProvider: ((providerId: string) => Promise<string | undefined>) | undefined;
260+
hoisted.resolvePluginTools.mockImplementation((params: unknown) => {
261+
resolveApiKeyForProvider = (
262+
params as {
263+
context?: {
264+
resolveApiKeyForProvider?: (providerId: string) => Promise<string | undefined>;
265+
};
266+
}
267+
).context?.resolveApiKeyForProvider;
268+
return [];
269+
});
270+
const config = {
271+
auth: { order: { acme: ["acme:profile"] } },
272+
models: {
273+
providers: {
274+
acme: {
275+
baseUrl: "https://example.com/v1",
276+
apiKey: "${ACME_API_KEY}",
277+
models: [],
278+
},
279+
},
280+
},
281+
plugins: { allow: ["xai"] },
282+
} as OpenClawConfig;
283+
284+
resolveOpenClawPluginToolsForOptions({
285+
options: {
286+
config,
287+
agentDir: TEST_AGENT_DIR,
288+
authProfileStore: {
289+
version: 1,
290+
profiles: {
291+
"acme:profile": {
292+
type: "api_key",
293+
provider: "acme",
294+
key: "profile-key", // pragma: allowlist secret
295+
},
296+
},
297+
},
298+
},
299+
resolvedConfig: config,
300+
});
301+
302+
await expect(resolveApiKeyForProvider?.("acme")).resolves.toBe("profile-key");
303+
});
304+
305+
it("preserves ungated plugin resolution when no authoritative auth store is supplied", () => {
306+
hoisted.resolvePluginTools.mockReturnValue([]);
307+
const config = { plugins: { allow: ["browser"] } } as OpenClawConfig;
308+
309+
resolveOpenClawPluginToolsForOptions({
310+
options: { config, agentDir: "/unread-auth-store" },
311+
resolvedConfig: config,
312+
});
313+
314+
const params = firstResolvePluginToolsParams() as {
315+
hasAuthForProvider?: unknown;
316+
context?: { hasAuthForProvider?: unknown; resolveApiKeyForProvider?: unknown };
317+
};
318+
expect(params.hasAuthForProvider).toBeUndefined();
319+
expect(params.context?.hasAuthForProvider).toBeUndefined();
320+
expect(params.context?.resolveApiKeyForProvider).toBeUndefined();
321+
});
322+
206323
it("forwards plugin tool deny policy to plugin resolution", () => {
207324
hoisted.resolvePluginTools.mockReturnValue([]);
208325
const config = {

‎src/agents/tools/model-config.helpers.test.ts‎

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,20 @@ vi.mock("../auth-profiles/external-cli-sync.js", () => ({
2020
const authMocks = vi.hoisted(() => ({ resolveEnvApiKey: vi.fn() }));
2121

2222
vi.mock("../model-auth.js", async (importOriginal) => {
23-
const actual = await importOriginal<Record<string, unknown>>();
24-
return { ...actual, resolveEnvApiKey: authMocks.resolveEnvApiKey };
23+
const actual = await importOriginal<typeof import("../model-auth.js")>();
24+
return {
25+
...actual,
26+
resolveEnvApiKey: authMocks.resolveEnvApiKey,
27+
hasRuntimeAvailableProviderAuth: (
28+
params: Parameters<typeof actual.hasRuntimeAvailableProviderAuth>[0],
29+
) => {
30+
const envAuth = authMocks.resolveEnvApiKey(params.provider, params.env, {
31+
config: params.cfg,
32+
workspaceDir: params.workspaceDir,
33+
});
34+
return Boolean(envAuth?.apiKey) || actual.hasRuntimeAvailableProviderAuth(params);
35+
},
36+
};
2537
});
2638

2739
const AGENT_DIR = "/tmp/openclaw-model-config-helper";
@@ -177,7 +189,20 @@ describe("hasProviderAuthForTool", () => {
177189
});
178190

179191
it("rejects providers without config, env, or profile auth", () => {
180-
expect(hasProviderAuthForTool({ provider: "unconfigured-provider" })).toBe(false);
192+
expect(
193+
hasProviderAuthForTool({
194+
provider: "unconfigured-provider",
195+
runtimeLookup: {
196+
envApiKey: {
197+
aliasMap: {},
198+
candidateMap: {},
199+
authEvidenceMap: {},
200+
skipSetupProviderFallback: true,
201+
},
202+
},
203+
}),
204+
).toBe(false);
205+
expect(authMocks.resolveEnvApiKey).toHaveBeenCalledTimes(1);
181206
});
182207
});
183208

‎src/agents/tools/model-config.helpers.ts‎

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {
2828
hasRuntimeAvailableProviderAuth,
2929
resolveProviderEntryApiKeyProfileReference,
3030
resolveEnvApiKey,
31+
type RuntimeProviderAuthLookup,
3132
} from "../model-auth.js";
3233
import { resolveConfiguredModelRef } from "../model-selection.js";
3334

@@ -69,12 +70,14 @@ export function hasAuthForProvider(params: {
6970
workspaceDir?: string;
7071
agentDir?: string;
7172
authStore?: AuthProfileStore;
73+
runtimeLookup?: RuntimeProviderAuthLookup;
7274
}): boolean {
7375
// Env-key resolution is config/workspace aware: plugin-provider env candidates
7476
// come from the metadata snapshot resolved for this config. Non-bundled or
7577
// config-scoped provider plugins are invisible without it, so a config-blind
7678
// lookup would wrongly report "no auth" for env-key providers.
7779
if (
80+
!params.runtimeLookup &&
7881
resolveEnvApiKey(params.provider, undefined, {
7982
config: params.cfg,
8083
workspaceDir: params.workspaceDir,
@@ -131,29 +134,27 @@ export function hasProviderAuthForTool(params: {
131134
workspaceDir?: string;
132135
agentDir?: string;
133136
authStore?: AuthProfileStore;
137+
runtimeLookup?: RuntimeProviderAuthLookup;
134138
}): boolean {
135139
if (
136140
hasRuntimeAvailableProviderAuth({
137141
provider: params.provider,
138142
cfg: params.cfg,
139143
workspaceDir: params.workspaceDir,
140144
allowPluginSyntheticAuth: false,
145+
runtimeLookup: params.runtimeLookup,
141146
})
142147
) {
143148
return true;
144149
}
145-
if (
146-
hasAuthForProvider({
147-
provider: params.provider,
148-
cfg: params.cfg,
149-
workspaceDir: params.workspaceDir,
150-
agentDir: params.agentDir,
151-
authStore: params.authStore,
152-
})
153-
) {
154-
return true;
155-
}
156-
return false;
150+
return hasAuthForProvider({
151+
provider: params.provider,
152+
cfg: params.cfg,
153+
workspaceDir: params.workspaceDir,
154+
agentDir: params.agentDir,
155+
authStore: params.authStore,
156+
runtimeLookup: params.runtimeLookup,
157+
});
157158
}
158159

159160
function formatProviderModelRef(provider: string, model: string): string {

0 commit comments

Comments
 (0)