Remove online update subsystem

go
codex 1 month ago
parent 92ace142d1
commit a587346d43

@ -20,14 +20,14 @@ Virt Cloud 是 PHP 8.5 面板与宿主机 Go Agent 组成的虚拟机销售平
virt-cloud/
agent/ 宿主机 Go Agent 与测试
web/ PHP 面板、页面、全新 SQL schema 与测试
deploy/ Agent、PHP workers、Agent Gateway 和更新 helper 的部署样例
deploy/ Agent、PHP workers 和 Agent Gateway 的部署样例
runtime/ 安装时生成的私有状态与密钥,不入 Git
docs/ 架构、部署、运维和验证说明
```
HTTP 文档根目录必须是 `web/public/`。普通页面和 API 全部由 PHP 提供。面板侧 `virt-cloud-agent-gateway` 同时承载 Agent 主动控制通道和反向控制台:浏览器只提交 PHP 签发的一次性票据;Gateway 仅在已连接会话的内存中暂存该 Agent 握手密钥,用于每次普通请求或控制台打开前向 PHP 复核,绝不持久化。未部署并验收 Gateway 时必须将 Agent 操作和控制台标记为不可用,不能回退到旧引擎。
Agent、更新 helper 和 Agent Gateway 只校验配置文件是否可读、JSON 与字段内容是否有效;它们不审计 owner、group 或 mode,不会因为可读文件是 `0600`、`0640` 或 `0644` 而拒绝启动。最小权限仍是运维加固建议,不是应用启动的权限位合规检查。
Agent 和 Agent Gateway 只校验配置文件是否可读、JSON 与字段内容是否有效;它们不审计 owner、group 或 mode,不会因为可读文件是 `0600`、`0640` 或 `0644` 而拒绝启动。最小权限仍是运维加固建议,不是应用启动的权限位合规检查。
云镜像只接受经过校验的独立 QCOW2 内容;远程导入只允许可公开解析的 HTTPS 直链,并对初始地址及全部跳转执行 SSRF、大小与摘要校验,自制镜像通过管理员上传入口导入。拒绝 raw/ISO、加密镜像、外部 data file、backing file 和超过限制的文件。镜像来源的发行方可信度由管理员负责确认。

@ -1,17 +0,0 @@
{
"socket_path": "/run/virt-cloud/update.sock",
"repository_dir": "/home/virt-cloud",
"remote_url": "https://g.luoli.live/root/virt.git",
"branch": "go",
"state_path": "/home/virt-cloud/runtime/update/state.json",
"service_secret": "REPLACE_ME",
"php_command": "/usr/bin/php",
"php_fpm_unit": "php-fpm.service",
"service_units": [
"virt-cloud-worker.service",
"virt-cloud-lifecycle.service",
"virt-cloud-policy-worker@1.service",
"virt-cloud-policy-worker@2.service",
"virt-cloud-agent-gateway.service"
]
}

@ -1,28 +0,0 @@
[Unit]
Description=Virt Cloud privileged update helper
After=network-online.target mariadb.service
Wants=network-online.target
[Service]
Type=simple
User=root
Group=apache
WorkingDirectory=/home/virt-cloud
ExecStart=/home/virt-cloud/bin/virt-cloud-update --config /home/virt-cloud/runtime/update.json
Restart=on-failure
RestartSec=5
UMask=0007
# UMask/RuntimeDirectoryMode only harden newly created files; the helper does
# not audit the owner, group, or mode of its readable JSON configuration.
RuntimeDirectory=virt-cloud
RuntimeDirectoryMode=0750
NoNewPrivileges=true
PrivateTmp=true
ProtectHome=false
ProtectSystem=strict
ReadWritePaths=/home/virt-cloud /run/virt-cloud
LimitCORE=0
TimeoutStopSec=10min
[Install]
WantedBy=multi-user.target

@ -47,18 +47,17 @@ POST /api/v1/admin/operations/jobs/{id}/force-delete
“近期上报进程数”不是任务数量,也不能证明期望的全部策略 worker 已启动。新安装、进程尚未启动、心跳不可读或只运行过 `--once` 时显示未知;不要把未知当作正常。
## 升级与心跳目录
## 维护与心跳目录
全新安装由 `php xcat cloudInit` 创建所需表。worker 在 `web/storage/cloud-runtime/` 原子写入私有心跳;网页 PHP 与 worker/lifecycle 应使用同一系统用户。目录不可读时页面显示未知,但心跳观测失败不能阻止 worker 执行业务任务。目录位于 Web 文档根之外并已加入 `.gitignore`。
已运行的 PHP 常驻进程不会自动载入新代码。使用在线更新流程会重启相关服务;手动更新则按现有维护流程,在任务安全的维护窗口重启普通 worker、生命周期调度和实际部署的全部策略 worker。标准服务名为:
已运行的 PHP 常驻进程不会自动载入新代码。代码升级只能由管理员通过 SSH 在任务安全的维护窗口完成:先备份业务库、日志库和 `runtime/php/data.key`,核对准备部署的提交与依赖变更,再更新源码、安装经过审查的依赖、执行 `php xcat cloudInit`,并重启 PHP-FPM、普通 worker、生命周期调度、实际部署的全部策略 worker 和 Agent Gateway。数据库步骤结果不确定时必须停止并人工核对,不能仅靠回退源码判断数据库已经恢复。标准应用服务名为:
```text
virt-cloud-worker.service
virt-cloud-lifecycle.service
virt-cloud-policy-worker@1.service
virt-cloud-policy-worker@2.service
virt-cloud-update.service
virt-cloud-agent-gateway.service
```

@ -9,7 +9,6 @@
- `virt-cloud-policy-worker.service`
- `virt-cloud-policy-worker@1.service`
- `virt-cloud-policy-worker@2.service`
- `virt-cloud-update.service`
- `virt-cloud-agent-gateway.service`
接口使用参数数组启动 `/usr/bin/journalctl`(系统没有该路径时使用 `/bin/journalctl`),不会经过 shell,也不接受任意 unit、命令或文件路径。单次最多读取 200 条,使用 `--cursor` 加 `--reverse` 加载更早记录;journalctl 会从游标向更早记录移动,返回结果通常不包含边界,服务端也会防御性移除意外返回的边界记录。这样可以避开部分旧 systemd 版本在 `--after-cursor` 与 unit 筛选组合下漏掉首条记录的问题。带时间下界的游标页会在 PHP 中继续应用下界,因为 systemd 不允许 `--since` 与游标同时出现。消息预览、详情字段、输出、stderr 和子进程运行时间都有上限。每条列表记录保留 journal cursor、服务和 UTC 微秒时间戳,点击后通过 `GET /api/v1/admin/system-logs/detail?cursor=...&unit=...&timestamp=...` 在该时间戳窗口内重新读取,再校验返回 cursor,避免不同 systemd 版本的 cursor seek 差异影响详情;日志轮转或游标不存在时返回 404。命令行、可执行文件和 core-dump 字段不会返回,名称明显表示密码、令牌、密钥、凭据或授权信息的结构化字段会隐藏值,消息中的常见凭据形式也会遮罩。日志内容可能包含运行时上下文,因此接口仅对管理员开放,并设置 `private, no-store`。

@ -2,7 +2,7 @@
本版本只支持全新安装:不导入旧数据库,不读取旧引擎密文、配置或宿主机凭据,也不提供兼容回退。示例使用 AlmaLinux、项目目录 `/home/virt-cloud`、PHP-FPM 用户 `apache`;请按实际发行版调整包名和服务名。
面板服务器运行 PHP、数据库客户端、workers、更新 helper 和 Go Agent Gateway。每台 KVM 宿主机单独运行 Go `virt-agent`,Agent 只连接该机的 `qemu:///system`,并主动连接面板 WSS;不经 SSH 管理另一台宿主机。
面板服务器运行 PHP、数据库客户端、workers 和 Go Agent Gateway。每台 KVM 宿主机单独运行 Go `virt-agent`,Agent 只连接该机的 `qemu:///system`,并主动连接面板 WSS;不经 SSH 管理另一台宿主机。
## 1. 依赖
@ -11,7 +11,7 @@
- PHP 8.5 CLI/FPM、PDO MySQL、sodium、mbstring、curl、XML/DOM、OpenSSL、session,Composer 2。
- MariaDB:一个业务库和一个日志库;初始化账户需要建表权限。当前 SQL 以 MariaDB 为目标,不承诺兼容 Oracle MySQL。
- Redis 或兼容 Valkey,用于会话、验证码与限流。
- Go 1.24 或更新版本,用于构建本机更新 helper 与 Agent Gateway。
- Go 1.24 或更新版本,用于构建本机 Agent Gateway。
- Nginx/Apache 与有效 HTTPS 证书。
每台虚拟化宿主机:
@ -145,7 +145,7 @@ journalctl -u virt-cloud-agent.service -n 100 --no-pager
在面板 `/admin/agents` 为相同宿主机 ID 保存对应密钥并启动 Agent。先保持宿主机调度状态为 disabled;到宿主机页选择实际网络接入并应用存储目录,再回到 `/admin/agents` 执行连接测试。健康、存储和所选网络等必要 capabilities 全部通过后,才能启用资源调度和销售。如果二进制未带 `libvirt` build tag,Agent 会以 `libvirt_adapter_not_built` 明确失败,不会退化成外部命令或远程连接。
## 5. 面板 workers、更新 helper 与 Agent Gateway
## 5. 面板 workers 与 Agent Gateway
PHP workers 不依赖本机虚拟化服务;它们先持久化任务,再通过本机 Agent Gateway 将请求发给任务绑定的 Agent:
@ -160,47 +160,27 @@ systemctl enable --now virt-cloud-worker.service virt-cloud-lifecycle.service \
普通 worker 负责购买、创建、重装、邮件和最终删除;至少两个策略 worker 处理到期断网、网络恢复与删除前策略;lifecycle 生成续费账单并推进账期、流量、宽限和删除状态。不要因某个 Agent 离线而停止其他宿主机的任务处理。
在线更新由本机 root Go helper 提供固定 Unix socket API,不通过 Agent 执行。在面板机器构建并安装更新 helper 和 Agent Gateway:
在面板机器构建并安装 Agent Gateway:
```bash
install -d /home/virt-cloud/bin /home/virt-cloud/runtime/update
install -d /home/virt-cloud/bin
cd /home/virt-cloud/gateway
go mod download
go mod verify
go test ./...
go build -trimpath -o /home/virt-cloud/bin/virt-cloud-update ./cmd/virt-cloud-update
go build -trimpath -o /home/virt-cloud/bin/virt-cloud-agent-gateway ./cmd/virt-cloud-agent-gateway
install -o root -g root -m 0600 \
/home/virt-cloud/deploy/virt-cloud-update.json.example /home/virt-cloud/runtime/update.json
install -o apache -g apache -m 0600 \
/home/virt-cloud/deploy/virt-cloud-agent-gateway.json.example /home/virt-cloud/runtime/agent-gateway.json
cp /home/virt-cloud/deploy/virt-cloud-update.service /etc/systemd/system/
cp /home/virt-cloud/deploy/virt-cloud-agent-gateway.service /etc/systemd/system/
```
启动前先编辑两个 runtime JSON,把所有 `REPLACE_ME` 占位符换成不同的安全随机密钥,并在 PHP `config.php` 中配置完全一致的 `cloud_update_helper_secret` 和 `cloud_agent_gateway_secret`;`cloud_agent_gateway_url` 必须严格为 `http://127.0.0.1:8001/internal/agent/request`。占位符短于 32 字节,保留它会使服务拒绝启动。然后才启动:
启动前先编辑 Gateway runtime JSON,把 `REPLACE_ME` 占位符换成安全随机密钥,并在 PHP `config.php` 中配置完全一致的 `cloud_agent_gateway_secret`;`cloud_agent_gateway_url` 必须严格为 `http://127.0.0.1:8001/internal/agent/request`。占位符短于 32 字节,保留它会使服务拒绝启动。然后才启动:
```bash
systemctl daemon-reload
systemctl enable --now virt-cloud-update.service virt-cloud-agent-gateway.service
systemctl enable --now virt-cloud-agent-gateway.service
```
`virt-cloud-update` 常驻并仅监听 `/run/virt-cloud/update.sock`,PHP 使用固定的 settings/check/apply/jobs API。配置字段为:
| 字段 | 约束 |
| --- | --- |
| `socket_path` | 必须为 `/run/virt-cloud/update.sock` |
| `repository_dir` | 必须是干净绝对路径,部署样例为 `/home/virt-cloud` |
| `remote_url` | 必须为 `https://g.luoli.live/root/virt.git` |
| `branch` | 必须为 `go` |
| `state_path` | 必须精确为 `<repository_dir>/runtime/update/state.json` |
| `service_secret` | 至少 32 字节,必须与 PHP `cloud_update_helper_secret` 一致 |
| `php_command` | PHP CLI 的干净绝对路径 |
| `php_fpm_unit` | 需停止并首先恢复的 PHP-FPM systemd unit |
| `service_units` | 1–32 个已加载且正在运行的应用 unit;不能包含 PHP-FPM 自身或 `virt-cloud-update.service` |
仓库访问令牌经 API 保存到 helper 状态,不写配置样例、URL、进程参数或日志。helper 只允许固定远端、固定 `go` 分支和干净工作树 fast-forward;`cloudInit` 结果不确定时保留维护锁,必须 SSH 人工核对。如果未部署某个可选应用 unit,启动 update helper 前必须把它从 `service_units` 样例中删除;不得为绕过预检而填写虚假 unit。
面板 Agent Gateway 只监听 `127.0.0.1:8001`。`runtime/agent-gateway.json` 的 `panel_shared_secret` 必须与 PHP `cloud_agent_gateway_secret` 一致;`panel_introspection_url` 和 `panel_agent_auth_url` 必须分别是字面 loopback IP 上的 `/api/internal/cloud/session-introspection` 与 `/api/internal/cloud/agent-auth`。Gateway 在 Agent WSS 握手以及每次普通请求或控制台打开前请 PHP 重新核验当前连接保存的 Agent 凭据;核验失败会撤销该连接。
`agent_request_timeout_seconds` 必须为 `870`。完整截止时间顺序是 Agent API 840 秒、Agent WSS 控制层 855 秒、Gateway 870 秒、PHP 900 秒,普通 worker 的 930 秒租约再包住 PHP 请求。不要把各层设置成相同值,否则外层可能先超时并丢失 Agent 已生成的结构化回执。
@ -225,7 +205,23 @@ PHP 只返回下列字段;`agent_id` 对应已连接的数字 Agent 身份,
Gateway 向对应 Agent 的控制长连接发送 `console_open`,Agent 消费票据并主动建立 `/agent/console` 反向 WSS。Gateway 每 15 秒发送 `{"ticket":"<browser-ticket>","connection_id":"<opaque-connection-id>","consume":false}`。会话、用户、VM 所有权和服务均仍有效时 PHP 必须严格只返回 `{"active":true}`。任何拒绝、超时或 `active:false` 都必须断开两端。
这两个 helper 的配置加载都不把 owner/group/mode 当成启动条件;只要实际 service 用户能读取并且 JSON/字段有效,不会因权限位不符合某个预设值而拒绝启动。建议使用最小读取范围,绝不要 `chmod 777`。
Agent Gateway 的配置加载不把 owner/group/mode 当成启动条件;只要实际 service 用户能读取并且 JSON/字段有效,不会因权限位不符合某个预设值而拒绝启动。建议使用最小读取范围,绝不要 `chmod 777`。
### 从旧版本撤除在线更新组件
如果旧版本已经安装过在线更新服务,请通过 SSH 在面板机确认以下路径确实属于该旧组件后,以 root 执行一次清理。本节只用于卸载遗留文件;当前版本不构建、安装或调用该组件。
```bash
systemctl disable --now virt-cloud-update.service || true
rm -f /etc/systemd/system/virt-cloud-update.service
rm -f /home/virt-cloud/bin/virt-cloud-update
rm -f /home/virt-cloud/runtime/update.json
rm -rf /home/virt-cloud/runtime/update
rm -f /run/virt-cloud/update.sock
systemctl daemon-reload
```
同时从 PHP 私有配置中删除旧的 `cloud_update_helper_secret`。不要删除 Agent Gateway 的二进制、配置、密钥或 systemd unit。
## 6. Web 与 WebSocket
@ -300,7 +296,7 @@ location = /agent/console {
}
```
不要公开 PHP 内省/认证接口、`/internal/agent/request`、helper Unix socket、Gateway 的本机端口或 libvirt。Agent 宿主机无需开放管理端口;出站网络只需能访问面板 HTTPS/WSS 和经校验的 HTTPS 镜像源。
不要公开 PHP 内省/认证接口、`/internal/agent/request`、Gateway 的本机端口或 libvirt。Agent 宿主机无需开放管理端口;出站网络只需能访问面板 HTTPS/WSS 和经校验的 HTTPS 镜像源。
控制台上线前必须验收:一次性票据只能消费一次、过期拒绝、登出/撤销后断开、跨用户/跨 VM 拒绝、Origin 校验、上游 Agent 断线及时关闭、浏览器不能注入 endpoint。Agent 未声明 console capability 时,PHP 必须显示不可用,不能回退旧实现。
@ -327,7 +323,7 @@ location = /agent/console {
- 在明确授权的非生产宿主机重启实际启用的 `virtqemud` 或 `libvirtd`,确认 Agent 检测连接关闭、后台重连并重新注册 lifecycle、`JOB_COMPLETED` 和 close callback;健康检查恢复后只按原 `operation_id` 扫描并核对,不重放变更。再重启 Agent,确认同一批未完成 operation 会从 Journal recovery 索引重新扫描。
- 并发购买不超卖 CPU/内存/磁盘/IP;同一 VM 变更串行。
- RX+TX 额度用尽只断网;下一账期恢复;最终到期只断网;三天宽限结束后删除定义与面板受管磁盘。
- MariaDB、Redis、SMTP、支付回调、大文件上传、在线更新恢复点和 PHP-FPM 重启。
- MariaDB、Redis、SMTP、支付回调、大文件上传和 PHP-FPM 重启。
验证命令和记录格式见 [VALIDATION.md](VALIDATION.md)。禁止在生产宿主机做破坏性验收;未授权主机只允许读 capabilities、版本、域/网络/池清单等只读信息。
@ -337,8 +333,7 @@ location = /agent/console {
journalctl -u virt-cloud-agent.service -n 200 --no-pager
journalctl -u virt-cloud-worker.service -u virt-cloud-lifecycle.service \
-u 'virt-cloud-policy-worker@*' -n 200 --no-pager
journalctl -u virt-cloud-update.service -u virt-cloud-agent-gateway.service \
-n 200 --no-pager
journalctl -u virt-cloud-agent-gateway.service -n 200 --no-pager
```
先区分 PHP 任务尚未派发、Gateway 未运行、Agent 出站 WSS 认证/网络失败、已派发但响应丢失、libvirt 明确失败和业务库提交失败。权限位与所有者不是应用层合规检查项;遇到启动错误应读取具体的“不可读、JSON 无效或字段无效”诊断,而不是盲目放宽权限。

@ -98,12 +98,6 @@ Gateway 固定以 `{"service":"agent-gateway"}` actor 签名 POST `/api/internal
当前部署只有在 Gateway 二进制、Agent console capability、一次性消费、会话撤销、Origin、超时和断线关闭测试全部通过后才能启用控制台;否则页面应显示不可用,不能回退到旧引擎。
## 在线更新
PHP 只做超级管理员授权、CSRF 和固定 Unix socket API 调用。本机 root Go helper `virt-cloud-update` 固定仓库 `https://g.luoli.live/root/virt.git` 与 `go` 分支,通过 `/run/virt-cloud/update.sock` 提供 settings/check/apply/jobs。访问令牌由 helper 保存到私有状态目录,不进入 URL、命令行或日志。
helper 只允许干净工作树 fast-forward,按持久检查点停止服务、更新源码、运行 `php xcat cloudInit` 并重启/健康检查。它不在线安装或改写依赖;修改 `deploy/**`、Go modules、Composer/JavaScript/Cargo 依赖声明或 lock 的版本必须在人工维护窗口升级与复测。对于历史文件类型,任何 `requirements*.txt` 变更也会被拒绝;这是变更面检查,不表示当前运行需要 Python。数据库初始化结果不确定时保持维护锁并要求 SSH 人工核对,不能仅回退源码。
## 上线边界
生产前必须实测 Agent WSS/HMAC、防重放、断线后对账、Agent 与实际 libvirt daemon 分别重启后的连接恢复和 Journal 扫描、宿主机重启、UEFI、cloud-init、快照/救援/重装、反向控制台撤销、流量跨 QEMU 重启、到期断网、宽限删除、HTTPS 镜像安全和并发购买。测试不得对未明确授权的宿主机做创建、写配置、改网络或修改虚拟机操作。

@ -25,7 +25,7 @@
- PHP 是账户、财务、资源账本、Agent 注册、公共 HTTP 授权和操作协调的唯一权威。
- 每台宿主机的 `virt-agent` 只负责本机 `qemu:///system`、受管磁盘、网络、防火墙、快照、救援、指标和控制台后端。
- 面板到 Agent 的每个请求使用该宿主机独立 HMAC 密钥,并在 Agent 主动建立的 WSS 会话上转发。Agent 不连接面板数据库,浏览器不能直接调用 Agent。
- 面板侧 Go helper 负责受限在线更新;Agent Gateway 负责 Agent 主动 WSS 会话、PHP 请求转发和反向控制台,不承担账户或 VM 调度逻辑。
- Agent Gateway 负责 Agent 主动 WSS 会话、PHP 请求转发和反向控制台,不承担账户或 VM 调度逻辑。
## 数据库边界

@ -4,7 +4,7 @@
## 1. 静态与单元测试
Go Agent 与本机 helper:
Go Agent 与 Agent Gateway:
```bash
cd /home/virt-cloud/agent
@ -134,14 +134,14 @@ virsh -c qemu:///system pool-list --all
用桌面和手机宽度检查页面,确认后端切换没有改变用户可见信息架构,也没有向普通用户暴露宿主机或 Agent 管理入口。
## 7. 更新与恢复
在测试副本验证固定 `go` 分支 fast-forward、脏工作树拒绝、签名 API、令牌不回显、依赖变更人工门槛、service 停启顺序、`cloudInit` 失败、PHP-FPM 启动失败和健康检查失败。进入 `cloudInit` 后中断必须保留维护锁;不得因回退 Git 提交自动判定数据库安全。本项是对当前全新 schema 初始化/重放的保守处理,不是旧库兼容迁移。
## 8. 发布记录
## 7. 发布记录
发布提交前把实际结果追加到本节;未执行项写“未执行”及原因,不能写成通过。
以下记录保留各版本当时的真实构建和验证事实。旧记录中出现的
`virt-cloud-update`、Updater 或“三产物”均指现已退役的历史组件,不代表
当前版本仍支持、构建或交付在线更新功能。
```text
commit:
Go version / libvirt version / PHP version / MariaDB version:

@ -1,27 +1,24 @@
# Virt Cloud panel-side Go helpers
# Virt Cloud panel-side Agent gateway
`gateway` is an independent Go module containing the two services that cannot
safely run inside PHP-FPM:
`gateway` is an independent Go module containing `virt-cloud-agent-gateway`.
The service keeps Agent-initiated control WebSockets, accepts
authenticated dispatches from local PHP, and pairs reverse console streams
with the existing browser console URL.
- `virt-cloud-update` is the privileged, Unix-socket-only update coordinator.
- `virt-cloud-agent-gateway` keeps Agent-initiated control WebSockets, accepts
authenticated dispatches from local PHP, and pairs reverse console streams
with the existing browser console URL.
Neither service is a compatibility layer for the retired Python engine.
It is not a compatibility layer for the retired Python engine.
## Build and test
```bash
cd gateway
go test ./...
go build ./cmd/virt-cloud-update ./cmd/virt-cloud-agent-gateway
go build ./cmd/virt-cloud-agent-gateway
```
Both commands require an absolute `--config` path. The JSON decoders reject
unknown fields and trailing values. They deliberately do **not** reject a
readable configuration because of its owner, group, or mode; missing,
unreadable, malformed, or unsafe configuration still fails startup.
The executable requires an absolute `--config` path. The JSON decoder rejects
unknown fields and trailing values. Configuration loading deliberately does
**not** reject a readable configuration because of its owner, group, or mode;
missing, unreadable, malformed, or unsafe configuration still fails startup.
The Agent request deadline is deliberately fixed at 870 seconds in the
Gateway configuration. The host Agent API caps operations at 840 seconds, its
@ -31,7 +28,7 @@ durably classify the inner layer's result instead of timing out first.
## Authentication
The update API and gateway-to-PHP requests use the shared `X-Virt-*` contract:
Gateway-to-PHP requests use the shared `X-Virt-*` contract:
```text
METHOD\n
@ -42,71 +39,10 @@ nonce\n
base64url-json-actor
```
The signature is lowercase HMAC-SHA256. The update helper persists nonce
expiry state, accepts a 60-second clock window, and requires a managing super
administrator actor. The Agent gateway signs PHP authentication and console
introspection requests with `{"service":"agent-gateway"}` and accepts local
PHP dispatches only with `{"service":"php-panel"}`.
## Update API
The helper listens only on `/run/virt-cloud/update.sock`. Its repository and
branch are pinned to `https://g.luoli.live/root/virt.git` and `go`. Its state
path is fixed below the configured checkout at
`runtime/update/state.json`; startup rejects a runtime symlink that escapes
the checkout, without auditing owner/group/mode.
| Method | Path | Purpose |
| --- | --- | --- |
| `GET` | `/v1/health` | Helper health and maintenance-lock state |
| `GET` | `/v1/settings` | Fixed repository, checkout state, job pointers, and `token_configured` |
| `PUT` | `/v1/settings` | Saves `{"access_token":"..."}`, preserves an empty value, or clears with `{"clear_token":true}` |
| `DELETE` | `/v1/settings/token` | Clears the saved token |
| `POST` | `/v1/check` | Read-only `ls-remote` check; body is empty or `{}` |
| `POST` | `/v1/apply` | Starts a pinned update transaction |
| `GET` | `/v1/jobs` | Lists durable jobs and lock state |
| `GET` | `/v1/jobs/{id}` | Reads one durable job |
| `POST` | `/v1/jobs/{id}/resolve` | Records an operator-confirmed reconciliation |
Apply requires the exact commits returned by check:
```json
{
"expected_current_commit": "<full lowercase object id>",
"expected_remote_commit": "<full lowercase object id>"
}
```
The helper verifies the Git root, clean worktree, literal origin, `go` branch,
fetched object, and fast-forward relationship. It rejects updates touching
`deploy/**`, Go modules, Composer manifests/lock files, JavaScript dependency
manifests/lock files, Cargo manifests/lock files, or Python requirement files.
Those changes require a manual maintenance window.
Before the first host mutation it verifies that PHP is runnable, the current
and target `web/xcat` are regular files, and every configured unit is loaded
and active. Before each mutation it atomically persists a checkpoint. The
sequence is stop application services, stop PHP-FPM, fast-forward source, run
`php xcat cloudInit` with `web/` as its working directory, start PHP-FPM and
wait for it before starting application services, then wait up to 60 seconds
for every unit to become active. A crash or error at or after the stop checkpoint becomes
`manual_intervention`; it does not replay schema initialization, reset source, or claim a
rollback. An operator can release that lock only after SSH reconciliation:
```json
{"resolution":"completed","evidence":"ssh: verified schema and all units"}
```
`rolled_back` is also accepted when the operator has independently verified a
safe rollback. Access tokens are stored only in the private state file, passed
to Git through a temporary askpass environment, omitted from command
arguments, responses, jobs, and logs, and can be explicitly cleared.
The settings, check, apply, and job responses retain the existing Venix admin
page DTO names. In particular, an internal `manual_intervention` state is
exposed as terminal `blocked`, `checkpoint` as `phase`, the applied commit as
`new_commit`, and the pre-update commit as an informational `rollback_ref`.
That reference never triggers an automatic rollback.
The signature is lowercase HMAC-SHA256. The Agent gateway signs PHP
authentication and console introspection requests with
`{"service":"agent-gateway"}` and accepts local PHP dispatches only with
`{"service":"php-panel"}`.
## Agent gateway contract

@ -1,98 +0,0 @@
package main
import (
"context"
"errors"
"flag"
"fmt"
"log"
"net"
"net/http"
"os"
"os/signal"
"path/filepath"
"syscall"
"time"
"git.luoli.live/root/virt/gateway/update"
)
func main() {
configPath := flag.String("config", "", "absolute path to the JSON configuration")
flag.Parse()
if *configPath == "" || !filepath.IsAbs(*configPath) {
log.Fatal("--config must be an absolute path")
}
config, err := update.LoadConfig(*configPath)
if err != nil {
log.Fatalf("update configuration is invalid: %v", err)
}
state, err := update.OpenConfiguredState(config)
if err != nil {
log.Fatalf("update state is unavailable: %v", err)
}
manager, err := update.NewManager(config, state, update.OSCommandExecutor{})
if err != nil {
log.Fatalf("update manager cannot start: %v", err)
}
handler, err := update.NewServer(manager, state, []byte(config.ServiceSecret))
if err != nil {
log.Fatalf("update API cannot start: %v", err)
}
listener, err := listenUnix(config.SocketPath)
if err != nil {
log.Fatalf("update socket cannot start: %v", err)
}
defer func() {
_ = listener.Close()
_ = os.Remove(config.SocketPath)
}()
server := &http.Server{Handler: handler, ReadHeaderTimeout: 5 * time.Second, ReadTimeout: 15 * time.Second,
WriteTimeout: 45 * time.Second, IdleTimeout: 30 * time.Second, MaxHeaderBytes: 32 << 10}
stop := make(chan os.Signal, 1)
signal.Notify(stop, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-stop
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
_ = server.Shutdown(ctx)
}()
log.Printf("virt-cloud-update listening on %s", config.SocketPath)
if err := server.Serve(listener); err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Fatalf("update API stopped unexpectedly: %v", err)
}
}
func listenUnix(path string) (net.Listener, error) {
if path != update.RequiredSocketPath {
return nil, errors.New("refusing non-standard update socket")
}
if err := os.MkdirAll(filepath.Dir(path), 0o750); err != nil {
return nil, err
}
if info, err := os.Lstat(path); err == nil {
if info.Mode()&os.ModeSocket == 0 {
return nil, errors.New("update socket path exists and is not a socket")
}
connection, dialErr := net.DialTimeout("unix", path, 250*time.Millisecond)
if dialErr == nil {
_ = connection.Close()
return nil, errors.New("another update helper is already listening")
}
if err := os.Remove(path); err != nil {
return nil, fmt.Errorf("remove stale update socket: %w", err)
}
} else if !errors.Is(err, os.ErrNotExist) {
return nil, err
}
listener, err := net.Listen("unix", path)
if err != nil {
return nil, err
}
if err := os.Chmod(path, 0o660); err != nil {
_ = listener.Close()
_ = os.Remove(path)
return nil, err
}
return listener, nil
}

@ -1,4 +1,5 @@
// Package protocol implements the authenticated local helper wire protocol.
// Package protocol implements the authenticated wire protocol shared by PHP
// and the Agent Gateway.
package protocol
import (
@ -36,7 +37,7 @@ var (
)
// Actor is deliberately small. Public callers never choose service identities;
// each local helper pins the exact service value it accepts.
// each authenticated endpoint pins the exact service value it accepts.
type Actor struct {
UserID int64 `json:"user_id,omitempty"`
IsAdmin bool `json:"is_admin,omitempty"`

@ -27,23 +27,23 @@ func TestAuthenticateBindsRequestAndRejectsReplay(t *testing.T) {
t.Fatal(err)
}
authenticator.SetClockForTesting(func() time.Time { return now })
body := []byte(`{"expected_current_commit":"abc"}`)
request, _ := http.NewRequest(http.MethodPost, "http://unix/v1/apply?dry=false", bytes.NewReader(body))
actor := Actor{UserID: 1, IsAdmin: true, IsSuperAdmin: true, CanManage: true}
body := []byte(`{"agent_id":"12","type":"request","id":"request_12345678","method":"GET","request_target":"/v1/health","body":"","headers":{}}`)
request, _ := http.NewRequest(http.MethodPost, "http://127.0.0.1/internal/agent/request?trace=false", bytes.NewReader(body))
actor := Actor{Service: "php-panel"}
if err := Sign(request, body, actor, secret, now, "0123456789abcdef0123456789abcdef"); err != nil {
t.Fatal(err)
}
got, err := authenticator.Authenticate(request, body)
if err != nil || !got.IsSuperAdmin {
if err != nil || got.Service != "php-panel" {
t.Fatalf("valid request rejected: actor=%#v err=%v", got, err)
}
if _, err := authenticator.Authenticate(request, body); !errors.Is(err, ErrReplay) {
t.Fatalf("replay error = %v", err)
}
request, _ = http.NewRequest(http.MethodPost, "http://unix/v1/apply?dry=false", bytes.NewReader(body))
request, _ = http.NewRequest(http.MethodPost, "http://127.0.0.1/internal/agent/request?trace=false", bytes.NewReader(body))
_ = Sign(request, body, actor, secret, now, "fedcba9876543210fedcba9876543210")
request.URL.RawQuery = "dry=true"
request.URL.RawQuery = "trace=true"
if _, err := authenticator.Authenticate(request, body); !errors.Is(err, ErrAuth) {
t.Fatalf("changed target error = %v", err)
}
@ -52,8 +52,8 @@ func TestAuthenticateBindsRequestAndRejectsReplay(t *testing.T) {
func TestSignUsesBodyDigest(t *testing.T) {
secret := []byte("0123456789abcdef0123456789abcdef")
now := time.Unix(2_000_000_000, 0)
request, _ := http.NewRequest(http.MethodPut, "http://unix/v1/settings", nil)
if err := Sign(request, []byte("one"), Actor{UserID: 1}, secret, now, "0123456789abcdef"); err != nil {
request, _ := http.NewRequest(http.MethodPost, "http://127.0.0.1/api/internal/cloud/session-introspection", nil)
if err := Sign(request, []byte("one"), Actor{Service: "agent-gateway"}, secret, now, "0123456789abcdef"); err != nil {
t.Fatal(err)
}
authenticator, _ := NewAuthenticator(secret, time.Minute, 2*time.Minute, testNonceStore{})
@ -73,8 +73,8 @@ func TestDecodeActorRejectsTrailingJSON(t *testing.T) {
func TestAuthenticateRejectsDuplicateSecurityHeaders(t *testing.T) {
secret := []byte("0123456789abcdef0123456789abcdef")
now := time.Unix(2_000_000_000, 0)
request, _ := http.NewRequest(http.MethodGet, "http://unix/v1/settings", nil)
if err := Sign(request, nil, Actor{UserID: 1}, secret, now, "0123456789abcdef"); err != nil {
request, _ := http.NewRequest(http.MethodPost, "http://127.0.0.1/api/internal/cloud/agent-auth", nil)
if err := Sign(request, nil, Actor{Service: "agent-gateway"}, secret, now, "0123456789abcdef"); err != nil {
t.Fatal(err)
}
request.Header.Add(HeaderTimestamp, request.Header.Get(HeaderTimestamp))

@ -1,135 +0,0 @@
package update
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"net/url"
"os"
"path/filepath"
"regexp"
"strings"
)
const (
RequiredSocketPath = "/run/virt-cloud/update.sock"
RequiredRemoteURL = "https://g.luoli.live/root/virt.git"
RequiredBranch = "go"
maxConfigBytes = 64 << 10
)
var unitPattern = regexp.MustCompile(`^[A-Za-z0-9][A-Za-z0-9_.@:-]{0,127}\.service$`)
type Config struct {
SocketPath string `json:"socket_path"`
RepositoryDir string `json:"repository_dir"`
RemoteURL string `json:"remote_url"`
Branch string `json:"branch"`
StatePath string `json:"state_path"`
ServiceSecret string `json:"service_secret"`
PHPCommand string `json:"php_command"`
PHPFPMUnit string `json:"php_fpm_unit"`
ServiceUnits []string `json:"service_units"`
}
func LoadConfig(path string) (Config, error) {
file, err := os.Open(path)
if err != nil {
return Config{}, fmt.Errorf("open update configuration: %w", err)
}
defer file.Close()
raw, err := io.ReadAll(io.LimitReader(file, maxConfigBytes+1))
if err != nil {
return Config{}, fmt.Errorf("read update configuration: %w", err)
}
if len(raw) > maxConfigBytes {
return Config{}, errors.New("update configuration exceeds 65536 bytes")
}
decoder := json.NewDecoder(bytes.NewReader(raw))
decoder.DisallowUnknownFields()
var config Config
if err := decoder.Decode(&config); err != nil {
return Config{}, fmt.Errorf("decode update configuration: %w", err)
}
if err := requireEOF(decoder); err != nil {
return Config{}, err
}
if err := config.Validate(); err != nil {
return Config{}, err
}
return config, nil
}
func requireEOF(decoder *json.Decoder) error {
var extra any
if err := decoder.Decode(&extra); !errors.Is(err, io.EOF) {
if err == nil {
return errors.New("configuration contains multiple JSON values")
}
return fmt.Errorf("configuration has trailing data: %w", err)
}
return nil
}
func (c Config) Validate() error {
var problems []string
if c.SocketPath != RequiredSocketPath {
problems = append(problems, "socket_path must be "+RequiredSocketPath)
}
if c.RemoteURL != RequiredRemoteURL {
problems = append(problems, "remote_url must identify the fixed Virt Cloud repository")
}
if c.Branch != RequiredBranch {
problems = append(problems, "branch must be go")
}
if err := safeAbsolutePath(c.RepositoryDir); err != nil {
problems = append(problems, "repository_dir "+err.Error())
}
if err := safeAbsolutePath(c.StatePath); err != nil {
problems = append(problems, "state_path "+err.Error())
} else if c.RepositoryDir != "" && c.StatePath != filepath.Join(c.RepositoryDir, "runtime", "update", "state.json") {
problems = append(problems, "state_path must be repository_dir/runtime/update/state.json")
}
if len(c.ServiceSecret) < 32 || strings.ContainsAny(c.ServiceSecret, "\x00\r\n") {
problems = append(problems, "service_secret must contain at least 32 non-line-breaking bytes")
}
if err := safeAbsolutePath(c.PHPCommand); err != nil {
problems = append(problems, "php_command "+err.Error())
}
if !unitPattern.MatchString(c.PHPFPMUnit) {
problems = append(problems, "php_fpm_unit is invalid")
}
if len(c.ServiceUnits) == 0 || len(c.ServiceUnits) > 32 {
problems = append(problems, "service_units must contain between 1 and 32 units")
}
seen := map[string]bool{}
for _, unit := range c.ServiceUnits {
if !unitPattern.MatchString(unit) || unit == c.PHPFPMUnit || seen[unit] || unit == "virt-cloud-update.service" {
problems = append(problems, "service_units contains an invalid, duplicate, or self-referential unit")
break
}
seen[unit] = true
}
parsed, err := url.Parse(c.RemoteURL)
if err != nil || parsed.Scheme != "https" || parsed.Hostname() != "g.luoli.live" || parsed.User != nil || parsed.RawQuery != "" || parsed.Fragment != "" {
problems = append(problems, "remote_url is not the approved HTTPS repository")
}
if len(problems) != 0 {
return errors.New(strings.Join(problems, "; "))
}
return nil
}
func safeAbsolutePath(value string) error {
if value == "" || !filepath.IsAbs(value) || filepath.Clean(value) != value || value == string(filepath.Separator) || strings.ContainsAny(value, "\x00\r\n") {
return errors.New("must be a clean absolute non-root path")
}
return nil
}
func pathWithin(parent, child string) bool {
relative, err := filepath.Rel(parent, child)
return err == nil && relative != "." && relative != ".." && !strings.HasPrefix(relative, ".."+string(filepath.Separator))
}

@ -1,89 +0,0 @@
package update
import (
"os"
"path/filepath"
"strings"
"testing"
)
func validTestConfig(directory string) Config {
return Config{
SocketPath: RequiredSocketPath, RepositoryDir: directory, RemoteURL: RequiredRemoteURL,
Branch: RequiredBranch, StatePath: filepath.Join(directory, "runtime", "update", "state.json"),
ServiceSecret: strings.Repeat("s", 32), PHPCommand: "/usr/bin/php", PHPFPMUnit: "php-fpm.service",
ServiceUnits: []string{"virt-cloud-worker.service", "virt-cloud-agent-gateway.service"},
}
}
func TestLoadConfigDoesNotRejectFileMode(t *testing.T) {
directory := t.TempDir()
path := filepath.Join(directory, "update.json")
config := `{"socket_path":"/run/virt-cloud/update.sock","repository_dir":"` + directory + `","remote_url":"https://g.luoli.live/root/virt.git","branch":"go","state_path":"` + filepath.Join(directory, "runtime/update/state.json") + `","service_secret":"0123456789abcdef0123456789abcdef","php_command":"/usr/bin/php","php_fpm_unit":"php-fpm.service","service_units":["virt-cloud-worker.service"]}`
if err := os.WriteFile(path, []byte(config), 0o644); err != nil {
t.Fatal(err)
}
if _, err := LoadConfig(path); err != nil {
t.Fatalf("readable 0644 configuration rejected: %v", err)
}
}
func TestLoadConfigRejectsDataPastSizeLimit(t *testing.T) {
path := filepath.Join(t.TempDir(), "update.json")
raw := []byte(`{}` + strings.Repeat(" ", maxConfigBytes))
if err := os.WriteFile(path, raw, 0o644); err != nil {
t.Fatal(err)
}
if _, err := LoadConfig(path); err == nil || !strings.Contains(err.Error(), "exceeds") {
t.Fatalf("oversized update configuration error = %v", err)
}
}
func TestConfigIsStrictAndFixed(t *testing.T) {
config := validTestConfig(t.TempDir())
config.Branch = "dev"
if err := config.Validate(); err == nil || !strings.Contains(err.Error(), "branch must be go") {
t.Fatalf("wrong branch error = %v", err)
}
config = validTestConfig(t.TempDir())
config.StatePath = "/tmp/outside-state.json"
if err := config.Validate(); err == nil || !strings.Contains(err.Error(), "repository_dir/runtime/update/state.json") {
t.Fatalf("outside state error = %v", err)
}
}
func TestOpenConfiguredStateRejectsRuntimeSymlinkEscape(t *testing.T) {
repository := filepath.Join(t.TempDir(), "repository")
outside := filepath.Join(t.TempDir(), "outside")
if err := os.MkdirAll(repository, 0o755); err != nil {
t.Fatal(err)
}
if err := os.MkdirAll(outside, 0o755); err != nil {
t.Fatal(err)
}
if err := os.Symlink(outside, filepath.Join(repository, "runtime")); err != nil {
t.Fatal(err)
}
config := validTestConfig(repository)
if _, err := OpenConfiguredState(config); err == nil || !strings.Contains(err.Error(), "escapes") {
t.Fatalf("runtime symlink escape error = %v", err)
}
}
func TestOpenConfiguredStateRejectsStateFileSymlink(t *testing.T) {
repository := filepath.Join(t.TempDir(), "repository")
stateDirectory := filepath.Join(repository, "runtime", "update")
if err := os.MkdirAll(stateDirectory, 0o755); err != nil {
t.Fatal(err)
}
target := filepath.Join(t.TempDir(), "outside-state.json")
if err := os.WriteFile(target, []byte(`{}`), 0o600); err != nil {
t.Fatal(err)
}
if err := os.Symlink(target, filepath.Join(stateDirectory, "state.json")); err != nil {
t.Fatal(err)
}
if _, err := OpenConfiguredState(validTestConfig(repository)); err == nil || !strings.Contains(err.Error(), "regular file") {
t.Fatalf("state symlink error = %v", err)
}
}

@ -1,624 +0,0 @@
package update
import (
"bytes"
"context"
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"sync"
"time"
)
const maxCommandOutput = 2 << 20
var commitPattern = regexp.MustCompile(`^[0-9a-f]{40,64}$`)
type CommandExecutor interface {
Run(ctx context.Context, directory string, environment []string, command string, arguments ...string) ([]byte, error)
}
type OSCommandExecutor struct{}
func (OSCommandExecutor) Run(ctx context.Context, directory string, environment []string, command string, arguments ...string) ([]byte, error) {
if command == "git" {
arguments = append([]string{"-c", "core.hooksPath=/dev/null", "-c", "core.fsmonitor=false", "-c", "credential.helper="}, arguments...)
}
cmd := exec.CommandContext(ctx, command, arguments...)
cmd.Dir = directory
cmd.Env = commandEnvironment(environment)
var output bytes.Buffer
writer := &limitedWriter{writer: &output, remaining: maxCommandOutput}
cmd.Stdout = writer
cmd.Stderr = writer
err := cmd.Run()
return output.Bytes(), err
}
func commandEnvironment(overrides []string) []string {
values := make(map[string]string, len(os.Environ())+len(overrides))
for _, entry := range os.Environ() {
key, value, ok := strings.Cut(entry, "=")
if !ok || unsafeCommandEnvironmentKey(key) {
continue
}
values[key] = value
}
for _, entry := range overrides {
key, value, ok := strings.Cut(entry, "=")
if ok && key != "" {
values[key] = value
}
}
result := make([]string, 0, len(values))
for key, value := range values {
result = append(result, key+"="+value)
}
return result
}
func unsafeCommandEnvironmentKey(key string) bool {
if strings.HasPrefix(key, "GIT_CONFIG_KEY_") || strings.HasPrefix(key, "GIT_CONFIG_VALUE_") ||
strings.HasPrefix(key, "DYLD_") {
return true
}
switch key {
case "GIT_DIR", "GIT_WORK_TREE", "GIT_INDEX_FILE", "GIT_OBJECT_DIRECTORY",
"GIT_ALTERNATE_OBJECT_DIRECTORIES", "GIT_CONFIG", "GIT_CONFIG_COUNT",
"GIT_SSH", "GIT_SSH_COMMAND", "GIT_PROXY_COMMAND", "LD_PRELOAD":
return true
default:
return false
}
}
type limitedWriter struct {
mu sync.Mutex
writer *bytes.Buffer
remaining int
}
func (w *limitedWriter) Write(value []byte) (int, error) {
w.mu.Lock()
defer w.mu.Unlock()
original := len(value)
if w.remaining > 0 {
amount := len(value)
if amount > w.remaining {
amount = w.remaining
}
_, _ = w.writer.Write(value[:amount])
w.remaining -= amount
}
return original, nil
}
type CheckResult struct {
Repository string `json:"repository"`
CurrentCommit string `json:"current_commit"`
RemoteCommit string `json:"remote_commit"`
UpdateAvailable bool `json:"update_available"`
Status string `json:"status"`
Branch string `json:"branch"`
MaintenanceLock bool `json:"maintenance_lock"`
}
type SettingsResult struct {
Repository string `json:"repository"`
Branch string `json:"branch"`
CurrentCommit string `json:"current_commit"`
CheckoutReady bool `json:"checkout_ready"`
CheckoutError string `json:"checkout_error,omitempty"`
TokenConfigured bool `json:"token_configured"`
ActiveJobID string `json:"active_job_id,omitempty"`
LastJobID string `json:"last_job_id,omitempty"`
MaintenanceLock bool `json:"maintenance_lock"`
}
type Manager struct {
config Config
state *StateStore
executor CommandExecutor
timeout time.Duration
healthTimeout time.Duration
healthPollInterval time.Duration
}
func NewManager(config Config, state *StateStore, executor CommandExecutor) (*Manager, error) {
if err := config.Validate(); err != nil {
return nil, err
}
if state == nil || executor == nil {
return nil, errors.New("update state and command executor are required")
}
return &Manager{
config: config,
state: state,
executor: executor,
timeout: 10 * time.Minute,
healthTimeout: 60 * time.Second,
healthPollInterval: time.Second,
}, nil
}
func (m *Manager) Check(ctx context.Context) (CheckResult, error) {
ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
defer cancel()
current, err := m.validateLocalRepository(ctx)
if err != nil {
return CheckResult{}, err
}
remote, err := m.remoteCommit(ctx)
if err != nil {
return CheckResult{}, err
}
status := "current"
if current != remote {
status = "update_available"
}
return CheckResult{Repository: RequiredRemoteURL, CurrentCommit: current, RemoteCommit: remote, UpdateAvailable: current != remote, Status: status,
Branch: RequiredBranch, MaintenanceLock: m.state.MaintenanceLocked()}, nil
}
func (m *Manager) Settings(ctx context.Context) SettingsResult {
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()
active, last := m.state.JobPointers()
result := SettingsResult{Repository: RequiredRemoteURL, Branch: RequiredBranch,
TokenConfigured: m.state.TokenConfigured(), ActiveJobID: active, LastJobID: last,
MaintenanceLock: m.state.MaintenanceLocked()}
commit, err := m.validateLocalRepository(ctx)
if err != nil {
result.CheckoutError = "仓库分支、远端或工作树状态不符合更新要求"
return result
}
result.CurrentCommit = commit
result.CheckoutReady = true
return result
}
func (m *Manager) StartApply(expectedCurrent, expectedRemote string) (Job, error) {
if !commitPattern.MatchString(expectedCurrent) || !commitPattern.MatchString(expectedRemote) {
return Job{}, errors.New("expected commits must be lowercase full object IDs")
}
if expectedCurrent == expectedRemote {
return Job{}, errors.New("remote commit does not contain an update")
}
id, err := randomID()
if err != nil {
return Job{}, err
}
job := Job{ID: id, ExpectedCurrentCommit: expectedCurrent, ExpectedRemoteCommit: expectedRemote}
if err := m.state.CreateJob(job); err != nil {
return Job{}, err
}
job, _ = m.state.Job(id)
go m.runApply(id)
return job, nil
}
func (m *Manager) runApply(id string) {
ctx, cancel := context.WithTimeout(context.Background(), m.timeout)
defer cancel()
job, ok := m.state.Job(id)
if !ok {
return
}
failSafe := func(code string) {
if err := m.state.UpdateJob(id, func(value *Job) {
value.Status = "failed"
value.ErrorCode = code
}); err != nil {
m.state.ForceManual(id, "state_persistence_failed")
}
}
failUncertain := func(code string) {
if err := m.state.UpdateJob(id, func(value *Job) {
value.Status = "manual_intervention"
value.ErrorCode = code
}); err != nil {
m.state.ForceManual(id, "state_persistence_failed")
}
}
checkpoint := func(name string) error {
err := m.state.UpdateJob(id, func(value *Job) {
value.Status = "running"
value.Checkpoint = name
})
if err != nil {
m.state.ForceManual(id, "state_persistence_failed")
}
return err
}
if err := checkpoint("validating_repository"); err != nil {
return
}
current, err := m.validateLocalRepository(ctx)
if err != nil || current != job.ExpectedCurrentCommit {
failSafe("local_repository_precondition_failed")
return
}
if err := checkpoint("fetching_expected_commit"); err != nil {
return
}
if err := m.fetch(ctx); err != nil {
failSafe("remote_fetch_failed")
return
}
remote, err := m.remoteTrackingCommit(ctx)
if err != nil || remote != job.ExpectedRemoteCommit {
failSafe("remote_commit_changed")
return
}
if err := m.ensureFastForward(ctx, current, remote); err != nil {
failSafe("non_fast_forward_update_rejected")
return
}
changed, err := m.changedPaths(ctx, current, remote)
if err != nil {
failSafe("change_inspection_failed")
return
}
if deniedChangedPath(changed) {
failSafe("manual_upgrade_required")
return
}
if err := checkpoint("preflight"); err != nil {
return
}
if err := m.preflight(ctx, remote); err != nil {
failSafe("host_preflight_failed")
return
}
if err := m.state.UpdateJob(id, func(value *Job) { value.PreviousCommit = current }); err != nil {
m.state.ForceManual(id, "state_persistence_failed")
return
}
// Every checkpoint below precedes a host mutation. A crash or any error is
// deliberately considered uncertain and holds the maintenance lock.
if err := checkpoint("stopping_services"); err != nil {
return
}
if err := m.stopServices(ctx); err != nil {
failUncertain("service_stop_requires_reconciliation")
return
}
if err := checkpoint("updating_source"); err != nil {
return
}
if _, err := m.run(ctx, nil, "git", "merge", "--ff-only", "refs/remotes/origin/"+RequiredBranch); err != nil {
failUncertain("source_update_requires_reconciliation")
return
}
if err := m.state.UpdateJob(id, func(value *Job) {
value.Status = "running"
value.Checkpoint = "source_updated"
value.AppliedCommit = remote
}); err != nil {
m.state.ForceManual(id, "state_persistence_failed")
return
}
if err := checkpoint("schema_init_started"); err != nil {
return
}
webDirectory, err := m.webDirectory()
if err != nil {
failUncertain("schema_init_result_uncertain")
return
}
if _, err := m.runAt(ctx, webDirectory, nil, m.config.PHPCommand, "xcat", "cloudInit"); err != nil {
failUncertain("schema_init_result_uncertain")
return
}
if err := checkpoint("schema_init_completed"); err != nil {
return
}
if err := checkpoint("starting_services"); err != nil {
return
}
if err := m.startServices(ctx); err != nil {
failUncertain("service_start_requires_reconciliation")
return
}
if err := checkpoint("health_checking"); err != nil {
return
}
if err := m.healthCheck(ctx); err != nil {
failUncertain("post_update_health_check_failed")
return
}
if err := m.state.UpdateJob(id, func(value *Job) {
value.Status = "succeeded"
value.Checkpoint = "completed"
value.ErrorCode = ""
}); err != nil {
m.state.ForceManual(id, "state_persistence_failed")
}
}
func (m *Manager) validateLocalRepository(ctx context.Context) (string, error) {
repository, err := filepath.EvalSymlinks(m.config.RepositoryDir)
if err != nil {
return "", errors.New("repository is unavailable")
}
root, err := m.gitLine(ctx, "rev-parse", "--show-toplevel")
if err != nil {
return "", errors.New("repository cannot be identified")
}
resolvedRoot, err := filepath.EvalSymlinks(root)
if err != nil || resolvedRoot != repository {
return "", errors.New("repository path does not match its Git root")
}
branch, err := m.gitLine(ctx, "symbolic-ref", "--short", "HEAD")
if err != nil || branch != RequiredBranch {
return "", errors.New("repository is not on the go branch")
}
remote, err := m.gitLine(ctx, "remote", "get-url", "origin")
if err != nil || remote != RequiredRemoteURL {
return "", errors.New("repository origin is not approved")
}
statePath, err := filepath.EvalSymlinks(m.config.StatePath)
if err != nil {
return "", errors.New("update state is unavailable")
}
stateRelative, err := filepath.Rel(repository, statePath)
if err != nil || strings.HasPrefix(stateRelative, ".."+string(filepath.Separator)) {
return "", errors.New("update state is outside the repository runtime directory")
}
if _, err := m.run(ctx, nil, "git", "check-ignore", "-q", "--", filepath.ToSlash(stateRelative)); err != nil {
return "", errors.New("update state path is not ignored by Git")
}
status, err := m.run(ctx, nil, "git", "status", "--porcelain=v1", "--untracked-files=normal")
if err != nil || len(status) != 0 {
return "", errors.New("repository worktree is not clean")
}
commit, err := m.gitLine(ctx, "rev-parse", "HEAD")
if err != nil || !commitPattern.MatchString(commit) {
return "", errors.New("current commit is invalid")
}
return commit, nil
}
func (m *Manager) remoteCommit(ctx context.Context) (string, error) {
environment, cleanup, err := m.gitEnvironment()
if err != nil {
return "", err
}
defer cleanup()
output, err := m.run(ctx, environment, "git", "ls-remote", "--exit-code", "--heads", RequiredRemoteURL, "refs/heads/"+RequiredBranch)
if err != nil {
return "", errors.New("remote branch check failed")
}
fields := strings.Fields(string(output))
if len(fields) != 2 || fields[1] != "refs/heads/"+RequiredBranch || !commitPattern.MatchString(fields[0]) {
return "", errors.New("remote branch response was invalid")
}
return fields[0], nil
}
func (m *Manager) fetch(ctx context.Context) error {
environment, cleanup, err := m.gitEnvironment()
if err != nil {
return err
}
defer cleanup()
_, err = m.run(ctx, environment, "git", "fetch", "--no-tags", "--prune", "origin",
"+refs/heads/"+RequiredBranch+":refs/remotes/origin/"+RequiredBranch)
return err
}
func (m *Manager) gitEnvironment() ([]string, func(), error) {
directory := filepath.Dir(m.config.StatePath)
if err := os.MkdirAll(directory, 0o700); err != nil {
return nil, func() {}, errors.New("cannot prepare Git credential helper")
}
file, err := os.CreateTemp(directory, ".git-askpass-*.sh")
if err != nil {
return nil, func() {}, errors.New("cannot prepare Git credential helper")
}
path := file.Name()
cleanup := func() { _ = os.Remove(path) }
script := "#!/bin/sh\ncase \"$1\" in\n*sername*) printf '%s\\n' 'oauth2' ;;\n*) printf '%s\\n' \"$VIRT_CLOUD_UPDATE_TOKEN\" ;;\nesac\n"
if err := file.Chmod(0o700); err != nil {
file.Close()
cleanup()
return nil, func() {}, errors.New("cannot protect Git credential helper")
}
if _, err := file.WriteString(script); err != nil {
file.Close()
cleanup()
return nil, func() {}, errors.New("cannot write Git credential helper")
}
if err := file.Close(); err != nil {
cleanup()
return nil, func() {}, errors.New("cannot finish Git credential helper")
}
return []string{"GIT_TERMINAL_PROMPT=0", "GIT_ASKPASS=" + path, "VIRT_CLOUD_UPDATE_TOKEN=" + m.state.AccessToken()}, cleanup, nil
}
func (m *Manager) remoteTrackingCommit(ctx context.Context) (string, error) {
return m.gitLine(ctx, "rev-parse", "refs/remotes/origin/"+RequiredBranch)
}
func (m *Manager) ensureFastForward(ctx context.Context, current, remote string) error {
_, err := m.run(ctx, nil, "git", "merge-base", "--is-ancestor", current, remote)
return err
}
func (m *Manager) changedPaths(ctx context.Context, current, remote string) ([]string, error) {
output, err := m.run(ctx, nil, "git", "diff", "--name-only", "-z", current+".."+remote)
if err != nil {
return nil, err
}
parts := strings.Split(string(output), "\x00")
if len(parts) > 0 && parts[len(parts)-1] == "" {
parts = parts[:len(parts)-1]
}
return parts, nil
}
func deniedChangedPath(paths []string) bool {
for _, path := range paths {
clean := filepath.ToSlash(filepath.Clean(path))
if clean == "." || clean == ".gitignore" || clean != path || strings.HasPrefix(clean, "../") || strings.HasPrefix(clean, "deploy/") {
return true
}
base := strings.ToLower(filepath.Base(clean))
if base == "go.mod" || base == "go.sum" || base == "composer.json" || base == "composer.lock" ||
base == "package.json" || base == "package-lock.json" || base == "yarn.lock" || base == "pnpm-lock.yaml" ||
base == "cargo.toml" || base == "cargo.lock" || strings.HasPrefix(base, "requirements") && strings.HasSuffix(base, ".txt") {
return true
}
}
return false
}
func (m *Manager) preflight(ctx context.Context, targetCommit string) error {
if _, err := m.webDirectory(); err != nil {
return err
}
if err := m.targetHasSchemaInitCommand(ctx, targetCommit); err != nil {
return err
}
if _, err := m.run(ctx, nil, m.config.PHPCommand, "--version"); err != nil {
return err
}
for _, unit := range append([]string{m.config.PHPFPMUnit}, m.config.ServiceUnits...) {
output, err := m.run(ctx, nil, "systemctl", "show", "--property=LoadState", "--property=ActiveState", unit)
if err != nil {
return err
}
properties := parseSystemdProperties(output)
if properties["LoadState"] != "loaded" || properties["ActiveState"] != "active" {
return errors.New("required service is not loaded and active")
}
}
return nil
}
func (m *Manager) webDirectory() (string, error) {
repository, err := filepath.EvalSymlinks(m.config.RepositoryDir)
if err != nil {
return "", errors.New("repository is unavailable")
}
webDirectory, err := filepath.EvalSymlinks(filepath.Join(m.config.RepositoryDir, "web"))
if err != nil || !pathWithin(repository, webDirectory) {
return "", errors.New("web directory is unavailable or escapes the repository")
}
xcatPath := filepath.Join(webDirectory, "xcat")
info, err := os.Lstat(xcatPath)
if err != nil || !info.Mode().IsRegular() || info.Mode()&os.ModeSymlink != 0 {
return "", errors.New("web/xcat must be a regular file")
}
return webDirectory, nil
}
func (m *Manager) targetHasSchemaInitCommand(ctx context.Context, targetCommit string) error {
output, err := m.run(ctx, nil, "git", "ls-tree", "-z", targetCommit, "--", "web/xcat")
if err != nil {
return err
}
line := strings.TrimSuffix(string(output), "\x00")
metadata, path, ok := strings.Cut(line, "\t")
fields := strings.Fields(metadata)
if !ok || path != "web/xcat" || len(fields) != 3 ||
(fields[0] != "100644" && fields[0] != "100755") || fields[1] != "blob" || !commitPattern.MatchString(fields[2]) {
return errors.New("target does not contain a regular web/xcat command")
}
return nil
}
func parseSystemdProperties(output []byte) map[string]string {
properties := make(map[string]string)
for _, line := range strings.Split(strings.TrimSpace(string(output)), "\n") {
key, value, ok := strings.Cut(strings.TrimSpace(line), "=")
if ok && key != "" {
properties[key] = value
}
}
return properties
}
func (m *Manager) stopServices(ctx context.Context) error {
for _, unit := range append(append([]string(nil), m.config.ServiceUnits...), m.config.PHPFPMUnit) {
if _, err := m.run(ctx, nil, "systemctl", "stop", unit); err != nil {
return err
}
}
return nil
}
func (m *Manager) startServices(ctx context.Context) error {
if _, err := m.run(ctx, nil, "systemctl", "start", m.config.PHPFPMUnit); err != nil {
return err
}
if err := m.waitUnitsActive(ctx, []string{m.config.PHPFPMUnit}); err != nil {
return err
}
for _, unit := range m.config.ServiceUnits {
if _, err := m.run(ctx, nil, "systemctl", "start", unit); err != nil {
return err
}
}
return nil
}
func (m *Manager) healthCheck(ctx context.Context) error {
return m.waitUnitsActive(ctx, append([]string{m.config.PHPFPMUnit}, m.config.ServiceUnits...))
}
func (m *Manager) waitUnitsActive(ctx context.Context, units []string) error {
deadline := time.Now().Add(m.healthTimeout)
for {
allActive := true
for _, unit := range units {
if _, err := m.run(ctx, nil, "systemctl", "is-active", "--quiet", unit); err != nil {
allActive = false
break
}
}
if allActive {
return nil
}
if !time.Now().Before(deadline) {
return errors.New("service health deadline exceeded")
}
timer := time.NewTimer(m.healthPollInterval)
select {
case <-ctx.Done():
timer.Stop()
return ctx.Err()
case <-timer.C:
}
}
}
func (m *Manager) gitLine(ctx context.Context, arguments ...string) (string, error) {
output, err := m.run(ctx, nil, "git", arguments...)
if err != nil {
return "", err
}
value := strings.TrimSpace(string(output))
if value == "" || strings.ContainsAny(value, "\x00\r\n") {
return "", errors.New("Git returned a non-canonical value")
}
return value, nil
}
func (m *Manager) run(ctx context.Context, environment []string, command string, arguments ...string) ([]byte, error) {
return m.runAt(ctx, m.config.RepositoryDir, environment, command, arguments...)
}
func (m *Manager) runAt(ctx context.Context, directory string, environment []string, command string, arguments ...string) ([]byte, error) {
output, err := m.executor.Run(ctx, directory, environment, command, arguments...)
if err != nil {
return nil, fmt.Errorf("update command failed")
}
return output, nil
}

@ -1,252 +0,0 @@
package update
import (
"context"
"errors"
"os"
"path/filepath"
"strings"
"sync"
"testing"
"time"
)
const (
testCurrent = "1111111111111111111111111111111111111111"
testRemote = "2222222222222222222222222222222222222222"
)
type recordedCommand struct {
directory string
command string
args []string
env []string
}
type fakeExecutor struct {
directory string
changed string
failMatch string
mu sync.Mutex
calls []recordedCommand
}
func (f *fakeExecutor) Run(_ context.Context, directory string, environment []string, command string, arguments ...string) ([]byte, error) {
f.mu.Lock()
f.calls = append(f.calls, recordedCommand{directory: directory, command: command, args: append([]string(nil), arguments...), env: append([]string(nil), environment...)})
f.mu.Unlock()
joined := command + " " + strings.Join(arguments, " ")
if f.failMatch != "" && strings.Contains(joined, f.failMatch) {
return nil, errors.New("sensitive fake command diagnostic")
}
if command == "git" {
switch {
case strings.HasPrefix(strings.Join(arguments, " "), "rev-parse --show-toplevel"):
return []byte(f.directory + "\n"), nil
case strings.HasPrefix(strings.Join(arguments, " "), "symbolic-ref"):
return []byte("go\n"), nil
case strings.HasPrefix(strings.Join(arguments, " "), "remote get-url"):
return []byte(RequiredRemoteURL + "\n"), nil
case strings.HasPrefix(strings.Join(arguments, " "), "status"):
return nil, nil
case strings.Join(arguments, " ") == "rev-parse HEAD":
return []byte(testCurrent + "\n"), nil
case strings.HasPrefix(strings.Join(arguments, " "), "ls-remote"):
return []byte(testRemote + "\trefs/heads/go\n"), nil
case strings.Join(arguments, " ") == "rev-parse refs/remotes/origin/go":
return []byte(testRemote + "\n"), nil
case strings.HasPrefix(strings.Join(arguments, " "), "diff --name-only"):
return []byte(f.changed), nil
case strings.HasPrefix(strings.Join(arguments, " "), "ls-tree -z"):
return []byte("100644 blob 3333333333333333333333333333333333333333\tweb/xcat\x00"), nil
default:
return nil, nil
}
}
if command == "systemctl" && len(arguments) > 0 && arguments[0] == "show" {
return []byte("LoadState=loaded\nActiveState=active\n"), nil
}
return nil, nil
}
func newTestManager(t *testing.T, executor *fakeExecutor) (*Manager, *StateStore) {
t.Helper()
directory := t.TempDir()
executor.directory = directory
if err := os.MkdirAll(filepath.Join(directory, "web"), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(directory, "web", "xcat"), []byte("<?php\n"), 0o644); err != nil {
t.Fatal(err)
}
config := validTestConfig(directory)
state, err := OpenState(config.StatePath)
if err != nil {
t.Fatal(err)
}
manager, err := NewManager(config, state, executor)
if err != nil {
t.Fatal(err)
}
return manager, state
}
func waitJob(t *testing.T, state *StateStore, id string) Job {
t.Helper()
deadline := time.Now().Add(3 * time.Second)
for time.Now().Before(deadline) {
job, _ := state.Job(id)
if job.Status == "succeeded" || job.Status == "failed" || job.Status == "manual_intervention" {
return job
}
time.Sleep(5 * time.Millisecond)
}
job, _ := state.Job(id)
t.Fatalf("job did not finish: %#v", job)
return Job{}
}
func TestCheckIsReadOnlyAndDoesNotPutTokenInArguments(t *testing.T) {
executor := &fakeExecutor{}
manager, state := newTestManager(t, executor)
if err := state.SetAccessToken("private-token-value"); err != nil {
t.Fatal(err)
}
result, err := manager.Check(context.Background())
if err != nil || result.CurrentCommit != testCurrent || result.RemoteCommit != testRemote || !result.UpdateAvailable ||
result.Status != "update_available" || result.Repository != RequiredRemoteURL {
t.Fatalf("check result=%#v err=%v", result, err)
}
for _, call := range executor.calls {
if strings.Contains(strings.Join(call.args, " "), "private-token-value") {
t.Fatal("access token was placed in process arguments")
}
if call.command == "git" && len(call.args) > 0 && call.args[0] == "fetch" {
t.Fatal("read-only check fetched into the repository")
}
}
}
func TestApplyRunsCheckpointedFastForward(t *testing.T) {
executor := &fakeExecutor{changed: "web/app.php\x00gateway/update/server.go\x00"}
manager, state := newTestManager(t, executor)
expectedWebDirectory, err := filepath.EvalSymlinks(filepath.Join(executor.directory, "web"))
if err != nil {
t.Fatal(err)
}
job, err := manager.StartApply(testCurrent, testRemote)
if err != nil {
t.Fatal(err)
}
job = waitJob(t, state, job.ID)
if job.Status != "succeeded" || job.Checkpoint != "completed" || job.PreviousCommit != testCurrent || job.AppliedCommit != testRemote {
t.Fatalf("unexpected completed job: %#v", job)
}
for _, call := range executor.calls {
if call.command == "/usr/bin/php" && strings.Join(call.args, " ") == "xcat cloudInit" &&
call.directory != expectedWebDirectory {
t.Fatalf("schema initialization ran in %q instead of web directory", call.directory)
}
}
}
func TestApplyRejectsDependencyAndDeployChangesBeforeStopping(t *testing.T) {
for _, changed := range []string{"deploy/virt-cloud.service\x00", "gateway/go.sum\x00", "web/composer.lock\x00"} {
executor := &fakeExecutor{changed: changed}
manager, state := newTestManager(t, executor)
job, err := manager.StartApply(testCurrent, testRemote)
if err != nil {
t.Fatal(err)
}
job = waitJob(t, state, job.ID)
if job.Status != "failed" || job.ErrorCode != "manual_upgrade_required" {
t.Fatalf("changed=%q job=%#v", changed, job)
}
for _, call := range executor.calls {
if call.command == "systemctl" {
t.Fatalf("services were touched for denied change %q", changed)
}
}
}
}
func TestSchemaInitializationFailureHoldsMaintenanceLock(t *testing.T) {
executor := &fakeExecutor{changed: "web/app.php\x00", failMatch: "/usr/bin/php xcat cloudInit"}
manager, state := newTestManager(t, executor)
job, err := manager.StartApply(testCurrent, testRemote)
if err != nil {
t.Fatal(err)
}
job = waitJob(t, state, job.ID)
if job.Status != "manual_intervention" || job.ErrorCode != "schema_init_result_uncertain" || !state.MaintenanceLocked() {
t.Fatalf("schema initialization failure was not fail-closed: %#v lock=%v", job, state.MaintenanceLocked())
}
if _, err := manager.StartApply(testCurrent, testRemote); err == nil {
t.Fatal("new apply accepted while manual reconciliation is required")
}
}
func TestPreflightFailureStopsBeforeAnyMutation(t *testing.T) {
executor := &fakeExecutor{changed: "web/app.php\x00", failMatch: "systemctl show"}
manager, state := newTestManager(t, executor)
job, err := manager.StartApply(testCurrent, testRemote)
if err != nil {
t.Fatal(err)
}
job = waitJob(t, state, job.ID)
if job.Status != "failed" || job.ErrorCode != "host_preflight_failed" || state.MaintenanceLocked() {
t.Fatalf("preflight failure was not a safe failure: %#v lock=%v", job, state.MaintenanceLocked())
}
for _, call := range executor.calls {
if call.command == "systemctl" && len(call.args) > 0 && (call.args[0] == "stop" || call.args[0] == "start") {
t.Fatalf("preflight failure mutated service state: %#v", call)
}
if call.command == "git" && len(call.args) > 0 && call.args[0] == "merge" {
t.Fatalf("preflight failure mutated the checkout: %#v", call)
}
}
}
func TestPreflightRejectsSchemaInitializerSymlink(t *testing.T) {
executor := &fakeExecutor{changed: "web/app.php\x00"}
manager, state := newTestManager(t, executor)
xcat := filepath.Join(executor.directory, "web", "xcat")
if err := os.Remove(xcat); err != nil {
t.Fatal(err)
}
if err := os.Symlink("elsewhere", xcat); err != nil {
t.Fatal(err)
}
job, err := manager.StartApply(testCurrent, testRemote)
if err != nil {
t.Fatal(err)
}
job = waitJob(t, state, job.ID)
if job.Status != "failed" || job.ErrorCode != "host_preflight_failed" {
t.Fatalf("schema initializer symlink was not rejected before mutation: %#v", job)
}
}
func TestApplyRejectsNoOpCommitPair(t *testing.T) {
manager, _ := newTestManager(t, &fakeExecutor{})
if _, err := manager.StartApply(testCurrent, testCurrent); err == nil {
t.Fatal("no-op update was accepted")
}
}
func TestCommandEnvironmentDropsGitAndLoaderInjection(t *testing.T) {
t.Setenv("GIT_DIR", "/attacker/repository")
t.Setenv("GIT_CONFIG_KEY_0", "core.hooksPath")
t.Setenv("GIT_CONFIG_VALUE_0", "/attacker/hooks")
t.Setenv("DYLD_INSERT_LIBRARIES", "/attacker/library")
values := commandEnvironment([]string{"GIT_TERMINAL_PROMPT=0"})
joined := strings.Join(values, "\n")
for _, forbidden := range []string{"GIT_DIR=", "GIT_CONFIG_KEY_0=", "GIT_CONFIG_VALUE_0=", "DYLD_INSERT_LIBRARIES="} {
if strings.Contains(joined, forbidden) {
t.Fatalf("unsafe environment survived: %s", forbidden)
}
}
if !strings.Contains(joined, "GIT_TERMINAL_PROMPT=0") {
t.Fatal("explicit safe override was lost")
}
}

@ -1,311 +0,0 @@
package update
import (
"bytes"
"crypto/rand"
"encoding/base64"
"encoding/json"
"errors"
"io"
"net/http"
"os"
"regexp"
"strings"
"time"
"git.luoli.live/root/virt/gateway/internal/protocol"
)
const maxAPIRequestBody = 64 << 10
var jobIDPattern = regexp.MustCompile(`^[A-Za-z0-9_-]{32}$`)
type Server struct {
manager *Manager
state *StateStore
authenticator *protocol.Authenticator
}
type jobResponse struct {
JobID string `json:"job_id"`
Status string `json:"status"`
Phase string `json:"phase"`
Error string `json:"error,omitempty"`
ErrorCode string `json:"error_code,omitempty"`
NewCommit string `json:"new_commit,omitempty"`
RollbackRef string `json:"rollback_ref,omitempty"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
func NewServer(manager *Manager, state *StateStore, secret []byte) (*Server, error) {
if manager == nil || state == nil {
return nil, errors.New("update manager and state are required")
}
authenticator, err := protocol.NewAuthenticator(secret, 60*time.Second, 2*time.Minute, state)
if err != nil {
return nil, err
}
return &Server{manager: manager, state: state, authenticator: authenticator}, nil
}
func (s *Server) ServeHTTP(response http.ResponseWriter, request *http.Request) {
response.Header().Set("Content-Type", "application/json; charset=utf-8")
response.Header().Set("Cache-Control", "no-store")
response.Header().Set("X-Content-Type-Options", "nosniff")
body, err := readRequestBody(response, request)
if err != nil {
writeAPIError(response, http.StatusBadRequest, "invalid_request")
return
}
actor, err := s.authenticator.Authenticate(request, body)
if err != nil {
if errors.Is(err, protocol.ErrNonceStore) {
writeAPIError(response, http.StatusServiceUnavailable, "authentication_state_unavailable")
return
}
writeAPIError(response, http.StatusUnauthorized, "authentication_failed")
return
}
if !actor.IsAdmin || !actor.IsSuperAdmin || !actor.CanManage || actor.UserID <= 0 || actor.Service != "" {
writeAPIError(response, http.StatusForbidden, "super_admin_required")
return
}
s.route(response, request, body)
}
func (s *Server) route(response http.ResponseWriter, request *http.Request, body []byte) {
if request.URL.RawQuery != "" {
writeAPIError(response, http.StatusNotFound, "not_found")
return
}
switch {
case request.URL.Path == "/v1/health" && request.Method == http.MethodGet:
writeJSON(response, http.StatusOK, map[string]any{"status": "ok", "maintenance_lock": s.state.MaintenanceLocked()})
case request.URL.Path == "/v1/settings" && request.Method == http.MethodGet:
writeJSON(response, http.StatusOK, s.manager.Settings(request.Context()))
case request.URL.Path == "/v1/settings" && request.Method == http.MethodPut:
s.updateSettings(response, request, body)
case request.URL.Path == "/v1/settings/token" && request.Method == http.MethodDelete:
if len(bytes.TrimSpace(body)) != 0 {
writeAPIError(response, http.StatusBadRequest, "body_not_allowed")
return
}
if err := s.state.SetAccessToken(""); err != nil {
writeAPIError(response, http.StatusServiceUnavailable, "state_unavailable")
return
}
writeJSON(response, http.StatusOK, s.manager.Settings(request.Context()))
case request.URL.Path == "/v1/check" && request.Method == http.MethodPost:
if !emptyJSONObject(body) {
writeAPIError(response, http.StatusBadRequest, "invalid_request")
return
}
result, err := s.manager.Check(request.Context())
if err != nil {
writeAPIError(response, http.StatusConflict, "update_check_failed")
return
}
writeJSON(response, http.StatusOK, result)
case request.URL.Path == "/v1/apply" && request.Method == http.MethodPost:
s.apply(response, body)
case request.URL.Path == "/v1/jobs" && request.Method == http.MethodGet:
jobs := s.state.Jobs()
result := make([]jobResponse, 0, len(jobs))
for _, job := range jobs {
result = append(result, publicJob(job))
}
writeJSON(response, http.StatusOK, map[string]any{"jobs": result, "maintenance_lock": s.state.MaintenanceLocked()})
case strings.HasPrefix(request.URL.Path, "/v1/jobs/"):
s.job(response, request, body)
default:
writeAPIError(response, http.StatusNotFound, "not_found")
}
}
func (s *Server) updateSettings(response http.ResponseWriter, request *http.Request, body []byte) {
var input struct {
AccessToken *string `json:"access_token"`
ClearToken *bool `json:"clear_token"`
}
if err := decodeJSONObject(body, &input); err != nil || (input.AccessToken == nil && input.ClearToken == nil) ||
(input.ClearToken != nil && *input.ClearToken && input.AccessToken != nil && *input.AccessToken != "") {
writeAPIError(response, http.StatusUnprocessableEntity, "invalid_access_token")
return
}
if input.ClearToken != nil && *input.ClearToken {
if err := s.state.SetAccessToken(""); err != nil {
writeAPIError(response, http.StatusServiceUnavailable, "state_unavailable")
return
}
} else if input.AccessToken != nil && *input.AccessToken != "" {
if !validAccessToken(*input.AccessToken) {
writeAPIError(response, http.StatusUnprocessableEntity, "invalid_access_token")
return
}
if err := s.state.SetAccessToken(*input.AccessToken); err != nil {
writeAPIError(response, http.StatusServiceUnavailable, "state_unavailable")
return
}
} else if input.AccessToken == nil {
writeAPIError(response, http.StatusUnprocessableEntity, "invalid_access_token")
return
}
if input.ClearToken != nil && !*input.ClearToken && input.AccessToken == nil {
writeAPIError(response, http.StatusUnprocessableEntity, "invalid_access_token")
return
}
writeJSON(response, http.StatusOK, s.manager.Settings(request.Context()))
}
func (s *Server) apply(response http.ResponseWriter, body []byte) {
var input struct {
ExpectedCurrentCommit string `json:"expected_current_commit"`
ExpectedRemoteCommit string `json:"expected_remote_commit"`
}
if err := decodeJSONObject(body, &input); err != nil {
writeAPIError(response, http.StatusBadRequest, "invalid_request")
return
}
job, err := s.manager.StartApply(input.ExpectedCurrentCommit, input.ExpectedRemoteCommit)
if err != nil {
writeAPIError(response, http.StatusConflict, "update_precondition_failed")
return
}
writeJSON(response, http.StatusAccepted, publicJob(job))
}
func (s *Server) job(response http.ResponseWriter, request *http.Request, body []byte) {
remainder := strings.TrimPrefix(request.URL.Path, "/v1/jobs/")
parts := strings.Split(remainder, "/")
if len(parts) == 1 && request.Method == http.MethodGet && jobIDPattern.MatchString(parts[0]) {
job, ok := s.state.Job(parts[0])
if !ok {
writeAPIError(response, http.StatusNotFound, "job_not_found")
return
}
writeJSON(response, http.StatusOK, publicJob(job))
return
}
if len(parts) == 2 && parts[1] == "resolve" && request.Method == http.MethodPost && jobIDPattern.MatchString(parts[0]) {
var input struct {
Resolution string `json:"resolution"`
Evidence string `json:"evidence"`
}
if err := decodeJSONObject(body, &input); err != nil ||
(input.Resolution != "completed" && input.Resolution != "rolled_back") ||
!validEvidence(input.Evidence) {
writeAPIError(response, http.StatusUnprocessableEntity, "invalid_resolution")
return
}
if err := s.state.ResolveJob(parts[0], input.Resolution, input.Evidence); err != nil {
if errors.Is(err, os.ErrNotExist) {
writeAPIError(response, http.StatusNotFound, "job_not_found")
} else {
writeAPIError(response, http.StatusConflict, "job_not_reconcilable")
}
return
}
job, _ := s.state.Job(parts[0])
writeJSON(response, http.StatusOK, publicJob(job))
return
}
writeAPIError(response, http.StatusNotFound, "not_found")
}
func publicJob(job Job) jobResponse {
status := job.Status
if status == "manual_intervention" {
status = "blocked"
} else if status == "resolved" {
if job.Resolution == "completed" {
status = "succeeded"
} else {
status = "failed"
}
}
result := jobResponse{JobID: job.ID, Status: status, Phase: job.Checkpoint,
ErrorCode: job.ErrorCode, NewCommit: job.AppliedCommit, RollbackRef: job.PreviousCommit,
CreatedAt: job.CreatedAt, UpdatedAt: job.UpdatedAt}
if job.Status == "manual_intervention" {
result.Error = "更新状态不确定,必须通过 SSH 核对后再处理"
} else if job.Status == "failed" {
result.Error = "更新已在主机变更前安全终止"
}
return result
}
func readRequestBody(response http.ResponseWriter, request *http.Request) ([]byte, error) {
request.Body = http.MaxBytesReader(response, request.Body, maxAPIRequestBody)
body, err := io.ReadAll(request.Body)
if err != nil {
return nil, err
}
return body, nil
}
func decodeJSONObject(body []byte, destination any) error {
if len(bytes.TrimSpace(body)) == 0 {
return errors.New("JSON object required")
}
decoder := json.NewDecoder(bytes.NewReader(body))
decoder.DisallowUnknownFields()
if err := decoder.Decode(destination); err != nil {
return err
}
var extra any
if err := decoder.Decode(&extra); !errors.Is(err, io.EOF) {
return errors.New("trailing JSON data")
}
return nil
}
func emptyJSONObject(body []byte) bool {
if len(bytes.TrimSpace(body)) == 0 {
return true
}
var value map[string]any
return decodeJSONObject(body, &value) == nil && len(value) == 0
}
func validAccessToken(value string) bool {
if len(value) < 8 || len(value) > 4096 {
return false
}
for _, character := range value {
if character < 0x21 || character == 0x7f {
return false
}
}
return true
}
func validEvidence(value string) bool {
value = strings.TrimSpace(value)
if len(value) < 8 || len(value) > 500 || !strings.Contains(value, ":") {
return false
}
for _, character := range value {
if character < 0x20 || character == 0x7f {
return false
}
}
return true
}
func randomID() (string, error) {
random := make([]byte, 24)
if _, err := rand.Read(random); err != nil {
return "", err
}
return base64.RawURLEncoding.EncodeToString(random), nil
}
func writeAPIError(response http.ResponseWriter, status int, code string) {
writeJSON(response, status, map[string]any{"error": map[string]string{"code": code}})
}
func writeJSON(response http.ResponseWriter, status int, value any) {
response.WriteHeader(status)
_ = json.NewEncoder(response).Encode(value)
}

@ -1,82 +0,0 @@
package update
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
"time"
"git.luoli.live/root/virt/gateway/internal/protocol"
)
func TestSettingsNeverEchoAccessTokenAndRejectReplay(t *testing.T) {
executor := &fakeExecutor{}
manager, state := newTestManager(t, executor)
secret := []byte(strings.Repeat("x", 32))
server, err := NewServer(manager, state, secret)
if err != nil {
t.Fatal(err)
}
token := "glpat-this-must-never-appear-in-response"
body := []byte(`{"access_token":"` + token + `"}`)
request := signedAPIRequest(t, http.MethodPut, "/v1/settings", body, secret, "0123456789abcdef0123456789abcdef")
response := httptest.NewRecorder()
server.ServeHTTP(response, request)
if response.Code != http.StatusOK || strings.Contains(response.Body.String(), token) {
t.Fatalf("settings response status=%d body=%q", response.Code, response.Body.String())
}
var payload map[string]any
if err := json.Unmarshal(response.Body.Bytes(), &payload); err != nil || payload["token_configured"] != true || payload["repository"] != RequiredRemoteURL {
t.Fatalf("settings payload=%#v err=%v", payload, err)
}
replay := httptest.NewRecorder()
server.ServeHTTP(replay, request)
if replay.Code != http.StatusUnauthorized {
t.Fatalf("replay status=%d", replay.Code)
}
raw, err := os.ReadFile(state.path)
if err != nil || !bytes.Contains(raw, []byte(token)) {
t.Fatal("token was not durably stored")
}
}
func TestSettingsBlankPreservesAndClearTokenRemovesCredential(t *testing.T) {
executor := &fakeExecutor{}
manager, state := newTestManager(t, executor)
secret := []byte(strings.Repeat("x", 32))
server, _ := NewServer(manager, state, secret)
if err := state.SetAccessToken("existing-private-token"); err != nil {
t.Fatal(err)
}
preserveBody := []byte(`{"access_token":""}`)
preserve := httptest.NewRecorder()
server.ServeHTTP(preserve, signedAPIRequest(t, http.MethodPut, "/v1/settings", preserveBody, secret,
"11111111111111111111111111111111"))
if preserve.Code != http.StatusOK || state.AccessToken() != "existing-private-token" ||
strings.Contains(preserve.Body.String(), "existing-private-token") {
t.Fatalf("blank preserve status=%d token=%q body=%q", preserve.Code, state.AccessToken(), preserve.Body.String())
}
clearBody := []byte(`{"clear_token":true}`)
clear := httptest.NewRecorder()
server.ServeHTTP(clear, signedAPIRequest(t, http.MethodPut, "/v1/settings", clearBody, secret,
"22222222222222222222222222222222"))
if clear.Code != http.StatusOK || state.TokenConfigured() || strings.Contains(clear.Body.String(), "existing-private-token") {
t.Fatalf("clear status=%d configured=%v body=%q", clear.Code, state.TokenConfigured(), clear.Body.String())
}
}
func signedAPIRequest(t *testing.T, method, path string, body, secret []byte, nonce string) *http.Request {
t.Helper()
request := httptest.NewRequest(method, "http://unix"+path, bytes.NewReader(body))
actor := protocol.Actor{UserID: 1, IsAdmin: true, IsSuperAdmin: true, CanManage: true}
if err := protocol.Sign(request, body, actor, secret, time.Now(), nonce); err != nil {
t.Fatal(err)
}
return request
}

@ -1,418 +0,0 @@
package update
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"regexp"
"sort"
"sync"
"time"
)
type Job struct {
ID string `json:"id"`
Status string `json:"status"`
Checkpoint string `json:"checkpoint"`
ExpectedCurrentCommit string `json:"expected_current_commit"`
ExpectedRemoteCommit string `json:"expected_remote_commit"`
PreviousCommit string `json:"previous_commit,omitempty"`
AppliedCommit string `json:"applied_commit,omitempty"`
ErrorCode string `json:"error_code,omitempty"`
Resolution string `json:"resolution,omitempty"`
ResolutionEvidence string `json:"resolution_evidence,omitempty"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
type diskState struct {
Version int `json:"version"`
AccessToken string `json:"access_token,omitempty"`
Maintenance bool `json:"maintenance_lock"`
Jobs map[string]Job `json:"jobs"`
Nonces map[string]time.Time `json:"nonces"`
}
type StateStore struct {
path string
now func() time.Time
mu sync.Mutex
data diskState
}
const maxStateBytes = 8 << 20
var (
stateNoncePattern = regexp.MustCompile(`^[A-Za-z0-9_-]{16,128}$`)
stateCheckpointPattern = regexp.MustCompile(`^[a-z][a-z0-9_]{0,63}$`)
)
func OpenState(path string) (*StateStore, error) {
store := &StateStore{path: path, now: time.Now, data: diskState{Version: 1, Jobs: map[string]Job{}, Nonces: map[string]time.Time{}}}
if err := store.load(); err != nil {
return nil, err
}
return store, nil
}
// OpenConfiguredState verifies that the fixed runtime directories do not
// escape the checkout through symlinks before creating or opening state.
func OpenConfiguredState(config Config) (*StateStore, error) {
if err := config.Validate(); err != nil {
return nil, err
}
repository, err := filepath.EvalSymlinks(config.RepositoryDir)
if err != nil {
return nil, errors.New("repository directory is unavailable")
}
runtimePath := filepath.Join(config.RepositoryDir, "runtime")
if err := os.Mkdir(runtimePath, 0o700); err != nil && !errors.Is(err, os.ErrExist) {
return nil, fmt.Errorf("create runtime directory: %w", err)
}
runtimeResolved, err := filepath.EvalSymlinks(runtimePath)
if err != nil || !pathWithin(repository, runtimeResolved) {
return nil, errors.New("runtime directory escapes the repository")
}
updatePath := filepath.Join(runtimePath, "update")
if err := os.Mkdir(updatePath, 0o700); err != nil && !errors.Is(err, os.ErrExist) {
return nil, fmt.Errorf("create update state directory: %w", err)
}
updateResolved, err := filepath.EvalSymlinks(updatePath)
if err != nil || !pathWithin(runtimeResolved, updateResolved) {
return nil, errors.New("update state directory escapes runtime")
}
if info, err := os.Lstat(config.StatePath); err == nil {
if info.Mode()&os.ModeSymlink != 0 || !info.Mode().IsRegular() {
return nil, errors.New("update state path must be a regular file")
}
} else if !errors.Is(err, os.ErrNotExist) {
return nil, fmt.Errorf("inspect update state path: %w", err)
}
return OpenState(config.StatePath)
}
func (s *StateStore) SetClockForTesting(now func() time.Time) {
s.mu.Lock()
defer s.mu.Unlock()
s.now = now
}
func (s *StateStore) load() error {
s.mu.Lock()
defer s.mu.Unlock()
file, err := os.Open(s.path)
if errors.Is(err, os.ErrNotExist) {
return s.persistLocked()
}
if err != nil {
return fmt.Errorf("open update state: %w", err)
}
defer file.Close()
raw, err := io.ReadAll(io.LimitReader(file, maxStateBytes+1))
if err != nil {
return fmt.Errorf("read update state: %w", err)
}
if len(raw) > maxStateBytes {
return errors.New("update state exceeds 8388608 bytes")
}
decoder := json.NewDecoder(bytes.NewReader(raw))
decoder.DisallowUnknownFields()
var loaded diskState
if err := decoder.Decode(&loaded); err != nil {
return fmt.Errorf("decode update state: %w", err)
}
if err := requireEOF(decoder); err != nil {
return fmt.Errorf("decode update state: %w", err)
}
if err := validateDiskState(loaded); err != nil {
return err
}
now := s.now().UTC()
changed := false
for id, job := range loaded.Jobs {
if job.Status == "queued" || job.Status == "running" {
job.Status = "manual_intervention"
job.ErrorCode = "interrupted_update_requires_reconciliation"
job.UpdatedAt = now
loaded.Jobs[id] = job
loaded.Maintenance = true
changed = true
}
}
s.data = loaded
if changed {
return s.persistLocked()
}
return nil
}
func validateDiskState(state diskState) error {
if state.Version != 1 || state.Jobs == nil || state.Nonces == nil {
return errors.New("update state has an unsupported or incomplete format")
}
if state.AccessToken != "" && !validAccessToken(state.AccessToken) {
return errors.New("update state contains an invalid access token")
}
if len(state.Jobs) > 10_000 || len(state.Nonces) > 100_000 {
return errors.New("update state exceeds its safe entry limit")
}
hasManual := false
for id, job := range state.Jobs {
if id != job.ID || !jobIDPattern.MatchString(id) || !commitPattern.MatchString(job.ExpectedCurrentCommit) ||
!commitPattern.MatchString(job.ExpectedRemoteCommit) || !stateCheckpointPattern.MatchString(job.Checkpoint) ||
job.CreatedAt.IsZero() || job.UpdatedAt.IsZero() {
return errors.New("update state contains an invalid job")
}
switch job.Status {
case "queued", "running", "succeeded", "failed":
case "manual_intervention":
hasManual = true
case "resolved":
if (job.Resolution != "completed" && job.Resolution != "rolled_back") || !validEvidence(job.ResolutionEvidence) {
return errors.New("update state contains an invalid resolution")
}
default:
return errors.New("update state contains an unknown job status")
}
}
if state.Maintenance != hasManual {
return errors.New("update state maintenance lock is inconsistent")
}
for nonce, expiry := range state.Nonces {
if !stateNoncePattern.MatchString(nonce) || expiry.IsZero() {
return errors.New("update state contains an invalid nonce")
}
}
return nil
}
func (s *StateStore) UseNonce(nonce string, now, expiresAt time.Time) (bool, error) {
s.mu.Lock()
defer s.mu.Unlock()
for value, expiry := range s.data.Nonces {
if !expiry.After(now) {
delete(s.data.Nonces, value)
}
}
if expiry, exists := s.data.Nonces[nonce]; exists && expiry.After(now) {
return false, nil
}
s.data.Nonces[nonce] = expiresAt
if err := s.persistLocked(); err != nil {
delete(s.data.Nonces, nonce)
return false, err
}
return true, nil
}
func (s *StateStore) TokenConfigured() bool {
s.mu.Lock()
defer s.mu.Unlock()
return s.data.AccessToken != ""
}
func (s *StateStore) AccessToken() string {
s.mu.Lock()
defer s.mu.Unlock()
return s.data.AccessToken
}
func (s *StateStore) SetAccessToken(token string) error {
s.mu.Lock()
defer s.mu.Unlock()
old := s.data.AccessToken
s.data.AccessToken = token
if err := s.persistLocked(); err != nil {
s.data.AccessToken = old
return err
}
return nil
}
func (s *StateStore) CreateJob(job Job) error {
s.mu.Lock()
defer s.mu.Unlock()
if s.data.Maintenance {
return errors.New("maintenance lock is active")
}
if _, exists := s.data.Jobs[job.ID]; exists {
return errors.New("update job ID already exists")
}
for _, existing := range s.data.Jobs {
if existing.Status == "queued" || existing.Status == "running" {
return errors.New("an update job is already active")
}
}
now := s.now().UTC()
job.Status = "queued"
job.Checkpoint = "accepted"
job.CreatedAt = now
job.UpdatedAt = now
s.data.Jobs[job.ID] = job
if err := s.persistLocked(); err != nil {
delete(s.data.Jobs, job.ID)
return err
}
return nil
}
func (s *StateStore) UpdateJob(id string, mutate func(*Job)) error {
s.mu.Lock()
defer s.mu.Unlock()
job, ok := s.data.Jobs[id]
if !ok {
return os.ErrNotExist
}
old := job
oldMaintenance := s.data.Maintenance
mutate(&job)
job.UpdatedAt = s.now().UTC()
s.data.Jobs[id] = job
if job.Status == "manual_intervention" {
s.data.Maintenance = true
}
if err := s.persistLocked(); err != nil {
s.data.Jobs[id] = old
s.data.Maintenance = oldMaintenance
return err
}
return nil
}
// ForceManual updates only process memory when durable state cannot be
// written. The previously persisted queued/running record will also be
// converted to manual_intervention on restart, so both the live process and a
// replacement fail closed.
func (s *StateStore) ForceManual(id, code string) {
s.mu.Lock()
defer s.mu.Unlock()
job, ok := s.data.Jobs[id]
if !ok {
return
}
job.Status = "manual_intervention"
job.ErrorCode = code
job.UpdatedAt = s.now().UTC()
s.data.Jobs[id] = job
s.data.Maintenance = true
}
func (s *StateStore) ResolveJob(id, resolution, evidence string) error {
s.mu.Lock()
defer s.mu.Unlock()
job, ok := s.data.Jobs[id]
if !ok {
return os.ErrNotExist
}
if job.Status != "manual_intervention" {
return errors.New("only a manual-intervention job can be resolved")
}
oldJob := job
oldMaintenance := s.data.Maintenance
job.Status = "resolved"
job.Checkpoint = "operator_reconciled"
job.Resolution = resolution
job.ResolutionEvidence = evidence
job.UpdatedAt = s.now().UTC()
s.data.Jobs[id] = job
s.data.Maintenance = false
for otherID, other := range s.data.Jobs {
if otherID != id && other.Status == "manual_intervention" {
s.data.Maintenance = true
}
}
if err := s.persistLocked(); err != nil {
s.data.Jobs[id] = oldJob
s.data.Maintenance = oldMaintenance
return err
}
return nil
}
func (s *StateStore) Job(id string) (Job, bool) {
s.mu.Lock()
defer s.mu.Unlock()
job, ok := s.data.Jobs[id]
return job, ok
}
func (s *StateStore) Jobs() []Job {
s.mu.Lock()
defer s.mu.Unlock()
jobs := make([]Job, 0, len(s.data.Jobs))
for _, job := range s.data.Jobs {
jobs = append(jobs, job)
}
sort.Slice(jobs, func(i, j int) bool { return jobs[i].CreatedAt.After(jobs[j].CreatedAt) })
return jobs
}
func (s *StateStore) JobPointers() (active, last string) {
s.mu.Lock()
defer s.mu.Unlock()
var activeAt, lastAt time.Time
for _, job := range s.data.Jobs {
if last == "" || job.CreatedAt.After(lastAt) {
last = job.ID
lastAt = job.CreatedAt
}
if job.Status == "queued" || job.Status == "running" || job.Status == "manual_intervention" {
if active == "" || job.CreatedAt.After(activeAt) {
active = job.ID
activeAt = job.CreatedAt
}
}
}
return active, last
}
func (s *StateStore) MaintenanceLocked() bool {
s.mu.Lock()
defer s.mu.Unlock()
return s.data.Maintenance
}
func (s *StateStore) persistLocked() error {
directory := filepath.Dir(s.path)
if err := os.MkdirAll(directory, 0o700); err != nil {
return fmt.Errorf("create update state directory: %w", err)
}
data, err := json.MarshalIndent(s.data, "", " ")
if err != nil {
return fmt.Errorf("encode update state: %w", err)
}
temporary, err := os.CreateTemp(directory, ".state-*.tmp")
if err != nil {
return fmt.Errorf("create temporary update state: %w", err)
}
temporaryPath := temporary.Name()
defer os.Remove(temporaryPath)
if err := temporary.Chmod(0o600); err != nil {
temporary.Close()
return err
}
if _, err := temporary.Write(append(data, '\n')); err != nil {
temporary.Close()
return err
}
if err := temporary.Sync(); err != nil {
temporary.Close()
return err
}
if err := temporary.Close(); err != nil {
return err
}
if err := os.Rename(temporaryPath, s.path); err != nil {
return fmt.Errorf("replace update state: %w", err)
}
directoryHandle, err := os.Open(directory)
if err != nil {
return err
}
defer directoryHandle.Close()
return directoryHandle.Sync()
}

@ -1,54 +0,0 @@
package update
import (
"os"
"path/filepath"
"strings"
"testing"
)
func TestInterruptedJobBecomesManualIntervention(t *testing.T) {
path := filepath.Join(t.TempDir(), "state.json")
state, err := OpenState(path)
if err != nil {
t.Fatal(err)
}
job := Job{ID: "abcdefghijklmnopqrstuvwxyzABCDEF", ExpectedCurrentCommit: testCurrent, ExpectedRemoteCommit: testRemote}
if err := state.CreateJob(job); err != nil {
t.Fatal(err)
}
reopened, err := OpenState(path)
if err != nil {
t.Fatal(err)
}
got, ok := reopened.Job(job.ID)
if !ok || got.Status != "manual_intervention" || got.Checkpoint != "accepted" || !reopened.MaintenanceLocked() {
t.Fatalf("interrupted job was not failed closed: %#v lock=%v", got, reopened.MaintenanceLocked())
}
if _, ok := reopened.Job("missing"); ok {
t.Fatal("missing job unexpectedly exists")
}
}
func TestManualInterventionUsesStableBlockedDTO(t *testing.T) {
job := Job{ID: "abcdefghijklmnopqrstuvwxyzABCDEF", Status: "manual_intervention",
Checkpoint: "schema_init_started", PreviousCommit: testCurrent, AppliedCommit: testRemote,
ErrorCode: "schema_init_result_uncertain"}
result := publicJob(job)
if result.Status != "blocked" || result.Phase != "schema_init_started" ||
result.RollbackRef != testCurrent || result.NewCommit != testRemote || result.Error == "" {
t.Fatalf("unexpected blocked DTO: %#v", result)
}
}
func TestOpenStateRejectsDataPastSizeLimit(t *testing.T) {
path := filepath.Join(t.TempDir(), "state.json")
raw := []byte(`{"version":1,"maintenance_lock":false,"jobs":{},"nonces":{}}` +
strings.Repeat(" ", maxStateBytes))
if err := os.WriteFile(path, raw, 0o600); err != nil {
t.Fatal(err)
}
if _, err := OpenState(path); err == nil || !strings.Contains(err.Error(), "exceeds") {
t.Fatalf("oversized update state error = %v", err)
}
}

@ -233,11 +233,6 @@ final class CloudPageController extends AdminController
->withHeader('Cache-Control', 'private, no-store');
}
public function update($request, $response, $args)
{
return $this->view()->display('admin/update.tpl');
}
public function systemLogs($request, $response, $args)
{
try {

@ -1,140 +0,0 @@
<?php
namespace App\Controllers;
use App\Services\Cloud\CloudAccess;
use App\Services\Cloud\CloudError;
use App\Services\Cloud\CloudInput;
use App\Services\Cloud\CloudOperationErrorCatalog;
use App\Services\Cloud\CloudUpdateHelperClient;
/** Super-admin-only proxy for the root-owned local update helper. */
final class AdminUpdateController
{
private function actor($request): array
{
$actor = $request->getAttribute('cloud_actor') ?? CloudAccess::actor();
CloudAccess::manage($actor);
return $actor;
}
private function helper(): CloudUpdateHelperClient
{
return new CloudUpdateHelperClient();
}
private function credentialTransportIsSecure($request): bool
{
if (strtolower((string) $request->getUri()->getScheme()) === 'https') {
return true;
}
// Permit a direct loopback request for local setup and tests. Do not
// trust forwarding headers here: proxy scheme handling belongs in the
// web server / PSR-7 request construction boundary.
$host = strtolower(rtrim((string) $request->getUri()->getHost(), '.'));
return in_array((string) $request->getServerParam('REMOTE_ADDR', ''), ['127.0.0.1', '::1'], true)
&& in_array($host, ['localhost', '127.0.0.1', '::1'], true);
}
private function json($response, array $result, int $status = 200)
{
return $response->withJson($this->withoutAccessToken($result), $status)
->withHeader('Cache-Control', 'no-store');
}
/** Defence in depth: a secret is never returned if the helper regresses. */
private function withoutAccessToken(array $value): array
{
foreach ($value as $key => $item) {
$name = strtolower((string) $key);
if (in_array($name, ['access_token', 'gitea_access_token'], true)) {
unset($value[$key]);
} elseif (CloudOperationErrorCatalog::isDiagnosticKey($name)) {
// Do not recursively pass arbitrary validation/error maps
// through: a nested non-diagnostic key can still carry a
// host path, command, URL, or credential. The updater UI only
// needs one bounded diagnostic sentence, so discard complex
// diagnostic payloads and retain a scalar redacted summary.
if (!is_scalar($item)) {
unset($value[$key]);
continue;
}
$summary = CloudOperationErrorCatalog::adminSummary(null, null, $item);
if ($summary === null) {
unset($value[$key]);
} else {
$value[$key] = $summary;
}
} elseif (is_array($item)) {
$value[$key] = $this->withoutAccessToken($item);
}
}
return $value;
}
public function settings($request, $response)
{
$actor = $this->actor($request);
if ($request->getMethod() === 'GET') {
return $this->json($response, $this->helper()->request(
'GET', '/v1/settings', $actor
));
}
$body = CloudInput::object($request, 8192);
CloudInput::only($body, ['access_token', 'clear_token']);
if (array_key_exists('access_token', $body) && !is_string($body['access_token'])) {
throw new CloudError('访问令牌格式无效');
}
if (array_key_exists('clear_token', $body) && !is_bool($body['clear_token'])) {
throw new CloudError('清除令牌选项无效');
}
if (($body['clear_token'] ?? false) === true && ($body['access_token'] ?? '') !== '') {
throw new CloudError('不能同时设置并清除访问令牌');
}
if (($body['access_token'] ?? '') !== '' && !$this->credentialTransportIsSecure($request)) {
throw new CloudError('访问令牌只能通过 HTTPS 或本机回环地址提交', 403);
}
return $this->json($response, $this->helper()->request(
'PUT', '/v1/settings', $actor, $body
));
}
public function check($request, $response)
{
$actor = $this->actor($request);
$body = CloudInput::object($request, 1024);
CloudInput::only($body, []);
return $this->json($response, $this->helper()->request(
'POST', '/v1/check', $actor, $body
));
}
public function apply($request, $response)
{
$actor = $this->actor($request);
$body = CloudInput::object($request, 4096);
CloudInput::only($body, ['expected_current_commit', 'expected_remote_commit']);
foreach (['expected_current_commit', 'expected_remote_commit'] as $field) {
if (!isset($body[$field]) || !is_string($body[$field])
|| preg_match('/^(?:[0-9a-f]{40}|[0-9a-f]{64})$/D', $body[$field]) !== 1) {
throw new CloudError('更新提交标识无效');
}
}
return $this->json($response, $this->helper()->request(
'POST', '/v1/apply', $actor, $body
), 202);
}
public function job($request, $response, $args)
{
$actor = $this->actor($request);
$jobId = (string) ($args['id'] ?? '');
if (preg_match('/^[A-Za-z0-9_-]{16,64}$/D', $jobId) !== 1) {
throw new CloudError('更新任务编号无效', 404);
}
return $this->json($response, $this->helper()->request(
'GET', '/v1/jobs/' . rawurlencode($jobId), $actor
));
}
}

@ -33,7 +33,6 @@ final class CloudSystemJournalService
'virt-cloud-policy-worker.service',
'virt-cloud-policy-worker@1.service',
'virt-cloud-policy-worker@2.service',
'virt-cloud-update.service',
'virt-cloud-agent-gateway.service',
];

@ -1,114 +0,0 @@
<?php
namespace App\Services\Cloud;
use App\Services\Config;
use GuzzleHttp\Client;
/** Authenticated PHP client for the root-owned local update helper. */
final class CloudUpdateHelperClient
{
private const SOCKET_PATH = '/run/virt-cloud/update.sock';
private const BASE_URI = 'http://localhost';
private Client $client;
private string $secret;
public function __construct(?Client $client = null, ?string $secret = null)
{
$this->secret = $secret ?? (string) Config::get(
'cloud_update_helper_secret',
''
);
if (strlen($this->secret) < 32
|| str_contains($this->secret, "\0")
|| str_contains($this->secret, "\r")
|| str_contains($this->secret, "\n")) {
throw new CloudError('本机更新助手密钥尚未正确配置', 503);
}
if ($client !== null) {
$this->client = $client;
return;
}
if (!defined('CURLOPT_UNIX_SOCKET_PATH')) {
throw new CloudError('PHP cURL 不支持本机 Unix Socket 更新助手', 503);
}
$this->client = new Client([
'base_uri' => self::BASE_URI,
'connect_timeout' => 3,
'timeout' => 95,
'http_errors' => false,
'allow_redirects' => false,
]);
}
public function request(
string $method,
string $target,
array $actor,
?array $payload = null
): array {
if (!preg_match('#^/v1(?:/[A-Za-z0-9_-]+)*$#D', $target)) {
throw new \InvalidArgumentException('Invalid update-helper target');
}
$body = $payload === null ? '' : json_encode(
$payload === [] ? (object) [] : $payload,
JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES
| JSON_UNESCAPED_UNICODE
);
$options = [
'headers' => CloudAgentClient::headers(
$method,
$target,
$body,
$actor,
$this->secret
),
'body' => $body,
'allow_redirects' => false,
'http_errors' => false,
'proxy' => '',
'timeout' => 95,
];
if (defined('CURLOPT_UNIX_SOCKET_PATH')) {
$options['curl'] = [constant('CURLOPT_UNIX_SOCKET_PATH') => self::SOCKET_PATH];
}
try {
$response = $this->client->request(
strtoupper($method),
self::BASE_URI . $target,
$options
);
} catch (\Throwable $error) {
throw new CloudError('本机更新助手不可用,请通过 SSH 检查服务状态', 503);
}
$status = $response->getStatusCode();
$raw = (string) $response->getBody();
if (strlen($raw) > 262144) {
throw new CloudError('本机更新助手响应过大', 502);
}
$result = json_decode($raw, true);
if (!is_array($result) || ($result !== [] && array_is_list($result))) {
throw new CloudError('本机更新助手返回了无效响应', 502);
}
if ($status < 200 || $status >= 300) {
$code = is_string($result['error']['code'] ?? null)
? $result['error']['code'] : '';
$message = match ($status) {
401 => '本机更新助手身份验证失败',
403 => '只有超级管理员可以执行在线更新',
404 => '更新任务不存在',
409 => '更新前置条件不满足或状态需要人工核对',
422 => '更新请求参数无效',
503 => '本机更新助手状态不可用',
default => '在线更新请求未成功',
};
if ($code === 'update_precondition_failed') {
$message = '代码、服务或依赖状态不满足安全更新条件';
}
throw new CloudError($message, in_array($status,
[403, 404, 409, 422, 503], true) ? $status : 502);
}
return $result;
}
}

@ -39,10 +39,6 @@ $System_Config['log_db_password']='';
$System_Config['cloud_data_key_file'] = dirname(__DIR__, 2) . '/runtime/php/data.key';
# 可选:直接填写独立的 base64 编码 32 字节随机密钥;不要使用网站 key 代替。
$System_Config['cloud_data_key'] = '';
# 在线更新重启的 PHP-FPM unit;使用系统默认名称时无需修改。
$System_Config['cloud_update_php_fpm_unit'] = 'php-fpm.service';
# 必须与 runtime/update.json 的 service_secret 完全一致,仅供本机 Unix Socket HMAC。
$System_Config['cloud_update_helper_secret'] = 'REPLACE_ME';
# 必须与 runtime/agent-gateway.json 的 panel_shared_secret 完全一致。
$System_Config['cloud_agent_gateway_secret'] = 'REPLACE_ME';
# PHP 只通过本机 Agent Gateway 向当前已连接的宿主机下发请求。

@ -172,7 +172,6 @@ $app->group('/admin', function () {
$this->get('/cloud/instances', 'App\Controllers\Admin\CloudPageController:instances');
$this->get('/billing', 'App\Controllers\Admin\CloudPageController:billing');
$this->get('/cloud/orders', 'App\Controllers\Admin\CloudPageController:orders');
$this->get('/update', 'App\Controllers\Admin\CloudPageController:update')->add(new SuperAdmin());
$this->get('/operations', 'App\Controllers\Admin\CloudPageController:operations');
$this->get('/system-logs', 'App\Controllers\Admin\CloudPageController:systemLogs');
$this->get('/platform-default-policy', 'App\Controllers\Admin\CloudPageController:platformDefaultPolicy');

@ -152,8 +152,4 @@ $app->group('/api/v1', function () {
$this->post('/admin/cloud-templates/import', 'App\Controllers\CloudAdminController:templates')->setArgument('kind', 'cloud');
$this->post('/admin/cloud-templates/upload', 'App\Controllers\CloudAdminController:cloudTemplateUpload');
$this->post('/admin/rescue-templates/upload', 'App\Controllers\CloudAdminController:rescueUpload');
$this->map(['GET', 'PUT'], '/admin/update/settings', 'App\Controllers\AdminUpdateController:settings');
$this->post('/admin/update/check', 'App\Controllers\AdminUpdateController:check');
$this->post('/admin/update/apply', 'App\Controllers\AdminUpdateController:apply');
$this->get('/admin/update/jobs/{id:[A-Za-z0-9_-]{16,64}}', 'App\Controllers\AdminUpdateController:job');
})->add(new \App\Middleware\CloudApi());

@ -99,8 +99,7 @@
grid-template-columns: minmax(360px, 0.95fr) minmax(440px, 1.2fr);
}
.cloud-admin-rescue-layout,
.cloud-update-grid {
.cloud-admin-rescue-layout {
grid-template-columns: repeat(2, minmax(0, 1fr));
}
@ -423,8 +422,7 @@
.cloud-admin-form-footer,
.cloud-admin-submit,
.cloud-admin-form-actions,
.cloud-update-actions {
.cloud-admin-form-actions {
display: flex;
align-items: center;
justify-content: space-between;
@ -438,8 +436,7 @@
}
.cloud-admin-submit,
.cloud-admin-form-actions,
.cloud-update-actions {
.cloud-admin-form-actions {
justify-content: flex-end;
}
@ -521,8 +518,7 @@
font-weight: 800;
}
.cloud-admin-support-note,
.cloud-update-warning {
.cloud-admin-support-note {
display: flex;
align-items: flex-start;
gap: 9px;
@ -536,8 +532,7 @@
line-height: 1.55;
}
.cloud-admin-support-note i,
.cloud-update-warning i {
.cloud-admin-support-note i {
flex: 0 0 auto;
color: #08785b;
font-size: 19px;
@ -594,71 +589,6 @@
text-align: center;
}
.cloud-update-details {
display: grid;
gap: 0;
margin: 0 0 20px;
border: 1px solid #e5ebf2;
border-radius: 8px;
overflow: hidden;
}
.cloud-update-details > div {
display: grid;
grid-template-columns: minmax(112px, 0.42fr) minmax(0, 1.58fr);
gap: 14px;
padding: 12px 14px;
border-bottom: 1px solid #e8eef5;
background: #fbfcfe;
}
.cloud-update-details > div:last-child {
border-bottom: 0;
}
.cloud-update-details dt {
color: #7b8795;
font-weight: 800;
}
.cloud-update-details dd {
min-width: 0;
margin: 0;
color: #172033;
font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
font-size: 13px;
font-weight: 700;
overflow-wrap: anywhere;
}
.cloud-update-credential {
padding: 16px;
border: 1px solid #e5ebf2;
border-radius: 8px;
background: #fff;
}
.cloud-update-job-details {
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 1px;
border: 0;
background: #e8eef5;
}
.cloud-update-job-details > div {
grid-template-columns: minmax(90px, 0.38fr) minmax(0, 1fr);
border: 0;
}
.cloud-update-warning {
color: #6e4b0b;
background: rgba(239, 177, 0, 0.11);
}
.cloud-update-warning i {
color: #9a5d00;
}
@media (max-width: 1199.98px) {
.cloud-admin-host-layout,
.cloud-admin-catalog-layout {
@ -726,8 +656,7 @@
.cloud-admin-grid,
.cloud-admin-host-layout,
.cloud-admin-catalog-layout,
.cloud-admin-rescue-layout,
.cloud-update-grid {
.cloud-admin-rescue-layout {
grid-template-columns: minmax(0, 1fr);
}
@ -740,9 +669,6 @@
max-height: none;
}
.cloud-update-job-details {
grid-template-columns: minmax(0, 1fr);
}
}
@media (max-width: 767.98px) {
@ -846,11 +772,6 @@
content: none;
}
.cloud-update-details > div,
.cloud-update-job-details > div {
grid-template-columns: minmax(0, 1fr);
gap: 4px;
}
}
@media (max-width: 575.98px) {
@ -895,8 +816,7 @@
.cloud-admin-form-footer,
.cloud-admin-submit,
.cloud-admin-form-actions,
.cloud-update-actions {
.cloud-admin-form-actions {
align-items: stretch;
flex-direction: column;
}
@ -904,8 +824,7 @@
.cloud-admin-form-footer .btn,
.cloud-admin-submit,
.cloud-admin-submit .btn,
.cloud-admin-form-actions .btn,
.cloud-update-actions .btn {
.cloud-admin-form-actions .btn {
width: 100%;
}

@ -8,8 +8,8 @@ let vmStatusRequest = null;
let vmStatusGeneration = 0;
const POWER_STATE_CONVERGENCE_INTERVAL_MS = 1500;
const POWER_STATE_CONVERGENCE_TIMEOUT_MS = 60000;
// Keep the historical storage key so an interrupted deployment made just
// before an online update can still reconcile to the same opening invoice.
// Keep the historical storage key so an interrupted deployment can still
// reconcile to the same opening invoice.
const PROVISION_INVOICE_STORAGE_PREFIX = 'virt-cloud:pending-order:';
const CLOUD_PATHS = Object.freeze({
instances: '/user/cloud/instances',

@ -1,380 +0,0 @@
const API_BASE = '/api/v1/admin/update';
const csrfToken = typeof window.ADMIN_CSRF_TOKEN === 'string' ? window.ADMIN_CSRF_TOKEN : '';
const terminalStatuses = new Set(['succeeded', 'failed', 'blocked']);
const commitPattern = /^(?:[0-9a-f]{40}|[0-9a-f]{64})$/;
const state = {
checkedCommits: null,
knownLastJobId: '',
pollingJobId: '',
pollingGeneration: 0,
updateConfigured: false,
updateAvailable: false,
maintenanceBlocked: false,
};
function element(id) {
return document.getElementById(id);
}
function text(id, value) {
const target = element(id);
if (target) target.textContent = value === null || value === undefined || value === '' ? '—' : String(value);
}
function setPageStatus(message, kind = 'info') {
const target = element('update-page-status');
if (!target) return;
target.textContent = String(message);
target.className = `alert alert-${kind} cloud-admin-notice`;
}
function errorMessage(payload, fallback) {
if (payload && typeof payload.detail === 'string' && payload.detail.trim()) return payload.detail.trim();
if (payload && typeof payload.error === 'string' && payload.error.trim()) return payload.error.trim();
return fallback;
}
async function api(path, options = {}) {
const method = String(options.method || 'GET').toUpperCase();
const headers = { Accept: 'application/json' };
const request = {
method,
headers,
credentials: 'same-origin',
cache: 'no-store',
};
if (method !== 'GET') {
if (!csrfToken) throw new Error('页面安全令牌缺失,请刷新页面后重试。');
headers['Content-Type'] = 'application/json';
headers['X-Admin-CSRF-Token'] = csrfToken;
request.body = JSON.stringify(options.body || {});
}
let response;
try {
response = await fetch(`${API_BASE}${path}`, request);
} catch (_error) {
throw new Error('暂时无法连接更新服务。');
}
const raw = await response.text();
let payload = {};
if (raw) {
try {
payload = JSON.parse(raw);
} catch (_error) {
throw new Error('更新服务返回了无法识别的响应。');
}
}
if (!response.ok) {
throw new Error(errorMessage(payload, `更新请求失败(HTTP ${response.status})。`));
}
if (!payload || typeof payload !== 'object' || Array.isArray(payload)) {
throw new Error('更新服务返回了无法识别的响应。');
}
return payload;
}
function validJobId(value) {
return typeof value === 'string' && /^[A-Za-z0-9_-]{16,64}$/.test(value);
}
function secureCredentialTransport() {
if (window.location.protocol === 'https:') return true;
return ['localhost', '127.0.0.1', '::1', '[::1]'].includes(window.location.hostname);
}
function invalidateCheck(message = '尚未检查') {
state.checkedCommits = null;
state.updateAvailable = false;
text('update-check-status', message);
text('update-checked-current', '—');
text('update-checked-remote', '—');
const apply = element('update-apply');
if (apply) apply.disabled = true;
}
function renderSettings(settings) {
text('update-repository', settings.repository);
text('update-branch', settings.branch);
text('update-current-commit', settings.current_commit);
let checkout = settings.checkout_ready === true ? '仓库可更新' : '仓库尚未就绪';
checkout += settings.token_configured === true ? ';令牌已配置' : ';令牌未配置';
if (typeof settings.checkout_error === 'string' && settings.checkout_error) checkout += `;${settings.checkout_error}`;
text('update-checkout-state', checkout);
state.knownLastJobId = validJobId(settings.last_job_id) ? settings.last_job_id : '';
state.updateConfigured = settings.token_configured === true && settings.checkout_ready === true;
const check = element('update-check');
if (check && !state.pollingJobId) {
check.disabled = state.maintenanceBlocked || !state.updateConfigured;
}
}
function setBusy(busy) {
for (const id of ['update-settings-save', 'update-check', 'update-apply']) {
const target = element(id);
if (!target) continue;
if (id === 'update-check') {
target.disabled = busy || state.maintenanceBlocked || !state.updateConfigured;
} else {
target.disabled = busy || state.maintenanceBlocked
|| (id === 'update-apply'
&& (state.checkedCommits === null || !state.updateAvailable));
}
}
}
function renderJob(job) {
text('update-job-id', job.job_id);
text('update-job-status', job.status);
text('update-job-phase', job.phase);
let result = '';
if (typeof job.error === 'string' && job.error) result = job.error;
else if (typeof job.new_commit === 'string' && job.new_commit) result = `已更新至 ${job.new_commit}`;
else if (job.status === 'succeeded') result = '更新完成';
else if (job.status === 'blocked') result = '维护锁已保留,请通过 SSH 人工恢复';
else if (job.status === 'failed') result = '更新失败,请查看服务日志';
else result = '任务执行中';
if (typeof job.rollback_ref === 'string' && job.rollback_ref) {
result += `;人工恢复回滚引用 ${job.rollback_ref}`;
}
text('update-job-result', result);
}
function delay(milliseconds) {
return new Promise((resolve) => window.setTimeout(resolve, milliseconds));
}
async function pollJob(jobId) {
if (!validJobId(jobId)) {
setPageStatus('更新服务返回了无效的任务编号。', 'danger');
return;
}
state.pollingJobId = jobId;
const generation = ++state.pollingGeneration;
let transientFailures = 0;
setBusy(true);
while (state.pollingGeneration === generation && state.pollingJobId === jobId) {
try {
// After apply, polling is deliberately GET-only. A restart must
// never cause the mutation request to be replayed.
const job = await api(`/jobs/${encodeURIComponent(jobId)}`, { method: 'GET' });
transientFailures = 0;
renderJob(job);
if (terminalStatuses.has(job.status)) {
state.pollingJobId = '';
if (job.status === 'blocked') {
state.maintenanceBlocked = true;
invalidateCheck('更新被阻断,维护锁已保留');
setBusy(true);
const recovery = errorMessage(
job,
'代码或数据库状态尚未确认,请通过 SSH 人工恢复。'
);
setPageStatus(
`${recovery} 若任务在全新数据库结构校验期间中断,仅回退源码不会解除维护锁;必须人工核对数据库与服务状态。`,
'danger'
);
} else if (job.status === 'succeeded') {
state.maintenanceBlocked = false;
setBusy(false);
invalidateCheck('更新已完成,请重新检查后再执行其他更新');
setPageStatus('在线更新已完成。', 'success');
} else {
state.maintenanceBlocked = false;
setBusy(false);
setPageStatus(errorMessage(job, '在线更新失败,请核对任务结果和服务日志。'), 'danger');
}
try {
renderSettings(await api('/settings', { method: 'GET' }));
} catch (_error) {
// The durable task result remains visible if settings refresh fails.
}
if (job.status === 'blocked') setBusy(true);
return;
}
if (!['queued', 'running'].includes(job.status)) {
throw new Error('更新任务状态无效。');
}
setPageStatus('更新任务正在执行,请勿关闭页面或重复提交。', 'info');
await delay(2000);
} catch (error) {
transientFailures += 1;
if (transientFailures > 30) {
state.pollingJobId = '';
// Keep mutating controls locked until a page refresh can
// recover the durable job identifier from settings.
setBusy(true);
setPageStatus(`${error.message} 请稍后刷新页面继续查看任务。`, 'danger');
return;
}
const seconds = Math.min(10, Math.max(2, transientFailures));
setPageStatus(`服务可能正在重启,${seconds} 秒后继续读取任务(第 ${transientFailures} 次重试)。`, 'warning');
await delay(seconds * 1000);
}
}
}
async function loadSettings() {
const settings = await api('/settings', { method: 'GET' });
renderSettings(settings);
if (validJobId(settings.active_job_id)) {
void pollJob(settings.active_job_id);
} else if (validJobId(settings.last_job_id)) {
try {
renderJob(await api(`/jobs/${encodeURIComponent(settings.last_job_id)}`, { method: 'GET' }));
} catch (_error) {
// A stale last-job pointer must not make the settings page unusable.
}
}
return settings;
}
async function saveSettings(event) {
event.preventDefault();
if (state.pollingJobId || state.maintenanceBlocked) return;
const tokenInput = element('update-access-token');
const clearInput = element('update-clear-token');
const token = tokenInput ? tokenInput.value : '';
const clearToken = clearInput ? clearInput.checked : false;
if (token !== '' && clearToken) {
setPageStatus('不能同时填写新令牌并选择清除令牌。', 'danger');
return;
}
if (token !== '' && !secureCredentialTransport()) {
setPageStatus('访问令牌只能通过 HTTPS(或本机回环地址)提交。', 'danger');
return;
}
// Sending an explicit blank value means "preserve" in the Agent contract.
const body = clearToken ? { clear_token: true } : { access_token: token };
setBusy(true);
invalidateCheck('令牌设置已变化,请重新检查');
try {
const settings = await api('/settings', { method: 'PUT', body });
renderSettings(settings);
setPageStatus(clearToken ? '已清除访问令牌。' : (token === '' ? '已保留原访问令牌。' : '访问令牌已安全保存。'), 'success');
} catch (error) {
setPageStatus(error.message, 'danger');
} finally {
// The credential is never copied into storage or restored from a response.
if (tokenInput) tokenInput.value = '';
if (clearInput) clearInput.checked = false;
setBusy(false);
}
}
async function checkForUpdate() {
if (state.pollingJobId || state.maintenanceBlocked) return;
if (!state.updateConfigured) {
setPageStatus('请先配置访问令牌并修复仓库检出状态。', 'warning');
return;
}
setBusy(true);
invalidateCheck('正在检查…');
try {
const result = await api('/check', { method: 'POST', body: {} });
if (!commitPattern.test(result.current_commit) || !commitPattern.test(result.remote_commit)
|| !['current', 'update_available'].includes(result.status)) {
throw new Error('更新检查结果无效。');
}
// Pin both commits in memory. Apply sends exactly this pair and never
// derives either value from editable DOM content.
state.checkedCommits = Object.freeze({
expected_current_commit: result.current_commit,
expected_remote_commit: result.remote_commit,
});
state.updateAvailable = result.status === 'update_available';
text('update-repository', result.repository);
text('update-branch', result.branch);
text('update-current-commit', result.current_commit);
text('update-checked-current', result.current_commit);
text('update-checked-remote', result.remote_commit);
text('update-check-status', result.status === 'update_available' ? '发现可用更新' : '当前已是最新版本');
const apply = element('update-apply');
if (apply) apply.disabled = result.status !== 'update_available';
setPageStatus(result.status === 'update_available' ? '检查完成,可以执行更新。' : '当前已是最新版本。', 'success');
} catch (error) {
invalidateCheck('检查失败');
setPageStatus(error.message, 'danger');
} finally {
setBusy(false);
}
}
async function recoverJobAfterApply(previousLastJobId) {
for (let attempt = 1; attempt <= 30; attempt += 1) {
try {
// Recovery is read-only: never replay the apply request after an
// interrupted response from services that may be restarting.
const settings = await api('/settings', { method: 'GET' });
const recoveredJobId = validJobId(settings.active_job_id)
? settings.active_job_id
: (validJobId(settings.last_job_id) && settings.last_job_id !== previousLastJobId
? settings.last_job_id : '');
renderSettings(settings);
return recoveredJobId;
} catch (_error) {
if (attempt === 30) return '';
const seconds = Math.min(10, Math.max(2, attempt));
setPageStatus(`执行响应中断,正在只读恢复任务;${seconds} 秒后重试(第 ${attempt} 次)。`, 'warning');
await delay(seconds * 1000);
}
}
return '';
}
async function applyUpdate() {
if (state.pollingJobId || state.maintenanceBlocked || !state.updateAvailable
|| state.checkedCommits === null) return;
const confirmed = window.confirm(
'在线更新仅快进应用代码,不会安装 Composer 或 Go 依赖,也不会修改 systemd 配置。它会暂停面板服务、切换代码、执行全新数据库结构校验,并重新启动现有服务。依赖或部署文件变更必须通过 SSH 升级。确定继续吗?'
);
if (!confirmed) return;
const pinned = state.checkedCommits;
const previousLastJobId = state.knownLastJobId;
state.checkedCommits = null;
state.updateAvailable = false;
setBusy(true);
try {
// This is the sole mutating request in the apply flow. It is never retried.
const job = await api('/apply', { method: 'POST', body: pinned });
if (!validJobId(job.job_id) || !['queued', 'running'].includes(job.status)) {
throw new Error('更新服务返回了无效的任务。');
}
renderJob(job);
setPageStatus('更新任务已提交,正在等待独立更新服务。', 'info');
await pollJob(job.job_id);
} catch (error) {
// The POST is never replayed. If its response was lost while services
// restarted, recover the newly durable job using GET requests only.
const recoveredJobId = await recoverJobAfterApply(previousLastJobId);
if (recoveredJobId) {
setPageStatus('已恢复更新任务,继续读取执行进度。', 'warning');
await pollJob(recoveredJobId);
return;
}
// The request outcome is ambiguous. Keep every mutation disabled;
// refreshing the page is the safe recovery path.
setBusy(true);
invalidateCheck('执行请求结果未确认');
setPageStatus(`执行请求结果未确认,请勿重复提交。${error.message} 请稍后刷新页面恢复任务状态。`, 'danger');
}
}
function initialize() {
const settingsForm = element('update-settings-form');
const checkButton = element('update-check');
const applyButton = element('update-apply');
if (settingsForm) settingsForm.addEventListener('submit', saveSettings);
if (checkButton) checkButton.addEventListener('click', checkForUpdate);
if (applyButton) applyButton.addEventListener('click', applyUpdate);
loadSettings()
.then(() => {
if (!state.pollingJobId) setPageStatus('更新配置已载入。', 'info');
})
.catch((error) => setPageStatus(error.message, 'danger'));
}
if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', initialize);
else initialize();

@ -164,7 +164,6 @@
<li><a class="{if $admin_active|default:'' == 'cloud-rescue'}active{/if}" href="/admin/cloud/rescue"><i class="mdi mdi-lifebuoy"></i><span>救援模版</span></a></li>
<li><a class="{if $admin_active|default:'' == 'cloud-instances'}active{/if}" href="/admin/cloud/instances"><i class="mdi mdi-server"></i><span>虚拟机管理</span></a></li>
<li><a class="{if $admin_active|default:'' == 'cloud-operations'}active{/if}" href="/admin/operations"><i class="mdi mdi-clipboard-pulse-outline"></i><span>系统运行与异常任务</span></a></li>
{if $admin_can_manage|default:false}<li><a class="{if $admin_active|default:'' == 'update'}active{/if}" href="/admin/update"><i class="mdi mdi-update"></i><span>在线更新</span></a></li>{/if}

@ -1,94 +0,0 @@
{assign var=admin_title value='在线更新'}
{assign var=admin_active value='update'}
{include file='admin/static_header.tpl'}
<link rel="stylesheet" href="/cloud/admin.css?v={$venix_asset_version}">
<section class="cloud-admin cloud-update" aria-labelledby="update-page-title">
<div class="py-3 d-flex align-items-sm-center flex-sm-row flex-column cloud-admin-page-head">
<div class="flex-grow-1">
<p class="admin-eyebrow mb-1">System</p>
<h4 id="update-page-title" class="fs-20 fw-semibold mb-1">在线更新</h4>
<p class="text-muted mb-0">安全检查固定私有仓库与分支,并执行受约束的快进更新。</p>
</div>
<div class="text-sm-end mt-3 mt-sm-0 cloud-admin-actions">
<p id="update-page-status" class="alert alert-info cloud-admin-notice" role="status" aria-live="polite">正在读取更新配置…</p>
</div>
</div>
<div class="alert alert-warning cloud-admin-guidance" role="note">
<i class="mdi mdi-alert-outline"></i>
<span>在线更新不会安装 Composer 或 Go 依赖,也不会改写 systemd unit。若目标版本修改 <code>web/composer.json</code>、锁文件、Go module 文件或 <code>deploy/**</code>,系统会在停服前拒绝,请改用 SSH 升级。</span>
</div>
<div class="cloud-admin-grid cloud-update-grid">
<form id="update-settings-form" class="card cloud-update-card" autocomplete="off">
<div class="card-body">
<div class="cloud-admin-card-head">
<div>
<p class="admin-eyebrow mb-1">Repository</p>
<h5 class="mb-1">更新源与凭据</h5>
<p class="text-muted mb-0">仓库与分支由服务器固定,网页只维护访问令牌。</p>
</div>
<span class="cloud-admin-card-icon"><i class="mdi mdi-source-branch"></i></span>
</div>
<dl class="cloud-update-details">
<div><dt>固定仓库</dt><dd id="update-repository">正在读取…</dd></div>
<div><dt>固定分支</dt><dd id="update-branch">正在读取…</dd></div>
<div><dt>当前提交</dt><dd id="update-current-commit">正在读取…</dd></div>
<div><dt>检出状态</dt><dd id="update-checkout-state">正在读取…</dd></div>
</dl>
<div class="cloud-update-credential">
<label for="update-access-token" class="form-label">Gitea 私有仓库访问令牌</label>
<input id="update-access-token" class="form-control admin-mono" type="password" maxlength="4096" autocomplete="new-password" spellcheck="false" placeholder="留空即保留现有令牌">
<div class="form-text">令牌只在保存请求中发送,不会回显或保存在浏览器中。</div>
<label class="form-check mt-3 mb-0" for="update-clear-token"><input id="update-clear-token" class="form-check-input" type="checkbox"><span class="form-check-label">明确清除已保存的访问令牌</span></label>
</div>
<div class="cloud-admin-form-actions"><button id="update-settings-save" class="btn btn-primary" type="submit"><i class="mdi mdi-content-save-outline me-1"></i>保存令牌设置</button></div>
</div>
</form>
<div class="card cloud-update-card">
<div class="card-body">
<div class="cloud-admin-card-head">
<div>
<p class="admin-eyebrow mb-1">Deploy</p>
<h5 class="mb-1">检查并执行</h5>
<p class="text-muted mb-0">每次执行前都必须重新检查并锁定本地与远端提交。</p>
</div>
<span class="cloud-admin-card-icon is-warning"><i class="mdi mdi-update"></i></span>
</div>
<dl class="cloud-update-details">
<div><dt>检查状态</dt><dd id="update-check-status">尚未检查</dd></div>
<div><dt>已锁定本地提交</dt><dd id="update-checked-current">—</dd></div>
<div><dt>已锁定远端提交</dt><dd id="update-checked-remote">—</dd></div>
</dl>
<div class="cloud-update-actions">
<button id="update-check" class="btn btn-outline-primary" type="button"><i class="mdi mdi-source-branch-check me-1"></i>检查更新</button>
<button id="update-apply" class="btn btn-danger" type="button" disabled><i class="mdi mdi-update me-1"></i>执行在线更新</button>
</div>
</div>
</div>
</div>
<div class="card mt-3 cloud-update-job-card">
<div class="card-body">
<div class="cloud-admin-card-head">
<div>
<p class="admin-eyebrow mb-1">Progress</p>
<h5 class="mb-1">更新任务</h5>
<p class="text-muted mb-0">PHP 与私有引擎重启期间会自动只读恢复任务,请保留本页面且不要重复提交。</p>
</div>
<span class="cloud-admin-card-icon"><i class="mdi mdi-timeline-clock-outline"></i></span>
</div>
<dl class="cloud-update-details cloud-update-job-details">
<div><dt>任务编号</dt><dd id="update-job-id">—</dd></div>
<div><dt>任务状态</dt><dd id="update-job-status">—</dd></div>
<div><dt>当前阶段</dt><dd id="update-job-phase">—</dd></div>
<div><dt>结果</dt><dd id="update-job-result">—</dd></div>
</dl>
<p class="cloud-update-warning mb-0"><i class="mdi mdi-lock-alert-outline"></i><span>若任务进入 <code>blocked</code>,维护锁会保持启用。全新数据库结构校验中断时仅回退源码不会解除维护锁,必须通过 SSH 完成核对并验证服务后再协调状态。</span></p>
</div>
</div>
</section>
<script type="module" src="/cloud/update.js?v={$venix_asset_version}"></script>
{include file='admin/static_footer.tpl'}

@ -1,147 +0,0 @@
<?php
// Static contract test: exercising the proxy itself would require a live,
// HMAC-authenticated local update helper. These assertions keep the browser/PHP
// security boundary reviewable without application boot or network access.
$root = dirname(__DIR__);
$read = static function (string $relative) use ($root): string {
$contents = file_get_contents($root . '/' . $relative);
if ($contents === false) {
throw new RuntimeException('Cannot read ' . $relative);
}
return $contents;
};
$assert = static function ($condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$routes = $read('config/routes.php');
$apiRoutes = $read('config/routes_cloud.php');
$controller = $read('app/Controllers/AdminUpdateController.php');
$helperClient = $read('app/Services/Cloud/CloudUpdateHelperClient.php');
$header = $read('resources/views/venix/admin/static_header.tpl');
$template = $read('resources/views/venix/admin/update.tpl');
$script = $read('public/cloud/update.js');
$assert(str_contains(
$routes,
"\$this->get('/update', 'App\\Controllers\\Admin\\CloudPageController:update')->add(new SuperAdmin());"
), 'The update page is not protected by the SuperAdmin middleware.');
$assert(str_contains($header, "{if \$admin_can_manage|default:false}<li><a")
&& str_contains($header, 'href="/admin/update"'),
'The Venix update navigation is not limited by admin_can_manage.');
foreach ([
"['GET', 'PUT'], '/admin/update/settings'",
"post('/admin/update/check'",
"post('/admin/update/apply'",
"get('/admin/update/jobs/{id:[A-Za-z0-9_-]{16,64}}'",
] as $routeContract) {
$assert(str_contains($apiRoutes, $routeContract), 'Missing update API route: ' . $routeContract);
}
$assert(substr_count($controller, '$actor = $this->actor($request);') === 4,
'Every update controller action must resolve the managed actor.');
$assert(str_contains($controller, 'CloudAccess::manage($actor);'),
'Update API actions do not force CloudAccess::manage.');
$assert(str_contains($controller, "'GET', '/v1/settings'")
&& str_contains($controller, "'POST', '/v1/check'")
&& str_contains($controller, "'POST', '/v1/apply'")
&& str_contains($controller, "'GET', '/v1/jobs/' . rawurlencode(\$jobId)"),
'The PHP proxy does not use fixed local-helper update paths.');
$assert(str_contains($controller, "CloudInput::only(\$body, ['access_token', 'clear_token']);")
&& str_contains($controller, "['expected_current_commit', 'expected_remote_commit']"),
'The update proxy request allowlists changed.');
$assert(str_contains($controller, 'new CloudUpdateHelperClient()')
&& str_contains($controller, '), 202);'),
'Update requests must use the local helper and apply must return HTTP 202.');
$assert(str_contains($controller, "['access_token', 'gitea_access_token']")
&& str_contains($controller, "withHeader('Cache-Control', 'no-store')"),
'Update responses are not defensively stripped/no-store.');
$assert(str_contains($controller, "getUri()->getScheme()) === 'https'")
&& str_contains($controller, "getServerParam('REMOTE_ADDR', '')")
&& str_contains($controller, "getUri()->getHost()")
&& str_contains($controller, "['localhost', '127.0.0.1', '::1']")
&& str_contains($controller, "['127.0.0.1', '::1']")
&& !str_contains($controller, 'X-Forwarded-Proto'),
'The PHP proxy may accept a repository token over an untrusted transport.');
$assert(str_contains($helperClient, "private const SOCKET_PATH = '/run/virt-cloud/update.sock'")
&& str_contains($helperClient, 'CURLOPT_UNIX_SOCKET_PATH')
&& str_contains($helperClient, 'CloudAgentClient::headers(')
&& str_contains($helperClient, "'allow_redirects' => false")
&& !str_contains($helperClient, 'CloudEngineClient'),
'The updater must use only the authenticated fixed Unix socket.');
$assert(str_contains($template, 'id="update-repository"')
&& str_contains($template, 'id="update-branch"')
&& !str_contains($template, 'https://g.luoli.live/root/virt.git'),
'The fixed repository and branch must be rendered from the API response.');
$assert(preg_match('/<input id="update-access-token"[^>]*type="password"[^>]*>/s', $template, $tokenInput) === 1
&& !str_contains($tokenInput[0], ' value='),
'The access token field must be an empty password input.');
$assert(str_contains($template, 'id="update-clear-token"')
&& str_contains($template, '/cloud/update.js?v={$venix_asset_version}')
&& !str_contains($template, '/cloud/admin.js'),
'The page needs explicit token clearing and its independent update script.');
$assert(str_contains($template, 'web/composer.json')
&& str_contains($template, 'Go module')
&& str_contains($template, 'deploy/**')
&& str_contains($template, '不会安装 Composer 或 Go 依赖')
&& str_contains($template, '不会改写 systemd unit')
&& !str_contains($template, 'Python'),
'The page must describe the code-only update boundary and all rejected paths.');
$assert(str_contains($template, '<code>blocked</code>')
&& str_contains($template, '维护锁会保持启用')
&& str_contains($template, '仅回退源码不会解除维护锁')
&& str_contains($template, '必须通过 SSH 完成核对并验证服务后再协调状态'),
'Blocked updates must direct the operator to SSH recovery without unlocking mutations.');
foreach (['innerHTML', 'insertAdjacentHTML', 'localStorage', 'sessionStorage', 'document.cookie'] as $unsafeBrowserApi) {
$assert(!str_contains($script, $unsafeBrowserApi),
'Update UI must not use unsafe rendering or durable browser storage: ' . $unsafeBrowserApi);
}
$assert(str_contains($script, '.textContent =')
&& str_contains($script, "headers['X-Admin-CSRF-Token'] = csrfToken")
&& str_contains($script, "credentials: 'same-origin'"),
'Safe rendering, CSRF, or same-origin credentials are missing.');
$assert(str_contains($script, "const body = clearToken ? { clear_token: true } : { access_token: token };")
&& str_contains($script, "tokenInput.value = ''"),
'Blank-token preservation, explicit clearing, or credential cleanup changed.');
$assert(str_contains($script, "window.location.protocol === 'https:'")
&& str_contains($script, "['localhost', '127.0.0.1', '::1', '[::1]']"),
'The browser may send a repository token over an insecure transport.');
$assert(str_contains($script, 'state.checkedCommits = Object.freeze({')
&& str_contains($script, 'expected_current_commit: result.current_commit')
&& str_contains($script, 'expected_remote_commit: result.remote_commit')
&& str_contains($script, "state.updateAvailable = result.status === 'update_available'")
&& str_contains($script, 'state.checkedCommits === null || !state.updateAvailable')
&& str_contains($script, 'state.maintenanceBlocked || !state.updateAvailable'),
'Check results do not pin both commits for apply.');
$assert(str_contains($script, 'settings.token_configured === true && settings.checkout_ready === true')
&& str_contains($script, 'window.confirm(')
&& str_contains($script, '仅快进应用代码')
&& str_contains($script, '不会安装 Composer 或 Go 依赖,也不会修改 systemd 配置')
&& str_contains($script, '依赖或部署文件变更必须通过 SSH 升级')
&& str_contains($script, '执行全新数据库结构校验'),
'Update readiness or the destructive-impact confirmation changed.');
$assert(substr_count($script, "api('/apply', { method: 'POST', body: pinned })") === 1
&& str_contains($script, "api(`/jobs/\${encodeURIComponent(jobId)}`, { method: 'GET' })")
&& str_contains($script, 'transientFailures > 30')
&& str_contains($script, 'recoverJobAfterApply')
&& str_contains($script, '正在只读恢复任务')
&& str_contains($script, 'job.rollback_ref')
&& str_contains($script, '人工恢复回滚引用')
&& str_contains($script, '请勿重复提交'),
'Apply must be single-shot while job polling survives service restarts.');
$assert(str_contains($script, "new Set(['succeeded', 'failed', 'blocked'])")
&& str_contains($script, 'state.maintenanceBlocked = true')
&& str_contains($script, "job.status === 'blocked'")
&& str_contains($script, '代码或数据库状态尚未确认')
&& str_contains($script, '若任务在全新数据库结构校验期间中断')
&& str_contains($script, '仅回退源码不会解除维护锁')
&& str_contains($script, 'busy || state.maintenanceBlocked'),
'Blocked is a polling terminal state but must keep every mutation control locked.');
echo "Admin online-update page, proxy, and browser contracts passed.\n";

@ -1,86 +0,0 @@
<?php
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Services\Cloud\CloudError;
use App\Services\Cloud\CloudUpdateHelperClient;
use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use GuzzleHttp\Psr7\Response;
$assert = static function (bool $condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$history = [];
$mock = new MockHandler([
new Response(200, ['Content-Type' => 'application/json'], json_encode([
'repository' => 'https://g.luoli.live/root/virt.git',
'branch' => 'go',
'checkout_ready' => true,
'token_configured' => false,
'maintenance_lock' => false,
], JSON_THROW_ON_ERROR)),
new Response(409, ['Content-Type' => 'application/json'], json_encode([
'error' => ['code' => 'update_precondition_failed'],
], JSON_THROW_ON_ERROR)),
]);
$stack = HandlerStack::create($mock);
$stack->push(Middleware::history($history));
$secret = 'update-helper-test-secret-at-least-32-bytes';
$client = new CloudUpdateHelperClient(
new Client(['handler' => $stack]),
$secret
);
$actor = [
'user_id' => 7,
'is_admin' => true,
'is_super_admin' => true,
'can_manage' => true,
'session_id' => 'must-not-cross-the-local-helper-boundary',
];
$settings = $client->request('GET', '/v1/settings', $actor);
$assert(($settings['branch'] ?? null) === 'go' && count($history) === 1,
'Update helper settings response did not round-trip.');
$request = $history[0]['request'];
$assert((string) $request->getUri() === 'http://localhost/v1/settings'
&& $request->getHeaderLine('X-Virt-Content-SHA256') === hash('sha256', '')
&& preg_match('/^[a-f0-9]{64}$/D',
$request->getHeaderLine('X-Virt-Signature')) === 1
&& $request->getHeaderLine('X-Virt-Actor') !== ''
&& $request->getHeaderLine('X-Cloud-Signature') === '',
'Update helper request did not use the fixed signed X-Virt contract.');
$encoded = $request->getHeaderLine('X-Virt-Actor');
$raw = base64_decode(strtr($encoded, '-_', '+/')
. str_repeat('=', (4 - strlen($encoded) % 4) % 4), true);
$forwardedActor = json_decode((string) $raw, true, 8, JSON_THROW_ON_ERROR);
$assert(!array_key_exists('session_id', $forwardedActor)
&& ($forwardedActor['is_super_admin'] ?? false) === true,
'Panel session data crossed into the privileged update helper.');
try {
$client->request('POST', '/v1/apply', $actor, [
'expected_current_commit' => str_repeat('a', 40),
'expected_remote_commit' => str_repeat('b', 40),
]);
throw new RuntimeException('Unsafe update precondition was accepted.');
} catch (CloudError $error) {
$assert($error->httpStatus === 409
&& !str_contains($error->getMessage(), 'update_precondition_failed'),
'Update helper error was not safely mapped.');
}
foreach (['/internal/v1/update', '/v1/jobs/../settings', '/v1/check?x=1'] as $unsafe) {
try {
$client->request('GET', $unsafe, $actor);
throw new RuntimeException('Unsafe helper target accepted: ' . $unsafe);
} catch (InvalidArgumentException $expected) {
// Expected before transport.
}
}
echo "Authenticated fixed Unix-socket update helper client contract passed.\n";

@ -8,6 +8,7 @@ $assert = static function ($condition, $message) {
}
};
$routes = file_get_contents($root . '/config/routes.php');
$apiRoutes = file_get_contents($root . '/config/routes_cloud.php');
$assert(!str_contains($routes, "get('/node'") && !str_contains($routes, 'NodeController:'), 'Old user/admin node routes remain');
$assert(str_contains($routes, "get('/hosts', 'App\\Controllers\\Admin\\CloudPageController:hosts')"), 'Admin host page missing');
foreach (['/community', '/paid-registration', '/trafficlog', '/tcplog', '/tcpmonitor', '/redis-online', '/inactive-', '/abnormal-users', '/apple-', '/detect', '/edge-config', '/node-access-api', '/sublink', '/sub_server', '/checkin', '/cleanup', '/doc'] as $retired) {
@ -20,10 +21,41 @@ $assert(!is_file($root . '/resources/views/venix/admin/telegram/index.tpl'), 'St
$ticketSystem = file_get_contents($root . '/resources/views/venix/admin/ticket/index.tpl');
$ticketTelegram = $root . '/resources/views/venix/admin/ticket/telegram_settings.tpl';
$adminSidebar = file_get_contents($root . '/resources/views/venix/admin/static_header.tpl');
$adminPageController = file_get_contents($root . '/app/Controllers/Admin/CloudPageController.php');
$systemJournal = file_get_contents($root . '/app/Services/Cloud/CloudSystemJournalService.php');
$repository = dirname($root);
$assert(is_file($ticketTelegram) && str_contains($ticketSystem, "admin/ticket/telegram_settings.tpl"), 'Ticket system Telegram settings were not integrated');
$assert(str_contains($ticketSystem, "value='工单系统'") && str_contains($adminSidebar, '<span>工单系统</span>'), 'Ticket system label was not updated');
$assert(!str_contains($adminSidebar, 'href="/admin/telegram"'), 'Standalone administrator Telegram navigation remains');
$config = file_get_contents($root . '/config/.config.php.example');
$assert(!str_contains($routes, "get('/update'")
&& !str_contains($apiRoutes, "'/admin/update")
&& !str_contains($adminSidebar, 'href="/admin/update"')
&& !str_contains($adminPageController, 'public function update('),
'Retired online-update route, navigation, or page action remains');
foreach ([
'app/Controllers/AdminUpdateController.php',
'app/Services/Cloud/CloudUpdateHelperClient.php',
'resources/views/venix/admin/update.tpl',
'public/cloud/update.js',
] as $retiredUpdateSource) {
$assert(!is_file($root . '/' . $retiredUpdateSource),
'Retired online-update source remains: ' . $retiredUpdateSource);
}
foreach ([
'gateway/cmd/virt-cloud-update',
'gateway/update',
'deploy/virt-cloud-update.json.example',
'deploy/virt-cloud-update.service',
'release/virt-cloud-update-linux-amd64',
] as $retiredUpdateSource) {
$assert(!file_exists($repository . '/' . $retiredUpdateSource),
'Retired online-update repository source remains: '
. $retiredUpdateSource);
}
$assert(!str_contains($config, 'cloud_update_')
&& !str_contains($systemJournal, 'virt-cloud-update.service'),
'Retired online-update configuration or journal unit remains');
foreach (['user_try', 'reg_method', 'user_class_default', 'enable_class_expire_reset', 'auto_clean_', 'auto_backup_webroot', 'mu_suffix', 'mu_regex', 'checkinMin', 'enable_documents', 'subUrl', 'tgUrl', 'enable_telegram', 'telegram_token', 'telegram_request_token', 'chatgpt_api'] as $key) {
$assert(!str_contains($config, $key), 'Retired configuration remains: ' . $key);
}
@ -114,7 +146,6 @@ $manualFixture = $root . '/tests/manual/admin_sidebar_fixture.php';
$assert(is_file($manualFixture)
&& !is_file($root . '/tests/manual/admin_sidebar_fixture.py'),
'The administrator sidebar regression fixture was not migrated from Python to PHP.');
$repository = dirname($root);
$pythonSources = [];
$iterator = new RecursiveIteratorIterator(
new RecursiveCallbackFilterIterator(

@ -37,11 +37,11 @@ $root = dirname(__DIR__);
$assert(str_contains(file_get_contents($root . '/bootstrap.php'), "'/config/config.php'"), 'Canonical config.php missing');
$example = file_get_contents($root . '/config/.config.php.example');
$assert(str_contains($example, "['cloud_data_key_file']")
&& str_contains($example, "['cloud_update_helper_secret']")
&& str_contains($example, "['cloud_agent_gateway_secret']")
&& str_contains($example, "['cloud_agent_gateway_url']")
&& !str_contains($example, "['cloud_engine_"),
'PHP data-seal, update-helper or Agent-gateway configuration is missing.');
&& !str_contains($example, "['cloud_engine_")
&& !str_contains($example, "['cloud_update_"),
'PHP data-seal or Agent-gateway configuration is missing, or retired configuration remains.');
$extractPlaceholder = static function (string $key) use ($example): string {
if (preg_match("/\\['" . preg_quote($key, '/')
. "'\\]\\s*=\\s*'([^']*)'/", $example, $match) !== 1) {
@ -49,8 +49,7 @@ $extractPlaceholder = static function (string $key) use ($example): string {
}
return $match[1];
};
$assert(strlen($extractPlaceholder('cloud_update_helper_secret')) < 32
&& strlen($extractPlaceholder('cloud_agent_gateway_secret')) < 32,
$assert(strlen($extractPlaceholder('cloud_agent_gateway_secret')) < 32,
'Public example secrets must fail the runtime minimum-length validation.');
$agentExample = json_decode(file_get_contents(dirname($root)
. '/deploy/virt-cloud-agent.json.example'), true, 32, JSON_THROW_ON_ERROR);

Loading…
Cancel
Save