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.
271 lines
12 KiB
Python
271 lines
12 KiB
Python
"""Private cloud-image catalog and verified AlmaLinux image downloads."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import fcntl
|
|
import hashlib
|
|
import json
|
|
import os
|
|
import re
|
|
import stat
|
|
import struct
|
|
import tempfile
|
|
import threading
|
|
import time
|
|
import urllib.error
|
|
import urllib.request
|
|
from contextlib import contextmanager
|
|
from pathlib import Path
|
|
from urllib.parse import urlsplit
|
|
|
|
|
|
MAX_IMAGE_BYTES = 4 * 1024**3
|
|
MAX_VIRTUAL_BYTES = 4096 * 1024**3
|
|
MAX_DOWNLOAD_SECONDS = 3600
|
|
GIB = 1024**3
|
|
IMAGE_PATH = re.compile(
|
|
r"/almalinux/(?P<version>9|10)/cloud/x86_64/images/"
|
|
r"AlmaLinux-(?P=version)-GenericCloud-[A-Za-z0-9._-]+\.x86_64\.qcow2\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)
|
|
|
|
|
|
def template_scope(profile: dict) -> str:
|
|
"""Bind template records to this SSH host identity and disk pool."""
|
|
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 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 parsed.hostname != "repo.almalinux.org"
|
|
or port not in (None, 443) or parsed.username or parsed.password
|
|
or parsed.query or parsed.fragment or not IMAGE_PATH.fullmatch(parsed.path)):
|
|
raise TemplateError("仅支持 AlmaLinux 官方仓库的 9/10 x86_64 GenericCloud qcow2 镜像")
|
|
return url
|
|
|
|
|
|
def image_version_for(url: str) -> str:
|
|
"""Derive the OS major version from an already restricted official URL."""
|
|
validate_image_url(url)
|
|
match = IMAGE_PATH.fullmatch(urlsplit(url).path)
|
|
assert match is not None
|
|
return match.group("version")
|
|
|
|
|
|
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 template_id_for(scope: str, digest: str, version: str = "10") -> str:
|
|
if version not in ("9", "10"):
|
|
raise TemplateError("云镜像系统版本无效")
|
|
return f"alma{version}-{scope[:8]}-{digest[:16]}"
|
|
|
|
|
|
def template_volume_name(template_id: str) -> str:
|
|
if not re.fullmatch(r"alma(?:9|10)-[0-9a-f]{8}-[0-9a-f]{16}", template_id):
|
|
raise TemplateError("模板标识无效")
|
|
return f"panel-template-{template_id}.qcow2"
|
|
|
|
|
|
def download_verified_image(url: str, expected_sha256: str, directory: Path) -> tuple[Path, int]:
|
|
"""Download to a private temporary file; return path and qcow2 virtual size."""
|
|
validate_image_url(url)
|
|
if not isinstance(expected_sha256, str) or not SHA256_RE.fullmatch(expected_sha256):
|
|
raise TemplateError("请填写 64 位 SHA-256 校验值")
|
|
request = urllib.request.Request(url, headers={"User-Agent": "virsh-panel/1"})
|
|
fd, raw_path = tempfile.mkstemp(prefix=".cloud-image-", suffix=".qcow2", dir=directory)
|
|
image_path = Path(raw_path)
|
|
try:
|
|
os.fchmod(fd, 0o600)
|
|
checksum = hashlib.sha256()
|
|
size = 0
|
|
deadline = time.monotonic() + MAX_DOWNLOAD_SECONDS
|
|
try:
|
|
opener = urllib.request.build_opener(_RejectRedirects())
|
|
with opener.open(request, timeout=30) as response, 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_IMAGE_BYTES:
|
|
raise TemplateError("云镜像超过 4 GiB 限制", 413)
|
|
output.write(chunk)
|
|
checksum.update(chunk)
|
|
output.flush()
|
|
os.fsync(output.fileno())
|
|
except (OSError, urllib.error.URLError) as exc:
|
|
raise TemplateError("下载官方云镜像失败,请检查面板服务器网络", 502) from exc
|
|
if checksum.hexdigest() != expected_sha256.lower():
|
|
raise TemplateError("云镜像 SHA-256 不匹配,已丢弃下载内容", 422)
|
|
with image_path.open("rb") as image:
|
|
header = image.read(104)
|
|
if len(header) < 72 or header[:4] != b"QFI\xfb":
|
|
raise TemplateError("下载文件不是有效的 qcow2 镜像", 422)
|
|
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 version not in (2, 3) or not 9 <= cluster_bits <= 21:
|
|
raise TemplateError("云镜像 qcow2 版本或簇大小不受支持", 422)
|
|
if 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)
|
|
return image_path, virtual_size
|
|
except BaseException:
|
|
image_path.unlink(missing_ok=True)
|
|
raise
|
|
finally:
|
|
if fd >= 0:
|
|
os.close(fd)
|
|
|
|
|
|
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"}
|