openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 2.62 kB
JavaScript
import{t as _usingCtx}from"./usingCtx-CUVyQ_FT.mjs";import{u as sealSecretSentinel}from"./sentinel-BjpdrOLs.mjs";import{r as assertSecretStoreValue}from"./secret-store-validation-fIUkGoUi.mjs";import{s as readSelectedSecretStoreEntries}from"./secret-store-DkBLQ_7g.mjs";import{t as startSecretEgressProxyWorker}from"./proxy-worker-DOFLBlhi.mjs";import path from"node:path";import fs from"node:fs/promises";import os from"node:os";
//!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`)}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()}}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)}export{withSetupSecretEgress as n,withJobSecretEgress as t};