# Security Center 本地部署与 E2E（全 Docker）

一键在本地用 Docker 跑起完整栈（MySQL + Redis + Backend，可选 Frontend），用于开发调试与 E2E 验收；测完可一键彻底清理。

> 参考 `scripts/enterprise/`（生产向 EC2 部署）简化而来：本地用 `bypass_sso` 开发会话免 OAuth、开发环境 auto-migrate 建表、SQL 迁移幂等补齐。

## 前置要求

- Docker（compose v2 或 v1）+ 可联网拉取镜像（镜像走 `public.ecr.aws`  mirror）
- `bash` / `curl` / `python3`（E2E 与 mint-token 用，macOS 自带）
- 首次构建 backend 镜像需要下载 Go 依赖；frontend 镜像需 `npm ci`（较慢，默认不构建）

## 快速开始

```bash
cd scripts/local

./local.sh up                    # 启动 MySQL+Redis+Backend（首次构建镜像，约几分钟）
./local.sh up --with-frontend    # 同时构建启动前端（首次 npm ci+build 较慢）

./local.sh status                # 查看状态
./local.sh logs backend          # 看日志
```

启动成功后：

| 入口 | 地址 | 用途 |
|---|---|---|
| Backend API | http://localhost:18080 | E2E / MCP / curl 直连 |
| Frontend | http://localhost:13000 | 浏览器（`--with-frontend` 时；点登录即得开发会话） |
| MySQL | 127.0.0.1:13306 | `./local.sh mysql` 或任意客户端 |
| Redis | 127.0.0.1:16379 | 配额计数 |

## 本地专用配置（仅本地，勿上生产）

- `bypass_sso=true`：`POST /api/v1/auth/google/login` 直接返回 admin 开发 token（`./local.sh token` 一键取）
- 开发环境 `auto-migrate` 自动建表；`local.sh up` 再幂等应用两个索引迁移（`202607270002`/`202607270003`，auto-migrate 覆盖不到的索引；历史迁移不重放）
- Agent 通道全开：`agent.enabled=true`、配额 100/min、`audit.retention_days=90`、生产姿态 `allow_permanent_key=false`
- 所有口令在 `.env`（首次 up 从 `local.env.example` 复制），均为本地弱口令

## 跑 E2E

```bash
./local.sh e2e all       # phase1（Agent 认证）+ phase2（Key 管理）+ phase3（配额/OpenAPI）
./local.sh e2e phase1    # 单独跑
```

- `phase1`：SQL 造/吊销 key + admin token，覆盖 AgentAuth/白名单/审计归因
- `phase2`：`mint-token` 自签两个普通用户（alice/bob）+ 低权限用户，验证 owner 隔离/越权/admin 兜底
- `phase3`：真实 Redis 计数 burst → 429 + Retry-After；agent key 拉 `openapi.json`（35 paths）

手动验证 Agent 通道：

```bash
TOKEN=$(./local.sh token)
# 创建一把只读 key（明文仅此一次）
curl -s -X POST http://localhost:18080/api/v1/agent/api-keys \
  -H "Authorization: Bearer $TOKEN" -H 'Content-Type: application/json' \
  -d '{"agent_name":"local-e2e","permission_scope":["caasm:view_assets"],"expires_at":"2027-01-01T00:00:00Z"}'
# 用 key 调只读接口 / 拉契约
curl -s http://localhost:18080/api/v1/caasm/assets -H "X-API-Key: sk-agent-..."
curl -s http://localhost:18080/api/v1/openapi.json -H "X-API-Key: sk-agent-..." | head -c 200
```

MCP server 对接（可选）：

```bash
cd ../../security-center-mcp && go build -o security-center-mcp .
SECURITY_CENTER_BASE_URL=http://localhost:18080 SECURITY_CENTER_API_KEY=sk-agent-... ./security-center-mcp
# Claude Desktop / Cursor 配置见 security-center-mcp/docs/client-setup.md
```

## 清理（测试完成后）

```bash
./local.sh clean           # 删除容器 + 数据卷（数据全丢）+ sc-local-* 镜像 + .env
./local.sh down            # 只停容器，保留数据卷（下次 up 数据还在）
```

## 常见问题

- **端口冲突**：改 `.env` 里 `SC_LOCAL_*_PORT`（18080/13000/13306/16379）
- **镜像拉取失败**：镜像走 `public.ecr.aws`；如网络受限可自行换成 dockerhub 官方镜像名
- **改代码后生效**：`./local.sh up`（会重新 build）；只想重启不重建用 `up --no-build`
- **数据库重置**：`./local.sh clean` 后重新 `up`（或 `./local.sh mysql` 手动 drop）
- **生产差异提醒**：本地 `bypass_sso=true` + auto-migrate；生产必须 `bypass_sso=false` 且走 SQL 迁移（见 `scripts/enterprise/`）
