UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

156 lines (155 loc) • 5.22 kB
import { o as resolveUserPath } from "./home-dir-CjKH62jd.mjs"; import { c as isRecord } from "./record-coerce-TIsquoVR.mjs"; import "./utils-DUAwk2p5.mjs"; import { a as resolveAgentDir } from "./agent-scope-config-BU4qc9bA.mjs"; import { i as listAgentIds } from "./agent-roster-B45JERT5.mjs"; import { t as createConfigIO } from "./io.factory-SFi7FTm2.mjs"; import "./agent-scope-DWB4x52T.mjs"; import { t as isNonEmptyString } from "./shared-cn7yr9R-.mjs"; import { o as listAuthProfileSecretTargetEntries } from "./target-registry-query-C2DhrDiw.mjs"; import "./target-registry-CvjdRZuO.mjs"; import "./config-B-SAEPyK.mjs"; import { c as resolveSharedAuthStorePath } from "./path-resolve-qRO5Wefj.mjs"; import { h as resolveAuthProfileDatabasePath } from "./sqlite-EkUBHrKK.mjs"; import { n as privateFileStoreSync } from "./private-file-store-DM0i141q.mjs"; import fs from "node:fs"; import path from "node:path"; import { replaceFileAtomicSync } from "@openclaw/fs-safe/atomic"; //#region src/secrets/auth-profiles-scan.ts /** Scans auth-profile stores for plaintext credentials, SecretRefs, and OAuth tokens. */ function getAuthProfileFieldName(pathPattern) { const segments = pathPattern.split(".").filter(Boolean); return segments[segments.length - 1] ?? ""; } const AUTH_PROFILE_FIELD_SPEC_BY_TYPE = (() => { const defaults = { api_key: { valueField: "key", refField: "keyRef" }, token: { valueField: "token", refField: "tokenRef" } }; for (const target of listAuthProfileSecretTargetEntries()) { if (!target.authProfileType) continue; defaults[target.authProfileType] = { valueField: getAuthProfileFieldName(target.pathPattern), refField: target.refPathPattern !== void 0 ? getAuthProfileFieldName(target.refPathPattern) : defaults[target.authProfileType].refField }; } return defaults; })(); function toSecretCredentialVisit(params) { const spec = AUTH_PROFILE_FIELD_SPEC_BY_TYPE[params.kind]; return { kind: params.kind, profileId: params.profileId, provider: params.provider, profile: params.profile, valueField: spec.valueField, refField: spec.refField, value: params.profile[spec.valueField], refValue: params.profile[spec.refField] }; } /** Iterates credential-bearing auth profiles with normalized field metadata for audit/apply. */ function* iterateAuthProfileCredentials(profiles) { for (const [profileId, value] of Object.entries(profiles)) { if (!isRecord(value) || !isNonEmptyString(value.provider)) continue; const provider = value.provider; if (value.type === "api_key" || value.type === "token") { yield toSecretCredentialVisit({ kind: value.type, profileId, provider, profile: value }); continue; } if (value.type === "oauth") yield { kind: "oauth", profileId, provider, profile: value, hasAccess: isNonEmptyString(value.access), hasRefresh: isNonEmptyString(value.refresh) }; } } //#endregion //#region src/secrets/auth-store-paths.ts /** Discovers auth-profile store paths that may contain secret refs. */ /** Lists canonical auth-profile databases that may contain SecretRefs. */ function listAuthProfileStoreTargets(config, stateDir, env = process.env) { const targets = /* @__PURE__ */ new Map(); const scopedEnv = { ...env, OPENCLAW_STATE_DIR: stateDir, OPENCLAW_AGENT_DIR: void 0 }; const addTarget = (target) => { const key = path.resolve(target.path); if (targets.get(key)?.kind === "shared") return; targets.set(key, target); }; addTarget({ kind: "shared", path: resolveSharedAuthStorePath(scopedEnv), env: scopedEnv, stateDir }); const agentsRoot = path.join(resolveUserPath(stateDir, scopedEnv), "agents"); if (fs.existsSync(agentsRoot)) for (const entry of fs.readdirSync(agentsRoot, { withFileTypes: true })) { if (!entry.isDirectory()) continue; const agentDir = path.join(agentsRoot, entry.name, "agent"); addTarget({ kind: "agent", agentDir, path: resolveAuthProfileDatabasePath(agentDir) }); } for (const agentId of listAgentIds(config)) { const agentDir = resolveUserPath(resolveAgentDir(config, agentId, scopedEnv), scopedEnv); addTarget({ kind: "agent", agentDir, path: resolveAuthProfileDatabasePath(agentDir) }); } return [...targets.values()]; } //#endregion //#region src/secrets/config-io.ts /** Config IO adapter used by secrets apply/configure flows. */ const silentConfigIoLogger = { error: () => {}, warn: () => {} }; /** * Creates config I/O for secrets commands with config-loader logging suppressed. */ function createSecretsConfigIO(params) { return createConfigIO({ env: params.env, logger: silentConfigIoLogger }); } /** * Atomically writes secret-adjacent text, using the private store for default 0600 files. */ function writeTextFileAtomic(pathname, value, mode = 384) { if (mode !== 384) { replaceFileAtomicSync({ filePath: pathname, content: value, mode, tempPrefix: ".openclaw-secrets" }); return; } privateFileStoreSync(path.dirname(pathname)).writeText(path.basename(pathname), value); } //#endregion export { iterateAuthProfileCredentials as i, writeTextFileAtomic as n, listAuthProfileStoreTargets as r, createSecretsConfigIO as t };