feat: 补齐 Piper 集群地址环境变量并明确功能授权边界

e562ae9 移除旧 HOSE_GRAY_GATEWAY_URL 逃生口后,两个 Piper 域名一直
硬编码,且 webhook.ts 把同一域名又写了一遍,改常量不会跟着变。

- 新增 core/http/piper-endpoints.ts 收口常量与 resolvePiperBaseUrl(),
  运行时读取 HOSE_PIPER_BASE_URL / HOSE_PIPER_TEST_BASE_URL;
  script-engine-client.ts 保留历史导出并改用运行时解析。
- 工作流引擎客户端、Webhook 触发 URL、灰度节点开通三处统一走
  resolvePiperBaseUrl,不再各写一份域名。
- EASO 换票默认地址复用 EKB_BASE_URL,消除第三份硬编码。
- .env.example 补齐 Piper、AI PRO 凭证与签发方、管理员密码变量;
  README 新增环境变量小节;README.transport.md 同步。

纠正上一轮的误判:hose-use 是免费功能,凭证只作用于 hose-aipro,
「hose-use 写命令未受凭证约束」不是安全缺口。该边界已写入
README、hose-aipro Skill 与项目记忆,不再列为待办。

校验:bun test 1103 pass / 0 fail,bun run check、check:readme 通过。
This commit is contained in:
gitea committed 2026-11-05 17:13:20 +08:00
1 parent d772ff0164
commit 13c47d7569
10 files changed
+112 -11

No files matched your search

+32
View File
@@ -0,0 +1,32 @@
import { afterEach, describe, expect, test } from "bun:test";
import { resolvePiperBaseUrl } from "./piper-endpoints.js";
const KEYS = ["HOSE_PIPER_BASE_URL", "HOSE_PIPER_TEST_BASE_URL"] as const;
afterEach(() => {
for (const key of KEYS) delete process.env[key];
});
describe("Piper 集群地址", () => {
test("未设置环境变量时使用默认地址", () => {
expect(resolvePiperBaseUrl()).toBe("https://piper.ekuaibao.com");
expect(resolvePiperBaseUrl(true)).toBe("https://piper-test.ekuaibao.net");
});
test("HOSE_PIPER_BASE_URL 覆盖标准集群并去掉尾部斜线", () => {
process.env.HOSE_PIPER_BASE_URL = "https://piper.internal.example///";
expect(resolvePiperBaseUrl()).toBe("https://piper.internal.example");
expect(resolvePiperBaseUrl(true)).toBe("https://piper-test.ekuaibao.net");
});
test("HOSE_PIPER_TEST_BASE_URL 只覆盖测试集群", () => {
process.env.HOSE_PIPER_TEST_BASE_URL = "https://piper-test.internal.example/";
expect(resolvePiperBaseUrl(true)).toBe("https://piper-test.internal.example");
expect(resolvePiperBaseUrl()).toBe("https://piper.ekuaibao.com");
});
test("空白值回退默认", () => {
process.env.HOSE_PIPER_BASE_URL = " ";
expect(resolvePiperBaseUrl()).toBe("https://piper.ekuaibao.com");
});
});
+24
View File
@@ -0,0 +1,24 @@
/**
* Piper / ScriptEngine 集群地址收口。
*
* 保持轻量(不依赖 ky、不触发网络),供 core、domains 各层共用。
* 旧版 HOSE_GRAY_GATEWAY_URL 逃生口在 e562ae9 移除后一直缺位,这里补齐。
*/
const DEFAULT_PIPER_BASE_URL = "https://piper.ekuaibao.com";
const DEFAULT_PIPER_TEST_BASE_URL = "https://piper-test.ekuaibao.net";
function envBaseUrl(value: string | undefined, fallback: string): string {
const normalized = value?.trim().replace(/\/+$/, "");
return normalized || fallback;
}
/** 运行时解析,每次读取环境变量,便于私有化/多集群部署与测试覆盖。 */
export function resolvePiperBaseUrl(isTest = false): string {
return isTest
? envBaseUrl(process.env.HOSE_PIPER_TEST_BASE_URL, DEFAULT_PIPER_TEST_BASE_URL)
: envBaseUrl(process.env.HOSE_PIPER_BASE_URL, DEFAULT_PIPER_BASE_URL);
}
/** 兼容历史常量导入;值为进程启动时的解析结果。 */
export const SCRIPT_ENGINE_BASE_URL = resolvePiperBaseUrl(false);
export const SCRIPT_ENGINE_TEST_BASE_URL = resolvePiperBaseUrl(true);
+11 -4
View File
@@ -16,10 +16,17 @@ import {
setCorpTokens,
} from "../auth/token-store.js";
export const SCRIPT_ENGINE_BASE_URL = "https://piper.ekuaibao.com";
import {
resolvePiperBaseUrl,
SCRIPT_ENGINE_BASE_URL,
SCRIPT_ENGINE_TEST_BASE_URL,
} from "./piper-endpoints.js";
/** 测试 Piper 集群,仅供显式 --is-test 的工作流灰度开通使用。 */
export const SCRIPT_ENGINE_TEST_BASE_URL = "https://piper-test.ekuaibao.net";
export {
resolvePiperBaseUrl,
SCRIPT_ENGINE_BASE_URL,
SCRIPT_ENGINE_TEST_BASE_URL,
} from "./piper-endpoints.js";
// 兼容历史常量名
export const PIPER_BASE_URL = SCRIPT_ENGINE_BASE_URL;
@@ -120,7 +127,7 @@ export async function getScriptEngineClient(
const cachedJwt = mem.jwt;
const cachedHoseToken = mem.hoseToken;
return createAuthClient({
prefixUrl: SCRIPT_ENGINE_BASE_URL,
prefixUrl: resolvePiperBaseUrl(),
getHeaders: () => {
const headers: Record<string, string> = {
Authorization: `Bearer ${scriptEngineMemoryCache.get(cacheKey)?.jwt ?? cachedJwt}`,
@@ -21,7 +21,7 @@
| 项目 | 说明 |
|---|---|
| 换票 | 通过企业 `baseUrl` 的 `/api/engine/realm/$EASO/actions/jwt` 获取 EASO JWT;复用 `fetchEasoJwt`,去除 `baseUrl` 末尾斜线 |
| 开通请求 | 携带 EASO JWT 直接 POST 到 Piper 的 `/api/easo/dsl/corporation`;默认使用 `SCRIPT_ENGINE_BASE_URL`,`isTest: true` 时使用 `https://piper-test.ekuaibao.net`。每个企业的工作流只在其中一个 Piper 环境,不会同时支持两者;请求失败时不自动切换 |
| 开通请求 | 携带 EASO JWT 直接 POST 到 Piper 的 `/api/easo/dsl/corporation`;地址经 `resolvePiperBaseUrl(isTest)` 解析,可用 `HOSE_PIPER_BASE_URL` / `HOSE_PIPER_TEST_BASE_URL` 覆盖,常量收口在 `core/http/piper-endpoints.ts`。每个企业的工作流只在其中一个 Piper 环境,不会同时支持两者;请求失败时不自动切换 |
| payload | 由编排层 `catalog/catalog.ts` 显式构造;固定附带 `recalculate-the-approval-flow`、`ui-notification`、`start`,dry-run 与成功输出均会展示 |
| 成功判定 | 传输层只做响应与认证校验;业务码 `code === 200`、`saved`/`skipped` 由编排层判定,HTTP 200 本身不作为成功依据 |
| 生产拦截 | CLI 层 `handleGrantNodes` 对 `isCorpProd` 企业直接拒绝,无 `--force` 逃生口;`--is-test` 只是 Piper 域名开关,不构成环境授权 |
@@ -1,7 +1,8 @@
import { z } from "zod";
import ky, { type KyInstance } from "ky";
import { EkbApiError, EkbError } from "../../../../../core/errors.js";
import { fetchEasoJwt, SCRIPT_ENGINE_BASE_URL, SCRIPT_ENGINE_TEST_BASE_URL } from "../../../../../core/http/script-engine-client.js";
import { fetchEasoJwt, resolvePiperBaseUrl } from "../../../../../core/http/script-engine-client.js";
import { EKB_BASE_URL } from "../../../../../core/http/unity-client.js";
import { WorkflowDslSchema, WorkflowDslCatalogSchema, StaffSchema, WorkflowEnumItemSchema, type WorkflowEnumItem } from "../../types.js";
import { unwrap, assertNoAuthFailure } from "../response.js";
@@ -81,11 +82,11 @@ const WorkflowNodeGrantResponseSchema = z.object({
}).passthrough().nullable().optional(),
}).passthrough();
// 兼容历史导入路径;常量已收敛到 core/http/script-engine-client.ts
export { SCRIPT_ENGINE_TEST_BASE_URL };
// 兼容历史导入路径;常量已收敛到 core/http/piper-endpoints.ts
export { SCRIPT_ENGINE_TEST_BASE_URL } from "../../../../../core/http/piper-endpoints.js";
function normalizeEkbBaseUrl(baseUrl?: string): string {
return (baseUrl || "https://app.ekuaibao.com").trim().replace(/\/+$/, "");
return (baseUrl || EKB_BASE_URL).trim().replace(/\/+$/, "");
}
export type WorkflowNodeGrantResponse = z.infer<typeof WorkflowNodeGrantResponseSchema>;
@@ -123,7 +124,7 @@ export async function grantWorkflowNodesApi(options: {
try {
// 每个企业的工作流只对应标准或测试 Piper 之一;显式选择,不探测或自动回退。
const piperBaseUrl = options.isTest ? SCRIPT_ENGINE_TEST_BASE_URL : SCRIPT_ENGINE_BASE_URL;
const piperBaseUrl = resolvePiperBaseUrl(options.isTest);
const response = await ky.post(`${piperBaseUrl}/api/easo/dsl/corporation`, {
json: options.payload,
headers: { Authorization: `Bearer ${easoJwt}` },
@@ -1,4 +1,5 @@
import type { WorkflowTask } from "../types.js";
import { resolvePiperBaseUrl } from "../../../../core/http/piper-endpoints.js";
/** 复制的工作流需要独立的 Webhook 地址和样本。 */
export function resetCopiedWebhookTrigger(task?: WorkflowTask): void {
@@ -14,5 +15,7 @@ export function ensureWebhookTriggerPath(task: WorkflowTask): void {
export function webhookTriggerUrl(task: WorkflowTask): string | undefined {
const rawPath = [task.urlPath, task.url, task.path].find((value) => typeof value === "string" && value);
if (typeof rawPath !== "string") return undefined;
return rawPath.startsWith("http") ? rawPath : `https://piper.ekuaibao.com/api/webhook/${rawPath.replace(/^\/+/, "")}`;
return rawPath.startsWith("http")
? rawPath
: `${resolvePiperBaseUrl()}/api/webhook/${rawPath.replace(/^\/+/, "")}`;
}