Compare commits

...

8 Commits

Author SHA1 Message Date
dev 399b5756ea fix(ci): 依赖漏洞例外可追溯到链底公告,picomatch ReDoS 记账
build / build (push) Successful in 2m25s
- check_deps_vuln: 包因依赖有洞被标记时,via 只有依赖名,原实现取不到 GHSA;
  改为沿 via 追到链底公告,fdir/tinyglobby/vite/vitest 与 picomatch 都记同一编号。
- 例外表:GHSA-fc5q-39xc-vjpf 在 npm 上范围 <=4.0.7、4.0.7 已是最新,没有修复版本;
  仅开发依赖、只解析本仓 glob,按策略记例外(到期 2027-02-15)。
2026-11-18 17:57:26 +08:00
姚章浩 3da82d48d9 feat(settings): 服务配置拆为 AI 模型连接,WeKnora 连接并入知识库映射
build / build (push) Failing after 2m22s
- 原「服务配置」更名为「AI 模型连接」,只保留问答分析与中文翻译共用的模型连接
- 测试/生产 WeKnora 地址、生产租户与知识库 ID 迁入「知识库映射」,与入库/封存/生产目标同屏
- 连接配置独立草稿与独立保存,刷新策略不夹带未保存地址
- 同步 README、运维手册、验收清单入口路径
2026-11-18 15:15:13 +08:00
姚章浩 afc38c921f style(settings): 为 DWS 账号补齐与 PP 一致的卡片边框
build / build (push) Successful in 1m49s
DWS 账号区使用与 PP 相同的边框、圆角、内边距、外边距及背景。
仅调整现有 CSS,保留账号信息、授权按钮和检查连接交互。

验证:Prettier 通过;DWS 现有 7 项回归通过;TypeScript/Vite 构建与 git diff --check 通过。
2026-11-17 20:36:08 +08:00
姚章浩 0dd4ad2737 fix(settings): 兼容 PP 目录接口的字符串总数
build / build (push) Successful in 1m46s
真实 PP 登录正常,目录接口将 Java Long 总数序列化为十进制字符串;新客户端的纯整数判断误报不兼容。
接受规范的非负十进制字符串,并保留对负数、布尔、小数和非规范字符串的拒绝。

验证:新增用例先复现 invalid_response,再完成 9 项 PP 回归;make verify 退出 0。
候选代码在测试站 API 和 worker 容器中均真实登录并读取目录成功,确认消费同一配置。
2026-11-17 20:01:44 +08:00
姚章浩 d2f9060b33 fix(settings): 禁止 PP 隐式回落旧凭据配置
build / build (push) Successful in 1m58s
生效状态文件缺失时,扫描与取件仅在显式配置迁移路径时读取旧凭据;默认未配置状态与 API 一致。
新增缺失状态文件的回归,先复现旧凭据被隐式启用,再验证修复。

验证:PP 客户端 7 项隔离服务/配置回归通过;完整 make verify 退出 0,全部本地必需门禁通过。
2026-11-17 19:32:17 +08:00
姚章浩 3825859da6 feat(settings): 打通 PP 认证配置并统一 DWS 来源授权
build / build (push) Successful in 2m20s
PP 增加受控凭据更新、真实登录与目录检查,待验证修改不会替换生效配置;扫描与取件共享生效修订。
钉钉来源向导、校验与登记共用 DWS 授权,支持中途恢复与换账号保护,取消旧扫码自动回落。
认证审计复用已有来源审计页,配置响应不回传密码;补齐运行镜像脚本与权限。

验证:make verify 退出 0;PP/DWS/httpapi 目标 race 回归通过;隔离 HTTP 服务验证 PP 协议及扫描/取件消费者。
尚未部署;真实 PP 凭据与目录可用性在测试站部署后验证。
2026-11-17 19:08:46 +08:00
姚章浩 e0fa31c9d6 fix(settings): 目录鉴权失败提示登录授权入口
build / build (push) Successful in 1m51s
目录检查不可用时给出 DWS 登录与权限检查说明,避免直接展示机器错误码。
新增真实 Response 回归已先失败复现,修复后完整 make verify 通过。
2026-11-17 17:17:42 +08:00
姚章浩 b2cea6ed84 feat(settings): 新增 DWS 设备登录与目录连接检查
build / build (push) Successful in 1m52s
数据源配置可发起钉钉官方设备授权、重新授权和取消,完成后刷新共享账号状态并独立检查目录权限。
授权会话限定发起的管理员登录会话,令牌由现有 CLI 存储,取消等待子进程退出。

验证:完整 make verify 通过;前端 582 项、Go 1545 项通过,487 项声明的可选集成未运行。
授权处理器与 HTTP 边界通过 race 检查;本人钉钉授权仍由管理员在官方页面完成。
2026-11-17 16:59:47 +08:00
78 changed files with 5176 additions and 1038 deletions
+18
View File
@@ -1,5 +1,18 @@
# Changelog
## 开发快照 0.1.399(系统设置分区调整:AI 模型连接 / 知识库映射)
### Changed
- 原「系统设置 → 服务配置」改名为「AI 模型连接」,只保留问答四字段分析与中文翻译共用的
模型连接(地址、模型、密钥操作、测试连接、保存),不再混放 WeKnora 连接。
- WeKnora 测试/生产地址、生产租户与知识库 ID 迁到「知识库映射」,与测试入库、封存源、
生产目标同屏;两个 ID 收进「生产核验参数」,并注明只用于核验、不决定发布写入目标。
- 连接配置在独立组件内维护自己的草稿:保存只提交连接的四项,刷新参数取服务器当前值;
保存刷新策略也不会带上未保存的连接地址。
- 凭据仍是服务器文件引用,连接卡只报状态,页面不提供 API Key 录入。
- 同步更新 README、运维手册、验收清单中的入口路径。
## 开发快照 0.1.384(系统设置审查修复:能力心跳、封存源对照、分区合并)
### Added
@@ -2386,6 +2399,11 @@
### Added
- 数据源配置新增 PP API 地址、账号与密码更新、真实登录及目录连接检查;配置校验通过才生效,临时故障保留待验证修改,扫描与取件共用生效配置。
- 钉钉添加来源统一使用 DWS 官方授权,增加真实空间校验、中途失效恢复与账号变化保护,取消登记自动回落旧扫码侧车;认证操作接入既有审计。
- 数据源配置新增 DWS 登录与重新授权入口,使用钉钉官方设备授权;成功后刷新账号并独立检查目录访问权限。支持取消、过期和重试,授权仅限发起的管理员会话。[操作说明](docs/DWS_LOGIN.md)
- 新增解析监控统一工作台:顶部运行指标与依赖卡片、下方单一文件处理表;来源文件与目标测试库归并最新版本,动作、阶段、状态分列,后端统一筛选、计数及分页。支持单条/跨页勾选重试和版本、失败、重试详情;测试库移除重试独立核验治理规则,不误走上传流程。取消六分页和手动开始入口,生产授权仍在发布管理;本次不改变后台自动调度策略,自动处理闭环需另行端到端验收。
- 问答记录新增“本周”快捷查询,默认展示北京时间本周数据;所选范围自动读取全部记录,校验完整数量后展示,不再需要分页或加载更多。固定展示完整字段,空列不再隐藏,中文原文直接展示、外语未翻译明确标记;保留源端有界传输、取消和失败保护。
+2 -1
View File
@@ -51,6 +51,7 @@ RUN set -eux; \
WORKDIR /app
COPY --from=build /out/ /app/
COPY scripts/source_catalog_import.py scripts/live_catalog_refresh.py scripts/dws_dingtalk_scan.py \
scripts/artifact_fetch.py scripts/axls_to_xlsx.py scripts/dws_error_map.py /app/scripts/
scripts/pp_client.py scripts/artifact_fetch.py scripts/axls_to_xlsx.py scripts/dws_error_map.py /app/scripts/
RUN chmod 0644 /app/scripts/*.py
USER 65532:65532
CMD ["/app/api"]
+3 -3
View File
@@ -52,9 +52,9 @@ Knowledge Adapter / DobotAI 网关
| 来源 | 当前接入方式 | 参考文档 |
| --- | --- | --- |
| 钉钉 | 知识库目录发现与取件;优先 dws,按错误类型决定是否回落兼容路径 | [钉钉取件](docs/DINGTALK_DWS_FETCHER.md) |
| 钉钉 | 知识库目录发现与取件;优先 dws,按错误类型决定是否回落兼容路径 | [登录授权](docs/DWS_LOGIN.md) · [钉钉取件](docs/DINGTALK_DWS_FETCHER.md) |
| AList | 来源目录与资料获取,依赖指定连接和凭据 | [用户操作流程](docs/OPERATIONS_RUNBOOK.md) |
| PP(资料门户) | 本地清单、历史账本接管与只读扫描 | [历史接管](docs/P2_PP_LEDGER_TAKEOVER_ACCEPTANCE.md)、[来源扫描](docs/P42_SOURCE_SCAN_ACCEPTANCE.md) |
| PP(资料门户) | 页面管理 API 与认证配置;真实校验、只读扫描及取件 | [认证配置](docs/SOURCE_AUTH_CONFIGURATION.md) · [历史接管](docs/P2_PP_LEDGER_TAKEOVER_ACCEPTANCE.md)、[来源扫描](docs/P42_SOURCE_SCAN_ACCEPTANCE.md) |
IT、行政等是待扩展的业务资料范围,不是已经完成的新连接器。接入敏感资料前,需要先确认 Owner、有效版本、可见范围与知识库路由。
@@ -127,7 +127,7 @@ make list # 查看检查清单与适用范围
## 问答运营模型配置
管理员在「系统设置 → 服务配置 → 问答评估模型」填写 OpenAI 兼容基础地址、模型和 API Key。测试连接发送最小非业务请求;保存不会启动历史全量任务,自动分析/翻译默认关闭。密钥不回显,保留、更换和清除分别选择;录入只允许有效 HTTPS 或本机回环连接,不允许跳过证书校验。
管理员在「系统设置 → AI 模型连接」填写 OpenAI 兼容基础地址、模型和 API Key。测试连接发送最小非业务请求;保存不会启动历史全量任务,自动分析/翻译默认关闭。密钥不回显,保留、更换和清除分别选择;录入只允许有效 HTTPS 或本机回环连接,不允许跳过证书校验。
API 和普通 worker 须只读挂载同一个独立的 **32 字节、0600 普通文件**,以 `QA_AI_MASTER_KEY_FILE` 指向容器内路径。文件属主应为实际运行 UID;不要复用 Agent 会话存储或把主密钥写进 Compose/Git。治理库只保存加密后的服务密钥;恢复时必须同时恢复治理库和这份独立主密钥。未配置主密钥时如实显示安全存储未就绪。
+339
View File
@@ -2682,6 +2682,211 @@
}
}
},
"/api/v1/pp-connection": {
"get": {
"operationId": "getPPConnection",
"summary": "管理员读取 PP 配置元数据及最近检查结果",
"responses": {
"200": {
"description": "请求完成",
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"data": {
"$ref": "#/components/schemas/PPConnectionView"
}
}
}
}
}
},
"400": {
"description": "输入格式错误"
},
"401": {
"description": "需要登录或重新授权"
},
"403": {
"description": "需要管理员权限或缺少目录权限"
},
"409": {
"description": "配置或账号已变化"
},
"422": {
"description": "明确拒绝本次配置,原配置保留"
},
"500": {
"description": "内部错误"
},
"503": {
"description": "依赖服务暂不可用"
}
}
},
"put": {
"operationId": "savePPConnection",
"summary": "管理员保存并真实校验 PP 配置",
"responses": {
"200": {
"description": "请求完成",
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"data": {
"$ref": "#/components/schemas/PPConnectionView"
}
}
}
}
}
},
"400": {
"description": "输入格式错误"
},
"401": {
"description": "需要登录或重新授权"
},
"403": {
"description": "需要管理员权限或缺少目录权限"
},
"409": {
"description": "配置或账号已变化"
},
"422": {
"description": "明确拒绝本次配置,原配置保留"
},
"500": {
"description": "内部错误"
},
"503": {
"description": "依赖服务暂不可用"
}
},
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/PPConnectionDraft"
}
}
}
}
}
},
"/api/v1/pp-connection/test": {
"post": {
"operationId": "testPPConnection",
"summary": "管理员使用草稿真实登录并读取最小目录,不登记来源",
"responses": {
"200": {
"description": "请求完成",
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"data": {
"$ref": "#/components/schemas/PPObservation"
}
}
}
}
}
},
"400": {
"description": "输入格式错误"
},
"401": {
"description": "需要登录或重新授权"
},
"403": {
"description": "需要管理员权限或缺少目录权限"
},
"409": {
"description": "配置或账号已变化"
},
"422": {
"description": "明确拒绝本次配置,原配置保留"
},
"500": {
"description": "内部错误"
},
"503": {
"description": "依赖服务暂不可用"
}
},
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/PPConnectionDraft"
}
}
}
}
}
},
"/api/v1/dingtalk-spaces/validate": {
"post": {
"operationId": "validateDingtalkSourceSpaces",
"summary": "管理员用共享 DWS 身份真实检查所选空间可见性",
"responses": {
"200": {
"description": "请求完成"
},
"400": {
"description": "输入格式错误"
},
"401": {
"description": "需要登录或重新授权"
},
"403": {
"description": "需要管理员权限或缺少目录权限"
},
"409": {
"description": "配置或账号已变化"
},
"422": {
"description": "明确拒绝本次配置,原配置保留"
},
"500": {
"description": "内部错误"
},
"503": {
"description": "依赖服务暂不可用"
}
},
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": {
"type": "object",
"required": [
"urls"
],
"properties": {
"urls": {
"type": "array",
"minItems": 1,
"maxItems": 100,
"items": {
"type": "string"
}
}
}
}
}
}
}
}
},
"/api/v1/dingtalk-spaces": {
"post": {
"tags": [
@@ -5879,6 +6084,140 @@
}
},
"schemas": {
"PPConnectionDraft": {
"type": "object",
"required": [
"base_url",
"username",
"revision"
],
"additionalProperties": false,
"properties": {
"base_url": {
"type": "string",
"format": "uri",
"description": "HTTPS API 根地址"
},
"username": {
"type": "string",
"maxLength": 255
},
"password": {
"type": "string",
"writeOnly": true,
"maxLength": 8192,
"description": "省略或空值保留已存密码"
},
"revision": {
"type": "integer",
"minimum": 0
},
"pending_id": {
"type": "string",
"description": "继续校验已保存的待验证修改"
}
}
},
"PPSafeMetadata": {
"type": "object",
"required": [
"id",
"base_url",
"username",
"password_configured"
],
"properties": {
"id": {
"type": "string"
},
"base_url": {
"type": "string",
"format": "uri"
},
"username": {
"type": "string"
},
"password_configured": {
"type": "boolean"
}
}
},
"PPObservation": {
"type": "object",
"required": [
"state",
"message",
"checked_at"
],
"properties": {
"state": {
"type": "string",
"enum": [
"valid",
"auth_failed",
"permission_denied",
"incomplete",
"unavailable",
"invalid_response"
]
},
"message": {
"type": "string"
},
"checked_at": {
"type": "string",
"format": "date-time"
}
}
},
"PPConnectionView": {
"type": "object",
"properties": {
"revision": {
"type": "integer"
},
"active": {
"anyOf": [
{
"$ref": "#/components/schemas/PPSafeMetadata"
},
{
"type": "null"
}
]
},
"pending": {
"anyOf": [
{
"$ref": "#/components/schemas/PPSafeMetadata"
},
{
"type": "null"
}
]
},
"observation": {
"anyOf": [
{
"$ref": "#/components/schemas/PPObservation"
},
{
"type": "null"
}
]
},
"pending_observation": {
"anyOf": [
{
"$ref": "#/components/schemas/PPObservation"
},
{
"type": "null"
}
]
}
}
},
"QAReportedHistoricalRate": {
"type": "object",
"additionalProperties": false,
+16 -1
View File
@@ -21,6 +21,7 @@ import (
"knowledge-governance/internal/monitoring"
postgresstore "knowledge-governance/internal/platform/postgres"
"knowledge-governance/internal/platform/weknora"
"knowledge-governance/internal/ppauth"
"knowledge-governance/internal/qaopsai"
"knowledge-governance/internal/qaopsorg"
"knowledge-governance/internal/qaopsquality"
@@ -93,7 +94,7 @@ func run(ctx context.Context, getenv func(string) string) error {
// Production WeKnora is probed for liveness only: the console must show the
// real state of that endpoint, while credentials and the publish target stay
// in the release runtime config (RELEASE_CONFIG_FILE).
// 生产 WeKnora 端点:配置入口在网页「系统设置 → 服务配置」,管理员保存的值是
// 生产 WeKnora 端点:配置入口在网页「系统设置 → 知识库映射」,管理员保存的值是
// 这里的真源;容器 env 只作为未在网页配置时的兜底。凭据与发布目标仍留在
// release 运行时配置(RELEASE_CONFIG_FILE)里,不经网页也不落明文。
actionStore := postgresstore.NewStore(pool)
@@ -193,7 +194,20 @@ func run(ctx context.Context, getenv func(string) string) error {
return fmt.Errorf("快照搬运接线无效: %w", handoffErr)
}
log.Printf("library target evidence identity=%s", config.LibraryTargetIdentity())
ppStateFile := getenv("PP_AUTH_STATE_FILE")
if ppStateFile == "" {
ppStateFile = "/data/source-auth/pp/settings.json"
}
ppPython := getenv("PP_AUTH_PYTHON")
if ppPython == "" {
ppPython = "/usr/bin/python3"
}
ppScript := getenv("PP_AUTH_CHECK_SCRIPT")
if ppScript == "" {
ppScript = "/app/scripts/pp_client.py"
}
handler, err := httpapi.NewHandler(httpapi.Config{
PPConnection: &ppauth.Manager{Path: ppStateFile, LegacyPath: getenv("PP_LEGACY_CREDENTIALS_FILE"), Probe: ppauth.CommandProbe(ppPython, ppScript)},
LibraryHandoffEnabled: handoffConfigured,
QAAI: qaopsai.NewFromEnvironment(pool, getenv, config.DatabaseURL),
QAWeeklyMail: qawemail.NewStore(pool, qaSettings.TenantKey, getenv("QA_AI_MASTER_KEY_FILE"), nil),
@@ -223,6 +237,7 @@ func run(ctx context.Context, getenv func(string) string) error {
ProductionReleaseAuthorizationToken: releaseAuthorizationToken,
// 数据源发现(系统设置 → 数据源配置):用与 worker 同一份 dws 状态做只读列举。
DingtalkSources: dingtalkSourceLister{},
DWSLogin: dingtalkdiscovery.NewDeviceLoginManager(ctx, dingtalkdiscovery.ExecRunner{}),
DingtalkDocuments: dingtalkSourceLister{},
BackupSchedule: postgresstore.NewStore(pool),
VersionHistory: versionHistoryStore,
+1 -1
View File
@@ -12,7 +12,7 @@ import (
)
// newWeKnoraReadinessResolver 把「探测目标」收敛到与业务同一组真源:
// 端点来自网页保存的服务配置(endpoint 内部已做 env 兜底),知识库来自
// 端点来自网页「知识库映射」保存的连接配置(endpoint 内部已做 env 兜底),知识库来自
// 当前持久化绑定(未绑定才用 env 引导值),凭据每次重新读同一份 key 文件。
// 任何一项读取失败都必须显式报错——静默退回旧值正是 F13 的成因。
func newWeKnoraReadinessResolver(
+44
View File
@@ -0,0 +1,44 @@
# DWS 登录授权
管理员入口:系统设置 → 数据源配置 → 钉钉账号(DWS)。
- 未登录时点击“登录授权”,在弹窗中打开钉钉官方授权页,由本人选择账号与组织并确认。
- 平台等待设备授权完成。成功后自动刷新账号、组织和续期凭证有效期,并检查知识库、钉盘目录访问。
- “重新授权”采用同一流程;授权中可取消,失败或过期后可重新发起。
- “检查连接”只列目录与读取登录元数据,不登记来源、不扫描已有来源、不发起入库或生产发布。
- 登录成功与目录可用分开显示;部分范围无权限时保留其他已成功范围,并展示原因。
## 服务端接线
API 直接管理 `dws auth login --device --no-browser --format json` 子进程,不复用旧浏览器扫码侧车。
CLI 负责官方 OAuth 协议、组织 CLI 权限校验及凭据加密持久化。平台只解析官方设备授权链接、用户授权码、有效期与状态;原始输出、access token、refresh token不进入接口或日志。
API 与 worker 必须使用相同 UID,以及同一组可写挂载:
| 配置 | 用途 |
|---|---|
| `DWS_BIN` | 固定版本的 DWS 程序 |
| `DWS_CONFIG_DIR` | 共享的 DWS 状态目录 |
| `DWS_HOME` | 共享 home,包含 `.local/share/dws-cli` 文件型密钥存储 |
| `DWS_DISABLE_KEYCHAIN=1` | 容器使用文件型凭据存储 |
子进程 HOME 由 DWS_HOME 指定,只透传所需环境。禁止把认证状态复制成多份,避免轮换令牌互相失效。
## 接口与生命周期
- `POST /api/v1/dws-login`:立即返回 starting,后台生成设备授权;同一浏览器管理员会话重复请求复用在途授权。
- `GET /api/v1/dws-login/{id}`:读取本人发起的授权。
- `DELETE /api/v1/dws-login/{id}`:取消并等待旧 CLI 退出,清除授权链接与用户码。
- 上述接口均为管理员专用;授权绑定管理员及其登录会话,另一会话无法读取或取消。
- 创建和取消要求同源操作意图;响应禁止缓存。仅允许 `https://login.dingtalk.com/oauth2/device/verify.htm` 官方链接。
- 每次只运行一个授权,防止覆盖共享状态。服务重启会结束在途授权;页面提示重新发起。
- CLI 成功退出后必须再次读到有效本地凭据才显示登录成功;真实目录访问另行检查。
## 添加来源
添加来源向导与账号卡共用官方 DWS 授权;列举、校验、登记使用同一身份。来源登记不再自动回落旧扫码侧车。保存中授权失效时保留已成功项并恢复剩余项;换账号或组织后需重新确认范围。详细交互、认证审计与 PP 接线见 [数据源认证配置](SOURCE_AUTH_CONFIGURATION.md)。
## 验证边界
自动回归覆盖授权生成、成功后状态更新、目录访问失败与部分可用、取消与进程退出、过期、重试、跨管理员/会话隔离、同源限制、官方链接校验及元数据脱敏。
真实本人授权需要管理员在钉钉官方页面确认;自动测试与生成真实授权链接不替代本人授权,也不证明其公司目录权限。
+2 -2
View File
@@ -10,7 +10,7 @@
## 0. 开工前:环境与凭据自检
| 项 | 怎么做 | 成功标志 |
|---|---|---|
| 测试/生产 WeKnora | 系统设置 → 服务配置 | 生产端"已启用 · 可提交生产请求";测试端探测正常 |
| 测试/生产 WeKnora | 系统设置 → 知识库映射 → WeKnora 连接 | 生产端"已启用 · 可提交生产请求";测试端探测正常 |
| 钉钉取件凭据 **【新】** | 容器内 `HOME=/data/home dws auth status --readonly -f json` | `authenticated:true`、`refresh_token_valid:true`(**必须带 `HOME=/data/home`,否则误报未登录**) |
| 备份主机接线 | 系统设置 → 存储与备份 | "目标主机 weknora-test · 已连接" |
@@ -80,7 +80,7 @@ bash scripts/target_kb_snapshot.sh # 默认云内执行;PG 8 表 +
| 分区 | 谁 | 现在要管什么 |
|---|---|---|
| 服务配置 | 管理员 | 测试地址 / 生产地址 / 生产 ID / 知识库 ID(保存时真探活,无效才拒) |
| 知识库映射 → WeKnora 连接 | 管理员 | 测试地址 / 生产地址 / 生产 ID / 知识库 ID(保存时真探活,无效才拒) |
| 测试知识库绑定 | 管理员 | 标注往哪个测试库路由(对内 ≠ 对外) |
| 生产知识库绑定 | 管理员 | 源库 → 目标库映射(发布写哪个生产库) |
| **存储与备份** **【新】** | 管理员 | **定时时间 + 储存天数**(+每月归档、启用开关)→ `保存并下发到主机`;另有 `重新下发当前设置`、`立即备份一次`;页面显示主机上**实际生效**的计划、cron 条目、最近一次结果与"已一致/待下发" |
+57
View File
@@ -0,0 +1,57 @@
# 数据源认证配置
管理员入口:系统设置 → 数据源配置。PP 连接位于钉钉账号卡旁;添加来源向导使用同一组账号与配置。
## PP 连接
- API 地址填写 HTTPS 根地址;后台沿用 `/admin/login/token`、`/potal/file/back/page` 接口路径。`potal` 是上游真实路径,不能改写。
- 账号允许修改。密码只显示已配置/未配置;点击“更新密码”录入新值。省略或留空保留既有密码,不回显原值。
- “测试连接”使用当前表单,真实登录并读取一条目录记录;合法空目录也可通过。它不保存配置、不登记来源、不启动扫描。
- “保存并校验”在后端重新检查本次提交。通过后切换生效;明确的认证拒绝、权限拒绝、接口不兼容阻止生效。
- 超时、限流或服务临时故障可保存为待验证修改;缺少凭据可保存未完成配置。两者都不会替换当前生效配置;需要再次保存并校验,通过后才切换。
- 草稿测试结果与当前生效配置状态分开;每次修改字段会使原草稿测试结果作废。检查失败时保留上次观测及时间,不能把网络失败当成退出登录。
- 后续目录扫描和远程取件读取同一生效配置;验证过的本地文件仍按既有内容校验逻辑复用。配置保存不恢复暂停的计划、不触发入库或发布。
## PP 服务端接线
| 参数 | 默认与用途 |
|---|---|
| `PP_AUTH_STATE_FILE` | `/data/source-auth/pp/settings.json`;API 和 worker 必须读同一份原子配置文件 |
| `PP_AUTH_PYTHON` | `/usr/bin/python3`;API 连接检查使用的解释器 |
| `PP_AUTH_CHECK_SCRIPT` | `/app/scripts/pp_client.py`;与扫描、取件共享的只读协议实现 |
| `PP_LEGACY_CREDENTIALS_FILE` | 可选,只用于首次迁移;有效状态文件存在后不再回落旧文件 |
配置目录及状态文件分别使用 0700 和 0600。API/worker 使用相同 UID,将宿主凭据目录挂载为共享目录,不能绑定单个文件:单文件 bind mount 会在原子 rename 后继续指向旧 inode。迁移时从目标机既有凭据文件直接读取,禁止通过聊天、命令行参数或仓库传递密码。
状态文件持有生效配置、待验证修改、最近检查及认证审计,包含密码,因此必须按密钥文件保护,不能进普通配置备份、版本记录或日志。更新使用跨进程文件锁、临时文件、fsync 和原子 rename。配置修订采用乐观并发控制;旧请求不能覆盖新修订。worker 的迟到检查只能更新自身生效配置 ID 对应的观测。
PP 检查子进程只从 stdin 接收本次凭据,stdout 只返回白名单检查结论。GET 接口仅返回配置元数据和是否存在密码;密码输入字段只可写。
| 接口 | 用途 |
|---|---|
| `GET /api/v1/pp-connection` | 管理员读取安全元数据、待验证修改与检查状态 |
| `POST /api/v1/pp-connection/test` | 测试当前草稿,不激活 |
| `PUT /api/v1/pp-connection` | 校验并保存;携带当前 `revision` |
使用已保存的待验证密码继续校验时携带 `pending_id`,避免空密码误用旧生效密码。修改请求验证管理员、同源操作意图和输入大小;响应禁止缓存。
## 钉钉添加向导
保留 类型 → 授权 → 范围 → 校验 → 保存 五步;授权步骤复用钉钉账号卡的官方 DWS 设备授权流程。
- 可见范围、校验和登记都使用共享 DWS 身份;登记不再自动回落旧扫码侧车。DWS 无法定位的历史空间返回明确原因。
- `POST /api/v1/dingtalk-spaces/validate` 真实读取并检查所选空间。失败、权限不足、部分范围不完整与合法空列表分别展示。
- 已接入空间按稳定空间身份标记,历史连接也从其存储的空间标识识别,不靠名称去重。
- 保存中授权失效:保留已成功项及待处理项,在向导内重新授权;同账号恢复后继续剩余项,复用操作标识,首次扫描按连接身份去重。
- 取消或失败后保留选择并停止自动提交;再次恢复失败不循环弹授权。
- 授权后换账号/组织:重新加载范围并清除旧选择,要求重新确认。客户端提交身份指纹,后端在列举前后确认身份,变化时拒绝登记。
- 只有已点击保存的操作才会在重新授权后继续登记;账号登录、刷新和连接检查本身不创建来源或扫描任务。
- 登记成功和首次扫描入队分别反馈,不能用登记成功冒充扫描已运行。
其他历史入口若仍调用旧扫码侧车,保留其原行为;本功能不删除整个侧车服务。
## 审计与验证
PP 配置/密码更新、连接检查及 DWS 授权结果,复用现有数据源操作审计页面。凭据事件与配置在同一文件内原子提交,并与数据库原有事件按时间合并分页;不把密码写入 SQL。DWS 终态审计按授权 ID 与状态去重,轮询不刷记录。
回归覆盖真实协议请求、空目录、认证拒绝、权限与限流、密码保留/替换、待验证密码重校验、并发修订冲突、响应脱敏、同源及角色限制、无意外任务、审计分页、DWS 部分成功恢复与换账号保护。协议与消费者测试使用隔离的本机 HTTP 服务;运行环境可用性需另行通过真实页面检查,不能用这些用例替代。
+2 -2
View File
@@ -255,8 +255,8 @@
## D. 系统设置
### D1 服务配置(测试 / 生产端点与 ID)
- 入口:**系统设置 → 服务配置**(管理员)
### D1 知识库映射 → WeKnora 连接(测试 / 生产端点与 ID)
- 入口:**系统设置 → 知识库映射 → WeKnora 连接**(管理员)
- 规则:只配四项(测试地址、生产地址、生产 ID、知识库 ID);**保存时真探测**,无效才拒;凭据只写不读
- 状态:✅(见 `P41/P42` 与 `ui_settings_*` 测试)
+21 -5
View File
@@ -2,16 +2,22 @@ package dingtalkdiscovery
import (
"context"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"errors"
)
// CredentialStatus contains metadata only, never credential values.
type CredentialStatus struct {
Identity string `json:"identity,omitempty"`
Authenticated bool `json:"authenticated"`
RefreshTokenValid bool `json:"refresh_token_valid"`
ExpiresAt string `json:"expires_at,omitempty"`
RefreshExpiresAt string `json:"refresh_expires_at,omitempty"`
CorpName string `json:"corp_name,omitempty"`
UserName string `json:"user_name,omitempty"`
StatusKnown bool `json:"status_known"`
}
func ReadCredentialStatus(ctx context.Context, runner Runner) (CredentialStatus, error) {
@@ -20,13 +26,23 @@ func ReadCredentialStatus(ctx context.Context, runner Runner) (CredentialStatus,
return CredentialStatus{}, errors.New("could not read DWS credential status")
}
var decoded struct {
Authenticated *bool `json:"authenticated"`
RefreshTokenValid bool `json:"refresh_token_valid"`
ExpiresAt string `json:"expires_at"`
RefreshExpiresAt string `json:"refresh_expires_at"`
CorpID json.RawMessage `json:"corp_id"`
UserID json.RawMessage `json:"user_id"`
Authenticated *bool `json:"authenticated"`
RefreshTokenValid bool `json:"refresh_token_valid"`
ExpiresAt string `json:"expires_at"`
RefreshExpiresAt string `json:"refresh_expires_at"`
CorpName string `json:"corp_name"`
UserName string `json:"user_name"`
Reason string `json:"reason"`
}
if json.Unmarshal(raw, &decoded) != nil || decoded.Authenticated == nil {
return CredentialStatus{}, errors.New("invalid DWS credential status")
}
return CredentialStatus{*decoded.Authenticated, decoded.RefreshTokenValid, decoded.ExpiresAt, decoded.RefreshExpiresAt}, nil
identity := ""
if len(decoded.CorpID) > 0 && len(decoded.UserID) > 0 && string(decoded.CorpID) != "null" && string(decoded.UserID) != "null" {
sum := sha256.Sum256(append(append(decoded.CorpID, 0), decoded.UserID...))
identity = hex.EncodeToString(sum[:])
}
return CredentialStatus{Identity: identity, Authenticated: *decoded.Authenticated, RefreshTokenValid: decoded.RefreshTokenValid, ExpiresAt: decoded.ExpiresAt, RefreshExpiresAt: decoded.RefreshExpiresAt, CorpName: decoded.CorpName, UserName: decoded.UserName, StatusKnown: decoded.Reason == ""}, nil
}
+243
View File
@@ -0,0 +1,243 @@
package dingtalkdiscovery
import (
"bufio"
"context"
"crypto/rand"
"encoding/hex"
"errors"
"io"
"net/url"
"regexp"
"strconv"
"strings"
"sync"
"time"
)
var ErrLoginBusy = errors.New("another DWS authorization is active")
var ErrLoginNotFound = errors.New("DWS authorization not found")
// DeviceLoginView contains only the user-facing challenge. Tokens remain in the
// CLI's shared credential store; command output is never logged or returned.
type DeviceLoginView struct {
ID string `json:"id"`
State string `json:"state"`
VerificationURL string `json:"verification_url,omitempty"`
UserCode string `json:"user_code,omitempty"`
ExpiresAt time.Time `json:"expires_at"`
Error string `json:"error,omitempty"`
}
type deviceLogin struct {
view DeviceLoginView
owner string
cancel context.CancelFunc
done chan struct{}
}
type DeviceLoginManager struct {
ctx context.Context
runner ExecRunner
mu sync.Mutex
sessions map[string]*deviceLogin
}
func NewDeviceLoginManager(ctx context.Context, runner ExecRunner) *DeviceLoginManager {
return &DeviceLoginManager{ctx: ctx, runner: runner, sessions: make(map[string]*deviceLogin)}
}
func loginActive(state string) bool {
return state == "starting" || state == "waiting" || state == "verifying"
}
func (m *DeviceLoginManager) Start(owner string) (DeviceLoginView, error) {
if owner == "" {
return DeviceLoginView{}, ErrLoginNotFound
}
m.mu.Lock()
defer m.mu.Unlock()
for id, s := range m.sessions {
if s.view.State == "cancelled" || s.view.State == "expired" {
select {
case <-s.done:
default:
return DeviceLoginView{}, ErrLoginBusy
}
}
if loginActive(s.view.State) {
if s.owner == owner {
return s.view, nil
}
return DeviceLoginView{}, ErrLoginBusy
}
if time.Since(s.view.ExpiresAt) > 30*time.Minute || len(m.sessions) >= 16 {
delete(m.sessions, id)
}
}
var b [16]byte
if _, err := rand.Read(b[:]); err != nil {
return DeviceLoginView{}, err
}
ctx, cancel := context.WithTimeout(m.ctx, 20*time.Minute)
s := &deviceLogin{view: DeviceLoginView{ID: hex.EncodeToString(b[:]), State: "starting", ExpiresAt: time.Now().UTC().Add(20 * time.Minute)}, owner: owner, cancel: cancel, done: make(chan struct{})}
m.sessions[s.view.ID] = s
go m.run(ctx, s)
return s.view, nil
}
func (m *DeviceLoginManager) Get(owner, id string) (DeviceLoginView, error) {
m.mu.Lock()
defer m.mu.Unlock()
s := m.sessions[id]
if s == nil || s.owner != owner {
return DeviceLoginView{}, ErrLoginNotFound
}
if loginActive(s.view.State) && !time.Now().Before(s.view.ExpiresAt) {
s.cancel()
finishLogin(s, "expired", "授权已过期,请重新发起。")
}
return s.view, nil
}
func (m *DeviceLoginManager) Cancel(owner, id string) (DeviceLoginView, error) {
m.mu.Lock()
s := m.sessions[id]
if s == nil || s.owner != owner {
m.mu.Unlock()
return DeviceLoginView{}, ErrLoginNotFound
}
if loginActive(s.view.State) {
s.cancel()
finishLogin(s, "cancelled", "")
}
m.mu.Unlock()
// Do not acknowledge cancellation while the old CLI could still write tokens.
timer := time.NewTimer(7 * time.Second)
defer timer.Stop()
select {
case <-s.done:
case <-timer.C:
return DeviceLoginView{}, ErrLoginBusy
}
m.mu.Lock()
defer m.mu.Unlock()
return s.view, nil
}
func finishLogin(s *deviceLogin, state, message string) {
s.view.State = state
s.view.Error = message
s.view.VerificationURL = ""
s.view.UserCode = ""
}
var ansiEscape = regexp.MustCompile(`\x1b\[[0-9;]*[A-Za-z]`)
var challengeCode = regexp.MustCompile(`(?:授权码|User code|User Code)\s*[::]\s*([A-Za-z0-9-]{4,32})`)
var challengeExpiry = regexp.MustCompile(`(?:将在\s*|expires in\s*)([0-9]+)\s*(?:秒|seconds)`)
func safeDeviceURL(raw string) bool {
u, err := url.Parse(raw)
return err == nil && u.Scheme == "https" && u.Host == "login.dingtalk.com" && u.User == nil && u.Fragment == "" && u.Path == "/oauth2/device/verify.htm"
}
func (m *DeviceLoginManager) observe(s *deviceLogin, line string) {
line = strings.TrimSpace(ansiEscape.ReplaceAllString(line, ""))
m.mu.Lock()
defer m.mu.Unlock()
if !loginActive(s.view.State) {
return
}
if match := challengeCode.FindStringSubmatch(line); len(match) == 2 {
s.view.UserCode = match[1]
}
if match := challengeExpiry.FindStringSubmatch(line); len(match) == 2 {
seconds, _ := strconv.Atoi(match[1])
if seconds > 0 && seconds <= 1800 {
s.view.ExpiresAt = time.Now().UTC().Add(time.Duration(seconds) * time.Second)
}
}
if safeDeviceURL(line) {
u, _ := url.Parse(line)
if s.view.VerificationURL == "" || u.Query().Get("user_code") != "" {
s.view.VerificationURL = line
}
}
if s.view.State == "starting" && s.view.UserCode != "" && s.view.VerificationURL != "" {
s.view.State = "waiting"
}
if strings.Contains(line, "Step 3:") {
s.view.State = "verifying"
}
}
func (m *DeviceLoginManager) run(ctx context.Context, s *deviceLogin) {
defer close(s.done)
defer s.cancel()
command, err := m.runner.command(ctx, "auth", "login", "--device", "--no-browser", "--format", "json")
if err != nil {
m.failed(s, "DWS 登录程序不可用,请联系管理员检查服务配置。")
return
}
command.Env = append(command.Env, "DWS_LANG=zh", "NO_COLOR=1", "TERM=dumb")
command.Stdout = io.Discard
stderr, err := command.StderrPipe()
if err != nil {
m.failed(s, "无法启动 DWS 授权,请重试。")
return
}
if err = command.Start(); err != nil {
m.failed(s, "无法启动 DWS 授权,请检查凭据目录权限。")
return
}
scanner := bufio.NewScanner(stderr)
scanner.Buffer(make([]byte, 4096), 64*1024)
for scanner.Scan() {
m.observe(s, scanner.Text())
}
if scanner.Err() != nil {
s.cancel()
}
err = command.Wait()
m.mu.Lock()
active := loginActive(s.view.State)
m.mu.Unlock()
if !active {
return
}
if err != nil || ctx.Err() != nil {
m.mu.Lock()
defer m.mu.Unlock()
if !loginActive(s.view.State) {
return
}
if errors.Is(ctx.Err(), context.DeadlineExceeded) || !time.Now().Before(s.view.ExpiresAt) {
finishLogin(s, "expired", "授权已过期,请重新发起。")
} else {
finishLogin(s, "failed", "DWS 授权未完成,请重试;若组织未开通 CLI 数据权限,请联系钉钉管理员。")
}
return
}
// A process exit alone is not proof that usable credentials were persisted.
checkCtx, cancel := context.WithTimeout(m.ctx, 5*time.Second)
defer cancel()
status, statusErr := ReadCredentialStatus(checkCtx, m.runner)
m.mu.Lock()
defer m.mu.Unlock()
if !loginActive(s.view.State) {
return
}
if statusErr != nil || !status.Authenticated || !status.RefreshTokenValid || !status.StatusKnown {
finishLogin(s, "failed", "授权结束,但未读到有效 DWS 凭据,请重新授权。")
} else {
finishLogin(s, "succeeded", "")
}
}
func (m *DeviceLoginManager) failed(s *deviceLogin, message string) {
m.mu.Lock()
defer m.mu.Unlock()
if loginActive(s.view.State) {
finishLogin(s, "failed", message)
}
}
@@ -0,0 +1,160 @@
package dingtalkdiscovery
import (
"context"
"encoding/json"
"errors"
"os"
"path/filepath"
"strconv"
"strings"
"syscall"
"testing"
"time"
)
func deviceFixture(t *testing.T) (*DeviceLoginManager, string) {
t.Helper()
root := t.TempDir()
binary := filepath.Join(root, "dws")
script := `#!/usr/bin/env python3
import sys,time,pathlib,json,os
root=pathlib.Path(` + strconv.Quote(root) + `)
a=sys.argv[1:]
if a==['auth','login','--device','--no-browser','--format','json']:
(root/'pid').write_text(str(os.getpid()))
print(' 授权码: TEST-CODE',file=sys.stderr,flush=True)
print('授权码将在 900 秒后过期。',file=sys.stderr,flush=True)
print('https://login.dingtalk.com/oauth2/device/verify.htm?user_code=TEST-CODE',file=sys.stderr,flush=True)
print('https://login.dingtalk.com/oauth2/device/verify.htm',file=sys.stderr,flush=True)
print('never-expose-this-marker',file=sys.stderr,flush=True)
while not (root/'finish').exists(): time.sleep(.01)
if (root/'fail').exists(): sys.exit(1)
elif a==['auth','status','--readonly','--format','json']:
print(json.dumps({'authenticated':not (root/'invalid').exists(),'refresh_token_valid':True,'corp_name':'测试组织','user_name':'测试账号','access_token':'never-expose-this-marker'}))
else: sys.exit(9)
`
if err := os.WriteFile(binary, []byte(script), 0700); err != nil {
t.Fatal(err)
}
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)
return NewDeviceLoginManager(ctx, ExecRunner{Binary: binary}), root
}
func awaitDevice(t *testing.T, m *DeviceLoginManager, id, state string) DeviceLoginView {
t.Helper()
deadline := time.Now().Add(3 * time.Second)
for time.Now().Before(deadline) {
v, e := m.Get("owner", id)
if e != nil {
t.Fatal(e)
}
if v.State == state {
return v
}
time.Sleep(10 * time.Millisecond)
}
v, _ := m.Get("owner", id)
t.Fatalf("state=%s want=%s error=%s", v.State, state, v.Error)
return v
}
func TestDeviceLoginUsesOfficialChallengeAndSharedCLI(t *testing.T) {
m, root := deviceFixture(t)
initial, err := m.Start("owner")
if err != nil {
t.Fatal(err)
}
if initial.State != "starting" {
t.Fatal(initial)
}
waiting := awaitDevice(t, m, initial.ID, "waiting")
if waiting.UserCode != "TEST-CODE" || !strings.Contains(waiting.VerificationURL, "user_code=TEST-CODE") {
t.Fatal("missing complete official challenge")
}
if _, err = m.Start("other"); !errors.Is(err, ErrLoginBusy) {
t.Fatal("parallel logins may overwrite shared credentials")
}
duplicate, _ := m.Start("owner")
if duplicate.ID != initial.ID {
t.Fatal("duplicate start was not idempotent")
}
if _, err = m.Get("other", initial.ID); !errors.Is(err, ErrLoginNotFound) {
t.Fatal("challenge leaked to another owner")
}
if _, err = m.Cancel("other", initial.ID); !errors.Is(err, ErrLoginNotFound) {
t.Fatal("foreign cancellation permitted")
}
if err = os.WriteFile(filepath.Join(root, "finish"), nil, 0600); err != nil {
t.Fatal(err)
}
done := awaitDevice(t, m, initial.ID, "succeeded")
raw, _ := json.Marshal(done)
if done.UserCode != "" || done.VerificationURL != "" || strings.Contains(string(raw), "never-expose") {
t.Fatal("completed session retained sensitive output")
}
}
func TestDeviceLoginCancellationClearsChallenge(t *testing.T) {
m, root := deviceFixture(t)
v, _ := m.Start("owner")
awaitDevice(t, m, v.ID, "waiting")
closed, err := m.Cancel("owner", v.ID)
if err != nil || closed.State != "cancelled" || closed.UserCode != "" || closed.VerificationURL != "" {
t.Fatal(closed, err)
}
pidBytes, err := os.ReadFile(filepath.Join(root, "pid"))
if err != nil {
t.Fatal(err)
}
pid, _ := strconv.Atoi(string(pidBytes))
if syscall.Kill(pid, 0) == nil {
t.Fatal("cancel returned while credential-writing process was still running")
}
again, _ := m.Get("owner", v.ID)
if again.State != "cancelled" {
t.Fatal("late process exit overwrote cancellation")
}
}
func TestDeviceLoginRejectsExitWithoutPersistedCredentials(t *testing.T) {
m, root := deviceFixture(t)
os.WriteFile(filepath.Join(root, "invalid"), nil, 0600)
os.WriteFile(filepath.Join(root, "finish"), nil, 0600)
v, _ := m.Start("owner")
done := awaitDevice(t, m, v.ID, "failed")
if !strings.Contains(done.Error, "未读到有效") {
t.Fatal("successful exit was treated as authenticated")
}
}
func TestDeviceLoginFailureIsRedactedAndRetryable(t *testing.T) {
m, root := deviceFixture(t)
os.WriteFile(filepath.Join(root, "fail"), nil, 0600)
os.WriteFile(filepath.Join(root, "finish"), nil, 0600)
v, _ := m.Start("owner")
done := awaitDevice(t, m, v.ID, "failed")
if strings.Contains(done.Error, "never-expose") || done.Error == "" {
t.Fatal("raw CLI output or missing error")
}
retry, e := m.Start("owner")
if e != nil || retry.ID == v.ID {
t.Fatal("cannot retry failed authorization")
}
}
func TestDeviceChallengeRejectsUntrustedLinksAndExpires(t *testing.T) {
m, root := deviceFixture(t)
_ = root
s := &deviceLogin{view: DeviceLoginView{State: "starting", ExpiresAt: time.Now().Add(time.Minute)}, cancel: func() {}}
m.observe(s, "授权码: TEST-CODE")
for _, link := range []string{"http://login.dingtalk.com/oauth2/device/verify.htm", "https://login.dingtalk.com.evil.invalid/oauth2/device/verify.htm", "https://user:pass@login.dingtalk.com/oauth2/device/verify.htm", "javascript:alert(1)", "https://login.dingtalk.com/other"} {
m.observe(s, link)
}
if s.view.State != "starting" || s.view.VerificationURL != "" {
t.Fatal("untrusted link accepted")
}
s.view.ID = "test-expired"
s.owner = "owner"
s.view.ExpiresAt = time.Now().Add(-time.Second)
m.sessions[s.view.ID] = s
got, e := m.Get("owner", s.view.ID)
if e != nil || got.State != "expired" || got.UserCode != "" {
t.Fatal("expired challenge retained")
}
}
+27 -9
View File
@@ -21,6 +21,7 @@ import (
// KnowledgeBase 是一个钉钉知识库(wiki 空间)。URL 是 alidocs 空间链接,直接可用于登记来源。
type KnowledgeBase struct {
Registered bool `json:"registered"`
Name string `json:"name"`
SpaceID string `json:"space_id"`
SpaceType string `json:"space_type"`
@@ -30,6 +31,7 @@ type KnowledgeBase struct {
// DriveSpace 是一个钉盘空间(我的文件 = mySpace;组织空间 = orgSpace)。
type DriveSpace struct {
Registered bool `json:"registered"`
Name string `json:"name"`
SpaceID string `json:"space_id"`
RootFolderID string `json:"root_folder_id,omitempty"`
@@ -38,6 +40,7 @@ type DriveSpace struct {
// Result 是一次发现的结果。
type Result struct {
Identity string `json:"identity,omitempty"`
KnowledgeBases []KnowledgeBase `json:"knowledge_bases"`
DriveSpaces []DriveSpace `json:"drive_spaces"`
// KnowledgeNotice / DriveNotice 说明「这一类为什么不全」:权限不足、未登录、或被上限截断。
@@ -70,21 +73,26 @@ func SpaceListArgs(spaceType string) []string {
// 知识库整体列不出来 → 返回 ErrUnavailable(页面必须提示「列不出来」,不能装成空列表)。
// 单独一类失败、钉盘失败、或结果被上限截断 → 只丢那一部分,把原因写进对应的 Notice。
func Discover(ctx context.Context, runner Runner) (Result, error) {
result := Result{FetchedAt: time.Now().UTC()}
result := Result{FetchedAt: time.Now().UTC(), KnowledgeBases: []KnowledgeBase{}, DriveSpaces: []DriveSpace{}}
successful := 0
causes := []error{}
failures := make([]string, 0, len(knowledgeSpaceTypes))
seenKnowledge := map[string]bool{}
for _, spaceType := range knowledgeSpaceTypes {
raw, err := runner.RunDWS(ctx, SpaceListArgs(spaceType)...)
if err != nil {
failures = append(failures, describe(spaceType, err))
failures = append(failures, describe(spaceType, failureCause(raw, err)))
causes = append(causes, failureCause(raw, err))
continue
}
bases, notice, err := parseKnowledgeBases(raw, spaceType)
if err != nil {
failures = append(failures, describe(spaceType, err))
failures = append(failures, describe(spaceType, failureCause(raw, err)))
causes = append(causes, failureCause(raw, err))
continue
}
successful++
if notice != "" {
result.KnowledgeNotice = notice
}
@@ -96,9 +104,6 @@ func Discover(ctx context.Context, runner Runner) (Result, error) {
result.KnowledgeBases = append(result.KnowledgeBases, base)
}
}
if len(result.KnowledgeBases) == 0 && len(failures) > 0 {
return result, fmt.Errorf("%w: %s", ErrUnavailable, strings.Join(failures, ";"))
}
if len(failures) > 0 {
result.KnowledgeNotice = strings.Join(append([]string{result.KnowledgeNotice}, failures...), ";")
}
@@ -107,14 +112,17 @@ func Discover(ctx context.Context, runner Runner) (Result, error) {
for _, spaceType := range []string{"mySpace", "orgSpace"} {
raw, err := runner.RunDWS(ctx, "wiki", "space", "list", "--type", spaceType)
if err != nil {
result.DriveNotice = err.Error()
result.DriveNotice = "钉盘目录暂时不可用,请检查该范围权限"
causes = append(causes, failureCause(raw, err))
continue
}
spaces, err := parseDriveSpaces(raw, spaceType)
if err != nil {
result.DriveNotice = err.Error()
result.DriveNotice = "钉盘目录暂时不可用,请检查该范围权限"
causes = append(causes, failureCause(raw, err))
continue
}
successful++
for _, space := range spaces {
if seenDrive[space.SpaceType+":"+space.SpaceID] {
continue
@@ -123,11 +131,21 @@ func Discover(ctx context.Context, runner Runner) (Result, error) {
result.DriveSpaces = append(result.DriveSpaces, space)
}
}
if successful == 0 && len(causes) > 0 {
return result, fmt.Errorf("%w: %w", ErrUnavailable, errors.Join(causes...))
}
return result, nil
}
func describe(scope string, err error) string {
return scope + ":" + err.Error()
reason := "读取失败,请重试"
if errors.Is(err, ErrCredentials) {
reason = "DWS 授权已失效"
}
if errors.Is(err, ErrPermission) {
reason = "当前范围权限不足"
}
return scope + ":" + reason
}
// parseKnowledgeBases 解析 `dws wiki +space-list`:
+1 -1
View File
@@ -127,7 +127,7 @@ func TestDiscoverKeepsKnowledgeBasesWhenDriveIsDenied(t *testing.T) {
if len(result.DriveSpaces) != 0 {
t.Fatalf("drive spaces = %d, want 0", len(result.DriveSpaces))
}
if !strings.Contains(result.DriveNotice, "permission denied") {
if !strings.Contains(result.DriveNotice, "权限") {
t.Fatalf("drive notice = %q", result.DriveNotice)
}
}
+14 -4
View File
@@ -19,8 +19,8 @@ type ExecRunner struct {
Timeout time.Duration
}
// RunDWS 运行 `dws <args…>` 并返回 stdout。
func (r ExecRunner) RunDWS(ctx context.Context, args ...string) ([]byte, error) {
// command keeps device login and source reads on one credential environment.
func (r ExecRunner) command(ctx context.Context, args ...string) (*exec.Cmd, error) {
binary := r.Binary
if binary == "" {
binary = os.Getenv("DWS_BIN")
@@ -32,14 +32,24 @@ func (r ExecRunner) RunDWS(ctx context.Context, args ...string) ([]byte, error)
}
binary = found
}
command := exec.CommandContext(ctx, binary, args...)
command.Env = dwsEnv()
command.WaitDelay = 2 * time.Second
return command, nil
}
// RunDWS uses the same executable and credential environment as device login.
func (r ExecRunner) RunDWS(ctx context.Context, args ...string) ([]byte, error) {
timeout := r.Timeout
if timeout <= 0 {
timeout = 25 * time.Second
}
runCtx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
command := exec.CommandContext(runCtx, binary, args...)
command.Env = dwsEnv()
command, err := r.command(runCtx, args...)
if err != nil {
return nil, err
}
var stdout, stderr bytes.Buffer
command.Stdout = &stdout
command.Stderr = &stderr
+40
View File
@@ -0,0 +1,40 @@
package dingtalkdiscovery
import (
"encoding/json"
"errors"
"fmt"
"strings"
)
var ErrCredentials = errors.New("DWS authorization required")
var ErrPermission = errors.New("DWS directory permission denied")
// failureCause inspects only structured CLI errors; no upstream text is returned.
func failureCause(raw []byte, cause error) error {
var v struct {
Error struct {
Type string `json:"type"`
Code any `json:"code"`
Message string `json:"message"`
} `json:"error"`
}
if json.Unmarshal(raw, &v) != nil {
return ErrUnavailable
}
text := strings.ToLower(v.Error.Type + " " + fmt.Sprint(v.Error.Code) + " " + v.Error.Message)
for _, key := range []string{"permission", "forbidden", "scope", "access denied", "权限", "403"} {
if strings.Contains(text, key) {
return ErrPermission
}
}
for _, key := range []string{"unauth", "not_logged_in", "not logged", "login_required", "login required", "authentication", "auth_required", "credential", "token_expired", "invalid_token", "token expired", "invalid_grant", "未登录", "授权已过期", "授权失效"} {
if strings.Contains(text, key) {
return ErrCredentials
}
}
if errors.Is(cause, ErrCredentials) || errors.Is(cause, ErrPermission) {
return cause
}
return ErrUnavailable
}
+3
View File
@@ -107,6 +107,9 @@ func classifyRole(method, path string) string {
if strings.HasPrefix(path, "/api/v1/users") || strings.HasPrefix(path, "/api/v1/qa-ops/") {
return roleAdmin
}
if strings.HasPrefix(path, "/api/v1/dws-login") || strings.HasPrefix(path, "/api/v1/pp-connection") || strings.HasPrefix(path, "/api/v1/dingtalk-spaces") || strings.HasPrefix(path, "/api/v1/dingtalk-session") {
return roleAdmin
}
if method == http.MethodGet {
return roleViewer
}
+63
View File
@@ -0,0 +1,63 @@
package httpapi
import (
"context"
postgresstore "knowledge-governance/internal/platform/postgres"
"knowledge-governance/internal/ppauth"
"sort"
)
// Credential events are committed with their secret revision in one atomic file.
// Merge them into the existing audit view without copying secrets into SQL.
func combinedSourceAudit(ctx context.Context, reader sourceAuditReader, manager *ppauth.Manager, limit, offset int) (postgresstore.SourceAuditPage, error) {
if manager == nil {
return reader.ListSourceAudit(ctx, limit, offset)
}
events, err := manager.Events(ctx)
if err != nil {
return postgresstore.SourceAuditPage{}, err
}
if len(events) == 0 {
return reader.ListSourceAudit(ctx, limit, offset)
}
start := offset - len(events)
if start < 0 {
start = 0
}
rows := make([]postgresstore.SourceAuditItem, 0, limit+len(events))
total := 0
for next := start; len(rows) < limit+len(events); {
take := limit + len(events) - len(rows)
if take > 100 {
take = 100
}
page, e := reader.ListSourceAudit(ctx, take, next)
if e != nil {
return postgresstore.SourceAuditPage{}, e
}
total = page.Total
rows = append(rows, page.Items...)
next += len(page.Items)
if next >= total || len(page.Items) == 0 {
break
}
}
for _, e := range events {
rows = append(rows, postgresstore.SourceAuditItem{ID: e.ID, ConnectionID: "", SourceName: e.SourceName, Action: e.Action, Actor: e.Actor, CreatedAt: e.CreatedAt, Detail: e.Detail})
}
sort.SliceStable(rows, func(i, j int) bool {
if rows[i].CreatedAt.Equal(rows[j].CreatedAt) {
return rows[i].ID > rows[j].ID
}
return rows[i].CreatedAt.After(rows[j].CreatedAt)
})
skip := offset - start
if skip > len(rows) {
skip = len(rows)
}
end := skip + limit
if end > len(rows) {
end = len(rows)
}
return postgresstore.SourceAuditPage{Items: rows[skip:end], Total: total + len(events), Limit: limit, Offset: offset}, nil
}
+5 -15
View File
@@ -2,7 +2,6 @@ package httpapi
import (
"context"
"errors"
"net/http"
"time"
@@ -25,25 +24,16 @@ func listDingtalkSources(w http.ResponseWriter, r *http.Request, config Config)
if _, ok := requireSessionIdentity(w, r); !ok {
return
}
lister := config.DingtalkSources
if lister == nil {
writeJSON(w, http.StatusNotImplemented, map[string]string{"error": "dingtalk source discovery is not wired"})
identity, _ := requireSessionIdentity(w, r)
if identity.Role != roleAdmin {
writeJSON(w, 403, map[string]string{"error": "需要管理员身份列举数据源"})
return
}
ctx, cancel := context.WithTimeout(r.Context(), 30*time.Second)
defer cancel()
result, err := lister.ListAvailableDingtalkSources(ctx)
result, err := availableDWS(ctx, config)
if err != nil {
if errors.Is(err, dingtalkdiscovery.ErrUnavailable) {
// 环境问题(dws 未登录 / 权限不足 / 二进制缺失):页面显示"暂时列不出来",
// 不是平台故障,也不该把内部错误细节当 500 抛出去。
writeJSON(w, http.StatusServiceUnavailable, map[string]string{
"error": "dingtalk_discovery_unavailable",
"hint": "dws 未登录或缺少租户目录读权限;请先刷新钉钉会话,或让管理员检查 dws 凭据。",
})
return
}
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": "could not list dingtalk sources"})
writeDWSSourceError(w, err)
return
}
writeJSON(w, http.StatusOK, map[string]any{"data": result})
+1 -1
View File
@@ -53,7 +53,7 @@ func TestListDingtalkSourcesReportsEnvironmentProblemsAsUnavailable(t *testing.T
if rec.Code != http.StatusServiceUnavailable {
t.Fatalf("unavailable = %d, want 503 body=%s", rec.Code, rec.Body.String())
}
if !strings.Contains(rec.Body.String(), "dingtalk_discovery_unavailable") {
if !strings.Contains(rec.Body.String(), "dws_unavailable") {
t.Fatalf("body = %s", rec.Body.String())
}
}
+18 -35
View File
@@ -2,59 +2,42 @@ package httpapi
import (
"context"
"knowledge-governance/internal/dingtalkdiscovery"
postgresstore "knowledge-governance/internal/platform/postgres"
"net/http"
"net/http/httptest"
"testing"
"time"
"knowledge-governance/internal/dingtalkdiscovery"
)
func TestDWSNameConflictIsNotASuccessfulRegistration(t *testing.T) {
store := &fakeDingtalkRegistrar{fakeJobStore: &fakeJobStore{}, err: postgresstore.ErrDingtalkSpaceNameTaken}
lister := fakeDingtalkSources{result: dingtalkdiscovery.Result{KnowledgeBases: []dingtalkdiscovery.KnowledgeBase{{Name: "wiki", SpaceID: "testspace"}}}}
rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodPost, "/api/v1/dingtalk-spaces", nil)
registerSpaceThroughDWS(rec, req, Config{Store: store, DingtalkSources: lister, Now: time.Now}, "testspace", "test-op")
if rec.Code != http.StatusConflict {
req := httptest.NewRequest("POST", "/api/v1/dingtalk-spaces", nil)
registerDWSOnlySpace(rec, req, Config{Store: store, DingtalkSources: lister, Now: time.Now}, "testspace", "test-op")
if rec.Code != 409 {
t.Fatalf("name conflict must not claim success: %d", rec.Code)
}
}
// 登记走 dws 不是侧车:解析出来的键、名称、根节点直接决定新来源的确定性 id。
func TestResolveSpaceViaDWSUsesTheSpaceCredentialsAlreadyHeld(t *testing.T) {
lister := fakeDingtalkSources{result: dingtalkdiscovery.Result{
KnowledgeBases: []dingtalkdiscovery.KnowledgeBase{
{Name: "国内技术支持管理", SpaceID: "9JOGOLBQndOyaz4Q"},
{Name: "视觉文档", SpaceID: "9JOGO9EEOMLMWX4Q"},
},
DriveSpaces: []dingtalkdiscovery.DriveSpace{
{Name: "全员文件夹", SpaceID: "102571304", RootFolderID: "g1YGl6Pm2Db8Dr3OayClVxLq0Ee4p79a"},
},
}}
rootKey, name, rootID, spaceID, ok := resolveSpaceViaDWS(
context.Background(), lister, dingtalkdiscovery.SpaceURL("9JOGOLBQndOyaz4Q"))
if !ok || rootKey != "space-9JOGOLBQndOyaz4Q" || name != "国内技术支持管理" || rootID != "9JOGOLBQndOyaz4Q" || spaceID != "9JOGOLBQndOyaz4Q" {
t.Fatalf("knowledge base resolved as key=%q name=%q root=%q space=%q ok=%v", rootKey, name, rootID, spaceID, ok)
result := dingtalkdiscovery.Result{KnowledgeBases: []dingtalkdiscovery.KnowledgeBase{{Name: "国内技术支持管理", SpaceID: "9JOGOLBQndOyaz4Q"}}, DriveSpaces: []dingtalkdiscovery.DriveSpace{{Name: "全员文件夹", SpaceID: "102571304", RootFolderID: "drive-root"}}}
input, err := resolveDWS(result, dingtalkdiscovery.SpaceURL("9JOGOLBQndOyaz4Q"))
if err != nil || input.RootKey != "space-9JOGOLBQndOyaz4Q" || input.Name != "国内技术支持管理" || input.RootID != "9JOGOLBQndOyaz4Q" || input.SpaceID != "9JOGOLBQndOyaz4Q" {
t.Fatalf("wiki resolution=%+v %v", input, err)
}
// 钉盘的根是 rootFolderId,不是空间 id:抓取侧按它遍历。
rootKey, _, rootID, _, ok = resolveSpaceViaDWS(context.Background(), lister, dingtalkdiscovery.SpaceURL("102571304"))
if !ok || rootID != "g1YGl6Pm2Db8Dr3OayClVxLq0Ee4p79a" || rootKey != "space-102571304" {
t.Fatalf("drive space resolved as key=%q root=%q ok=%v", rootKey, rootID, ok)
input, err = resolveDWS(result, dingtalkdiscovery.SpaceURL("102571304"))
if err != nil || input.RootID != "drive-root" || input.RootKey != "space-102571304" {
t.Fatalf("drive resolution=%+v %v", input, err)
}
}
func TestResolveSpaceViaDWSLeavesUnknownSpacesToTheFallback(t *testing.T) {
lister := fakeDingtalkSources{result: dingtalkdiscovery.Result{}}
if _, _, _, _, ok := resolveSpaceViaDWS(context.Background(), lister, dingtalkdiscovery.SpaceURL("unknownspace1")); ok {
t.Fatal("an unknown space must not resolve")
func TestResolveDWSRejectsUnknownAndUnwiredWithoutBrowserFallback(t *testing.T) {
if _, err := resolveDWS(dingtalkdiscovery.Result{}, dingtalkdiscovery.SpaceURL("unknownspace1")); err == nil {
t.Fatal("unknown space resolved")
}
if _, _, _, _, ok := resolveSpaceViaDWS(context.Background(), nil, dingtalkdiscovery.SpaceURL("9JOGOLBQndOyaz4Q")); ok {
t.Fatal("without a lister nothing can resolve")
if _, err := availableDWS(context.Background(), Config{}); err == nil {
t.Fatal("unwired service resolved")
}
if _, _, _, _, ok := resolveSpaceViaDWS(context.Background(), lister, "not-a-space-url"); ok {
t.Fatal("a URL without a space id must not resolve")
if _, err := resolveDWS(dingtalkdiscovery.Result{}, "not-a-space-url"); err == nil {
t.Fatal("invalid space resolved")
}
}
+4 -154
View File
@@ -2,9 +2,7 @@ package httpapi
import (
"bytes"
"context"
"encoding/json"
"errors"
"io"
"log"
"net/http"
@@ -12,10 +10,6 @@ import (
"regexp"
"strings"
"time"
"knowledge-governance/internal/governance"
"knowledge-governance/internal/jobs"
postgresstore "knowledge-governance/internal/platform/postgres"
)
// DingTalk login proxy. The scan-code login itself lives in the
@@ -102,86 +96,15 @@ func registerDingtalkSpace(w http.ResponseWriter, r *http.Request, config Config
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "url is required"})
return
}
// dws 已经持有有效凭据(access 自动续期、refresh 一个月),登记也该走它:
// 只有 dws 解析不出这个空间时,才回落到老的扫码侧车。
if registered, ok := registerSpaceThroughDWS(w, r, config, strings.TrimSpace(input.URL), operationID); ok || registered {
return
}
payload, _ := json.Marshal(map[string]string{"url": strings.TrimSpace(input.URL)})
status, data := proxyDingtalk(http.MethodPost, "/spaces/add", payload)
if status != http.StatusOK {
if status == 0 {
writeJSON(w, http.StatusServiceUnavailable, map[string]string{"error": "dingtalk_login_unavailable"})
return
}
// Relay the sidecar's error (login_required / space_not_found /
// name_conflict) verbatim; bodies are machine codes, no secrets.
writeProxyResult(w, status, data)
return
}
var resolved struct {
Key string `json:"key"`
Name string `json:"name"`
Root string `json:"root"`
}
if err := json.Unmarshal(data, &resolved); err != nil || resolved.Key == "" || resolved.Name == "" || resolved.Root == "" {
writeJSON(w, http.StatusBadGateway, map[string]string{"error": "invalid sidecar registration response"})
return
}
store, ok := config.Store.(interface {
RegisterDingtalkSpace(context.Context, postgresstore.RegisterDingtalkSpaceInput) (string, error)
})
if !ok {
writeJSON(w, http.StatusServiceUnavailable, map[string]string{"error": "registration_unavailable"})
return
}
spaceID := extractDingtalkSpaceID(input.URL)
if spaceID == "" {
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "invalid space url"})
return
}
connectionID, err := store.RegisterDingtalkSpace(r.Context(), postgresstore.RegisterDingtalkSpaceInput{
RootKey: resolved.Key, Name: resolved.Name, SpaceID: spaceID, RootID: resolved.Root, Now: config.Now(), Actor: actorFor(r),
})
switch {
case errors.Is(err, postgresstore.ErrDingtalkSpaceNameTaken):
writeJSON(w, http.StatusConflict, map[string]string{
"error": "space_name_conflict",
"hint": "已有同名来源仍在使用(未下架):请先在该来源卡片上「去掉来源」,或确认选的是同一个知识库空间。",
})
return
case errors.Is(err, postgresstore.ErrDingtalkSpaceIdentityConflict):
writeJSON(w, http.StatusConflict, map[string]string{
"error": "space_identity_conflict",
"hint": "这个登记键已指向另一个知识库空间:请刷新列表后重试;若仍失败,把该来源「去掉来源」后再登记。",
})
return
case err != nil:
// 真正的数据库/内部故障不能伪装成"冲突":换个 500 才有人去查日志。
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": "space_registration_failed"})
return
}
// First scan is enqueued right away: the new connection has no catalog
// head yet, so the generic sources/{id}/refresh endpoint would 404 until
// the first snapshot exists.
if enqueuer, ok := config.Store.(interface {
EnqueueCatalogRefreshWithOperation(context.Context, governance.CatalogSource, string, time.Time) (jobs.Job, bool, error)
}); ok {
if _, _, enqueueErr := enqueuer.EnqueueCatalogRefreshWithOperation(r.Context(), governance.CatalogSource{
ConnectionID: connectionID, SourceType: governance.SourceTypeDingTalk, Name: resolved.Name,
}, operationID+":first", config.Now().UTC()); enqueueErr != nil {
log.Printf("dingtalk space %s registered but first refresh enqueue failed: %v", connectionID, enqueueErr)
}
}
log.Printf("dingtalk space %q registered as connection %s by %s", resolved.Name, connectionID, actorFor(r))
writeJSON(w, http.StatusCreated, map[string]any{"data": map[string]string{
"connection_id": connectionID, "name": resolved.Name,
}})
registerDWSOnlySpace(w, r, config, strings.TrimSpace(input.URL), operationID)
}
var dingtalkSpacePattern = regexp.MustCompile(`/spaces/([A-Za-z0-9_-]+)`)
func extractDingtalkSpaceID(raw string) string {
if !validDingtalkURL(raw) {
return ""
}
match := dingtalkSpacePattern.FindStringSubmatch(raw)
if len(match) == 2 {
return match[1]
@@ -244,76 +167,3 @@ func proxyDingtalk(method, path string, body []byte) (int, []byte) {
}
return resp.StatusCode, data
}
// resolveSpaceViaDWS 用 dws 列举结果定位一个空间:返回确定性登记键、名称与根节点。
// 认不出就 ok=false,调用方回落到老的扫码侧车(历史空间仍能被登记)。
func resolveSpaceViaDWS(ctx context.Context, lister dingtalkSourcesLister, rawURL string) (rootKey, name, rootID, spaceID string, ok bool) {
spaceID = extractDingtalkSpaceID(rawURL)
if lister == nil || spaceID == "" {
return "", "", "", "", false
}
discovered, err := lister.ListAvailableDingtalkSources(ctx)
if err != nil {
return "", "", "", "", false
}
for _, base := range discovered.KnowledgeBases {
if base.SpaceID == spaceID {
// 知识库:URL 里的 id 就是空间 id,抓取侧按 spaces/<id> 匹配。
return "space-" + spaceID, base.Name, spaceID, spaceID, true
}
}
for _, space := range discovered.DriveSpaces {
if space.SpaceID == spaceID && space.RootFolderID != "" {
return "space-" + spaceID, space.Name, space.RootFolderID, spaceID, true
}
}
return "", "", "", "", false
}
// registerSpaceThroughDWS 用 dws 凭据直接登记来源,不再要求扫码会话。
func registerSpaceThroughDWS(w http.ResponseWriter, r *http.Request, config Config, rawURL, operationID string) (bool, bool) {
ctx, cancel := context.WithTimeout(r.Context(), 30*time.Second)
defer cancel()
rootKey, name, rootID, spaceID, ok := resolveSpaceViaDWS(ctx, config.DingtalkSources, rawURL)
if !ok {
return false, false
}
store, wired := config.Store.(interface {
RegisterDingtalkSpace(context.Context, postgresstore.RegisterDingtalkSpaceInput) (string, error)
})
if !wired {
writeJSON(w, http.StatusServiceUnavailable, map[string]string{"error": "registration_unavailable"})
return true, false
}
connectionID, err := store.RegisterDingtalkSpace(ctx, postgresstore.RegisterDingtalkSpaceInput{
RootKey: rootKey, Name: name, SpaceID: spaceID, RootID: rootID, Now: config.Now(), Actor: actorFor(r),
})
switch {
case errors.Is(err, postgresstore.ErrDingtalkSpaceNameTaken):
writeJSON(w, http.StatusConflict, map[string]string{"error": "space_name_conflict", "hint": "已有同名来源,但空间标识不一致;请核对已有来源,不能按名称判定添加成功。"})
return true, false
case errors.Is(err, postgresstore.ErrDingtalkSpaceIdentityConflict):
writeJSON(w, http.StatusConflict, map[string]string{
"error": "space_identity_conflict",
"hint": "这个登记键已指向另一个知识库空间:请刷新列表后重试。",
})
return true, false
case err != nil:
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": "space_registration_failed"})
return true, false
}
if enqueuer, wired := config.Store.(interface {
EnqueueCatalogRefreshWithOperation(context.Context, governance.CatalogSource, string, time.Time) (jobs.Job, bool, error)
}); wired {
if _, _, enqueueErr := enqueuer.EnqueueCatalogRefreshWithOperation(ctx, governance.CatalogSource{
ConnectionID: connectionID, SourceType: governance.SourceTypeDingTalk, Name: name,
}, operationID+":first", config.Now().UTC()); enqueueErr != nil {
log.Printf("dingtalk space %s registered but first refresh enqueue failed: %v", connectionID, enqueueErr)
}
}
log.Printf("dingtalk space %q registered as connection %s by %s (dws)", name, connectionID, actorFor(r))
writeJSON(w, http.StatusCreated, map[string]any{"data": map[string]string{
"connection_id": connectionID, "name": name,
}})
return true, true
}
+2 -1
View File
@@ -9,6 +9,7 @@ import (
"strings"
"testing"
"knowledge-governance/internal/dingtalkdiscovery"
postgresstore "knowledge-governance/internal/platform/postgres"
)
@@ -123,7 +124,7 @@ func TestDingtalkSpaceRegistrationErrorClassification(t *testing.T) {
call := func(storeErr error) (int, string) {
store := &fakeDingtalkRegistrar{fakeJobStore: &fakeJobStore{}, err: storeErr}
handler, err := NewHandler(Config{Store: store})
handler, err := NewHandler(Config{Store: store, DingtalkSources: fakeDingtalkSources{result: dingtalkdiscovery.Result{KnowledgeBases: []dingtalkdiscovery.KnowledgeBase{{Name: "公司级技术支持知识库", SpaceID: "9JOK9EEOMLMWX4Q"}}}}})
if err != nil {
t.Fatalf("NewHandler() error = %v", err)
}
+83
View File
@@ -0,0 +1,83 @@
package httpapi
import (
"errors"
"log"
"net/http"
"net/url"
"knowledge-governance/internal/dingtalkdiscovery"
)
func registerDWSLoginRoutes(mux *http.ServeMux, config Config) {
mux.HandleFunc("POST /api/v1/dws-login", func(w http.ResponseWriter, r *http.Request) { handleDWSLogin(w, r, config, "start") })
mux.HandleFunc("GET /api/v1/dws-login/{id}", func(w http.ResponseWriter, r *http.Request) { handleDWSLogin(w, r, config, "get") })
mux.HandleFunc("DELETE /api/v1/dws-login/{id}", func(w http.ResponseWriter, r *http.Request) { handleDWSLogin(w, r, config, "cancel") })
}
func handleDWSLogin(w http.ResponseWriter, r *http.Request, config Config, action string) {
w.Header().Set("Cache-Control", "no-store")
identity, ok := requireSessionIdentity(w, r)
if !ok {
return
}
if identity.Role != roleAdmin {
writeJSON(w, http.StatusForbidden, map[string]string{"error": "需要管理员身份才能管理 DWS 授权"})
return
}
if action != "get" {
originOK := true
if raw := r.Header.Get("Origin"); raw != "" {
origin, err := url.Parse(raw)
originOK = err == nil && origin.Host == r.Host && (origin.Scheme == "http" || origin.Scheme == "https")
}
if !originOK || r.Header.Get("Sec-Fetch-Site") == "cross-site" || r.Header.Get("X-Governance-Action") != "dws-login" {
writeJSON(w, http.StatusForbidden, map[string]string{"error": "请从本平台的数据源配置页发起授权"})
return
}
}
if config.DWSLogin == nil {
writeJSON(w, http.StatusServiceUnavailable, map[string]string{"error": "DWS 登录服务未接通,请联系管理员"})
return
}
// Bind the challenge to this authenticated administrator and browser session.
owner := identity.UserID + "/" + identity.SessionID
var view dingtalkdiscovery.DeviceLoginView
var err error
switch action {
case "start":
view, err = config.DWSLogin.Start(owner)
case "get":
view, err = config.DWSLogin.Get(owner, r.PathValue("id"))
case "cancel":
view, err = config.DWSLogin.Cancel(owner, r.PathValue("id"))
}
if err != nil {
switch {
case errors.Is(err, dingtalkdiscovery.ErrLoginNotFound):
writeJSON(w, http.StatusNotFound, map[string]string{"error": "授权会话已结束或服务已重启,请重新发起"})
case errors.Is(err, dingtalkdiscovery.ErrLoginBusy):
writeJSON(w, http.StatusConflict, map[string]string{"error": "另一位管理员正在授权,请稍后再试"})
default:
writeJSON(w, http.StatusServiceUnavailable, map[string]string{"error": "无法发起 DWS 授权,请重试"})
}
return
}
if config.PPConnection != nil && (action != "get" || view.State == "succeeded" || view.State == "failed" || view.State == "expired") {
if err := config.PPConnection.AuditOnce(r.Context(), "dws-"+view.ID+"-"+string(view.State), "钉钉账号(DWS)", "dws_authorization", identity.Username, "DWS 授权状态:"+string(view.State)); err != nil {
if action == "start" {
_, _ = config.DWSLogin.Cancel(owner, view.ID)
}
writeJSON(w, 503, map[string]string{"error": "授权审计写入失败,请重试"})
return
}
}
if action != "get" {
log.Printf("DWS authorization action=%s id=%s actor=%s", action, view.ID, identity.Username)
}
status := http.StatusOK
if action == "start" {
status = http.StatusAccepted
}
writeJSON(w, status, map[string]any{"data": view})
}
+82
View File
@@ -0,0 +1,82 @@
package httpapi
import (
"context"
"encoding/json"
"knowledge-governance/internal/dingtalkdiscovery"
"net/http"
"net/http/httptest"
"testing"
)
func TestDWSLoginHTTPAuthenticationOwnershipAndOrigin(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
manager := dingtalkdiscovery.NewDeviceLoginManager(ctx, dingtalkdiscovery.ExecRunner{Binary: "/nonexistent/dws"})
h, err := NewHandler(Config{Store: &fakeJobStore{}, DWSLogin: manager})
if err != nil {
t.Fatal(err)
}
call := func(method, path, role, session, origin, intent string) *httptest.ResponseRecorder {
r := httptest.NewRequest(method, path, nil)
if role != "" {
r = r.WithContext(context.WithValue(r.Context(), sessionKey{}, sessionIdentity{UserID: "admin-user", Username: "tester", Role: role, SessionID: session}))
}
r.Header.Set("Origin", origin)
r.Header.Set("X-Governance-Action", intent)
w := httptest.NewRecorder()
h.ServeHTTP(w, r)
return w
}
for _, role := range []string{"", roleViewer, roleEngineer} {
w := call("POST", "/api/v1/dws-login", role, "s1", "", "dws-login")
if w.Code != 401 && w.Code != 403 {
t.Fatalf("role=%q status=%d", role, w.Code)
}
}
for _, input := range [][2]string{{"", ""}, {"https://foreign.invalid", "dws-login"}} {
w := call("POST", "/api/v1/dws-login", roleAdmin, "s1", input[0], input[1])
if w.Code != 403 {
t.Fatalf("origin/intent not enforced: %d", w.Code)
}
}
w := call("POST", "/api/v1/dws-login", roleAdmin, "s1", "", "dws-login")
if w.Code != 202 || w.Header().Get("Cache-Control") != "no-store" {
t.Fatalf("start=%d body=%s", w.Code, w.Body)
}
var body struct {
Data dingtalkdiscovery.DeviceLoginView `json:"data"`
}
if err := json.Unmarshal(w.Body.Bytes(), &body); err != nil || body.Data.ID == "" {
t.Fatal("missing session", err)
}
path := "/api/v1/dws-login/" + body.Data.ID
for _, method := range []string{"GET", "DELETE"} {
w = call(method, path, roleAdmin, "other-session", "", "dws-login")
if w.Code != 404 {
t.Fatalf("foreign session %s=%d", method, w.Code)
}
w = call(method, path, roleEngineer, "s1", "", "dws-login")
if w.Code != 403 {
t.Fatalf("engineer %s=%d", method, w.Code)
}
}
w = call("GET", path, roleAdmin, "s1", "", "")
if w.Code != 200 {
t.Fatalf("get=%d", w.Code)
}
w = call("DELETE", path, roleAdmin, "s1", "", "dws-login")
if w.Code != 200 {
t.Fatalf("cancel=%d", w.Code)
}
}
func TestDWSLoginRoutesAreAdminOnly(t *testing.T) {
for _, method := range []string{http.MethodGet, http.MethodPost, http.MethodDelete} {
for _, path := range []string{"/api/v1/dws-login", "/api/v1/dws-login/session-id"} {
if got := classifyRole(method, path); got != roleAdmin {
t.Errorf("%s %s role=%s; authorization artifacts must be admin-only", method, path, got)
}
}
}
}
+212
View File
@@ -0,0 +1,212 @@
package httpapi
import (
"context"
"errors"
"knowledge-governance/internal/dingtalkdiscovery"
"knowledge-governance/internal/governance"
"knowledge-governance/internal/jobs"
postgresstore "knowledge-governance/internal/platform/postgres"
"net/http"
"net/url"
"strings"
"time"
)
type dwsSourceError struct {
Status int
Code, Hint string
}
func (e *dwsSourceError) Error() string { return e.Hint }
func writeDWSSourceError(w http.ResponseWriter, err error) {
var e *dwsSourceError
if errors.As(err, &e) {
writeJSON(w, e.Status, map[string]string{"error": e.Code, "hint": e.Hint})
return
}
writeJSON(w, 500, map[string]string{"error": "dws_source_failed", "hint": "读取钉钉来源失败,请重试"})
}
func availableDWS(ctx context.Context, config Config) (dingtalkdiscovery.Result, error) {
identity := ""
if config.DingtalkSources == nil {
return dingtalkdiscovery.Result{}, &dwsSourceError{501, "dws_unwired", "DWS 来源服务未接通"}
}
if reader, ok := config.DingtalkSources.(interface {
ReadDWSMetadata(context.Context) (dingtalkdiscovery.CredentialStatus, error)
}); ok {
status, err := reader.ReadDWSMetadata(ctx)
if err != nil || !status.StatusKnown {
return dingtalkdiscovery.Result{}, &dwsSourceError{503, "dws_status_unavailable", "暂时无法读取 DWS 授权状态,请重试"}
}
identity = status.Identity
if !status.Authenticated && !status.RefreshTokenValid {
return dingtalkdiscovery.Result{}, &dwsSourceError{401, "dws_login_required", "DWS 未登录或授权已失效,请重新授权"}
}
}
result, err := config.DingtalkSources.ListAvailableDingtalkSources(ctx)
if err != nil {
if errors.Is(err, dingtalkdiscovery.ErrCredentials) {
return result, &dwsSourceError{401, "dws_login_required", "DWS 授权已失效,请重新授权"}
}
if errors.Is(err, dingtalkdiscovery.ErrPermission) {
return result, &dwsSourceError{403, "dws_permission_denied", "当前 DWS 账号缺少目录读取权限"}
}
if errors.Is(err, dingtalkdiscovery.ErrUnavailable) {
return result, &dwsSourceError{503, "dws_unavailable", "DWS 目录暂时无法读取,请检查连接后重试"}
}
return result, err
}
if identity != "" {
reader := config.DingtalkSources.(interface {
ReadDWSMetadata(context.Context) (dingtalkdiscovery.CredentialStatus, error)
})
fresh, e := reader.ReadDWSMetadata(ctx)
if e != nil {
return result, &dwsSourceError{503, "dws_status_unavailable", "无法确认当前账号,请重试"}
}
if fresh.Identity != identity {
return result, &dwsSourceError{409, "dws_identity_changed", "DWS 账号或组织已变化,请重新确认来源"}
}
}
result.Identity = identity
if config.Store != nil {
if page, e := config.Store.ListSourceConnections(ctx); e == nil {
seen := map[string]bool{}
for _, c := range page.Items {
seen[c.ID] = true
}
for i, kb := range result.KnowledgeBases {
in := postgresstore.RegisterDingtalkSpaceInput{RootKey: "space-" + kb.SpaceID}
result.KnowledgeBases[i].Registered = seen[in.ConnectionID()]
}
for i, space := range result.DriveSpaces {
in := postgresstore.RegisterDingtalkSpaceInput{RootKey: "space-" + space.SpaceID}
result.DriveSpaces[i].Registered = seen[in.ConnectionID()]
}
}
}
if registry, ok := config.Store.(interface {
RegisteredDingtalkSpaceIDs(context.Context) (map[string]bool, error)
}); ok {
ids, e := registry.RegisteredDingtalkSpaceIDs(ctx)
if e != nil {
return result, &dwsSourceError{503, "dws_registration_state_unavailable", "无法核对已接入空间,请重试"}
}
for i, kb := range result.KnowledgeBases {
result.KnowledgeBases[i].Registered = ids[kb.SpaceID]
}
for i, space := range result.DriveSpaces {
result.DriveSpaces[i].Registered = ids[space.SpaceID]
}
}
return result, nil
}
func resolveDWS(result dingtalkdiscovery.Result, raw string) (postgresstore.RegisterDingtalkSpaceInput, error) {
spaceID := extractDingtalkSpaceID(raw)
if spaceID == "" {
return postgresstore.RegisterDingtalkSpaceInput{}, &dwsSourceError{400, "invalid_space_url", "请输入有效的钉钉空间地址"}
}
for _, base := range result.KnowledgeBases {
if base.SpaceID == spaceID {
return postgresstore.RegisterDingtalkSpaceInput{RootKey: "space-" + spaceID, Name: base.Name, RootID: spaceID, SpaceID: spaceID}, nil
}
}
for _, space := range result.DriveSpaces {
if space.SpaceID == spaceID && space.RootFolderID != "" {
return postgresstore.RegisterDingtalkSpaceInput{RootKey: "space-" + spaceID, Name: space.Name, RootID: space.RootFolderID, SpaceID: spaceID}, nil
}
}
if result.KnowledgeNotice != "" || result.DriveNotice != "" {
return postgresstore.RegisterDingtalkSpaceInput{}, &dwsSourceError{503, "dws_scope_incomplete", "当前目录结果不完整,无法确认所选空间;请检查相应目录权限后重试"}
}
return postgresstore.RegisterDingtalkSpaceInput{}, &dwsSourceError{409, "dws_space_not_visible", "当前 DWS 账号已看不到所选空间,请刷新范围或核对账号权限"}
}
func registerDWSOnlySpace(w http.ResponseWriter, r *http.Request, config Config, raw, operationID string) {
if extractDingtalkSpaceID(raw) == "" {
writeDWSSourceError(w, &dwsSourceError{400, "invalid_space_url", "请输入有效的钉钉空间地址"})
return
}
ctx, cancel := context.WithTimeout(r.Context(), 30*time.Second)
defer cancel()
result, err := availableDWS(ctx, config)
if err != nil {
writeDWSSourceError(w, err)
return
}
if wanted := r.Header.Get("X-DWS-Identity"); wanted != "" && wanted != result.Identity {
writeDWSSourceError(w, &dwsSourceError{409, "dws_identity_changed", "DWS 账号或组织已变化,请重新确认来源"})
return
}
input, err := resolveDWS(result, raw)
if err != nil {
writeDWSSourceError(w, err)
return
}
input.Now = config.Now()
input.Actor = actorFor(r)
store, ok := config.Store.(interface {
RegisterDingtalkSpace(context.Context, postgresstore.RegisterDingtalkSpaceInput) (string, error)
})
if !ok {
writeJSON(w, 503, map[string]string{"error": "registration_unavailable"})
return
}
connectionID, err := store.RegisterDingtalkSpace(ctx, input)
switch {
case errors.Is(err, postgresstore.ErrDingtalkSpaceNameTaken):
writeJSON(w, 409, map[string]string{"error": "space_name_conflict", "hint": "已有同名来源仍在使用:请核对空间身份,必要时先移除旧连接(原去掉来源操作)"})
return
case errors.Is(err, postgresstore.ErrDingtalkSpaceIdentityConflict):
writeJSON(w, 409, map[string]string{"error": "space_identity_conflict", "hint": "登记键已指向另一个空间,请刷新后核对来源"})
return
case err != nil:
writeJSON(w, 500, map[string]string{"error": "space_registration_failed"})
return
}
queued := false
if queue, ok := config.Store.(interface {
EnqueueCatalogRefreshWithOperation(context.Context, governance.CatalogSource, string, time.Time) (jobs.Job, bool, error)
}); ok {
_, _, err = queue.EnqueueCatalogRefreshWithOperation(ctx, governance.CatalogSource{ConnectionID: connectionID, SourceType: governance.SourceTypeDingTalk, Name: input.Name}, "dws-source-first:"+connectionID, config.Now().UTC())
queued = err == nil
}
writeJSON(w, 201, map[string]any{"data": map[string]any{"connection_id": connectionID, "name": input.Name, "first_scan_queued": queued}})
}
func validateDWSSpace(w http.ResponseWriter, r *http.Request, config Config) {
if !credentialAdmin(w, r, "dingtalk-space") {
return
}
var input struct {
URLs []string `json:"urls"`
}
if decodeJSONStrict(r, &input) != nil || len(input.URLs) == 0 || len(input.URLs) > 100 {
writeJSON(w, 400, map[string]string{"error": "请选择有效的空间范围"})
return
}
ctx, cancel := context.WithTimeout(r.Context(), 30*time.Second)
defer cancel()
result, err := availableDWS(ctx, config)
if err != nil {
writeDWSSourceError(w, err)
return
}
if wanted := r.Header.Get("X-DWS-Identity"); wanted != "" && wanted != result.Identity {
writeDWSSourceError(w, &dwsSourceError{409, "dws_identity_changed", "DWS 账号或组织已变化,请重新确认来源"})
return
}
for _, raw := range input.URLs {
if _, err := resolveDWS(result, raw); err != nil {
writeDWSSourceError(w, err)
return
}
}
writeJSON(w, 200, map[string]any{"data": map[string]any{"valid": true, "checked_at": config.Now().UTC(), "count": len(input.URLs)}})
}
func validDingtalkURL(raw string) bool {
if !strings.Contains(raw, "://") {
return true
}
u, err := url.Parse(raw)
return err == nil && u.Scheme == "https" && u.Host == "alidocs.dingtalk.com" && u.User == nil && u.Fragment == ""
}
+30
View File
@@ -0,0 +1,30 @@
package httpapi
import (
"context"
"knowledge-governance/internal/dingtalkdiscovery"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestDWSRegistrationNeverFallsBackToBrowserSession(t *testing.T) {
calls := 0
sidecar := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { calls++; w.WriteHeader(401) }))
defer sidecar.Close()
t.Setenv("DINGTALK_LOGIN_SIDECAR_URL", sidecar.URL)
h, err := NewHandler(Config{Store: &fakeDingtalkRegistrar{fakeJobStore: &fakeJobStore{}}, DingtalkSources: fakeDingtalkSources{err: dingtalkdiscovery.ErrUnavailable}})
if err != nil {
t.Fatal(err)
}
req := httptest.NewRequest("POST", "/api/v1/dingtalk-spaces", strings.NewReader(`{"url":"https://alidocs.dingtalk.com/i/spaces/valid-space-id/overview"}`))
req.Header.Set("Idempotency-Key", "op-dws-only")
req.Header.Set("X-Governance-Action", "dingtalk-space")
req = req.WithContext(context.WithValue(req.Context(), sessionKey{}, sessionIdentity{Username: "tester", Role: roleAdmin}))
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
if calls != 0 || rec.Code != 503 || !strings.Contains(rec.Body.String(), "dws_unavailable") {
t.Fatalf("DWS failed: legacy calls=%d status=%d body=%s", calls, rec.Code, rec.Body.String())
}
}
+1 -1
View File
@@ -52,7 +52,7 @@ func TestOpenAPIContractMatchesRegisteredRoutesAndOmitsSecrets(t *testing.T) {
switch value := node.(type) {
case map[string]any:
for key, child := range value {
if key == "base_url" && path != "components.schemas.QAAISafeSettings.properties" && path != "components.schemas.QAAISaveSettings.properties" {
if key == "base_url" && path != "components.schemas.QAAISafeSettings.properties" && path != "components.schemas.QAAISaveSettings.properties" && path != "components.schemas.PPSafeMetadata.properties" && path != "components.schemas.PPConnectionDraft.properties" {
t.Fatalf("unapproved endpoint exposure at %s", path)
}
next := key
+94
View File
@@ -0,0 +1,94 @@
package httpapi
import (
"context"
"errors"
"knowledge-governance/internal/ppauth"
"net/http"
"net/url"
"time"
)
func credentialAdmin(w http.ResponseWriter, r *http.Request, action string) bool {
w.Header().Set("Cache-Control", "no-store")
identity, ok := requireSessionIdentity(w, r)
if !ok {
return false
}
if identity.Role != roleAdmin {
writeJSON(w, 403, map[string]string{"error": "需要管理员身份管理来源凭据"})
return false
}
if r.Method != "GET" {
originOK := true
if raw := r.Header.Get("Origin"); raw != "" {
u, err := url.Parse(raw)
originOK = err == nil && u.Host == r.Host && (u.Scheme == "http" || u.Scheme == "https")
}
if !originOK || r.Header.Get("Sec-Fetch-Site") == "cross-site" || r.Header.Get("X-Governance-Action") != action {
writeJSON(w, 403, map[string]string{"error": "请从本平台的数据源配置页操作"})
return false
}
}
return true
}
func writePPError(w http.ResponseWriter, err error) {
status := http.StatusBadRequest
if errors.Is(err, ppauth.ErrConflict) {
status = http.StatusConflict
}
writeJSON(w, status, map[string]string{"error": err.Error()})
}
func registerPPConnectionRoutes(mux *http.ServeMux, config Config) {
for _, pattern := range []string{"GET /api/v1/pp-connection", "PUT /api/v1/pp-connection", "POST /api/v1/pp-connection/test"} {
mux.HandleFunc(pattern, func(w http.ResponseWriter, r *http.Request) {
if !credentialAdmin(w, r, "pp-connection") {
return
}
if config.PPConnection == nil {
writeJSON(w, 503, map[string]string{"error": "PP 配置服务未接通,请联系管理员"})
return
}
if r.Method == "GET" {
v, err := config.PPConnection.View()
if err != nil {
writeJSON(w, 503, map[string]string{"error": err.Error()})
return
}
writeJSON(w, 200, map[string]any{"data": v})
return
}
var draft ppauth.Draft
r.Body = http.MaxBytesReader(w, r.Body, 16384)
if decodeJSONStrict(r, &draft) != nil {
writeJSON(w, 400, map[string]string{"error": "PP 配置请求格式不正确"})
return
}
ctx, cancel := context.WithTimeout(r.Context(), 50*time.Second)
defer cancel()
if r.Method == "POST" {
result, err := config.PPConnection.Test(ctx, draft, actorFor(r))
if err != nil {
writePPError(w, err)
return
}
writeJSON(w, 200, map[string]any{"data": result})
return
}
result, err := config.PPConnection.Save(ctx, draft, actorFor(r))
if err != nil {
status := 400
if errors.Is(err, ppauth.ErrConflict) {
status = 409
}
if errors.Is(err, ppauth.ErrRejected) {
status = 422
}
writeJSON(w, status, map[string]any{"error": err.Error(), "data": result})
return
}
writeJSON(w, 200, map[string]any{"data": result})
})
}
}
+142
View File
@@ -0,0 +1,142 @@
package httpapi
import (
"context"
"encoding/json"
"fmt"
"knowledge-governance/internal/jobs"
postgresstore "knowledge-governance/internal/platform/postgres"
"knowledge-governance/internal/ppauth"
"net/http/httptest"
"os"
"path/filepath"
"sort"
"strings"
"testing"
"time"
)
type noPPJobStore struct {
*fakeJobStore
queued int
}
func (s *noPPJobStore) Enqueue(ctx context.Context, request jobs.EnqueueRequest, now time.Time) (*jobs.Job, error) {
s.queued++
return s.fakeJobStore.Enqueue(ctx, request, now)
}
func TestPPHTTPRoundTripRechecksSaveAndNeverEnqueues(t *testing.T) {
probes := 0
m := &ppauth.Manager{Path: t.TempDir() + "/credentials/settings.json", Probe: func(_ context.Context, c ppauth.Credentials) ppauth.Observation {
probes++
return ppauth.Observation{State: "valid", Message: "认证有效 · 目录可读"}
}}
store := &noPPJobStore{fakeJobStore: &fakeJobStore{}}
h, err := NewHandler(Config{Store: store, PPConnection: m})
if err != nil {
t.Fatal(err)
}
call := func(method, path, body string) *httptest.ResponseRecorder {
req := httptest.NewRequest(method, path, strings.NewReader(body))
req.Header.Set("X-Governance-Action", "pp-connection")
req = req.WithContext(context.WithValue(req.Context(), sessionKey{}, sessionIdentity{Username: "tester", Role: roleAdmin}))
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
return rec
}
draft := `{"base_url":"https://api.example.invalid","username":"example","password":"example-private","revision":0}`
for _, op := range [][2]string{{"POST", "/api/v1/pp-connection/test"}, {"PUT", "/api/v1/pp-connection"}, {"GET", "/api/v1/pp-connection"}} {
rec := call(op[0], op[1], draft)
if rec.Code != 200 {
t.Fatalf("%s status=%d body=%s", op[0], rec.Code, rec.Body)
}
if strings.Contains(rec.Body.String(), "example-private") || strings.Contains(rec.Body.String(), `"password":`) {
t.Fatal("credential leaked through response")
}
}
if probes != 2 || store.queued != 0 {
t.Fatalf("save must recheck and must not enqueue: checks=%d jobs=%d", probes, store.queued)
}
rec := call("PUT", "/api/v1/pp-connection", draft)
if rec.Code != 409 {
t.Fatalf("stale revision accepted: %d", rec.Code)
}
}
func TestPPHTTPRefusesCrossOriginBeforeProbe(t *testing.T) {
probes := 0
m := &ppauth.Manager{Path: t.TempDir() + "/credentials/settings.json", Probe: func(_ context.Context, _ ppauth.Credentials) ppauth.Observation {
probes++
return ppauth.Observation{State: "valid"}
}}
h, _ := NewHandler(Config{Store: &fakeJobStore{}, PPConnection: m})
req := httptest.NewRequest("POST", "/api/v1/pp-connection/test", strings.NewReader(`{}`))
req.Header.Set("Origin", "https://foreign.example.invalid")
req.Header.Set("X-Governance-Action", "pp-connection")
req = req.WithContext(context.WithValue(req.Context(), sessionKey{}, sessionIdentity{Username: "tester", Role: roleAdmin}))
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
if rec.Code != 403 || probes != 0 {
t.Fatal("cross-origin request reached credential probe")
}
}
type combinedAuditStore struct {
*fakeJobStore
items []postgresstore.SourceAuditItem
}
func (s *combinedAuditStore) ListSourceAudit(_ context.Context, limit, offset int) (postgresstore.SourceAuditPage, error) {
start := offset
if start > len(s.items) {
start = len(s.items)
}
end := start + limit
if end > len(s.items) {
end = len(s.items)
}
return postgresstore.SourceAuditPage{Items: s.items[start:end], Total: len(s.items), Limit: limit, Offset: offset}, nil
}
func TestPPSourceAuditSharesTheExistingPagedView(t *testing.T) {
ctx := context.Background()
m := &ppauth.Manager{Path: t.TempDir() + "/credentials/settings.json"}
if err := os.Mkdir(filepath.Dir(m.Path), 0700); err != nil {
t.Fatal(err)
}
now := time.Now().UTC()
store := &combinedAuditStore{fakeJobStore: &fakeJobStore{}}
expected := []postgresstore.SourceAuditItem{}
for i := 0; i < 8; i++ {
item := postgresstore.SourceAuditItem{ID: fmt.Sprintf("db-%02d", i), CreatedAt: now.Add(-time.Duration(i*2) * time.Minute), SourceName: "existing source"}
store.items = append(store.items, item)
expected = append(expected, item)
}
state := ppauth.State{}
for i := 0; i < 4; i++ {
event := ppauth.AuditEvent{ID: fmt.Sprintf("auth-%02d", i), CreatedAt: now.Add(-time.Duration(i*2+1) * time.Minute), SourceName: "PP", Action: "credential_test", Actor: "tester", Detail: "只读连接检查"}
state.Audit = append(state.Audit, event)
expected = append(expected, postgresstore.SourceAuditItem{ID: event.ID, CreatedAt: event.CreatedAt, SourceName: event.SourceName})
}
raw, _ := json.Marshal(state)
if err := os.WriteFile(m.Path, raw, 0600); err != nil {
t.Fatal(err)
}
sort.Slice(expected, func(i, j int) bool { return expected[i].CreatedAt.After(expected[j].CreatedAt) })
for offset := 0; offset <= len(expected); offset++ {
page, err := combinedSourceAudit(ctx, store, m, 3, offset)
if err != nil || page.Total != len(expected) {
t.Fatalf("offset=%d total=%d err=%v", offset, page.Total, err)
}
end := offset + 3
if end > len(expected) {
end = len(expected)
}
if len(page.Items) != end-offset {
t.Fatalf("wrong page length at %d", offset)
}
for i, item := range page.Items {
if item.ID != expected[offset+i].ID {
t.Fatalf("audit order mismatch at offset %d", offset)
}
}
}
}
+42
View File
@@ -0,0 +1,42 @@
package httpapi
import (
"context"
"net/http/httptest"
"testing"
)
func TestPPAuthRoutesRequireAdminAndFailClosedWhenUnwired(t *testing.T) {
h, err := NewHandler(Config{Store: &fakeJobStore{}})
if err != nil {
t.Fatal(err)
}
for _, method := range []string{"GET", "PUT", "POST"} {
path := "/api/v1/pp-connection"
if method == "POST" {
path += "/test"
}
for _, role := range []string{"", roleViewer, roleEngineer, roleAdmin} {
req := httptest.NewRequest(method, path, nil)
if role != "" {
req = req.WithContext(context.WithValue(req.Context(), sessionKey{}, sessionIdentity{UserID: "admin", Username: "tester", Role: role, SessionID: "session"}))
}
req.Header.Set("X-Governance-Action", "pp-connection")
res := httptest.NewRecorder()
h.ServeHTTP(res, req)
want := 403
if role == "" {
want = 401
}
if role == roleAdmin {
want = 503
}
if res.Code != want {
t.Errorf("%s role=%q status=%d want=%d", method, role, res.Code, want)
}
}
if got := classifyRole(method, path); got != roleAdmin {
t.Errorf("%s classified=%q; credential metadata and tests must be admin only", method, got)
}
}
}
+7
View File
@@ -15,10 +15,12 @@ import (
"time"
"knowledge-governance/internal/backuphost"
"knowledge-governance/internal/dingtalkdiscovery"
"knowledge-governance/internal/governance"
"knowledge-governance/internal/jobs"
"knowledge-governance/internal/monitoring"
postgresstore "knowledge-governance/internal/platform/postgres"
"knowledge-governance/internal/ppauth"
"knowledge-governance/internal/qaopsai"
"knowledge-governance/internal/qaopsmirror"
"knowledge-governance/internal/qaopsorg"
@@ -222,6 +224,8 @@ type Config struct {
DingtalkSources dingtalkSourcesLister
// DingtalkDocuments 列在线文档/表格并校验可读(只读,dws)。
DingtalkDocuments dingtalkDocLister
DWSLogin *dingtalkdiscovery.DeviceLoginManager
PPConnection *ppauth.Manager
NewID func() (string, error)
Auth *AuthConfig
}
@@ -256,6 +260,9 @@ func NewHandler(config Config) (http.Handler, error) {
config.NewID = newID
}
mux := http.NewServeMux()
registerDWSLoginRoutes(mux, config)
registerPPConnectionRoutes(mux, config)
mux.HandleFunc("POST /api/v1/dingtalk-spaces/validate", func(w http.ResponseWriter, r *http.Request) { validateDWSSpace(w, r, config) })
registerQAAIRoutes(mux, config)
mux.HandleFunc("POST /api/v1/qa-ops/departments/statistics", func(w http.ResponseWriter, r *http.Request) { qaDepartmentStatistics(w, r, config) })
mux.HandleFunc("POST /api/v1/qa-ops/dashboard", func(w http.ResponseWriter, r *http.Request) { qaOperationsDashboard(w, r, config) })
+1 -1
View File
@@ -35,7 +35,7 @@ func listSourceAudit(w http.ResponseWriter, r *http.Request, config Config) {
writeJSON(w, 501, map[string]string{"error": "audit not configured"})
return
}
page, err := reader.ListSourceAudit(r.Context(), limit, offset)
page, err := combinedSourceAudit(r.Context(), reader, config.PPConnection, limit, offset)
if err != nil {
writeJSON(w, 500, map[string]string{"error": "无法读取数据源操作记录"})
return
+1 -1
View File
@@ -31,7 +31,7 @@ type RealProviderConfig struct {
WeKnoraBaseURL string
MinerUBaseURL string
// TestWeKnoraResolver reads the test endpoint an admin saved in the web
// settings, so 「服务配置」is the single place the console probes. The env
// settings, so 「知识库映射 → WeKnora 连接」is the single place the console probes. The env
// value above stays the bootstrap fallback for containers wired before the
// UI existed.
TestWeKnoraResolver func(ctx context.Context) string
@@ -0,0 +1,36 @@
package postgres
import (
"context"
"net/url"
"strings"
)
// RegisteredDingtalkSpaceIDs also covers historical connections whose root key
// predates DWS; a source name is never an identity.
func (s *Store) RegisteredDingtalkSpaceIDs(ctx context.Context) (map[string]bool, error) {
rows, err := s.db.Query(ctx, "SELECT base_url FROM source_connections WHERE source_type='dingtalk' AND archived_at IS NULL")
if err != nil {
return nil, err
}
defer rows.Close()
result := map[string]bool{}
for rows.Next() {
var raw string
if err := rows.Scan(&raw); err != nil {
return nil, err
}
u, err := url.Parse(raw)
if err != nil {
continue
}
space := u.Query().Get("space")
if space == "" && strings.HasPrefix(u.Path, "spaces/") {
space = strings.TrimPrefix(u.Path, "spaces/")
}
if space != "" {
result[space] = true
}
}
return result, rows.Err()
}
+356
View File
@@ -0,0 +1,356 @@
package ppauth
import (
"context"
"crypto/rand"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"net/url"
"os"
"path/filepath"
"strings"
"syscall"
"time"
)
type Credentials struct {
ID string `json:"id"`
BaseURL string `json:"base_url"`
Username string `json:"username"`
Password string `json:"password"`
}
type Draft struct {
BaseURL string `json:"base_url"`
Username string `json:"username"`
Password string `json:"password,omitempty"`
PendingID string `json:"pending_id,omitempty"`
Revision int `json:"revision"`
}
type Observation struct {
State string `json:"state"`
Message string `json:"message"`
CheckedAt string `json:"checked_at"`
Previous *Observation `json:"previous,omitempty"`
}
type Metadata struct {
ID string `json:"id"`
BaseURL string `json:"base_url"`
Username string `json:"username"`
PasswordConfigured bool `json:"password_configured"`
}
type View struct {
Revision int `json:"revision"`
Active *Metadata `json:"active"`
Pending *Metadata `json:"pending"`
Observation *Observation `json:"observation"`
PendingObservation *Observation `json:"pending_observation"`
}
type AuditEvent struct {
ID string `json:"id"`
SourceName string `json:"source_name"`
Action string `json:"action"`
Actor string `json:"actor"`
CreatedAt time.Time `json:"created_at"`
Detail string `json:"detail"`
}
type State struct {
Revision int `json:"revision"`
Active *Credentials `json:"active"`
Pending *Credentials `json:"pending"`
Observation *Observation `json:"observation"`
PendingObservation *Observation `json:"pending_observation"`
Audit []AuditEvent `json:"audit"`
}
type Manager struct {
Path string
LegacyPath string
Probe func(context.Context, Credentials) Observation
}
var ErrRejected = errors.New("PP 配置校验未通过,原配置保留")
var ErrInvalid = errors.New("API 地址必须为 HTTPS 根地址,且不能包含凭据、查询参数或片段")
var ErrConflict = errors.New("配置已被更新,请刷新后重新提交")
func ValidateBaseURL(raw string) bool {
u, err := url.Parse(raw)
return err == nil && len(raw) <= 300 && strings.TrimSpace(raw) == raw && u.Scheme == "https" && u.Host != "" && u.Hostname() != "" && u.Opaque == "" && u.User == nil && u.RawQuery == "" && !u.ForceQuery && u.Fragment == "" && !strings.Contains(raw, "#") && (u.Path == "" || u.Path == "/")
}
func metadata(c *Credentials) *Metadata {
if c == nil {
return nil
}
return &Metadata{ID: c.ID, BaseURL: c.BaseURL, Username: c.Username, PasswordConfigured: c.Password != ""}
}
func view(s State) View {
return View{Revision: s.Revision, Active: metadata(s.Active), Pending: metadata(s.Pending), Observation: s.Observation, PendingObservation: s.PendingObservation}
}
func randomID() string {
var b [16]byte
if _, err := rand.Read(b[:]); err != nil {
panic("secure random unavailable")
}
return hex.EncodeToString(b[:])
}
func (m *Manager) locked(ctx context.Context, fn func(*State) (bool, error)) error {
if !filepath.IsAbs(m.Path) {
return errors.New("PP credential storage path is invalid")
}
dir := filepath.Dir(m.Path)
if err := os.MkdirAll(dir, 0700); err != nil {
return errors.New("PP 配置存储不可用")
}
info, err := os.Lstat(dir)
if err != nil || !info.IsDir() || info.Mode().Perm()&0077 != 0 {
return errors.New("PP 配置目录权限必须为 0700")
}
lockPath := m.Path + ".lock"
if info, e := os.Lstat(lockPath); e == nil && (info.Mode()&os.ModeSymlink != 0 || info.Mode().Perm()&0077 != 0) {
return errors.New("PP 配置锁权限不安全")
}
lock, err := os.OpenFile(lockPath, os.O_CREATE|os.O_RDWR, 0600)
if err != nil {
return errors.New("PP 配置存储不可用")
}
defer lock.Close()
for {
err = syscall.Flock(int(lock.Fd()), syscall.LOCK_EX|syscall.LOCK_NB)
if err == nil {
break
}
if err != syscall.EWOULDBLOCK {
return errors.New("PP 配置锁不可用")
}
select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(20 * time.Millisecond):
}
}
defer syscall.Flock(int(lock.Fd()), syscall.LOCK_UN)
var s State
info, err = os.Lstat(m.Path)
if err == nil {
if info.Mode()&os.ModeSymlink != 0 || !info.Mode().IsRegular() || info.Mode().Perm()&0077 != 0 || info.Size() > 16<<20 {
return errors.New("PP 凭据文件权限或格式不安全")
}
raw, e := os.ReadFile(m.Path)
if e != nil || json.Unmarshal(raw, &s) != nil {
return errors.New("无法读取 PP 配置")
}
} else if !errors.Is(err, os.ErrNotExist) {
return errors.New("无法读取 PP 配置")
} else if m.LegacyPath != "" {
raw, e := os.ReadFile(m.LegacyPath)
if e == nil {
var c Credentials
if json.Unmarshal(raw, &c) != nil {
return errors.New("旧 PP 配置格式不正确")
}
if c.BaseURL == "" {
c.BaseURL = "https://api.dobot.cn"
}
c.ID = "legacy"
s.Active = &c
} else if !errors.Is(e, os.ErrNotExist) {
return errors.New("无法读取旧 PP 配置")
}
}
changed, err := fn(&s)
if err != nil || !changed {
return err
}
raw, err := json.Marshal(s)
if err != nil {
return errors.New("PP 配置保存失败")
}
f, err := os.CreateTemp(dir, ".pp-next-")
if err != nil {
return errors.New("PP 配置保存失败")
}
tmp := f.Name()
defer os.Remove(tmp)
if err = f.Chmod(0600); err == nil {
_, err = f.Write(raw)
}
if err == nil {
err = f.Sync()
}
closeErr := f.Close()
if err == nil {
err = closeErr
}
if err == nil {
err = os.Rename(tmp, m.Path)
}
if err != nil {
return errors.New("PP 配置保存失败")
}
d, e := os.Open(dir)
if e != nil {
return errors.New("PP 配置持久化未确认")
}
defer d.Close()
if d.Sync() != nil {
return errors.New("PP 配置持久化未确认")
}
return nil
}
func (m *Manager) View() (v View, err error) {
err = m.locked(context.Background(), func(s *State) (bool, error) { v = view(*s); return false, nil })
return
}
func (m *Manager) snapshot(ctx context.Context) (s State, err error) {
err = m.locked(ctx, func(state *State) (bool, error) { s = *state; return false, nil })
return
}
func candidate(s State, d Draft) (Credentials, error) {
if !ValidateBaseURL(d.BaseURL) {
return Credentials{}, ErrInvalid
}
if len(d.Username) > 255 || strings.ContainsAny(d.Username, "\n\r\x00") || len(d.Password) > 8192 {
return Credentials{}, errors.New("账号或密码格式不正确")
}
c := Credentials{ID: randomID(), BaseURL: strings.TrimRight(d.BaseURL, "/"), Username: strings.TrimSpace(d.Username), Password: d.Password}
if c.Password == "" && d.PendingID != "" {
if s.Pending == nil || s.Pending.ID != d.PendingID {
return Credentials{}, ErrConflict
}
c.Password = s.Pending.Password
} else if c.Password == "" && s.Active != nil {
c.Password = s.Active.Password
}
return c, nil
}
func same(a, b *Credentials) bool {
return a != nil && b != nil && a.BaseURL == b.BaseURL && a.Username == b.Username && a.Password == b.Password
}
func (m *Manager) probe(ctx context.Context, c Credentials) Observation {
o := Observation{State: "incomplete", Message: "配置未完成,请填写账号和密码"}
if c.Username != "" && c.Password != "" {
if m.Probe == nil {
o = Observation{State: "unavailable", Message: "PP 检查服务未接通"}
} else {
o = m.Probe(ctx, c)
}
}
switch o.State {
case "valid", "auth_failed", "permission_denied", "incomplete", "unavailable", "invalid_response":
default:
o = Observation{State: "unavailable", Message: "检查结果不可识别,请重试"}
}
if o.CheckedAt == "" {
o.CheckedAt = time.Now().UTC().Format(time.RFC3339Nano)
}
return o
}
func addAudit(s *State, source, action, actor, detail string) {
s.Audit = append(s.Audit, AuditEvent{ID: "auth-" + randomID(), SourceName: source, Action: action, Actor: actor, CreatedAt: time.Now().UTC(), Detail: detail})
}
func retainObservation(old *Observation, next Observation) *Observation {
if next.State == "unavailable" && old != nil {
if old.State == "unavailable" && old.Previous != nil {
next.Previous = old.Previous
} else {
next.Previous = old
}
}
return &next
}
func (m *Manager) Save(ctx context.Context, d Draft, actor string) (v View, err error) {
s, err := m.snapshot(ctx)
if err != nil {
return v, err
}
if s.Revision != d.Revision {
return v, ErrConflict
}
c, err := candidate(s, d)
if err != nil {
return v, err
}
o := m.probe(ctx, c)
rejected := o.State == "auth_failed" || o.State == "permission_denied" || o.State == "invalid_response"
err = m.locked(ctx, func(current *State) (bool, error) {
if current.Revision != s.Revision {
return false, ErrConflict
}
if rejected {
if same(current.Active, &c) {
current.Observation = retainObservation(current.Observation, o)
}
addAudit(current, "PP 资料下载中心", "credential_rejected", actor, o.Message)
v = view(*current)
return true, nil
}
current.Revision++
if o.State == "valid" {
current.Active = &c
current.Pending = nil
current.Observation = retainObservation(current.Observation, o)
current.PendingObservation = nil
} else {
current.Pending = &c
current.PendingObservation = &o
}
detail := "认证配置已保存并生效"
if o.State != "valid" {
detail = "修改已保存为待验证,未替换生效配置"
}
if d.Password != "" {
detail += ";密码已更新"
}
addAudit(current, "PP 资料下载中心", "credential_configured", actor, detail)
v = view(*current)
return true, nil
})
if err == nil && rejected {
err = fmt.Errorf("%w:%s", ErrRejected, o.Message)
}
return
}
func (m *Manager) Test(ctx context.Context, d Draft, actor string) (o Observation, err error) {
s, err := m.snapshot(ctx)
if err != nil {
return o, err
}
if s.Revision != d.Revision {
return o, ErrConflict
}
c, err := candidate(s, d)
if err != nil {
return o, err
}
o = m.probe(ctx, c)
err = m.locked(ctx, func(current *State) (bool, error) {
if current.Revision != s.Revision {
return false, ErrConflict
}
if same(current.Active, &c) {
current.Observation = retainObservation(current.Observation, o)
}
addAudit(current, "PP 资料下载中心", "credential_test", actor, o.Message)
return true, nil
})
return
}
func (m *Manager) Audit(ctx context.Context, source, action, actor, detail string) error {
return m.locked(ctx, func(s *State) (bool, error) { addAudit(s, source, action, actor, detail); return true, nil })
}
func (m *Manager) AuditOnce(ctx context.Context, id, source, action, actor, detail string) error {
return m.locked(ctx, func(s *State) (bool, error) {
for _, e := range s.Audit {
if e.ID == id {
return false, nil
}
}
addAudit(s, source, action, actor, detail)
s.Audit[len(s.Audit)-1].ID = id
return true, nil
})
}
func (m *Manager) Events(ctx context.Context) (events []AuditEvent, err error) {
err = m.locked(ctx, func(s *State) (bool, error) { events = append(events, s.Audit...); return false, nil })
return
}
+35
View File
@@ -0,0 +1,35 @@
package ppauth
import (
"context"
"errors"
"testing"
)
func TestSaveValidatesBeforeActivationAndTransientKeepsActive(t *testing.T) {
state := "valid"
seen := ""
manager := &Manager{Path: t.TempDir() + "/credentials/settings.json", Probe: func(_ context.Context, c Credentials) Observation {
seen = c.Password
return Observation{State: state, Message: state}
}}
view, err := manager.Save(context.Background(), Draft{BaseURL: "https://api.example.invalid", Username: "example", Password: "example-password", Revision: 0}, "tester")
if err != nil || view.Active == nil || view.Active.Username != "example" || !view.Active.PasswordConfigured || seen != "example-password" {
t.Fatalf("validated save did not activate: %+v %v", view, err)
}
activeID := view.Active.ID
state = "unavailable"
view, err = manager.Save(context.Background(), Draft{BaseURL: "https://changed.example.invalid", Username: "changed", Revision: view.Revision}, "tester")
if err != nil || view.Active == nil || view.Active.ID != activeID || view.Pending == nil || view.Pending.Username != "changed" || seen != "example-password" {
t.Fatalf("transient save replaced active or lost password: %+v %v", view, err)
}
state = "auth_failed"
_, err = manager.Save(context.Background(), Draft{BaseURL: "https://changed.example.invalid", Username: "changed", Password: "bad-example", Revision: view.Revision}, "tester")
if !errors.Is(err, ErrRejected) {
t.Fatalf("explicit rejection allowed activation: %v", err)
}
after, _ := manager.View()
if after.Active.ID != activeID {
t.Fatal("rejection changed active config")
}
}
+104
View File
@@ -0,0 +1,104 @@
package ppauth
import (
"context"
"encoding/json"
"errors"
"os"
"os/exec"
"strings"
"sync"
"sync/atomic"
"testing"
)
func TestPendingPasswordSurvivesRevalidationWithoutReturningSecrets(t *testing.T) {
state := "valid"
seen := ""
m := &Manager{Path: t.TempDir() + "/credentials/settings.json", Probe: func(_ context.Context, c Credentials) Observation {
seen = c.Password
return Observation{State: state, Message: state}
}}
v, err := m.Save(context.Background(), Draft{BaseURL: "https://api.example.invalid", Username: "example", Password: "example-old"}, "tester")
if err != nil {
t.Fatal(err)
}
state = "unavailable"
v, err = m.Save(context.Background(), Draft{BaseURL: "https://api.example.invalid", Username: "example", Password: "example-new", Revision: v.Revision}, "tester")
if err != nil || v.Pending == nil {
t.Fatalf("pending save: %v", err)
}
state = "valid"
v, err = m.Save(context.Background(), Draft{BaseURL: v.Pending.BaseURL, Username: v.Pending.Username, PendingID: v.Pending.ID, Revision: v.Revision}, "tester")
if err != nil || seen != "example-new" || v.Pending != nil {
t.Fatal("saved pending password was not reused")
}
raw, _ := json.Marshal(v)
if strings.Contains(string(raw), "example-new") || strings.Contains(string(raw), `"password":`) {
t.Fatal("metadata leaked secret")
}
info, err := os.Stat(m.Path)
if err != nil || info.Mode().Perm() != 0600 {
t.Fatal("secret file not mode 0600")
}
}
func TestConcurrentSavesCannotOverwriteANewerRevision(t *testing.T) {
var calls atomic.Int32
barrier := make(chan struct{})
m := &Manager{Path: t.TempDir() + "/credentials/settings.json", Probe: func(_ context.Context, c Credentials) Observation {
if calls.Add(1) == 2 {
close(barrier)
}
<-barrier
return Observation{State: "valid", Message: "valid"}
}}
outcomes := make(chan error, 2)
var wg sync.WaitGroup
for _, name := range []string{"example-one", "example-two"} {
wg.Add(1)
go func(user string) {
defer wg.Done()
_, err := m.Save(context.Background(), Draft{BaseURL: "https://api.example.invalid", Username: user, Password: "example-password"}, "tester")
outcomes <- err
}(name)
}
wg.Wait()
close(outcomes)
success, conflict := 0, 0
for err := range outcomes {
if err == nil {
success++
} else if errors.Is(err, ErrConflict) {
conflict++
} else {
t.Fatal(err)
}
}
if success != 1 || conflict != 1 {
t.Fatalf("success=%d conflict=%d", success, conflict)
}
}
func TestFailedObservationRetainsPreviousSuccess(t *testing.T) {
state := "valid"
m := &Manager{Path: t.TempDir() + "/credentials/settings.json", Probe: func(_ context.Context, c Credentials) Observation { return Observation{State: state, Message: state} }}
v, err := m.Save(context.Background(), Draft{BaseURL: "https://api.example.invalid", Username: "example", Password: "example-password"}, "tester")
if err != nil {
t.Fatal(err)
}
state = "unavailable"
_, err = m.Test(context.Background(), Draft{BaseURL: v.Active.BaseURL, Username: v.Active.Username, Revision: v.Revision}, "tester")
if err != nil {
t.Fatal(err)
}
v, err = m.View()
if err != nil || v.Observation.Previous == nil || v.Observation.Previous.State != "valid" {
t.Fatal("failed check erased previous successful observation")
}
}
func TestPPPythonProtocolAndConfigurationConsumers(t *testing.T) {
command := exec.Command("python3", "-m", "unittest", "discover", "-s", "../../scripts", "-p", "test_pp_client.py", "-v")
output, err := command.CombinedOutput()
if err != nil {
t.Fatalf("PP protocol regression: %v\n%s", err, output)
}
}
+31
View File
@@ -0,0 +1,31 @@
package ppauth
import (
"bytes"
"context"
"encoding/json"
"os"
"os/exec"
"time"
)
// CommandProbe reuses the worker's read-only PP adapter; secrets travel on stdin.
func CommandProbe(python, script string) func(context.Context, Credentials) Observation {
return func(ctx context.Context, c Credentials) Observation {
ctx, cancel := context.WithTimeout(ctx, 45*time.Second)
defer cancel()
raw, _ := json.Marshal(c)
command := exec.CommandContext(ctx, python, script)
command.Stdin = bytes.NewReader(raw)
command.Env = []string{"PATH=" + os.Getenv("PATH"), "PYTHONUNBUFFERED=1"}
out, err := command.Output()
if err != nil || len(out) > 8192 {
return Observation{State: "unavailable", Message: "PP 检查服务暂不可用,请重试"}
}
var o Observation
if json.Unmarshal(out, &o) != nil {
return Observation{State: "unavailable", Message: "PP 检查结果不可识别"}
}
return o
}
}
+1 -1
View File
@@ -181,7 +181,7 @@ func NewCommandRunner(pythonPath, scriptPath, spoolRoot, spoolPrefix string, max
// ——钉钉取件在真实链路上会整体失败(2026-10-10 端到端验收实测;脚本单跑正常,
// 因为那只绕过了这个白名单)。
var dwsEnvNames = []string{
"ARTIFACT_DINGTALK_FETCHER", "DWS_BIN", "DWS_CONFIG_DIR", "DWS_HOME", "DWS_DISABLE_KEYCHAIN",
"ARTIFACT_DINGTALK_FETCHER", "PP_AUTH_STATE_FILE", "PP_LEGACY_CREDENTIALS_FILE", "DWS_BIN", "DWS_CONFIG_DIR", "DWS_HOME", "DWS_DISABLE_KEYCHAIN",
}
func forwardedDWSEnv() []string {
+18 -4
View File
@@ -278,7 +278,7 @@ def pp_find_entry(manifest):
def pp_download(manifest, max_bytes):
entry = pp_find_entry(manifest)
if entry is None:
raise MissingSource("PP ledger entry for this locator no longer exists")
entry = {}
# Ledger titles may omit the extension; recover the true file name from the
# local path or source URL before type checks run.
manifest_name = manifest.setdefault("name", "")
@@ -301,9 +301,23 @@ def pp_download(manifest, max_bytes):
if expected_hash and len(expected_hash) == 64 and actual != expected_hash:
raise FetchError("PP local copy hash mismatch (ledger changed?)", retryable=True, code="local_mismatch")
return str(candidate), size, actual, True
url = entry.get("url", "")
if not url:
raise MissingSource("PP ledger entry has no local file and no download URL")
from pp_client import active_client, record_observation, observation, PPError
client = None
try:
client = active_client()
inventory = client.inventory(client.login())
record_observation(client.config, observation('valid'))
except PPError as error:
if client is not None:
record_observation(client.config, observation(error.state))
raise FetchError(str(error), retryable=error.state == 'unavailable', code='pp_' + error.state) from None
locator = manifest.get('locator_hash', '')
matches = [row for row in inventory['items'] if row.get('online') == 1 and (row.get('potalGroup') or {}).get('groupName') == '公开' and digest_of(str(row.get('fileUrl') or '').strip()) == locator]
if not matches:
raise MissingSource('PP file is no longer visible in the configured portal')
url = str(matches[0].get('fileUrl') or '').strip()
if not pathlib.PurePosixPath(manifest['name']).suffix:
manifest['name'] += pathlib.PurePosixPath(urllib.parse.unquote(urllib.parse.urlsplit(url).path)).suffix
path, size, actual = http_get(url, {}, 180, max_bytes)
expected_hash = str(entry.get("sha256", "")).lower()
if expected_hash and len(expected_hash) == 64 and actual != expected_hash:
+32 -3
View File
@@ -172,14 +172,43 @@ def npm_audit(args: list[str]) -> tuple[dict, str | None]:
return {}, f"npm audit 输出解析不了(exit={p.returncode}):{(p.stdout or p.stderr)[:300]}"
def advisory_ids(vulns: dict, name: str, seen: set[str] | None = None) -> list[str]:
"""收集一个包命中的 GHSA 编号,沿 via 里的依赖链向上追。
为什么不能只看本包的 via:npm 对 fdir/tinyglobby/vite 这类"由于依赖了有洞的包"
的条目,via 里只有依赖名(字符串),直接取 url 会得到空 id;空 id 的发现既没法
写进例外表(例外要求 id 非空),也会让排查人以为"没有具体漏洞"。追到链底才是
真正要豁免/修复的那条公告。
"""
seen = seen or set()
if name in seen:
return []
seen.add(name)
entry = vulns.get(name)
if not isinstance(entry, dict):
return []
ids: list[str] = []
for via in entry.get("via") or []:
if isinstance(via, dict) and via.get("url"):
ids.append(str(via["url"]).rsplit("/", 1)[-1])
elif isinstance(via, str):
ids.extend(advisory_ids(vulns, via, seen))
# 去重但保持稳定顺序,便于例外表逐字核对
out: list[str] = []
for item in ids:
if item and item not in out:
out.append(item)
return out
def audit_findings(data: dict, scope: str) -> list[dict]:
out = []
for name, adv in (data.get("vulnerabilities") or {}).items():
vulns = data.get("vulnerabilities") or {}
for name, adv in vulns.items():
if not isinstance(adv, dict):
continue
sev = str(adv.get("severity") or "").lower()
ids = [v.get("url", "").rsplit("/", 1)[-1]
for v in (adv.get("via") or []) if isinstance(v, dict) and v.get("url")]
ids = advisory_ids(vulns, name)
out.append({"tool": "npm-audit", "id": ids[0] if ids else "", "package": name,
"severity": sev, "scope": scope,
"detail": f"isDirect={adv.get('isDirect')} range={adv.get('range')}"})
+10 -5
View File
@@ -8,6 +8,7 @@ import argparse,csv,io,json,os,pathlib,re,subprocess,sys,tempfile
from collections import deque
sys.path.insert(0,'/app/scripts')
from source_catalog_import import build_alist_source,build_dingtalk_sources,build_pp_live_source,catalog_sql,validate_source
from pp_client import active_client,record_observation,observation,PPError
class RefreshBlocked(RuntimeError):pass
_source_name='';_last_directory=''
@@ -39,12 +40,16 @@ def run_psql(sql):
if result.returncode:raise RuntimeError('catalog activation failed')
def refresh_pp():
sys.path.insert(0,'/sources/pp');from download_scope_a import login,fetch_inventory
emit('connecting',10,'正在登录PP资料下载中心')
try:token=login('/run/source-secrets/pp_credentials.json')
except Exception as error:raise RefreshBlocked('PP登录失败,请在来源链接更新凭据') from error
emit('scanning',35,'正在实时读取PP公开资料清单')
inventory=fetch_inventory(token)
client=None
try:
client=active_client();token=client.login()
emit('scanning',35,'正在实时读取PP公开资料清单')
inventory=client.inventory(token)
record_observation(client.config,observation('valid'))
except PPError as error:
if client is not None:record_observation(client.config,observation(error.state))
raise RefreshBlocked(str(error)+',请在数据源配置检查PP连接') from error
emit('building',75,'正在构建PP八类目录快照',len(inventory.get('items',[])),len(inventory.get('items',[])))
manifest={}
try:
+213
View File
@@ -0,0 +1,213 @@
"""PP read-only API client; the only PP authentication protocol implementation.
API checks and new worker tasks load the same atomically activated configuration.
Secrets enter the check subprocess on stdin, never through argv or stdout.
"""
import contextlib
import datetime
import fcntl
import json
import os
import pathlib
import stat
import sys
import tempfile
import urllib.error
import urllib.parse
import urllib.request
DEFAULT_BASE = "https://api.dobot.cn"
DEFAULT_STATE = "/data/source-auth/pp/settings.json"
MESSAGES = {
"valid": "认证有效 · 目录可读",
"incomplete": "配置未完成,请填写账号和密码",
"auth_failed": "PP 拒绝认证,请检查账号和密码",
"permission_denied": "已认证,但目录权限不足",
"unavailable": "PP 暂时无法检查,请稍后重试",
"invalid_response": "PP 接口地址或返回格式不兼容",
}
class PPError(Exception):
def __init__(self, state):
self.state = state
super().__init__(MESSAGES[state])
def observation(state):
return {"state": state, "message": MESSAGES[state], "checked_at": datetime.datetime.now(datetime.timezone.utc).isoformat()}
def validate_base(raw, allow_http=False):
if not isinstance(raw, str) or len(raw) > 300 or raw.strip() != raw:
raise PPError("invalid_response")
try:
u = urllib.parse.urlsplit(raw)
if (u.scheme not in (("https", "http") if allow_http else ("https",))
or not u.hostname or u.username is not None or u.password is not None
or u.path not in ("", "/") or u.query or u.fragment or "?" in raw or "#" in raw):
raise PPError("invalid_response")
_ = u.port
except ValueError:
raise PPError("invalid_response") from None
return raw.rstrip("/")
def load_config(path=None):
p = pathlib.Path(path or os.environ.get("PP_AUTH_STATE_FILE") or DEFAULT_STATE)
if p.exists():
info = p.lstat()
if not stat.S_ISREG(info.st_mode) or stat.S_IMODE(info.st_mode) & 0o077:
raise PPError("incomplete")
state = json.loads(p.read_text(encoding="utf-8"))
config = state.get("active")
if not isinstance(config, dict):
raise PPError("incomplete")
else:
legacy_path = os.environ.get("PP_LEGACY_CREDENTIALS_FILE")
if not legacy_path:
raise PPError("incomplete")
legacy = pathlib.Path(legacy_path)
try:
config = json.loads(legacy.read_text(encoding="utf-8"))
except (OSError, ValueError):
raise PPError("incomplete") from None
config = {**config, "base_url": config.get("base_url") or DEFAULT_BASE, "id": "legacy"}
validate_base(config.get("base_url"))
if not config.get("username") or not config.get("password"):
raise PPError("incomplete")
return config
class NoRedirect(urllib.request.HTTPRedirectHandler):
def redirect_request(self, req, fp, code, msg, headers, newurl):
return None
class Client:
def __init__(self, config, allow_http=False, timeout=20):
self.config = dict(config)
self.base = validate_base(config.get("base_url"), allow_http)
self.timeout = timeout
self.opener = urllib.request.build_opener(NoRedirect())
def request(self, path, token="", body=None, login=False):
headers = {"Accept": "application/json", "User-Agent": "KnowledgeGovernance/PPReadOnly"}
if token:
headers["Authorization"] = "Bearer " + token
if body is not None:
headers["Content-Type"] = "application/x-www-form-urlencoded"
req = urllib.request.Request(self.base + path, data=body, headers=headers)
try:
with self.opener.open(req, timeout=self.timeout) as response:
raw = response.read((8 << 20) + 1)
if len(raw) > 8 << 20:
raise PPError("invalid_response")
payload = json.loads(raw)
except urllib.error.HTTPError as error:
error.close()
if error.code == 401 or (error.code == 403 and login):
raise PPError("auth_failed") from None
if error.code == 403:
raise PPError("permission_denied") from None
if error.code in (408, 425, 429) or error.code >= 500:
raise PPError("unavailable") from None
raise PPError("invalid_response") from None
except (TimeoutError, OSError):
raise PPError("unavailable") from None
except (ValueError, TypeError):
raise PPError("invalid_response") from None
if not isinstance(payload, dict) or not isinstance(payload.get("code"), int) or isinstance(payload.get("code"), bool):
raise PPError("invalid_response")
if payload["code"] != 0:
code = payload["code"]
if code in (408, 429) or 500 <= code < 600:
raise PPError("unavailable")
if login or code == 401:
raise PPError("auth_failed")
if code == 403:
raise PPError("permission_denied")
raise PPError("invalid_response")
return payload.get("data")
def login(self):
if not self.config.get("username") or not self.config.get("password"):
raise PPError("incomplete")
body = urllib.parse.urlencode({"username": self.config["username"], "password": self.config["password"]}).encode()
data = self.request("/admin/login/token", body=body, login=True)
token = data.get("access_token") if isinstance(data, dict) else None
if not isinstance(token, str) or not token:
raise PPError("invalid_response")
return token
def page(self, token, current=1, size=200):
query = urllib.parse.urlencode({"current": current, "size": size})
data = self.request("/potal/file/back/page?" + query, token=token)
total = data.get("total") if isinstance(data, dict) else None
# PP serializes Java Long counts as canonical decimal strings.
if (isinstance(total, str) and len(total) <= 19 and total.isascii() and total.isdecimal()
and (total == "0" or not total.startswith("0"))):
total = int(total)
if (not isinstance(data, dict) or not isinstance(data.get("list"), list)
or not isinstance(total, int) or isinstance(total, bool)
or total < 0 or not isinstance(data.get("hasNextPage"), bool)):
raise PPError("invalid_response")
return {**data, "total": total}
def check(self):
try:
self.page(self.login(), size=1)
return observation("valid")
except PPError as error:
return observation(error.state)
def inventory(self, token):
items = []
for current in range(1, 10001):
page = self.page(token, current)
items.extend(page["list"])
if not page["hasNextPage"]:
if len(items) != page["total"]:
raise PPError("invalid_response")
return {"reported_total": page["total"], "pages": current, "items": items}
raise PPError("invalid_response")
def record_observation(config, result, path=None):
"""An old task may update only the observation of its own active revision."""
p = pathlib.Path(path or os.environ.get("PP_AUTH_STATE_FILE") or DEFAULT_STATE)
if not p.is_file():
return
with os.fdopen(os.open(str(p) + ".lock", os.O_CREAT | os.O_RDWR, 0o600), "r+", encoding="utf-8") as lock:
fcntl.flock(lock, fcntl.LOCK_EX)
state = json.loads(p.read_text(encoding="utf-8"))
if not state.get("active") or state["active"].get("id") != config.get("id"):
return
old = state.get("observation")
if result.get("state") == "unavailable" and isinstance(old, dict):
result = {**result, "previous": old.get("previous") or old}
state["observation"] = result
fd, temp = tempfile.mkstemp(prefix=".pp-next-", dir=p.parent)
try:
with os.fdopen(fd, "w", encoding="utf-8") as out:
json.dump(state, out, ensure_ascii=False)
out.flush()
os.fsync(out.fileno())
os.replace(temp, p)
directory = os.open(p.parent, os.O_RDONLY)
try:
os.fsync(directory)
finally:
os.close(directory)
finally:
with contextlib.suppress(FileNotFoundError):
os.unlink(temp)
def active_client():
return Client(load_config())
def main():
try:
config = json.load(sys.stdin)
result = Client(config).check()
except PPError as error:
result = observation(error.state)
except Exception:
result = observation("unavailable")
print(json.dumps(result, ensure_ascii=False))
if __name__ == "__main__":
main()
+71
View File
@@ -0,0 +1,71 @@
import json
import contextlib
import hashlib
import io
import os
from unittest.mock import patch
import pathlib
import tempfile
import threading
import unittest
from http.server import BaseHTTPRequestHandler, HTTPServer
import pp_client
class PPClientTests(unittest.TestCase):
def setUp(self):
self.calls=[];self.login_code=0;self.list_status=200;self.items=[];self.total=None
owner=self
class Handler(BaseHTTPRequestHandler):
def log_message(self,*args):pass
def do_POST(self):
owner.calls.append((self.command,self.path));self.rfile.read(int(self.headers.get('Content-Length','0')))
self.send_response(200);self.end_headers();self.wfile.write(json.dumps({'code':owner.login_code,'data':{'access_token':'example-token'} if owner.login_code==0 else {}}).encode())
def do_GET(self):
owner.calls.append((self.command,self.path))
if self.path=='/file.pdf':
content=b'%PDF-1.4\nExample PP download\n';self.send_response(200);self.send_header('Content-Length',str(len(content)));self.end_headers();self.wfile.write(content);return
self.send_response(owner.list_status);self.end_headers();self.wfile.write(json.dumps({'code':0,'data':{'list':owner.items,'total':len(owner.items) if owner.total is None else owner.total,'hasNextPage':False}}).encode())
self.server=HTTPServer(('127.0.0.1',0),Handler);self.thread=threading.Thread(target=self.server.serve_forever,daemon=True);self.thread.start()
self.client=pp_client.Client({'base_url':'http://127.0.0.1:'+str(self.server.server_port),'username':'example','password':'example-password'},allow_http=True)
def tearDown(self):self.server.shutdown();self.server.server_close();self.thread.join()
def test_check_really_logs_in_and_reads_empty_directory(self):
result=self.client.check();self.assertEqual(result['state'],'valid');self.assertEqual([x[0] for x in self.calls],['POST','GET']);self.assertTrue(self.calls[0][1].endswith('/admin/login/token'));self.assertTrue(self.calls[1][1].startswith('/potal/file/back/page?'))
def test_real_vendor_decimal_string_total_is_readable(self):
for count,items in [('0',[]),('1',[{'id':'example'}])]:
with self.subTest(count=count):
self.total=count;self.items=items
self.assertEqual(self.client.check()['state'],'valid')
inventory=self.client.inventory(self.client.login())
self.assertEqual(inventory['reported_total'],int(count));self.assertEqual(inventory['items'],items)
def test_malformed_vendor_counts_are_not_normalized(self):
for value in ['01',' 1','+1','1.0','-1','١','9'*20,True,1.5,-1]:
with self.subTest(value=value):
self.total=value;self.assertEqual(self.client.check()['state'],'invalid_response')
def test_explicit_rejection_never_reads_catalog(self):
self.login_code=401;result=self.client.check();self.assertEqual(result['state'],'auth_failed');self.assertEqual(len(self.calls),1)
def test_permission_failure_and_transient_remain_distinct(self):
self.list_status=403;self.assertEqual(self.client.check()['state'],'permission_denied');self.list_status=429;self.assertEqual(self.client.check()['state'],'unavailable')
def test_latest_saved_active_config_owns_each_new_task(self):
with tempfile.TemporaryDirectory() as root:
p=pathlib.Path(root)/'settings.json';p.write_text(json.dumps({'active':{'id':'one','base_url':'https://one.example.invalid','username':'example','password':'example-password'}}));p.chmod(0o600)
self.assertEqual(pp_client.load_config(str(p))['id'],'one');p.write_text(json.dumps({'active':{'id':'two','base_url':'https://two.example.invalid','username':'example','password':'example-password'}}));self.assertEqual(pp_client.load_config(str(p))['base_url'],'https://two.example.invalid')
def test_refresh_consumer_uses_the_shared_client(self):
import live_catalog_refresh
with patch('live_catalog_refresh.active_client',return_value=self.client),patch('live_catalog_refresh.build_pp_live_source',side_effect=lambda inventory,manifest:inventory),contextlib.redirect_stdout(io.StringIO()):
result=live_catalog_refresh.refresh_pp()
self.assertEqual(result['items'],[]);self.assertEqual([x[0] for x in self.calls],['POST','GET'])
def test_download_consumer_uses_current_portal_and_real_file_bytes(self):
import artifact_fetch
url=self.client.base+'/file.pdf'
self.items=[{'online':1,'potalGroup':{'groupName':'公开'},'fileUrl':url}]
with patch('pp_client.active_client',return_value=self.client),patch('artifact_fetch.pp_find_entry',return_value=None):
path,size,digest,local=artifact_fetch.pp_download({'locator_hash':hashlib.sha256(url.encode()).hexdigest(),'name':'example.pdf'},1024)
try:
self.assertFalse(local);self.assertGreater(size,0);self.assertEqual(hashlib.sha256(pathlib.Path(path).read_bytes()).hexdigest(),digest)
self.assertEqual([x[0] for x in self.calls],['POST','GET','GET'])
finally:os.unlink(path)
def test_missing_state_never_implicitly_reuses_legacy_credentials(self):
fake=json.dumps({'username':'example','password':'synthetic-'+'fixture'})
with patch.dict(os.environ,{'PP_LEGACY_CREDENTIALS_FILE':''}),patch('pathlib.Path.exists',return_value=False),patch('pathlib.Path.read_text',return_value=fake):
with self.assertRaises(pp_client.PPError):pp_client.load_config('/example-missing/settings.json')
if __name__=='__main__':unittest.main()
+58 -2
View File
@@ -14,5 +14,61 @@
" expires_on : YYYY-MM-DD,过期当天即失效 —— 过期不是\"继续有效\",是\"重新走一遍审批\"",
"纪律:例外是记账,不是免检。到期没人处理 ⇒ 门禁红 ⇒ 必须有人做决定。"
],
"exceptions": []
}
"exceptions": [
{
"tool": "npm-audit",
"id": "GHSA-fc5q-39xc-vjpf",
"package": "picomatch",
"severity": "high",
"scope": "dev",
"reason": "picomatch ReDoS(GHSA-fc5q-39xc-vjpf)公告范围为 <=4.0.7,npm 上 4.0.7 已是最新、没有修复版本;该链路只在构建/测试工具里解析本仓自己的 glob 模式(vite/vitest/tinyglobby/fdir),不接受外部输入,运行时不加载,故按仅开发依赖记账。到期前若上游发布修复版本必须升级并删除本条。",
"owner": "姚章浩",
"approved_by": "姚章浩",
"expires_on": "2027-02-15"
},
{
"tool": "npm-audit",
"id": "GHSA-fc5q-39xc-vjpf",
"package": "fdir",
"severity": "high",
"scope": "dev",
"reason": "picomatch ReDoS(GHSA-fc5q-39xc-vjpf)公告范围为 <=4.0.7,npm 上 4.0.7 已是最新、没有修复版本;该链路只在构建/测试工具里解析本仓自己的 glob 模式(vite/vitest/tinyglobby/fdir),不接受外部输入,运行时不加载,故按仅开发依赖记账。到期前若上游发布修复版本必须升级并删除本条。",
"owner": "姚章浩",
"approved_by": "姚章浩",
"expires_on": "2027-02-15"
},
{
"tool": "npm-audit",
"id": "GHSA-fc5q-39xc-vjpf",
"package": "tinyglobby",
"severity": "high",
"scope": "dev",
"reason": "picomatch ReDoS(GHSA-fc5q-39xc-vjpf)公告范围为 <=4.0.7,npm 上 4.0.7 已是最新、没有修复版本;该链路只在构建/测试工具里解析本仓自己的 glob 模式(vite/vitest/tinyglobby/fdir),不接受外部输入,运行时不加载,故按仅开发依赖记账。到期前若上游发布修复版本必须升级并删除本条。",
"owner": "姚章浩",
"approved_by": "姚章浩",
"expires_on": "2027-02-15"
},
{
"tool": "npm-audit",
"id": "GHSA-fc5q-39xc-vjpf",
"package": "vite",
"severity": "high",
"scope": "dev",
"reason": "picomatch ReDoS(GHSA-fc5q-39xc-vjpf)公告范围为 <=4.0.7,npm 上 4.0.7 已是最新、没有修复版本;该链路只在构建/测试工具里解析本仓自己的 glob 模式(vite/vitest/tinyglobby/fdir),不接受外部输入,运行时不加载,故按仅开发依赖记账。到期前若上游发布修复版本必须升级并删除本条。",
"owner": "姚章浩",
"approved_by": "姚章浩",
"expires_on": "2027-02-15"
},
{
"tool": "npm-audit",
"id": "GHSA-fc5q-39xc-vjpf",
"package": "vitest",
"severity": "high",
"scope": "dev",
"reason": "picomatch ReDoS(GHSA-fc5q-39xc-vjpf)公告范围为 <=4.0.7,npm 上 4.0.7 已是最新、没有修复版本;该链路只在构建/测试工具里解析本仓自己的 glob 模式(vite/vitest/tinyglobby/fdir),不接受外部输入,运行时不加载,故按仅开发依赖记账。到期前若上游发布修复版本必须升级并删除本条。",
"owner": "姚章浩",
"approved_by": "姚章浩",
"expires_on": "2027-02-15"
}
]
}
+1 -1
View File
@@ -125,7 +125,7 @@ describe('ModulePage', () => {
expect(html).toContain('P2 · 知识治理总览')
expect(html).toContain('PP历史来源清单已接管')
expect(html).toContain('生产写入:关闭')
expect(html).toContain('以「系统设置 → 服务配置」的实时探测为准')
expect(html).toContain('以「系统设置 → 知识库映射」的实时探测为准')
expect(html).not.toContain('本地测试WeKnora已连接')
expect(html).not.toContain('生产WeKnora仍未连接')
expect(html).not.toContain('尚未连接测试或生产WeKnora')
+1 -1
View File
@@ -505,7 +505,7 @@ export function ModulePage({
? '已开启'
: '已开启(快照恢复未验证,仍被阻断)'
: '关闭'}
;测试 / 生产 WeKnora 连接状态以「系统设置 → 服务配置」的实时探测为准。
;测试 / 生产 WeKnora 连接状态以「系统设置 → 知识库映射」的实时探测为准。
</span>
</div>
<span className="safe-badge">Release Worker 默认关闭</span>
+167
View File
@@ -0,0 +1,167 @@
// @vitest-environment jsdom
import { act } from 'react'
import { createRoot, type Root } from 'react-dom/client'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { DWSAccount } from './DWSAccount'
const id = 'a'.repeat(32)
const session = {
id,
state: 'waiting',
verification_url: 'https://login.dingtalk.com/oauth2/device/verify.htm?user_code=TEST-CODE',
user_code: 'TEST-CODE',
expires_at: '2099-01-01T01:00:00Z',
}
const json = (data: unknown, status = 200) => new Response(JSON.stringify({ data }), { status })
let root: Root
let container: HTMLDivElement
let fetcher: ReturnType<typeof vi.fn<(path: string, init?: RequestInit) => Promise<Response>>>
const flush = async () => {
await act(async () => {
await Promise.resolve()
})
}
const click = async (label: string) => {
const button = Array.from(container.querySelectorAll('button')).find((x) => x.textContent === label)
expect(button, label).toBeTruthy()
await act(async () => button!.click())
}
beforeEach(() => {
Object.assign(globalThis, { IS_REACT_ACT_ENVIRONMENT: true })
vi.useFakeTimers()
container = document.createElement('div')
document.body.append(container)
root = createRoot(container)
fetcher = vi.fn((path: string, init?: RequestInit) => {
if (path.endsWith('credential-status'))
return Promise.resolve(json({ authenticated: false, refresh_token_valid: false }))
if (path === '/api/v1/dws-login') return Promise.resolve(json(session, 202))
if (path.startsWith('/api/v1/dws-login/'))
return Promise.resolve(json({ ...session, state: init?.method === 'DELETE' ? 'cancelled' : 'waiting' }))
if (path.endsWith('/available'))
return Promise.resolve(json({ knowledge_bases: [], drive_spaces: [], fetched_at: '2026-11-17T09:00:00Z' }))
throw new Error(`unexpected ${path}`)
})
vi.stubGlobal('fetch', fetcher)
})
afterEach(async () => {
await act(async () => root.unmount())
container.remove()
vi.useRealTimers()
vi.unstubAllGlobals()
})
const mount = async () => {
await act(async () => root.render(<DWSAccount />))
await flush()
}
describe('DWS device authorization', () => {
it('opens the official challenge and cancellation removes it', async () => {
await mount()
await click('登录授权')
expect(container.querySelector('[role="dialog"]')).not.toBeNull()
expect(container.querySelector('a')?.href).toBe(session.verification_url)
expect(container.textContent).toContain('TEST-CODE')
await click('取消授权')
expect(container.querySelector('[role="dialog"]')).toBeNull()
expect(fetcher.mock.calls.some(([path, init]) => path.endsWith(id) && init?.method === 'DELETE')).toBe(true)
})
it('refreshes identity after login and independently reports failed directory access', async () => {
let authorized = false
fetcher.mockImplementation((path: string, init?: RequestInit) => {
if (path.endsWith('credential-status'))
return Promise.resolve(
json({
authenticated: authorized,
refresh_token_valid: authorized,
corp_name: '测试组织',
user_name: '测试账号',
})
)
if (path === '/api/v1/dws-login') return Promise.resolve(json(session, 202))
if (path.endsWith('/available'))
return Promise.resolve(new Response(JSON.stringify({ error: '目录权限不足' }), { status: 503 }))
if (init?.method === 'DELETE') return Promise.resolve(json({ ...session, state: 'cancelled' }))
authorized = true
return Promise.resolve(json({ ...session, state: 'succeeded', verification_url: '', user_code: '' }))
})
await mount()
await click('登录授权')
await act(async () => vi.advanceTimersByTimeAsync(1500))
await flush()
expect(container.querySelector('[role="dialog"]')).toBeNull()
expect(container.textContent).toContain('DWS 登录成功')
expect(container.textContent).toContain('测试账号 · 测试组织')
expect(container.textContent).toContain('目录连接检查未通过:目录权限不足')
expect(container.textContent).not.toContain('目录连接正常')
})
it('reports partial directory access without claiming full connection success', async () => {
const fallback = fetcher.getMockImplementation()!
fetcher.mockImplementation((path: string, init?: RequestInit) =>
path.endsWith('/available')
? Promise.resolve(
json({
knowledge_bases: [{}],
drive_spaces: [],
knowledge_notice: '个人知识库权限不足',
fetched_at: '2026-11-17T09:00:00Z',
})
)
: fallback(path, init)
)
await mount()
await click('检查连接')
expect(container.textContent).toContain('目录部分可用')
expect(container.textContent).toContain('个人知识库权限不足')
expect(container.textContent).not.toContain('目录连接正常')
expect(fetcher.mock.calls.some(([, init]) => init?.method === 'POST')).toBe(false)
})
it('shows backend startup errors and provides retry', async () => {
const fallback = fetcher.getMockImplementation()!
fetcher.mockImplementation((path: string, init?: RequestInit) =>
path === '/api/v1/dws-login'
? Promise.resolve(new Response(JSON.stringify({ error: '另一位管理员正在授权' }), { status: 409 }))
: fallback(path, init)
)
await mount()
await click('登录授权')
expect(container.textContent).toContain('另一位管理员正在授权')
expect(Array.from(container.querySelectorAll('button')).some((x) => x.textContent === '重试')).toBe(true)
})
it('cancels a session returned after its dialog was closed during startup', async () => {
let resolve!: (response: Response) => void
const fallback = fetcher.getMockImplementation()!
fetcher.mockImplementation((path: string, init?: RequestInit) =>
path === '/api/v1/dws-login'
? new Promise<Response>((done) => {
resolve = done
})
: fallback(path, init)
)
await mount()
await click('登录授权')
await click('取消授权')
await act(async () => resolve(json(session, 202)))
await flush()
expect(container.querySelector('[role="dialog"]')).toBeNull()
expect(fetcher.mock.calls.some(([path, init]) => path.endsWith(id) && init?.method === 'DELETE')).toBe(true)
})
it('does not reopen a cancelled dialog when a late poll succeeds', async () => {
let resolve!: (response: Response) => void
const fallback = fetcher.getMockImplementation()!
fetcher.mockImplementation((path: string, init?: RequestInit) =>
path.endsWith(id) && init?.method === 'GET'
? new Promise<Response>((done) => {
resolve = done
})
: fallback(path, init)
)
await mount()
await click('登录授权')
await act(async () => vi.advanceTimersByTimeAsync(1500))
await click('取消授权')
await act(async () => resolve(json({ ...session, state: 'succeeded' })))
await flush()
expect(container.querySelector('[role="dialog"]')).toBeNull()
expect(container.textContent).not.toContain('DWS 登录成功')
})
})
+12
View File
@@ -0,0 +1,12 @@
import { renderToStaticMarkup } from 'react-dom/server'
import { describe, expect, it } from 'vitest'
import { DingtalkAccountStatus } from './SourceManagement'
describe('DWS account entry', () => {
it('offers device login and a separate connection check beside account status', () => {
const html = renderToStaticMarkup(<DingtalkAccountStatus />)
expect(html).toContain('登录授权')
expect(html).toContain('检查连接')
expect(html).not.toContain('扫码刷新会话')
})
})
+313
View File
@@ -0,0 +1,313 @@
import { useCallback, useEffect, useRef, useState } from 'react'
import {
cancelDWSLogin,
checkDWSConnection,
dwsLoginActive,
fetchDWSCredentials,
fetchDWSLogin,
startDWSLogin,
} from './dwsLogin'
import type { DWSCredentials, DWSLogin } from './dwsLogin'
const messageOf = (cause: unknown) => (cause instanceof Error ? cause.message : '请求失败,请重试')
const moment = (value: string) => new Date(value).toLocaleString('zh-CN', { hour12: false })
export function DWSAccount({
autoStart = false,
onAuthorized,
onCredentials,
onCancel,
}: {
autoStart?: boolean
onAuthorized?: (credentials: DWSCredentials) => void
onCredentials?: (credentials: DWSCredentials) => void
onCancel?: () => void
} = {}) {
const callbacks = useRef({ onAuthorized, onCredentials, onCancel })
callbacks.current = { onAuthorized, onCredentials, onCancel }
const startedAutomatically = useRef(false)
const [credentials, setCredentials] = useState<DWSCredentials | null>(null)
const [loading, setLoading] = useState(true)
const [error, setError] = useState('')
const [notice, setNotice] = useState('')
const [connection, setConnection] = useState<Awaited<ReturnType<typeof checkDWSConnection>> | null>(null)
const [checking, setChecking] = useState(false)
const [session, setSession] = useState<DWSLogin | null>(null)
const [open, setOpen] = useState(false)
const [starting, setStarting] = useState(false)
const [loginError, setLoginError] = useState('')
const id = useRef('')
const generation = useRef(0)
const mounted = useRef(false)
const handled = useRef('')
const action = useRef<AbortController | null>(null)
const checkAction = useRef<AbortController | null>(null)
useEffect(() => {
mounted.current = true
const controller = new AbortController()
void fetchDWSCredentials(controller.signal)
.then((fresh) => {
setCredentials(fresh)
callbacks.current.onCredentials?.(fresh)
})
.catch((cause) => {
if (!controller.signal.aborted) setError(messageOf(cause))
})
.finally(() => {
if (!controller.signal.aborted) setLoading(false)
})
return () => {
mounted.current = false
controller.abort()
action.current?.abort()
checkAction.current?.abort()
generation.current++
if (id.current) void cancelDWSLogin(id.current).catch(() => undefined)
}
}, [])
const check = useCallback(async () => {
checkAction.current?.abort()
const controller = new AbortController()
checkAction.current = controller
setChecking(true)
setConnection(null)
setError('')
try {
const result = await checkDWSConnection(controller.signal)
const fresh = await fetchDWSCredentials(controller.signal)
if (!controller.signal.aborted && mounted.current) {
setConnection(result)
setCredentials(fresh)
}
} catch (cause) {
if (!controller.signal.aborted && mounted.current) setError(`目录连接检查未通过:${messageOf(cause)}`)
} finally {
if (!controller.signal.aborted && mounted.current) setChecking(false)
}
}, [])
const start = async () => {
const current = ++generation.current
action.current?.abort()
checkAction.current?.abort()
setChecking(false)
const controller = new AbortController()
action.current = controller
setOpen(true)
setStarting(true)
setSession(null)
setLoginError('')
setNotice('')
setConnection(null)
setError('')
try {
const next = await startDWSLogin(controller.signal)
if (!mounted.current || current !== generation.current) {
void cancelDWSLogin(next.id).catch(() => undefined)
return
}
id.current = next.id
setSession(next)
} catch (cause) {
if (mounted.current && current === generation.current) setLoginError(messageOf(cause))
} finally {
if (mounted.current && current === generation.current) setStarting(false)
}
}
useEffect(() => {
if (autoStart && !startedAutomatically.current) {
startedAutomatically.current = true
void start()
}
}, [autoStart])
const close = async () => {
const current = generation.current
const sessionID = id.current
setLoginError('')
if (!sessionID && starting) {
// Keep the POST alive so its returned session can be cancelled by ID.
++generation.current
setOpen(false)
setStarting(false)
callbacks.current.onCancel?.()
return
}
if (sessionID && session && dwsLoginActive(session.state)) {
try {
await cancelDWSLogin(sessionID)
} catch (cause) {
if (mounted.current && generation.current === current) setLoginError(`取消未完成:${messageOf(cause)}`)
return
}
}
if (mounted.current && generation.current === current) {
++generation.current
id.current = ''
setOpen(false)
setSession(null)
callbacks.current.onCancel?.()
}
}
useEffect(() => {
if (!session || !dwsLoginActive(session.state) || loginError) return
const controller = new AbortController()
const current = generation.current
const timer = window.setTimeout(() => {
void fetchDWSLogin(session.id, controller.signal)
.then((next) => {
if (!controller.signal.aborted && current === generation.current) setSession(next)
})
.catch((cause) => {
if (!controller.signal.aborted && current === generation.current) setLoginError(messageOf(cause))
})
}, 1500)
return () => {
window.clearTimeout(timer)
controller.abort()
}
}, [session, loginError])
useEffect(() => {
if (session?.state !== 'succeeded' || handled.current === session.id) return
handled.current = session.id
const current = generation.current
id.current = ''
setOpen(false)
setNotice('DWS 登录成功,正在检查目录连接…')
void fetchDWSCredentials()
.then((fresh) => {
if (!mounted.current || current !== generation.current) return
setCredentials(fresh)
setNotice('DWS 登录成功')
callbacks.current.onCredentials?.(fresh)
void check()
callbacks.current.onAuthorized?.(fresh)
})
.catch((cause) => {
if (mounted.current && current === generation.current) setError(`授权成功,状态刷新失败:${messageOf(cause)}`)
})
}, [session, check])
const status = loading
? '读取中…'
: error && !credentials
? '状态读取失败'
: !credentials?.status_known
? '登录状态待核验'
: !credentials.authenticated
? '未登录'
: credentials.refresh_token_valid
? '已登录'
: '凭证需更新'
const busy = starting || Boolean(session && dwsLoginActive(session.state))
const labels: Record<DWSLogin['state'], string> = {
starting: '正在生成授权入口…',
waiting: '请打开钉钉授权页,确认本次登录。完成后此页会自动更新。',
verifying: '已收到授权,正在验证并保存登录状态…',
succeeded: '登录成功',
expired: '授权已过期,请重新发起。',
failed: '授权未完成,请重试。',
cancelled: '授权已取消。',
}
return (
<section className="source-account" aria-label="钉钉账号(DWS)">
<div className="dws-account-heading">
<div>
<strong>钉钉账号(DWS) · {status}</strong>
{credentials?.authenticated ? (
<div>
{[credentials.user_name, credentials.corp_name].filter(Boolean).join(' · ') || '账号信息暂未返回'}
</div>
) : null}
{credentials?.authenticated && credentials.refresh_expires_at ? (
<small>授权有效期至 {moment(credentials.refresh_expires_at)}</small>
) : null}
</div>
<div className="row-actions">
<button onClick={() => void start()} disabled={busy}>
{busy ? '授权进行中…' : credentials?.authenticated ? '重新授权' : '登录授权'}
</button>
<button className="secondary" onClick={() => void check()} disabled={checking || busy}>
{checking ? '检查中…' : '检查连接'}
</button>
</div>
</div>
<small>所有钉钉来源共用此账号。登录后检查知识库与钉盘的实际访问权限。</small>
{notice ? (
<p role="status" className="success-message">
{notice}
</p>
) : null}
{error ? (
<p role="alert" className="error-message">
{error}
</p>
) : null}
{connection ? (
<div role="status">
<p className={connection.notices.length ? 'hint' : 'success-message'}>
{connection.notices.length ? '目录部分可用' : '目录连接正常'}:{connection.message} · 检查于{' '}
{moment(connection.checkedAt)}
</p>
{connection.notices.map((value) => (
<p className="hint" key={value}>
{value}
</p>
))}
</div>
) : null}
{open ? (
<div className="modal-backdrop" role="dialog" aria-modal="true" aria-label="DWS 登录授权">
<div className="modal-card dws-login-card">
<h3>DWS 登录授权</h3>
<p role="status">{starting ? '正在生成授权入口…' : session ? labels[session.state] : '授权入口生成失败'}</p>
{session?.state === 'waiting' && session.verification_url ? (
<>
<a
className="primary-action dws-authorize-link"
href={session.verification_url}
target="_blank"
rel="noopener noreferrer"
referrerPolicy="no-referrer"
>
打开钉钉授权页
</a>
<p>
若授权页要求输入授权码:<code className="dws-user-code">{session.user_code}</code>
</p>
<small>本次授权有效期至 {moment(session.expires_at)}</small>
</>
) : null}
{session?.error ? (
<p role="alert" className="error-message">
{session.error}
</p>
) : null}
{loginError ? (
<p role="alert" className="error-message">
{loginError}
</p>
) : null}
<div className="row-actions">
{session && !dwsLoginActive(session.state) ? (
<button onClick={() => void start()}>重新发起授权</button>
) : null}
{loginError && session && dwsLoginActive(session.state) ? (
<button onClick={() => setLoginError('')}>重试读取状态</button>
) : null}
{!starting && !session ? <button onClick={() => void start()}>重试</button> : null}
<button className="secondary" onClick={() => void close()}>
{busy ? '取消授权' : '关闭'}
</button>
</div>
</div>
</div>
) : null}
</section>
)
}
+9 -2
View File
@@ -8,11 +8,18 @@ describe('DingtalkSourceDiscovery', () => {
const html = renderToStaticMarkup(
<DingtalkSourceDiscovery registeredNames={['ATOM知识库']} onRegistered={vi.fn()} />
)
for (const text of ['dws 导入(钉钉)', '刷新列表', '加入为来源(选中 0)', '已是来源']) {
for (const text of ['添加数据源向导', '类型', '授权', '范围', '校验', '保存', '钉钉知识库', '钉盘']) {
expect(html).toContain(text)
}
// 需求(2026-10-10):数据源配置这里不再做文件格式筛选——格式归目录级标注决定。
for (const removed of ['文件格式筛选', '未勾选的一律不取件', '保存保留格式', '恢复继承上级规则']) {
for (const removed of [
'文件格式筛选',
'未勾选的一律不取件',
'保存保留格式',
'恢复继承上级规则',
'扫码会话',
'弹二维码',
]) {
expect(html).not.toContain(removed)
}
})
+3 -184
View File
@@ -1,21 +1,6 @@
import { useCallback, useEffect, useState } from 'react'
import { DingtalkRequestError, registerDingtalkSpace } from './dingtalkSession'
import { DingtalkSessionControl } from './DingtalkSessionControl'
import {
discoveryCandidates,
fetchAvailableDingtalkSources,
type AvailableDingtalkSources,
type DiscoveryCandidate,
} from './dingtalkSources'
// 「系统设置 → 数据源配置」里的数据源发现面板(管理员):
// ① 列出当前钉钉账号(dws)能看到的钉钉知识库;
// ② 列出钉盘空间(我的文件 / 组织空间);
// ③ 勾选后加入为来源(登记走既有路径,首次目录扫描自动排队);
// ④ 登记需要有效的钉钉扫码会话:会话失效时直接弹二维码,扫完自动重试本次登记。
// 这里不显示文件格式筛选:格式由目录级标注决定,不在这张卡上做。
import { SourceWizard } from './SourceWizard'
// Keep historical callers on the same DWS authorization and recovery path.
export function DingtalkSourceDiscovery({
registeredNames,
onRegistered,
@@ -23,171 +8,5 @@ export function DingtalkSourceDiscovery({
registeredNames: string[]
onRegistered: () => void
}) {
const [list, setList] = useState<AvailableDingtalkSources | null>(null)
const [loading, setLoading] = useState(false)
const [loadError, setLoadError] = useState<string | null>(null)
const [checked, setChecked] = useState<string[]>([])
const [busy, setBusy] = useState(false)
const [message, setMessage] = useState<string | null>(null)
const [failure, setFailure] = useState<string | null>(null)
const [loginRequired, setLoginRequired] = useState(false)
const load = useCallback(async (signal?: AbortSignal) => {
setLoading(true)
setLoadError(null)
try {
const next = await fetchAvailableDingtalkSources(signal)
setList(next)
// 已登记的默认不再勾选:避免重复登记。
setChecked([])
} catch (cause) {
if (cause instanceof DOMException && cause.name === 'AbortError') return
setLoadError(cause instanceof Error ? cause.message : '列举数据源失败')
setList(null)
} finally {
setLoading(false)
}
}, [])
useEffect(() => {
const controller = new AbortController()
void load(controller.signal)
return () => controller.abort()
}, [load])
const candidates = discoveryCandidates(list, registeredNames)
const knowledgeBases = candidates.filter((item) => item.kind === 'knowledge_base')
const driveSpaces = candidates.filter((item) => item.kind === 'drive_space')
const selected = candidates.filter((item) => checked.includes(item.key))
const toggle = (key: string) =>
setChecked((current) => (current.includes(key) ? current.filter((item) => item !== key) : [...current, key]))
const submit = async (retryAfterLogin = false) => {
setBusy(true)
setFailure(null)
setMessage(null)
const added: string[] = []
const failed: string[] = []
let needLogin = false
for (const candidate of selected) {
try {
const result = await registerDingtalkSpace(candidate.url)
added.push(result.name || candidate.name)
} catch (cause) {
// 扫码会话失效:登记本身没问题,直接弹二维码,扫完自动重试剩下的。
if (cause instanceof DingtalkRequestError && cause.code === 'login_required' && !retryAfterLogin) {
needLogin = true
break
}
failed.push(`${candidate.name}:${cause instanceof Error ? cause.message : '登记失败'}`)
}
}
setBusy(false)
if (needLogin) {
setLoginRequired(true)
setFailure('钉钉扫码会话已失效:请扫码登录,登录成功后会自动继续这次登记。')
return
}
if (added.length) {
setMessage(`已加入 ${added.length} 个来源:${added.join('、')}。首次目录扫描已在后台排队。`)
onRegistered()
void load()
}
if (failed.length) setFailure(failed.join(';'))
}
const resumeAfterLogin = () => {
setLoginRequired(false)
setFailure(null)
void submit(true)
}
const renderRow = (candidate: DiscoveryCandidate) => {
const already = candidate.alreadyRegistered
return (
<li key={candidate.key} className={already ? 'discovery-row registered' : 'discovery-row'}>
<label>
<input
type="checkbox"
aria-label={`选择${candidate.name}`}
checked={checked.includes(candidate.key)}
disabled={already || busy}
onChange={() => toggle(candidate.key)}
/>
<span>
<strong>{candidate.name}</strong>
<small>{candidate.detail}</small>
</span>
</label>
{already ? <span className="safe-badge">已是来源</span> : null}
</li>
)
}
return (
<section className="dingtalk-discovery" aria-label="可加入的数据源">
<div className="dingtalk-discovery-heading">
<div>
<strong>dws 导入(钉钉)</strong>
<small>按当前钉钉账号列出能访问的知识库与钉盘空间;勾选后加入为来源,首次目录扫描自动排队。</small>
</div>
<button type="button" className="secondary" onClick={() => void load()} disabled={loading}>
{loading ? '正在列举…' : '刷新列表'}
</button>
</div>
{loadError ? (
<p className="error-message" role="alert">
{loadError}
</p>
) : null}
{list ? (
<div className="dingtalk-discovery-groups">
<div className="discovery-group">
<h4>知识库({knowledgeBases.length})</h4>
{list.knowledge_notice ? (
<small className="empty-inline">知识库列表可能不全:{list.knowledge_notice}</small>
) : null}
{knowledgeBases.length ? (
<ul>{knowledgeBases.map(renderRow)}</ul>
) : (
<small className="empty-inline">当前账号下没有可访问的知识库。</small>
)}
</div>
<div className="discovery-group">
<h4>钉盘({driveSpaces.length})</h4>
{list.drive_notice ? <small className="empty-inline">钉盘这次列不出来:{list.drive_notice}</small> : null}
{!list.drive_notice && !driveSpaces.length ? (
<small className="empty-inline">当前账号下没有可见的钉盘空间。</small>
) : null}
{driveSpaces.length ? <ul>{driveSpaces.map(renderRow)}</ul> : null}
</div>
</div>
) : null}
<div className="dingtalk-discovery-actions">
<button type="button" disabled={!selected.length || busy} onClick={() => void submit()}>
{busy ? '正在加入…' : `加入为来源(选中 ${selected.length})`}
</button>
<small>勾选的知识库会登记为来源连接;已登记的条目标为「已是来源」,不会重复登记。</small>
</div>
{message ? (
<p className="success-message" role="status">
{message}
</p>
) : null}
{failure ? (
<p className="error-message" role="alert">
{failure}
</p>
) : null}
{loginRequired ? (
<DingtalkSessionControl
autoStart
hideTrigger
space={selected[0]?.url}
onSuccess={resumeAfterLogin}
onClose={() => setLoginRequired(false)}
/>
) : null}
</section>
)
return <SourceWizard registeredNames={registeredNames} ppConnected={false} onRegistered={onRegistered} initialOpen />
}
+93
View File
@@ -0,0 +1,93 @@
.pp-connection {
border: 1px solid var(--border, #dce3eb);
border-radius: 10px;
padding: 16px;
margin: 12px 0;
background: var(--surface, #fff);
}
.pp-heading,
.pp-actions,
.pp-password {
display: flex;
align-items: center;
gap: 10px;
flex-wrap: wrap;
}
.pp-heading {
justify-content: space-between;
}
.pp-heading h3 {
margin: 0;
font-size: 15px;
}
.pp-state {
color: var(--text-muted, #66758a);
font-size: 13px;
}
.pp-connection dl {
margin: 12px 0;
}
.pp-connection dl > div {
display: flex;
gap: 12px;
margin: 6px 0;
font-size: 13px;
}
.pp-connection dt {
flex: 0 0 76px;
color: var(--text-muted, #66758a);
}
.pp-connection dd {
margin: 0;
overflow-wrap: anywhere;
}
.pp-connection form {
display: grid;
gap: 12px;
margin-top: 14px;
}
.pp-connection label {
display: grid;
gap: 5px;
font-size: 13px;
}
.pp-connection input {
width: 100%;
max-width: 540px;
box-sizing: border-box;
padding: 9px 11px;
border: 1px solid var(--border, #cdd7e4);
border-radius: 6px;
background: var(--surface, #fff);
color: inherit;
}
.pp-connection button {
padding: 7px 12px;
border: 1px solid var(--border, #cdd7e4);
border-radius: 6px;
background: var(--surface, #fff);
color: inherit;
cursor: pointer;
}
.pp-connection button.primary {
background: #2563eb;
color: #fff;
border-color: #2563eb;
}
.pp-connection button:disabled {
opacity: 0.5;
cursor: default;
}
.pp-connection [role='alert'] {
color: #a93835;
}
.pp-connection [role='status'] {
font-size: 13px;
}
.pp-connection.compact {
margin-top: 0;
}
.pp-test-result {
padding-top: 8px;
border-top: 1px solid var(--border, #dce3eb);
}
+76
View File
@@ -0,0 +1,76 @@
// @vitest-environment jsdom
import { act } from 'react'
import { createRoot, type Root } from 'react-dom/client'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { PPConnection } from './PPConnection'
let root: Root, host: HTMLDivElement
let fetcher: ReturnType<typeof vi.fn<(path: string, init?: RequestInit) => Promise<Response>>>
const active = {
id: 'active-one',
base_url: 'https://api.example.invalid',
username: 'example',
password_configured: true,
}
const view = {
revision: 1,
active,
pending: null,
observation: { state: 'valid', message: '认证有效 · 目录可读', checked_at: '2026-11-17T10:00:00Z' },
pending_observation: null,
}
const json = (data: unknown) => new Response(JSON.stringify({ data }))
const click = async (label: string) => {
const b = [...host.querySelectorAll('button')].find((x) => x.textContent?.trim() === label)
expect(b, label).toBeTruthy()
await act(async () => b!.click())
}
const type = async (label: string, value: string) => {
const input = [...host.querySelectorAll('label')]
.find((x) => x.textContent?.startsWith(label))
?.querySelector('input')
expect(input, label).toBeTruthy()
await act(async () => {
Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, 'value')!.set!.call(input, value)
input!.dispatchEvent(new Event('input', { bubbles: true }))
})
}
beforeEach(() => {
Object.assign(globalThis, { IS_REACT_ACT_ENVIRONMENT: true })
host = document.createElement('div')
document.body.append(host)
root = createRoot(host)
fetcher = vi.fn(async (_p: string, init?: RequestInit) =>
json(init?.method === 'PUT' ? { ...view, revision: 2 } : view)
)
vi.stubGlobal('fetch', fetcher)
})
afterEach(async () => {
await act(async () => root.unmount())
host.remove()
vi.unstubAllGlobals()
})
describe('PP configuration editor', () => {
it('keeps the stored password without sending a masked replacement or launching scans', async () => {
await act(async () => root.render(<PPConnection />))
await click('配置')
expect(host.querySelector('input[type=password]')).toBeNull()
await click('保存并校验')
const writes = fetcher.mock.calls.filter(([, i]) => i?.method === 'PUT')
expect(writes).toHaveLength(1)
const data = JSON.parse(String(writes[0][1]?.body))
expect(data).toEqual({ base_url: active.base_url, username: 'example', revision: 1 })
expect(host.textContent).toContain('已保存并生效')
expect(fetcher.mock.calls.every(([p]) => p.startsWith('/api/v1/pp-connection'))).toBe(true)
})
it('sends a newly entered password only on explicit replacement and clears it after save', async () => {
await act(async () => root.render(<PPConnection />))
await click('配置')
await click('更新密码')
await type('新密码', 'example-replacement')
await click('保存并校验')
const write = fetcher.mock.calls.find(([, i]) => i?.method === 'PUT')
expect(JSON.parse(String(write?.[1]?.body)).password).toBe('example-replacement')
expect(host.querySelector('input[type=password]')).toBeNull()
expect(host.textContent).not.toContain('example-replacement')
})
})
+13
View File
@@ -0,0 +1,13 @@
import { renderToStaticMarkup } from 'react-dom/server'
import { describe, expect, it } from 'vitest'
import { PPConnection } from './PPConnection'
describe('PP connection card', () => {
it('starts with an observation gap and exposes configuration without inventing validity', () => {
const html = renderToStaticMarkup(<PPConnection />)
expect(html).toContain('PP 连接')
expect(html).toContain('读取中')
expect(html).toContain('配置')
expect(html).not.toContain('认证有效')
expect(html).not.toContain('公开只读可达')
})
})
+288
View File
@@ -0,0 +1,288 @@
import { useCallback, useEffect, useRef, useState } from 'react'
import {
readPPConnection,
savePPConnection,
testPPConnection,
type PPDraft,
type PPObservation,
type PPView,
} from './ppConnection'
import './PPConnection.css'
const errorText = (e: unknown) => (e instanceof Error ? e.message : '请求失败,请重试')
const when = (s: string) => new Date(s).toLocaleString('zh-CN', { hour12: false })
export function PPConnection({ compact = false }: { compact?: boolean }) {
const [view, setView] = useState<PPView | null>(null)
const [loading, setLoading] = useState(true)
const [error, setError] = useState('')
const [notice, setNotice] = useState('')
const [editing, setEditing] = useState(false)
const [draft, setDraft] = useState<PPDraft>({ base_url: 'https://api.dobot.cn', username: '', revision: 0 })
const [password, setPassword] = useState('')
const [replacing, setReplacing] = useState(false)
const [busy, setBusy] = useState('')
const [tested, setTested] = useState<PPObservation | null>(null)
const mounted = useRef(false)
const sequence = useRef(0)
const action = useRef<AbortController | null>(null)
const formState = useRef({ editing: false, busy: '' })
formState.current = { editing, busy }
const load = useCallback(async () => {
const current = ++sequence.current
try {
const next = await readPPConnection()
if (mounted.current && sequence.current === current) {
setView(next)
setError('')
}
} catch (e) {
if (mounted.current && sequence.current === current) setError(errorText(e))
} finally {
if (mounted.current && sequence.current === current) setLoading(false)
}
}, [])
useEffect(() => {
mounted.current = true
void load()
const refresh = () => {
if (!formState.current.editing && !formState.current.busy) void load()
}
window.addEventListener('pp-config-updated', refresh)
return () => {
mounted.current = false
sequence.current++
action.current?.abort()
window.removeEventListener('pp-config-updated', refresh)
}
}, [load])
const open = () => {
if (!view) return
const source = view.pending ?? view.active
setDraft({
base_url: source?.base_url ?? 'https://api.dobot.cn',
username: source?.username ?? '',
revision: view.revision,
...(view.pending ? { pending_id: view.pending.id } : {}),
})
setPassword('')
setReplacing(false)
setTested(null)
setNotice('')
setError('')
setEditing(true)
}
const resetTest = () => {
setTested(null)
setNotice('')
}
const run = async (save: boolean) => {
if (!view) return
setBusy(save ? 'saving' : 'testing')
setError('')
setNotice('')
setTested(null)
const controller = new AbortController()
action.current = controller
const current = ++sequence.current
const payload: PPDraft = editing
? { ...draft, ...(password ? { password } : {}) }
: {
base_url: view.active?.base_url ?? 'https://api.dobot.cn',
username: view.active?.username ?? '',
revision: view.revision,
}
try {
if (save) {
const next = await savePPConnection(payload, controller.signal)
if (!mounted.current || sequence.current !== current) return
setView(next)
setPassword('')
setReplacing(false)
setEditing(false)
setNotice(next.pending ? '修改已保存为待验证;原生效配置保留。' : 'PP 配置已保存并生效。')
window.dispatchEvent(new Event('pp-config-updated'))
} else {
const result = await testPPConnection(payload, controller.signal)
if (!mounted.current || sequence.current !== current) return
setTested(result)
const next = await readPPConnection(controller.signal)
if (mounted.current && sequence.current === current) setView(next)
}
} catch (e) {
if (!controller.signal.aborted && mounted.current && sequence.current === current) {
setError(errorText(e))
try {
const next = await readPPConnection(controller.signal)
if (mounted.current && sequence.current === current) setView(next)
} catch {
/* Keep the last observation with the visible error. */
}
}
} finally {
if (mounted.current && sequence.current === current) setBusy('')
}
}
const observation = view?.observation
const status = loading
? '读取中…'
: error && !view
? '状态读取失败'
: !view?.active
? '未配置'
: !observation
? '未检查'
: observation.state === 'valid'
? '认证有效 · 目录可读'
: observation.message
return (
<section className={`pp-connection${compact ? ' compact' : ''}`} aria-label="PP 连接">
<div className="pp-heading">
<h3>PP 连接</h3>
<span className={observation?.state === 'valid' ? 'safe-badge' : 'pp-state'}>{status}</span>
</div>
{!editing && (
<>
<dl>
<div>
<dt>API 地址</dt>
<dd>{view?.active?.base_url || '未配置'}</dd>
</div>
<div>
<dt>账号</dt>
<dd>{view?.active?.username || '未配置'}</dd>
</div>
<div>
<dt>密码</dt>
<dd>{view?.active?.password_configured ? '已配置' : '未配置'}</dd>
</div>
<div>
<dt>最近检查</dt>
<dd>{observation ? `${when(observation.checked_at)} · ${observation.message}` : '尚无检查记录'}</dd>
</div>
</dl>
{observation?.previous && (
<p className="hint">
上次结果(旧):{observation.previous.message} · {when(observation.previous.checked_at)}
</p>
)}
{view?.pending && (
<p className="hint">存在待验证修改;{view.active ? '当前仍使用原生效配置。' : '尚无生效配置。'}</p>
)}
<div className="pp-actions">
<button type="button" disabled={!view || !!busy} onClick={open}>
配置
</button>
<button type="button" disabled={!view || !!busy || !view.active} onClick={() => void run(false)}>
{busy === 'testing' ? '正在检查…' : '测试连接'}
</button>
{error && (
<button type="button" disabled={!!busy} onClick={() => void load()}>
刷新状态
</button>
)}
</div>
</>
)}
{editing && (
<form
onSubmit={(e) => {
e.preventDefault()
void run(true)
}}
>
<label>
API 地址
<input
type="url"
value={draft.base_url}
disabled={!!busy}
autoComplete="off"
onChange={(e) => {
setDraft({ ...draft, base_url: e.target.value })
resetTest()
}}
required
/>
</label>
<label>
账号
<input
value={draft.username}
disabled={!!busy}
autoComplete="off"
onChange={(e) => {
setDraft({ ...draft, username: e.target.value })
resetTest()
}}
/>
</label>
<div className="pp-password">
<span>密码:{(view?.pending ?? view?.active)?.password_configured ? '已配置' : '未配置'}</span>
<button
type="button"
disabled={!!busy}
onClick={() => {
setReplacing(!replacing)
setPassword('')
resetTest()
}}
>
{replacing ? '保留已存密码' : '更新密码'}
</button>
</div>
{replacing && (
<label>
新密码
<input
type="password"
autoComplete="new-password"
value={password}
disabled={!!busy}
placeholder="留空保留已存密码"
onChange={(e) => {
setPassword(e.target.value)
resetTest()
}}
/>
</label>
)}
<div className="pp-actions">
<button
type="button"
disabled={!!busy}
onClick={() => {
setEditing(false)
setPassword('')
setTested(null)
setError('')
}}
>
取消修改
</button>
<button type="button" disabled={!!busy} onClick={() => void run(false)}>
{busy === 'testing' ? '正在检查…' : '测试连接'}
</button>
<button type="submit" className="primary" disabled={!!busy}>
{busy === 'saving' ? '正在校验…' : '保存并校验'}
</button>
</div>
</form>
)}
{tested && (
<p role="status" className="pp-test-result">
{editing ? '未保存配置测试:' : '当前配置检查:'}
{tested.message}({when(tested.checked_at)})
</p>
)}
{notice && <p role="status">{notice}</p>}
{error && (
<p role="alert">
{error}
{view && ';上次读取的配置与检查结果仍保留。'}
</p>
)}
</section>
)
}
export function PPAuthorizationStep() {
return <PPConnection compact />
}
+1 -1
View File
@@ -288,7 +288,7 @@ export function QAAIWorkbench({
{status ? (
<small>
{status.worker_ready ? '执行器在线' : '执行器未在线,任务会持久保留'}
{!ready ? ';请先在系统设置 → 服务配置完成模型核验' : ''}
{!ready ? ';请先在系统设置 → AI 模型连接完成模型核验' : ''}
</small>
) : (
<small>读取后台状态…</small>
+31 -4
View File
@@ -20,12 +20,39 @@
border-bottom: 0;
}
.source-account {
margin: 0 0 14px;
border: 1px solid var(--border, #dce3eb);
border-radius: 10px;
padding: 16px;
margin: 12px 0;
background: var(--surface, #fff);
font-size: 12px;
}
.source-account > button {
padding: 5px 0;
color: var(--ink-mute);
.dws-account-heading {
display: flex;
align-items: flex-start;
justify-content: space-between;
flex-wrap: wrap;
gap: 12px;
margin-bottom: 8px;
}
.dws-account-heading strong {
display: block;
margin-bottom: 6px;
font-size: 14px;
}
.dws-login-card {
width: min(480px, calc(100vw - 40px));
}
.dws-authorize-link {
display: inline-block;
margin: 8px 0;
}
.dws-user-code {
font-size: 18px;
letter-spacing: 2px;
}
.dws-login-card .row-actions {
margin-top: 20px;
}
.source-account-detail {
padding: 12px 16px;
+1 -36
View File
@@ -21,42 +21,7 @@ const choices = [
]
const errorText = (error: unknown) => (error instanceof Error ? error.message : '操作失败,请重试')
export function DingtalkAccountStatus() {
const [status, setStatus] = useState('读取中…')
const [expires, setExpires] = useState('')
useEffect(() => {
const controller = new AbortController()
void fetch('/api/v1/dingtalk-spaces/credential-status', {
headers: { Accept: 'application/json' },
signal: controller.signal,
})
.then(async (response) => {
if (!response.ok) throw new Error('读取失败')
const value = await response.json()
const data = value.data
if (!data || typeof data.authenticated !== 'boolean' || typeof data.refresh_token_valid !== 'boolean')
throw new Error('状态格式错误')
if (controller.signal.aborted) return
setStatus(!data.authenticated ? '未登录' : data.refresh_token_valid ? '已登录' : '凭证需更新')
setExpires(data.authenticated && typeof data.refresh_expires_at === 'string' ? data.refresh_expires_at : '')
})
.catch(() => {
if (!controller.signal.aborted) setStatus('状态读取失败')
})
return () => controller.abort()
}, [])
return (
<div className="source-account" role="status">
<span>DWS 凭证 · {status}</span>
{expires ? (
<small> · 续期凭证有效期至 {new Date(expires).toLocaleString('zh-CN', { hour12: false })}</small>
) : null}
<div>
<small>仅显示本地凭证状态,不检测目录权限或业务接口。</small>
</div>
</div>
)
}
export { DWSAccount as DingtalkAccountStatus } from './DWSAccount'
export function SourceSettingsCard({
connection,
+168
View File
@@ -0,0 +1,168 @@
// @vitest-environment jsdom
import { act } from 'react'
import { createRoot, type Root } from 'react-dom/client'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { SourceWizard } from './SourceWizard'
let root: Root, host: HTMLDivElement
let fetcher: ReturnType<typeof vi.fn<(path: string, init?: RequestInit) => Promise<Response>>>
const reply = (data: unknown, status = 200) => new Response(JSON.stringify({ data }), { status })
const account = {
authenticated: true,
refresh_token_valid: true,
status_known: true,
identity: 'account-a',
corp_name: '示例组织',
user_name: '示例账号',
}
const spaces = {
knowledge_bases: [
{ name: '资料一', space_id: 'space-one', url: 'https://alidocs.dingtalk.com/i/spaces/space-one/overview' },
{ name: '资料二', space_id: 'space-two', url: 'https://alidocs.dingtalk.com/i/spaces/space-two/overview' },
],
drive_spaces: [],
fetched_at: '2026-11-17T10:00:00Z',
}
const click = async (text: string) => {
const b = [...host.querySelectorAll('button')].find(
(x) => x.textContent?.trim() === text || x.textContent?.trim().includes(text)
)
expect(b, text).toBeTruthy()
await act(async () => b!.click())
}
const flush = async () => {
await act(async () => {
await Promise.resolve()
})
}
beforeEach(() => {
Object.assign(globalThis, { IS_REACT_ACT_ENVIRONMENT: true })
vi.useFakeTimers()
host = document.createElement('div')
document.body.append(host)
root = createRoot(host)
fetcher = vi.fn(async (path: string) => {
if (path.endsWith('credential-status')) return reply(account)
if (path.endsWith('/available')) return reply(spaces)
if (path.endsWith('/validate')) return reply({ valid: true, count: 2, checked_at: '2026-11-17T10:00:00Z' })
throw new Error('unexpected ' + path)
})
vi.stubGlobal('fetch', fetcher)
})
afterEach(async () => {
await act(async () => root.unmount())
host.remove()
vi.useRealTimers()
vi.unstubAllGlobals()
})
async function selectTwo() {
await act(async () => root.render(<SourceWizard registeredNames={[]} ppConnected onRegistered={() => {}} />))
await click('+ 添加数据源')
await click('钉钉知识库')
await click('下一步')
await flush()
await click('下一步')
await act(async () => {
for (const box of host.querySelectorAll<HTMLInputElement>('input[type=checkbox]')) box.click()
})
await click('下一步')
await flush()
}
describe('DWS source wizard', () => {
it('uses shared DWS identity and really validates the selected spaces', async () => {
await selectTwo()
expect(host.textContent).not.toContain('扫码会话')
expect(fetcher.mock.calls.some(([p]) => String(p).endsWith('/validate'))).toBe(true)
expect(host.textContent).toContain('校验通过')
})
it('resumes only the unregistered space after inline DWS authorization', async () => {
const fallback = fetcher.getMockImplementation()!
let tries = 0
fetcher.mockImplementation(async (path: string, init?: RequestInit) => {
if (path === '/api/v1/dingtalk-spaces') {
const url = JSON.parse(String(init?.body)).url
if (url.includes('space-two') && ++tries === 1)
return new Response(JSON.stringify({ error: 'dws_login_required', hint: '授权已失效' }), { status: 401 })
return reply(
{
connection_id: url.includes('space-one') ? 'one' : 'two',
name: url.includes('space-one') ? '资料一' : '资料二',
first_scan_queued: true,
},
201
)
}
if (path === '/api/v1/dws-login')
return reply(
{
id: 'a'.repeat(32),
state: 'waiting',
expires_at: '2099-01-01T00:00:00Z',
verification_url: 'https://login.dingtalk.com/oauth2/device/verify.htm?user_code=EXAMPLE',
user_code: 'EXAMPLE',
},
202
)
if (path.startsWith('/api/v1/dws-login/'))
return reply({
id: 'a'.repeat(32),
state: init?.method === 'DELETE' ? 'cancelled' : 'succeeded',
expires_at: '2099-01-01T00:00:00Z',
verification_url: '',
user_code: '',
})
return fallback(path, init)
})
await selectTwo()
await click('保存并首次扫描')
await flush()
expect(host.textContent).toContain('DWS 登录授权')
await act(async () => vi.advanceTimersByTimeAsync(1500))
await flush()
const calls = fetcher.mock.calls.filter(([p]) => p === '/api/v1/dingtalk-spaces')
expect(calls.filter(([, i]) => String((i as RequestInit).body).includes('space-one'))).toHaveLength(1)
const second = calls.filter(([, i]) => String((i as RequestInit).body).includes('space-two'))
expect(second).toHaveLength(2)
expect((second[0][1] as RequestInit).headers).toEqual((second[1][1] as RequestInit).headers)
expect(fetcher.mock.calls.some(([p]) => String(p).includes('/dingtalk-session'))).toBe(false)
})
it('requires a new selection when authorization changes the account', async () => {
const fallback = fetcher.getMockImplementation()!
let authorized = false
fetcher.mockImplementation(async (path: string, init?: RequestInit) => {
if (path.endsWith('credential-status'))
return reply({ ...account, identity: authorized ? 'account-b' : 'account-a' })
if (path === '/api/v1/dingtalk-spaces')
return new Response(JSON.stringify({ error: 'dws_login_required', hint: '授权失效' }), { status: 401 })
if (path === '/api/v1/dws-login')
return reply(
{
id: 'b'.repeat(32),
state: 'waiting',
expires_at: '2099-01-01T00:00:00Z',
verification_url: 'https://login.dingtalk.com/oauth2/device/verify.htm?user_code=EXAMPLE',
user_code: 'EXAMPLE',
},
202
)
if (path.startsWith('/api/v1/dws-login/')) {
authorized = true
return reply({
id: 'b'.repeat(32),
state: 'succeeded',
expires_at: '2099-01-01T00:00:00Z',
verification_url: '',
user_code: '',
})
}
return fallback(path, init)
})
await selectTwo()
await click('保存并首次扫描')
await act(async () => vi.advanceTimersByTimeAsync(1500))
await flush()
expect(host.textContent).toContain('账号或组织已变化,请重新确认来源')
expect(fetcher.mock.calls.filter(([p]) => p === '/api/v1/dingtalk-spaces')).toHaveLength(1)
expect([...host.querySelectorAll<HTMLInputElement>('input[type=checkbox]')].every((x) => !x.checked)).toBe(true)
})
})
+340 -196
View File
@@ -1,147 +1,294 @@
import { useCallback, useEffect, useMemo, useState } from 'react'
import { DingtalkRequestError, registerDingtalkSpace } from './dingtalkSession'
import { DingtalkSessionControl } from './DingtalkSessionControl'
import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
import { DWSAccount } from './DWSAccount'
import { PPAuthorizationStep } from './PPConnection'
import { DingtalkRequestError, registerDingtalkSpace, type RegisteredDingtalkSpace } from './dingtalkSession'
import { fetchDWSCredentials, type DWSCredentials } from './dwsLogin'
import {
discoveryCandidates,
fetchAvailableDingtalkSources,
validateDingtalkSources,
type AvailableDingtalkSources,
type DiscoveryCandidate,
} from './dingtalkSources'
import { newOperationID } from './operationKey'
import './sourceWizard.css'
// 「系统设置 → 数据源配置」的统一添加向导。
// 一个入口、五步固定:类型 → 授权 → 范围 → 校验 → 保存。
// 类型只决定「候选从哪来」与「授权怎么说明」,版式和动作词完全一致。
// 对内/对外不在这里配置:策略只在「来源与目录」标注。
// 单篇钉钉文档/表格不进这里——把链接发给我,我用 dws 导入。
type SourceKind = 'wiki' | 'drive' | 'pp'
const kinds: { key: SourceKind; title: string; detail: string; icon: string }[] = [
{ key: 'wiki', title: '钉钉知识库', detail: '组织 / 我的知识库', icon: '库' },
{ key: 'drive', title: '钉盘', detail: '我的文件 / 组织空间', icon: '盘' },
{ key: 'pp', title: 'Partner Portal', detail: '公开只读资料中心', icon: '↧' },
const kinds = [
{ key: 'wiki' as const, title: '钉钉知识库', detail: '组织 / 我的知识库', icon: '库' },
{ key: 'drive' as const, title: '钉盘', detail: '我的文件 / 组织空间', icon: '盘' },
{ key: 'pp' as const, title: 'Partner Portal', detail: '资料中心 · 只读访问', icon: '↧' },
]
const steps = ['类型', '授权', '范围', '校验', '保存'] as const
const steps = ['类型', '授权', '范围', '校验', '保存']
const text = (e: unknown) => (e instanceof Error ? e.message : '请求失败,请重试')
const needsLogin = (e: unknown) =>
e instanceof DingtalkRequestError && ['dws_login_required', 'login_required'].includes(e.code)
const validAccount = (c: DWSCredentials | null) =>
Boolean(c?.status_known && (c.authenticated || c.refresh_token_valid))
export function SourceWizard({
registeredNames,
ppConnected,
onRegistered,
initialOpen = false,
}: {
registeredNames: string[]
// PP 是一个门户一条来源:已接入时向导只做说明,不重复登记。
ppConnected: boolean
onRegistered: () => void
initialOpen?: boolean
}) {
const [open, setOpen] = useState(false)
const [open, setOpen] = useState(initialOpen)
const [step, setStep] = useState(0)
const [kind, setKind] = useState<SourceKind | null>(null)
const [list, setList] = useState<AvailableDingtalkSources | null>(null)
const [credentials, setCredentials] = useState<DWSCredentials | null>(null)
const [loading, setLoading] = useState(false)
const [loadError, setLoadError] = useState<string | null>(null)
const [loadError, setLoadError] = useState('')
const [checked, setChecked] = useState<string[]>([])
const [busy, setBusy] = useState(false)
const [failure, setFailure] = useState<string | null>(null)
const [message, setMessage] = useState<string | null>(null)
const [loginRequired, setLoginRequired] = useState(false)
const load = useCallback(async (signal?: AbortSignal) => {
setLoading(true)
setLoadError(null)
try {
setList(await fetchAvailableDingtalkSources(signal))
} catch (cause) {
if (cause instanceof DOMException && cause.name === 'AbortError') return
setLoadError(cause instanceof Error ? cause.message : '列举数据源失败')
setList(null)
} finally {
setLoading(false)
const [failure, setFailure] = useState('')
const [message, setMessage] = useState('')
const [authNeeded, setAuthNeeded] = useState(false)
const [autoAuthorize, setAutoAuthorize] = useState(false)
const [verified, setVerified] = useState('')
const alive = useRef(true)
const epoch = useRef(0)
const completed = useRef(new Map<string, RegisteredDingtalkSpace>())
const pending = useRef<DiscoveryCandidate[]>([])
const operationIDs = useRef(new Map<string, string>())
const identity = useRef('')
const submitted = useRef(false)
const recovering = useRef(false)
const callbacks = useRef({ onRegistered })
callbacks.current = { onRegistered }
useEffect(() => {
alive.current = true
return () => {
alive.current = false
epoch.current++
}
}, [])
const load = useCallback(async () => {
const current = ++epoch.current
setLoading(true)
setLoadError('')
try {
const account = await fetchDWSCredentials()
if (!alive.current || current !== epoch.current) return null
setCredentials(account)
if (identity.current && account.identity && identity.current !== account.identity && !submitted.current) {
setChecked([])
setVerified('')
setFailure('DWS 账号或组织已变化,请重新确认来源。')
identity.current = account.identity
}
if (!identity.current && account.identity) identity.current = account.identity
if (!validAccount(account)) {
setList(null)
setAuthNeeded(true)
setLoadError('请先完成 DWS 登录授权')
return null
}
const result = await fetchAvailableDingtalkSources()
if (!alive.current || current !== epoch.current) return null
setList(result)
return result
} catch (e) {
if (alive.current && current === epoch.current) {
setList(null)
setLoadError(text(e))
if (needsLogin(e)) setAuthNeeded(true)
}
return null
} finally {
if (alive.current && current === epoch.current) setLoading(false)
}
}, [])
useEffect(() => {
if (!open || (kind !== 'wiki' && kind !== 'drive')) return
const controller = new AbortController()
void load(controller.signal)
return () => controller.abort()
if (open && (kind === 'wiki' || kind === 'drive')) void load()
return () => {
epoch.current++
}
}, [open, kind, load])
const candidates = useMemo(() => discoveryCandidates(list, registeredNames), [list, registeredNames])
const visible = useMemo(
() =>
candidates.filter((item) =>
kind === 'wiki' ? item.kind === 'knowledge_base' : kind === 'drive' ? item.kind === 'drive_space' : false
),
[candidates, kind]
const visible = candidates.filter((x) =>
kind === 'wiki' ? x.kind === 'knowledge_base' : kind === 'drive' ? x.kind === 'drive_space' : false
)
const selected = visible.filter((item) => checked.includes(item.key))
const selected = visible.filter((x) => checked.includes(x.key) && !x.alreadyRegistered)
const close = () => {
if (busy) return
epoch.current++
setOpen(false)
setStep(0)
setKind(null)
setChecked([])
setFailure(null)
setMessage(null)
setLoginRequired(false)
setFailure('')
setMessage('')
setAuthNeeded(false)
setAutoAuthorize(false)
setVerified('')
identity.current = ''
submitted.current = false
pending.current = []
completed.current.clear()
operationIDs.current.clear()
}
const toggle = (key: string) =>
setChecked((current) => (current.includes(key) ? current.filter((item) => item !== key) : [...current, key]))
const submit = async (retryAfterLogin = false) => {
const report = () => {
const values = [...completed.current.values()]
const queued = values.filter((x) => x.first_scan_queued === true).length
setMessage(
values.length
? `已加入 ${values.length} 个来源:${values.map((x) => x.name).join('、')}。${queued ? `首次目录扫描已入队 ${queued} 项。` : ''}${values.some((x) => x.first_scan_queued === false) ? '部分首次扫描尚未入队,请检查来源状态。' : ''}`
: ''
)
}
const submit = async (items: DiscoveryCandidate[] = selected) => {
if (busy && !recovering.current) return
setBusy(true)
setFailure(null)
const added: string[] = []
const failed: string[] = []
let needLogin = false
for (const candidate of selected) {
setFailure('')
submitted.current = true
pending.current = items.filter((x) => !completed.current.has(x.key))
const remaining = [...pending.current]
let changed = false
for (const candidate of remaining) {
if (!alive.current) break
let operation = operationIDs.current.get(candidate.key)
if (!operation) {
operation = newOperationID()
operationIDs.current.set(candidate.key, operation)
}
try {
const result = await registerDingtalkSpace(candidate.url)
added.push(result.name || candidate.name)
} catch (cause) {
if (cause instanceof DingtalkRequestError && cause.code === 'login_required' && !retryAfterLogin) {
needLogin = true
break
const result = await registerDingtalkSpace(candidate.url, operation, identity.current || undefined)
if (!alive.current) return
completed.current.set(candidate.key, result)
pending.current = pending.current.filter((x) => x.key !== candidate.key)
changed = true
} catch (e) {
if (!alive.current) return
if (needsLogin(e)) {
setAuthNeeded(true)
setAutoAuthorize(!recovering.current)
setFailure('DWS 授权已失效;重新授权后继续剩余来源。')
setBusy(false)
report()
if (changed) callbacks.current.onRegistered()
return
}
failed.push(`${candidate.name}:${cause instanceof Error ? cause.message : '登记失败'}`)
if (e instanceof DingtalkRequestError && e.code === 'dws_identity_changed') {
pending.current = []
submitted.current = false
setChecked([])
setStep(2)
setVerified('')
setFailure('DWS 账号或组织已变化,请重新确认来源。')
void load()
} else setFailure(`${candidate.name}:${text(e)}。已成功项保留,可重试剩余来源。`)
setBusy(false)
report()
if (changed) callbacks.current.onRegistered()
return
}
}
setBusy(false)
if (needLogin) {
setLoginRequired(true)
setFailure('钉钉扫码会话已失效:扫码后会自动继续这次添加。')
if (alive.current) {
setBusy(false)
setAuthNeeded(false)
setAutoAuthorize(false)
submitted.current = false
report()
if (changed) callbacks.current.onRegistered()
}
}
const authorized = async (account: DWSCredentials) => {
if (!alive.current) return
setCredentials(account)
setAuthNeeded(false)
setAutoAuthorize(false)
const changed = Boolean(identity.current && account.identity !== identity.current)
if (!identity.current && account.identity) identity.current = account.identity
if (changed) {
identity.current = account.identity || ''
pending.current = []
submitted.current = false
completed.current.clear()
operationIDs.current.clear()
setChecked([])
setVerified('')
setStep(2)
setFailure('DWS 账号或组织已变化,请重新确认来源。')
await load()
return
}
if (added.length) {
setMessage(`已加入 ${added.length} 个来源:${added.join('、')}。首次目录扫描已在后台排队。`)
onRegistered()
void load()
const result = await load()
if (!result || !alive.current) return
if (submitted.current && pending.current.length) {
if (!identity.current || !account.identity) {
pending.current = []
submitted.current = false
setChecked([])
setStep(2)
setVerified('')
setFailure('无法确认授权前后为同一账号,请重新选择来源。')
return
}
const available = new Set(discoveryCandidates(result, []).map((x) => x.key))
if (pending.current.some((x) => !available.has(x.key))) {
setFailure('重新授权后部分来源已不可见,请重新确认范围。')
setStep(2)
setVerified('')
submitted.current = false
return
}
recovering.current = true
await submit([...pending.current])
recovering.current = false
}
if (failed.length) setFailure(failed.join(';'))
}
const canAdvance =
(step === 0 && kind !== null) ||
(step === 1 && kind !== null) ||
(step === 2 && (kind === 'pp' || selected.length > 0)) ||
(step === 3 && (kind === 'pp' || selected.length > 0))
const verify = async () => {
if (!selected.length) return
const current = epoch.current
setStep(3)
setBusy(true)
setVerified('')
setFailure('')
try {
const result = await validateDingtalkSources(
selected.map((x) => x.url),
identity.current || undefined
)
if (alive.current && current === epoch.current) setVerified(result.checked_at)
} catch (e) {
if (alive.current && current === epoch.current) {
setFailure(text(e))
if (needsLogin(e)) setAuthNeeded(true)
}
} finally {
if (alive.current && current === epoch.current) setBusy(false)
}
}
const authPanel = (
<DWSAccount
autoStart={autoAuthorize}
onCredentials={(c) => {
setCredentials(c)
if (!identity.current && c.identity) identity.current = c.identity
}}
onAuthorized={(c) => void authorized(c)}
onCancel={() => {
setAutoAuthorize(false)
setFailure('授权已取消,选择已保留。')
}}
/>
)
const scopeNotice = kind === 'wiki' ? list?.knowledge_notice : list?.drive_notice
return (
<section className="source-wizard" aria-label="添加数据源">
<div className="source-wizard-bar">
<div>
<h3>数据源</h3>
</div>
<h3>数据源</h3>
<button type="button" className="source-wizard-open" onClick={() => setOpen(true)}>
+ 添加数据源
</button>
</div>
{open ? (
{open && (
<div className="source-wizard-panel" role="dialog" aria-label="添加数据源向导">
<ol className="source-wizard-steps">
{steps.map((label, index) => (
@@ -150,8 +297,7 @@ export function SourceWizard({
</li>
))}
</ol>
{step === 0 ? (
{step === 0 && (
<div className="source-wizard-types">
{kinds.map((item) => (
<button
@@ -161,6 +307,9 @@ export function SourceWizard({
onClick={() => {
setKind(item.key)
setChecked([])
setVerified('')
setFailure('')
setAuthNeeded(false)
}}
>
<span aria-hidden="true">{item.icon}</span>
@@ -169,136 +318,131 @@ export function SourceWizard({
</button>
))}
</div>
) : null}
{step === 1 ? (
<div className="source-wizard-auth">
{kind === 'pp' ? (
<>
<div className="row">
<span>授权方式</span>
<b>公开只读</b>
</div>
<div className="row">
<span>凭据</span>
<span>由服务端持有,无需填写</span>
</div>
<p className="hint">资料下载中心的登录凭据由运维放在服务端,向导里不录入任何密码。</p>
</>
) : (
<>
<div className="row">
<span>授权方式</span>
<b>当前钉钉账号扫码会话</b>
</div>
<div className="row">
<span>读取权限</span>
<span>只读;不写回、不删除源文件</span>
</div>
<p className="hint">
用的是平台已登录的 dws 凭据,不需要每个来源单独扫码;会话失效时会在保存时弹二维码。
</p>
</>
)}
</div>
) : null}
{step === 2 ? (
)}
{step === 1 && (
<div className="source-wizard-auth">{kind === 'pp' ? <PPAuthorizationStep /> : authPanel}</div>
)}
{step === 2 && (
<div className="source-wizard-scope">
{kind === 'pp' ? (
<p className="hint">
{ppConnected
? '已接入 PP 资料下载中心(一个门户一条来源),无需重复添加;刷新与归档在下方清单里做。'
: 'PP 需要服务端放置只读凭据后才能接入;凭据到位后联系我开通,向导本身不录密码。'}
? 'PP 资料下载中心已接入,一个门户保留一条来源。'
: 'PP 来源尚未接入;认证配置与来源登记分别管理。'}
</p>
) : loading ? (
<p className="hint">正在列举当前账号可见的来源…</p>
<p>正在列举当前 DWS 账号可见的来源…</p>
) : loadError ? (
<p role="alert">{loadError}</p>
) : visible.length === 0 ? (
<p className="hint">
当前账号没有可添加的{kind === 'wiki' ? '知识库' : '钉盘空间'};已加入的不会重复列出。
</p>
<>
<p role="alert">{loadError}</p>
<button onClick={() => void load()}>重新读取目录</button>
</>
) : (
<>
<div className="source-wizard-check">
{visible.map((item) => (
<label key={item.key}>
<input type="checkbox" checked={checked.includes(item.key)} onChange={() => toggle(item.key)} />
<span>{item.name}</span>
</label>
))}
</div>
<p className="hint">
已选 {selected.length} 项。勾选空间即可,目录细节加入后再在「来源与目录」里标注。
</p>
{scopeNotice && <p role="alert">{scopeNotice}</p>}
{visible.length ? (
<div className="source-wizard-check">
{visible.map((item) => (
<label key={item.key}>
<input
type="checkbox"
checked={checked.includes(item.key)}
disabled={item.alreadyRegistered || busy}
onChange={() => {
setChecked((current) =>
current.includes(item.key)
? current.filter((x) => x !== item.key)
: [...current, item.key]
)
setVerified('')
}}
/>
<span>
{item.name}
{item.alreadyRegistered ? '(已接入)' : ''}
</span>
</label>
))}
</div>
) : (
<p>当前账号没有可添加的{kind === 'wiki' ? '知识库' : '钉盘空间'}。</p>
)}
<p className="hint">已选 {selected.length} 项;目录规则在加入后的“来源与目录”设置。</p>
</>
)}
</div>
) : null}
{step === 3 ? (
)}
{step === 3 && (
<div className="source-wizard-verify">
<ul>
{(kind === 'pp' ? [] : selected).map((item: DiscoveryCandidate) => (
<li key={item.key}>
<span>{item.name}</span>
<b className="ok">仍可见</b>
</li>
))}
{kind === 'pp' ? (
<li>
<span>Partner Portal 公开目录</span>
<b className="ok">公开只读可达</b>
</li>
) : null}
</ul>
<p className="hint">添加时会按当前账号再读一次;如果已看不到,会拒绝并提示刷新。</p>
{busy ? (
<p>正在校验所选来源…</p>
) : verified ? (
<>
<ul>
{selected.map((item) => (
<li key={item.key}>
<span>{item.name}</span>
<b className="ok">校验通过</b>
</li>
))}
</ul>
<small>检查于 {new Date(verified).toLocaleString('zh-CN', { hour12: false })}</small>
</>
) : (
<button onClick={() => void verify()}>重新校验</button>
)}
</div>
) : null}
{step === 4 ? (
)}
{step === 4 && (
<div className="source-wizard-save">
{message ? <p role="status">{message}</p> : null}
{failure ? <p role="alert">{failure}</p> : null}
{!message && !failure ? (
<p className="hint">
将加入{' '}
{(kind === 'pp' ? ['Partner Portal'] : selected.map((item) => item.name)).join('、') || '(未选择)'}
。
</p>
) : null}
{loginRequired ? (
<DingtalkSessionControl
autoStart
hideTrigger
space={selected[0]?.url}
onSuccess={() => {
setLoginRequired(false)
void submit(true)
}}
/>
) : null}
{message && <p role="status">{message}</p>}
{pending.current.length > 0 && !authNeeded && !busy && (
<button onClick={() => void submit([...pending.current])}>重试剩余来源</button>
)}
</div>
) : null}
)}
{failure && <p role="alert">{failure}</p>}
{authNeeded && step !== 1 && authPanel}
<div className="source-wizard-foot">
<button type="button" className="ghost" onClick={() => (step === 0 ? close() : setStep(step - 1))}>
<button
type="button"
className="ghost"
disabled={busy}
onClick={() => {
setVerified('')
setFailure('')
step === 0 ? close() : setStep(step - 1)
}}
>
{step === 0 ? '取消' : '上一步'}
</button>
{kind === 'pp' ? (
{kind === 'pp' && step === 1 ? (
<button type="button" className="primary" onClick={close}>
完成
</button>
) : step < 3 ? (
<button type="button" className="primary" disabled={!canAdvance} onClick={() => setStep(step + 1)}>
) : step < 2 ? (
<button
type="button"
className="primary"
disabled={busy || kind === null || (step === 1 && !validAccount(credentials))}
onClick={() => setStep(step + 1)}
>
下一步
</button>
) : step === 2 ? (
<button
type="button"
className="primary"
disabled={busy || !selected.length}
onClick={() => void verify()}
>
下一步
</button>
) : step === 3 ? (
<button
type="button"
className="primary"
disabled={busy || !canAdvance}
disabled={busy || !verified}
onClick={() => {
setStep(4)
void submit()
@@ -307,13 +451,13 @@ export function SourceWizard({
{busy ? '正在加入…' : '保存并首次扫描'}
</button>
) : (
<button type="button" className="primary" onClick={close}>
<button type="button" className="primary" disabled={busy} onClick={close}>
完成
</button>
)}
</div>
</div>
) : null}
)}
</section>
)
}
+76 -75
View File
@@ -119,57 +119,40 @@ const authValue = (role: 'admin' | 'engineer') => ({
signedOut: 0,
notifySignedOut: () => {},
})
describe('SystemSettingsView service configuration', () => {
it('lets an administrator edit exactly the four service fields in the web console', () => {
const configured: UISettings = {
...settings,
test_weknora_base_url: 'https://weknora.test.example',
production_weknora_base_url: 'https://weknora.prod.example',
production_tenant_id: '10000',
production_knowledge_base_id: 'kb-abc123',
}
const view = (role: 'admin' | 'engineer') =>
renderToStaticMarkup(
<AuthContext.Provider value={authValue(role)}>
<SystemSettingsView
sources={sources}
connections={connections}
monitor={monitor}
settings={configured}
productionEnabled={false}
routingCapabilities={{ internal_enabled: true, external_enabled: true }}
loading={false}
saving={false}
error={null}
saveMessage={null}
onSave={vi.fn()}
onSourcesChanged={vi.fn()}
onRefresh={vi.fn()}
initialSection="services"
/>
</AuthContext.Provider>
)
describe('SystemSettingsView AI 模型连接', () => {
const view = (role: 'admin' | 'engineer') =>
renderToStaticMarkup(
<AuthContext.Provider value={authValue(role)}>
<SystemSettingsView
sources={sources}
connections={connections}
monitor={monitor}
settings={settings}
productionEnabled={false}
routingCapabilities={{ internal_enabled: true, external_enabled: true }}
loading={false}
saving={false}
error={null}
saveMessage={null}
onSave={vi.fn()}
onSourcesChanged={vi.fn()}
onRefresh={vi.fn()}
initialSection="services"
/>
</AuthContext.Provider>
)
it('keeps the model connection alone in AI 模型连接 and points WeKnora to 知识库映射', () => {
const adminHtml = view('admin')
for (const text of [
'测试地址',
'生产地址',
'生产租户 ID',
'知识库 ID',
'保存并启用服务配置',
'https://weknora.test.example',
'https://weknora.prod.example',
'10000',
'kb-abc123',
'四项留空即表示未配置',
])
for (const text of ['AI 模型连接', '问答评估模型', 'WeKnora 的知识库连接已移到「知识库映射」']) {
expect(adminHtml).toContain(text)
expect(adminHtml).not.toContain('仅管理员可修改')
// 界面只剩这四项:依赖探活矩阵与对内/对外绑定下拉不再出现在服务配置页。
for (const removed of ['MinerU', 'Embedding', 'Redis / MQ', 'PostgreSQL', '测试知识库映射'])
}
// 地址类配置已迁走:这里不再出现 WeKnora 连接表单,也不出现旧的“服务配置”分区名。
for (const removed of ['保存并校验连接', '保存并启用服务配置', '生产租户 ID', '服务配置'])
expect(adminHtml).not.toContain(removed)
const engineerHtml = view('engineer')
expect(engineerHtml).toContain('仅管理员可修改')
expect(engineerHtml).not.toContain('保存并启用服务配置')
expect(engineerHtml).toContain('仅管理员可修改模型连接')
// 工程师看不到模型编辑表单(QAModelSettings 只在管理员下挂载)。
expect(engineerHtml).not.toContain('问答评估模型')
})
})
describe('SystemSettingsView', () => {
@@ -191,7 +174,8 @@ describe('SystemSettingsView', () => {
onRefresh={vi.fn()}
/>
)
for (const text of ['系统设置', '数据源配置', '服务配置', '存储与备份', '刷新策略']) expect(html).toContain(text)
for (const text of ['系统设置', '数据源配置', 'AI 模型连接', '知识库映射', '存储与备份', '刷新策略'])
expect(html).toContain(text)
// 主人 2026-10-26 要求去掉的噪声:页头副标题、刷新状态、权限条、活动版本横幅。
for (const removed of [
'查看内容源、服务与备份状态',
@@ -213,26 +197,41 @@ describe('SystemSettingsView', () => {
expect(html).not.toContain(removed)
// 需求(2026-10-10):新增入口只剩「可加入的来源」勾选,粘贴链接的按钮已下线。
for (const removed of ['+ 新增钉钉知识库', '新增钉钉知识库']) expect(html).not.toContain(removed)
const servicesHtml = renderToStaticMarkup(
<SystemSettingsView
sources={sources}
connections={connections}
monitor={monitor}
settings={settings}
productionEnabled={false}
routingCapabilities={{ internal_enabled: true, external_enabled: true }}
loading={false}
saving={false}
error={null}
saveMessage={null}
onSave={vi.fn()}
onSourcesChanged={vi.fn()}
onRefresh={vi.fn()}
initialSection="services"
/>
// WeKnora 连接已迁到「知识库映射」:地址与核验参数跟入库/封存/生产目标同屏。
const mapHtml = renderToStaticMarkup(
<AuthContext.Provider value={authValue('admin')}>
<SystemSettingsView
sources={sources}
connections={connections}
monitor={monitor}
settings={settings}
productionEnabled={false}
routingCapabilities={{ internal_enabled: true, external_enabled: true }}
loading={false}
saving={false}
error={null}
saveMessage={null}
onSave={vi.fn()}
onSourcesChanged={vi.fn()}
onRefresh={vi.fn()}
initialSection="knowledge-map"
/>
</AuthContext.Provider>
)
for (const text of ['测试 WeKnora', '生产 WeKnora', '测试地址', '生产地址', '生产 ID', '知识库 ID'])
expect(servicesHtml).toContain(text)
for (const text of [
'WeKnora 连接',
'测试 WeKnora',
'生产 WeKnora',
'测试地址',
'生产地址',
'生产租户 ID',
'知识库 ID',
'保存并校验连接',
'生产核验参数',
])
expect(mapHtml).toContain(text)
// 凭据是服务器文件引用:连接卡只报状态,页面不提供 API Key 录入。
expect(mapHtml).not.toContain('API Key')
const storageHtml = renderToStaticMarkup(
<SystemSettingsView
sources={sources}
@@ -291,9 +290,9 @@ describe('SystemSettingsView', () => {
for (const forbidden of ['DATABASE_URL', 'RELEASE_CONFIG_FILE', 'API Key明文', 'secret_ref'])
expect(html).not.toContain(forbidden)
// 两个环境卡仍由真实探测驱动(依赖矩阵搬到总览页,本页不再渲染)。
expect(servicesHtml).toContain('解析队列 对内 已配置 · 对外 已配置')
expect(servicesHtml).not.toContain('PostgreSQL')
expect(servicesHtml).toContain('端点探测:暂无真实探测结果')
expect(mapHtml).toContain('解析队列 对内 已配置 · 对外 已配置')
expect(mapHtml).not.toContain('PostgreSQL')
expect(mapHtml).toContain('端点探测:暂无真实探测结果')
// V1: left-nav section controls, per-source schedule editing, danger modal & history affordances.
expect(html).toContain('settings-nav')
for (const kept of ['更多', '自动刷新中']) expect(html).toContain(kept)
@@ -424,7 +423,7 @@ describe('SystemSettingsView', () => {
onSave={vi.fn()}
onSourcesChanged={vi.fn()}
onRefresh={vi.fn()}
initialSection="services"
initialSection="knowledge-map"
/>
)
const on = view(true, true)
@@ -433,7 +432,7 @@ describe('SystemSettingsView', () => {
expect(on).toContain('延迟 86ms')
expect(on).toContain('已启用 · 可提交生产请求')
expect(on).toContain('生产写入:开启')
expect(on).toContain('发布目标与凭据:管理员受限配置')
expect(on).toContain('凭据:服务器文件引用,页面不回读')
const off = view(false, false)
expect(off).toContain('未启用')
expect(off).toContain('生产写入:关闭')
@@ -457,7 +456,7 @@ describe('SystemSettingsView', () => {
onSave={vi.fn()}
onSourcesChanged={vi.fn()}
onRefresh={vi.fn()}
initialSection="services"
initialSection="knowledge-map"
/>
)
expect(blocked).toContain('已启用 · 被阻断')
@@ -495,7 +494,7 @@ describe('SystemSettingsView', () => {
onSave={vi.fn()}
onSourcesChanged={vi.fn()}
onRefresh={vi.fn()}
initialSection="services"
initialSection="knowledge-map"
/>
)
expect(unconfigured).toContain('端点探测:未配置')
@@ -574,7 +573,9 @@ describe('SystemSettingsView 数据源配置的默认清单', () => {
expect(html).toContain('aria-expanded="false"')
expect(html).toContain('仅手动刷新')
expect(html).toContain('数据源设置审计')
expect((html.match(/DWS 凭证 ·/g) ?? []).length).toBe(1)
expect((html.match(/aria-label="钉钉账号(DWS)"/g) ?? []).length).toBe(1)
expect(html).toContain('登录授权')
expect(html).toContain('检查连接')
for (const removed of ['+ 新增钉钉知识库', '文件格式筛选']) expect(html).not.toContain(removed)
// 文档工程师看不到添加向导与「去掉来源」:两者都是管理员动作。
const engineer = view('engineer')
+41 -269
View File
@@ -8,27 +8,28 @@ import { DingtalkAccountStatus, SourceManagementDetails, SourceSettingsCard } fr
import { RoutingBindingsSection } from './RoutingBindingsSection'
import { ProductionBindingsSection } from './ProductionBindingsSection'
import { SourceWizard } from './SourceWizard'
import { PPConnection } from './PPConnection'
import { useOptionalAuth } from './auth'
import { fetchMonitorOverview } from './monitor'
import type { MonitorOverview } from './monitor'
import { fetchReleaseCapabilities } from './reviewRelease'
import { fetchWorkerCapabilities, testUploadLabel } from './systemCapabilities'
import { fetchWorkerCapabilities } from './systemCapabilities'
import type { WorkerCapabilities } from './systemCapabilities'
import { fetchCatalogRoutingCapabilities, fetchCatalogSources } from './sourceCatalog'
import type { CatalogRoutingCapabilities, CatalogSource } from './sourceCatalog'
import { archiveSourceConnection, fetchSourceConnectionList } from './sourceConnections'
import type { SourceConnectionListItem } from './sourceConnections'
import {
activateUISettings,
createUISettingsCandidate,
fetchActiveUISettings,
validKnowledgeBaseID,
validProductionTenantID,
validServiceEndpoint,
} from './uiSettings'
import { activateUISettings, createUISettingsCandidate, fetchActiveUISettings } from './uiSettings'
import type { UISettings, UISettingsCandidateInput } from './uiSettings'
import { refreshSettingsInput } from './settingsInputs'
import { WeKnoraConnections } from './WeKnoraConnections'
import './systemSettings.css'
// 连接配置已从「服务配置」迁到「知识库映射」。这两个纯函数仍从本模块导出,
// 既有调用与回归测试的导入路径不变。
export { refreshSettingsInput, serviceSettingsInput } from './settingsInputs'
export type { RefreshDrafts, ServiceDrafts } from './settingsInputs'
interface Props {
sources: CatalogSource[]
connections: SourceConnectionListItem[]
@@ -53,7 +54,7 @@ type SectionKey = 'sources' | 'services' | 'knowledge-map' | 'storage' | 'refres
const sections: { key: SectionKey; label: string; adminOnly?: boolean }[] = [
{ key: 'sources', label: '数据源配置' },
{ key: 'services', label: '服务配置' },
{ key: 'services', label: 'AI 模型连接' },
{ key: 'knowledge-map', label: '知识库映射' },
{ key: 'storage', label: '存储与备份' },
{ key: 'refresh', label: '刷新策略' },
@@ -78,96 +79,6 @@ const secondsLabel = (value: number) =>
value < 60 ? `${value}秒` : value < 3600 ? `${value / 60}分钟` : `${value / 3600}小时`
export const visibleSaveMessage = (message: string | null, dirty: boolean) => (dirty ? null : message)
// 保存边界是纯函数:服务分区只取服务草稿,刷新分区只取刷新草稿,
// 另一侧一律用服务器当前值。组件与测试共用同一实现。
export interface ServiceDrafts {
testEndpoint: string
endpoint: string
productionTenantID: string
productionKnowledgeBaseID: string
}
export interface RefreshDrafts {
documentRefresh: 15 | 30 | 60
resourceRefresh: 30 | 60
stalled: 1800 | 3600
finalizing: 7200 | 14400
concurrency: number
}
export const serviceSettingsInput = (settings: UISettings, drafts: ServiceDrafts): UISettingsCandidateInput => ({
document_refresh_seconds: settings.document_refresh_seconds,
resource_refresh_seconds: settings.resource_refresh_seconds,
stalled_after_seconds: settings.stalled_after_seconds,
finalizing_after_seconds: settings.finalizing_after_seconds,
ingestion_concurrency: settings.ingestion_concurrency,
test_weknora_base_url: drafts.testEndpoint.trim(),
production_weknora_base_url: drafts.endpoint.trim(),
production_tenant_id: drafts.productionTenantID.trim(),
production_knowledge_base_id: drafts.productionKnowledgeBaseID.trim(),
})
export const refreshSettingsInput = (settings: UISettings, drafts: RefreshDrafts): UISettingsCandidateInput => ({
document_refresh_seconds: drafts.documentRefresh,
resource_refresh_seconds: drafts.resourceRefresh,
stalled_after_seconds: drafts.stalled,
finalizing_after_seconds: drafts.finalizing,
ingestion_concurrency: drafts.concurrency,
test_weknora_base_url: settings.test_weknora_base_url,
production_weknora_base_url: settings.production_weknora_base_url,
production_tenant_id: settings.production_tenant_id,
production_knowledge_base_id: settings.production_knowledge_base_id,
})
type ServiceHealthView = MonitorOverview['health'][number]
type ServiceView = MonitorOverview['services'][number]
// 服务配置只保留四项可写值(测试地址、生产地址、生产 ID、知识库 ID),
// 依赖与解析队列的状态仍按真实探测呈现,不写死。
const findHealth = (monitor: MonitorOverview | null, name: string): ServiceHealthView | undefined =>
(monitor?.health || []).find((item) => item.name === name || item.name.startsWith(name))
export const healthStateLabel = (health: ServiceHealthView | undefined, service: ServiceView | undefined): string => {
if (!health) {
if (!service) return '待配置'
return service.state === 'healthy' ? '正常' : service.state === 'unknown' ? '未接入' : '需关注'
}
if (health.readiness === 'ready') return '可达 · 可接单'
if (health.readiness === 'not_configured') return '状态未知 · 未配置'
if (health.readiness === 'not_applicable') return '未部署'
return health.liveness === 'up' ? '可达 · 不可接单' : '不可达 · 不可接单'
}
// 测试 WeKnora 卡片只证明“接口就绪”:readiness 不覆盖上传/解析执行器,
// 所以不能沿用“可接单”这种承诺式措辞。
export const testWeKnoraLabel = (health: ServiceHealthView | undefined, service: ServiceView | undefined): string => {
const label = healthStateLabel(health, service)
return label === '可达 · 可接单' ? '接口可达 · 执行器未验证' : label
}
export const healthStateDetail = (health: ServiceHealthView | undefined, service: ServiceView | undefined): string => {
if (!health) return service?.detail || '真实 Provider 未接入'
const success = health.last_success_at
? `最近成功 ${new Date(health.last_success_at).toLocaleTimeString('zh-CN', { hour12: false })}`
: ''
if (health.readiness === 'ready') return [`延迟 ${health.latency_ms}ms`, success].filter(Boolean).join(';')
const failures = health.consecutive_failures ? `连续失败${health.consecutive_failures}次` : ''
return [health.reason, success, failures].filter(Boolean).join(';')
}
// 生产 WeKnora 卡片:端点可达性来自真实探测,写入门禁来自生产开关,两者分开呈现。
// 生产侧只做探活(/health),不做一次真实读写,所以接单能力永远是「未做接单验证」,
// 不跟着别处的「可达 · 可接单」一起含糊过去。
export const productionWeKnoraProbe = (monitor: MonitorOverview | null): string => {
const health = findHealth(monitor, '生产 WeKnora')
if (!health) return '端点探测:暂无真实探测结果'
if (!health.configured) return '端点探测:未配置'
const label =
health.liveness === 'up'
? health.readiness === 'ready'
? '端点可达 · 未做接单验证'
: '端点可达 · 不可接单'
: '端点不可达'
const detail = healthStateDetail(health, undefined)
return `端点探测:${label}${detail ? `(${detail})` : ''}`
}
export function SystemSettingsView({
sources,
connections,
@@ -197,12 +108,6 @@ export function SystemSettingsView({
const [stalled, setStalled] = useState<1800 | 3600>(settings?.stalled_after_seconds ?? 1800)
const [finalizing, setFinalizing] = useState<7200 | 14400>(settings?.finalizing_after_seconds ?? 7200)
const [concurrency, setConcurrency] = useState<number>(settings?.ingestion_concurrency ?? 2)
const [endpoint, setEndpoint] = useState<string>(settings?.production_weknora_base_url ?? '')
const [testEndpoint, setTestEndpoint] = useState<string>(settings?.test_weknora_base_url ?? '')
const [productionTenantID, setProductionTenantID] = useState<string>(settings?.production_tenant_id ?? '')
const [productionKnowledgeBaseID, setProductionKnowledgeBaseID] = useState<string>(
settings?.production_knowledge_base_id ?? ''
)
const [dirty, setDirty] = useState(false)
// 统一入口后,清单里列全部来源(钉钉知识库 / 钉盘 / PP),不再只显示 PP。
@@ -215,28 +120,11 @@ export function SystemSettingsView({
setStalled(settings.stalled_after_seconds)
setFinalizing(settings.finalizing_after_seconds)
setConcurrency(settings.ingestion_concurrency)
setEndpoint(settings.production_weknora_base_url)
setTestEndpoint(settings.test_weknora_base_url)
setProductionTenantID(settings.production_tenant_id)
setProductionKnowledgeBaseID(settings.production_knowledge_base_id)
setDirty(false)
}, [settings])
const canSave = Boolean(settings && !saving)
const isAdmin = identity?.role === 'admin'
const endpointValid = validServiceEndpoint(endpoint)
const testEndpointValid = validServiceEndpoint(testEndpoint)
const productionTenantIDValid = validProductionTenantID(productionTenantID)
const productionKnowledgeBaseIDValid = validKnowledgeBaseID(productionKnowledgeBaseID)
const serviceSettingsValid =
endpointValid && testEndpointValid && productionTenantIDValid && productionKnowledgeBaseIDValid
const serviceSettingsDirty = Boolean(
settings &&
(testEndpoint.trim() !== settings.test_weknora_base_url ||
endpoint.trim() !== settings.production_weknora_base_url ||
productionTenantID.trim() !== settings.production_tenant_id ||
productionKnowledgeBaseID.trim() !== settings.production_knowledge_base_id)
)
const refreshSettingsDirty = Boolean(
settings &&
(documentRefresh !== settings.document_refresh_seconds ||
@@ -245,21 +133,12 @@ export function SystemSettingsView({
finalizing !== settings.finalizing_after_seconds ||
concurrency !== settings.ingestion_concurrency)
)
// 保存边界:服务分区只带上自己这一组的输入,另一组一律用服务器当前值。
// 否则“改了地址没保存、切到刷新策略保存”会把未确认的地址一起写进去。
// 两个分区都有独立草稿;缺 settings 时按钮本就不可点,这里只防一手。
const serviceInput = (): UISettingsCandidateInput =>
settings
? serviceSettingsInput(settings, { testEndpoint, endpoint, productionTenantID, productionKnowledgeBaseID })
: ({} as UISettingsCandidateInput)
// 保存边界:刷新分区只带刷新草稿;WeKnora 连接草稿在 WeKnoraConnections 内独立保存,
// 互不夹带(未保存的地址不会因为保存刷新策略被写进去)。
const refreshInput = (): UISettingsCandidateInput =>
settings
? refreshSettingsInput(settings, { documentRefresh, resourceRefresh, stalled, finalizing, concurrency })
: ({} as UISettingsCandidateInput)
const saveEndpoint = () => {
setDirty(false)
onSave(serviceInput())
}
return (
<main className="page-shell system-settings">
<header className="hero compact page-header">
@@ -297,7 +176,12 @@ export function SystemSettingsView({
onRegistered={onSourcesChanged}
/>
) : null}
{identity?.role === 'admin' ? <DingtalkAccountStatus /> : null}
{identity?.role === 'admin' ? (
<>
<DingtalkAccountStatus />
<PPConnection />
</>
) : null}
<div className="settings-source-group" id="settings-sources-pp">
{listedConnections.length ? (
<div className="settings-source-list">
@@ -320,147 +204,35 @@ export function SystemSettingsView({
</section>
) : null}
{section === 'services' && settings ? (
<section className="settings-panel wide" id="settings-services">
<section className="settings-panel wide" id="settings-ai-model">
<div className="settings-section-heading">
<div>
<h2>服务配置</h2>
<p>
四项可写:测试地址、生产地址、生产租户、生产知识库。保存前会做真实探活与列库核验,凭据只写不读。
</p>
<h2>AI 模型连接</h2>
<p>问答四字段分析与中文问题翻译共用的模型服务;WeKnora 的知识库连接已移到「知识库映射」。</p>
</div>
</div>
{isAdmin ? <QAModelSettings /> : null}
<div className="environment-cards">
<article>
<b>测试 WeKnora</b>
<span>{testWeKnoraLabel(findHealth(monitor, 'WeKnora API'), undefined)}</span>
<small>{`${healthStateDetail(findHealth(monitor, 'WeKnora API'), undefined) || '端点探测:未配置'};解析队列 对内 ${routingCapabilities.internal_enabled ? '已配置' : '未配置'} · 对外 ${routingCapabilities.external_enabled ? '已配置' : '未配置'}`}</small>
<small className={`worker-capability ${workerCapabilities?.test_upload.state ?? 'unknown'}`}>
{testUploadLabel(workerCapabilities)}
{workerCapabilities ? `:${workerCapabilities.test_upload.detail}` : ''}
</small>
</article>
<article className="production">
<b>生产 WeKnora</b>
<span>
{!productionEnabled
? '未启用'
: productionBackupRestoreVerified
? '已启用 · 可提交生产请求'
: '已启用 · 被阻断'}
</span>
<small>{`${productionWeKnoraProbe(monitor)};生产写入:${productionEnabled ? '开启' : '关闭'};发布目标与凭据:管理员受限配置,本页不可回读`}</small>
</article>
</div>
<div className="safe-setting-grid production-endpoint-form">
{isAdmin ? (
<>
<label>
测试地址
<input
value={testEndpoint}
onChange={(event) => {
setTestEndpoint(event.target.value)
setDirty(true)
}}
placeholder="https://weknora.test.example"
aria-invalid={!testEndpointValid}
/>
</label>
<label>
生产地址
<input
value={endpoint}
onChange={(event) => {
setEndpoint(event.target.value)
setDirty(true)
}}
placeholder="https://weknora.prod.example"
aria-invalid={!endpointValid}
/>
</label>
<label>
生产租户 ID
<input
value={productionTenantID}
onChange={(event) => {
setProductionTenantID(event.target.value)
setDirty(true)
}}
placeholder="10000"
aria-invalid={!productionTenantIDValid}
/>
</label>
<label>
知识库 ID
<input
value={productionKnowledgeBaseID}
onChange={(event) => {
setProductionKnowledgeBaseID(event.target.value)
setDirty(true)
}}
placeholder="生产知识库 ID"
aria-invalid={!productionKnowledgeBaseIDValid}
/>
</label>
</>
) : (
<>
<label>
测试地址
<input value={settings.test_weknora_base_url || '未配置'} readOnly disabled />
</label>
<label>
生产地址
<input value={settings.production_weknora_base_url || '未配置'} readOnly disabled />
</label>
<label>
生产 ID
<input value={settings.production_tenant_id || '未配置'} readOnly disabled />
</label>
<label>
知识库 ID
<input value={settings.production_knowledge_base_id || '未配置'} readOnly disabled />
</label>
</>
)}
</div>
{isAdmin ? (
<>
<button
onClick={saveEndpoint}
disabled={!canSave || !serviceSettingsValid || !serviceSettingsDirty}
>
{saving ? '正在验证并启用…' : '保存并启用服务配置'}
</button>
<details className="settings-details">
<summary>保存规则与核验说明</summary>
<small>
四项留空即表示未配置。地址只接受 http(s) 源地址(不能带用户名密码、查询串或 # 片段,
凭据一律走文件引用);生产租户 ID 为十进制数字;知识库 ID 粘贴 URL 不会通过。
保存会生成修订、真实验证后原子启用;端点回答“没有这个知识库”或租户不符会拒收,
端点抖动、鉴权失败或未配置生产凭据只警告不拦。
</small>
</details>
{serviceSettingsValid ? null : (
<p className="error-message" role="alert">
格式不合法:地址需 http:// 或 https:// 开头的纯地址,不能带用户名密码、? 查询串或 # 片段;生产
ID 需为十进制数字;知识库 ID 不能含空格、/、?、# 或 \。
</p>
)}
</>
) : (
<small>
服务配置仅管理员可修改;当前值:测试地址 {settings.test_weknora_base_url || '未配置'} · 生产地址{' '}
{settings.production_weknora_base_url || '未配置'} · 生产 ID{' '}
{settings.production_tenant_id || '未配置'} · 知识库 ID{' '}
{settings.production_knowledge_base_id || '未配置'}。
</small>
)}
{isAdmin ? <QAModelSettings /> : <small>仅管理员可修改模型连接;密钥不回显。</small>}
<p className="settings-hint-wide">
保存只更新这份模型连接;自动分析与自动翻译仍按问答运营里的任务开关执行,不会因为保存连接自动打开。
</p>
</section>
) : null}
{section === 'knowledge-map' ? (
<>
{settings ? (
<WeKnoraConnections
settings={settings}
monitor={monitor}
routingCapabilities={routingCapabilities}
workerCapabilities={workerCapabilities}
productionEnabled={productionEnabled}
productionBackupRestoreVerified={productionBackupRestoreVerified}
isAdmin={Boolean(isAdmin)}
saving={saving}
saveMessage={visibleSaveMessage(saveMessage, dirty)}
onSave={onSave}
/>
) : null}
<p className="settings-hint-wide">
一张图看清三件事:<strong>入库目标</strong>(来源打标分发到此库)、
<strong>封存源</strong>(整库封存实际导出的库)、<strong>生产目标</strong>(同步写入的库)。
@@ -482,7 +254,7 @@ export function SystemSettingsView({
<div className="settings-section-heading">
<div>
<h2>刷新策略</h2>
<p>保存只改这一组参数,不会带上服务配置里未保存的地址。</p>
<p>保存只改这一组参数,不会带上「知识库映射」里未保存的连接地址。</p>
</div>
<span className="safe-badge">当前活动版本</span>
</div>
+130
View File
@@ -0,0 +1,130 @@
// @vitest-environment jsdom
import { act } from 'react'
import { createRoot, type Root } from 'react-dom/client'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { WeKnoraConnections } from './WeKnoraConnections'
import type { UISettings } from './uiSettings'
const settings: UISettings = {
id: 'settings-live',
state: 'active',
document_refresh_seconds: 30,
resource_refresh_seconds: 60,
stalled_after_seconds: 3600,
finalizing_after_seconds: 14400,
ingestion_concurrency: 4,
test_weknora_base_url: 'https://server.test',
production_weknora_base_url: 'https://server.prod',
production_tenant_id: '10000',
production_knowledge_base_id: 'kb-server',
updated_by: 'admin-1',
created_at: '2026-11-18T08:00:00Z',
activated_at: '2026-11-18T08:00:00Z',
retired_at: null,
error_code: '',
}
let root: Root
let host: HTMLDivElement
const click = async (label: string) => {
const button = [...host.querySelectorAll('button')].find((x) => x.textContent?.trim() === label)
expect(button, label).toBeTruthy()
await act(async () => button!.click())
}
const type = async (label: string, value: string) => {
const input = [...host.querySelectorAll('label')]
.find((x) => x.textContent?.startsWith(label))
?.querySelector('input')
expect(input, label).toBeTruthy()
await act(async () => {
Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, 'value')!.set!.call(input, value)
input!.dispatchEvent(new Event('input', { bubbles: true }))
})
}
beforeEach(() => {
Object.assign(globalThis, { IS_REACT_ACT_ENVIRONMENT: true })
host = document.createElement('div')
document.body.append(host)
root = createRoot(host)
})
afterEach(async () => {
await act(async () => root.unmount())
host.remove()
})
describe('WeKnoraConnections', () => {
it('保存只带连接草稿,刷新参数取服务器当前值', async () => {
const onSave = vi.fn()
await act(async () =>
root.render(
<WeKnoraConnections
settings={settings}
monitor={null}
routingCapabilities={{ internal_enabled: true, external_enabled: false }}
productionEnabled={false}
isAdmin
saving={false}
saveMessage={null}
onSave={onSave}
/>
)
)
await type('测试地址', 'https://draft.test')
await click('保存并校验连接')
expect(onSave).toHaveBeenCalledTimes(1)
expect(onSave.mock.calls[0][0]).toMatchObject({
test_weknora_base_url: 'https://draft.test',
production_weknora_base_url: 'https://server.prod',
production_tenant_id: '10000',
production_knowledge_base_id: 'kb-server',
document_refresh_seconds: 30,
ingestion_concurrency: 4,
})
})
it('未修改时保存按钮不可点,非法地址也不允许提交', async () => {
const onSave = vi.fn()
await act(async () =>
root.render(
<WeKnoraConnections
settings={settings}
monitor={null}
routingCapabilities={{ internal_enabled: true, external_enabled: true }}
productionEnabled={false}
isAdmin
saving={false}
saveMessage={null}
onSave={onSave}
/>
)
)
const saveButton = () =>
[...host.querySelectorAll('button')].find((x) => x.textContent?.trim() === '保存并校验连接') as
HTMLButtonElement | undefined
expect(saveButton()?.disabled).toBe(true)
await type('生产地址', 'https://draft.prod/?token=x')
expect(saveButton()?.disabled).toBe(true)
expect(host.textContent).toContain('格式不合法')
await click('保存并校验连接')
expect(onSave).not.toHaveBeenCalled()
})
it('工程师只看到只读摘要,没有保存入口', async () => {
await act(async () =>
root.render(
<WeKnoraConnections
settings={settings}
monitor={null}
routingCapabilities={{ internal_enabled: true, external_enabled: true }}
productionEnabled={false}
isAdmin={false}
saving={false}
saveMessage={null}
onSave={vi.fn()}
/>
)
)
expect(host.textContent).toContain('仅管理员可修改 WeKnora 连接')
expect(host.textContent).toContain('https://server.test')
expect([...host.querySelectorAll('button')]).toHaveLength(0)
})
})
+242
View File
@@ -0,0 +1,242 @@
import { useEffect, useState } from 'react'
import type { MonitorOverview } from './monitor'
import { serviceSettingsInput } from './settingsInputs'
import type { CatalogRoutingCapabilities } from './sourceCatalog'
import type { WorkerCapabilities } from './systemCapabilities'
import { testUploadLabel } from './systemCapabilities'
import { validKnowledgeBaseID, validProductionTenantID, validServiceEndpoint } from './uiSettings'
import type { UISettings, UISettingsCandidateInput } from './uiSettings'
type ServiceHealthView = MonitorOverview['health'][number]
type ServiceView = MonitorOverview['services'][number]
// 连接分区只保留四项可写值(测试地址、生产地址、生产 ID、知识库 ID),
// 依赖与解析队列的状态仍按真实探测呈现,不写死。
export const findHealth = (monitor: MonitorOverview | null, name: string): ServiceHealthView | undefined =>
(monitor?.health || []).find((item) => item.name === name || item.name.startsWith(name))
export const healthStateLabel = (health: ServiceHealthView | undefined, service: ServiceView | undefined): string => {
if (!health) {
if (!service) return '待配置'
return service.state === 'healthy' ? '正常' : service.state === 'unknown' ? '未接入' : '需关注'
}
if (health.readiness === 'ready') return '可达 · 可接单'
if (health.readiness === 'not_configured') return '状态未知 · 未配置'
if (health.readiness === 'not_applicable') return '未部署'
return health.liveness === 'up' ? '可达 · 不可接单' : '不可达 · 不可接单'
}
// 测试 WeKnora 卡片只证明“接口就绪”:readiness 不覆盖上传/解析执行器,
// 所以不能沿用“可接单”这种承诺式措辞。
export const testWeKnoraLabel = (health: ServiceHealthView | undefined, service: ServiceView | undefined): string => {
const label = healthStateLabel(health, service)
return label === '可达 · 可接单' ? '接口可达 · 执行器未验证' : label
}
export const healthStateDetail = (health: ServiceHealthView | undefined, service: ServiceView | undefined): string => {
if (!health) return service?.detail || '真实 Provider 未接入'
const success = health.last_success_at
? `最近成功 ${new Date(health.last_success_at).toLocaleTimeString('zh-CN', { hour12: false })}`
: ''
if (health.readiness === 'ready') return [`延迟 ${health.latency_ms}ms`, success].filter(Boolean).join(';')
const failures = health.consecutive_failures ? `连续失败${health.consecutive_failures}次` : ''
return [health.reason, success, failures].filter(Boolean).join(';')
}
// 生产 WeKnora 卡片:端点可达性来自真实探测,写入门禁来自生产开关,两者分开呈现。
// 生产侧只做探活(/health),不做一次真实读写,所以接单能力永远是「未做接单验证」,
// 不跟着别处的「可达 · 可接单」一起含糊过去。
export const productionWeKnoraProbe = (monitor: MonitorOverview | null): string => {
const health = findHealth(monitor, '生产 WeKnora')
if (!health) return '端点探测:暂无真实探测结果'
if (!health.configured) return '端点探测:未配置'
const label =
health.liveness === 'up'
? health.readiness === 'ready'
? '端点可达 · 未做接单验证'
: '端点可达 · 不可接单'
: '端点不可达'
const detail = healthStateDetail(health, undefined)
return `端点探测:${label}${detail ? `(${detail})` : ''}`
}
export interface WeKnoraConnectionsProps {
settings: UISettings
monitor: MonitorOverview | null
routingCapabilities: CatalogRoutingCapabilities
workerCapabilities?: WorkerCapabilities | null
productionEnabled: boolean
productionBackupRestoreVerified?: boolean
isAdmin: boolean
saving: boolean
saveMessage: string | null
onSave: (input: UISettingsCandidateInput) => void
}
// 「知识库映射 → WeKnora 连接」:地址由管理员在页面维护,保存前后端会真做一次
// 探活与列库核验;凭据仍是服务器文件引用,页面只显示状态、不回读也不接受录入。
export function WeKnoraConnections({
settings,
monitor,
routingCapabilities,
workerCapabilities = null,
productionEnabled,
productionBackupRestoreVerified = false,
isAdmin,
saving,
saveMessage,
onSave,
}: WeKnoraConnectionsProps) {
const [endpoint, setEndpoint] = useState<string>(settings.production_weknora_base_url ?? '')
const [testEndpoint, setTestEndpoint] = useState<string>(settings.test_weknora_base_url ?? '')
const [productionTenantID, setProductionTenantID] = useState<string>(settings.production_tenant_id ?? '')
const [productionKnowledgeBaseID, setProductionKnowledgeBaseID] = useState<string>(
settings.production_knowledge_base_id ?? ''
)
useEffect(() => {
setEndpoint(settings.production_weknora_base_url)
setTestEndpoint(settings.test_weknora_base_url)
setProductionTenantID(settings.production_tenant_id)
setProductionKnowledgeBaseID(settings.production_knowledge_base_id)
}, [settings])
const endpointValid = validServiceEndpoint(endpoint)
const testEndpointValid = validServiceEndpoint(testEndpoint)
const productionTenantIDValid = validProductionTenantID(productionTenantID)
const productionKnowledgeBaseIDValid = validKnowledgeBaseID(productionKnowledgeBaseID)
const valid = endpointValid && testEndpointValid && productionTenantIDValid && productionKnowledgeBaseIDValid
const dirty =
testEndpoint.trim() !== settings.test_weknora_base_url ||
endpoint.trim() !== settings.production_weknora_base_url ||
productionTenantID.trim() !== settings.production_tenant_id ||
productionKnowledgeBaseID.trim() !== settings.production_knowledge_base_id
return (
<section className="settings-panel wide" id="settings-weknora-connections">
<div className="settings-section-heading">
<div>
<h2>WeKnora 连接</h2>
<p>
测试与生产各自的地址在这里维护;保存前后端会真做一次探活与列库核验。凭据由服务器文件引用提供,
页面只显示是否已配置,不回读、也不能在这里录入。
</p>
</div>
</div>
<div className="environment-cards">
<article>
<b>测试 WeKnora</b>
<span>{testWeKnoraLabel(findHealth(monitor, 'WeKnora API'), undefined)}</span>
<small>{`${healthStateDetail(findHealth(monitor, 'WeKnora API'), undefined) || '端点探测:未配置'};解析队列 对内 ${routingCapabilities.internal_enabled ? '已配置' : '未配置'} · 对外 ${routingCapabilities.external_enabled ? '已配置' : '未配置'}`}</small>
<small className={`worker-capability ${workerCapabilities?.test_upload.state ?? 'unknown'}`}>
{testUploadLabel(workerCapabilities)}
{workerCapabilities ? `:${workerCapabilities.test_upload.detail}` : ''}
</small>
</article>
<article className="production">
<b>生产 WeKnora</b>
<span>
{!productionEnabled
? '未启用'
: productionBackupRestoreVerified
? '已启用 · 可提交生产请求'
: '已启用 · 被阻断'}
</span>
<small>{`${productionWeKnoraProbe(monitor)};生产写入:${productionEnabled ? '开启' : '关闭'};凭据:服务器文件引用,页面不回读`}</small>
</article>
</div>
{isAdmin ? (
<>
<div className="safe-setting-grid production-endpoint-form">
<label>
测试地址
<input
value={testEndpoint}
onChange={(event) => setTestEndpoint(event.target.value)}
placeholder="https://weknora.test.example"
aria-invalid={!testEndpointValid}
/>
</label>
<label>
生产地址
<input
value={endpoint}
onChange={(event) => setEndpoint(event.target.value)}
placeholder="https://weknora.prod.example"
aria-invalid={!endpointValid}
/>
</label>
</div>
<details className="settings-details">
<summary>生产核验参数(仅用于核验,不决定发布写入目标)</summary>
<div className="safe-setting-grid">
<label>
生产租户 ID
<input
value={productionTenantID}
onChange={(event) => setProductionTenantID(event.target.value)}
placeholder="10000"
aria-invalid={!productionTenantIDValid}
/>
</label>
<label>
知识库 ID
<input
value={productionKnowledgeBaseID}
onChange={(event) => setProductionKnowledgeBaseID(event.target.value)}
placeholder="生产知识库 ID"
aria-invalid={!productionKnowledgeBaseIDValid}
/>
</label>
</div>
<small>
这两项用于确认生产端确实存在目标库、租户归属正确;整库发布的实际写入目标见下方「生产知识库绑定」,
由部署配置决定,不在本页修改。
</small>
</details>
<button
onClick={() =>
onSave(
serviceSettingsInput(settings, {
testEndpoint,
endpoint,
productionTenantID,
productionKnowledgeBaseID,
})
)
}
disabled={saving || !valid || !dirty}
>
{saving ? '正在验证并启用…' : '保存并校验连接'}
</button>
<details className="settings-details">
<summary>保存规则与核验说明</summary>
<small>
四项留空即表示未配置。地址只接受 http(s) 源地址(不能带用户名密码、查询串或 # 片段,
凭据一律走文件引用);生产租户 ID 为十进制数字;知识库 ID 粘贴 URL 不会通过。
保存会生成修订、真实验证后原子启用;端点回答“没有这个知识库”或租户不符会拒收,
端点抖动、鉴权失败或未配置生产凭据只警告不拦。
</small>
</details>
{valid ? null : (
<p className="error-message" role="alert">
格式不合法:地址需 http:// 或 https:// 开头的纯地址,不能带用户名密码、? 查询串或 # 片段;生产 ID
需为十进制数字;知识库 ID 不能含空格、/、?、# 或 \。
</p>
)}
{saveMessage ? (
<p className="success-message" role="status">
{saveMessage}
</p>
) : null}
</>
) : (
<small>
仅管理员可修改 WeKnora 连接;当前值:测试地址 {settings.test_weknora_base_url || '未配置'} · 生产地址{' '}
{settings.production_weknora_base_url || '未配置'} · 生产 ID {settings.production_tenant_id || '未配置'} ·
知识库 ID {settings.production_knowledge_base_id || '未配置'}。
</small>
)}
</section>
)
}
+18 -4
View File
@@ -65,6 +65,7 @@ export async function cancelDingtalkSession(id: string): Promise<void> {
export interface RegisteredDingtalkSpace {
connection_id: string
name: string
first_scan_queued?: boolean
}
export class DingtalkRequestError extends Error {
@@ -81,14 +82,19 @@ export class DingtalkRequestError extends Error {
// Register a DingTalk knowledge-base space (URL or bare space id) as a new
// source connection. Errors carry the sidecar's machine codes so the UI can
// explain them: login_required, space_not_found, name_conflict.
export async function registerDingtalkSpace(url: string): Promise<RegisteredDingtalkSpace> {
export async function registerDingtalkSpace(
url: string,
operationID = newOperationID(),
identity?: string
): Promise<RegisteredDingtalkSpace> {
const response = await fetch('/api/v1/dingtalk-spaces', {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
'X-Governance-Action': 'dingtalk-space',
'Idempotency-Key': newOperationID(),
'Idempotency-Key': operationID,
...(identity ? { 'X-DWS-Identity': identity } : {}),
},
body: JSON.stringify({ url }),
})
@@ -106,7 +112,11 @@ export async function registerDingtalkSpace(url: string): Promise<RegisteredDing
/* status-only */
}
const friendly: Record<string, string> = {
login_required: '钉钉扫码会话无效或已过期,请先在钉钉来源卡片上「扫码刷新会话」',
login_required: 'DWS 授权已失效,请重新授权',
dws_login_required: 'DWS 授权已失效,请重新授权',
dws_unavailable: 'DWS 目录暂时不可用,请重试',
dws_permission_denied: '当前 DWS 账号缺少目录权限',
dws_identity_changed: 'DWS 账号或组织已变化,请重新确认来源',
space_not_found: '未找到该知识库空间:请确认空间链接正确且当前账号有阅读权限',
name_conflict: '该空间与已登记的来源同名',
// 登记冲突分两种:同名来源还在用 vs 同一个登记键指向了别的空间。原因要说清,
@@ -127,7 +137,11 @@ export async function registerDingtalkSpace(url: string): Promise<RegisteredDing
const data = value.data
if (typeof data.connection_id !== 'string' || typeof data.name !== 'string')
throw new Error('invalid dingtalk space response')
return { connection_id: data.connection_id, name: data.name }
return {
connection_id: data.connection_id,
name: data.name,
...(typeof data.first_scan_queued === 'boolean' ? { first_scan_queued: data.first_scan_queued } : {}),
}
}
function parseSession(value: unknown): DingtalkSessionView {
+1
View File
@@ -7,6 +7,7 @@ const payload = {
knowledge_bases: [
{
name: 'ATOM知识库',
registered: true,
space_id: '9JOGOQneRgkneX4Q',
space_type: 'orgWikiSpace',
url: 'https://alidocs.dingtalk.com/i/spaces/9JOGOQneRgkneX4Q/overview',
+49 -10
View File
@@ -1,7 +1,10 @@
// 「系统设置 → 数据源配置」的数据源发现客户端:列出当前 dws 账号能看到的钉钉知识库与钉盘空间。
// 只读;勾选后的登记仍走既有的 /api/v1/dingtalk-spaces(由扫码会话解析空间根目录)。
import { DingtalkRequestError } from './dingtalkSession'
export interface AvailableKnowledgeBase {
registered?: boolean
name: string
space_id: string
space_type?: string
@@ -10,6 +13,7 @@ export interface AvailableKnowledgeBase {
}
export interface AvailableDriveSpace {
registered?: boolean
name: string
space_id: string
space_type: string
@@ -38,12 +42,17 @@ function isRecord(value: unknown): value is Record<string, unknown> {
export async function fetchAvailableDingtalkSources(signal?: AbortSignal): Promise<AvailableDingtalkSources> {
const response = await fetch('/api/v1/dingtalk-spaces/available', { headers: { Accept: 'application/json' }, signal })
if (!response.ok) {
// 503 是环境问题(dws 未登录/权限不足),501 是后端没接线:都要说清楚"不是没有来源"。
if (response.status === 503)
throw new Error('暂时列不出来:dws 未登录或缺少租户目录读权限。先刷新钉钉会话,或让管理员检查 dws 凭据。')
if (response.status === 501) throw new Error('后端未接线:数据源发现接口不可用。')
if (response.status === 401 || response.status === 403) throw new Error('需要管理员身份才能列举数据源。')
throw new Error(`列举数据源失败(HTTP ${response.status})`)
const body: unknown = await response.json().catch(() => null)
const code = isRecord(body) && typeof body.error === 'string' ? body.error : 'request_failed'
const hint =
isRecord(body) && typeof body.hint === 'string'
? body.hint
: response.status === 501
? '后端未接线:DWS 来源服务不可用。'
: response.status === 401 || response.status === 403
? '需要管理员身份才能列举数据源。'
: `目录暂时列不出来(HTTP ${response.status})`
throw new DingtalkRequestError(hint, code, response.status)
}
const payload = (await response.json()) as unknown
const data = isRecord(payload) && isRecord(payload.data) ? payload.data : {}
@@ -60,6 +69,7 @@ export async function fetchAvailableDingtalkSources(signal?: AbortSignal): Promi
? [
{
name,
registered: item.registered === true,
space_id: spaceID,
space_type: spaceType,
url,
@@ -77,6 +87,7 @@ export async function fetchAvailableDingtalkSources(signal?: AbortSignal): Promi
? [
{
name,
registered: item.registered === true,
space_id: spaceID,
space_type: spaceType,
root_folder_id: typeof item.root_folder_id === 'string' ? item.root_folder_id : '',
@@ -103,9 +114,8 @@ export interface DiscoveryCandidate {
// 避免重复登记。
export function discoveryCandidates(
list: AvailableDingtalkSources | null,
registeredNames: string[]
_registeredNames: string[]
): DiscoveryCandidate[] {
const registered = new Set(registeredNames.map((name) => name.trim()).filter(Boolean))
const knowledgeBases = (list?.knowledge_bases ?? []).map((item): DiscoveryCandidate => ({
key: `kb:${item.space_id}`,
kind: 'knowledge_base',
@@ -113,7 +123,7 @@ export function discoveryCandidates(
spaceID: item.space_id,
url: item.url || driveSpaceURL(item.space_id),
detail: item.space_type === 'myWikiSpace' ? '钉钉知识库 · 我的知识库' : '钉钉知识库 · 组织知识库',
alreadyRegistered: registered.has(item.name.trim()),
alreadyRegistered: item.registered === true,
}))
const driveSpaces = (list?.drive_spaces ?? []).map((item): DiscoveryCandidate => ({
key: `drive:${item.space_id}`,
@@ -122,7 +132,36 @@ export function discoveryCandidates(
spaceID: item.space_id,
url: driveSpaceURL(item.space_id),
detail: item.space_type === 'mySpace' ? '钉盘 · 我的文件' : '钉盘 · 组织空间',
alreadyRegistered: registered.has(item.name.trim()),
alreadyRegistered: item.registered === true,
}))
return [...knowledgeBases, ...driveSpaces]
}
export async function validateDingtalkSources(urls: string[], identity?: string): Promise<{ checked_at: string }> {
const response = await fetch('/api/v1/dingtalk-spaces/validate', {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
'X-Governance-Action': 'dingtalk-space',
...(identity ? { 'X-DWS-Identity': identity } : {}),
},
body: JSON.stringify({ urls }),
})
const body: unknown = await response.json().catch(() => null)
if (!response.ok) {
const code = isRecord(body) && typeof body.error === 'string' ? body.error : 'validation_failed'
const hint = isRecord(body) && typeof body.hint === 'string' ? body.hint : '来源校验失败,请重新读取范围'
throw new DingtalkRequestError(hint, code, response.status)
}
if (
!isRecord(body) ||
!isRecord(body.data) ||
body.data.valid !== true ||
body.data.count !== urls.length ||
typeof body.data.checked_at !== 'string' ||
!Number.isFinite(Date.parse(body.data.checked_at))
)
throw new Error('未取得有效的来源校验结果')
return { checked_at: body.data.checked_at }
}
+74
View File
@@ -0,0 +1,74 @@
import { afterEach, describe, expect, it, vi } from 'vitest'
import { checkDWSConnection, fetchDWSCredentials, startDWSLogin, validDWSAuthorizationURL } from './dwsLogin'
afterEach(() => vi.unstubAllGlobals())
describe('DWS transport boundaries', () => {
it('explains missing directory authorization with the new login entry', async () => {
vi.stubGlobal(
'fetch',
vi
.fn()
.mockResolvedValue(new Response(JSON.stringify({ error: 'dingtalk_discovery_unavailable' }), { status: 503 }))
)
await expect(checkDWSConnection()).rejects.toThrow('请先登录授权')
})
it('accepts only the official device verification URL', () => {
expect(validDWSAuthorizationURL('https://login.dingtalk.com/oauth2/device/verify.htm?user_code=TEST')).toBe(true)
for (const value of [
'javascript:alert(1)',
'http://login.dingtalk.com/oauth2/device/verify.htm',
'https://login.dingtalk.com.evil.invalid/oauth2/device/verify.htm',
'https://login.dingtalk.com/other',
])
expect(validDWSAuthorizationURL(value)).toBe(false)
})
it('keeps backend errors from a real single-use response', async () => {
vi.stubGlobal(
'fetch',
vi.fn().mockResolvedValue(new Response(JSON.stringify({ error: 'DWS 登录服务未接通' }), { status: 503 }))
)
await expect(startDWSLogin()).rejects.toThrow('DWS 登录服务未接通')
})
it('rejects missing or hostile session data', async () => {
vi.stubGlobal(
'fetch',
vi.fn().mockResolvedValue(
new Response(
JSON.stringify({
data: {
id: 'a'.repeat(32),
state: 'waiting',
expires_at: '2099-01-01T00:00:00Z',
verification_url: 'https://evil.invalid/',
},
})
)
)
)
await expect(startDWSLogin()).rejects.toThrow('不是钉钉官方地址')
})
it('does not treat an invalid directory envelope as a successful empty result', async () => {
vi.stubGlobal('fetch', vi.fn().mockResolvedValue(new Response(JSON.stringify({ data: {} }))))
await expect(checkDWSConnection()).rejects.toThrow('未取得有效目录检查结果')
})
it('returns only safe credential metadata', async () => {
vi.stubGlobal(
'fetch',
vi.fn().mockResolvedValue(
new Response(
JSON.stringify({
data: {
authenticated: true,
refresh_token_valid: true,
corp_name: '测试组织',
user_name: '测试账号',
access_token: 'do-not-return',
},
})
)
)
)
const result = await fetchDWSCredentials()
expect(result.corp_name).toBe('测试组织')
expect(JSON.stringify(result)).not.toContain('do-not-return')
})
})
+105
View File
@@ -0,0 +1,105 @@
export interface DWSCredentials {
identity?: string
authenticated: boolean
refresh_token_valid: boolean
status_known: boolean
corp_name: string
user_name: string
refresh_expires_at: string
}
export type DWSLoginState = 'starting' | 'waiting' | 'verifying' | 'succeeded' | 'failed' | 'expired' | 'cancelled'
export interface DWSLogin {
id: string
state: DWSLoginState
verification_url: string
user_code: string
expires_at: string
error: string
}
const states = new Set(['starting', 'waiting', 'verifying', 'succeeded', 'failed', 'expired', 'cancelled'])
export const dwsLoginActive = (state: DWSLoginState) => ['starting', 'waiting', 'verifying'].includes(state)
const record = (value: unknown): value is Record<string, unknown> => typeof value === 'object' && value !== null
const text = (value: unknown) => (typeof value === 'string' ? value : '')
export function validDWSAuthorizationURL(value: string) {
try {
const url = new URL(value)
return (
url.protocol === 'https:' &&
url.host === 'login.dingtalk.com' &&
!url.username &&
!url.password &&
!url.hash &&
url.pathname === '/oauth2/device/verify.htm'
)
} catch {
return false
}
}
async function request(path: string, signal?: AbortSignal, method = 'GET') {
const response = await fetch(path, {
method,
signal,
headers: { Accept: 'application/json', ...(method !== 'GET' ? { 'X-Governance-Action': 'dws-login' } : {}) },
})
const payload: unknown = await response.json().catch(() => null)
if (!response.ok) {
const reason = record(payload) && typeof payload.error === 'string' ? payload.error : ''
if (reason === 'dingtalk_discovery_unavailable')
throw new Error('DWS 未登录、授权已过期或缺少目录权限;请先登录授权,再检查连接。')
throw new Error((record(payload) ? text(payload.hint) : '') || reason || `DWS 请求失败(HTTP ${response.status})`)
}
if (!record(payload) || !record(payload.data)) throw new Error('DWS 返回的数据格式不正确,请重试')
return payload.data
}
export async function fetchDWSCredentials(signal?: AbortSignal): Promise<DWSCredentials> {
const data = await request('/api/v1/dingtalk-spaces/credential-status', signal)
if (typeof data.authenticated !== 'boolean' || typeof data.refresh_token_valid !== 'boolean')
throw new Error('无法判断 DWS 登录状态')
return {
identity: text(data.identity),
authenticated: data.authenticated,
refresh_token_valid: data.refresh_token_valid,
status_known: data.status_known !== false,
corp_name: text(data.corp_name),
user_name: text(data.user_name),
refresh_expires_at: text(data.refresh_expires_at),
}
}
function parseLogin(data: Record<string, unknown>): DWSLogin {
if (
typeof data.id !== 'string' ||
!/^[a-f0-9]{32}$/.test(data.id) ||
typeof data.state !== 'string' ||
!states.has(data.state) ||
!Number.isFinite(Date.parse(text(data.expires_at)))
)
throw new Error('授权会话格式不正确,请重新发起')
const url = text(data.verification_url)
if (url && !validDWSAuthorizationURL(url)) throw new Error('授权链接不是钉钉官方地址,请重新发起')
return {
id: data.id,
state: data.state as DWSLoginState,
verification_url: url,
user_code: text(data.user_code),
expires_at: text(data.expires_at),
error: text(data.error),
}
}
export async function startDWSLogin(signal?: AbortSignal) {
return parseLogin(await request('/api/v1/dws-login', signal, 'POST'))
}
export async function fetchDWSLogin(id: string, signal?: AbortSignal) {
return parseLogin(await request(`/api/v1/dws-login/${encodeURIComponent(id)}`, signal))
}
export async function cancelDWSLogin(id: string) {
return parseLogin(await request(`/api/v1/dws-login/${encodeURIComponent(id)}`, undefined, 'DELETE'))
}
export async function checkDWSConnection(signal?: AbortSignal) {
const data = await request('/api/v1/dingtalk-spaces/available', signal)
if (!('knowledge_bases' in data) || !('drive_spaces' in data) || !text(data.fetched_at))
throw new Error('未取得有效目录检查结果,请重试')
const knowledge = Array.isArray(data.knowledge_bases) ? data.knowledge_bases.length : 0
const drive = Array.isArray(data.drive_spaces) ? data.drive_spaces.length : 0
const notices = [text(data.knowledge_notice), text(data.drive_notice)].filter(Boolean)
return { message: `已读取 ${knowledge} 个知识库、${drive} 个钉盘空间`, notices, checkedAt: text(data.fetched_at) }
}
+45
View File
@@ -0,0 +1,45 @@
import { describe, expect, it, vi, afterEach } from 'vitest'
import { readPPConnection, testPPConnection, savePPConnection } from './ppConnection'
afterEach(() => vi.unstubAllGlobals())
describe('PP connection transport', () => {
it('uses current draft on POST without saving or sending a placeholder password', async () => {
const fetcher = vi.fn(
async () =>
new Response(
JSON.stringify({ data: { state: 'valid', message: '目录可读', checked_at: '2026-11-17T10:00:00Z' } })
)
)
vi.stubGlobal('fetch', fetcher)
const result = await testPPConnection({ base_url: 'https://api.example.invalid', username: 'example', revision: 3 })
expect(result.state).toBe('valid')
const [path, init] = fetcher.mock.calls[0] as unknown as [string, RequestInit]
expect(path).toBe('/api/v1/pp-connection/test')
expect(init.method).toBe('POST')
expect(JSON.parse(String(init.body))).toEqual({
base_url: 'https://api.example.invalid',
username: 'example',
revision: 3,
})
})
it('preserves real response errors and reads metadata only', async () => {
vi.stubGlobal(
'fetch',
vi.fn(async () => new Response(JSON.stringify({ error: '原配置保留' }), { status: 422 }))
)
await expect(
savePPConnection({ base_url: 'https://api.example.invalid', username: 'example', revision: 1 })
).rejects.toThrow('原配置保留')
vi.stubGlobal(
'fetch',
vi.fn(
async () =>
new Response(
JSON.stringify({
data: { revision: 1, active: null, pending: null, observation: null, pending_observation: null },
})
)
)
)
expect((await readPPConnection()).revision).toBe(1)
})
})
+86
View File
@@ -0,0 +1,86 @@
export type PPObservation = { state: string; message: string; checked_at: string; previous?: PPObservation | null }
export type PPMetadata = { id: string; base_url: string; username: string; password_configured: boolean }
export type PPView = {
revision: number
active: PPMetadata | null
pending: PPMetadata | null
observation: PPObservation | null
pending_observation: PPObservation | null
}
export type PPDraft = { base_url: string; username: string; password?: string; revision: number; pending_id?: string }
const record = (v: unknown): v is Record<string, unknown> => typeof v === 'object' && v !== null
const states = new Set(['valid', 'auth_failed', 'permission_denied', 'incomplete', 'unavailable', 'invalid_response'])
async function request(path: string, method: string, draft?: PPDraft, signal?: AbortSignal) {
const response = await fetch(path, {
method,
signal,
cache: 'no-store',
headers: {
Accept: 'application/json',
...(draft ? { 'Content-Type': 'application/json', 'X-Governance-Action': 'pp-connection' } : {}),
},
...(draft ? { body: JSON.stringify(draft) } : {}),
})
const payload: unknown = await response.json().catch(() => null)
if (!response.ok)
throw new Error(
record(payload) && typeof payload.error === 'string' ? payload.error : `PP 请求失败(HTTP ${response.status})`
)
if (!record(payload) || !record(payload.data)) throw new Error('PP 返回的数据格式不正确')
return payload.data
}
function parseObservation(data: unknown): PPObservation | null {
if (data === null || data === undefined) return null
if (
!record(data) ||
!states.has(String(data.state)) ||
typeof data.message !== 'string' ||
typeof data.checked_at !== 'string' ||
!Number.isFinite(Date.parse(data.checked_at))
)
throw new Error('PP 检查状态无法识别')
return {
state: String(data.state),
message: data.message,
checked_at: data.checked_at,
...(record(data.previous) ? { previous: parseObservation({ ...data.previous, previous: null }) } : {}),
}
}
function parseMetadata(data: unknown): PPMetadata | null {
if (data === null) return null
if (
!record(data) ||
typeof data.id !== 'string' ||
typeof data.base_url !== 'string' ||
typeof data.username !== 'string' ||
typeof data.password_configured !== 'boolean'
)
throw new Error('PP 配置元数据不正确')
return {
id: data.id,
base_url: data.base_url,
username: data.username,
password_configured: data.password_configured,
}
}
function parseView(data: Record<string, unknown>): PPView {
if (!Number.isSafeInteger(data.revision) || Number(data.revision) < 0) throw new Error('PP 配置版本不正确')
return {
revision: Number(data.revision),
active: parseMetadata(data.active),
pending: parseMetadata(data.pending),
observation: parseObservation(data.observation),
pending_observation: parseObservation(data.pending_observation),
}
}
export async function readPPConnection(signal?: AbortSignal): Promise<PPView> {
return parseView(await request('/api/v1/pp-connection', 'GET', undefined, signal))
}
export async function testPPConnection(draft: PPDraft, signal?: AbortSignal): Promise<PPObservation> {
const result = parseObservation(await request('/api/v1/pp-connection/test', 'POST', draft, signal))
if (!result) throw new Error('未取得 PP 检查结果')
return result
}
export async function savePPConnection(draft: PPDraft, signal?: AbortSignal): Promise<PPView> {
return parseView(await request('/api/v1/pp-connection', 'PUT', draft, signal))
}
+39
View File
@@ -0,0 +1,39 @@
import type { UISettings, UISettingsCandidateInput } from './uiSettings'
// 保存边界是纯函数:连接分区只取连接草稿,刷新分区只取刷新草稿,
// 另一侧一律用服务器当前值。组件与测试共用同一实现。
export interface ServiceDrafts {
testEndpoint: string
endpoint: string
productionTenantID: string
productionKnowledgeBaseID: string
}
export interface RefreshDrafts {
documentRefresh: 15 | 30 | 60
resourceRefresh: 30 | 60
stalled: 1800 | 3600
finalizing: 7200 | 14400
concurrency: number
}
export const serviceSettingsInput = (settings: UISettings, drafts: ServiceDrafts): UISettingsCandidateInput => ({
document_refresh_seconds: settings.document_refresh_seconds,
resource_refresh_seconds: settings.resource_refresh_seconds,
stalled_after_seconds: settings.stalled_after_seconds,
finalizing_after_seconds: settings.finalizing_after_seconds,
ingestion_concurrency: settings.ingestion_concurrency,
test_weknora_base_url: drafts.testEndpoint.trim(),
production_weknora_base_url: drafts.endpoint.trim(),
production_tenant_id: drafts.productionTenantID.trim(),
production_knowledge_base_id: drafts.productionKnowledgeBaseID.trim(),
})
export const refreshSettingsInput = (settings: UISettings, drafts: RefreshDrafts): UISettingsCandidateInput => ({
document_refresh_seconds: drafts.documentRefresh,
resource_refresh_seconds: drafts.resourceRefresh,
stalled_after_seconds: drafts.stalled,
finalizing_after_seconds: drafts.finalizing,
ingestion_concurrency: drafts.concurrency,
test_weknora_base_url: settings.test_weknora_base_url,
production_weknora_base_url: settings.production_weknora_base_url,
production_tenant_id: settings.production_tenant_id,
production_knowledge_base_id: settings.production_knowledge_base_id,
})
+4
View File
@@ -38,6 +38,10 @@ export async function fetchSourceAudit(offset = 0, signal?: AbortSignal): Promis
return data
}
export const sourceAuditActions: Record<string, string> = {
credential_configured: '更新认证配置',
credential_rejected: '认证配置校验失败',
credential_test: '测试连接',
dws_authorization: '钉钉授权',
created: '添加连接',
settings_updated: '修改配置',
paused: '暂停自动刷新',
+2 -1
View File
@@ -880,7 +880,8 @@ tbody tr:hover {
#release-plan,
#release-execution,
#settings-sources,
#settings-services,
#settings-ai-model,
#settings-weknora-connections,
#settings-storage,
#settings-refresh {
scroll-margin-top: 60px;
+1 -1
View File
@@ -30,7 +30,7 @@ export interface UISettingsCandidateInput {
production_knowledge_base_id: string
}
// 保存服务配置时后端会用生产地址真查一次:找到了才写入,并把知识库名回显;
// 保存连接配置时后端会用生产地址真查一次:找到了才写入,并把知识库名回显;
// 端点明确回答"不存在"会拒绝保存(HTTP 400 + 这句 message),端点抖动只警告不拦。
export interface ServiceEndpointStatus {
status: string