Fix Device Core mount-order fingerprints with exact r5 recovery
This commit is contained in:
@@ -11,6 +11,7 @@ import io
|
||||
import itertools
|
||||
import json
|
||||
import os
|
||||
import posixpath
|
||||
import pwd
|
||||
import re
|
||||
import secrets
|
||||
@@ -4979,7 +4980,7 @@ def allowed_payload_path(component, rel):
|
||||
return True
|
||||
die("Device Core drift audit accepts only its exact descriptor")
|
||||
if component == "device-core-release-reconciliation":
|
||||
if rel in ("deployment", DEVICE_RELEASE_RECONCILIATION_ENTRY):
|
||||
if rel in ("deployment", DEVICE_RELEASE_RECONCILIATION_ENTRY, DEVICE_RELEASE_RECONCILIATION_V2_ENTRY):
|
||||
return True
|
||||
die("Device Core reconciliation accepts only its exact descriptor")
|
||||
if component == "device-core-release":
|
||||
@@ -22457,7 +22458,7 @@ def component_services(component, entries=None):
|
||||
die("Device Core drift audit requires the exact descriptor entry")
|
||||
return ()
|
||||
if component == "device-core-release-reconciliation":
|
||||
if tuple(entries or ()) != (DEVICE_RELEASE_RECONCILIATION_ENTRY,):
|
||||
if tuple(entries or ()) not in ((DEVICE_RELEASE_RECONCILIATION_ENTRY,), (DEVICE_RELEASE_RECONCILIATION_V2_ENTRY,)):
|
||||
die("Device Core reconciliation requires the exact descriptor entry")
|
||||
return ()
|
||||
if component == "device-core-release":
|
||||
@@ -32505,6 +32506,8 @@ DEVICE_RELEASE_R5_SHA = "b8dd1384f499d1173ce7d95dc1f2433a404290f443bd612816ad913
|
||||
DEVICE_RELEASE_R5_BACKUP = "device-core-release-" + DEVICE_RELEASE_R5_ID + "-20260929-190019"
|
||||
DEVICE_RELEASE_RECONCILIATION_ID = "device-core-release-r5-reconciliation-v1-20260929-001"
|
||||
DEVICE_RELEASE_RECONCILIATION_ENTRY = "deployment/device-core-release-r5-reconciliation-v1.json"
|
||||
DEVICE_RELEASE_RECONCILIATION_V2_ID = "device-core-release-r5-reconciliation-v2-20260929-002"
|
||||
DEVICE_RELEASE_RECONCILIATION_V2_ENTRY = "deployment/device-core-release-r5-reconciliation-v2.json"
|
||||
DEVICE_RELEASE_R5_BACKUP_HASHES = {
|
||||
"device-release-before.json": "0e30ebd1a1e511da5e8026afbba9b761c5edb2fcc151b23ecc79bbae8af0b739",
|
||||
"source-before.tgz": "d597d1cd1dcb1eeaf79836c40523258ae1a9aa1b6e3333a94238f40f078accb5",
|
||||
@@ -32557,7 +32560,20 @@ def device_release_reconciliation_descriptor():
|
||||
"backupId": DEVICE_RELEASE_R5_BACKUP, "mode": "prove-restored-baseline-only"}
|
||||
|
||||
|
||||
def device_release_reconciliation_v2_descriptor():
|
||||
return {"schemaVersion": "nodedc.device-core.release-reconciliation.v2",
|
||||
"releaseId": DEVICE_RELEASE_RECONCILIATION_V2_ID,
|
||||
"failedReleaseId": DEVICE_RELEASE_R5_ID, "failedSha256": DEVICE_RELEASE_R5_SHA,
|
||||
"backupId": DEVICE_RELEASE_R5_BACKUP,
|
||||
"mode": "prove-restored-baseline-mount-order-only", "runtimeFingerprintVersion": 2}
|
||||
|
||||
|
||||
def validate_device_release_reconciliation_payload(payload, entries, patch_id):
|
||||
if tuple(entries) == (DEVICE_RELEASE_RECONCILIATION_V2_ENTRY,):
|
||||
if (patch_id != DEVICE_RELEASE_RECONCILIATION_V2_ID or
|
||||
read_strict_json(payload / DEVICE_RELEASE_RECONCILIATION_V2_ENTRY, "Device Core reconciliation v2") != device_release_reconciliation_v2_descriptor()):
|
||||
die("Device Core reconciliation v2 identity/descriptor mismatch")
|
||||
return
|
||||
if tuple(entries) != (DEVICE_RELEASE_RECONCILIATION_ENTRY,) or patch_id != DEVICE_RELEASE_RECONCILIATION_ID:
|
||||
die("Device Core reconciliation identity/entry mismatch")
|
||||
if read_strict_json(payload / DEVICE_RELEASE_RECONCILIATION_ENTRY, "Device Core reconciliation") != device_release_reconciliation_descriptor():
|
||||
@@ -32606,13 +32622,19 @@ def device_release_r5_evidence():
|
||||
|
||||
def preflight_device_release_reconciliation(payload, entries, patch_id, *, marker_installed=False):
|
||||
validate_device_release_reconciliation_payload(payload, entries, patch_id)
|
||||
marker = DEVICE_PLANE_ROOT / DEVICE_RELEASE_RECONCILIATION_ENTRY
|
||||
v2 = tuple(entries) == (DEVICE_RELEASE_RECONCILIATION_V2_ENTRY,)
|
||||
marker = DEVICE_PLANE_ROOT / entries[0]
|
||||
other = DEVICE_PLANE_ROOT / (DEVICE_RELEASE_RECONCILIATION_ENTRY if v2 else DEVICE_RELEASE_RECONCILIATION_V2_ENTRY)
|
||||
if other.exists() or other.is_symlink():
|
||||
die("Device Core reconciliation marker versions conflict")
|
||||
if not marker_installed and (marker.exists() or marker.is_symlink()):
|
||||
die("Device Core reconciliation marker already exists")
|
||||
before = device_release_r5_evidence()
|
||||
# Source, current image IDs, configuration and every preserved dependency
|
||||
# are checked without invoking any build, Compose mutation or secret ensure.
|
||||
run_healthchecks("device-core-release", DEVICE_RELEASE_ENTRIES, DEVICE_RELEASE_SERVICES)
|
||||
if v2:
|
||||
before = device_release_r5_mount_order_baseline(before)
|
||||
accept_device_release(before, rollback=True)
|
||||
print("device_release_reconciliation=restored-baseline-proven;services=none;build=none;runtime-mutation=none")
|
||||
return before
|
||||
@@ -32621,18 +32643,27 @@ def preflight_device_release_reconciliation(payload, entries, patch_id, *, marke
|
||||
def device_release_r5_reconciled(row):
|
||||
if row != device_release_r5_failed_row():
|
||||
return False
|
||||
marker = DEVICE_PLANE_ROOT / DEVICE_RELEASE_RECONCILIATION_ENTRY
|
||||
if marker.is_symlink() or not marker.is_file():
|
||||
candidates = [(DEVICE_RELEASE_RECONCILIATION_ENTRY, DEVICE_RELEASE_RECONCILIATION_ID),
|
||||
(DEVICE_RELEASE_RECONCILIATION_V2_ENTRY, DEVICE_RELEASE_RECONCILIATION_V2_ID)]
|
||||
present = [(entry, identity) for entry, identity in candidates
|
||||
if (DEVICE_PLANE_ROOT / entry).exists() or (DEVICE_PLANE_ROOT / entry).is_symlink()]
|
||||
if not present:
|
||||
return False
|
||||
validate_device_release_reconciliation_payload(DEVICE_PLANE_ROOT, (DEVICE_RELEASE_RECONCILIATION_ENTRY,), DEVICE_RELEASE_RECONCILIATION_ID)
|
||||
records = [r for r in load_state(STATE_FILE) if r.get("id") == DEVICE_RELEASE_RECONCILIATION_ID]
|
||||
if len(present) != 1:
|
||||
die("Device Core reconciliation marker versions conflict")
|
||||
entry, identity = present[0]
|
||||
marker = DEVICE_PLANE_ROOT / entry
|
||||
if marker.is_symlink() or not marker.is_file():
|
||||
die("Device Core reconciliation marker is unsafe")
|
||||
validate_device_release_reconciliation_payload(DEVICE_PLANE_ROOT, (entry,), identity)
|
||||
records = [r for r in load_state(STATE_FILE) if r.get("id") == identity]
|
||||
if len(records) != 1 or records[0].get("component") != "device-core-release-reconciliation":
|
||||
die("Device Core reconciliation marker lacks its canonical applied receipt")
|
||||
with tempfile.TemporaryDirectory(prefix="device-r5-marker-", dir=TMP_DIR) as tmp:
|
||||
manifest, entries, payload = load_artifact(device_release_applied_archive(records[0]), Path(tmp))
|
||||
if manifest.get("component") != "device-core-release-reconciliation" or manifest.get("id") != DEVICE_RELEASE_RECONCILIATION_ID:
|
||||
if manifest.get("component") != "device-core-release-reconciliation" or manifest.get("id") != identity:
|
||||
die("Device Core reconciliation applied archive identity mismatch")
|
||||
if sha256_file(payload / DEVICE_RELEASE_RECONCILIATION_ENTRY) != sha256_file(marker):
|
||||
if sha256_file(payload / entry) != sha256_file(marker):
|
||||
die("Device Core reconciliation installed marker drift")
|
||||
# Keep original incident evidence immutable after acknowledgement.
|
||||
device_release_r5_evidence()
|
||||
@@ -32792,6 +32823,89 @@ def device_release_drift_audit(payload, entries, patch_id):
|
||||
return report
|
||||
|
||||
|
||||
def device_release_mounts_v2(fields):
|
||||
"""Sort only uniquely addressed mount objects; keep every option and value."""
|
||||
result = json.loads(json.dumps(fields))
|
||||
host = result["HostConfig"]
|
||||
mounts = host.get("Mounts")
|
||||
if mounts is None:
|
||||
return result
|
||||
if not isinstance(mounts, list):
|
||||
die("Device Core mount fingerprint requires a mount list")
|
||||
targets = set()
|
||||
for mount in mounts:
|
||||
target = mount.get("Target") if isinstance(mount, dict) else None
|
||||
if (not isinstance(target, str) or not target.startswith("/") or "\0" in target
|
||||
or posixpath.normpath(target) != target or target.startswith("//")
|
||||
or target in targets):
|
||||
die("Device Core mount fingerprint requires unique absolute targets")
|
||||
targets.add(target)
|
||||
host["Mounts"] = sorted(mounts, key=lambda value: json.dumps(value, sort_keys=True))
|
||||
return result
|
||||
|
||||
|
||||
def device_release_runtime_snapshot_v2():
|
||||
samples = device_release_drift_sample()
|
||||
result = {}
|
||||
for service, sample in samples.items():
|
||||
row = sample["runtime"]
|
||||
if not row["running"] or row["health"] not in (None, "healthy"):
|
||||
die("Device Core predecessor service is not healthy: " + service)
|
||||
row["runtimeConfigSha256"] = device_release_drift_digest(device_release_mounts_v2(sample["fields"]))
|
||||
result[service] = row
|
||||
return result
|
||||
|
||||
|
||||
def device_release_mount_order_proof(fields, expected):
|
||||
# Unlike the diagnostic probe, this CANNOT change Binds or NetworkMode.
|
||||
# The legacy backup contains only a digest, so prove an exact old preimage.
|
||||
device_release_mounts_v2(fields)
|
||||
if not isinstance(expected, str) or not re.fullmatch(r"[a-f0-9]{64}", expected):
|
||||
die("Device Core mount order proof requires an exact saved digest")
|
||||
mounts = fields["HostConfig"].get("Mounts")
|
||||
if mounts is not None and len(mounts) > 7:
|
||||
die("Device Core r5 mount order proof exceeds seven-mount bound")
|
||||
if device_release_drift_digest(fields) == expected:
|
||||
return 1
|
||||
candidate = json.loads(json.dumps(fields))
|
||||
checked = 1
|
||||
for order in (() if mounts is None else itertools.permutations(mounts)):
|
||||
candidate["HostConfig"]["Mounts"] = list(order)
|
||||
checked += 1
|
||||
if device_release_drift_digest(candidate) == expected:
|
||||
return checked
|
||||
die("Device Core r5 configuration differs beyond HostConfig.Mounts order")
|
||||
|
||||
|
||||
def device_release_r5_mount_order_baseline(before):
|
||||
"""Derive v2 hashes only after exact legacy proof, preserving original evidence."""
|
||||
if "runtimeFingerprintVersion" in before or set(before["runtime"]) != set(DEVICE_RELEASE_INVENTORY):
|
||||
die("Device Core r5 legacy baseline mismatch")
|
||||
observations = [device_release_drift_sample() for _ in range(3)]
|
||||
baseline = json.loads(json.dumps(before))
|
||||
baseline["runtimeFingerprintVersion"] = 2
|
||||
report = {}
|
||||
for service in DEVICE_RELEASE_INVENTORY:
|
||||
old = before["runtime"][service]
|
||||
samples = [observation[service] for observation in observations]
|
||||
if any(sample != samples[0] for sample in samples):
|
||||
die("Device Core r5 runtime changed during mount order proof: " + service)
|
||||
sample = samples[0]
|
||||
row, fields = sample["runtime"], sample["fields"]
|
||||
if (not row["running"] or row["health"] not in (None, "healthy") or row["imageId"] != old["imageId"]
|
||||
or row["runtimeConfigSha256"] != device_release_drift_digest(fields)):
|
||||
die("Device Core r5 service image/health/evidence mismatch: " + service)
|
||||
if service not in DEVICE_RELEASE_SERVICES and row != old:
|
||||
die("Device Core r5 preserved service differs: " + service)
|
||||
checked = device_release_mount_order_proof(fields, old["runtimeConfigSha256"])
|
||||
digest = device_release_drift_digest(device_release_mounts_v2(fields))
|
||||
baseline["runtime"][service]["runtimeConfigSha256"] = digest
|
||||
report[service] = {"exactLegacyProof": True, "sampleCount": 3,
|
||||
"candidatesChecked": checked, "runtimeFingerprintV2": digest}
|
||||
print("device_release_mount_order_proof=" + json.dumps(report, sort_keys=True, separators=(",", ":")))
|
||||
return baseline
|
||||
|
||||
|
||||
def preflight_device_release(payload, entries, patch_id):
|
||||
d = validate_device_release_payload(payload, entries, patch_id)
|
||||
predecessor = device_release_predecessor(d)
|
||||
@@ -32802,15 +32916,18 @@ def preflight_device_release(payload, entries, patch_id):
|
||||
die("Device Core release changes the immutable database/build foundation")
|
||||
validate_device_manager_control_plane_runtime(require_edge_channel=True, core_network_mode="private-egress", require_persistent_data=True, read_only=True)
|
||||
run_healthchecks("device-core-release", entries, DEVICE_RELEASE_SERVICES)
|
||||
before = device_release_runtime_snapshot()
|
||||
before = device_release_runtime_snapshot_v2()
|
||||
compose = {str(p): sha256_file(p) for p in component_compose_files("device-plane")}
|
||||
return {"descriptor": d, "predecessor": predecessor["releaseId"] if predecessor else "manager-v13/core-v4/edge-v4",
|
||||
return {"runtimeFingerprintVersion": 2, "descriptor": d, "predecessor": predecessor["releaseId"] if predecessor else "manager-v13/core-v4/edge-v4",
|
||||
"runtime": before, "compose": compose, "otherComposeServices": device_release_other_compose_services(),
|
||||
"installedFiles": collect_exact_files(DEVICE_PLANE_ROOT, DEVICE_RELEASE_ENTRIES if predecessor else DEVICE_RELEASE_ENTRIES[:-1], "Device Core rollback baseline")}
|
||||
|
||||
|
||||
def accept_device_release(before, *, rollback=False):
|
||||
current = device_release_runtime_snapshot()
|
||||
version = before.get("runtimeFingerprintVersion", 1)
|
||||
if type(version) is not int or version not in (1, 2):
|
||||
die("Device Core release runtime fingerprint version mismatch")
|
||||
current = device_release_runtime_snapshot_v2() if version == 2 else device_release_runtime_snapshot()
|
||||
for service, old in before["runtime"].items():
|
||||
new = current[service]
|
||||
if service not in DEVICE_RELEASE_SERVICES:
|
||||
@@ -32920,6 +33037,7 @@ def plan_artifact(artifact):
|
||||
release_before = preflight_device_release(payload_dir, entries, manifest["id"])
|
||||
print("device_release_source=" + release_before["descriptor"]["sourceCommit"])
|
||||
print("device_release_predecessor=" + release_before["predecessor"])
|
||||
print("device_release_runtime_fingerprint=v2;normalized=HostConfig.Mounts-order-only")
|
||||
print("device_release_scope=core,manager;database-volumes-topology-secrets=preserved;rollback=exact-image-ids")
|
||||
if is_gitea_fresh_install_slice(manifest["component"], entries):
|
||||
validate_gitea_compose_schema(payload_dir / GITEA_COMPOSE_REL)
|
||||
@@ -39081,7 +39199,8 @@ def run_healthchecks(component, entries=None, services=None):
|
||||
if component == "device-core-release-reconciliation":
|
||||
if tuple(services or ()) != ():
|
||||
die("Device Core reconciliation must not select services")
|
||||
preflight_device_release_reconciliation(DEVICE_PLANE_ROOT, entries, DEVICE_RELEASE_RECONCILIATION_ID, marker_installed=True)
|
||||
identity = DEVICE_RELEASE_RECONCILIATION_V2_ID if tuple(entries or ()) == (DEVICE_RELEASE_RECONCILIATION_V2_ENTRY,) else DEVICE_RELEASE_RECONCILIATION_ID
|
||||
preflight_device_release_reconciliation(DEVICE_PLANE_ROOT, entries, identity, marker_installed=True)
|
||||
return
|
||||
if component == "device-core-release":
|
||||
if tuple(services or ()) != DEVICE_RELEASE_SERVICES or tuple(entries or ()) != DEVICE_RELEASE_ENTRIES:
|
||||
@@ -40997,7 +41116,7 @@ def apply_artifact(artifact):
|
||||
if component == "device-core-release-reconciliation":
|
||||
try:
|
||||
restore_platform_overlay(root, backup_dir, entries, current_stamp)
|
||||
if (root / DEVICE_RELEASE_RECONCILIATION_ENTRY).exists():
|
||||
if any((root / entry).exists() or (root / entry).is_symlink() for entry in entries):
|
||||
die("Device Core reconciliation rollback left a marker")
|
||||
rollback_status = "ok:device-core-release-reconciliation-marker-only"
|
||||
except Exception as rollback_exc:
|
||||
|
||||
Reference in New Issue
Block a user