Compare commits
191 Commits
| Author | SHA1 | Date |
|---|---|---|
|
|
887072ac61 | 1 month ago |
|
|
be5efa7033 | 1 month ago |
|
|
a578502798 | 1 month ago |
|
|
5b8240939b | 1 month ago |
|
|
287670d584 | 1 month ago |
|
|
177ebb7cb6 | 1 month ago |
|
|
d3bba00f14 | 1 month ago |
|
|
13aacd7636 | 1 month ago |
|
|
ce9d77f999 | 1 month ago |
|
|
78cadb051f | 1 month ago |
|
|
36636221f7 | 1 month ago |
|
|
e7230fffe2 | 1 month ago |
|
|
fc01273112 | 1 month ago |
|
|
521d40ae72 | 1 month ago |
|
|
2bac113155 | 1 month ago |
|
|
6dbd1fe593 | 1 month ago |
|
|
67e59a263a | 1 month ago |
|
|
212f770154 | 1 month ago |
|
|
b5ed1e130d | 1 month ago |
|
|
b1429f61b4 | 1 month ago |
|
|
e290872e5a | 1 month ago |
|
|
4ec4fefc30 | 1 month ago |
|
|
b333246e9f | 1 month ago |
|
|
04e12d1906 | 1 month ago |
|
|
9f0d16600d | 1 month ago |
|
|
40f9c549da | 1 month ago |
|
|
7d60707246 | 2 months ago |
|
|
38fd302195 | 2 months ago |
|
|
2112379323 | 2 months ago |
|
|
41528cfc76 | 2 months ago |
|
|
d51b169ffc | 2 months ago |
|
|
ecd65d6b65 | 2 months ago |
|
|
afbd8c2941 | 2 months ago |
|
|
3e2b36723a | 2 months ago |
|
|
3f9d3d2d63 | 2 months ago |
|
|
b8105fab23 | 2 months ago |
|
|
9bb5d30b0a | 2 months ago |
|
|
7b70b8f285 | 2 months ago |
|
|
12e20d150f | 2 months ago |
|
|
fad3b4e4fd | 2 months ago |
|
|
77566fc15c | 2 months ago |
|
|
b7c4cc270c | 2 months ago |
|
|
59dd1e4d33 | 2 months ago |
|
|
9c84724eb1 | 2 months ago |
|
|
b9a2829bed | 2 months ago |
|
|
ff75762a75 | 2 months ago |
|
|
f3ca55b5e4 | 2 months ago |
|
|
f3822ac66d | 2 months ago |
|
|
7f804125b5 | 2 months ago |
|
|
ff0cdcb620 | 2 months ago |
|
|
6d6e497db0 | 2 months ago |
|
|
f2c624b2db | 2 months ago |
|
|
069c7f1b56 | 2 months ago |
|
|
514861f562 | 2 months ago |
|
|
5e8f623e2a | 2 months ago |
|
|
328b18b8e3 | 2 months ago |
|
|
3fcebe4d40 | 2 months ago |
|
|
0347169753 | 2 months ago |
|
|
4f889fb1cc | 2 months ago |
|
|
f0ad01fb47 | 2 months ago |
|
|
8b9d8d7e82 | 2 months ago |
|
|
05f42e51bf | 2 months ago |
|
|
74cd779901 | 2 months ago |
|
|
d6b6d390d5 | 2 months ago |
|
|
68108b3f38 | 2 months ago |
|
|
a07e11c681 | 2 months ago |
|
|
5c8d95bd9a | 2 months ago |
|
|
3c305a552d | 2 months ago |
|
|
fe43fdbf9c | 2 months ago |
|
|
5e71f81c5a | 2 months ago |
|
|
40d32f5bfc | 2 months ago |
|
|
4559cc57b2 | 2 months ago |
|
|
ecc15d56f1 | 2 months ago |
|
|
a94c5667a8 | 2 months ago |
|
|
8829ad009d | 2 months ago |
|
|
95787ca9c3 | 2 months ago |
|
|
a442dd0292 | 2 months ago |
|
|
cf2a4e3ec4 | 2 months ago |
|
|
b2c19d18bb | 2 months ago |
|
|
8658d6b4e6 | 2 months ago |
|
|
8944cf3588 | 2 months ago |
|
|
c133b788f4 | 2 months ago |
|
|
b5a6b792e1 | 2 months ago |
|
|
982aa16449 | 2 months ago |
|
|
6b143a02e1 | 2 months ago |
|
|
6c223a3ead | 2 months ago |
|
|
3a7e272b04 | 2 months ago |
|
|
92b2e9666e | 2 months ago |
|
|
407d368892 | 2 months ago |
|
|
59ff1e1d43 | 2 months ago |
|
|
b2368a86da | 2 months ago |
|
|
5c98954067 | 2 months ago |
|
|
433e34c5f5 | 2 months ago |
|
|
6b292373c6 | 2 months ago |
|
|
a912b1c39e | 2 months ago |
|
|
d96dfa3166 | 2 months ago |
|
|
efbadcfbf2 | 2 months ago |
|
|
2a50650059 | 2 months ago |
|
|
1e52eb28d2 | 2 months ago |
|
|
e8004b6b52 | 2 months ago |
|
|
a942124549 | 2 months ago |
|
|
fb855241d0 | 2 months ago |
|
|
8419152f1c | 2 months ago |
|
|
56b28045c0 | 2 months ago |
|
|
45eed216ba | 2 months ago |
|
|
611c1949ce | 2 months ago |
|
|
d3ce270654 | 2 months ago |
|
|
d791fc5f1d | 2 months ago |
|
|
cac637c615 | 2 months ago |
|
|
c98b43988f | 2 months ago |
|
|
9d40f5095f | 2 months ago |
|
|
b58473a52f | 2 months ago |
|
|
dd3dcedb8b | 2 months ago |
|
|
c5a7ca6d35 | 2 months ago |
|
|
7353f9f7eb | 2 months ago |
|
|
1df35d092d | 2 months ago |
|
|
6902215b2f | 2 months ago |
|
|
5f5f89c68b | 2 months ago |
|
|
8759373324 | 2 months ago |
|
|
eca57ab75f | 2 months ago |
|
|
5905699742 | 2 months ago |
|
|
5919dff9a9 | 2 months ago |
|
|
8338b3ce21 | 2 months ago |
|
|
b7efaa282c | 2 months ago |
|
|
f781e5065d | 2 months ago |
|
|
8b2cb35b58 | 2 months ago |
|
|
3aeeff22a7 | 2 months ago |
|
|
73afe9db50 | 2 months ago |
|
|
18e22dacee | 2 months ago |
|
|
eb21b2ad92 | 2 months ago |
|
|
34259bd277 | 2 months ago |
|
|
1e2f8aba60 | 2 months ago |
|
|
4c3e98cad7 | 2 months ago |
|
|
2df07d9143 | 2 months ago |
|
|
f51ee5268e | 2 months ago |
|
|
9695f4c68c | 2 months ago |
|
|
0c98d4e336 | 2 months ago |
|
|
26ca956431 | 2 months ago |
|
|
99b4e96405 | 2 months ago |
|
|
a6cccf5951 | 2 months ago |
|
|
b2654590c1 | 2 months ago |
|
|
d92f3570d4 | 2 months ago |
|
|
ebf1860d45 | 2 months ago |
|
|
f8bf9cf171 | 2 months ago |
|
|
a4b2ee923e | 2 months ago |
|
|
44f681efa8 | 2 months ago |
|
|
8e201ace0f | 2 months ago |
|
|
8f1fa892de | 2 months ago |
|
|
3ba575213e | 2 months ago |
|
|
542e8fa10e | 2 months ago |
|
|
2d22f41ed6 | 2 months ago |
|
|
059dcf0f6f | 2 months ago |
|
|
85839e097e | 2 months ago |
|
|
961c90b361 | 2 months ago |
|
|
a8336ee468 | 2 months ago |
|
|
149bb28a1d | 2 months ago |
|
|
b82abf5547 | 2 months ago |
|
|
dad9fa6ad2 | 2 months ago |
|
|
64205cf8f2 | 2 months ago |
|
|
40248c1064 | 2 months ago |
|
|
308977473f | 2 months ago |
|
|
59f262b9aa | 2 months ago |
|
|
352ac4bdb8 | 2 months ago |
|
|
ea84d9519e | 2 months ago |
|
|
1e1f68d503 | 2 months ago |
|
|
0dae9397b0 | 2 months ago |
|
|
d510eeca36 | 2 months ago |
|
|
73b4c12d19 | 2 months ago |
|
|
83bf4b0f29 | 2 months ago |
|
|
d7262885ea | 2 months ago |
|
|
32ac875475 | 2 months ago |
|
|
2ef392ffa9 | 2 months ago |
|
|
4bb98a3de8 | 2 months ago |
|
|
de871e0d74 | 2 months ago |
|
|
9809b2cee2 | 2 months ago |
|
|
9689e386f0 | 2 months ago |
|
|
c27fab1cff | 2 months ago |
|
|
772fa83ae4 | 2 months ago |
|
|
74b89051a7 | 2 months ago |
|
|
5044441aba | 2 months ago |
|
|
c6e36159d5 | 2 months ago |
|
|
0c7c8a7022 | 2 months ago |
|
|
52294f7c3f | 2 months ago |
|
|
aac5024245 | 2 months ago |
|
|
848318b65a | 2 months ago |
|
|
d99dca3ff3 | 2 months ago |
|
|
1f4051a030 | 2 months ago |
|
|
bb9b244364 | 2 months ago |
|
|
4bfda1cc9a | 2 months ago |
|
|
f535424087 | 2 months ago |
|
|
a4947c0c24 | 2 months ago |
@ -1,7 +1,40 @@
|
|||||||
.venv/
|
# Local environment, credentials and machine metadata
|
||||||
data/
|
|
||||||
__pycache__/
|
|
||||||
.pytest_cache/
|
|
||||||
*.py[cod]
|
|
||||||
*.egg-info/
|
|
||||||
.DS_Store
|
.DS_Store
|
||||||
|
.env
|
||||||
|
.env.*
|
||||||
|
!.env.example
|
||||||
|
!.env.sample
|
||||||
|
.user.ini
|
||||||
|
master.key
|
||||||
|
id_rsa
|
||||||
|
id_ed25519
|
||||||
|
*.key
|
||||||
|
*.pem
|
||||||
|
*.p12
|
||||||
|
|
||||||
|
# Installed dependencies and runtime data
|
||||||
|
/web/vendor/
|
||||||
|
/web/node_modules/
|
||||||
|
/web/config/.config.php
|
||||||
|
/web/storage/backups/
|
||||||
|
/web/storage/logs/
|
||||||
|
/web/storage/cloud-runtime/
|
||||||
|
/web/storage/ticket-attachments/
|
||||||
|
/web/storage/framework/smarty/compile/*
|
||||||
|
/web/storage/framework/smarty/cache/*
|
||||||
|
/web/storage/framework/views/*
|
||||||
|
!/web/storage/framework/views/.gitignore
|
||||||
|
*.log
|
||||||
|
*.sqlite
|
||||||
|
*.sqlite3
|
||||||
|
|
||||||
|
# Local archives; source SQL under web/sql remains versioned
|
||||||
|
*.zip
|
||||||
|
*.tar
|
||||||
|
*.tar.gz
|
||||||
|
*.tgz
|
||||||
|
*.sql.gz
|
||||||
|
runtime/
|
||||||
|
web/config/config.php
|
||||||
|
engine/.venv/
|
||||||
|
engine/**/__pycache__/
|
||||||
|
|||||||
@ -0,0 +1,37 @@
|
|||||||
|
# Virt Cloud development rules
|
||||||
|
|
||||||
|
- This repository is the independent Virt Cloud project. Work only inside this
|
||||||
|
checkout unless the user explicitly authorizes changes to another project.
|
||||||
|
- When a task depends on historical decisions, test results, or project
|
||||||
|
context, and the knowledge vault is available, first read
|
||||||
|
`/Users/admin/Documents/www/codex-knowledge/AGENTS.md`, then
|
||||||
|
`Memory/INDEX.md` and `Memory/CURRENT.md`; follow only the linked notes
|
||||||
|
relevant to the task. If the vault is unavailable, say so explicitly and do
|
||||||
|
not guess the old project state.
|
||||||
|
- The Git remote is `https://g.luoli.live/root/virt.git`.
|
||||||
|
- All subsequent changes, commits and pushes for this project must use the
|
||||||
|
`venix` branch. Do not commit or push them to `main` or force-push.
|
||||||
|
- Before editing or committing, check the current branch and working tree.
|
||||||
|
Preserve unrelated user changes. If this checkout is not on `venix`, stop
|
||||||
|
before changing files and resolve the branch safely.
|
||||||
|
- Never commit real configuration, credentials, private keys, database dumps,
|
||||||
|
installed dependencies, runtime logs or caches. Keep source configuration
|
||||||
|
examples and source schema files versioned.
|
||||||
|
- The PHP application lives in `web/`; its eventual HTTP document root is
|
||||||
|
`web/public/`, not the repository root.
|
||||||
|
- Only the venix theme is retained. Ordinary users must not receive host
|
||||||
|
administration or retired node-management routes.
|
||||||
|
- PHP owns accounts, wallet purchases, orders, public-key ownership and public
|
||||||
|
HTTP authorization. The Python engine owns libvirt, host credentials and VM
|
||||||
|
operations. Keep the engine private and authenticate every internal request.
|
||||||
|
- Purchases provision one VM from the plan snapshot. Each purchase or renewal
|
||||||
|
creates an independent prepaid service period with its own traffic quota.
|
||||||
|
An exhausted quota disconnects VM networking until the next paid period;
|
||||||
|
final expiry disconnects networking without powering off the VM. An unpaid
|
||||||
|
renewal invoice enters a three-day grace period and then permanently removes
|
||||||
|
the VM definition and its panel-managed disks. Never replay an ambiguous
|
||||||
|
destructive operation or refund it without confirmed cleanup; reconcile its
|
||||||
|
operation identifier.
|
||||||
|
- Users share the original VM-panel styling; administrator pages retain venix.
|
||||||
|
- See `docs/PRUNING_REPORT.md` for retained/removed feature boundaries and
|
||||||
|
`docs/INTEGRATION_PLAN.md` for the planned Python engine integration.
|
||||||
@ -1,8 +0,0 @@
|
|||||||
# Third-party code
|
|
||||||
|
|
||||||
`static/vendor/novnc/` contains the `core/` and `vendor/` directories from
|
|
||||||
[noVNC v1.7.0](https://github.com/novnc/noVNC/releases/tag/v1.7.0), plus its
|
|
||||||
`LICENSE.txt`. The upstream source archive was downloaded from the noVNC GitHub
|
|
||||||
release tag without modifications. The bundled `vendor/pako/` dependency retains
|
|
||||||
its own license file.
|
|
||||||
|
|
||||||
@ -0,0 +1,106 @@
|
|||||||
|
{
|
||||||
|
"schema_version": 1,
|
||||||
|
"policy_version": 1,
|
||||||
|
"scope": "public_ipv4_interface",
|
||||||
|
"applies_to": "new_public_cloud_image_instances_only",
|
||||||
|
"migrate_existing": false,
|
||||||
|
"rules": [
|
||||||
|
{
|
||||||
|
"id": "qotd",
|
||||||
|
"label": "QOTD",
|
||||||
|
"category": "permanent",
|
||||||
|
"protocols": ["tcp", "udp"],
|
||||||
|
"port_from": 17,
|
||||||
|
"port_to": 17,
|
||||||
|
"directions": ["ingress", "egress"],
|
||||||
|
"reason": "降低反射放大与滥用风险。"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "chargen",
|
||||||
|
"label": "Chargen",
|
||||||
|
"category": "permanent",
|
||||||
|
"protocols": ["tcp", "udp"],
|
||||||
|
"port_from": 19,
|
||||||
|
"port_to": 19,
|
||||||
|
"directions": ["ingress", "egress"],
|
||||||
|
"reason": "降低反射放大与滥用风险。"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "ssdp",
|
||||||
|
"label": "SSDP",
|
||||||
|
"category": "permanent",
|
||||||
|
"protocols": ["tcp", "udp"],
|
||||||
|
"port_from": 1900,
|
||||||
|
"port_to": 1900,
|
||||||
|
"directions": ["ingress", "egress"],
|
||||||
|
"reason": "降低公网发现服务与反射放大滥用风险。"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "memcached",
|
||||||
|
"label": "Memcached UDP",
|
||||||
|
"category": "permanent",
|
||||||
|
"protocols": ["udp"],
|
||||||
|
"port_from": 11211,
|
||||||
|
"port_to": 11211,
|
||||||
|
"directions": ["ingress", "egress"],
|
||||||
|
"reason": "降低 Memcached UDP 反射放大风险。"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "brand-rex-53413",
|
||||||
|
"label": "Netis / Netcore 后门端口",
|
||||||
|
"category": "permanent",
|
||||||
|
"protocols": ["udp"],
|
||||||
|
"port_from": 53413,
|
||||||
|
"port_to": 53413,
|
||||||
|
"directions": ["ingress", "egress"],
|
||||||
|
"reason": "阻断已知路由器后门相关公网流量。"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "smtp-25",
|
||||||
|
"label": "SMTP 直投",
|
||||||
|
"category": "default",
|
||||||
|
"protocols": ["tcp"],
|
||||||
|
"port_from": 25,
|
||||||
|
"port_to": 25,
|
||||||
|
"directions": ["egress"],
|
||||||
|
"reason": "降低垃圾邮件滥用风险;例外申请流程尚未实现。"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "netbios",
|
||||||
|
"label": "NetBIOS",
|
||||||
|
"category": "default",
|
||||||
|
"protocols": ["tcp", "udp"],
|
||||||
|
"port_from": 137,
|
||||||
|
"port_to": 139,
|
||||||
|
"directions": ["ingress", "egress"],
|
||||||
|
"reason": "默认阻断 NetBIOS 服务暴露与公网访问。"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "smb",
|
||||||
|
"label": "SMB",
|
||||||
|
"category": "default",
|
||||||
|
"protocols": ["tcp", "udp"],
|
||||||
|
"port_from": 445,
|
||||||
|
"port_to": 445,
|
||||||
|
"directions": ["ingress", "egress"],
|
||||||
|
"reason": "默认阻断 SMB 文件共享服务的公网暴露与访问。"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "kms-1688",
|
||||||
|
"label": "KMS 服务端",
|
||||||
|
"category": "default",
|
||||||
|
"protocols": ["tcp"],
|
||||||
|
"port_from": 1688,
|
||||||
|
"port_to": 1688,
|
||||||
|
"directions": ["ingress"],
|
||||||
|
"reason": "默认阻断 KMS 服务端口直接暴露到公网。"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"explicitly_unrestricted": [
|
||||||
|
{
|
||||||
|
"protocol": "tcp",
|
||||||
|
"port": 587,
|
||||||
|
"reason": "SMTP 提交端口当前不受平台默认策略限制。"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
@ -1,22 +0,0 @@
|
|||||||
[Unit]
|
|
||||||
Description=Virsh Panel one-shot update from the pinned private Gitea repository
|
|
||||||
After=network-online.target
|
|
||||||
Wants=network-online.target
|
|
||||||
|
|
||||||
[Service]
|
|
||||||
Type=oneshot
|
|
||||||
User=root
|
|
||||||
Group=root
|
|
||||||
WorkingDirectory=/home/virsh-panel
|
|
||||||
Environment=PANEL_DATA_DIR=/home/virsh-panel/data
|
|
||||||
Environment=PYTHONDONTWRITEBYTECODE=1
|
|
||||||
ExecStart=/home/virsh-panel/.venv/bin/python -m virsh_panel.update_worker
|
|
||||||
ExecStopPost=/home/virsh-panel/.venv/bin/python -m virsh_panel.update_worker --finalize-incomplete
|
|
||||||
TimeoutStartSec=30min
|
|
||||||
UMask=0077
|
|
||||||
LimitCORE=0
|
|
||||||
NoNewPrivileges=true
|
|
||||||
PrivateTmp=true
|
|
||||||
ProtectSystem=strict
|
|
||||||
ProtectHome=false
|
|
||||||
ReadWritePaths=/home/virsh-panel
|
|
||||||
@ -1,26 +0,0 @@
|
|||||||
[Unit]
|
|
||||||
Description=Virsh Panel (local HTTP upstream and remote libvirt)
|
|
||||||
After=network-online.target
|
|
||||||
Wants=network-online.target
|
|
||||||
|
|
||||||
[Service]
|
|
||||||
Type=exec
|
|
||||||
User=root
|
|
||||||
Group=root
|
|
||||||
WorkingDirectory=/home/virsh-panel
|
|
||||||
Environment=PANEL_DATA_DIR=/home/virsh-panel/data
|
|
||||||
Environment=PYTHONDONTWRITEBYTECODE=1
|
|
||||||
ExecStartPre=+/usr/bin/install -d -o root -g root -m 0700 /home/virsh-panel/data
|
|
||||||
ExecStart=/home/virsh-panel/.venv/bin/python -m uvicorn virsh_panel.app:create_app --factory --host 127.0.0.1 --port 8000 --workers 1 --proxy-headers --forwarded-allow-ips 127.0.0.1
|
|
||||||
Restart=on-failure
|
|
||||||
RestartSec=5
|
|
||||||
UMask=0077
|
|
||||||
NoNewPrivileges=true
|
|
||||||
CapabilityBoundingSet=CAP_SETPCAP
|
|
||||||
PrivateTmp=true
|
|
||||||
ProtectSystem=strict
|
|
||||||
ProtectHome=false
|
|
||||||
ReadWritePaths=-/home/virsh-panel/data
|
|
||||||
|
|
||||||
[Install]
|
|
||||||
WantedBy=multi-user.target
|
|
||||||
@ -0,0 +1,25 @@
|
|||||||
|
[Unit]
|
||||||
|
Description=Virt Cloud private libvirt engine
|
||||||
|
After=network-online.target
|
||||||
|
Wants=network-online.target
|
||||||
|
|
||||||
|
[Service]
|
||||||
|
Type=exec
|
||||||
|
User=root
|
||||||
|
Group=root
|
||||||
|
WorkingDirectory=/home/virt-cloud/engine
|
||||||
|
Environment=CLOUD_ENGINE_CONFIG=/home/virt-cloud/runtime/engine.json
|
||||||
|
Environment=PYTHONDONTWRITEBYTECODE=1
|
||||||
|
ExecStart=/home/virt-cloud/engine/.venv/bin/python -m uvicorn virsh_panel.internal:create_engine --factory --host 127.0.0.1 --port 8000 --workers 1 --proxy-headers --forwarded-allow-ips 127.0.0.1 --no-access-log
|
||||||
|
Restart=on-failure
|
||||||
|
RestartSec=5
|
||||||
|
UMask=0077
|
||||||
|
NoNewPrivileges=true
|
||||||
|
PrivateTmp=true
|
||||||
|
ProtectSystem=strict
|
||||||
|
ProtectHome=false
|
||||||
|
ReadWritePaths=/home/virt-cloud/runtime
|
||||||
|
LimitCORE=0
|
||||||
|
|
||||||
|
[Install]
|
||||||
|
WantedBy=multi-user.target
|
||||||
@ -0,0 +1,24 @@
|
|||||||
|
[Unit]
|
||||||
|
Description=Virt Cloud service-period and traffic lifecycle worker
|
||||||
|
After=network-online.target virt-cloud-engine.service
|
||||||
|
Wants=network-online.target
|
||||||
|
|
||||||
|
[Service]
|
||||||
|
Type=simple
|
||||||
|
User=apache
|
||||||
|
Group=apache
|
||||||
|
WorkingDirectory=/home/virt-cloud/web
|
||||||
|
ExecStart=/usr/bin/php /home/virt-cloud/web/xcat cloudLifecycle
|
||||||
|
Restart=on-failure
|
||||||
|
RestartSec=5
|
||||||
|
UMask=0077
|
||||||
|
NoNewPrivileges=true
|
||||||
|
PrivateTmp=true
|
||||||
|
ProtectHome=false
|
||||||
|
ProtectSystem=strict
|
||||||
|
ReadWritePaths=-/home/virt-cloud/runtime/php
|
||||||
|
ReadWritePaths=-/home/virt-cloud/web/storage
|
||||||
|
TimeoutStopSec=30
|
||||||
|
|
||||||
|
[Install]
|
||||||
|
WantedBy=multi-user.target
|
||||||
@ -0,0 +1,24 @@
|
|||||||
|
[Unit]
|
||||||
|
Description=Virt Cloud enforcement policy worker %i
|
||||||
|
After=network-online.target virt-cloud-engine.service
|
||||||
|
Wants=network-online.target
|
||||||
|
|
||||||
|
[Service]
|
||||||
|
Type=simple
|
||||||
|
User=apache
|
||||||
|
Group=apache
|
||||||
|
WorkingDirectory=/home/virt-cloud/web
|
||||||
|
ExecStart=/usr/bin/php /home/virt-cloud/web/xcat cloudWorker --policy-only
|
||||||
|
Restart=on-failure
|
||||||
|
RestartSec=3
|
||||||
|
UMask=0077
|
||||||
|
NoNewPrivileges=true
|
||||||
|
PrivateTmp=true
|
||||||
|
ProtectHome=false
|
||||||
|
ProtectSystem=strict
|
||||||
|
ReadWritePaths=-/home/virt-cloud/runtime/php
|
||||||
|
ReadWritePaths=-/home/virt-cloud/web/storage
|
||||||
|
TimeoutStopSec=30
|
||||||
|
|
||||||
|
[Install]
|
||||||
|
WantedBy=multi-user.target
|
||||||
@ -0,0 +1,23 @@
|
|||||||
|
[Unit]
|
||||||
|
Description=Virt Cloud privileged online update worker
|
||||||
|
After=network-online.target mariadb.service
|
||||||
|
Wants=network-online.target
|
||||||
|
|
||||||
|
[Service]
|
||||||
|
Type=oneshot
|
||||||
|
User=root
|
||||||
|
Group=root
|
||||||
|
WorkingDirectory=/home/virt-cloud/engine
|
||||||
|
Environment=CLOUD_ENGINE_CONFIG=/home/virt-cloud/runtime/engine.json
|
||||||
|
Environment=PYTHONDONTWRITEBYTECODE=1
|
||||||
|
ExecStart=/home/virt-cloud/engine/.venv/bin/python -m virsh_panel.update_worker
|
||||||
|
ExecStopPost=/home/virt-cloud/engine/.venv/bin/python -m virsh_panel.update_worker --finalize-incomplete
|
||||||
|
TimeoutStartSec=45min
|
||||||
|
TimeoutStopSec=10min
|
||||||
|
UMask=0077
|
||||||
|
NoNewPrivileges=true
|
||||||
|
PrivateTmp=true
|
||||||
|
ProtectSystem=strict
|
||||||
|
ProtectHome=false
|
||||||
|
ReadWritePaths=/home/virt-cloud
|
||||||
|
LimitCORE=0
|
||||||
@ -0,0 +1,25 @@
|
|||||||
|
[Unit]
|
||||||
|
Description=Virt Cloud purchased VM provisioning worker
|
||||||
|
After=network-online.target virt-cloud-engine.service
|
||||||
|
Wants=network-online.target
|
||||||
|
|
||||||
|
[Service]
|
||||||
|
Type=simple
|
||||||
|
# AlmaLinux PHP-FPM normally runs as apache; use the same user as your PHP pool.
|
||||||
|
User=apache
|
||||||
|
Group=apache
|
||||||
|
WorkingDirectory=/home/virt-cloud/web
|
||||||
|
ExecStart=/usr/bin/php /home/virt-cloud/web/xcat cloudWorker
|
||||||
|
Restart=on-failure
|
||||||
|
RestartSec=5
|
||||||
|
UMask=0077
|
||||||
|
NoNewPrivileges=true
|
||||||
|
PrivateTmp=true
|
||||||
|
ProtectHome=false
|
||||||
|
ProtectSystem=strict
|
||||||
|
ReadWritePaths=-/home/virt-cloud/runtime/php
|
||||||
|
ReadWritePaths=-/home/virt-cloud/web/storage
|
||||||
|
TimeoutStopSec=30
|
||||||
|
|
||||||
|
[Install]
|
||||||
|
WantedBy=multi-user.target
|
||||||
@ -0,0 +1,5 @@
|
|||||||
|
.venv/
|
||||||
|
data/
|
||||||
|
__pycache__/
|
||||||
|
*.py[cod]
|
||||||
|
.pytest_cache/
|
||||||
|
Before Width: | Height: | Size: 3.2 KiB After Width: | Height: | Size: 3.2 KiB |
|
Before Width: | Height: | Size: 1.7 KiB After Width: | Height: | Size: 1.7 KiB |
|
Before Width: | Height: | Size: 4.9 KiB After Width: | Height: | Size: 4.9 KiB |
|
Before Width: | Height: | Size: 1.3 KiB After Width: | Height: | Size: 1.3 KiB |
@ -0,0 +1,496 @@
|
|||||||
|
"""Cloud-init public networking and NoCloud seed behavior."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import importlib.util
|
||||||
|
import json
|
||||||
|
import uuid
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from virsh_panel.cloud_init import (
|
||||||
|
ROOT_PASSWORD_ALPHABET, ROOT_PASSWORD_DIGITS, ROOT_PASSWORD_LENGTH,
|
||||||
|
ROOT_PASSWORD_LOWERCASE, ROOT_PASSWORD_SYMBOLS, ROOT_PASSWORD_UPPERCASE,
|
||||||
|
_build_seed_iso, _enable_network_updates, build_seed_iso,
|
||||||
|
generate_root_password,
|
||||||
|
hash_root_password,
|
||||||
|
read_seed_network, replace_seed_network, replace_seed_settings,
|
||||||
|
validate_cloud_network, validate_guest_hostname,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
PUBLIC_KEY = (
|
||||||
|
"ssh-ed25519 "
|
||||||
|
"AAAAC3NzaC1lZDI1NTE5AAAAIF7mFh4aYz3lRzNqjoFQasAQLpm7BdSULhe1iWuvj3y5 "
|
||||||
|
"panel-test"
|
||||||
|
)
|
||||||
|
GUEST_PASSWORD = "A-strong-guest-password-2026"
|
||||||
|
|
||||||
|
|
||||||
|
def _user_data(seed: bytes) -> dict:
|
||||||
|
"""Read the single-line JSON cloud-config embedded in the generated ISO."""
|
||||||
|
prefix = b"#cloud-config\n"
|
||||||
|
start = seed.index(prefix) + len(prefix)
|
||||||
|
return json.loads(seed[start:seed.index(b"\n", start)])
|
||||||
|
|
||||||
|
|
||||||
|
def _meta_data(seed: bytes) -> dict:
|
||||||
|
"""Read the single-line JSON meta-data embedded in the generated ISO."""
|
||||||
|
prefix = b'{"instance-id":'
|
||||||
|
start = seed.index(prefix)
|
||||||
|
return json.loads(seed[start:seed.index(b"\n", start)])
|
||||||
|
|
||||||
|
|
||||||
|
def _network_data(seed: bytes) -> dict:
|
||||||
|
"""Read the single-line JSON network-config embedded in the ISO."""
|
||||||
|
prefix = b'{"version": 2, "ethernets":'
|
||||||
|
start = seed.index(prefix)
|
||||||
|
return json.loads(seed[start:seed.index(b"\n", start)])
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
("address", "gateway"),
|
||||||
|
[
|
||||||
|
("8.8.8.18/24", "8.8.8.1"),
|
||||||
|
("8.8.8.18/28", "8.8.8.17"),
|
||||||
|
("123.123.123.114/24", "123.123.123.113"),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_public_network_accepts_24_and_28(address, gateway):
|
||||||
|
validated = validate_cloud_network(
|
||||||
|
address, gateway, ["1.1.1.1", "8.8.8.8"], PUBLIC_KEY,
|
||||||
|
)
|
||||||
|
assert validated == {
|
||||||
|
"public_ip_cidr": address,
|
||||||
|
"gateway": gateway,
|
||||||
|
"dns_servers": ["1.1.1.1", "8.8.8.8"],
|
||||||
|
"guest_ssh_public_key": PUBLIC_KEY,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def test_cloud_login_accepts_key_and_root_password_together():
|
||||||
|
network = ("8.8.8.18/24", "8.8.8.1", ["1.1.1.1"])
|
||||||
|
password_hash = hash_root_password(GUEST_PASSWORD)
|
||||||
|
assert password_hash.startswith("$6$")
|
||||||
|
assert GUEST_PASSWORD not in password_hash
|
||||||
|
assert hash_root_password(GUEST_PASSWORD) != password_hash
|
||||||
|
|
||||||
|
validated = validate_cloud_network(
|
||||||
|
*network, "", root_password_hash=password_hash,
|
||||||
|
)
|
||||||
|
assert validated["root_password_hash"] == password_hash
|
||||||
|
assert not validated["guest_ssh_public_key"]
|
||||||
|
|
||||||
|
together = validate_cloud_network(
|
||||||
|
*network, PUBLIC_KEY, root_password_hash=password_hash,
|
||||||
|
)
|
||||||
|
assert together["guest_ssh_public_key"] == PUBLIC_KEY
|
||||||
|
assert together["root_password_hash"] == password_hash
|
||||||
|
|
||||||
|
with pytest.raises(ValueError):
|
||||||
|
validate_cloud_network(*network, "", root_password_hash=None)
|
||||||
|
with pytest.raises(ValueError):
|
||||||
|
validate_cloud_network(*network, "", root_password_hash=GUEST_PASSWORD)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("invalid", [
|
||||||
|
"Short1!", "alllowercase12!", "ALLUPPERCASE12!", "NoSymbolPassword12",
|
||||||
|
"NoDigitsLong!Aa", "A-lowercase password12!", "A-lowercase\npassword12!",
|
||||||
|
"A-lowercase-password-1" + "x" * 110, "ValidAscii1!含中文",
|
||||||
|
])
|
||||||
|
def test_manual_root_password_requires_ascii_and_all_character_classes(invalid):
|
||||||
|
with pytest.raises(ValueError, match="初始 root 密码"):
|
||||||
|
hash_root_password(invalid)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("value", [
|
||||||
|
"node-1", "node.example.com", "A-Node.Example",
|
||||||
|
])
|
||||||
|
def test_guest_hostname_is_canonical_and_bounded(value):
|
||||||
|
assert validate_guest_hostname(value) == value.lower()
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("value", [
|
||||||
|
"", ".node", "node.", "node..example", "-node", "node-",
|
||||||
|
"node_name", "node with spaces", "节点", "a" * 64,
|
||||||
|
])
|
||||||
|
def test_guest_hostname_rejects_unsafe_values(value):
|
||||||
|
with pytest.raises(ValueError):
|
||||||
|
validate_guest_hostname(value)
|
||||||
|
|
||||||
|
|
||||||
|
def test_generated_root_password_always_meets_policy():
|
||||||
|
assert ROOT_PASSWORD_ALPHABET == (
|
||||||
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
|
||||||
|
"0123456789!@#$%*-_=+?"
|
||||||
|
)
|
||||||
|
samples = set()
|
||||||
|
for _ in range(50):
|
||||||
|
password = generate_root_password()
|
||||||
|
samples.add(password)
|
||||||
|
assert len(password) == ROOT_PASSWORD_LENGTH
|
||||||
|
assert set(password) <= set(ROOT_PASSWORD_ALPHABET)
|
||||||
|
assert any(char in ROOT_PASSWORD_UPPERCASE for char in password)
|
||||||
|
assert any(char in ROOT_PASSWORD_LOWERCASE for char in password)
|
||||||
|
assert any(char in ROOT_PASSWORD_DIGITS for char in password)
|
||||||
|
assert any(char in ROOT_PASSWORD_SYMBOLS for char in password)
|
||||||
|
assert len(samples) == 50
|
||||||
|
assert len({password[:4] for password in samples}) > 1
|
||||||
|
assert hash_root_password(password).startswith("$6$")
|
||||||
|
|
||||||
|
|
||||||
|
def test_generated_root_password_rejects_whole_noncompliant_candidate(monkeypatch):
|
||||||
|
# First candidate is 32 uppercase A characters and must be discarded.
|
||||||
|
# The second one is a known compliant candidate with no post-processing.
|
||||||
|
indexes = iter([0] * 32 + [0, 26, 52, 62] * 8)
|
||||||
|
bounds = []
|
||||||
|
|
||||||
|
def deterministic_randbelow(bound):
|
||||||
|
bounds.append(bound)
|
||||||
|
return next(indexes)
|
||||||
|
|
||||||
|
monkeypatch.setattr("virsh_panel.cloud_init.secrets.randbelow",
|
||||||
|
deterministic_randbelow)
|
||||||
|
assert generate_root_password() == "Aa0!" * 8
|
||||||
|
assert bounds == [len(ROOT_PASSWORD_ALPHABET)] * 64
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
("address", "gateway"),
|
||||||
|
[
|
||||||
|
("192.168.1.18/24", "192.168.1.1"),
|
||||||
|
("10.0.0.18/24", "10.0.0.1"),
|
||||||
|
("172.16.0.18/24", "172.16.0.1"),
|
||||||
|
("127.0.0.18/24", "127.0.0.1"),
|
||||||
|
("169.254.0.18/24", "169.254.0.1"),
|
||||||
|
("224.0.0.18/24", "224.0.0.1"),
|
||||||
|
("2001:4860:4860::8888/64", "2001:4860:4860::1"),
|
||||||
|
("8.8.8.0/24", "8.8.8.1"),
|
||||||
|
("8.8.8.255/24", "8.8.8.1"),
|
||||||
|
("8.8.8.18/28", "8.8.8.1"),
|
||||||
|
("8.8.8.18", "8.8.8.1"),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_public_network_rejects_nonpublic_or_unroutable_pairs(address, gateway):
|
||||||
|
with pytest.raises(ValueError):
|
||||||
|
validate_cloud_network(address, gateway, ["1.1.1.1"], PUBLIC_KEY)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
("dns", "key"),
|
||||||
|
[
|
||||||
|
(["not-an-ip"], PUBLIC_KEY),
|
||||||
|
(["192.168.1.1"], PUBLIC_KEY),
|
||||||
|
([], PUBLIC_KEY),
|
||||||
|
(["1.1.1.1"], "not-an-ssh-key"),
|
||||||
|
(["1.1.1.1"], PUBLIC_KEY + "\nruncmd: [evil]"),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_public_network_rejects_bad_dns_and_ssh_key(dns, key):
|
||||||
|
with pytest.raises(ValueError):
|
||||||
|
validate_cloud_network("8.8.8.18/24", "8.8.8.1", dns, key)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.skipif(importlib.util.find_spec("pycdlib") is None,
|
||||||
|
reason="pycdlib is not installed")
|
||||||
|
def test_seed_iso_has_nocloud_files_and_mac_specific_static_network():
|
||||||
|
vm_uuid = str(uuid.uuid4())
|
||||||
|
mac = "52:54:00:12:34:56"
|
||||||
|
validated = validate_cloud_network(
|
||||||
|
"8.8.8.18/28", "8.8.8.17", ["1.1.1.1"], PUBLIC_KEY,
|
||||||
|
)
|
||||||
|
seed = build_seed_iso(vm_uuid, "cloud-vm", mac, validated)
|
||||||
|
|
||||||
|
assert isinstance(seed, bytes)
|
||||||
|
assert seed[32769:32774] == b"CD001" # ISO-9660 primary volume descriptor
|
||||||
|
assert b"CIDATA" in seed
|
||||||
|
for marker in (
|
||||||
|
b"user-data", b"meta-data", b"network-config", b"8.8.8.18/28",
|
||||||
|
b"8.8.8.17", b"1.1.1.1", mac.encode(), PUBLIC_KEY.encode(),
|
||||||
|
):
|
||||||
|
assert marker in seed
|
||||||
|
assert b'"routes": [{"to": "0.0.0.0/0", "via": "8.8.8.17"}]' in seed
|
||||||
|
assert b'"to": "default"' not in seed
|
||||||
|
assert b'"renderer": "networkd"' not in seed
|
||||||
|
assert b'"set-name"' not in seed
|
||||||
|
assert b"IPv6AcceptRA=no" not in seed
|
||||||
|
assert b"systemd-machine-id-setup" not in seed
|
||||||
|
user_data = _user_data(seed)
|
||||||
|
metadata = _meta_data(seed)
|
||||||
|
assert user_data["disable_root"] is False
|
||||||
|
assert user_data["ssh_pwauth"] is False
|
||||||
|
assert user_data["updates"] == {"network": {"when": ["boot"]}}
|
||||||
|
assert metadata["virt-cloud-network-policy"] == "boot-v1"
|
||||||
|
assert user_data["users"][0]["name"] == "root"
|
||||||
|
assert user_data["users"][0]["ssh_authorized_keys"] == [PUBLIC_KEY]
|
||||||
|
assert "hashed_passwd" not in user_data["users"][0]
|
||||||
|
assert "passwd" not in user_data["users"][0]
|
||||||
|
sshd_dropin = user_data["write_files"][0]
|
||||||
|
assert sshd_dropin["path"] == "/etc/ssh/sshd_config.d/00-virsh-panel-root.conf"
|
||||||
|
assert "PermitRootLogin prohibit-password" in sshd_dropin["content"]
|
||||||
|
assert "PasswordAuthentication no" in sshd_dropin["content"]
|
||||||
|
assert "runcmd" not in user_data
|
||||||
|
assert "bootcmd" not in user_data
|
||||||
|
assert "dsmode" not in metadata
|
||||||
|
assert all(
|
||||||
|
item["path"] != "/etc/systemd/network/05-virt-cloud-public.network"
|
||||||
|
for item in user_data["write_files"]
|
||||||
|
)
|
||||||
|
assert b"almalinux" not in seed
|
||||||
|
assert read_seed_network(seed) == {
|
||||||
|
"public_ip_cidr": "8.8.8.18/28",
|
||||||
|
"gateway": "8.8.8.17",
|
||||||
|
"dns_servers": ["1.1.1.1"],
|
||||||
|
"mac": mac,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def test_enable_network_updates_upgrades_legacy_panel_user_data():
|
||||||
|
legacy = b'#cloud-config\n{"users": [{"name": "root"}]}\n'
|
||||||
|
|
||||||
|
upgraded = _enable_network_updates(legacy)
|
||||||
|
|
||||||
|
assert upgraded.startswith(b"#cloud-config\n")
|
||||||
|
payload = json.loads(upgraded.split(b"\n", 1)[1])
|
||||||
|
assert payload["users"] == [{"name": "root"}]
|
||||||
|
assert payload["updates"] == {"network": {"when": ["boot"]}}
|
||||||
|
assert _enable_network_updates(upgraded) == upgraded
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.skipif(importlib.util.find_spec("pycdlib") is None,
|
||||||
|
reason="pycdlib is not installed")
|
||||||
|
def test_debian12_network_fix_uses_standard_nocloud_network_config():
|
||||||
|
mac = "52:54:00:12:34:56"
|
||||||
|
validated = validate_cloud_network(
|
||||||
|
"8.8.8.18/28", "8.8.8.17", ["1.1.1.1", "8.8.8.8"], PUBLIC_KEY,
|
||||||
|
)
|
||||||
|
seed = build_seed_iso(
|
||||||
|
str(uuid.uuid4()), "debian-12-vm", mac, validated,
|
||||||
|
debian12_network_fix=True,
|
||||||
|
)
|
||||||
|
user_data = _user_data(seed)
|
||||||
|
metadata = _meta_data(seed)
|
||||||
|
network_data = _network_data(seed)
|
||||||
|
|
||||||
|
assert network_data == {
|
||||||
|
"version": 2,
|
||||||
|
"ethernets": {
|
||||||
|
"public0": {
|
||||||
|
"match": {"macaddress": mac},
|
||||||
|
"dhcp4": False,
|
||||||
|
"dhcp6": False,
|
||||||
|
"addresses": ["8.8.8.18/28"],
|
||||||
|
"routes": [{"to": "0.0.0.0/0", "via": "8.8.8.17"}],
|
||||||
|
"nameservers": {"addresses": ["1.1.1.1", "8.8.8.8"]},
|
||||||
|
"accept-ra": False,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"renderer": "networkd",
|
||||||
|
}
|
||||||
|
assert metadata["dsmode"] == "local"
|
||||||
|
bootcmd = user_data["bootcmd"]
|
||||||
|
assert bootcmd[0][:2] == ["/bin/sh", "-ec"]
|
||||||
|
queue_networkd = bootcmd[0][2]
|
||||||
|
assert queue_networkd.splitlines() == [
|
||||||
|
"systemd-machine-id-setup",
|
||||||
|
"netplan generate",
|
||||||
|
"systemctl unmask systemd-networkd.service",
|
||||||
|
"systemctl enable systemd-networkd.service",
|
||||||
|
"systemctl start --no-block systemd-networkd.service",
|
||||||
|
]
|
||||||
|
assert "ip address" not in queue_networkd
|
||||||
|
assert "ip route" not in queue_networkd
|
||||||
|
assert "restart" not in json.dumps(bootcmd)
|
||||||
|
assert "--now" not in json.dumps(bootcmd)
|
||||||
|
assert "runcmd" not in user_data
|
||||||
|
assert b'"set-name"' not in seed
|
||||||
|
assert b"enp1s0" not in seed
|
||||||
|
assert all(
|
||||||
|
not item["path"].startswith("/etc/systemd/network/")
|
||||||
|
for item in user_data["write_files"]
|
||||||
|
)
|
||||||
|
assert read_seed_network(seed) == {
|
||||||
|
"public_ip_cidr": "8.8.8.18/28",
|
||||||
|
"gateway": "8.8.8.17",
|
||||||
|
"dns_servers": ["1.1.1.1", "8.8.8.8"],
|
||||||
|
"mac": mac,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("invalid", [None, 0, 1, "networkd"])
|
||||||
|
def test_seed_rejects_invalid_debian12_network_fix_option(invalid):
|
||||||
|
validated = validate_cloud_network(
|
||||||
|
"8.8.8.18/28", "8.8.8.17", ["1.1.1.1"], PUBLIC_KEY,
|
||||||
|
)
|
||||||
|
with pytest.raises(ValueError, match="Debian 12 网络修复选项"):
|
||||||
|
build_seed_iso(
|
||||||
|
str(uuid.uuid4()), "cloud-vm", "52:54:00:12:34:56", validated,
|
||||||
|
debian12_network_fix=invalid,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.skipif(importlib.util.find_spec("pycdlib") is None,
|
||||||
|
reason="pycdlib is not installed")
|
||||||
|
def test_read_seed_network_rejects_invalid_iso_without_exposing_credentials():
|
||||||
|
with pytest.raises(ValueError):
|
||||||
|
read_seed_network(b"not an ISO")
|
||||||
|
with pytest.raises(ValueError):
|
||||||
|
read_seed_network(b"x" * (1024 * 1024 + 1))
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.skipif(importlib.util.find_spec("pycdlib") is None,
|
||||||
|
reason="pycdlib is not installed")
|
||||||
|
def test_password_seed_sets_root_hash_and_enables_ssh_password_login():
|
||||||
|
password_hash = hash_root_password(GUEST_PASSWORD)
|
||||||
|
validated = validate_cloud_network(
|
||||||
|
"8.8.8.18/28", "8.8.8.17", ["1.1.1.1"], "",
|
||||||
|
root_password_hash=password_hash,
|
||||||
|
)
|
||||||
|
seed = build_seed_iso(
|
||||||
|
str(uuid.uuid4()), "password-vm", "52:54:00:12:34:56", validated,
|
||||||
|
)
|
||||||
|
user_data = _user_data(seed)
|
||||||
|
|
||||||
|
assert user_data["disable_root"] is False
|
||||||
|
assert user_data["ssh_pwauth"] is True
|
||||||
|
assert user_data["users"][0]["name"] == "root"
|
||||||
|
assert user_data["users"][0]["lock_passwd"] is False
|
||||||
|
assert user_data["users"][0]["hashed_passwd"] == password_hash
|
||||||
|
assert "ssh_authorized_keys" not in user_data["users"][0]
|
||||||
|
assert user_data["chpasswd"] == {
|
||||||
|
"expire": False,
|
||||||
|
"users": [{"name": "root", "password": password_hash, "type": "hash"}],
|
||||||
|
}
|
||||||
|
sshd_dropin = user_data["write_files"][0]
|
||||||
|
assert sshd_dropin["path"] == "/etc/ssh/sshd_config.d/00-virsh-panel-root.conf"
|
||||||
|
assert "PermitRootLogin yes" in sshd_dropin["content"]
|
||||||
|
assert "PasswordAuthentication yes" in sshd_dropin["content"]
|
||||||
|
assert GUEST_PASSWORD.encode() not in seed
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.skipif(importlib.util.find_spec("pycdlib") is None,
|
||||||
|
reason="pycdlib is not installed")
|
||||||
|
def test_seed_allows_root_password_and_ssh_key_together():
|
||||||
|
password_hash = hash_root_password(GUEST_PASSWORD)
|
||||||
|
validated = validate_cloud_network(
|
||||||
|
"8.8.8.18/28", "8.8.8.17", ["1.1.1.1"], PUBLIC_KEY,
|
||||||
|
root_password_hash=password_hash,
|
||||||
|
)
|
||||||
|
seed = build_seed_iso(
|
||||||
|
str(uuid.uuid4()), "both-vm", "52:54:00:12:34:56", validated,
|
||||||
|
)
|
||||||
|
user_data = _user_data(seed)
|
||||||
|
root = user_data["users"][0]
|
||||||
|
assert root["name"] == "root"
|
||||||
|
assert root["lock_passwd"] is False
|
||||||
|
assert root["ssh_authorized_keys"] == [PUBLIC_KEY]
|
||||||
|
assert root["hashed_passwd"] == password_hash
|
||||||
|
assert user_data["ssh_pwauth"] is True
|
||||||
|
assert user_data["chpasswd"]["users"][0]["password"] == password_hash
|
||||||
|
assert "PermitRootLogin yes" in user_data["write_files"][0]["content"]
|
||||||
|
assert "PasswordAuthentication yes" in user_data["write_files"][0]["content"]
|
||||||
|
assert GUEST_PASSWORD.encode() not in seed
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.skipif(importlib.util.find_spec("pycdlib") is None,
|
||||||
|
reason="pycdlib is not installed")
|
||||||
|
def test_replace_seed_network_preserves_user_data_and_metadata():
|
||||||
|
password_hash = hash_root_password(GUEST_PASSWORD)
|
||||||
|
validated = validate_cloud_network(
|
||||||
|
"8.8.8.18/28", "8.8.8.17", ["1.1.1.1"], PUBLIC_KEY,
|
||||||
|
root_password_hash=password_hash,
|
||||||
|
)
|
||||||
|
seed = build_seed_iso(
|
||||||
|
str(uuid.uuid4()), "network-change-vm", "52:54:00:12:34:56", validated,
|
||||||
|
)
|
||||||
|
import pycdlib
|
||||||
|
from io import BytesIO
|
||||||
|
|
||||||
|
def file_bytes(image: bytes, path: str) -> bytes:
|
||||||
|
iso = pycdlib.PyCdlib()
|
||||||
|
iso.open_fp(BytesIO(image))
|
||||||
|
output = BytesIO()
|
||||||
|
iso.get_file_from_iso_fp(output, rr_path=path)
|
||||||
|
iso.close()
|
||||||
|
return output.getvalue()
|
||||||
|
|
||||||
|
user_data = file_bytes(seed, "/user-data")
|
||||||
|
metadata = file_bytes(seed, "/meta-data")
|
||||||
|
original_metadata = json.loads(metadata)
|
||||||
|
changed = replace_seed_network(
|
||||||
|
seed, "8.8.8.19/28", "8.8.8.17", ["8.8.8.8"],
|
||||||
|
)
|
||||||
|
assert file_bytes(changed, "/user-data") == user_data
|
||||||
|
changed_metadata = json.loads(file_bytes(changed, "/meta-data"))
|
||||||
|
assert changed_metadata["instance-id"] == original_metadata["instance-id"]
|
||||||
|
assert changed_metadata["local-hostname"] == original_metadata["local-hostname"]
|
||||||
|
assert changed_metadata["virt-cloud-network-policy"] == "boot-v1"
|
||||||
|
assert read_seed_network(changed) == {
|
||||||
|
"public_ip_cidr": "8.8.8.19/28",
|
||||||
|
"gateway": "8.8.8.17",
|
||||||
|
"dns_servers": ["8.8.8.8"],
|
||||||
|
"mac": "52:54:00:12:34:56",
|
||||||
|
}
|
||||||
|
|
||||||
|
# A seed created before the network-refresh policy has no marker and must
|
||||||
|
# rotate its NoCloud generation once so cloud-init reloads the new files.
|
||||||
|
legacy_user_data = b'#cloud-config\n{"users": [{"name": "root"}]}\n'
|
||||||
|
legacy_metadata = json.dumps({
|
||||||
|
"instance-id": original_metadata["instance-id"],
|
||||||
|
"local-hostname": original_metadata["local-hostname"],
|
||||||
|
}).encode("ascii") + b"\n"
|
||||||
|
legacy_network = file_bytes(seed, "/network-config")
|
||||||
|
legacy_seed = _build_seed_iso((
|
||||||
|
("/USERDATA.;1", "user-data", "/user-data", legacy_user_data),
|
||||||
|
("/METADATA.;1", "meta-data", "/meta-data", legacy_metadata),
|
||||||
|
("/NETWORK.;1", "network-config", "/network-config", legacy_network),
|
||||||
|
))
|
||||||
|
migrated = replace_seed_network(
|
||||||
|
legacy_seed, "8.8.8.19/28", "8.8.8.17", ["8.8.8.8"],
|
||||||
|
)
|
||||||
|
migrated_metadata = json.loads(file_bytes(migrated, "/meta-data"))
|
||||||
|
assert migrated_metadata["instance-id"] != original_metadata["instance-id"]
|
||||||
|
assert migrated_metadata["instance-id"].startswith("virt-cloud-network-")
|
||||||
|
migrated_user_data = file_bytes(migrated, "/user-data")
|
||||||
|
assert json.loads(migrated_user_data.split(b"\n", 1)[1])["updates"] == {
|
||||||
|
"network": {"when": ["boot"]}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.skipif(importlib.util.find_spec("pycdlib") is None,
|
||||||
|
reason="pycdlib is not installed")
|
||||||
|
def test_replace_seed_network_rejects_non_panel_seed():
|
||||||
|
with pytest.raises(ValueError, match="无法读取原 cloud-init 配置盘"):
|
||||||
|
replace_seed_network(
|
||||||
|
b"not-an-iso", "8.8.8.19/28", "8.8.8.17", ["8.8.8.8"],
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.skipif(importlib.util.find_spec("pycdlib") is None,
|
||||||
|
reason="pycdlib is not installed")
|
||||||
|
def test_replace_seed_settings_rotates_generation_without_exposing_password():
|
||||||
|
password_hash = hash_root_password(GUEST_PASSWORD)
|
||||||
|
validated = validate_cloud_network(
|
||||||
|
"8.8.8.18/28", "8.8.8.17", ["1.1.1.1"], PUBLIC_KEY,
|
||||||
|
root_password_hash=password_hash,
|
||||||
|
)
|
||||||
|
seed = build_seed_iso(
|
||||||
|
str(uuid.uuid4()), "settings-vm", "52:54:00:12:34:56", validated,
|
||||||
|
)
|
||||||
|
changed = replace_seed_settings(
|
||||||
|
seed, hostname="Node.Example", root_password_hash=hash_root_password(
|
||||||
|
"Another-strong-password-2026!"
|
||||||
|
), ssh_public_key=PUBLIC_KEY,
|
||||||
|
)
|
||||||
|
user_data = _user_data(changed)
|
||||||
|
root = user_data["users"][0]
|
||||||
|
assert root["ssh_authorized_keys"] == [PUBLIC_KEY]
|
||||||
|
assert root["lock_passwd"] is False
|
||||||
|
assert root["hashed_passwd"].startswith("$6$")
|
||||||
|
assert GUEST_PASSWORD.encode() not in changed
|
||||||
|
assert b"Another-strong-password-2026!" not in changed
|
||||||
|
assert user_data["ssh_pwauth"] is True
|
||||||
|
assert user_data["chpasswd"]["users"][0]["type"] == "hash"
|
||||||
|
assert _meta_data(changed)["local-hostname"] == "node.example"
|
||||||
|
assert _meta_data(changed)["instance-id"].startswith("virt-cloud-settings-")
|
||||||
|
assert read_seed_network(changed) == read_seed_network(seed)
|
||||||
@ -0,0 +1,750 @@
|
|||||||
|
"""Global cloud-template catalog, source file and import job tests."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import hashlib
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
import stat
|
||||||
|
import threading
|
||||||
|
import time
|
||||||
|
from pathlib import Path
|
||||||
|
from types import SimpleNamespace
|
||||||
|
from unittest.mock import patch
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from virsh_panel.cloud_template_runtime import (
|
||||||
|
CACHE_TOMBSTONE_KIND,
|
||||||
|
CATALOG_KIND,
|
||||||
|
IMPORT_JOB_KIND,
|
||||||
|
CloudTemplateRuntime,
|
||||||
|
GlobalCloudTemplateFiles,
|
||||||
|
GlobalCloudTemplateStore,
|
||||||
|
PERSISTENT_FREE_RESERVE_BYTES,
|
||||||
|
cloud_template_id_for,
|
||||||
|
)
|
||||||
|
from virsh_panel.cloud_templates import GIB, TemplateError, template_volume_name
|
||||||
|
|
||||||
|
|
||||||
|
ALMA_10_URL = (
|
||||||
|
"https://repo.almalinux.org/almalinux/10/cloud/x86_64/images/"
|
||||||
|
"AlmaLinux-10-GenericCloud-latest.x86_64.qcow2"
|
||||||
|
)
|
||||||
|
ALMA_8_URL = (
|
||||||
|
"https://repo.almalinux.org/almalinux/8/cloud/x86_64/images/"
|
||||||
|
"AlmaLinux-8-GenericCloud-latest.x86_64.qcow2"
|
||||||
|
)
|
||||||
|
DEBIAN_12_URL = (
|
||||||
|
"https://cloud.debian.org/images/cloud/bookworm/latest/"
|
||||||
|
"debian-12-generic-amd64.qcow2"
|
||||||
|
)
|
||||||
|
UBUNTU_24_URL = (
|
||||||
|
"https://cloud-images.ubuntu.com/releases/24.04/release/"
|
||||||
|
"ubuntu-24.04-server-cloudimg-amd64.img"
|
||||||
|
)
|
||||||
|
CENTOS_STREAM_10_URL = (
|
||||||
|
"https://cloud.centos.org/centos/10-stream/x86_64/images/"
|
||||||
|
"CentOS-Stream-GenericCloud-10-latest.x86_64.qcow2"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class MemoryStore:
|
||||||
|
def __init__(self) -> None:
|
||||||
|
self.values: dict[tuple[str, str], str] = {}
|
||||||
|
self.lock = threading.RLock()
|
||||||
|
|
||||||
|
def list(self, kind: str) -> dict[str, str]:
|
||||||
|
with self.lock:
|
||||||
|
return {
|
||||||
|
key: value for (record_kind, key), value in self.values.items()
|
||||||
|
if record_kind == kind
|
||||||
|
}
|
||||||
|
|
||||||
|
def get(self, kind: str, key: str) -> str | None:
|
||||||
|
with self.lock:
|
||||||
|
return self.values.get((kind, key))
|
||||||
|
|
||||||
|
def set(self, kind: str, key: str, value: str) -> None:
|
||||||
|
with self.lock:
|
||||||
|
self.values[(kind, key)] = value
|
||||||
|
|
||||||
|
def insert(self, kind: str, key: str, value: str) -> bool:
|
||||||
|
with self.lock:
|
||||||
|
identity = (kind, key)
|
||||||
|
if identity in self.values:
|
||||||
|
return False
|
||||||
|
self.values[identity] = value
|
||||||
|
return True
|
||||||
|
|
||||||
|
def delete(self, kind: str, key: str) -> None:
|
||||||
|
with self.lock:
|
||||||
|
self.values.pop((kind, key), None)
|
||||||
|
|
||||||
|
|
||||||
|
class CommitThenRaiseCatalogStore(MemoryStore):
|
||||||
|
"""Simulate a committed INSERT whose acknowledgement is lost."""
|
||||||
|
|
||||||
|
def insert(self, kind: str, key: str, value: str) -> bool:
|
||||||
|
inserted = super().insert(kind, key, value)
|
||||||
|
if kind == CATALOG_KIND and inserted:
|
||||||
|
raise RuntimeError("catalog insert acknowledgement lost")
|
||||||
|
return inserted
|
||||||
|
|
||||||
|
|
||||||
|
class AmbiguousCatalogCommitStore(CommitThenRaiseCatalogStore):
|
||||||
|
"""Also fail the first catalog read after the ambiguous INSERT."""
|
||||||
|
|
||||||
|
def __init__(self) -> None:
|
||||||
|
super().__init__()
|
||||||
|
self.fail_next_catalog_read = False
|
||||||
|
|
||||||
|
def list(self, kind: str) -> dict[str, str]:
|
||||||
|
if kind == CATALOG_KIND and self.fail_next_catalog_read:
|
||||||
|
self.fail_next_catalog_read = False
|
||||||
|
raise RuntimeError("catalog read unavailable")
|
||||||
|
return super().list(kind)
|
||||||
|
|
||||||
|
def insert(self, kind: str, key: str, value: str) -> bool:
|
||||||
|
try:
|
||||||
|
return super().insert(kind, key, value)
|
||||||
|
except RuntimeError:
|
||||||
|
self.fail_next_catalog_read = True
|
||||||
|
raise
|
||||||
|
|
||||||
|
|
||||||
|
class LoseFirstCompletedJobWriteStore(MemoryStore):
|
||||||
|
"""Drop one terminal job update while retaining its running record."""
|
||||||
|
|
||||||
|
def __init__(self) -> None:
|
||||||
|
super().__init__()
|
||||||
|
self.lost_completed_write = False
|
||||||
|
|
||||||
|
def set(self, kind: str, key: str, value: str) -> None:
|
||||||
|
if kind == IMPORT_JOB_KIND:
|
||||||
|
job = json.loads(value)
|
||||||
|
if (job.get("status") == "completed"
|
||||||
|
and not self.lost_completed_write):
|
||||||
|
self.lost_completed_write = True
|
||||||
|
raise RuntimeError("completed job write acknowledgement lost")
|
||||||
|
super().set(kind, key, value)
|
||||||
|
|
||||||
|
|
||||||
|
def wait_for_job(runtime: CloudTemplateRuntime, job_id: str) -> dict:
|
||||||
|
for _ in range(200):
|
||||||
|
job = runtime.get_job(job_id)
|
||||||
|
if job["status"] in {"completed", "failed"}:
|
||||||
|
return job
|
||||||
|
time.sleep(0.01)
|
||||||
|
raise AssertionError("cloud template import did not finish")
|
||||||
|
|
||||||
|
|
||||||
|
def fake_download_bytes(payload: bytes):
|
||||||
|
digest = hashlib.sha256(payload).hexdigest()
|
||||||
|
|
||||||
|
def download(_url: str, expected_sha256: str, directory: Path, **_kwargs):
|
||||||
|
assert expected_sha256 in {"", digest}
|
||||||
|
source = Path(directory) / ".cloud-image-test.qcow2"
|
||||||
|
source.write_bytes(payload)
|
||||||
|
os.chmod(source, 0o600)
|
||||||
|
return source, 2 * GIB, digest
|
||||||
|
|
||||||
|
return digest, download
|
||||||
|
|
||||||
|
|
||||||
|
def record_for(payload: bytes, *, name: str = "AlmaLinux 10") -> dict:
|
||||||
|
digest = hashlib.sha256(payload).hexdigest()
|
||||||
|
template_id = cloud_template_id_for(digest, "10")
|
||||||
|
return {
|
||||||
|
"id": template_id,
|
||||||
|
"name": name,
|
||||||
|
"os_family": "AlmaLinux",
|
||||||
|
"os_version": "10",
|
||||||
|
"sha256": digest,
|
||||||
|
"volume_name": template_volume_name(template_id),
|
||||||
|
"virtual_size": 2 * GIB,
|
||||||
|
"image_bytes": len(payload),
|
||||||
|
"created_at": 1,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def test_stable_global_id_and_catalog_use_an_independent_record_kind():
|
||||||
|
digest = "a" * 64
|
||||||
|
first = cloud_template_id_for(digest, "10")
|
||||||
|
second = cloud_template_id_for(digest.upper(), "10")
|
||||||
|
assert first == second
|
||||||
|
assert first.startswith("alma10-")
|
||||||
|
assert template_volume_name(first).endswith(".qcow2")
|
||||||
|
|
||||||
|
store = MemoryStore()
|
||||||
|
catalog = GlobalCloudTemplateStore(store)
|
||||||
|
record = record_for(b"catalog-image")
|
||||||
|
catalog.add(record)
|
||||||
|
|
||||||
|
assert catalog.get(record["id"]) == record
|
||||||
|
assert list(store.list(CATALOG_KIND)) == [record["id"]]
|
||||||
|
assert store.list("template") == {}
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"message",
|
||||||
|
[
|
||||||
|
"官方云镜像地址无法解析",
|
||||||
|
"官方云镜像地址解析结果无效",
|
||||||
|
"官方云镜像地址解析到非公网地址",
|
||||||
|
"无法连接官方云镜像下载地址",
|
||||||
|
"官方云镜像重定向目标不安全",
|
||||||
|
"官方云镜像重定向次数过多",
|
||||||
|
"官方云镜像下载服务器返回异常状态",
|
||||||
|
"官方云镜像下载响应格式无效",
|
||||||
|
"云镜像下载内容不完整",
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_public_job_preserves_fixed_safe_download_errors(message):
|
||||||
|
job = {
|
||||||
|
"job_id": "safe-error",
|
||||||
|
"status": "failed",
|
||||||
|
"phase": "导入失败",
|
||||||
|
"created_at": 1,
|
||||||
|
"error": message,
|
||||||
|
}
|
||||||
|
assert CloudTemplateRuntime.public_job(job)["error"] == message
|
||||||
|
|
||||||
|
|
||||||
|
def test_import_publishes_private_source_and_restores_global_catalog(tmp_path):
|
||||||
|
store = MemoryStore()
|
||||||
|
runtime = CloudTemplateRuntime(store, tmp_path)
|
||||||
|
payload = b"verified-global-cloud-image"
|
||||||
|
digest, downloader = fake_download_bytes(payload)
|
||||||
|
try:
|
||||||
|
with patch(
|
||||||
|
"virsh_panel.cloud_template_runtime.download_verified_image",
|
||||||
|
side_effect=downloader,
|
||||||
|
):
|
||||||
|
queued = runtime.queue_import(
|
||||||
|
ALMA_10_URL, "AlmaLinux 10", digest
|
||||||
|
)
|
||||||
|
job = wait_for_job(runtime, queued["job_id"])
|
||||||
|
|
||||||
|
assert job["status"] == "completed", job
|
||||||
|
template_id = job["template"]["id"]
|
||||||
|
assert template_id == cloud_template_id_for(digest, "10")
|
||||||
|
assert "volume_name" not in job["template"]
|
||||||
|
listed = runtime.list()
|
||||||
|
assert listed == [job["template"]]
|
||||||
|
assert "active_import_job" not in runtime.status()
|
||||||
|
|
||||||
|
root = tmp_path / "cloud-templates"
|
||||||
|
source = root / template_volume_name(template_id)
|
||||||
|
assert source.read_bytes() == payload
|
||||||
|
assert stat.S_IMODE(root.stat().st_mode) == 0o700
|
||||||
|
assert stat.S_IMODE(source.stat().st_mode) == 0o600
|
||||||
|
assert store.get(IMPORT_JOB_KIND, queued["job_id"]) is not None
|
||||||
|
|
||||||
|
# A new runtime reads the same host-independent catalog and verifies
|
||||||
|
# the persisted source from disk instead of trusting process memory.
|
||||||
|
restored = CloudTemplateRuntime(store, tmp_path)
|
||||||
|
try:
|
||||||
|
restored.restore_jobs()
|
||||||
|
assert restored.list() == listed
|
||||||
|
with restored.source(template_id) as (record, path):
|
||||||
|
assert record["sha256"] == digest
|
||||||
|
assert path == source
|
||||||
|
assert restored.in_use[template_id] == 1
|
||||||
|
assert template_id not in restored.in_use
|
||||||
|
finally:
|
||||||
|
restored.close()
|
||||||
|
finally:
|
||||||
|
runtime.close()
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
("url", "family", "version", "prefix"),
|
||||||
|
[
|
||||||
|
(ALMA_8_URL, "AlmaLinux", "8", "alma8-"),
|
||||||
|
(DEBIAN_12_URL, "Debian", "12", "debian12-"),
|
||||||
|
(UBUNTU_24_URL, "Ubuntu", "24.04", "ubuntu2404-"),
|
||||||
|
(CENTOS_STREAM_10_URL, "CentOS Stream", "10", "centosstream10-"),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_import_derives_family_version_and_safe_source_name(
|
||||||
|
tmp_path, url, family, version, prefix):
|
||||||
|
runtime = CloudTemplateRuntime(MemoryStore(), tmp_path)
|
||||||
|
payload = f"{family}-{version}-cloud-image".encode()
|
||||||
|
digest, downloader = fake_download_bytes(payload)
|
||||||
|
try:
|
||||||
|
with patch(
|
||||||
|
"virsh_panel.cloud_template_runtime.download_verified_image",
|
||||||
|
side_effect=downloader,
|
||||||
|
):
|
||||||
|
queued = runtime.queue_import(url, f"{family} {version}", digest)
|
||||||
|
job = wait_for_job(runtime, queued["job_id"])
|
||||||
|
|
||||||
|
assert job["status"] == "completed", job
|
||||||
|
template = job["template"]
|
||||||
|
assert template["id"].startswith(prefix)
|
||||||
|
assert template["os_family"] == family
|
||||||
|
assert template["os_version"] == version
|
||||||
|
internal = runtime.get(template["id"])
|
||||||
|
assert internal["volume_name"] == template_volume_name(template["id"])
|
||||||
|
assert (
|
||||||
|
tmp_path / "cloud-templates" / internal["volume_name"]
|
||||||
|
).read_bytes() == payload
|
||||||
|
finally:
|
||||||
|
runtime.close()
|
||||||
|
|
||||||
|
|
||||||
|
def test_available_bytes_preserves_panel_disk_reserve(tmp_path):
|
||||||
|
files = GlobalCloudTemplateFiles(tmp_path)
|
||||||
|
with patch(
|
||||||
|
"virsh_panel.cloud_template_runtime.shutil.disk_usage",
|
||||||
|
return_value=SimpleNamespace(
|
||||||
|
free=PERSISTENT_FREE_RESERVE_BYTES + 3 * 1024 * 1024
|
||||||
|
),
|
||||||
|
):
|
||||||
|
assert files.available_bytes() == 3 * 1024 * 1024
|
||||||
|
|
||||||
|
with patch(
|
||||||
|
"virsh_panel.cloud_template_runtime.shutil.disk_usage",
|
||||||
|
return_value=SimpleNamespace(free=PERSISTENT_FREE_RESERVE_BYTES - 1),
|
||||||
|
):
|
||||||
|
assert files.available_bytes() == 0
|
||||||
|
|
||||||
|
|
||||||
|
def test_import_rejects_before_queue_when_only_reserved_space_remains(tmp_path):
|
||||||
|
runtime = CloudTemplateRuntime(MemoryStore(), tmp_path)
|
||||||
|
try:
|
||||||
|
with patch.object(
|
||||||
|
runtime.files, "available_bytes", return_value=1024 * 1024 - 1
|
||||||
|
):
|
||||||
|
with pytest.raises(TemplateError, match="空间不足") as insufficient:
|
||||||
|
runtime.queue_import(ALMA_10_URL, "No Disk Space", "")
|
||||||
|
assert insufficient.value.status == 507
|
||||||
|
assert runtime.jobs == {}
|
||||||
|
assert runtime.pending == set()
|
||||||
|
finally:
|
||||||
|
runtime.close()
|
||||||
|
|
||||||
|
|
||||||
|
def test_import_passes_only_space_above_reserve_to_downloader(tmp_path):
|
||||||
|
runtime = CloudTemplateRuntime(MemoryStore(), tmp_path)
|
||||||
|
payload = b"bounded-global-cloud-image"
|
||||||
|
digest = hashlib.sha256(payload).hexdigest()
|
||||||
|
limits: list[int] = []
|
||||||
|
|
||||||
|
def bounded_download(_url: str, _expected: str, directory: Path, *,
|
||||||
|
max_bytes: int):
|
||||||
|
limits.append(max_bytes)
|
||||||
|
source = Path(directory) / ".cloud-image-bounded.qcow2"
|
||||||
|
source.write_bytes(payload)
|
||||||
|
os.chmod(source, 0o600)
|
||||||
|
return source, 2 * GIB, digest
|
||||||
|
|
||||||
|
try:
|
||||||
|
with patch.object(
|
||||||
|
runtime.files, "available_bytes",
|
||||||
|
side_effect=[3 * 1024 * 1024, 2 * 1024 * 1024],
|
||||||
|
), patch(
|
||||||
|
"virsh_panel.cloud_template_runtime.download_verified_image",
|
||||||
|
side_effect=bounded_download,
|
||||||
|
):
|
||||||
|
queued = runtime.queue_import(ALMA_10_URL, "Bounded", "")
|
||||||
|
job = wait_for_job(runtime, queued["job_id"])
|
||||||
|
assert job["status"] == "completed", job
|
||||||
|
assert limits == [2 * 1024 * 1024]
|
||||||
|
finally:
|
||||||
|
runtime.close()
|
||||||
|
|
||||||
|
|
||||||
|
def test_duplicate_digest_and_casefold_name_are_rejected_globally():
|
||||||
|
store = MemoryStore()
|
||||||
|
catalog = GlobalCloudTemplateStore(store)
|
||||||
|
first = record_for(b"first-image", name="AlmaLinux Gold")
|
||||||
|
catalog.add(first)
|
||||||
|
|
||||||
|
with pytest.raises(TemplateError, match="已经导入") as duplicate_digest:
|
||||||
|
catalog.add(first)
|
||||||
|
assert duplicate_digest.value.status == 409
|
||||||
|
|
||||||
|
second = record_for(b"second-image", name="almalinux gold")
|
||||||
|
with pytest.raises(TemplateError, match="同名") as duplicate_name:
|
||||||
|
catalog.add(second)
|
||||||
|
assert duplicate_name.value.status == 409
|
||||||
|
|
||||||
|
|
||||||
|
def test_rename_changes_only_display_name_and_keeps_source_identity(tmp_path):
|
||||||
|
store = MemoryStore()
|
||||||
|
catalog = GlobalCloudTemplateStore(store)
|
||||||
|
record = record_for(b"rename-image")
|
||||||
|
catalog.add(record)
|
||||||
|
updated = catalog.rename(record["id"], "AlmaLinux 10 Production")
|
||||||
|
|
||||||
|
assert updated["name"] == "AlmaLinux 10 Production"
|
||||||
|
for key in (
|
||||||
|
"id", "sha256", "volume_name", "virtual_size", "image_bytes",
|
||||||
|
"os_family", "os_version", "created_at",
|
||||||
|
):
|
||||||
|
assert updated[key] == record[key]
|
||||||
|
|
||||||
|
files = GlobalCloudTemplateFiles(tmp_path)
|
||||||
|
temporary = files.directory() / ".cloud-image-rename.qcow2"
|
||||||
|
temporary.write_bytes(b"rename-image")
|
||||||
|
os.chmod(temporary, 0o600)
|
||||||
|
source = files.publish(
|
||||||
|
temporary, record["id"], expected_size=len(b"rename-image")
|
||||||
|
)
|
||||||
|
assert source.name == record["volume_name"]
|
||||||
|
|
||||||
|
|
||||||
|
def test_delete_removes_global_catalog_source_and_verification_cache(tmp_path):
|
||||||
|
store = MemoryStore()
|
||||||
|
runtime = CloudTemplateRuntime(store, tmp_path)
|
||||||
|
payload = b"delete-global-cloud-image"
|
||||||
|
record = record_for(payload)
|
||||||
|
runtime.catalog.add(record)
|
||||||
|
temporary = runtime.files.directory() / ".cloud-image-delete.qcow2"
|
||||||
|
temporary.write_bytes(payload)
|
||||||
|
os.chmod(temporary, 0o600)
|
||||||
|
source = runtime.files.publish(
|
||||||
|
temporary, record["id"], expected_size=len(payload)
|
||||||
|
)
|
||||||
|
runtime.verified_sources[record["id"]] = (1, 2, 3, 4, record["sha256"])
|
||||||
|
try:
|
||||||
|
runtime.delete(record["id"])
|
||||||
|
|
||||||
|
assert not source.exists()
|
||||||
|
assert store.get(CATALOG_KIND, record["id"]) is None
|
||||||
|
assert record["id"] not in runtime.verified_sources
|
||||||
|
with pytest.raises(TemplateError) as missing:
|
||||||
|
runtime.get(record["id"])
|
||||||
|
assert missing.value.status == 404
|
||||||
|
finally:
|
||||||
|
runtime.close()
|
||||||
|
|
||||||
|
|
||||||
|
def test_delete_rejects_import_or_active_source_lease(tmp_path):
|
||||||
|
runtime = CloudTemplateRuntime(MemoryStore(), tmp_path)
|
||||||
|
payload = b"leased-global-cloud-image"
|
||||||
|
record = record_for(payload)
|
||||||
|
runtime.catalog.add(record)
|
||||||
|
temporary = runtime.files.directory() / ".cloud-image-leased.qcow2"
|
||||||
|
temporary.write_bytes(payload)
|
||||||
|
os.chmod(temporary, 0o600)
|
||||||
|
source = runtime.files.publish(
|
||||||
|
temporary, record["id"], expected_size=len(payload)
|
||||||
|
)
|
||||||
|
try:
|
||||||
|
runtime.pending.add("active-import")
|
||||||
|
with pytest.raises(TemplateError, match="正在导入") as importing:
|
||||||
|
runtime.delete(record["id"])
|
||||||
|
assert importing.value.status == 409
|
||||||
|
runtime.pending.clear()
|
||||||
|
|
||||||
|
with runtime.source(record["id"]):
|
||||||
|
with pytest.raises(TemplateError, match="正在同步") as leased:
|
||||||
|
runtime.delete(record["id"])
|
||||||
|
assert leased.value.status == 409
|
||||||
|
assert source.exists()
|
||||||
|
assert runtime.get(record["id"])["id"] == record["id"]
|
||||||
|
finally:
|
||||||
|
runtime.close()
|
||||||
|
|
||||||
|
|
||||||
|
def test_reimport_waits_for_old_host_cache_gc_then_reuses_stable_id(tmp_path):
|
||||||
|
store = MemoryStore()
|
||||||
|
runtime = CloudTemplateRuntime(store, tmp_path)
|
||||||
|
record = record_for(b"reimport-identical-image")
|
||||||
|
tombstone = {
|
||||||
|
"deletion_id": "a" * 32,
|
||||||
|
"template_id": record["id"],
|
||||||
|
"template_created_at": record["created_at"],
|
||||||
|
"source_volume_name": record["volume_name"],
|
||||||
|
"pending_hosts": ["offline-host"],
|
||||||
|
"source_deleted": True,
|
||||||
|
"created_at": 1,
|
||||||
|
}
|
||||||
|
store.set(CACHE_TOMBSTONE_KIND, record["id"], json.dumps(tombstone))
|
||||||
|
try:
|
||||||
|
with pytest.raises(TemplateError, match="缓存仍在清理") as pending:
|
||||||
|
runtime._prepare_reimport(record["id"])
|
||||||
|
assert pending.value.status == 409
|
||||||
|
assert store.get(CACHE_TOMBSTONE_KIND, record["id"]) is not None
|
||||||
|
|
||||||
|
tombstone["pending_hosts"] = []
|
||||||
|
store.set(CACHE_TOMBSTONE_KIND, record["id"], json.dumps(tombstone))
|
||||||
|
previous_created_at = runtime._prepare_reimport(record["id"])
|
||||||
|
assert store.get(CACHE_TOMBSTONE_KIND, record["id"]) is None
|
||||||
|
assert previous_created_at == record["created_at"]
|
||||||
|
first_generation = runtime._next_generation(
|
||||||
|
record["id"], previous_created_at)
|
||||||
|
second_generation = runtime._next_generation(record["id"])
|
||||||
|
assert second_generation > first_generation > previous_created_at
|
||||||
|
finally:
|
||||||
|
runtime.close()
|
||||||
|
|
||||||
|
|
||||||
|
def test_restore_cancels_provisional_delete_when_source_and_catalog_survive(
|
||||||
|
tmp_path):
|
||||||
|
store = MemoryStore()
|
||||||
|
runtime = CloudTemplateRuntime(store, tmp_path)
|
||||||
|
payload = b"delete-never-committed"
|
||||||
|
record = record_for(payload)
|
||||||
|
runtime.catalog.add(record)
|
||||||
|
temporary = runtime.files.directory() / ".cloud-image-delete-intact.qcow2"
|
||||||
|
temporary.write_bytes(payload)
|
||||||
|
os.chmod(temporary, 0o600)
|
||||||
|
source = runtime.files.publish(
|
||||||
|
temporary, record["id"], expected_size=len(payload)
|
||||||
|
)
|
||||||
|
tombstone = {
|
||||||
|
"deletion_id": "c" * 32,
|
||||||
|
"template_id": record["id"],
|
||||||
|
"template_created_at": record["created_at"],
|
||||||
|
"source_volume_name": record["volume_name"],
|
||||||
|
"pending_hosts": ["host-one"],
|
||||||
|
"source_deleted": False,
|
||||||
|
"created_at": 1,
|
||||||
|
}
|
||||||
|
store.set(CACHE_TOMBSTONE_KIND, record["id"], json.dumps(tombstone))
|
||||||
|
try:
|
||||||
|
runtime.restore_jobs()
|
||||||
|
|
||||||
|
assert runtime.get(record["id"])["id"] == record["id"]
|
||||||
|
assert source.is_file()
|
||||||
|
assert store.get(CACHE_TOMBSTONE_KIND, record["id"]) is None
|
||||||
|
finally:
|
||||||
|
runtime.close()
|
||||||
|
|
||||||
|
|
||||||
|
def test_restore_finishes_delete_after_source_unlink_crash_window(tmp_path):
|
||||||
|
store = MemoryStore()
|
||||||
|
runtime = CloudTemplateRuntime(store, tmp_path)
|
||||||
|
payload = b"delete-unlink-committed"
|
||||||
|
record = record_for(payload)
|
||||||
|
runtime.catalog.add(record)
|
||||||
|
tombstone = {
|
||||||
|
"deletion_id": "d" * 32,
|
||||||
|
"template_id": record["id"],
|
||||||
|
"template_created_at": record["created_at"],
|
||||||
|
"source_volume_name": record["volume_name"],
|
||||||
|
"pending_hosts": ["host-one"],
|
||||||
|
"source_deleted": False,
|
||||||
|
"created_at": 1,
|
||||||
|
}
|
||||||
|
store.set(CACHE_TOMBSTONE_KIND, record["id"], json.dumps(tombstone))
|
||||||
|
try:
|
||||||
|
# Listing is observational. Only delete recovery/maintenance may
|
||||||
|
# advance the durable tombstone and remove local source/catalog data.
|
||||||
|
assert [row["id"] for row in runtime.list()] == [record["id"]]
|
||||||
|
before = json.loads(store.get(CACHE_TOMBSTONE_KIND, record["id"]))
|
||||||
|
assert before["source_deleted"] is False
|
||||||
|
assert runtime.get(record["id"])["id"] == record["id"]
|
||||||
|
runtime.restore_jobs()
|
||||||
|
|
||||||
|
with pytest.raises(TemplateError) as missing:
|
||||||
|
runtime.get(record["id"])
|
||||||
|
assert missing.value.status == 404
|
||||||
|
restored = json.loads(store.get(
|
||||||
|
CACHE_TOMBSTONE_KIND, record["id"]
|
||||||
|
))
|
||||||
|
assert restored["source_deleted"] is True
|
||||||
|
assert restored["pending_hosts"] == ["host-one"]
|
||||||
|
finally:
|
||||||
|
runtime.close()
|
||||||
|
|
||||||
|
|
||||||
|
def test_source_rejects_permission_damage_and_content_tampering(tmp_path):
|
||||||
|
store = MemoryStore()
|
||||||
|
runtime = CloudTemplateRuntime(store, tmp_path)
|
||||||
|
payload = b"source-integrity"
|
||||||
|
record = record_for(payload)
|
||||||
|
runtime.catalog.add(record)
|
||||||
|
temporary = runtime.files.directory() / ".cloud-image-integrity.qcow2"
|
||||||
|
temporary.write_bytes(payload)
|
||||||
|
os.chmod(temporary, 0o600)
|
||||||
|
source = runtime.files.publish(
|
||||||
|
temporary, record["id"], expected_size=len(payload)
|
||||||
|
)
|
||||||
|
try:
|
||||||
|
os.chmod(source, 0o640)
|
||||||
|
with pytest.raises(TemplateError, match="已损坏"):
|
||||||
|
with runtime.source(record["id"]):
|
||||||
|
pass
|
||||||
|
|
||||||
|
os.chmod(source, 0o600)
|
||||||
|
source.write_bytes(b"x" * len(payload))
|
||||||
|
with pytest.raises(TemplateError, match="SHA-256"):
|
||||||
|
with runtime.source(record["id"]):
|
||||||
|
pass
|
||||||
|
finally:
|
||||||
|
runtime.close()
|
||||||
|
|
||||||
|
|
||||||
|
def test_concurrent_import_is_serialized_and_second_request_is_rejected(tmp_path):
|
||||||
|
runtime = CloudTemplateRuntime(MemoryStore(), tmp_path)
|
||||||
|
started = threading.Event()
|
||||||
|
release = threading.Event()
|
||||||
|
payload = b"blocking-image"
|
||||||
|
digest = hashlib.sha256(payload).hexdigest()
|
||||||
|
|
||||||
|
def blocking_download(_url: str, _expected: str, directory: Path,
|
||||||
|
**_kwargs):
|
||||||
|
started.set()
|
||||||
|
assert release.wait(timeout=5)
|
||||||
|
source = Path(directory) / ".cloud-image-blocking.qcow2"
|
||||||
|
source.write_bytes(payload)
|
||||||
|
os.chmod(source, 0o600)
|
||||||
|
return source, 2 * GIB, digest
|
||||||
|
|
||||||
|
try:
|
||||||
|
with patch(
|
||||||
|
"virsh_panel.cloud_template_runtime.download_verified_image",
|
||||||
|
side_effect=blocking_download,
|
||||||
|
):
|
||||||
|
first = runtime.queue_import(ALMA_10_URL, "First", "")
|
||||||
|
assert started.wait(timeout=2)
|
||||||
|
assert runtime.has_active_jobs() is True
|
||||||
|
with pytest.raises(TemplateError, match="正在导入") as busy:
|
||||||
|
runtime.queue_import(ALMA_10_URL, "Second", "")
|
||||||
|
assert busy.value.status == 409
|
||||||
|
release.set()
|
||||||
|
assert wait_for_job(runtime, first["job_id"])["status"] == "completed"
|
||||||
|
assert runtime.has_active_jobs() is False
|
||||||
|
finally:
|
||||||
|
release.set()
|
||||||
|
runtime.close()
|
||||||
|
|
||||||
|
|
||||||
|
def test_restore_marks_interrupted_job_failed_and_removes_partial_files(tmp_path):
|
||||||
|
store = MemoryStore()
|
||||||
|
job_id = "interrupted-cloud-template"
|
||||||
|
store.set(IMPORT_JOB_KIND, job_id, json.dumps({
|
||||||
|
"job_id": job_id,
|
||||||
|
"status": "running",
|
||||||
|
"phase": "正在下载并校验云镜像",
|
||||||
|
"created_at": 1,
|
||||||
|
}))
|
||||||
|
files = GlobalCloudTemplateFiles(tmp_path)
|
||||||
|
partial = files.directory() / ".cloud-image-abandoned.qcow2"
|
||||||
|
partial.write_bytes(b"partial")
|
||||||
|
os.chmod(partial, 0o600)
|
||||||
|
|
||||||
|
runtime = CloudTemplateRuntime(store, tmp_path)
|
||||||
|
try:
|
||||||
|
runtime.restore_jobs()
|
||||||
|
job = runtime.get_job(job_id)
|
||||||
|
assert job["status"] == "failed"
|
||||||
|
assert "重启" in job["phase"]
|
||||||
|
assert "重启" in job["error"]
|
||||||
|
assert not partial.exists()
|
||||||
|
assert runtime.has_active_jobs() is False
|
||||||
|
finally:
|
||||||
|
runtime.close()
|
||||||
|
|
||||||
|
|
||||||
|
def test_import_failure_redacts_internal_paths_and_releases_pending(tmp_path):
|
||||||
|
runtime = CloudTemplateRuntime(MemoryStore(), tmp_path)
|
||||||
|
try:
|
||||||
|
with patch(
|
||||||
|
"virsh_panel.cloud_template_runtime.download_verified_image",
|
||||||
|
side_effect=TemplateError(
|
||||||
|
"私有目录 /home/virt-cloud/data/cloud-templates 损坏", 500
|
||||||
|
),
|
||||||
|
):
|
||||||
|
queued = runtime.queue_import(ALMA_10_URL, "Broken", "")
|
||||||
|
failed = wait_for_job(runtime, queued["job_id"])
|
||||||
|
assert failed["status"] == "failed"
|
||||||
|
assert failed["error"] == "云镜像下载或保存失败,请联系管理员查看日志"
|
||||||
|
assert "/home/" not in json.dumps(failed, ensure_ascii=False)
|
||||||
|
assert runtime.pending == set()
|
||||||
|
finally:
|
||||||
|
runtime.close()
|
||||||
|
|
||||||
|
|
||||||
|
def test_catalog_insert_commit_with_lost_ack_is_completed_not_rolled_back(
|
||||||
|
tmp_path):
|
||||||
|
store = CommitThenRaiseCatalogStore()
|
||||||
|
runtime = CloudTemplateRuntime(store, tmp_path)
|
||||||
|
payload = b"catalog-commit-ack-lost"
|
||||||
|
digest, downloader = fake_download_bytes(payload)
|
||||||
|
try:
|
||||||
|
with patch(
|
||||||
|
"virsh_panel.cloud_template_runtime.download_verified_image",
|
||||||
|
side_effect=downloader,
|
||||||
|
):
|
||||||
|
queued = runtime.queue_import(ALMA_10_URL, "Committed", digest)
|
||||||
|
job = wait_for_job(runtime, queued["job_id"])
|
||||||
|
|
||||||
|
assert job["status"] == "completed"
|
||||||
|
template_id = cloud_template_id_for(digest, "10")
|
||||||
|
assert runtime.get(template_id)["sha256"] == digest
|
||||||
|
source = tmp_path / "cloud-templates" / template_volume_name(template_id)
|
||||||
|
assert source.read_bytes() == payload
|
||||||
|
finally:
|
||||||
|
runtime.close()
|
||||||
|
|
||||||
|
|
||||||
|
def test_ambiguous_catalog_commit_preserves_source_for_startup_recovery(
|
||||||
|
tmp_path):
|
||||||
|
store = AmbiguousCatalogCommitStore()
|
||||||
|
runtime = CloudTemplateRuntime(store, tmp_path)
|
||||||
|
payload = b"ambiguous-catalog-commit"
|
||||||
|
digest, downloader = fake_download_bytes(payload)
|
||||||
|
try:
|
||||||
|
with patch(
|
||||||
|
"virsh_panel.cloud_template_runtime.download_verified_image",
|
||||||
|
side_effect=downloader,
|
||||||
|
):
|
||||||
|
queued = runtime.queue_import(ALMA_10_URL, "Ambiguous", digest)
|
||||||
|
failed = wait_for_job(runtime, queued["job_id"])
|
||||||
|
|
||||||
|
assert failed["status"] == "failed"
|
||||||
|
template_id = cloud_template_id_for(digest, "10")
|
||||||
|
source = tmp_path / "cloud-templates" / template_volume_name(template_id)
|
||||||
|
assert source.read_bytes() == payload
|
||||||
|
finally:
|
||||||
|
runtime.close()
|
||||||
|
|
||||||
|
restored = CloudTemplateRuntime(store, tmp_path)
|
||||||
|
try:
|
||||||
|
restored.restore_jobs()
|
||||||
|
recovered = restored.get_job(queued["job_id"])
|
||||||
|
assert recovered["status"] == "completed"
|
||||||
|
assert recovered["template"]["id"] == template_id
|
||||||
|
assert source.read_bytes() == payload
|
||||||
|
finally:
|
||||||
|
restored.close()
|
||||||
|
|
||||||
|
|
||||||
|
def test_lost_completed_job_write_is_success_in_memory_and_repaired_on_restart(
|
||||||
|
tmp_path):
|
||||||
|
store = LoseFirstCompletedJobWriteStore()
|
||||||
|
runtime = CloudTemplateRuntime(store, tmp_path)
|
||||||
|
payload = b"lost-completed-job-write"
|
||||||
|
digest, downloader = fake_download_bytes(payload)
|
||||||
|
try:
|
||||||
|
with patch(
|
||||||
|
"virsh_panel.cloud_template_runtime.download_verified_image",
|
||||||
|
side_effect=downloader,
|
||||||
|
):
|
||||||
|
queued = runtime.queue_import(ALMA_10_URL, "Recoverable", digest)
|
||||||
|
completed = wait_for_job(runtime, queued["job_id"])
|
||||||
|
|
||||||
|
assert completed["status"] == "completed"
|
||||||
|
persisted = json.loads(store.get(IMPORT_JOB_KIND, queued["job_id"]))
|
||||||
|
assert persisted["status"] == "running"
|
||||||
|
assert persisted["candidate_template_id"] == cloud_template_id_for(
|
||||||
|
digest, "10"
|
||||||
|
)
|
||||||
|
finally:
|
||||||
|
runtime.close()
|
||||||
|
|
||||||
|
restored = CloudTemplateRuntime(store, tmp_path)
|
||||||
|
try:
|
||||||
|
restored.restore_jobs()
|
||||||
|
repaired = restored.get_job(queued["job_id"])
|
||||||
|
assert repaired["status"] == "completed"
|
||||||
|
persisted = json.loads(store.get(IMPORT_JOB_KIND, queued["job_id"]))
|
||||||
|
assert persisted["status"] == "completed"
|
||||||
|
finally:
|
||||||
|
restored.close()
|
||||||
@ -0,0 +1,502 @@
|
|||||||
|
"""Official-image import restrictions and private template catalog tests."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import hashlib
|
||||||
|
import io
|
||||||
|
import socket
|
||||||
|
import stat
|
||||||
|
import struct
|
||||||
|
from types import SimpleNamespace
|
||||||
|
from unittest.mock import patch
|
||||||
|
from urllib.parse import urlsplit
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from virsh_panel.cloud_templates import (
|
||||||
|
TemplateError,
|
||||||
|
TemplateStore,
|
||||||
|
_RejectRedirects,
|
||||||
|
_open_image_response,
|
||||||
|
_redirect_target,
|
||||||
|
_resolve_global_addresses,
|
||||||
|
download_verified_image,
|
||||||
|
image_metadata_for,
|
||||||
|
image_version_for,
|
||||||
|
template_id_for,
|
||||||
|
template_scope,
|
||||||
|
template_volume_name,
|
||||||
|
validate_image_url,
|
||||||
|
verify_qcow2_image,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
OFFICIAL = (
|
||||||
|
"https://repo.almalinux.org/almalinux/10/cloud/x86_64/images/"
|
||||||
|
"AlmaLinux-10-GenericCloud-10.1-20260101.x86_64.qcow2"
|
||||||
|
)
|
||||||
|
OFFICIAL_9 = (
|
||||||
|
"https://repo.almalinux.org/almalinux/9/cloud/x86_64/images/"
|
||||||
|
"AlmaLinux-9-GenericCloud-latest.x86_64.qcow2"
|
||||||
|
)
|
||||||
|
OFFICIAL_8 = (
|
||||||
|
"https://repo.almalinux.org/almalinux/8/cloud/x86_64/images/"
|
||||||
|
"AlmaLinux-8-GenericCloud-latest.x86_64.qcow2"
|
||||||
|
)
|
||||||
|
DEBIAN_12 = (
|
||||||
|
"https://cloud.debian.org/images/cloud/bookworm/latest/"
|
||||||
|
"debian-12-generic-amd64.qcow2"
|
||||||
|
)
|
||||||
|
DEBIAN_13_BUILD = (
|
||||||
|
"https://cloud.debian.org/images/cloud/trixie/20260801-2204/"
|
||||||
|
"debian-13-generic-amd64-20260801-2204.qcow2"
|
||||||
|
)
|
||||||
|
DEBIAN_12_GENERICCLOUD = (
|
||||||
|
"https://cloud.debian.org/images/cloud/bookworm/latest/"
|
||||||
|
"debian-12-genericcloud-amd64.qcow2"
|
||||||
|
)
|
||||||
|
UBUNTU_24 = (
|
||||||
|
"https://cloud-images.ubuntu.com/releases/noble/release/"
|
||||||
|
"ubuntu-24.04-server-cloudimg-amd64.img"
|
||||||
|
)
|
||||||
|
UBUNTU_26_CURRENT = (
|
||||||
|
"https://cloud-images.ubuntu.com/resolute/current/"
|
||||||
|
"resolute-server-cloudimg-amd64.img"
|
||||||
|
)
|
||||||
|
CENTOS_STREAM_9 = (
|
||||||
|
"https://cloud.centos.org/centos/9-stream/x86_64/images/"
|
||||||
|
"CentOS-Stream-GenericCloud-9-latest.x86_64.qcow2"
|
||||||
|
)
|
||||||
|
CENTOS_STREAM_10 = (
|
||||||
|
"https://cloud.centos.org/centos/10-stream/x86_64/images/"
|
||||||
|
"CentOS-Stream-GenericCloud-x86_64-10-20260810.0.x86_64.qcow2"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class FakeHeaders:
|
||||||
|
def __init__(self, values=None):
|
||||||
|
self.values = values or {}
|
||||||
|
|
||||||
|
def get_all(self, name, default=None):
|
||||||
|
return self.values.get(name, default)
|
||||||
|
|
||||||
|
|
||||||
|
class FakeImageResponse(io.BytesIO):
|
||||||
|
def __init__(self, body=b"", *, status=200, headers=None):
|
||||||
|
super().__init__(body)
|
||||||
|
self.status = status
|
||||||
|
self.header_values = headers or {}
|
||||||
|
self.headers = FakeHeaders(self.header_values)
|
||||||
|
|
||||||
|
def getheader(self, name, default=None):
|
||||||
|
values = self.header_values.get(name)
|
||||||
|
if not values:
|
||||||
|
return default
|
||||||
|
return values[0]
|
||||||
|
|
||||||
|
|
||||||
|
def fake_image_response(body: bytes):
|
||||||
|
return (
|
||||||
|
SimpleNamespace(close=lambda: None),
|
||||||
|
FakeImageResponse(
|
||||||
|
body,
|
||||||
|
headers={"Content-Length": [str(len(body))]},
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_official_almalinux_8_9_and_10_are_distinct_supported_versions():
|
||||||
|
assert validate_image_url(OFFICIAL_8) == OFFICIAL_8
|
||||||
|
assert validate_image_url(OFFICIAL_9) == OFFICIAL_9
|
||||||
|
assert image_metadata_for(OFFICIAL_8) == ("AlmaLinux", "8")
|
||||||
|
assert image_version_for(OFFICIAL_9) == "9"
|
||||||
|
assert image_version_for(OFFICIAL) == "10"
|
||||||
|
scope = "a" * 64
|
||||||
|
digest = "b" * 64
|
||||||
|
assert template_id_for(scope, digest, "8").startswith("alma8-")
|
||||||
|
assert template_id_for(scope, digest, "9").startswith("alma9-")
|
||||||
|
assert template_id_for(scope, digest, "10").startswith("alma10-")
|
||||||
|
assert template_volume_name(template_id_for(scope, digest, "9")).startswith(
|
||||||
|
"panel-template-alma9-"
|
||||||
|
)
|
||||||
|
with pytest.raises(TemplateError):
|
||||||
|
validate_image_url(OFFICIAL_9.replace("/almalinux/9/", "/almalinux/10/"))
|
||||||
|
with pytest.raises(TemplateError):
|
||||||
|
validate_image_url(OFFICIAL_9.replace("AlmaLinux-9-", "AlmaLinux-8-"))
|
||||||
|
with pytest.raises(TemplateError):
|
||||||
|
template_id_for(scope, digest, "7")
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
("url", "family", "version", "prefix"),
|
||||||
|
[
|
||||||
|
(DEBIAN_12, "Debian", "12", "debian12-"),
|
||||||
|
(DEBIAN_12_GENERICCLOUD, "Debian", "12", "debian12-"),
|
||||||
|
(DEBIAN_13_BUILD, "Debian", "13", "debian13-"),
|
||||||
|
(UBUNTU_24, "Ubuntu", "24.04", "ubuntu2404-"),
|
||||||
|
(UBUNTU_26_CURRENT, "Ubuntu", "26.04", "ubuntu2604-"),
|
||||||
|
(CENTOS_STREAM_9, "CentOS Stream", "9", "centosstream9-"),
|
||||||
|
(CENTOS_STREAM_10, "CentOS Stream", "10", "centosstream10-"),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_supported_official_cloud_images_have_distinct_safe_identifiers(
|
||||||
|
url, family, version, prefix):
|
||||||
|
assert validate_image_url(url) == url
|
||||||
|
assert image_metadata_for(url) == (family, version)
|
||||||
|
template_id = template_id_for("a" * 64, "b" * 64, version, family)
|
||||||
|
assert template_id.startswith(prefix)
|
||||||
|
assert template_volume_name(template_id) == (
|
||||||
|
f"panel-template-{template_id}.qcow2"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_template_identifier_rejects_invalid_digest_precisely():
|
||||||
|
with pytest.raises(TemplateError, match="SHA-256 格式"):
|
||||||
|
template_id_for("a" * 64, "not-a-digest", "12", "Debian")
|
||||||
|
with pytest.raises(TemplateError, match="系统版本无效"):
|
||||||
|
template_id_for("a" * 64, "b" * 64, ["12"], "Debian")
|
||||||
|
|
||||||
|
|
||||||
|
def qcow2_fixture(virtual_size=2 * 1024**3) -> bytes:
|
||||||
|
header = bytearray(104)
|
||||||
|
header[:4] = b"QFI\xfb"
|
||||||
|
header[4:8] = struct.pack(">I", 3)
|
||||||
|
header[20:24] = struct.pack(">I", 16)
|
||||||
|
header[24:32] = struct.pack(">Q", virtual_size)
|
||||||
|
header[100:104] = struct.pack(">I", 104)
|
||||||
|
return bytes(header) + b"fake qcow2 content for controlled import test"
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"url",
|
||||||
|
[
|
||||||
|
"http://repo.almalinux.org/almalinux/10/cloud/x86_64/images/AlmaLinux-10-GenericCloud-10.1-20260101.x86_64.qcow2",
|
||||||
|
"https://evil.example/almalinux/10/cloud/x86_64/images/AlmaLinux-10-GenericCloud-10.1-20260101.x86_64.qcow2",
|
||||||
|
"https://repo.almalinux.org.evil.example/almalinux/10/cloud/x86_64/images/AlmaLinux-10-GenericCloud-10.1-20260101.x86_64.qcow2",
|
||||||
|
"https://user:pass@repo.almalinux.org/almalinux/10/cloud/x86_64/images/AlmaLinux-10-GenericCloud-10.1-20260101.x86_64.qcow2",
|
||||||
|
"https://repo.almalinux.org/almalinux/10/cloud/x86_64/images/../../etc/passwd",
|
||||||
|
"https://repo.almalinux.org/almalinux/10/cloud/x86_64/images/AlmaLinux-10-GenericCloud-10.1-20260101.x86_64.qcow2?token=secret",
|
||||||
|
"https://cloud.debian.org/images/cloud/bookworm/latest/debian-13-generic-amd64.qcow2",
|
||||||
|
"https://cloud.debian.org/images/cloud/bookworm/20260801-2204/debian-12-generic-amd64-20260802-2205.qcow2",
|
||||||
|
"https://cloud.debian.org/images/cloud/bookworm/latest/debian-12-generic-arm64.qcow2",
|
||||||
|
"https://cloud-images.ubuntu.com/releases/noble/release/ubuntu-22.04-server-cloudimg-amd64.img",
|
||||||
|
"https://cloud-images.ubuntu.com/releases/24.04/release/ubuntu-24.04-server-cloudimg-arm64.img",
|
||||||
|
"https://cloud.centos.org/centos/9-stream/x86_64/images/CentOS-Stream-GenericCloud-10-latest.x86_64.qcow2",
|
||||||
|
"https://cloud.centos.org/centos/9-stream/aarch64/images/CentOS-Stream-GenericCloud-9-latest.aarch64.qcow2",
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_import_url_rejects_nonofficial_and_ambiguous_targets(url):
|
||||||
|
with pytest.raises(TemplateError):
|
||||||
|
validate_image_url(url)
|
||||||
|
|
||||||
|
|
||||||
|
def test_verified_image_is_private_and_has_expected_virtual_size(tmp_path):
|
||||||
|
body = qcow2_fixture()
|
||||||
|
digest = hashlib.sha256(body).hexdigest()
|
||||||
|
with patch("virsh_panel.cloud_templates._open_image_response",
|
||||||
|
return_value=fake_image_response(body)), patch(
|
||||||
|
"virsh_panel.cloud_templates._verify_qcow2_with_qemu"
|
||||||
|
) as checked:
|
||||||
|
downloaded, virtual_size, actual_sha256 = download_verified_image(
|
||||||
|
OFFICIAL, digest, tmp_path
|
||||||
|
)
|
||||||
|
try:
|
||||||
|
assert downloaded.read_bytes() == body
|
||||||
|
assert virtual_size == 2 * 1024**3
|
||||||
|
assert actual_sha256 == digest
|
||||||
|
assert stat.S_IMODE(downloaded.stat().st_mode) == 0o600
|
||||||
|
checked.assert_called_once_with(downloaded, 2 * 1024**3)
|
||||||
|
finally:
|
||||||
|
downloaded.unlink()
|
||||||
|
|
||||||
|
|
||||||
|
def test_image_without_expected_hash_records_computed_digest(tmp_path):
|
||||||
|
body = qcow2_fixture()
|
||||||
|
digest = hashlib.sha256(body).hexdigest()
|
||||||
|
with patch("virsh_panel.cloud_templates._open_image_response",
|
||||||
|
return_value=fake_image_response(body)), patch(
|
||||||
|
"virsh_panel.cloud_templates._verify_qcow2_with_qemu"
|
||||||
|
):
|
||||||
|
downloaded, virtual_size, actual_sha256 = download_verified_image(
|
||||||
|
OFFICIAL, "", tmp_path
|
||||||
|
)
|
||||||
|
try:
|
||||||
|
assert virtual_size == 2 * 1024**3
|
||||||
|
assert actual_sha256 == digest
|
||||||
|
finally:
|
||||||
|
downloaded.unlink()
|
||||||
|
|
||||||
|
|
||||||
|
def test_failed_hash_never_leaves_unverified_template_file(tmp_path):
|
||||||
|
body = qcow2_fixture()
|
||||||
|
with patch("virsh_panel.cloud_templates._open_image_response",
|
||||||
|
return_value=fake_image_response(body)):
|
||||||
|
with pytest.raises(TemplateError, match="SHA-256"):
|
||||||
|
download_verified_image(OFFICIAL, "0" * 64, tmp_path)
|
||||||
|
assert list(tmp_path.iterdir()) == []
|
||||||
|
|
||||||
|
|
||||||
|
def test_download_limit_preserves_persistent_disk_reserve(tmp_path):
|
||||||
|
body = qcow2_fixture()
|
||||||
|
digest = hashlib.sha256(body).hexdigest()
|
||||||
|
with patch("virsh_panel.cloud_templates._open_image_response",
|
||||||
|
return_value=fake_image_response(body)):
|
||||||
|
with pytest.raises(TemplateError, match="持久目录空间不足") as error:
|
||||||
|
download_verified_image(
|
||||||
|
OFFICIAL, digest, tmp_path, max_bytes=len(body) - 1
|
||||||
|
)
|
||||||
|
assert error.value.status == 507
|
||||||
|
assert list(tmp_path.iterdir()) == []
|
||||||
|
|
||||||
|
|
||||||
|
def test_redirect_to_nonofficial_target_is_rejected_before_following():
|
||||||
|
handler = _RejectRedirects()
|
||||||
|
with pytest.raises(TemplateError, match="重定向"):
|
||||||
|
handler.redirect_request(None, None, 302, "Found", {},
|
||||||
|
"http://127.0.0.1/private")
|
||||||
|
|
||||||
|
|
||||||
|
def test_debian_official_redirect_downloads_same_object_from_public_mirror(
|
||||||
|
tmp_path):
|
||||||
|
mirror = DEBIAN_12.replace(
|
||||||
|
"cloud.debian.org", "chuangtzu.ftp.acc.umu.se"
|
||||||
|
)
|
||||||
|
body = qcow2_fixture()
|
||||||
|
first = FakeImageResponse(
|
||||||
|
status=302, headers={"Location": [mirror]}
|
||||||
|
)
|
||||||
|
final = FakeImageResponse(
|
||||||
|
body,
|
||||||
|
status=200,
|
||||||
|
headers={"Content-Length": [str(len(body))]},
|
||||||
|
)
|
||||||
|
connections = [SimpleNamespace(close=lambda: None) for _ in range(2)]
|
||||||
|
with patch(
|
||||||
|
"virsh_panel.cloud_templates._resolve_global_addresses",
|
||||||
|
return_value=["2001:4860:4860::8888"],
|
||||||
|
), patch(
|
||||||
|
"virsh_panel.cloud_templates._open_pinned_response",
|
||||||
|
side_effect=[(connections[0], first), (connections[1], final)],
|
||||||
|
) as opened, patch(
|
||||||
|
"virsh_panel.cloud_templates._verify_qcow2_with_qemu"
|
||||||
|
):
|
||||||
|
downloaded, virtual_size, digest = download_verified_image(
|
||||||
|
DEBIAN_12, hashlib.sha256(body).hexdigest(), tmp_path
|
||||||
|
)
|
||||||
|
|
||||||
|
assert downloaded.read_bytes() == body
|
||||||
|
assert virtual_size == 2 * 1024**3
|
||||||
|
assert digest == hashlib.sha256(body).hexdigest()
|
||||||
|
assert opened.call_args_list[0].args[0].hostname == "cloud.debian.org"
|
||||||
|
assert opened.call_args_list[1].args[0].hostname == "chuangtzu.ftp.acc.umu.se"
|
||||||
|
assert opened.call_args_list[1].args[0].path == urlsplit(DEBIAN_12).path
|
||||||
|
downloaded.unlink()
|
||||||
|
|
||||||
|
|
||||||
|
def test_ubuntu_redirect_stays_on_official_host_and_same_release():
|
||||||
|
target = UBUNTU_24.replace(
|
||||||
|
"/releases/noble/release/", "/releases/24.04/release/"
|
||||||
|
)
|
||||||
|
first = FakeImageResponse(
|
||||||
|
status=302, headers={"Location": [UBUNTU_24]}
|
||||||
|
)
|
||||||
|
final = FakeImageResponse(b"image", status=200)
|
||||||
|
connections = [SimpleNamespace(close=lambda: None) for _ in range(2)]
|
||||||
|
with patch(
|
||||||
|
"virsh_panel.cloud_templates._resolve_global_addresses",
|
||||||
|
return_value=["8.8.8.8"],
|
||||||
|
), patch(
|
||||||
|
"virsh_panel.cloud_templates._open_pinned_response",
|
||||||
|
side_effect=[(connections[0], first), (connections[1], final)],
|
||||||
|
):
|
||||||
|
connection, response = _open_image_response(target)
|
||||||
|
|
||||||
|
assert connection is connections[1]
|
||||||
|
assert response is final
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"target",
|
||||||
|
[
|
||||||
|
"http://127.0.0.1/images/cloud/bookworm/latest/debian-12-generic-amd64.qcow2",
|
||||||
|
"https://mirror.example/images/cloud/bookworm/latest/debian-13-generic-amd64.qcow2",
|
||||||
|
"https://user:password@mirror.example/images/cloud/bookworm/latest/debian-12-generic-amd64.qcow2",
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_debian_redirect_rejects_unsafe_or_different_object(target):
|
||||||
|
with pytest.raises(TemplateError, match="重定向目标不安全"):
|
||||||
|
_redirect_target(
|
||||||
|
DEBIAN_12, DEBIAN_12, target, ("Debian", "12")
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_ubuntu_redirect_rejects_other_host_or_release():
|
||||||
|
for target in (
|
||||||
|
UBUNTU_24.replace("cloud-images.ubuntu.com", "mirror.example"),
|
||||||
|
UBUNTU_24.replace("24.04", "22.04"),
|
||||||
|
):
|
||||||
|
with pytest.raises(TemplateError, match="重定向目标不安全"):
|
||||||
|
_redirect_target(
|
||||||
|
UBUNTU_24, UBUNTU_24, target, ("Ubuntu", "24.04")
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_image_download_dns_rejects_any_nonpublic_answer():
|
||||||
|
rows = [
|
||||||
|
(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP, "",
|
||||||
|
("8.8.8.8", 443)),
|
||||||
|
(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP, "",
|
||||||
|
("127.0.0.1", 443)),
|
||||||
|
]
|
||||||
|
with patch("virsh_panel.cloud_templates.socket.getaddrinfo", return_value=rows):
|
||||||
|
with pytest.raises(TemplateError, match="非公网"):
|
||||||
|
_resolve_global_addresses("mirror.example")
|
||||||
|
|
||||||
|
|
||||||
|
def test_image_redirect_count_is_bounded():
|
||||||
|
def redirect_response(*_args):
|
||||||
|
connection = SimpleNamespace(close=lambda: None)
|
||||||
|
response = FakeImageResponse(
|
||||||
|
status=302, headers={"Location": [UBUNTU_24]}
|
||||||
|
)
|
||||||
|
return connection, response
|
||||||
|
|
||||||
|
with patch(
|
||||||
|
"virsh_panel.cloud_templates._resolve_global_addresses",
|
||||||
|
return_value=["8.8.8.8"],
|
||||||
|
), patch(
|
||||||
|
"virsh_panel.cloud_templates._open_pinned_response",
|
||||||
|
side_effect=redirect_response,
|
||||||
|
):
|
||||||
|
with pytest.raises(TemplateError, match="次数过多"):
|
||||||
|
_open_image_response(UBUNTU_24)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
("response", "message"),
|
||||||
|
[
|
||||||
|
(FakeImageResponse(status=404), "异常状态"),
|
||||||
|
(FakeImageResponse(
|
||||||
|
qcow2_fixture(), headers={"Content-Encoding": ["gzip"]}
|
||||||
|
), "响应格式无效"),
|
||||||
|
(FakeImageResponse(
|
||||||
|
qcow2_fixture(),
|
||||||
|
headers={"Content-Length": [str(len(qcow2_fixture()) + 1)]},
|
||||||
|
), "内容不完整"),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_image_download_rejects_unsafe_or_incomplete_http_response(
|
||||||
|
tmp_path, response, message):
|
||||||
|
connection = SimpleNamespace(close=lambda: None)
|
||||||
|
with patch(
|
||||||
|
"virsh_panel.cloud_templates._open_image_response",
|
||||||
|
return_value=(connection, response),
|
||||||
|
):
|
||||||
|
with pytest.raises(TemplateError, match=message):
|
||||||
|
download_verified_image(OFFICIAL, "", tmp_path)
|
||||||
|
assert list(tmp_path.iterdir()) == []
|
||||||
|
|
||||||
|
|
||||||
|
def test_external_data_file_feature_is_rejected(tmp_path):
|
||||||
|
body = bytearray(qcow2_fixture())
|
||||||
|
body[72:80] = struct.pack(">Q", 4)
|
||||||
|
digest = hashlib.sha256(body).hexdigest()
|
||||||
|
with patch("virsh_panel.cloud_templates._open_image_response",
|
||||||
|
return_value=fake_image_response(body)):
|
||||||
|
with pytest.raises(TemplateError, match="外部依赖"):
|
||||||
|
download_verified_image(OFFICIAL, digest, tmp_path)
|
||||||
|
assert list(tmp_path.iterdir()) == []
|
||||||
|
|
||||||
|
|
||||||
|
def test_qcow2_full_integrity_check_rejects_corrupt_image(tmp_path):
|
||||||
|
image = tmp_path / "corrupt.qcow2"
|
||||||
|
image.write_bytes(qcow2_fixture())
|
||||||
|
info = [{"format": "qcow2", "virtual-size": 2 * 1024**3}]
|
||||||
|
with patch(
|
||||||
|
"virsh_panel.cloud_templates._qemu_img_json",
|
||||||
|
side_effect=[info, TemplateError("云镜像完整性检查失败", 422)],
|
||||||
|
) as inspect:
|
||||||
|
with pytest.raises(TemplateError, match="完整性检查"):
|
||||||
|
verify_qcow2_image(image)
|
||||||
|
assert inspect.call_count == 2
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"info",
|
||||||
|
[
|
||||||
|
[{"format": "raw", "virtual-size": 2 * 1024**3}],
|
||||||
|
[{"format": "qcow2", "virtual-size": 2 * 1024**3,
|
||||||
|
"backing-filename": "/private/base.qcow2"}],
|
||||||
|
[{"format": "qcow2", "virtual-size": 2 * 1024**3,
|
||||||
|
"format-specific": {"data": {"data-file": "/private/data"}}}],
|
||||||
|
[{"format": "qcow2", "virtual-size": 2 * 1024**3},
|
||||||
|
{"format": "qcow2", "virtual-size": 2 * 1024**3}],
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_qcow2_full_integrity_check_rejects_dependencies_and_wrong_format(
|
||||||
|
tmp_path, info):
|
||||||
|
image = tmp_path / "unsafe.qcow2"
|
||||||
|
image.write_bytes(qcow2_fixture())
|
||||||
|
with patch("virsh_panel.cloud_templates._qemu_img_json",
|
||||||
|
return_value=info) as inspect:
|
||||||
|
with pytest.raises(TemplateError, match="外部依赖"):
|
||||||
|
verify_qcow2_image(image)
|
||||||
|
inspect.assert_called_once()
|
||||||
|
|
||||||
|
|
||||||
|
def test_template_catalog_is_private_and_scoped_to_host_and_disk_pool(tmp_path):
|
||||||
|
data_dir = tmp_path / "data"
|
||||||
|
data_dir.mkdir(mode=0o700)
|
||||||
|
store = TemplateStore(data_dir)
|
||||||
|
profile = {
|
||||||
|
"host": "hypervisor.example", "port": 2222,
|
||||||
|
"fingerprint": "SHA256:fixture", "storage_pool": "/home/kvm",
|
||||||
|
}
|
||||||
|
scope = template_scope(profile)
|
||||||
|
other_scope = template_scope({**profile, "storage_pool": "/other/pool"})
|
||||||
|
record = {
|
||||||
|
"id": "alma10-example", "name": "AlmaLinux 10", "sha256": "a" * 64,
|
||||||
|
"volume_name": "alma10-cloud.qcow2", "virtual_size": 2 * 1024**3,
|
||||||
|
}
|
||||||
|
assert store.add(scope, record) == record
|
||||||
|
assert store.list(scope) == [record]
|
||||||
|
assert store.list(other_scope) == []
|
||||||
|
assert TemplateStore(data_dir).get(scope, record["id"]) == record
|
||||||
|
assert stat.S_IMODE(store.file.stat().st_mode) == 0o600
|
||||||
|
with pytest.raises(TemplateError):
|
||||||
|
store.get(other_scope, record["id"])
|
||||||
|
with pytest.raises(TemplateError):
|
||||||
|
store.add(scope, record)
|
||||||
|
|
||||||
|
|
||||||
|
def test_template_rename_preserves_image_record_and_host_scope(tmp_path):
|
||||||
|
data_dir = tmp_path / "data"
|
||||||
|
data_dir.mkdir(mode=0o700)
|
||||||
|
store = TemplateStore(data_dir)
|
||||||
|
scope = "a" * 64
|
||||||
|
other_scope = "b" * 64
|
||||||
|
template_id = template_id_for(scope, "c" * 64, "9")
|
||||||
|
record = {
|
||||||
|
"id": template_id, "name": "AlmaLinux 9", "os_family": "AlmaLinux",
|
||||||
|
"os_version": "9", "sha256": "c" * 64,
|
||||||
|
"volume_name": template_volume_name(template_id), "virtual_size": 2 * 1024**3,
|
||||||
|
}
|
||||||
|
store.add(scope, record)
|
||||||
|
store.add(scope, {**record, "id": "other", "name": "Another name"})
|
||||||
|
store.add(other_scope, {**record, "name": "Other host"})
|
||||||
|
|
||||||
|
with pytest.raises(TemplateError) as inaccessible:
|
||||||
|
store.rename(other_scope, "other", "Missing")
|
||||||
|
assert inaccessible.value.status == 404
|
||||||
|
with pytest.raises(TemplateError) as duplicate:
|
||||||
|
store.rename(scope, template_id, "another NAME")
|
||||||
|
assert duplicate.value.status == 409
|
||||||
|
for invalid in ("", "x" * 65, "bad\x7fentry"):
|
||||||
|
with pytest.raises(TemplateError):
|
||||||
|
store.rename(scope, template_id, invalid)
|
||||||
|
|
||||||
|
updated = store.rename(scope, template_id, " 新名称 ")
|
||||||
|
assert updated == {**record, "name": "新名称"}
|
||||||
|
assert TemplateStore(data_dir).get(scope, template_id) == updated
|
||||||
|
assert store.get(other_scope, template_id)["name"] == "Other host"
|
||||||
|
assert stat.S_IMODE(store.file.stat().st_mode) == 0o600
|
||||||