UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

160 lines (159 loc) • 5.61 kB
import { n as resolveDatabaseFamilyEngines } from "./registry-Dd7FIY0t.mjs"; import { a as withDatabaseEngineSelection, n as getDatabaseEngineSelection } from "./context-DE59wyBs.mjs"; import { n as cloneConfigWithResolutionFacts, p as resolveConfigSecretRef } from "./resolution-facts-xaIKuREs.mjs"; import { o as isSecretResolutionError } from "./resolve-errors-B7cgJIhM.mjs"; import { r as trimToUndefined } from "./credential-planner-DCQbkiQO.mjs"; import { i as resolveGatewayCredentialsFromConfig, r as resolveExplicitGatewayAuth, t as GatewaySecretRefUnavailableError } from "./credentials-UDpmcQCV.mjs"; import { a as isTokenGatewaySecretInputPath, i as isSupportedGatewaySecretInputPath, o as readGatewaySecretInputValue, r as assignResolvedGatewaySecretInput, t as ALL_GATEWAY_SECRET_INPUT_PATHS } from "./secret-input-paths-k_sABTwS.mjs"; import { t as materializeSecretInput } from "./resolve-secret-input-string-DjfC9izQ.mjs"; //#region src/gateway/credentials-secret-inputs.ts async function resolveConfiguredGatewaySecretInput(e) { const t = readGatewaySecretInputValue(e.config, e.path); const n = resolveConfigSecretRef({ config: e.config, path: e.path, value: t, defaults: e.config.secrets?.defaults }); const materialize = () => materializeSecretInput({ config: e.config, value: n ?? t, env: e.env, normalize: trimToUndefined, onResolveRefError: (t) => { if (isSecretResolutionError(t) && t.code === "SECRET_REF_REDACTED_VALUE") throw t; throw new GatewaySecretRefUnavailableError(e.path); } }); const r = n?.source === "store" && !getDatabaseEngineSelection() && resolveDatabaseFamilyEngines(e.config).state === "postgres" ? await withDatabaseEngineSelection(await resolveConfiguredDatabaseEngineSelection(e), materialize) : await materialize(); if (!r) throw new Error(`${e.path} resolved to an empty or non-string value.`); return r; } async function resolveConfiguredDatabaseEngineSelection(e) { const { resolveDatabaseEngineSelection: t } = await import("./config-eNRW_vew.mjs"); try { return await t(e.config, e.env); } catch { throw new GatewaySecretRefUnavailableError(e.path); } } function hasConfiguredGatewaySecretRef(e, t) { return Boolean(resolveConfigSecretRef({ config: e, path: t, value: readGatewaySecretInputValue(e, t), defaults: e.secrets?.defaults })); } function localAuthModeAllowsGatewaySecretInputPath(e) { const { authMode: t, path: n } = e; if (t === "none") return false; if (t === "trusted-proxy") return !isTokenGatewaySecretInputPath(n); if (t === "token") return isTokenGatewaySecretInputPath(n); if (t === "password") return !isTokenGatewaySecretInputPath(n); return true; } function canGatewaySecretInputPathWin(e) { if (!hasConfiguredGatewaySecretRef(e.config, e.path)) return false; const t = e.options.modeOverride ?? (e.config.gateway?.mode === "remote" ? "remote" : "local"); if (t === "local" && !localAuthModeAllowsGatewaySecretInputPath({ authMode: e.config.gateway?.auth?.mode, path: e.path })) return false; const n = `__OPENCLAW_GATEWAY_SECRET_REF_PROBE_${e.path.replaceAll(".", "_")}__`; const r = cloneConfigWithResolutionFacts(e.config); for (const e of ALL_GATEWAY_SECRET_INPUT_PATHS) { if (!hasConfiguredGatewaySecretRef(r, e)) continue; assignResolvedGatewaySecretInput({ config: r, path: e, value: void 0 }); } assignResolvedGatewaySecretInput({ config: r, path: e.path, value: n }); try { const i = resolveGatewayCredentialsFromConfig({ ...e.options, cfg: r, env: e.env }); const a = e.config.gateway?.auth?.mode; const o = i.token === n && (t === "local" && a === "token" || !i.password); const s = i.password === n && (t === "local" && (a === "password" || a === "trusted-proxy") || !i.token); return o || s; } catch { return false; } } /** Test whether resolving a configured secret-ref path could affect selected credentials. */ function gatewaySecretInputPathCanWin(e) { const { path: t, env: n = process.env, ...r } = e; return canGatewaySecretInputPathWin({ options: { ...r, explicitAuth: resolveExplicitGatewayAuth(r.explicitAuth) }, env: n, config: e.config, path: t }); } /** Resolve only secret refs that can win, then select Gateway credentials. */ async function resolveGatewayCredentialsWithSecretInputs(e) { const t = resolveExplicitGatewayAuth(e.explicitAuth); if (t.token || t.password) return t; const n = { ...e, explicitAuth: t }; const r = n.env ?? process.env; const i = n.config; let a = i; const resolvePath = async (e) => { const t = await resolveConfiguredGatewaySecretInput({ config: a, path: e, env: r }); assignResolvedGatewaySecretInput({ config: a, path: e, value: t }); }; for (const e of ALL_GATEWAY_SECRET_INPUT_PATHS) { if (!canGatewaySecretInputPathWin({ options: n, env: r, config: a, path: e })) continue; if (a === i) a = cloneConfigWithResolutionFacts(i); try { await resolvePath(e); } catch (e) { if (isSecretResolutionError(e) && e.code === "SECRET_REF_REDACTED_VALUE") throw e; } } const o = /* @__PURE__ */ new Set(); for (;;) try { return resolveGatewayCredentialsFromConfig({ ...n, cfg: a, env: r }); } catch (e) { if (!(e instanceof GatewaySecretRefUnavailableError)) throw e; const t = e.path; if (!isSupportedGatewaySecretInputPath(t) || o.has(t)) throw e; if (a === i) a = cloneConfigWithResolutionFacts(i); await resolvePath(t); o.add(t); } } //#endregion export { resolveGatewayCredentialsWithSecretInputs as n, gatewaySecretInputPathCanWin as t };