openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
100 lines (99 loc) • 3.26 kB
JavaScript
import { t as _usingCtx } from "./usingCtx-rhpPzaNq.mjs";
import { u as sealSecretSentinel } from "./sentinel-BfPjneyJ.mjs";
import { r as assertSecretStoreValue } from "./secret-store-validation-CU_ws9sE.mjs";
import { s as readSelectedSecretStoreEntries } from "./secret-store-BZfjrX_2.mjs";
import { t as startSecretEgressProxyWorker } from "./proxy-worker-Bb92Xxcx.mjs";
import path from "node:path";
import fs from "node:fs/promises";
import os from "node:os";
//#region src/secrets/job-egress.ts
function assertJobEnvName(e) {
if (!/^[A-Za-z_][A-Za-z0-9_]*$/u.test(e)) throw new Error("Setup egress environment names must be valid POSIX names");
}
/** One owner for standalone jobs' proxy, grants, cancellation, and CA cleanup. */
async function withJobSecretEgress(e, t) {
try {
var n = _usingCtx();
e.signal?.throwIfAborted();
const r = {};
const i = e.secrets.map((e) => {
assertJobEnvName(e.envName);
assertSecretStoreValue(e.value, "secret", e.name);
const t = sealSecretSentinel(e.value, { label: `job-egress:${e.name}` });
r[e.envName] = t;
return {
name: e.name,
sentinel: t,
allowedHosts: e.allowedHosts
};
});
const a = n.a(new AsyncDisposableStack());
const o = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-job-egress-"));
a.defer(() => fs.rm(o, {
recursive: true,
force: true
}));
e.signal?.throwIfAborted();
const s = await startSecretEgressProxyWorker({
caDir: o,
allowedHosts: e.allowedHosts,
onAudit: () => {}
});
a.defer(() => s.stop());
e.signal?.throwIfAborted();
const c = await s.registerProcess(i);
a.defer(() => {
c.revoke();
e.signal?.removeEventListener("abort", c.revoke);
});
e.signal?.addEventListener("abort", c.revoke, { once: true });
e.signal?.throwIfAborted();
const l = await fs.readFile(c.env.NODE_EXTRA_CA_CERTS, "utf8");
e.signal?.throwIfAborted();
const u = await t({
remoteEnv: r,
hostEnv: c.env,
caBundle: l,
revoke: c.revoke
});
e.signal?.throwIfAborted();
return u;
} catch (e) {
n.e = e;
} finally {
await n.d();
}
}
/** Reads only selected protected store rows and binds their destination policy to one job. */
async function withSetupSecretEgress(e, t) {
e.signal.throwIfAborted();
const n = Object.entries(e.secrets).map(([e, t]) => {
assertJobEnvName(e);
if (t.source !== "store") throw new Error("Setup egress requires shared-store SecretRefs");
return {
envName: e,
name: t.id
};
});
const r = await readSelectedSecretStoreEntries({
names: n.map((e) => e.name),
assertCurrent: () => e.signal.throwIfAborted()
});
const i = new Map(r.map((e) => [e.name, e]));
return withJobSecretEgress({
secrets: n.map(({ envName: e, name: t }) => {
const n = i.get(t);
if (!n) throw new Error(`Setup egress secret "${t}" was not found as a protected shared-store entry`);
if (!n.allowedHosts.length) throw new Error(`Setup secret "${t}" has no destination bindings; run openclaw secrets store set ${t} --allow-host <host> before retrying`);
return {
envName: e,
name: n.name,
value: n.value,
allowedHosts: n.allowedHosts
};
}),
signal: e.signal
}, t);
}
//#endregion
export { withSetupSecretEgress as n, withJobSecretEgress as t };