You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
virt/engine/tests/test_cloud_templates.py

503 lines
19 KiB
Python

"""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