Skip to content

Commit 02a66be

Browse files
Cad from Arcaarcabotai
authored andcommitted
fix(secrets): resolve active exec refs locally
1 parent aa12e71 commit 02a66be

2 files changed

Lines changed: 245 additions & 3 deletions

File tree

‎src/cli/command-secret-gateway.test.ts‎

Lines changed: 143 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -621,9 +621,8 @@ describe("resolveCommandSecretRefsViaGateway", () => {
621621
});
622622
});
623623

624-
it("keeps local exec SecretRef fallback enabled by default", async () => {
624+
it("resolves active exec SecretRefs locally without calling the gateway", async () => {
625625
const { config, markerPath } = await createExecProviderConfig("talk/providers/api-key");
626-
callGateway.mockRejectedValueOnce(new Error("gateway closed"));
627626

628627
const result = await resolveCommandSecretRefsViaGateway({
629628
config,
@@ -632,10 +631,151 @@ describe("resolveCommandSecretRefsViaGateway", () => {
632631
mode: "read_only_status",
633632
});
634633

634+
expect(callGateway).not.toHaveBeenCalled();
635635
expect(await markerExists(markerPath)).toBe(true);
636636
expect(readTalkProviderApiKey(result.resolvedConfig)).toBe("exec-local-key");
637637
expect(result.targetStatesByPath[TALK_TEST_PROVIDER_API_KEY_PATH]).toBe("resolved_local");
638-
expectGatewayUnavailableLocalFallbackDiagnostics(result);
638+
expect(
639+
result.diagnostics.some((entry) =>
640+
entry.includes(
641+
`memory status: resolved exec SecretRefs locally without gateway secrets.resolve at ${TALK_TEST_PROVIDER_API_KEY_PATH}`,
642+
),
643+
),
644+
).toBe(true);
645+
});
646+
647+
it("falls back to gateway resolution when a local exec provider fails", async () => {
648+
const { config } = await createExecProviderConfig("talk/providers/failing-api-key");
649+
const provider = config.secrets?.providers?.default;
650+
if (!provider || provider.source !== "exec") {
651+
throw new Error("expected exec provider");
652+
}
653+
(provider as { args?: string[] }).args = ["-e", "process.exit(7)"];
654+
callGateway.mockResolvedValueOnce({
655+
assignments: [
656+
{
657+
path: TALK_TEST_PROVIDER_API_KEY_PATH,
658+
pathSegments: [...TALK_TEST_PROVIDER_API_KEY_PATH_SEGMENTS],
659+
value: "gateway-exec-key",
660+
},
661+
],
662+
diagnostics: [],
663+
});
664+
665+
const result = await resolveCommandSecretRefsViaGateway({
666+
config,
667+
commandName: "reply",
668+
targetIds: new Set(["talk.providers.*.apiKey"]),
669+
mode: "read_only_status",
670+
});
671+
672+
expect(callGateway).toHaveBeenCalledTimes(1);
673+
expect(readTalkProviderApiKey(result.resolvedConfig)).toBe("gateway-exec-key");
674+
expect(result.targetStatesByPath[TALK_TEST_PROVIDER_API_KEY_PATH]).toBe("resolved_gateway");
675+
expect(result.hadUnresolvedTargets).toBe(false);
676+
expect(result.diagnostics.some((entry) => entry.includes("continuing with degraded"))).toBe(
677+
false,
678+
);
679+
expect(
680+
result.diagnostics.some(
681+
(entry) => entry.includes("failed to resolve") && entry.includes("locally"),
682+
),
683+
).toBe(false);
684+
});
685+
686+
it("preserves the local exec failure when gateway recovery also leaves the path unresolved", async () => {
687+
const { config } = await createExecProviderConfig("talk/providers/failing-api-key");
688+
const provider = config.secrets?.providers?.default;
689+
if (!provider || provider.source !== "exec") {
690+
throw new Error("expected exec provider");
691+
}
692+
(provider as { args?: string[] }).args = ["-e", "process.exit(7)"];
693+
callGateway.mockResolvedValueOnce({
694+
assignments: [],
695+
diagnostics: ["gateway did not resolve the exec-backed path"],
696+
});
697+
698+
const result = await resolveCommandSecretRefsViaGateway({
699+
config,
700+
commandName: "reply",
701+
targetIds: new Set(["talk.providers.*.apiKey"]),
702+
mode: "read_only_status",
703+
});
704+
705+
expect(callGateway).toHaveBeenCalledTimes(1);
706+
expect(readTalkProviderApiKey(result.resolvedConfig)).toBeUndefined();
707+
expect(result.targetStatesByPath[TALK_TEST_PROVIDER_API_KEY_PATH]).toBe("unresolved");
708+
expect(result.hadUnresolvedTargets).toBe(true);
709+
expect(result.diagnostics).toContain("gateway did not resolve the exec-backed path");
710+
expect(
711+
result.diagnostics.some(
712+
(entry) =>
713+
entry.includes(`failed to resolve ${TALK_TEST_PROVIDER_API_KEY_PATH} locally`) &&
714+
entry.includes("exited with code 7"),
715+
),
716+
).toBe(true);
717+
expect(
718+
result.diagnostics.filter((entry) =>
719+
entry.includes(`failed to resolve ${TALK_TEST_PROVIDER_API_KEY_PATH} locally`),
720+
),
721+
).toHaveLength(1);
722+
expect(
723+
result.diagnostics.some((entry) => entry.includes("skipped local exec SecretRef resolution")),
724+
).toBe(false);
725+
expect(result.diagnostics.some((entry) => entry.includes("rerun with --allow-exec"))).toBe(
726+
false,
727+
);
728+
expect(result.diagnostics).toHaveLength(3);
729+
});
730+
731+
it("keeps non-exec targets on gateway resolution when the command also uses exec refs", async () => {
732+
const { config: execConfig, markerPath } =
733+
await createExecProviderConfig("talk/providers/api-key");
734+
const gatewayPath = "talk.providers.gateway-speech.apiKey";
735+
const config = {
736+
...execConfig,
737+
talk: {
738+
...execConfig.talk,
739+
providers: {
740+
...execConfig.talk?.providers,
741+
"gateway-speech": {
742+
apiKey: { source: "env", provider: "env", id: "GATEWAY_SPEECH_API_KEY" },
743+
},
744+
},
745+
},
746+
secrets: {
747+
providers: {
748+
...execConfig.secrets?.providers,
749+
env: { source: "env" },
750+
},
751+
},
752+
} as OpenClawConfig;
753+
callGateway.mockResolvedValueOnce({
754+
assignments: [
755+
{
756+
path: gatewayPath,
757+
pathSegments: ["talk", "providers", "gateway-speech", "apiKey"],
758+
value: "gateway-only-key",
759+
},
760+
],
761+
diagnostics: [],
762+
});
763+
764+
const result = await resolveCommandSecretRefsViaGateway({
765+
config,
766+
commandName: "reply",
767+
targetIds: new Set(["talk.providers.*.apiKey"]),
768+
mode: "read_only_status",
769+
});
770+
771+
expect(await markerExists(markerPath)).toBe(true);
772+
expect(callGateway).toHaveBeenCalledTimes(1);
773+
expect(readTalkProviderApiKey(result.resolvedConfig)).toBe("exec-local-key");
774+
expect(result.resolvedConfig.talk?.providers?.["gateway-speech"]?.apiKey).toBe(
775+
"gateway-only-key",
776+
);
777+
expect(result.targetStatesByPath[TALK_TEST_PROVIDER_API_KEY_PATH]).toBe("resolved_local");
778+
expect(result.targetStatesByPath[gatewayPath]).toBe("resolved_gateway");
639779
});
640780

641781
it("skips local exec SecretRef fallback when the caller disallows exec providers", async () => {

‎src/cli/command-secret-gateway.ts‎

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,12 +358,14 @@ function classifyConfiguredTargetRefs(params: {
358358
}): {
359359
hasActiveConfiguredRef: boolean;
360360
hasUnknownConfiguredRef: boolean;
361+
activeOrUnknownRefPaths: Set<string>;
361362
diagnostics: string[];
362363
} {
363364
if (params.configuredTargetRefPaths.size === 0) {
364365
return {
365366
hasActiveConfiguredRef: false,
366367
hasUnknownConfiguredRef: false,
368+
activeOrUnknownRefPaths: new Set(),
367369
diagnostics: [],
368370
};
369371
}
@@ -388,6 +390,7 @@ function classifyConfiguredTargetRefs(params: {
388390
const diagnostics = new Set<string>();
389391
let hasActiveConfiguredRef = false;
390392
let hasUnknownConfiguredRef = false;
393+
const activeOrUnknownRefPaths = new Set<string>();
391394

392395
for (const path of params.configuredTargetRefPaths) {
393396
if (
@@ -396,6 +399,7 @@ function classifyConfiguredTargetRefs(params: {
396399
params.optionalActivePaths?.has(path)
397400
) {
398401
hasActiveConfiguredRef = true;
402+
activeOrUnknownRefPaths.add(path);
399403
continue;
400404
}
401405
const inactiveWarning = inactiveWarningsByPath.get(path);
@@ -404,15 +408,37 @@ function classifyConfiguredTargetRefs(params: {
404408
continue;
405409
}
406410
hasUnknownConfiguredRef = true;
411+
activeOrUnknownRefPaths.add(path);
407412
}
408413

409414
return {
410415
hasActiveConfiguredRef,
411416
hasUnknownConfiguredRef,
417+
activeOrUnknownRefPaths,
412418
diagnostics: [...diagnostics],
413419
};
414420
}
415421

422+
function collectExecSecretRefPaths(params: {
423+
config: OpenClawConfig;
424+
targetIds: Set<string>;
425+
paths: ReadonlySet<string>;
426+
}): string[] {
427+
const defaults = params.config.secrets?.defaults;
428+
return commandSecretGatewayDeps
429+
.discoverConfigSecretTargetsByIds(params.config, params.targetIds)
430+
.filter((target) => params.paths.has(target.path))
431+
.filter((target) => {
432+
const { ref } = resolveSecretInputRef({
433+
value: target.value,
434+
refValue: target.refValue,
435+
defaults,
436+
});
437+
return ref?.source === "exec";
438+
})
439+
.map((target) => target.path);
440+
}
441+
416442
function parseGatewaySecretsResolveResult(payload: unknown): {
417443
assignments: Array<{ path?: string; pathSegments: string[]; value: unknown }>;
418444
diagnostics: string[];
@@ -935,6 +961,82 @@ export async function resolveCommandSecretRefsViaGateway(params: {
935961
hadUnresolvedTargets: false,
936962
};
937963
}
964+
const commandExecSecretRefPaths = resolutionPolicy.allowExecSecretRefs
965+
? collectExecSecretRefPaths({
966+
config: params.config,
967+
targetIds: params.targetIds,
968+
paths: preflight.activeOrUnknownRefPaths,
969+
})
970+
: [];
971+
if (commandExecSecretRefPaths.length > 0) {
972+
const execPaths = new Set(commandExecSecretRefPaths);
973+
const localExec = await resolveCommandSecretRefsLocally({
974+
config: params.config,
975+
commandName: params.commandName,
976+
targetIds: params.targetIds,
977+
preflightDiagnostics: preflight.diagnostics,
978+
mode: "read_only_operational",
979+
allowedPaths: execPaths,
980+
forcedActivePaths: params.forcedActivePaths,
981+
optionalActivePaths: params.optionalActivePaths,
982+
resolutionPolicy: {
983+
...resolutionPolicy,
984+
scrubUnresolvedSecretRefs: false,
985+
},
986+
});
987+
const resolvedExecPaths = new Set(
988+
commandExecSecretRefPaths.filter(
989+
(path) => localExec.targetStatesByPath[path] === "resolved_local",
990+
),
991+
);
992+
const remainingPaths = new Set(
993+
[...preflight.activeOrUnknownRefPaths].filter((path) => !resolvedExecPaths.has(path)),
994+
);
995+
const remaining =
996+
remainingPaths.size > 0
997+
? await resolveCommandSecretRefsViaGateway({
998+
...params,
999+
config: localExec.resolvedConfig,
1000+
allowedPaths: remainingPaths,
1001+
allowLocalExecSecretRefs: false,
1002+
})
1003+
: undefined;
1004+
const targetStatesByPath = {
1005+
...localExec.targetStatesByPath,
1006+
...remaining?.targetStatesByPath,
1007+
};
1008+
const unrecoveredExecDiagnostics = localExec.diagnostics.filter((diagnostic) =>
1009+
commandExecSecretRefPaths.some(
1010+
(path) =>
1011+
targetStatesByPath[path] === "unresolved" &&
1012+
diagnostic.startsWith(`${params.commandName}: failed to resolve ${path} locally (`),
1013+
),
1014+
);
1015+
const attemptedExecSkipDiagnostics = new Set(
1016+
commandExecSecretRefPaths.map(
1017+
(path) =>
1018+
`${params.commandName}: skipped local exec SecretRef resolution for ${path}; rerun with --allow-exec to execute configured exec providers.`,
1019+
),
1020+
);
1021+
const remainingDiagnostics = (remaining?.diagnostics ?? []).filter(
1022+
(diagnostic) => !attemptedExecSkipDiagnostics.has(diagnostic),
1023+
);
1024+
return {
1025+
resolvedConfig: remaining?.resolvedConfig ?? localExec.resolvedConfig,
1026+
diagnostics: dedupeDiagnostics([
1027+
...preflight.diagnostics,
1028+
...(resolvedExecPaths.size > 0
1029+
? [
1030+
`${params.commandName}: resolved exec SecretRefs locally without gateway secrets.resolve at ${[...resolvedExecPaths].join(", ")}.`,
1031+
]
1032+
: []),
1033+
...unrecoveredExecDiagnostics,
1034+
...remainingDiagnostics,
1035+
]),
1036+
targetStatesByPath,
1037+
hadUnresolvedTargets: Object.values(targetStatesByPath).includes("unresolved"),
1038+
};
1039+
}
9381040
const gatewayExecSecretRefCredentialPaths = resolutionPolicy.allowExecSecretRefs
9391041
? []
9401042
: collectActiveGatewayExecSecretRefCredentialPaths(params.config);

0 commit comments

Comments
 (0)