Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 140 additions & 0 deletions .github/workflows/deploy-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ on:
required: false
default: 'false'
type: boolean
deploy_prod:
description: '同时部署到生产环境'
required: false
default: 'false'
type: boolean

env:
REGISTRY: ghcr.io
Expand Down Expand Up @@ -340,6 +345,141 @@ jobs:
docker image prune -f
echo "后端部署完成,地址: http://localhost:18000"

# ==================== 部署到生产服务器 ====================
# 触发方式:workflow_dispatch + 勾选 deploy_prod=true。
# 硬性要求:build-backend 与 deploy-test 都成功后才允许执行——生产用的就是
# 测试环境刚验证过的那个 digest,不存在"另build一份"的漂移。
# 依赖(redis/nacos)不在本文件里:生产机 /opt/thvote/prod/docker-compose.deps.yml
# 单独维护,否则 CI 每次覆盖 docker-compose.yml 会把 nacos 冲掉。
deploy-prod:
name: 部署到生产环境
runs-on: ubuntu-latest
needs: [build-backend, deploy-test]
if: ${{ needs.build-backend.result == 'success' && needs.deploy-test.result == 'success' && github.event_name == 'workflow_dispatch' && inputs.deploy_prod }}

steps:
- name: 检出部署配置
uses: actions/checkout@v4
with:
sparse-checkout: |
docker
sparse-checkout-cone-mode: false

- name: 上传 compose 模板到生产服务器
uses: appleboy/scp-action@v0.1.7
with:
host: ${{ secrets.PROD_SERVER_HOST }}
username: ${{ secrets.PROD_SERVER_USER }}
key: ${{ secrets.PROD_SERVER_SSH_KEY }}
source: "docker/docker-compose.prod.yml"
target: "${{ vars.PROD_SERVER_DIR || '/opt/thvote/prod' }}"
strip_components: 1

- name: 部署到生产服务器
uses: appleboy/ssh-action@v1
env:
NACOS_SERVER_ADDRS: ${{ secrets.PROD_NACOS_SERVER_ADDRS }}
NACOS_NAMESPACE: ${{ secrets.PROD_NACOS_NAMESPACE }}
NACOS_USERNAME: ${{ secrets.PROD_NACOS_USERNAME }}
NACOS_PASSWORD: ${{ secrets.PROD_NACOS_PASSWORD }}
# 与 deploy-test 同一个不可变 digest
BACKEND_IMAGE: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}-backend@${{ needs.build-backend.outputs.backend-digest }}
REGISTRY: ${{ env.REGISTRY }}
GITHUB_ACTOR: ${{ github.actor }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
host: ${{ secrets.PROD_SERVER_HOST }}
username: ${{ secrets.PROD_SERVER_USER }}
key: ${{ secrets.PROD_SERVER_SSH_KEY }}
envs: NACOS_SERVER_ADDRS,NACOS_NAMESPACE,NACOS_USERNAME,NACOS_PASSWORD,BACKEND_IMAGE,REGISTRY,GITHUB_ACTOR,GITHUB_TOKEN
script: |
set -e
DEPLOY_DIR="${{ vars.PROD_SERVER_DIR || '/opt/thvote/prod' }}"
mkdir -p "$DEPLOY_DIR/logs"
cd "$DEPLOY_DIR"

echo "$GITHUB_TOKEN" | docker login "$REGISTRY" -u "$GITHUB_ACTOR" --password-stdin

# redis 由 docker-compose.deps.yml 维护(与 CI 覆盖的 docker-compose.yml 分离)
if ! docker ps --format '{{.Names}}' | grep -q '^thvote-redis$'; then
docker compose -f docker-compose.deps.yml up -d redis
fi

# Nacos 必须先跑:后端在 import 阶段就要读配置,读不到会 crash-loop
if ! docker ps --format '{{.Names}}' | grep -q '^mynacos$'; then
echo "Nacos 容器(mynacos)未运行,后端将拿不到任何配置。请先启动 mynacos。终止部署。"
exit 1
fi
echo "nacos 就绪"

# .env 只放 Nacos 连接信息 + 镜像地址,业务配置全在 Nacos
umask 077
{
echo "NACOS_ENABLED=true"
echo "NACOS_SERVER_ADDRS=${NACOS_SERVER_ADDRS}"
echo "NACOS_NAMESPACE=${NACOS_NAMESPACE}"
echo "NACOS_GROUP=DEFAULT_GROUP"
echo "NACOS_DATA_ID=thvote_be"
echo "NACOS_USERNAME=${NACOS_USERNAME}"
echo "NACOS_PASSWORD=${NACOS_PASSWORD}"
echo "BACKEND_IMAGE=${BACKEND_IMAGE}"
} > .env
chmod 600 .env

# 仅覆盖 compose;deps 文件不动
cp docker-compose.prod.yml docker-compose.yml

# 拉取镜像;失败必须硬红(不回退到旧镜像)
docker pull "${BACKEND_IMAGE}" || { sleep 5; docker pull "${BACKEND_IMAGE}"; } || { echo "镜像拉取失败,终止"; exit 1; }

# 应用 Alembic 迁移(DATABASE_URL 由 Nacos 下发)
echo "执行 alembic upgrade head ..."
docker run --rm --network thvote-net -e NACOS_ENABLED=true -e NACOS_SERVER_ADDRS="${NACOS_SERVER_ADDRS}" -e NACOS_NAMESPACE="${NACOS_NAMESPACE}" -e NACOS_GROUP=DEFAULT_GROUP -e NACOS_DATA_ID=thvote_be -e NACOS_USERNAME="${NACOS_USERNAME}" -e NACOS_PASSWORD="${NACOS_PASSWORD}" "${BACKEND_IMAGE}" alembic upgrade head

# 启动后端(--force-recreate:即使镜像未变也重建,便于 Nacos 配置变更后重启)
docker compose up -d --force-recreate backend

# 等待健康检查
for i in $(seq 1 40); do
BACKEND_HEALTH="$(docker inspect -f '{{.State.Health.Status}}' thvote-backend 2>/dev/null || true)"
if [ "$BACKEND_HEALTH" = "healthy" ]; then
echo "后端就绪"
break
fi
if [ "$BACKEND_HEALTH" = "unhealthy" ]; then
echo "后端已进入 unhealthy 状态"
docker compose logs backend | tail -50
exit 1
fi
echo "等待后端启动... ($i/40), 当前状态: ${BACKEND_HEALTH:-starting}"
sleep 3
done

BACKEND_HEALTH="$(docker inspect -f '{{.State.Health.Status}}' thvote-backend 2>/dev/null || true)"
if [ "$BACKEND_HEALTH" != "healthy" ]; then
echo "后端健康检查失败,当前状态: ${BACKEND_HEALTH:-unknown}"
docker compose logs backend | tail -50
exit 1
fi

# 冒烟:P-1 文档关闭 + P-4 测试旁路已删
for PATH_ in /docs /openapi.json /redoc; do
CODE_="$(curl -s -o /dev/null -w '%{http_code}' "http://127.0.0.1:18000${PATH_}")"
if [ "$CODE_" != "404" ]; then
echo "P-1 未生效:${PATH_} 返回 ${CODE_}(应为 404)"
exit 1
fi
done
LOGIN_CODE="$(curl -s -o /dev/null -w '%{http_code}' -X POST "http://127.0.0.1:18000/api/v1/user/login-phone" -H 'Content-Type: application/json' -d '{"phone":"19900000001","code":"888888"}')"
if [ "$LOGIN_CODE" = "200" ]; then
echo "P-4 未生效:测试旁路账号仍能登录"
exit 1
fi
echo "冒烟通过:/docs 404,旁路登录 ${LOGIN_CODE}"

docker image prune -f
echo "生产部署完成,地址: http://localhost:18000"

# ==================== 通知 ====================
notify:
name: 通知
Expand Down
116 changes: 116 additions & 0 deletions alembic/versions/0022_add_pii_encryption.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
"""0022 add encrypted PII columns and migrate the identity unique constraint.

Adds AES-GCM ciphertext columns and HMAC blind-index columns so the plaintext
``subject`` / ``register_ip_address`` / ``created_ip`` can be retired:

- ``user_identity.subject_enc`` / ``subject_idx`` (email/phone/qq/thbwiki)
- ``user_identity.created_ip_enc`` (per-identity bind IP)
- ``user.register_ip_enc`` / ``register_ip_prefix`` (account register IP + /24)

The old plaintext columns are kept (made nullable, server defaults dropped) so
they end up NULL for every new write; migration 0023 drops them after the
import has been verified. The ``(provider, subject)`` unique constraint is
replaced by ``(provider, subject_idx)`` so equality lookup keeps working on the
blind index.

``register_ip_prefix`` is a plain ``String`` (CIDR ``a.b.c.0/24``) rather than
``INET`` to keep the ORM model portable across Postgres and the sqlite test
database; there is no range-query consumer yet.
"""

from alembic import op
import sqlalchemy as sa

revision = "0022"
down_revision = "0021"
branch_labels = None
depends_on = None


def upgrade() -> None:
# Encrypted + blind-index columns.
op.add_column(
"user_identity", sa.Column("subject_enc", sa.LargeBinary(), nullable=True)
)
op.add_column(
"user_identity", sa.Column("subject_idx", sa.LargeBinary(), nullable=True)
)
op.add_column(
"user_identity", sa.Column("created_ip_enc", sa.LargeBinary(), nullable=True)
)
op.add_column("user", sa.Column("register_ip_enc", sa.LargeBinary(), nullable=True))
op.add_column(
"user", sa.Column("register_ip_prefix", sa.String(length=64), nullable=True)
)

# Plaintext columns become NULL-able; drop the "" server defaults so a
# missing value is NULL (and the §10.2 "is not null → 0" scan holds).
op.alter_column(
"user_identity",
"subject",
existing_type=sa.String(length=255),
nullable=True,
)
op.alter_column(
"user",
"register_ip_address",
existing_type=sa.String(length=64),
nullable=True,
server_default=None,
)
op.alter_column(
"user_identity",
"created_ip",
existing_type=sa.String(length=64),
nullable=True,
server_default=None,
)

# Unique constraint migrates from plaintext subject to the blind index.
op.drop_constraint(
"uq_user_identity_provider_subject", "user_identity", type_="unique"
)
op.create_index(
"uq_user_identity_provider_idx",
"user_identity",
["provider", "subject_idx"],
unique=True,
)
op.create_index(
"ix_user_identity_subject_idx", "user_identity", ["subject_idx"]
)


def downgrade() -> None:
op.drop_index("ix_user_identity_subject_idx", table_name="user_identity")
op.drop_index("uq_user_identity_provider_idx", table_name="user_identity")
op.create_unique_constraint(
"uq_user_identity_provider_subject",
"user_identity",
["provider", "subject"],
)
op.alter_column(
"user_identity",
"created_ip",
existing_type=sa.String(length=64),
nullable=False,
server_default="",
)
op.alter_column(
"user",
"register_ip_address",
existing_type=sa.String(length=64),
nullable=False,
server_default="",
)
op.alter_column(
"user_identity",
"subject",
existing_type=sa.String(length=255),
nullable=False,
)
op.drop_column("user", "register_ip_prefix")
op.drop_column("user", "register_ip_enc")
op.drop_column("user_identity", "created_ip_enc")
op.drop_column("user_identity", "subject_idx")
op.drop_column("user_identity", "subject_enc")
4 changes: 4 additions & 0 deletions docker/docker-compose.prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,14 @@ services:
NACOS_PASSWORD: "${NACOS_PASSWORD}"
APP_HOST: "0.0.0.0"
APP_PORT: "8000"
PII_DEK_PATH: "/run/secrets/pii_dek"
ports:
- "18000:8000"
volumes:
- ./logs:/app/logs
# DEK 用相对路径挂载:测试机解析到 /opt/thvote/test/secrets/,生产到
# /opt/thvote/prod/secrets/。宿主文件权限(0400/uid1000)随 bind mount 保留。
- ./secrets/pii_dek:/run/secrets/pii_dek:ro
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:8000/health"]
interval: 15s
Expand Down
9 changes: 9 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@
> 创建日期:2026-04-27
> **2026-08-31 整理**:合并 9 组重复条目、按日期倒序重排;**2026-07-01 之前**的条目已迁至 [CHANGELOG-archive-2026H1.md](./CHANGELOG-archive-2026H1.md)。

## [2026-10-29] 生产上线前加固:关闭 API 文档 + 移除测试登录旁路 + deploy-prod 流水线

- **Added**:`Settings.expose_api_docs`(`EXPOSE_API_DOCS`,默认 `false`)。`src/main.py` 按开关传 `docs_url`/`redoc_url`/`openapi_url`,并给 `AppGraphQLRouter` 传 `graphql_ide`——关闭时 `/docs`、`/redoc`、`/openapi.json`、浏览器打开 `/graphql` 均 404,`POST /graphql` 不受影响。回归用例 `tests/unit/test_expose_api_docs.py`(8 例)。
- **Removed**:测试登录旁路整体删除(P-4):`src/common/verification/test_bypass.py`、`sms_code.py`/`email_code.py` 两个 `consume` 短路、`Settings` 两个字段、`tests/unit/test_test_login_bypass.py`。测试/生产 Nacos 同步删键;`grep -r TEST_LOGIN_BYPASS src` 为空。设计稿与操作文档标记为已移除,`scripts/e2e/scraper_frontend.js` 改为需真实验证码。
- **Added**:`.github/workflows/deploy-test.yml` 新增 `deploy_prod` 手动触发开关与 `deploy-prod` job(`needs: [build-backend, deploy-test]`,复用同一 digest 部署到生产机 `/opt/thvote/prod`);compose 未变,依赖(redis/nacos)由生产目录的 `docker-compose.deps.yml` 单独维护。
- **Fixed**:`TRUSTED_PROXY_IPS` / `ADMIN_ALLOWED_IPS` 改为 `NoDecode` + 宽容解析,接受 `""`、JSON 数组、逗号分隔三种写法。此前把这两个键按文档写成空串会让 `Settings()` 在 import 期抛 `SettingsError`、进程起不来(生产首次部署实测踩中)。用例 `tests/unit/test_config_ip_lists.py`。
- **兼容性影响**:生产环境 graphQL 文档/调试界面不可用属预期;调用方若依赖 `/openapi.json` 生成客户端,需在测试环境用 `EXPOSE_API_DOCS=true`。
- **数据迁移 / 配置变更**:生产 Nacos 新建 namespace `thvote-prod` + dataId `thvote_be`;`ADMIN_ALLOWED_IPS`/`TRUSTED_PROXY_IPS` 留空、`EXPOSE_API_DOCS=false`。未导入用户数据、未改前端仓库。

## [2026-10-22] 详情问卷保留筛选条件与组合目标

- **Fixed**:角色/音乐/组合详情问卷将原约束与目标对象通过 AND 合并,保留 OR 分组;三个本届榜单的指令模式详情链接也携带原约束。沿用已有压缩 URL 参数并兼容旧链接。
Expand Down
2 changes: 1 addition & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@
| [`2026-07-17-submit-timing-signal-design.md`](./superpowers/specs/2026-07-17-submit-timing-signal-design.md) | 提交耗时 + 服务端改票计数(B-045/B-046,`fill_duration_ms` + `attempt`;改票假阳性由 attempt 兜底) |
| [`2026-07-17-block-scripts-design.md`](./superpowers/specs/2026-07-17-block-scripts-design.md) | 拦脚本(B-048,Origin/Referer 校验;只拦变更、query 放行;**默认关灰度**) |
| [`2026-06-08-security-backend-design.md`](./superpowers/specs/2026-06-08-security-backend-design.md) | 安全块后端(B-037):二创提名校验 + 人工审核队列 + 提名时间窗 + 投票问卷门禁 |
| [`2026-08-14-test-login-bypass-design.md`](./superpowers/specs/2026-08-14-test-login-bypass-design.md) | ⚠️ 测试登录旁路(白名单固定验证码)。**临时测试设施,公开上线前必须整体移除**:`grep -r TEST_LOGIN_BYPASS` |
| [`2026-08-14-test-login-bypass-design.md`](./superpowers/specs/2026-08-14-test-login-bypass-design.md) | ❌ 已移除(P-4,2026-10-29)测试登录旁路(白名单固定验证码)历史存档;代码与 Nacos 键均已删除 |

### 管理端

Expand Down
18 changes: 6 additions & 12 deletions docs/operations/captcha-onboarding.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,18 +97,12 @@ curl -s -X POST http://154.37.215.62:18000/graphql -H 'content-type: application

> 前后端的 SceneId 必须是**同一个场景**;前端 prefix 与后端 AK 必须是**同一个账户**——混用新旧账户的任意组合都会校验失败(阿里云侧对不上)。

## 六点五、测试期登录旁路(TEST_LOGIN_BYPASS,上线前移除)

自动化测试(浏览器端到端)无法过滑块、也收不到真实验证码。测试环境提供**验码侧白名单固定码**旁路(设计稿 [test-login-bypass](../superpowers/specs/2026-08-14-test-login-bypass-design.md)):

- 测试 Nacos `thvote_be` 配两个 key(**生产 dataId 永不配置**):
```json
"TEST_LOGIN_BYPASS_ACCOUNTS": "[\"19900000001\", \"test1@example.com\"]",
"TEST_LOGIN_BYPASS_CODE": "888888"
```
- 用法:登录页填白名单假号 → 验证码直接填固定码 → 登录。**不要点"发送验证码"**——旁路只挂在验码侧,发码链路(captcha/PNVS/限流)原样不动,真人真号不受影响。
- 每次命中打 WARNING 日志(账号脱敏)。改完 Nacos 要重启容器(B-017,workflow_dispatch 即可)。
- **移除条件**:公开上线前删除,`grep -r TEST_LOGIN_BYPASS` 全量定位(模块/短路行/config 字段/Nacos key/本节)。
## 六点五、测试期登录旁路(已于 2026-10-29 移除)

> ❌ **本节已失效**:白名单固定验证码旁路(P-4)已于 2026-10-29 从代码与 Nacos 中整体删除
> (`src/common/verification/test_bypass.py`、两个 `consume` 短路、`Settings` 两个字段)。
> 现在所有账号(含测试假号)都必须走真实发码 + 真实验码,自动化端到端测试需要真人提供验证码。
> 历史设计与取舍见 [test-login-bypass](../superpowers/specs/2026-08-14-test-login-bypass-design.md)。

## 七、常见坑速查

Expand Down
Loading
Loading