The cloud-image download put image_url, hand-wrapped in single quotes, and the cache path built from its basename unquoted into commands that run as root on the node: a quote, $(...), ; or space in a URL from a release feed was acted on by the shell. The URL and every path built from it now go through shlex.quote (exists check, wget, mv, checksum, rm, unpack, qm importdisk), and the cache file name keeps only the characters Proxmox keeps in a content file name, as the import volume name already did. The checksum algorithm is checked against the ones Proxmox knows, since it is part of the command name. The file-name helpers move to image_names.py; vm_provision_mixin.py would otherwise cross the 1000-line limit. _download_cloud_image drops below 50 lines, so its baseline entry goes. Closes #43
71 lines
2.7 KiB
Python
71 lines
2.7 KiB
Python
"""File names and checksums of the cloud images a VM is provisioned from.
|
|
|
|
The image URL comes from whoever publishes the image (a release feed), so
|
|
nothing taken from it reaches a file name on the node unless Proxmox would keep
|
|
it in a content file name too.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import hashlib
|
|
import re
|
|
|
|
# Proxmox reads an import volume's format off its extension and accepts only
|
|
# these. Ubuntu ships its qcow2 cloud images as ".img", so that is stored as
|
|
# qcow2: if an .img is really raw, the import fails loudly, whereas guessing
|
|
# raw for a qcow2 would attach the qcow2 container as a raw disk silently.
|
|
_IMPORT_EXTENSIONS = {"qcow2": "qcow2", "raw": "raw", "vmdk": "vmdk", "img": "qcow2"}
|
|
|
|
# Characters Proxmox keeps in a content file name (PVE::Storage's
|
|
# SAFE_CHAR_CLASS_RE); anything else it would rewrite behind our back.
|
|
_UNSAFE_FILENAME_CHARS = re.compile(r"[^A-Za-z0-9\-.+=_]")
|
|
|
|
# The checksum algorithms Proxmox's download-url accepts; each has a coreutils
|
|
# "<algo>sum" on the node.
|
|
_CHECKSUM_ALGORITHMS = frozenset({"md5", "sha1", "sha224", "sha256", "sha384", "sha512"})
|
|
|
|
|
|
def url_key(image_url: str) -> str:
|
|
"""Short hash of the full URL — Ubuntu and others publish a new build
|
|
under the same basename every day, so the basename alone is no cache key."""
|
|
return hashlib.sha256(image_url.encode()).hexdigest()[:12]
|
|
|
|
|
|
def _basename(image_url: str) -> str:
|
|
return image_url.rstrip("/").rsplit("/", 1)[-1]
|
|
|
|
|
|
def split_checksum(image_checksum: str) -> tuple[str, str]:
|
|
"""``"<algo>:<hex>"`` as ``(algo, hex)``; the algorithm defaults to sha256.
|
|
|
|
:raises ValueError: for an algorithm Proxmox does not know.
|
|
"""
|
|
algo, _, expected = image_checksum.partition(":")
|
|
algo = (algo or "sha256").lower()
|
|
if algo not in _CHECKSUM_ALGORITHMS:
|
|
raise ValueError(f"unknown checksum algorithm {algo!r}")
|
|
return algo, expected
|
|
|
|
|
|
def cache_name(image_url: str) -> str:
|
|
"""The file name *image_url* gets in the node's image cache.
|
|
|
|
The URL's hash, then its basename with every character Proxmox would not
|
|
keep replaced by ``_``.
|
|
"""
|
|
return f"{url_key(image_url)}-{_UNSAFE_FILENAME_CHARS.sub('_', _basename(image_url))}"
|
|
|
|
|
|
def import_volume_name(image_url: str) -> str | None:
|
|
"""The file name *image_url* gets in an import storage.
|
|
|
|
None when Proxmox cannot import the image's type at all (compressed,
|
|
ISO, no extension) — provisioning then downloads it over SSH as before.
|
|
"""
|
|
stem, dot, ext = _basename(image_url).rpartition(".")
|
|
extension = _IMPORT_EXTENSIONS.get(ext.lower()) if dot and stem else None
|
|
if extension is None:
|
|
return None
|
|
safe_stem = _UNSAFE_FILENAME_CHARS.sub("_", stem)
|
|
return f"netork-{url_key(image_url)}-{safe_stem}.{extension}"
|