|
|
"""Private cloud-image catalog and verified official image downloads."""
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
import errno
|
|
|
import fcntl
|
|
|
import hashlib
|
|
|
import http.client
|
|
|
import ipaddress
|
|
|
import json
|
|
|
import os
|
|
|
import re
|
|
|
import socket
|
|
|
import ssl
|
|
|
import stat
|
|
|
import struct
|
|
|
import subprocess
|
|
|
import tempfile
|
|
|
import threading
|
|
|
import time
|
|
|
import urllib.request
|
|
|
from contextlib import contextmanager
|
|
|
from pathlib import Path
|
|
|
from urllib.parse import SplitResult, urljoin, urlsplit
|
|
|
|
|
|
|
|
|
MAX_IMAGE_BYTES = 4 * 1024**3
|
|
|
MAX_VIRTUAL_BYTES = 4096 * 1024**3
|
|
|
MAX_DOWNLOAD_SECONDS = 3600
|
|
|
MAX_IMAGE_REDIRECTS = 3
|
|
|
QEMU_IMG = "/usr/bin/qemu-img"
|
|
|
QEMU_IMG_TIMEOUT_SECONDS = 300
|
|
|
QEMU_IMG_OUTPUT_LIMIT = 1024 * 1024
|
|
|
GIB = 1024**3
|
|
|
ALMALINUX_IMAGE_PATH = re.compile(
|
|
|
r"/almalinux/(?P<version>8|9|10)/cloud/x86_64/images/"
|
|
|
r"AlmaLinux-(?P=version)-GenericCloud-[A-Za-z0-9._-]+\.x86_64\.qcow2\Z"
|
|
|
)
|
|
|
# Compatibility alias for integrations that imported the former Alma-only
|
|
|
# path expression directly.
|
|
|
IMAGE_PATH = ALMALINUX_IMAGE_PATH
|
|
|
DEBIAN_IMAGE_PATH = re.compile(
|
|
|
r"/images/cloud/(?P<codename>bullseye|bookworm|trixie)/"
|
|
|
r"(?P<build>latest|[0-9]{8}-[0-9]+)/"
|
|
|
r"debian-(?P<version>11|12|13)-generic(?:cloud)?-amd64"
|
|
|
r"(?:-(?P=build))?\.qcow2\Z"
|
|
|
)
|
|
|
UBUNTU_RELEASE_IMAGE_PATH = re.compile(
|
|
|
r"/releases/(?P<release>[a-z]+|[0-9]{2}\.[0-9]{2})/"
|
|
|
r"release(?:-[0-9]{8})?/"
|
|
|
r"ubuntu-(?P<version>[0-9]{2}\.(?:04|10))-server-cloudimg-amd64\.img\Z"
|
|
|
)
|
|
|
UBUNTU_CURRENT_IMAGE_PATH = re.compile(
|
|
|
r"/(?P<codename>focal|jammy|noble|plucky|questing|resolute)/current/"
|
|
|
r"(?P=codename)-server-cloudimg-amd64\.img\Z"
|
|
|
)
|
|
|
CENTOS_STREAM_IMAGE_PATH = re.compile(
|
|
|
r"/centos/(?P<version>9|10)-stream/x86_64/images/"
|
|
|
r"CentOS-Stream-GenericCloud-(?:x86_64-)?(?P=version)-"
|
|
|
r"[A-Za-z0-9._-]+\.x86_64\.qcow2\Z"
|
|
|
)
|
|
|
|
|
|
DEBIAN_RELEASES = {"bullseye": "11", "bookworm": "12", "trixie": "13"}
|
|
|
UBUNTU_RELEASES = {
|
|
|
"focal": "20.04",
|
|
|
"jammy": "22.04",
|
|
|
"noble": "24.04",
|
|
|
"plucky": "25.04",
|
|
|
"questing": "25.10",
|
|
|
"resolute": "26.04",
|
|
|
}
|
|
|
SUPPORTED_TEMPLATE_VERSIONS = {
|
|
|
"AlmaLinux": frozenset({"8", "9", "10"}),
|
|
|
"Debian": frozenset({"11", "12", "13"}),
|
|
|
"Ubuntu": frozenset(UBUNTU_RELEASES.values()),
|
|
|
"CentOS Stream": frozenset({"9", "10"}),
|
|
|
}
|
|
|
TEMPLATE_ID_RE = re.compile(
|
|
|
r"(?:alma(?:8|9|10)|debian(?:11|12|13)|"
|
|
|
r"ubuntu(?:2004|2204|2404|2504|2510|2604)|"
|
|
|
r"centosstream(?:9|10))-[0-9a-f]{8}-[0-9a-f]{16}\Z"
|
|
|
)
|
|
|
SHA256_RE = re.compile(r"[0-9a-fA-F]{64}\Z")
|
|
|
|
|
|
|
|
|
class TemplateError(Exception):
|
|
|
def __init__(self, message: str, status: int = 400) -> None:
|
|
|
super().__init__(message)
|
|
|
self.status = status
|
|
|
|
|
|
|
|
|
class _RejectRedirects(urllib.request.HTTPRedirectHandler):
|
|
|
"""Never let a validated official URL redirect to a different target."""
|
|
|
|
|
|
def redirect_request(self, request, file_pointer, code, message, headers, new_url):
|
|
|
raise TemplateError("官方镜像地址发生重定向,请使用最终的官方 HTTPS 直链", 422)
|
|
|
|
|
|
|
|
|
class _PinnedHTTPSConnection(http.client.HTTPSConnection):
|
|
|
"""TLS-connect to one validated public address without a second DNS lookup."""
|
|
|
|
|
|
def __init__(self, hostname: str, address: str, *, timeout: float) -> None:
|
|
|
super().__init__(
|
|
|
hostname, 443, timeout=timeout, context=ssl.create_default_context()
|
|
|
)
|
|
|
self._pinned_address = address
|
|
|
|
|
|
def connect(self) -> None:
|
|
|
raw = socket.create_connection(
|
|
|
(self._pinned_address, 443), self.timeout, self.source_address
|
|
|
)
|
|
|
try:
|
|
|
self.sock = self._context.wrap_socket(raw, server_hostname=self.host)
|
|
|
except Exception:
|
|
|
raw.close()
|
|
|
raise
|
|
|
|
|
|
|
|
|
def template_scope(profile: dict) -> str:
|
|
|
"""Bind template records to this SSH host identity and disk pool."""
|
|
|
if profile.get("host_id"):
|
|
|
raw = json.dumps(["virt-cloud", profile["host_id"], profile["storage_pool"]])
|
|
|
return hashlib.sha256(raw.encode("ascii")).hexdigest()
|
|
|
keys = ("host", "port", "fingerprint", "storage_pool")
|
|
|
if not all(key in profile for key in keys):
|
|
|
raise TemplateError("宿主机资料不完整,无法选择镜像模板", 409)
|
|
|
identity = json.dumps([profile[key] for key in keys], ensure_ascii=False,
|
|
|
separators=(",", ":"))
|
|
|
return hashlib.sha256(identity.encode("utf-8")).hexdigest()
|
|
|
|
|
|
|
|
|
def _image_metadata(parsed) -> tuple[str, str] | None:
|
|
|
"""Return the canonical family/version for an allowlisted official URL."""
|
|
|
match = None
|
|
|
if parsed.hostname == "repo.almalinux.org":
|
|
|
match = ALMALINUX_IMAGE_PATH.fullmatch(parsed.path)
|
|
|
if match is not None:
|
|
|
return "AlmaLinux", match.group("version")
|
|
|
elif parsed.hostname == "cloud.debian.org":
|
|
|
match = DEBIAN_IMAGE_PATH.fullmatch(parsed.path)
|
|
|
if (match is not None
|
|
|
and DEBIAN_RELEASES.get(match.group("codename"))
|
|
|
== match.group("version")):
|
|
|
build = match.group("build")
|
|
|
# The immutable Debian build suffix, when present, must match its
|
|
|
# enclosing directory. ``latest`` filenames have no suffix.
|
|
|
has_suffix = match.group(0).endswith(f"-{build}.qcow2")
|
|
|
if (build == "latest" and not has_suffix) or (
|
|
|
build != "latest" and has_suffix):
|
|
|
return "Debian", match.group("version")
|
|
|
elif parsed.hostname == "cloud-images.ubuntu.com":
|
|
|
match = UBUNTU_RELEASE_IMAGE_PATH.fullmatch(parsed.path)
|
|
|
if match is not None:
|
|
|
version = match.group("version")
|
|
|
release = match.group("release")
|
|
|
if release == version or UBUNTU_RELEASES.get(release) == version:
|
|
|
return "Ubuntu", version
|
|
|
match = UBUNTU_CURRENT_IMAGE_PATH.fullmatch(parsed.path)
|
|
|
if match is not None:
|
|
|
return "Ubuntu", UBUNTU_RELEASES[match.group("codename")]
|
|
|
elif parsed.hostname == "cloud.centos.org":
|
|
|
match = CENTOS_STREAM_IMAGE_PATH.fullmatch(parsed.path)
|
|
|
if match is not None:
|
|
|
return "CentOS Stream", match.group("version")
|
|
|
return None
|
|
|
|
|
|
|
|
|
def validate_image_url(url: str) -> str:
|
|
|
if not isinstance(url, str) or len(url) > 2048:
|
|
|
raise TemplateError("云镜像地址无效")
|
|
|
try:
|
|
|
parsed = urlsplit(url)
|
|
|
except ValueError as exc:
|
|
|
raise TemplateError("云镜像地址无效") from exc
|
|
|
try:
|
|
|
port = parsed.port
|
|
|
except ValueError as exc:
|
|
|
raise TemplateError("云镜像地址无效") from exc
|
|
|
if (parsed.scheme != "https" or port not in (None, 443)
|
|
|
or parsed.username or parsed.password or parsed.query
|
|
|
or parsed.fragment or _image_metadata(parsed) is None):
|
|
|
raise TemplateError(
|
|
|
"仅支持 AlmaLinux、Debian、Ubuntu 和 CentOS Stream 官方 x86_64 云镜像直链"
|
|
|
)
|
|
|
return url
|
|
|
|
|
|
|
|
|
def image_metadata_for(url: str) -> tuple[str, str]:
|
|
|
"""Derive canonical OS metadata from a restricted official image URL."""
|
|
|
validate_image_url(url)
|
|
|
metadata = _image_metadata(urlsplit(url))
|
|
|
assert metadata is not None
|
|
|
return metadata
|
|
|
|
|
|
|
|
|
def _resolve_global_addresses(hostname: str) -> list[str]:
|
|
|
try:
|
|
|
rows = socket.getaddrinfo(
|
|
|
hostname,
|
|
|
443,
|
|
|
family=socket.AF_UNSPEC,
|
|
|
type=socket.SOCK_STREAM,
|
|
|
proto=socket.IPPROTO_TCP,
|
|
|
)
|
|
|
except socket.gaierror as exc:
|
|
|
raise TemplateError("官方云镜像地址无法解析", 422) from exc
|
|
|
addresses: list[str] = []
|
|
|
for _family, _socktype, _proto, _canonname, sockaddr in rows:
|
|
|
try:
|
|
|
address = str(ipaddress.ip_address(sockaddr[0]))
|
|
|
except (IndexError, TypeError, ValueError) as exc:
|
|
|
raise TemplateError("官方云镜像地址解析结果无效", 422) from exc
|
|
|
if not ipaddress.ip_address(address).is_global:
|
|
|
raise TemplateError("官方云镜像地址解析到非公网地址", 422)
|
|
|
if address not in addresses:
|
|
|
addresses.append(address)
|
|
|
if not addresses:
|
|
|
raise TemplateError("官方云镜像地址无法解析", 422)
|
|
|
return addresses
|
|
|
|
|
|
|
|
|
def _open_pinned_response(parsed: SplitResult, addresses: list[str]):
|
|
|
hostname = parsed.hostname
|
|
|
assert hostname is not None
|
|
|
host_header = f"[{hostname}]" if ":" in hostname else hostname
|
|
|
last_error: Exception | None = None
|
|
|
for address in addresses:
|
|
|
connection = _PinnedHTTPSConnection(hostname, address, timeout=30)
|
|
|
try:
|
|
|
connection.request(
|
|
|
"GET",
|
|
|
parsed.path,
|
|
|
headers={
|
|
|
"Host": host_header,
|
|
|
"User-Agent": "virsh-panel/1",
|
|
|
"Accept": "application/octet-stream,*/*;q=0.1",
|
|
|
"Accept-Encoding": "identity",
|
|
|
"Connection": "close",
|
|
|
},
|
|
|
)
|
|
|
return connection, connection.getresponse()
|
|
|
except (OSError, ssl.SSLError, http.client.HTTPException) as exc:
|
|
|
last_error = exc
|
|
|
connection.close()
|
|
|
raise TemplateError("无法连接官方云镜像下载地址", 502) from last_error
|
|
|
|
|
|
|
|
|
def _basic_redirect_target(current_url: str, location: str) -> tuple[str, SplitResult]:
|
|
|
if not isinstance(location, str) or not location or len(location) > 2048:
|
|
|
raise TemplateError("官方云镜像重定向目标不安全", 422)
|
|
|
target = urljoin(current_url, location)
|
|
|
try:
|
|
|
parsed = urlsplit(target)
|
|
|
port = parsed.port
|
|
|
hostname = parsed.hostname
|
|
|
target.encode("ascii")
|
|
|
if hostname is not None:
|
|
|
hostname.encode("idna")
|
|
|
except (TypeError, UnicodeError, ValueError) as exc:
|
|
|
raise TemplateError("官方云镜像重定向目标不安全", 422) from exc
|
|
|
if (parsed.scheme != "https" or not hostname or port not in (None, 443)
|
|
|
or parsed.username or parsed.password or parsed.query
|
|
|
or parsed.fragment or any(
|
|
|
ord(char) < 33 or ord(char) == 127 for char in target
|
|
|
)):
|
|
|
raise TemplateError("官方云镜像重定向目标不安全", 422)
|
|
|
return target, parsed
|
|
|
|
|
|
|
|
|
def _redirect_target(
|
|
|
original_url: str,
|
|
|
current_url: str,
|
|
|
location: str,
|
|
|
metadata: tuple[str, str],
|
|
|
) -> str:
|
|
|
"""Validate one server-selected redirect without weakening the URL policy."""
|
|
|
target, parsed = _basic_redirect_target(current_url, location)
|
|
|
family, version = metadata
|
|
|
if family == "Ubuntu":
|
|
|
try:
|
|
|
target_metadata = image_metadata_for(target)
|
|
|
except TemplateError as exc:
|
|
|
raise TemplateError(
|
|
|
"官方云镜像重定向目标不安全", 422
|
|
|
) from exc
|
|
|
if (parsed.hostname != "cloud-images.ubuntu.com"
|
|
|
or target_metadata != metadata):
|
|
|
raise TemplateError("官方云镜像重定向目标不安全", 422)
|
|
|
return target
|
|
|
if family == "Debian":
|
|
|
original_path = urlsplit(original_url).path
|
|
|
current_host = urlsplit(current_url).hostname
|
|
|
# Debian's official endpoint delegates the exact same object path to
|
|
|
# a public mirror. Only that first official cross-host hop is trusted;
|
|
|
# a mirror can subsequently redirect only within its own hostname.
|
|
|
if (parsed.path != original_path
|
|
|
or DEBIAN_IMAGE_PATH.fullmatch(parsed.path) is None
|
|
|
or (current_host != "cloud.debian.org"
|
|
|
and parsed.hostname != current_host)):
|
|
|
raise TemplateError("官方云镜像重定向目标不安全", 422)
|
|
|
match = DEBIAN_IMAGE_PATH.fullmatch(parsed.path)
|
|
|
assert match is not None
|
|
|
if (DEBIAN_RELEASES.get(match.group("codename")) != version
|
|
|
or match.group("version") != version):
|
|
|
raise TemplateError("官方云镜像重定向目标不安全", 422)
|
|
|
return target
|
|
|
raise TemplateError(
|
|
|
"官方镜像地址发生重定向,请使用最终的官方 HTTPS 直链", 422
|
|
|
)
|
|
|
|
|
|
|
|
|
def _open_image_response(url: str):
|
|
|
"""Open one official image with bounded, family-specific redirects."""
|
|
|
metadata = image_metadata_for(url)
|
|
|
current_url = url
|
|
|
for redirect_count in range(MAX_IMAGE_REDIRECTS + 1):
|
|
|
parsed = urlsplit(current_url)
|
|
|
hostname = parsed.hostname
|
|
|
assert hostname is not None
|
|
|
addresses = _resolve_global_addresses(hostname)
|
|
|
connection, response = _open_pinned_response(parsed, addresses)
|
|
|
if response.status not in {301, 302, 303, 307, 308}:
|
|
|
return connection, response
|
|
|
location = response.getheader("Location", "")
|
|
|
response.close()
|
|
|
connection.close()
|
|
|
if redirect_count >= MAX_IMAGE_REDIRECTS:
|
|
|
raise TemplateError("官方云镜像重定向次数过多", 422)
|
|
|
current_url = _redirect_target(url, current_url, location, metadata)
|
|
|
raise TemplateError("官方云镜像重定向次数过多", 422)
|
|
|
|
|
|
|
|
|
def _declared_size(response: http.client.HTTPResponse) -> int | None:
|
|
|
values = response.headers.get_all("Content-Length", [])
|
|
|
if not values:
|
|
|
return None
|
|
|
if len(values) != 1 or re.fullmatch(r"[0-9]+", values[0].strip()) is None:
|
|
|
raise TemplateError("官方云镜像下载响应格式无效", 422)
|
|
|
return int(values[0])
|
|
|
|
|
|
|
|
|
def image_version_for(url: str) -> str:
|
|
|
"""Derive the OS major version from an already restricted official URL."""
|
|
|
return image_metadata_for(url)[1]
|
|
|
|
|
|
|
|
|
def validate_template_name(name: str) -> str:
|
|
|
if not isinstance(name, str) or not 1 <= len(name.strip()) <= 64:
|
|
|
raise TemplateError("模板名称应为 1 至 64 个字符")
|
|
|
if any(ord(char) < 32 or ord(char) == 127 for char in name):
|
|
|
raise TemplateError("模板名称不能包含控制字符")
|
|
|
return name.strip()
|
|
|
|
|
|
|
|
|
def validate_template_metadata(os_family: str, version: str) -> tuple[str, str]:
|
|
|
"""Validate administrator-supplied metadata for an uploaded image.
|
|
|
|
|
|
Remote imports derive these values from an allowlisted official URL. A
|
|
|
browser upload has no trustworthy filename or URL, so it must provide the
|
|
|
exact canonical values already supported by template identifiers.
|
|
|
"""
|
|
|
if (not isinstance(os_family, str) or not isinstance(version, str)
|
|
|
or version not in SUPPORTED_TEMPLATE_VERSIONS.get(os_family, ())):
|
|
|
raise TemplateError("云镜像系统或版本不受支持", 422)
|
|
|
return os_family, version
|
|
|
|
|
|
|
|
|
def _template_id_prefix(os_family: str, version: str) -> str:
|
|
|
try:
|
|
|
validate_template_metadata(os_family, version)
|
|
|
except TemplateError as exc:
|
|
|
raise TemplateError("云镜像系统版本无效") from exc
|
|
|
if os_family == "AlmaLinux":
|
|
|
return f"alma{version}"
|
|
|
if os_family == "Debian":
|
|
|
return f"debian{version}"
|
|
|
if os_family == "Ubuntu":
|
|
|
return f"ubuntu{version.replace('.', '')}"
|
|
|
if os_family == "CentOS Stream":
|
|
|
return f"centosstream{version}"
|
|
|
raise TemplateError("云镜像系统版本无效")
|
|
|
|
|
|
|
|
|
def template_id_for(scope: str, digest: str, version: str = "10",
|
|
|
os_family: str = "AlmaLinux") -> str:
|
|
|
prefix = _template_id_prefix(os_family, version)
|
|
|
if not isinstance(scope, str) or not re.fullmatch(r"[0-9a-f]{8,}", scope):
|
|
|
raise TemplateError("云镜像模板范围无效")
|
|
|
if not isinstance(digest, str) or SHA256_RE.fullmatch(digest) is None:
|
|
|
raise TemplateError("云镜像 SHA-256 格式无效")
|
|
|
return f"{prefix}-{scope[:8]}-{digest.lower()[:16]}"
|
|
|
|
|
|
|
|
|
def template_volume_name(template_id: str) -> str:
|
|
|
if (not isinstance(template_id, str)
|
|
|
or TEMPLATE_ID_RE.fullmatch(template_id) is None):
|
|
|
raise TemplateError("模板标识无效")
|
|
|
return f"panel-template-{template_id}.qcow2"
|
|
|
|
|
|
|
|
|
def _qemu_img_json(arguments: list[str]) -> object:
|
|
|
"""Run the trusted local image inspector with bounded time and output."""
|
|
|
with tempfile.TemporaryFile() as stdout, tempfile.TemporaryFile() as stderr:
|
|
|
try:
|
|
|
result = subprocess.run(
|
|
|
[QEMU_IMG, *arguments],
|
|
|
cwd="/",
|
|
|
env={"PATH": "/usr/bin:/bin", "LANG": "C", "LC_ALL": "C"},
|
|
|
stdin=subprocess.DEVNULL,
|
|
|
stdout=stdout,
|
|
|
stderr=stderr,
|
|
|
timeout=QEMU_IMG_TIMEOUT_SECONDS,
|
|
|
check=False,
|
|
|
close_fds=True,
|
|
|
)
|
|
|
except FileNotFoundError as exc:
|
|
|
raise TemplateError(
|
|
|
"面板未安装 qemu-img,无法验证云镜像", 503
|
|
|
) from exc
|
|
|
except (OSError, subprocess.TimeoutExpired) as exc:
|
|
|
raise TemplateError("云镜像完整性检查失败", 422) from exc
|
|
|
if (stdout.tell() > QEMU_IMG_OUTPUT_LIMIT
|
|
|
or stderr.tell() > QEMU_IMG_OUTPUT_LIMIT):
|
|
|
raise TemplateError("云镜像完整性检查失败", 422)
|
|
|
if result.returncode != 0:
|
|
|
raise TemplateError("云镜像完整性检查失败", 422)
|
|
|
stdout.seek(0)
|
|
|
try:
|
|
|
return json.loads(stdout.read(QEMU_IMG_OUTPUT_LIMIT + 1))
|
|
|
except (UnicodeDecodeError, json.JSONDecodeError) as exc:
|
|
|
raise TemplateError("云镜像完整性检查失败", 422) from exc
|
|
|
|
|
|
|
|
|
def _contains_external_qcow2_dependency(value: object) -> bool:
|
|
|
"""Reject dependency/encryption fields wherever qemu-img reports them."""
|
|
|
if isinstance(value, dict):
|
|
|
for key, nested in value.items():
|
|
|
normalized = str(key).lower().replace("_", "-")
|
|
|
if normalized in {
|
|
|
"backing-filename", "full-backing-filename", "data-file",
|
|
|
"full-data-file", "encrypted", "encrypt", "encryption",
|
|
|
} and (nested is not None and nested is not False
|
|
|
and nested != "" and nested != 0):
|
|
|
return True
|
|
|
if _contains_external_qcow2_dependency(nested):
|
|
|
return True
|
|
|
elif isinstance(value, list):
|
|
|
return any(_contains_external_qcow2_dependency(item) for item in value)
|
|
|
return False
|
|
|
|
|
|
|
|
|
def _verify_qcow2_with_qemu(image_path: Path, virtual_size: int) -> None:
|
|
|
path = str(Path(image_path))
|
|
|
info = _qemu_img_json(["info", "--output=json", "--backing-chain", path])
|
|
|
chain = info if isinstance(info, list) else [info]
|
|
|
if (len(chain) != 1 or not isinstance(chain[0], dict)
|
|
|
or chain[0].get("format") != "qcow2"
|
|
|
or chain[0].get("virtual-size") != virtual_size
|
|
|
or _contains_external_qcow2_dependency(chain[0])):
|
|
|
raise TemplateError(
|
|
|
"云镜像含外部依赖、加密或容量超出支持范围", 422
|
|
|
)
|
|
|
checked = _qemu_img_json(["check", "--output=json", "-f", "qcow2", path])
|
|
|
if (not isinstance(checked, dict)
|
|
|
or any(isinstance(checked.get(key), int) and checked[key] > 0
|
|
|
for key in ("corruptions", "leaks", "check-errors"))):
|
|
|
raise TemplateError("云镜像完整性检查失败", 422)
|
|
|
|
|
|
|
|
|
def verify_qcow2_image(image_path: Path) -> int:
|
|
|
"""Return capacity after a header and full qemu-img integrity check."""
|
|
|
with Path(image_path).open("rb") as image:
|
|
|
header = image.read(104)
|
|
|
if len(header) < 72 or header[:4] != b"QFI\xfb":
|
|
|
raise TemplateError("上传文件不是有效的 qcow2 镜像", 422)
|
|
|
qcow_version = struct.unpack(">I", header[4:8])[0]
|
|
|
backing_offset = struct.unpack(">Q", header[8:16])[0]
|
|
|
cluster_bits = struct.unpack(">I", header[20:24])[0]
|
|
|
virtual_size = struct.unpack(">Q", header[24:32])[0]
|
|
|
crypt_method = struct.unpack(">I", header[32:36])[0]
|
|
|
if qcow_version not in (2, 3) or not 9 <= cluster_bits <= 21:
|
|
|
raise TemplateError("云镜像 qcow2 版本或簇大小不受支持", 422)
|
|
|
if qcow_version == 3:
|
|
|
if len(header) < 104:
|
|
|
raise TemplateError("云镜像 qcow2 头部不完整", 422)
|
|
|
incompatible = struct.unpack(">Q", header[72:80])[0]
|
|
|
header_length = struct.unpack(">I", header[100:104])[0]
|
|
|
# The external data-file, dirty and corrupt flags all make this
|
|
|
# unsuitable as a standalone template. Reject unknown features.
|
|
|
if (incompatible & ~0x18 or header_length < 104
|
|
|
or header_length > 1 << cluster_bits or header_length % 8):
|
|
|
raise TemplateError(
|
|
|
"云镜像包含不受支持或外部依赖的 qcow2 特性", 422)
|
|
|
if (backing_offset or crypt_method
|
|
|
or not GIB <= virtual_size <= MAX_VIRTUAL_BYTES):
|
|
|
raise TemplateError("云镜像含外部依赖、加密或容量超出支持范围", 422)
|
|
|
_verify_qcow2_with_qemu(Path(image_path), virtual_size)
|
|
|
return virtual_size
|
|
|
|
|
|
|
|
|
def download_verified_image(
|
|
|
url: str, expected_sha256: str, directory: Path, *,
|
|
|
max_bytes: int = MAX_IMAGE_BYTES,
|
|
|
) -> tuple[Path, int, str]:
|
|
|
"""Download privately and return path, qcow2 virtual size and actual digest."""
|
|
|
validate_image_url(url)
|
|
|
if not isinstance(expected_sha256, str):
|
|
|
raise TemplateError("SHA-256 校验值格式无效")
|
|
|
if expected_sha256 and not SHA256_RE.fullmatch(expected_sha256):
|
|
|
raise TemplateError("SHA-256 可留空;填写时必须为 64 位十六进制校验值")
|
|
|
if (isinstance(max_bytes, bool) or not isinstance(max_bytes, int)
|
|
|
or max_bytes < 1 or max_bytes > MAX_IMAGE_BYTES):
|
|
|
raise TemplateError("面板云镜像持久目录空间不足", 507)
|
|
|
fd, raw_path = tempfile.mkstemp(prefix=".cloud-image-", suffix=".qcow2", dir=directory)
|
|
|
image_path = Path(raw_path)
|
|
|
connection = None
|
|
|
response = None
|
|
|
try:
|
|
|
os.fchmod(fd, 0o600)
|
|
|
checksum = hashlib.sha256()
|
|
|
size = 0
|
|
|
deadline = time.monotonic() + MAX_DOWNLOAD_SECONDS
|
|
|
try:
|
|
|
connection, response = _open_image_response(url)
|
|
|
if response.status != 200:
|
|
|
raise TemplateError(
|
|
|
"官方云镜像下载服务器返回异常状态", 422
|
|
|
)
|
|
|
encoding = response.getheader(
|
|
|
"Content-Encoding", "identity"
|
|
|
).strip().lower()
|
|
|
if encoding not in {"", "identity"}:
|
|
|
raise TemplateError("官方云镜像下载响应格式无效", 422)
|
|
|
declared = _declared_size(response)
|
|
|
if declared is not None and declared > max_bytes:
|
|
|
if max_bytes < MAX_IMAGE_BYTES:
|
|
|
raise TemplateError("面板云镜像持久目录空间不足", 507)
|
|
|
raise TemplateError("云镜像超过 4 GiB 限制", 413)
|
|
|
with os.fdopen(fd, "wb") as output:
|
|
|
fd = -1
|
|
|
while True:
|
|
|
if time.monotonic() > deadline:
|
|
|
raise TemplateError("云镜像下载超过 60 分钟,请检查面板服务器网络", 504)
|
|
|
chunk = response.read(1024 * 1024)
|
|
|
if not chunk:
|
|
|
break
|
|
|
size += len(chunk)
|
|
|
if size > max_bytes:
|
|
|
if max_bytes < MAX_IMAGE_BYTES:
|
|
|
raise TemplateError(
|
|
|
"面板云镜像持久目录空间不足", 507)
|
|
|
raise TemplateError("云镜像超过 4 GiB 限制", 413)
|
|
|
output.write(chunk)
|
|
|
checksum.update(chunk)
|
|
|
output.flush()
|
|
|
os.fsync(output.fileno())
|
|
|
if declared is not None and size != declared:
|
|
|
raise TemplateError("云镜像下载内容不完整", 502)
|
|
|
except (OSError, ssl.SSLError, http.client.HTTPException) as exc:
|
|
|
if getattr(exc, "errno", None) in {errno.ENOSPC, errno.EDQUOT}:
|
|
|
raise TemplateError(
|
|
|
"面板云镜像持久目录空间不足", 507) from exc
|
|
|
raise TemplateError("下载官方云镜像失败,请检查面板服务器网络", 502) from exc
|
|
|
actual_sha256 = checksum.hexdigest()
|
|
|
if expected_sha256 and actual_sha256 != expected_sha256.lower():
|
|
|
raise TemplateError("云镜像 SHA-256 不匹配,已丢弃下载内容", 422)
|
|
|
try:
|
|
|
virtual_size = verify_qcow2_image(image_path)
|
|
|
except TemplateError as exc:
|
|
|
if str(exc) == "上传文件不是有效的 qcow2 镜像":
|
|
|
raise TemplateError("下载文件不是有效的 qcow2 镜像", 422) from exc
|
|
|
raise
|
|
|
return image_path, virtual_size, actual_sha256
|
|
|
except BaseException:
|
|
|
image_path.unlink(missing_ok=True)
|
|
|
raise
|
|
|
finally:
|
|
|
if fd >= 0:
|
|
|
os.close(fd)
|
|
|
if response is not None:
|
|
|
response.close()
|
|
|
if connection is not None:
|
|
|
connection.close()
|
|
|
|
|
|
|
|
|
class TemplateStore:
|
|
|
"""Small atomic catalog; never stores guest credentials or SSH keys."""
|
|
|
|
|
|
def __init__(self, data_dir: Path) -> None:
|
|
|
self.directory = Path(data_dir)
|
|
|
self.file = self.directory / "templates.json"
|
|
|
self.lock_file = self.directory / ".templates.lock"
|
|
|
self._lock = threading.RLock()
|
|
|
|
|
|
@contextmanager
|
|
|
def _locked(self):
|
|
|
with self._lock:
|
|
|
flags = os.O_RDWR | os.O_CREAT | getattr(os, "O_NOFOLLOW", 0)
|
|
|
fd = os.open(self.lock_file, flags, 0o600)
|
|
|
try:
|
|
|
info = os.fstat(fd)
|
|
|
if not stat.S_ISREG(info.st_mode) or info.st_mode & 0o077:
|
|
|
raise TemplateError("模板锁文件权限不安全", 500)
|
|
|
fcntl.flock(fd, fcntl.LOCK_EX)
|
|
|
try:
|
|
|
yield
|
|
|
finally:
|
|
|
fcntl.flock(fd, fcntl.LOCK_UN)
|
|
|
finally:
|
|
|
os.close(fd)
|
|
|
|
|
|
def _read(self) -> dict:
|
|
|
try:
|
|
|
info = self.file.lstat()
|
|
|
if not stat.S_ISREG(info.st_mode) or info.st_mode & 0o077:
|
|
|
raise TemplateError("模板目录文件权限不安全", 500)
|
|
|
with self.file.open("r", encoding="utf-8") as stream:
|
|
|
data = json.load(stream)
|
|
|
except FileNotFoundError:
|
|
|
return {"version": 1, "templates": []}
|
|
|
except (OSError, ValueError, json.JSONDecodeError) as exc:
|
|
|
raise TemplateError("模板目录文件损坏或不可读取", 500) from exc
|
|
|
if (not isinstance(data, dict) or data.get("version") != 1
|
|
|
or not isinstance(data.get("templates"), list)):
|
|
|
raise TemplateError("模板目录文件格式无效", 500)
|
|
|
return data
|
|
|
|
|
|
def _write(self, data: dict) -> None:
|
|
|
fd, raw_path = tempfile.mkstemp(prefix=".templates-", dir=self.directory)
|
|
|
try:
|
|
|
os.fchmod(fd, 0o600)
|
|
|
with os.fdopen(fd, "w", encoding="utf-8") as stream:
|
|
|
json.dump(data, stream, ensure_ascii=False, separators=(",", ":"))
|
|
|
stream.write("\n")
|
|
|
stream.flush()
|
|
|
os.fsync(stream.fileno())
|
|
|
os.replace(raw_path, self.file)
|
|
|
directory_fd = os.open(self.directory, os.O_RDONLY)
|
|
|
try:
|
|
|
os.fsync(directory_fd)
|
|
|
finally:
|
|
|
os.close(directory_fd)
|
|
|
finally:
|
|
|
if os.path.exists(raw_path):
|
|
|
os.unlink(raw_path)
|
|
|
|
|
|
def list(self, scope: str) -> list[dict]:
|
|
|
with self._locked():
|
|
|
records = self._read()["templates"]
|
|
|
return [{key: value for key, value in record.items() if key != "scope"}
|
|
|
for record in records if record.get("scope") == scope]
|
|
|
|
|
|
def get(self, scope: str, template_id: str) -> dict:
|
|
|
for record in self.list(scope):
|
|
|
if record.get("id") == template_id:
|
|
|
return record
|
|
|
raise TemplateError("模板不存在或不属于当前宿主机", 404)
|
|
|
|
|
|
def add(self, scope: str, record: dict) -> dict:
|
|
|
with self._locked():
|
|
|
data = self._read()
|
|
|
if any(item.get("scope") == scope and item.get("id") == record["id"]
|
|
|
for item in data["templates"]):
|
|
|
raise TemplateError("此云镜像已导入", 409)
|
|
|
if any(item.get("scope") == scope
|
|
|
and item.get("name", "").casefold() == record["name"].casefold()
|
|
|
for item in data["templates"]):
|
|
|
raise TemplateError("当前宿主机已有同名云镜像模板", 409)
|
|
|
data["templates"].append({**record, "scope": scope})
|
|
|
self._write(data)
|
|
|
return record
|
|
|
|
|
|
def rename(self, scope: str, template_id: str, name: str) -> dict:
|
|
|
name = validate_template_name(name)
|
|
|
with self._locked():
|
|
|
data = self._read()
|
|
|
target = next((record for record in data["templates"]
|
|
|
if record.get("scope") == scope and record.get("id") == template_id),
|
|
|
None)
|
|
|
if target is None:
|
|
|
raise TemplateError("模板不存在或不属于当前宿主机", 404)
|
|
|
if any(record.get("scope") == scope and record.get("id") != template_id
|
|
|
and record.get("name", "").casefold() == name.casefold()
|
|
|
for record in data["templates"]):
|
|
|
raise TemplateError("当前宿主机已有同名云镜像模板", 409)
|
|
|
target["name"] = name
|
|
|
self._write(data)
|
|
|
return {key: value for key, value in target.items() if key != "scope"}
|