Skip to content

Commit adf78ce

Browse files
authored
refactor(daemon): centralize environment source lookup (#113283)
1 parent e3c2a8b commit adf78ce

7 files changed

Lines changed: 54 additions & 73 deletions

File tree

‎src/commands/daemon-install-helpers.test.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1705,7 +1705,7 @@ describe("buildGatewayInstallPlan — dotenv merge", () => {
17051705
expect(plan.environment.CUSTOM_TOOL_HOME).toBe("/Users/test/.custom-tool");
17061706
});
17071707

1708-
it("keeps source metadata for EnvironmentFile-backed preserved vars", async () => {
1708+
it("keeps differently-cased source metadata for EnvironmentFile-backed preserved vars", async () => {
17091709
mockNodeGatewayPlanFixture({
17101710
serviceEnvironment: {
17111711
HOME: "/from-service",
@@ -1723,9 +1723,9 @@ describe("buildGatewayInstallPlan — dotenv merge", () => {
17231723
OPENCLAW_GATEWAY_TOKEN: "old-token",
17241724
},
17251725
existingEnvironmentValueSources: {
1726-
OPENROUTER_API_KEY: "file",
1727-
CUSTOM_TOOL_HOME: "inline",
1728-
OPENCLAW_GATEWAY_TOKEN: "file",
1726+
openrouter_api_key: "file",
1727+
custom_tool_home: "inline",
1728+
openclaw_gateway_token: "file",
17291729
},
17301730
});
17311731

‎src/commands/daemon-install-helpers.ts‎

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { buildServiceEnvironment } from "../daemon/service-env.js";
2424
import {
2525
formatManagedServiceEnvKeys,
2626
hasEnvironmentFileSource,
27+
readEnvironmentValueSource,
2728
readManagedServiceEnvKeysFromEnvironment,
2829
} from "../daemon/service-managed-env.js";
2930
import { isNonMinimalServicePathEntry } from "../daemon/service-path-policy.js";
@@ -500,22 +501,6 @@ function collectPreservedExistingServiceEnvVars(
500501
return preserved;
501502
}
502503

503-
function readExistingEnvironmentValueSource(params: {
504-
existingEnvironmentValueSources?: Record<
505-
string,
506-
GatewayServiceEnvironmentValueSource | undefined
507-
>;
508-
normalizedKey: string;
509-
}): GatewayServiceEnvironmentValueSource | undefined {
510-
for (const [rawKey, source] of Object.entries(params.existingEnvironmentValueSources ?? {})) {
511-
const key = normalizeEnvVarKey(rawKey, { portable: true })?.toUpperCase();
512-
if (key === params.normalizedKey) {
513-
return source;
514-
}
515-
}
516-
return undefined;
517-
}
518-
519504
function collectExistingEnvironmentFileManagedServiceEnvVars(params: {
520505
existingEnvironment: Record<string, string | undefined> | undefined;
521506
existingEnvironmentValueSources?: Record<
@@ -540,10 +525,10 @@ function collectExistingEnvironmentFileManagedServiceEnvVars(params: {
540525
if (isDangerousHostEnvVarName(key) || isDangerousHostEnvOverrideVarName(key)) {
541526
continue;
542527
}
543-
const source = readExistingEnvironmentValueSource({
544-
existingEnvironmentValueSources: params.existingEnvironmentValueSources,
528+
const source = readEnvironmentValueSource(
529+
params.existingEnvironmentValueSources,
545530
normalizedKey,
546-
});
531+
);
547532
if (!hasEnvironmentFileSource(source)) {
548533
continue;
549534
}
@@ -651,10 +636,7 @@ async function buildGatewayInstallEnvironment(params: {
651636
const plan = createMutableServiceEnvPlan();
652637
addServiceEnvPlanEntries(plan, preservedExistingEnvironment, {
653638
valueSource: ({ normalizedKey }) =>
654-
readExistingEnvironmentValueSource({
655-
existingEnvironmentValueSources: params.existingEnvironmentValueSources,
656-
normalizedKey,
657-
}) ?? "inline",
639+
readEnvironmentValueSource(params.existingEnvironmentValueSources, normalizedKey) ?? "inline",
658640
});
659641
addServiceEnvPlanEntries(plan, stateDirDotEnvEnvironment, {});
660642
addServiceEnvPlanEntries(plan, configEnvironment, {});

‎src/daemon/service-audit.test.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,23 @@ describe("auditGatewayServiceConfig", () => {
715715

716716
expect(hasIssue(audit, SERVICE_AUDIT_CODES.gatewayProxyEnvEmbedded)).toBe(true);
717717
});
718+
719+
it("matches managed and proxy source metadata keys case-insensitively", async () => {
720+
const audit = await createGatewayAudit({
721+
expectedManagedServiceEnvKeys: ["TAVILY_API_KEY"],
722+
extraEnvironment: {
723+
TAVILY_API_KEY: "tvly-test",
724+
HTTPS_PROXY: "https://proxy.local:7890",
725+
},
726+
environmentValueSources: {
727+
tavily_api_key: "file",
728+
https_proxy: "file",
729+
},
730+
});
731+
732+
expect(hasIssue(audit, SERVICE_AUDIT_CODES.gatewayManagedEnvEmbedded)).toBe(false);
733+
expect(hasIssue(audit, SERVICE_AUDIT_CODES.gatewayProxyEnvEmbedded)).toBe(false);
734+
});
718735
});
719736

720737
describe("checkTokenDrift", () => {

‎src/daemon/service-audit.ts‎

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
collectInlineManagedServiceEnvKeys,
2828
hasInlineEnvironmentSource,
2929
isEnvironmentFileOnlySource,
30+
readEnvironmentValueSource,
3031
} from "./service-managed-env.js";
3132
import { isNonMinimalServicePathEntry, normalizeServicePathEntry } from "./service-path-policy.js";
3233
import type { GatewayServiceEnvironmentValueSource } from "./service-types.js";
@@ -393,18 +394,6 @@ function normalizeServiceEnvKey(key: string): string | null {
393394
return normalizeEnvVarKey(key, { portable: true })?.toUpperCase() ?? null;
394395
}
395396

396-
function readEnvironmentValueSource(
397-
command: GatewayServiceCommand,
398-
normalizedKey: string,
399-
): GatewayServiceEnvironmentValueSource | undefined {
400-
for (const [rawKey, source] of Object.entries(command?.environmentValueSources ?? {})) {
401-
if (normalizeServiceEnvKey(rawKey) === normalizedKey) {
402-
return source;
403-
}
404-
}
405-
return undefined;
406-
}
407-
408397
const SERVICE_PROXY_ENV_KEY_SET = new Set(
409398
SERVICE_PROXY_ENV_KEYS.flatMap((key) => {
410399
const normalized = normalizeServiceEnvKey(key);
@@ -425,7 +414,11 @@ function collectInlineProxyEnvKeys(command: GatewayServiceCommand): string[] {
425414
if (!normalized || !SERVICE_PROXY_ENV_KEY_SET.has(normalized)) {
426415
continue;
427416
}
428-
if (!hasInlineEnvironmentSource(readEnvironmentValueSource(command, normalized))) {
417+
if (
418+
!hasInlineEnvironmentSource(
419+
readEnvironmentValueSource(command.environmentValueSources, normalized),
420+
)
421+
) {
429422
continue;
430423
}
431424
inlineKeys.push(normalized);

‎src/daemon/service-managed-env.ts‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,17 @@ export function writeManagedServiceEnvKeysToEnvironment(
114114
environment[MANAGED_SERVICE_ENV_KEYS_VAR] = value;
115115
}
116116

117-
function readEnvironmentValueSource(
118-
command: ServiceEnvCommand,
119-
normalizedKey: string,
117+
export function readEnvironmentValueSource(
118+
environmentValueSources:
119+
| Record<string, GatewayServiceEnvironmentValueSource | undefined>
120+
| undefined,
121+
key: string,
120122
): GatewayServiceEnvironmentValueSource | undefined {
121-
for (const [rawKey, source] of Object.entries(command?.environmentValueSources ?? {})) {
123+
const normalizedKey = normalizeServiceEnvKey(key);
124+
if (!normalizedKey) {
125+
return undefined;
126+
}
127+
for (const [rawKey, source] of Object.entries(environmentValueSources ?? {})) {
122128
if (normalizeServiceEnvKey(rawKey) === normalizedKey) {
123129
return source;
124130
}
@@ -155,7 +161,11 @@ export function collectInlineManagedServiceEnvKeys(
155161
if (normalized === MANAGED_SERVICE_ENV_KEYS_VAR) {
156162
continue;
157163
}
158-
if (!hasInlineEnvironmentSource(readEnvironmentValueSource(command, normalized))) {
164+
if (
165+
!hasInlineEnvironmentSource(
166+
readEnvironmentValueSource(command.environmentValueSources, normalized),
167+
)
168+
) {
159169
continue;
160170
}
161171
// Only inline/file-overlap sources can be repaired from the service command

‎src/daemon/systemd.test.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1322,7 +1322,7 @@ describe("stageSystemdService", () => {
13221322
});
13231323
});
13241324

1325-
it("writes node file-backed managed values to the node env file instead of the unit", async () => {
1325+
it("matches differently-cased source metadata when writing node file-backed values", async () => {
13261326
await withStageFixture(async ({ env, stateDir, unitPath, envFilePath, nodeEnvFilePath }) => {
13271327
await fs.rm(stateDir, { recursive: true, force: true });
13281328
const gatewayPassword = 'symbol " \\ $ `'; // pragma: allowlist secret
@@ -1342,9 +1342,9 @@ describe("stageSystemdService", () => {
13421342
OPENCLAW_SERVICE_KIND: "node",
13431343
},
13441344
environmentValueSources: {
1345-
OPENCLAW_GATEWAY_TOKEN: "file",
1346-
OPENCLAW_GATEWAY_PASSWORD: "file", // pragma: allowlist secret
1347-
OPENCLAW_SERVICE_MANAGED_ENV_KEYS: "inline",
1345+
openclaw_gateway_token: "file",
1346+
openclaw_gateway_password: "file", // pragma: allowlist secret
1347+
openclaw_service_managed_env_keys: "inline",
13481348
},
13491349
});
13501350

‎src/daemon/systemd.ts‎

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import {
3131
hasEnvironmentFileSource,
3232
hasInlineEnvironmentSource,
3333
isEnvironmentFileOnlySource,
34+
readEnvironmentValueSource,
3435
readManagedServiceEnvKeysFromEnvironment,
3536
} from "./service-managed-env.js";
3637
import { createGatewayLifecycleMutationReporter } from "./service-mutation.js";
@@ -254,22 +255,6 @@ function normalizeSystemdEnvironmentKey(key: string): string | null {
254255
return normalizeEnvVarKey(key, { portable: true })?.toUpperCase() ?? null;
255256
}
256257

257-
function readSystemdEnvironmentValueSource(params: {
258-
environmentValueSources?: Record<string, GatewayServiceEnvironmentValueSource | undefined>;
259-
key: string;
260-
}): GatewayServiceEnvironmentValueSource | undefined {
261-
const normalizedKey = normalizeSystemdEnvironmentKey(params.key);
262-
if (!normalizedKey) {
263-
return undefined;
264-
}
265-
for (const [rawKey, source] of Object.entries(params.environmentValueSources ?? {})) {
266-
if (normalizeSystemdEnvironmentKey(rawKey) === normalizedKey) {
267-
return source;
268-
}
269-
}
270-
return undefined;
271-
}
272-
273258
function collectSystemdInlineManagedKeys(params: {
274259
environment?: GatewayServiceEnv;
275260
environmentValueSources?: Record<string, GatewayServiceEnvironmentValueSource | undefined>;
@@ -288,10 +273,7 @@ function collectSystemdInlineManagedKeys(params: {
288273
if (!key) {
289274
continue;
290275
}
291-
const source = readSystemdEnvironmentValueSource({
292-
environmentValueSources: params.environmentValueSources,
293-
key: rawKey,
294-
});
276+
const source = readEnvironmentValueSource(params.environmentValueSources, rawKey);
295277
if (hasInlineEnvironmentSource(source) && !hasEnvironmentFileSource(source)) {
296278
keys.add(key);
297279
}
@@ -1040,10 +1022,7 @@ async function writeSystemdUnit({
10401022
if (typeof value !== "string") {
10411023
return false;
10421024
}
1043-
const source = readSystemdEnvironmentValueSource({
1044-
environmentValueSources,
1045-
key,
1046-
});
1025+
const source = readEnvironmentValueSource(environmentValueSources, key);
10471026
if (hasEnvironmentFileSource(source) && isUnresolvedShellReference(value)) {
10481027
return false;
10491028
}

0 commit comments

Comments
 (0)