|
|
|
|
@ -34,7 +34,12 @@ from virsh_panel.cloud_init import (
|
|
|
|
|
)
|
|
|
|
|
from virsh_panel.metrics import RawVMStats
|
|
|
|
|
from virsh_panel.inventory_diagnostics import inventory_stage
|
|
|
|
|
from virsh_panel.platform_network_policy import PLATFORM_DEFAULT_NETWORK_POLICY
|
|
|
|
|
from virsh_panel.platform_network_policy import (
|
|
|
|
|
MAX_PLATFORM_POLICY_SNAPSHOT_BYTES,
|
|
|
|
|
PLATFORM_DEFAULT_NETWORK_POLICY,
|
|
|
|
|
default_platform_policy_snapshot,
|
|
|
|
|
normalize_platform_policy_snapshot,
|
|
|
|
|
)
|
|
|
|
|
from virsh_panel.user_isos import (
|
|
|
|
|
MAX_USER_ISO_BYTES, MAX_USER_ISOS, MAX_USER_ISO_TOTAL_BYTES,
|
|
|
|
|
USER_ISO_ID_RE, USER_ISO_VOLUME_RE, verify_user_iso,
|
|
|
|
|
@ -46,8 +51,8 @@ GIB = 1024**3
|
|
|
|
|
PANEL_NS = "urn:virsh-panel:1"
|
|
|
|
|
CLEAN_TRAFFIC_FILTER = "clean-traffic"
|
|
|
|
|
FIREWALL_FILTER_PREFIX = "vc-fw-"
|
|
|
|
|
FIREWALL_FILTER_COMPILER_VERSION = 3
|
|
|
|
|
SUPPORTED_FIREWALL_FILTER_COMPILER_VERSIONS = frozenset({1, 2, 3})
|
|
|
|
|
FIREWALL_FILTER_COMPILER_VERSION = 4
|
|
|
|
|
SUPPORTED_FIREWALL_FILTER_COMPILER_VERSIONS = frozenset({1, 2, 3, 4})
|
|
|
|
|
MAX_FIREWALL_RULES = 50
|
|
|
|
|
NAME_RE = re.compile(r"[A-Za-z0-9][A-Za-z0-9._-]{0,62}\Z")
|
|
|
|
|
INTERNAL_RESOURCE_PREFIX_LENGTHS = (16, 20, 24, 28, 32)
|
|
|
|
|
@ -109,6 +114,142 @@ class VMSnapshotUncertainError(VMError):
|
|
|
|
|
super().__init__(message, 503, error_code="snapshot_result_uncertain")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _platform_policy_from_domain_xml(
|
|
|
|
|
root: ET.Element, *, require_explicit: bool = False,
|
|
|
|
|
) -> dict[str, Any]:
|
|
|
|
|
"""Read one immutable per-VM snapshot; missing means legacy baseline."""
|
|
|
|
|
managed = root.find(f"./metadata/{{{PANEL_NS}}}managed")
|
|
|
|
|
if managed is None:
|
|
|
|
|
raise VMError(
|
|
|
|
|
"虚拟机缺少面板网络策略元数据", 409,
|
|
|
|
|
error_code="firewall_drift_detected",
|
|
|
|
|
)
|
|
|
|
|
nodes = managed.findall(f"{{{PANEL_NS}}}platform-policy")
|
|
|
|
|
if not nodes:
|
|
|
|
|
if require_explicit:
|
|
|
|
|
raise VMError(
|
|
|
|
|
"虚拟机缺少显式平台网络策略元数据", 409,
|
|
|
|
|
error_code="firewall_drift_detected",
|
|
|
|
|
)
|
|
|
|
|
return default_platform_policy_snapshot()
|
|
|
|
|
if len(nodes) != 1 or set(nodes[0].attrib) != {"version", "digest"}:
|
|
|
|
|
raise VMError(
|
|
|
|
|
"虚拟机平台网络策略元数据已损坏", 409,
|
|
|
|
|
error_code="firewall_drift_detected",
|
|
|
|
|
)
|
|
|
|
|
node = nodes[0]
|
|
|
|
|
raw = node.text or ""
|
|
|
|
|
if (node.get("version") != "1"
|
|
|
|
|
or len(raw.encode("utf-8")) > MAX_PLATFORM_POLICY_SNAPSHOT_BYTES):
|
|
|
|
|
raise VMError(
|
|
|
|
|
"虚拟机平台网络策略元数据已损坏", 409,
|
|
|
|
|
error_code="firewall_drift_detected",
|
|
|
|
|
)
|
|
|
|
|
try:
|
|
|
|
|
decoded = json.loads(raw)
|
|
|
|
|
snapshot = normalize_platform_policy_snapshot(decoded)
|
|
|
|
|
except (TypeError, ValueError, UnicodeError, json.JSONDecodeError) as exc:
|
|
|
|
|
raise VMError(
|
|
|
|
|
"虚拟机平台网络策略元数据已损坏", 409,
|
|
|
|
|
error_code="firewall_drift_detected",
|
|
|
|
|
) from exc
|
|
|
|
|
if not hmac.compare_digest(node.get("digest") or "", snapshot["digest"]):
|
|
|
|
|
raise VMError(
|
|
|
|
|
"虚拟机平台网络策略摘要已漂移", 409,
|
|
|
|
|
error_code="firewall_drift_detected",
|
|
|
|
|
)
|
|
|
|
|
return snapshot
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _platform_policy_state_from_domain_xml(
|
|
|
|
|
root: ET.Element,
|
|
|
|
|
) -> tuple[dict[str, Any] | None, bool]:
|
|
|
|
|
"""Return policy evidence for managed domains; unmanaged domains opt out."""
|
|
|
|
|
managed = root.find(f"./metadata/{{{PANEL_NS}}}managed")
|
|
|
|
|
if managed is None:
|
|
|
|
|
return None, False
|
|
|
|
|
explicit = bool(
|
|
|
|
|
managed.findall(f"{{{PANEL_NS}}}platform-policy"))
|
|
|
|
|
return (
|
|
|
|
|
_platform_policy_from_domain_xml(
|
|
|
|
|
root, require_explicit=explicit),
|
|
|
|
|
explicit,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _require_same_platform_policy_state(
|
|
|
|
|
root: ET.Element,
|
|
|
|
|
expected: dict[str, Any] | None,
|
|
|
|
|
*,
|
|
|
|
|
require_explicit: bool,
|
|
|
|
|
message: str,
|
|
|
|
|
) -> None:
|
|
|
|
|
"""Reject a managed policy change between two already-read definitions."""
|
|
|
|
|
observed, observed_explicit = _platform_policy_state_from_domain_xml(root)
|
|
|
|
|
if expected is None:
|
|
|
|
|
if observed is None:
|
|
|
|
|
return
|
|
|
|
|
elif (observed is not None
|
|
|
|
|
and observed_explicit == require_explicit
|
|
|
|
|
and isinstance(observed.get("digest"), str)
|
|
|
|
|
and hmac.compare_digest(
|
|
|
|
|
observed["digest"], expected["digest"])
|
|
|
|
|
and observed == expected):
|
|
|
|
|
return
|
|
|
|
|
raise VMError(
|
|
|
|
|
message, 409,
|
|
|
|
|
error_code="firewall_drift_detected",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _verify_platform_policy_in_domain(
|
|
|
|
|
domain: Any, expected: dict[str, Any], *, require_explicit: bool,
|
|
|
|
|
) -> dict[str, Any]:
|
|
|
|
|
"""Prove a defineXML call retained the intended inactive metadata."""
|
|
|
|
|
try:
|
|
|
|
|
root = ET.fromstring(
|
|
|
|
|
domain.XMLDesc(libvirt.VIR_DOMAIN_XML_INACTIVE))
|
|
|
|
|
observed = _platform_policy_from_domain_xml(
|
|
|
|
|
root, require_explicit=require_explicit)
|
|
|
|
|
except Exception as exc:
|
|
|
|
|
raise VMError(
|
|
|
|
|
"虚拟机平台网络策略写入结果无法确认", 502,
|
|
|
|
|
error_code="firewall_result_uncertain",
|
|
|
|
|
) from exc
|
|
|
|
|
if (not isinstance(observed.get("digest"), str)
|
|
|
|
|
or not hmac.compare_digest(
|
|
|
|
|
observed["digest"], expected["digest"])
|
|
|
|
|
or observed != expected):
|
|
|
|
|
raise VMError(
|
|
|
|
|
"虚拟机平台网络策略写入结果无法确认", 502,
|
|
|
|
|
error_code="firewall_result_uncertain",
|
|
|
|
|
)
|
|
|
|
|
return observed
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _store_platform_policy_in_domain_xml(
|
|
|
|
|
managed: ET.Element, snapshot: Any,
|
|
|
|
|
) -> dict[str, Any]:
|
|
|
|
|
"""Persist one canonical snapshot as namespaced, libvirt-retained text."""
|
|
|
|
|
try:
|
|
|
|
|
normalized = normalize_platform_policy_snapshot(snapshot)
|
|
|
|
|
except ValueError as exc:
|
|
|
|
|
raise VMError(
|
|
|
|
|
"平台网络策略快照无效", 422,
|
|
|
|
|
error_code="firewall_rule_invalid",
|
|
|
|
|
) from exc
|
|
|
|
|
for old in managed.findall(f"{{{PANEL_NS}}}platform-policy"):
|
|
|
|
|
managed.remove(old)
|
|
|
|
|
node = ET.SubElement(
|
|
|
|
|
managed, f"{{{PANEL_NS}}}platform-policy",
|
|
|
|
|
version="1", digest=normalized["digest"],
|
|
|
|
|
)
|
|
|
|
|
node.text = json.dumps(
|
|
|
|
|
normalized, ensure_ascii=False, separators=(",", ":"),
|
|
|
|
|
)
|
|
|
|
|
return normalized
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def firewall_filter_name(vm_uuid: str) -> str:
|
|
|
|
|
"""Return the stable, host-local nwfilter name for one VM UUID."""
|
|
|
|
|
try:
|
|
|
|
|
@ -227,6 +368,7 @@ def firewall_policy_digest(enabled: bool, rules: Any) -> str:
|
|
|
|
|
def build_firewall_filter_xml(
|
|
|
|
|
vm_uuid: str, enabled: bool, rules: Any, *,
|
|
|
|
|
compiler_version: int = FIREWALL_FILTER_COMPILER_VERSION,
|
|
|
|
|
platform_policy_snapshot: Any = None,
|
|
|
|
|
) -> tuple[str, list[dict[str, Any]], str, str]:
|
|
|
|
|
"""Compile a bounded typed policy; callers can never supply raw XML."""
|
|
|
|
|
if (isinstance(compiler_version, bool)
|
|
|
|
|
@ -248,9 +390,28 @@ def build_firewall_filter_xml(
|
|
|
|
|
# public interface, so these are public ingress/egress restrictions.
|
|
|
|
|
priority = 10
|
|
|
|
|
direction_names = {"ingress": "in", "egress": "out"}
|
|
|
|
|
for platform_rule in PLATFORM_DEFAULT_NETWORK_POLICY.rules:
|
|
|
|
|
for direction in platform_rule.directions:
|
|
|
|
|
for protocol in platform_rule.protocols:
|
|
|
|
|
if compiler_version >= 4:
|
|
|
|
|
try:
|
|
|
|
|
platform_rules = normalize_platform_policy_snapshot(
|
|
|
|
|
platform_policy_snapshot
|
|
|
|
|
if platform_policy_snapshot is not None
|
|
|
|
|
else default_platform_policy_snapshot()
|
|
|
|
|
)["rules"]
|
|
|
|
|
except ValueError as exc:
|
|
|
|
|
raise VMError(
|
|
|
|
|
"平台网络策略快照无效", 422,
|
|
|
|
|
error_code="firewall_rule_invalid",
|
|
|
|
|
) from exc
|
|
|
|
|
else:
|
|
|
|
|
platform_rules = [{
|
|
|
|
|
"protocols": platform_rule.protocols,
|
|
|
|
|
"port_from": platform_rule.port_from,
|
|
|
|
|
"port_to": platform_rule.port_to,
|
|
|
|
|
"directions": platform_rule.directions,
|
|
|
|
|
} for platform_rule in PLATFORM_DEFAULT_NETWORK_POLICY.rules]
|
|
|
|
|
for platform_rule in platform_rules:
|
|
|
|
|
for direction in platform_rule["directions"]:
|
|
|
|
|
for protocol in platform_rule["protocols"]:
|
|
|
|
|
rule = ET.SubElement(
|
|
|
|
|
root, "rule", action="drop",
|
|
|
|
|
direction=direction_names[direction],
|
|
|
|
|
@ -258,11 +419,16 @@ def build_firewall_filter_xml(
|
|
|
|
|
)
|
|
|
|
|
ET.SubElement(
|
|
|
|
|
rule, protocol,
|
|
|
|
|
dstportstart=str(platform_rule.port_from),
|
|
|
|
|
dstportend=str(platform_rule.port_to),
|
|
|
|
|
dstportstart=str(platform_rule["port_from"]),
|
|
|
|
|
dstportend=str(platform_rule["port_to"]),
|
|
|
|
|
state="NONE",
|
|
|
|
|
)
|
|
|
|
|
priority += 1
|
|
|
|
|
if priority > 80:
|
|
|
|
|
raise VMError(
|
|
|
|
|
"平台网络策略超过过滤器优先级预算", 422,
|
|
|
|
|
error_code="firewall_rule_invalid",
|
|
|
|
|
)
|
|
|
|
|
# The product does not provision guest IPv6. Enforce that boundary in
|
|
|
|
|
# this wrapper as well as in an enabled tenant firewall, otherwise the
|
|
|
|
|
# IPv4 platform deny list could be bypassed if a bridge later starts
|
|
|
|
|
@ -452,7 +618,7 @@ class VMService:
|
|
|
|
|
interface, public_ip_cidr, CLEAN_TRAFFIC_FILTER)
|
|
|
|
|
|
|
|
|
|
def _ensure_default_firewall_filter(
|
|
|
|
|
self, conn: Any, vm_uuid: str,
|
|
|
|
|
self, conn: Any, vm_uuid: str, platform_policy_snapshot: Any,
|
|
|
|
|
) -> tuple[str, str, bool]:
|
|
|
|
|
"""Define the immutable platform wrapper before a new guest can boot.
|
|
|
|
|
|
|
|
|
|
@ -461,7 +627,10 @@ class VMService:
|
|
|
|
|
ours for rollback cleanup.
|
|
|
|
|
"""
|
|
|
|
|
expected_xml, _rules, _policy_digest, expected_digest = (
|
|
|
|
|
build_firewall_filter_xml(vm_uuid, False, []))
|
|
|
|
|
build_firewall_filter_xml(
|
|
|
|
|
vm_uuid, False, [],
|
|
|
|
|
platform_policy_snapshot=platform_policy_snapshot,
|
|
|
|
|
))
|
|
|
|
|
expected_name = firewall_filter_name(vm_uuid)
|
|
|
|
|
existing = self._lookup_filter(conn, expected_name)
|
|
|
|
|
if existing is not None:
|
|
|
|
|
@ -2327,6 +2496,9 @@ class VMService:
|
|
|
|
|
current_xml = self._rescue_vm_preflight(domain)
|
|
|
|
|
if not self._rescue_xml_matches(current_xml, original_xml):
|
|
|
|
|
raise VMError("虚拟机配置已变化,请重新提交救援操作", 409)
|
|
|
|
|
original_root = ET.fromstring(original_xml)
|
|
|
|
|
platform_policy, platform_policy_was_explicit = (
|
|
|
|
|
_platform_policy_state_from_domain_xml(original_root))
|
|
|
|
|
pool, volume = self._rescue_volume(conn, rescue_volume_name)
|
|
|
|
|
original_autostart = bool(domain.autostart())
|
|
|
|
|
rescue_xml = self._rescue_xml(
|
|
|
|
|
@ -2341,6 +2513,11 @@ class VMService:
|
|
|
|
|
if rescue_domain is None:
|
|
|
|
|
raise VMError("宿主机未能保存救援启动配置", 502)
|
|
|
|
|
switched = True
|
|
|
|
|
if platform_policy is not None:
|
|
|
|
|
_verify_platform_policy_in_domain(
|
|
|
|
|
rescue_domain, platform_policy,
|
|
|
|
|
require_explicit=platform_policy_was_explicit,
|
|
|
|
|
)
|
|
|
|
|
self._restore_rescue_autostart(rescue_domain, original_autostart)
|
|
|
|
|
staged_xml = rescue_domain.XMLDesc(libvirt.VIR_DOMAIN_XML_INACTIVE)
|
|
|
|
|
except Exception as exc:
|
|
|
|
|
@ -2350,6 +2527,11 @@ class VMService:
|
|
|
|
|
restored_domain = conn.defineXML(original_xml)
|
|
|
|
|
if restored_domain is None:
|
|
|
|
|
raise VMError("宿主机未能恢复原虚拟机配置", 502)
|
|
|
|
|
if platform_policy is not None:
|
|
|
|
|
_verify_platform_policy_in_domain(
|
|
|
|
|
restored_domain, platform_policy,
|
|
|
|
|
require_explicit=platform_policy_was_explicit,
|
|
|
|
|
)
|
|
|
|
|
self._restore_rescue_autostart(restored_domain, original_autostart)
|
|
|
|
|
except Exception:
|
|
|
|
|
LOG.exception("Could not restore original VM XML for %s", vm_uuid)
|
|
|
|
|
@ -2402,6 +2584,11 @@ class VMService:
|
|
|
|
|
restored_domain = conn.defineXML(original_xml)
|
|
|
|
|
if restored_domain is None:
|
|
|
|
|
raise VMError("宿主机未能恢复原虚拟机配置", 502)
|
|
|
|
|
if platform_policy is not None:
|
|
|
|
|
_verify_platform_policy_in_domain(
|
|
|
|
|
restored_domain, platform_policy,
|
|
|
|
|
require_explicit=platform_policy_was_explicit,
|
|
|
|
|
)
|
|
|
|
|
self._restore_rescue_autostart(restored_domain, original_autostart)
|
|
|
|
|
restart_domain = restored_domain
|
|
|
|
|
except Exception:
|
|
|
|
|
@ -2521,6 +2708,8 @@ class VMService:
|
|
|
|
|
if (original_root.findtext("uuid") != vm_uuid
|
|
|
|
|
or original_root.find(f"./metadata/{{{PANEL_NS}}}rescue") is not None):
|
|
|
|
|
raise VMError("原虚拟机配置备份与目标虚拟机不匹配", 409)
|
|
|
|
|
platform_policy, platform_policy_was_explicit = (
|
|
|
|
|
_platform_policy_state_from_domain_xml(original_root))
|
|
|
|
|
target_link_state = "down" if network_suspended else "up"
|
|
|
|
|
for interface in original_root.findall("./devices/interface"):
|
|
|
|
|
link = interface.find("link")
|
|
|
|
|
@ -2533,6 +2722,11 @@ class VMService:
|
|
|
|
|
rescue_root = ET.fromstring(rescue_xml)
|
|
|
|
|
except ET.ParseError as exc:
|
|
|
|
|
raise VMError("救援虚拟机配置 XML 无效", 502) from exc
|
|
|
|
|
_require_same_platform_policy_state(
|
|
|
|
|
rescue_root, platform_policy,
|
|
|
|
|
require_explicit=platform_policy_was_explicit,
|
|
|
|
|
message="救援虚拟机平台网络策略已变化,已停止恢复",
|
|
|
|
|
)
|
|
|
|
|
marker = rescue_root.find(f"./metadata/{{{PANEL_NS}}}rescue")
|
|
|
|
|
if (marker is None or marker.get("original-sha256") !=
|
|
|
|
|
self._rescue_original_hash(original_xml)
|
|
|
|
|
@ -2562,6 +2756,11 @@ class VMService:
|
|
|
|
|
if normal_domain is None:
|
|
|
|
|
raise VMError("宿主机未能恢复原虚拟机配置", 502)
|
|
|
|
|
switched = True
|
|
|
|
|
if platform_policy is not None:
|
|
|
|
|
_verify_platform_policy_in_domain(
|
|
|
|
|
normal_domain, platform_policy,
|
|
|
|
|
require_explicit=platform_policy_was_explicit,
|
|
|
|
|
)
|
|
|
|
|
self._restore_rescue_autostart(normal_domain, original_autostart)
|
|
|
|
|
except Exception as exc:
|
|
|
|
|
if switched:
|
|
|
|
|
@ -2569,6 +2768,11 @@ class VMService:
|
|
|
|
|
rescue_domain = conn.defineXML(rescue_xml)
|
|
|
|
|
if rescue_domain is None:
|
|
|
|
|
raise VMError("宿主机未能恢复救援启动配置", 502)
|
|
|
|
|
if platform_policy is not None:
|
|
|
|
|
_verify_platform_policy_in_domain(
|
|
|
|
|
rescue_domain, platform_policy,
|
|
|
|
|
require_explicit=platform_policy_was_explicit,
|
|
|
|
|
)
|
|
|
|
|
self._restore_rescue_autostart(
|
|
|
|
|
rescue_domain, rescue_autostart)
|
|
|
|
|
except Exception:
|
|
|
|
|
@ -2594,6 +2798,11 @@ class VMService:
|
|
|
|
|
if normal_domain is None:
|
|
|
|
|
raise VMError("宿主机未能恢复原虚拟机配置", 502)
|
|
|
|
|
switched = True
|
|
|
|
|
if platform_policy is not None:
|
|
|
|
|
_verify_platform_policy_in_domain(
|
|
|
|
|
normal_domain, platform_policy,
|
|
|
|
|
require_explicit=platform_policy_was_explicit,
|
|
|
|
|
)
|
|
|
|
|
self._restore_rescue_autostart(normal_domain, original_autostart)
|
|
|
|
|
normal_domain.create()
|
|
|
|
|
except Exception as exc:
|
|
|
|
|
@ -2612,6 +2821,11 @@ class VMService:
|
|
|
|
|
restored_domain = conn.defineXML(rescue_xml)
|
|
|
|
|
if restored_domain is None:
|
|
|
|
|
raise VMError("宿主机未能恢复救援启动配置", 502)
|
|
|
|
|
if platform_policy is not None:
|
|
|
|
|
_verify_platform_policy_in_domain(
|
|
|
|
|
restored_domain, platform_policy,
|
|
|
|
|
require_explicit=platform_policy_was_explicit,
|
|
|
|
|
)
|
|
|
|
|
self._restore_rescue_autostart(restored_domain, rescue_autostart)
|
|
|
|
|
restart_domain = restored_domain
|
|
|
|
|
except Exception:
|
|
|
|
|
@ -2687,6 +2901,9 @@ class VMService:
|
|
|
|
|
managed.set("os-source", "cloud_template")
|
|
|
|
|
if spec.get("os_name") is not None:
|
|
|
|
|
managed.set("os-name", spec["os_name"])
|
|
|
|
|
_store_platform_policy_in_domain_xml(
|
|
|
|
|
managed, spec.get("platform_network_policy_snapshot")
|
|
|
|
|
or default_platform_policy_snapshot())
|
|
|
|
|
if self.bridge_name is not None:
|
|
|
|
|
self._attach_public_filter(
|
|
|
|
|
interface, public_ip_cidr, firewall_filter_name(vm_uuid))
|
|
|
|
|
@ -2697,6 +2914,18 @@ class VMService:
|
|
|
|
|
validated = self._validate_common_create(spec)
|
|
|
|
|
validated["created_at"] = datetime.now(timezone.utc).isoformat(timespec="seconds").replace("+00:00", "Z")
|
|
|
|
|
validated["os_name"] = self._validated_os_name(spec.get("os_name"))
|
|
|
|
|
try:
|
|
|
|
|
validated["platform_network_policy_snapshot"] = (
|
|
|
|
|
normalize_platform_policy_snapshot(
|
|
|
|
|
spec.get("platform_network_policy_snapshot")
|
|
|
|
|
if "platform_network_policy_snapshot" in spec
|
|
|
|
|
else default_platform_policy_snapshot()
|
|
|
|
|
))
|
|
|
|
|
except ValueError as exc:
|
|
|
|
|
raise VMError(
|
|
|
|
|
"平台网络策略快照无效", 422,
|
|
|
|
|
error_code="firewall_rule_invalid",
|
|
|
|
|
) from exc
|
|
|
|
|
requested_uuid = spec.get("vm_uuid")
|
|
|
|
|
if requested_uuid is not None:
|
|
|
|
|
try:
|
|
|
|
|
@ -2786,7 +3015,8 @@ class VMService:
|
|
|
|
|
try:
|
|
|
|
|
(default_filter_xml, default_filter_digest,
|
|
|
|
|
created_filter) = self._ensure_default_firewall_filter(
|
|
|
|
|
conn, vm_uuid)
|
|
|
|
|
conn, vm_uuid,
|
|
|
|
|
validated["platform_network_policy_snapshot"])
|
|
|
|
|
disk = pool.createXMLFrom(self._volume_xml_bytes(
|
|
|
|
|
disk_name, template_capacity, "qcow2"), template, 0)
|
|
|
|
|
if disk is None:
|
|
|
|
|
@ -2808,11 +3038,23 @@ class VMService:
|
|
|
|
|
domain = conn.defineXML(domain_xml)
|
|
|
|
|
if domain is None:
|
|
|
|
|
raise VMError("宿主机未能定义云镜像虚拟机", 502)
|
|
|
|
|
_verify_platform_policy_in_domain(
|
|
|
|
|
domain, validated["platform_network_policy_snapshot"],
|
|
|
|
|
require_explicit=True,
|
|
|
|
|
)
|
|
|
|
|
self._enable_vm_autostart(domain)
|
|
|
|
|
domain.create()
|
|
|
|
|
evidence = self._firewall_observation(
|
|
|
|
|
conn, domain, default_filter_xml)
|
|
|
|
|
if not evidence["matches"]:
|
|
|
|
|
expected_platform_policy = (
|
|
|
|
|
validated["platform_network_policy_snapshot"])
|
|
|
|
|
if (not evidence["matches"]
|
|
|
|
|
or evidence["platform_policy_snapshot"]
|
|
|
|
|
!= expected_platform_policy
|
|
|
|
|
or not hmac.compare_digest(
|
|
|
|
|
evidence["platform_policy_digest"],
|
|
|
|
|
expected_platform_policy["digest"],
|
|
|
|
|
)):
|
|
|
|
|
raise VMError(
|
|
|
|
|
"实例已启动但平台默认网络策略校验失败", 502,
|
|
|
|
|
error_code="firewall_result_uncertain",
|
|
|
|
|
@ -2876,6 +3118,15 @@ class VMService:
|
|
|
|
|
if domain.snapshotNum(0) > 0:
|
|
|
|
|
raise VMError("虚拟机存在快照,请先处理后再重装", 409)
|
|
|
|
|
original_root = self._vm_xml(domain)
|
|
|
|
|
preflight_root = ET.fromstring(
|
|
|
|
|
domain.XMLDesc(libvirt.VIR_DOMAIN_XML_INACTIVE))
|
|
|
|
|
platform_policy, platform_policy_was_explicit = (
|
|
|
|
|
_platform_policy_state_from_domain_xml(preflight_root))
|
|
|
|
|
_require_same_platform_policy_state(
|
|
|
|
|
original_root, platform_policy,
|
|
|
|
|
require_explicit=platform_policy_was_explicit,
|
|
|
|
|
message="虚拟机实时与持久平台网络策略不一致,已停止重装",
|
|
|
|
|
)
|
|
|
|
|
managed = original_root.find(f"./metadata/{{{PANEL_NS}}}managed")
|
|
|
|
|
if (managed is None or managed.get("version") != "2"
|
|
|
|
|
or not managed.get("seed-volume")
|
|
|
|
|
@ -2939,6 +3190,11 @@ class VMService:
|
|
|
|
|
old_xml = domain.XMLDesc(libvirt.VIR_DOMAIN_XML_INACTIVE)
|
|
|
|
|
inactive_root = ET.fromstring(old_xml)
|
|
|
|
|
inactive_managed = inactive_root.find(f"./metadata/{{{PANEL_NS}}}managed")
|
|
|
|
|
_require_same_platform_policy_state(
|
|
|
|
|
inactive_root, platform_policy,
|
|
|
|
|
require_explicit=platform_policy_was_explicit,
|
|
|
|
|
message="关机后虚拟机平台网络策略已变化,重装已停止",
|
|
|
|
|
)
|
|
|
|
|
inactive_interface = inactive_root.find("./devices/interface")
|
|
|
|
|
inactive_mac = (inactive_interface.find("mac") if inactive_interface is not None
|
|
|
|
|
else None)
|
|
|
|
|
@ -3018,6 +3274,10 @@ class VMService:
|
|
|
|
|
if new_domain is None:
|
|
|
|
|
raise VMError("宿主机未能切换虚拟机定义", 502)
|
|
|
|
|
switched = True
|
|
|
|
|
_verify_platform_policy_in_domain(
|
|
|
|
|
new_domain, platform_policy,
|
|
|
|
|
require_explicit=platform_policy_was_explicit,
|
|
|
|
|
)
|
|
|
|
|
new_domain.create()
|
|
|
|
|
except Exception as exc:
|
|
|
|
|
if switched:
|
|
|
|
|
@ -3046,7 +3306,14 @@ class VMService:
|
|
|
|
|
restored = True
|
|
|
|
|
if switched:
|
|
|
|
|
try:
|
|
|
|
|
conn.defineXML(old_xml)
|
|
|
|
|
restored_domain = conn.defineXML(old_xml)
|
|
|
|
|
if restored_domain is None:
|
|
|
|
|
raise VMError("宿主机未能恢复原虚拟机定义", 502)
|
|
|
|
|
if platform_policy is not None:
|
|
|
|
|
_verify_platform_policy_in_domain(
|
|
|
|
|
restored_domain, platform_policy,
|
|
|
|
|
require_explicit=platform_policy_was_explicit,
|
|
|
|
|
)
|
|
|
|
|
except Exception:
|
|
|
|
|
LOG.exception("Could not restore original definition for %s", vm_id)
|
|
|
|
|
restored = False
|
|
|
|
|
@ -3156,6 +3423,15 @@ class VMService:
|
|
|
|
|
if domain.snapshotNum(0) > 0:
|
|
|
|
|
raise VMError("虚拟机存在快照,请先处理后再修改网络", 409)
|
|
|
|
|
original_root = self._vm_xml(domain)
|
|
|
|
|
preflight_root = ET.fromstring(
|
|
|
|
|
domain.XMLDesc(libvirt.VIR_DOMAIN_XML_INACTIVE))
|
|
|
|
|
platform_policy, platform_policy_was_explicit = (
|
|
|
|
|
_platform_policy_state_from_domain_xml(preflight_root))
|
|
|
|
|
_require_same_platform_policy_state(
|
|
|
|
|
original_root, platform_policy,
|
|
|
|
|
require_explicit=platform_policy_was_explicit,
|
|
|
|
|
message="虚拟机实时与持久平台网络策略不一致,已停止网络修改",
|
|
|
|
|
)
|
|
|
|
|
managed = original_root.find(f"./metadata/{{{PANEL_NS}}}managed")
|
|
|
|
|
if (managed is None or managed.get("version") != "2"
|
|
|
|
|
or managed.get("os-source") != "cloud_template"
|
|
|
|
|
@ -3247,6 +3523,11 @@ class VMService:
|
|
|
|
|
old_xml = domain.XMLDesc(libvirt.VIR_DOMAIN_XML_INACTIVE)
|
|
|
|
|
inactive_root = ET.fromstring(old_xml)
|
|
|
|
|
inactive_managed = inactive_root.find(f"./metadata/{{{PANEL_NS}}}managed")
|
|
|
|
|
_require_same_platform_policy_state(
|
|
|
|
|
inactive_root, platform_policy,
|
|
|
|
|
require_explicit=platform_policy_was_explicit,
|
|
|
|
|
message="关机后虚拟机平台网络策略已变化,网络修改已停止",
|
|
|
|
|
)
|
|
|
|
|
inactive_interface = inactive_root.find("./devices/interface")
|
|
|
|
|
inactive_mac = (inactive_interface.find("mac")
|
|
|
|
|
if inactive_interface is not None else None)
|
|
|
|
|
@ -3290,6 +3571,10 @@ class VMService:
|
|
|
|
|
if new_domain is None:
|
|
|
|
|
raise VMError("宿主机未能保存新的虚拟机网络配置", 502)
|
|
|
|
|
switched = True
|
|
|
|
|
_verify_platform_policy_in_domain(
|
|
|
|
|
new_domain, platform_policy,
|
|
|
|
|
require_explicit=platform_policy_was_explicit,
|
|
|
|
|
)
|
|
|
|
|
if was_running:
|
|
|
|
|
phase("starting")
|
|
|
|
|
try:
|
|
|
|
|
@ -3320,6 +3605,11 @@ class VMService:
|
|
|
|
|
restored_domain = conn.defineXML(old_xml)
|
|
|
|
|
if restored_domain is None:
|
|
|
|
|
restored = False
|
|
|
|
|
elif platform_policy is not None:
|
|
|
|
|
_verify_platform_policy_in_domain(
|
|
|
|
|
restored_domain, platform_policy,
|
|
|
|
|
require_explicit=platform_policy_was_explicit,
|
|
|
|
|
)
|
|
|
|
|
except Exception:
|
|
|
|
|
LOG.exception("Could not restore original VM XML for %s", vm_id)
|
|
|
|
|
restored = False
|
|
|
|
|
@ -3400,6 +3690,15 @@ class VMService:
|
|
|
|
|
raise VMError("虚拟机存在快照,请先处理后再修改设置", 409)
|
|
|
|
|
was_running = bool(domain.isActive())
|
|
|
|
|
original_root = self._vm_xml(domain)
|
|
|
|
|
preflight_root = ET.fromstring(
|
|
|
|
|
domain.XMLDesc(libvirt.VIR_DOMAIN_XML_INACTIVE))
|
|
|
|
|
platform_policy, platform_policy_was_explicit = (
|
|
|
|
|
_platform_policy_state_from_domain_xml(preflight_root))
|
|
|
|
|
_require_same_platform_policy_state(
|
|
|
|
|
original_root, platform_policy,
|
|
|
|
|
require_explicit=platform_policy_was_explicit,
|
|
|
|
|
message="虚拟机实时与持久平台网络策略不一致,已停止修改设置",
|
|
|
|
|
)
|
|
|
|
|
managed = original_root.find(f"./metadata/{{{PANEL_NS}}}managed")
|
|
|
|
|
if (managed is None or managed.get("version") != "2"
|
|
|
|
|
or managed.get("os-source") != "cloud_template"
|
|
|
|
|
@ -3475,6 +3774,11 @@ class VMService:
|
|
|
|
|
old_xml = domain.XMLDesc(libvirt.VIR_DOMAIN_XML_INACTIVE)
|
|
|
|
|
inactive_root = ET.fromstring(old_xml)
|
|
|
|
|
inactive_managed = inactive_root.find(f"./metadata/{{{PANEL_NS}}}managed")
|
|
|
|
|
_require_same_platform_policy_state(
|
|
|
|
|
inactive_root, platform_policy,
|
|
|
|
|
require_explicit=platform_policy_was_explicit,
|
|
|
|
|
message="关机后虚拟机平台网络策略已变化,设置修改已停止",
|
|
|
|
|
)
|
|
|
|
|
if (inactive_managed is None
|
|
|
|
|
or inactive_managed.get("seed-volume") != old_seed.name()
|
|
|
|
|
or inactive_managed.get("public-ip-cidr") != managed.get("public-ip-cidr")):
|
|
|
|
|
@ -3503,6 +3807,11 @@ class VMService:
|
|
|
|
|
if new_domain is None:
|
|
|
|
|
raise VMError("宿主机未能保存新的虚拟机主机名", 502)
|
|
|
|
|
switched = True
|
|
|
|
|
if platform_policy is not None:
|
|
|
|
|
_verify_platform_policy_in_domain(
|
|
|
|
|
new_domain, platform_policy,
|
|
|
|
|
require_explicit=platform_policy_was_explicit,
|
|
|
|
|
)
|
|
|
|
|
if was_running:
|
|
|
|
|
phase("starting")
|
|
|
|
|
start_attempted = True
|
|
|
|
|
@ -3549,6 +3858,11 @@ class VMService:
|
|
|
|
|
restored_domain = conn.defineXML(old_xml)
|
|
|
|
|
if restored_domain is None:
|
|
|
|
|
restored = False
|
|
|
|
|
elif platform_policy is not None:
|
|
|
|
|
_verify_platform_policy_in_domain(
|
|
|
|
|
restored_domain, platform_policy,
|
|
|
|
|
require_explicit=platform_policy_was_explicit,
|
|
|
|
|
)
|
|
|
|
|
except Exception:
|
|
|
|
|
LOG.exception("Could not restore original VM XML for %s", vm_id)
|
|
|
|
|
restored = False
|
|
|
|
|
@ -3941,18 +4255,45 @@ class VMService:
|
|
|
|
|
expected_name = firewall_filter_name(vm_uuid)
|
|
|
|
|
inactive = ET.fromstring(
|
|
|
|
|
domain.XMLDesc(libvirt.VIR_DOMAIN_XML_INACTIVE))
|
|
|
|
|
inactive_managed = inactive.find(
|
|
|
|
|
f"./metadata/{{{PANEL_NS}}}managed")
|
|
|
|
|
inactive_policy_explicit = bool(
|
|
|
|
|
inactive_managed is not None
|
|
|
|
|
and inactive_managed.findall(f"{{{PANEL_NS}}}platform-policy")
|
|
|
|
|
)
|
|
|
|
|
platform_policy = _platform_policy_from_domain_xml(inactive)
|
|
|
|
|
_inactive_interface, inactive_ip, inactive_filter = self._firewall_interface(
|
|
|
|
|
inactive, vm_uuid)
|
|
|
|
|
live_filter = inactive_filter
|
|
|
|
|
live_ip = inactive_ip
|
|
|
|
|
if domain.isActive():
|
|
|
|
|
live = ET.fromstring(domain.XMLDesc(0))
|
|
|
|
|
live_managed = live.find(f"./metadata/{{{PANEL_NS}}}managed")
|
|
|
|
|
live_policy_explicit = bool(
|
|
|
|
|
live_managed is not None
|
|
|
|
|
and live_managed.findall(f"{{{PANEL_NS}}}platform-policy")
|
|
|
|
|
)
|
|
|
|
|
live_platform_policy = _platform_policy_from_domain_xml(live)
|
|
|
|
|
if (inactive_policy_explicit != live_policy_explicit
|
|
|
|
|
or not hmac.compare_digest(
|
|
|
|
|
platform_policy["digest"],
|
|
|
|
|
live_platform_policy["digest"])
|
|
|
|
|
or platform_policy != live_platform_policy):
|
|
|
|
|
raise VMError(
|
|
|
|
|
"虚拟机实时与持久平台网络策略不一致", 409,
|
|
|
|
|
error_code="firewall_drift_detected",
|
|
|
|
|
)
|
|
|
|
|
_live_interface, live_ip, live_filter = self._firewall_interface(
|
|
|
|
|
live, vm_uuid)
|
|
|
|
|
policy_evidence = {
|
|
|
|
|
"platform_policy_snapshot": platform_policy,
|
|
|
|
|
"platform_policy_digest": platform_policy["digest"],
|
|
|
|
|
}
|
|
|
|
|
coherent_refs = (
|
|
|
|
|
inactive_filter == live_filter and inactive_ip == live_ip)
|
|
|
|
|
if inactive_filter == CLEAN_TRAFFIC_FILTER:
|
|
|
|
|
return {
|
|
|
|
|
**policy_evidence,
|
|
|
|
|
"enabled": False,
|
|
|
|
|
"filter_mode": "baseline",
|
|
|
|
|
"filter_name": CLEAN_TRAFFIC_FILTER,
|
|
|
|
|
@ -3963,6 +4304,7 @@ class VMService:
|
|
|
|
|
}
|
|
|
|
|
if inactive_filter != expected_name:
|
|
|
|
|
return {
|
|
|
|
|
**policy_evidence,
|
|
|
|
|
"enabled": None,
|
|
|
|
|
"filter_mode": "unknown",
|
|
|
|
|
"filter_name": inactive_filter,
|
|
|
|
|
@ -3974,6 +4316,7 @@ class VMService:
|
|
|
|
|
filter_object = self._lookup_filter(conn, expected_name)
|
|
|
|
|
if filter_object is None or not callable(getattr(filter_object, "XMLDesc", None)):
|
|
|
|
|
return {
|
|
|
|
|
**policy_evidence,
|
|
|
|
|
"enabled": None,
|
|
|
|
|
"filter_mode": "managed",
|
|
|
|
|
"filter_name": expected_name,
|
|
|
|
|
@ -3996,6 +4339,7 @@ class VMService:
|
|
|
|
|
matches = (coherent_refs and expected_filter_digest is not None
|
|
|
|
|
and actual_digest == expected_filter_digest)
|
|
|
|
|
return {
|
|
|
|
|
**policy_evidence,
|
|
|
|
|
"enabled": enabled,
|
|
|
|
|
"filter_mode": "managed",
|
|
|
|
|
"filter_name": expected_name,
|
|
|
|
|
@ -4019,11 +4363,15 @@ class VMService:
|
|
|
|
|
compiler_version: int = FIREWALL_FILTER_COMPILER_VERSION,
|
|
|
|
|
) -> dict[str, Any]:
|
|
|
|
|
"""Compare exact host evidence with one target policy; never mutate."""
|
|
|
|
|
(expected_xml, normalized, policy_digest,
|
|
|
|
|
expected_filter_digest) = build_firewall_filter_xml(
|
|
|
|
|
vm_id, enabled, rules, compiler_version=compiler_version)
|
|
|
|
|
with self._connect() as conn:
|
|
|
|
|
domain = self._domain(conn, vm_id)
|
|
|
|
|
inactive_root = ET.fromstring(
|
|
|
|
|
domain.XMLDesc(libvirt.VIR_DOMAIN_XML_INACTIVE))
|
|
|
|
|
platform_policy = _platform_policy_from_domain_xml(inactive_root)
|
|
|
|
|
(expected_xml, normalized, policy_digest,
|
|
|
|
|
expected_filter_digest) = build_firewall_filter_xml(
|
|
|
|
|
vm_id, enabled, rules, compiler_version=compiler_version,
|
|
|
|
|
platform_policy_snapshot=platform_policy)
|
|
|
|
|
observation = self._firewall_observation(
|
|
|
|
|
conn, domain, expected_xml)
|
|
|
|
|
return {
|
|
|
|
|
@ -4037,9 +4385,6 @@ class VMService:
|
|
|
|
|
self, vm_id: str, *, enabled: bool, rules: Any,
|
|
|
|
|
) -> dict[str, Any]:
|
|
|
|
|
"""Atomically converge one VM's typed ingress policy where possible."""
|
|
|
|
|
(expected_xml, normalized, policy_digest,
|
|
|
|
|
expected_filter_digest) = build_firewall_filter_xml(
|
|
|
|
|
vm_id, enabled, rules)
|
|
|
|
|
expected_name = firewall_filter_name(vm_id)
|
|
|
|
|
with self._connect() as conn:
|
|
|
|
|
self._require_clean_traffic_filter(conn)
|
|
|
|
|
@ -4053,6 +4398,11 @@ class VMService:
|
|
|
|
|
error_code="firewall_capability_missing")
|
|
|
|
|
inactive_root = ET.fromstring(
|
|
|
|
|
domain.XMLDesc(libvirt.VIR_DOMAIN_XML_INACTIVE))
|
|
|
|
|
platform_policy = _platform_policy_from_domain_xml(inactive_root)
|
|
|
|
|
(expected_xml, normalized, policy_digest,
|
|
|
|
|
expected_filter_digest) = build_firewall_filter_xml(
|
|
|
|
|
vm_id, enabled, rules,
|
|
|
|
|
platform_policy_snapshot=platform_policy)
|
|
|
|
|
inactive_interface, public_ip, old_filter_name = \
|
|
|
|
|
self._firewall_interface(inactive_root, domain.UUIDString())
|
|
|
|
|
old_inactive_xml = ET.tostring(
|
|
|
|
|
@ -5116,6 +5466,9 @@ class VMService:
|
|
|
|
|
was_running = bool(domain.isActive())
|
|
|
|
|
old_xml = domain.XMLDesc(
|
|
|
|
|
libvirt.VIR_DOMAIN_XML_INACTIVE if domain.isPersistent() else 0)
|
|
|
|
|
old_root = ET.fromstring(old_xml)
|
|
|
|
|
platform_policy, platform_policy_was_explicit = (
|
|
|
|
|
_platform_policy_state_from_domain_xml(old_root))
|
|
|
|
|
old_name = old_disk.name()
|
|
|
|
|
if expected_old_volume is not None and old_name != expected_old_volume:
|
|
|
|
|
raise VMSnapshotUncertainError(
|
|
|
|
|
@ -5159,6 +5512,11 @@ class VMService:
|
|
|
|
|
time.sleep(SNAPSHOT_RESTORE_SHUTDOWN_POLL_INTERVAL)
|
|
|
|
|
inactive_xml = domain.XMLDesc(libvirt.VIR_DOMAIN_XML_INACTIVE)
|
|
|
|
|
inactive_root = ET.fromstring(inactive_xml)
|
|
|
|
|
_require_same_platform_policy_state(
|
|
|
|
|
inactive_root, platform_policy,
|
|
|
|
|
require_explicit=platform_policy_was_explicit,
|
|
|
|
|
message="关机后虚拟机平台网络策略已变化,快照恢复已停止",
|
|
|
|
|
)
|
|
|
|
|
inactive_managed = inactive_root.find(
|
|
|
|
|
f"./metadata/{{{PANEL_NS}}}managed")
|
|
|
|
|
if inactive_managed is None or inactive_managed.get("disk-volume") != old_name:
|
|
|
|
|
@ -5209,6 +5567,11 @@ class VMService:
|
|
|
|
|
else:
|
|
|
|
|
raise VMSnapshotUncertainError(
|
|
|
|
|
"恢复系统盘切换结果不确定,已保留新旧磁盘") from exc
|
|
|
|
|
if platform_policy is not None:
|
|
|
|
|
_verify_platform_policy_in_domain(
|
|
|
|
|
new_domain, platform_policy,
|
|
|
|
|
require_explicit=platform_policy_was_explicit,
|
|
|
|
|
)
|
|
|
|
|
warning = None
|
|
|
|
|
if was_running:
|
|
|
|
|
if progress is not None:
|
|
|
|
|
@ -5279,6 +5642,11 @@ class VMService:
|
|
|
|
|
if restored_domain is None:
|
|
|
|
|
restored = False
|
|
|
|
|
else:
|
|
|
|
|
if platform_policy is not None:
|
|
|
|
|
_verify_platform_policy_in_domain(
|
|
|
|
|
restored_domain, platform_policy,
|
|
|
|
|
require_explicit=platform_policy_was_explicit,
|
|
|
|
|
)
|
|
|
|
|
restored_root = ET.fromstring(restored_domain.XMLDesc(
|
|
|
|
|
libvirt.VIR_DOMAIN_XML_INACTIVE))
|
|
|
|
|
restored_managed = restored_root.find(
|
|
|
|
|
|