Files
watchman/docs/runbook.md
T
unknownandCursor ab31d828d4 feat(auth): 接入 Casdoor 登录并让前端持有本服务会话
管理台可以密码登录、按角色进入,并把 Watchman 会话 token 交给浏览器。HXOPS 真实跳转仍未联调。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-17 18:39:56 +08:00

119 lines
4.2 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Watchman 本地开发手册(M01)
## 前置
- Python 通过 `uv` 使用 3.12
- Node.js + pnpm 12
- Docker Desktop(RabbitMQ)
- 本机 PostgreSQL(默认);Compose 可选库见下文
## 安装
```powershell
uv sync --group dev
cd web
pnpm install
```
## 本机数据库(默认)
1. 确认 `.env.local` 存在且含密码与 `DATABASE_*_URL`(勿提交)。
2. 建库与角色:
```powershell
uv run python services/watchman/scripts/bootstrap_local_db.py
uv run --directory services/watchman alembic upgrade head
```
## RabbitMQ(Compose,仅本机回环)
```powershell
docker compose -f deploy/compose.dev.yml --env-file .env.local up -d rabbitmq
docker compose -f deploy/compose.dev.yml --env-file .env.local ps
```
管理台:`http://127.0.0.1:15679`(默认 guest/guest,仅开发)。
停止:
```powershell
docker compose -f deploy/compose.dev.yml --env-file .env.local stop rabbitmq
```
## 启动 API / worker / beat(宿主机)
```powershell
uv run python -m watchman api
uv run python -m watchman worker
uv run python -m watchman beat
```
健康检查:`http://127.0.0.1:8000/health`
Windows 上 worker 自动使用 `--pool=solo`。
## 启动控制台壳
```powershell
cd web
pnpm --filter @watchman/console dev
```
打开:`http://127.0.0.1:5173`
## Compose 应用进程(可选 profile)
`.env.local` 需提供 `DATABASE_URL_DOCKER` / `DATABASE_MIGRATOR_URL_DOCKER` / `CELERY_BROKER_URL_DOCKER`。
```powershell
docker compose -f deploy/compose.dev.yml --env-file .env.local --profile app up -d --build
```
可选隔离 Postgres(端口 `127.0.0.1:54329`):
```powershell
docker compose -f deploy/compose.dev.yml --env-file .env.local --profile compose-db up -d postgres
```
## 测试
```powershell
uv run pytest services/watchman/tests -q
cd web
pnpm --filter @watchman/console test:unit
pnpm --filter @watchman/console typecheck
pnpm --filter @watchman/console test:e2e
```
Playwright 截图写入 `docs/evidence/M01/ui/`(相对仓库根,勿再多上一级)。`/_probe/*` 与 `/docs` 仅在 `ENV_NAME=dev|test` 暴露。
`.env.dev` 可选;缺失时加载顺序会跳过该文件。租户 id 放在 `.env.local`(见 D16)。Casdoor 与 HXOPS 入口的地址、应用名和密钥也只放 `.env.local`,键名见 `.env.example`。
## 登录(M02)
管理台先访问 `GET /auth/me`。没有会话时进入登录页。密码登录走 `POST /auth/login`,成功后浏览器只保存 HttpOnly cookie。`GET /auth/me` 返回身份和角色,`POST /auth/logout` 只结束 Watchman 会话。
Casdoor 上的三个角色是 `watchman-admin`、`watchman-operator`、`watchman-viewer`。没有这三个角色时,页面只显示无权限。`POST /platform/audit-events` 和 `POST /platform/run-records` 需要 `watchman-admin`:未登录是 41000,已登录但角色不够是 42000。角色快照超过 5 分钟会按登录名再问 Casdoor;问失败则本次操作返回 503,不沿用旧角色。
`POST /auth/hxops-entry` 接收短时断言,并且必须带 `casdoor_sub`。HXOPS 令牌自己的 `sub` 不能当作 Casdoor 身份。未配置 `HXOPS_SSO_ISSUER`、`HXOPS_SSO_AUDIENCE`、`HXOPS_SSO_PUBLIC_KEY` 或 `CASDOOR_ISSUER` 时返回 503。断言里的角色不会被采用。HXOPS 尚未向 Watchman 签发真实断言,所以这条路径只有本地签名测试,不能写成真实跳转已通过。
本机曾用一个已有 Casdoor 账号走通密码登录、当前用户和退出。这不替代 HXOPS 真实入口验收。密钥和密码不写入仓库。前端 `VITE_AUTH_MODE=fixture` 时,登录页会显示本地身份标识。
## 端口(仅开发,全部绑 127.0.0.1)
| 服务 | 地址 |
|---|---|
| API | 127.0.0.1:8000 |
| Console | 127.0.0.1:5173 |
| Postgres(本机) | 127.0.0.1:5432 |
| Postgres(Compose 可选) | 127.0.0.1:54329 |
| RabbitMQ | 127.0.0.1:56729 |
| RabbitMQ 管理台 | 127.0.0.1:15679 |
## 当前已知限制
- 正式 Casdoor 密码登录已在本机走过一次;HXOPS 真实跳转仍未做。
- 平台写接口需要已登录的 `watchman-admin`。健康检查仍然匿名。
- Compose `app` profile 依赖 Docker 构建;日常开发可用宿主机进程 + 仅 RabbitMQ 容器。
- 容器内 `API_HOST=0.0.0.0` 仅开发监听(D18);宿主端口仍绑 `127.0.0.1`。