// 执行机敏感凭证下发与缓存:中心机在线拉取(RSA 解密) + 加密缓存(离线复用)。 // 对齐 Python creds_sync.py:RSA-2048 密钥对 + OAEP-SHA256 解密 + cache.json 密文缓存。 "use strict"; const fs = require("fs"); const path = require("path"); const crypto = require("crypto"); const cp = require("./cpclient"); const config = require("./config"); const session = require("./session"); const { loadOrCreateRsaKey, rsaDecrypt } = require("./crypto"); const { makeLogger } = require("./logger"); const logger = makeLogger("creds-sync"); // 与服务端 config_push._ALLOWED_KEYS 保持一致(双重保障:下发限定范围) const ALLOWED_KEYS = new Set([ "DINGTALK_APPKEY", "DINGTALK_APPSECRET", "DINGTALK_AGENT_ID", "DINGTALK_CORP_ID", ]); const CREDS_DIR = path.join(config.APP_DATA, "creds"); const PRIV_PATH = path.join(CREDS_DIR, "id_rsa.pem"); const CACHE_FILE = path.join(CREDS_DIR, "cache.json"); let _priv = null; function privateKey() { if (!_priv) _priv = loadOrCreateRsaKey(PRIV_PATH); return _priv; } function publicKeyPem() { const key = privateKey(); return crypto.createPublicKey(key).export({ type: "spki", format: "pem" }).toString(); } function decrypt(cipherB64) { return rsaDecrypt(privateKey(), cipherB64); } function saveCache(encrypted) { try { fs.mkdirSync(CREDS_DIR, { recursive: true }); fs.writeFileSync(CACHE_FILE, JSON.stringify(encrypted), "utf-8"); } catch (e) { logger.warning(`敏感凭证缓存写入失败: ${e.message}`); } } function loadCache() { if (!fs.existsSync(CACHE_FILE)) return {}; try { const raw = JSON.parse(fs.readFileSync(CACHE_FILE, "utf-8")); const out = {}; for (const [k, b64] of Object.entries(raw || {})) { if (!ALLOWED_KEYS.has(k)) continue; try { out[k] = decrypt(b64); } catch (e) { logger.warning(`敏感凭证缓存解密失败: ${k}`); } } return out; } catch (e) { return {}; } } // 中心机在线拉取并解密敏感凭证;不可用返回 null(走缓存兜底) async function sync(account) { if (!config.CONTROLPLANE_URL || !account || !session.cloudToken(account)) return null; try { const headers = { Authorization: `Bearer ${session.cloudToken(account)}`, }; if (config.CONTROLPLANE_TOKEN) headers["X-API-Token"] = config.CONTROLPLANE_TOKEN; // 1. 上报本机 RSA 公钥(幂等 upsert;中心机用该公钥加密下发) await cp.request("POST", "/api/admin/creds/register", { headers, json: { account, pubkey_pem: publicKeyPem() }, timeout: 10, }); // 2. 拉取加密后的敏感凭证 const r = await cp.request("GET", "/api/admin/creds", { headers, timeout: 10 }); const encrypted = r.creds || {}; if (Object.keys(encrypted).length === 0) return {}; // 3. 本机私钥逐个解密 const out = {}; for (const [k, b64] of Object.entries(encrypted)) { if (!ALLOWED_KEYS.has(k)) continue; try { out[k] = decrypt(b64); } catch (e) { logger.warning(`敏感凭证解密失败: ${k}`); } } // 4. 密文原样落盘缓存(离线复用) saveCache(encrypted); return out; } catch (e) { logger.warning(`中心机敏感凭证拉取失败(用本地缓存): ${e.message}`); return null; } } async function load(account = "") { account = account || config.CONTROLPLANE_ACCOUNT; if (!account) account = session.sessActiveAccount(); const synced = await sync(account); if (synced !== null) return synced; const cached = loadCache(); if (Object.keys(cached).length) logger.info("使用本地敏感凭证缓存(中心机不可达或无云令牌)"); return cached; } module.exports = { load, sync, publicKeyPem, decrypt, ALLOWED_KEYS, PRIV_PATH };