86 lines
3.0 KiB
Python
86 lines
3.0 KiB
Python
"""Bounded prepared-Ubuntu package smoke check; never installs or opens hardware."""
|
|
|
|
import argparse
|
|
import hashlib
|
|
import json
|
|
import os
|
|
import platform
|
|
import subprocess
|
|
import tempfile
|
|
import time
|
|
import uuid
|
|
from datetime import UTC, datetime
|
|
from pathlib import Path
|
|
|
|
|
|
def qualify(package, expected):
|
|
assert os.geteuid() != 0
|
|
release = platform.freedesktop_os_release()
|
|
assert (release["ID"], release["VERSION_ID"], platform.machine()) == (
|
|
"ubuntu",
|
|
"24.04",
|
|
"x86_64",
|
|
)
|
|
raw = package.read_bytes()
|
|
assert hashlib.sha256(raw).hexdigest() == expected
|
|
report = dict(
|
|
schema="missioncore.bms.prepared-host-package-check/v1",
|
|
session_id=uuid.uuid4().hex,
|
|
started_at=datetime.now(UTC).isoformat(),
|
|
monotonic_started=time.monotonic(),
|
|
clean_image_qualified=False,
|
|
hardware_access=False,
|
|
checks=[],
|
|
)
|
|
|
|
def run(name, args, allowed=(0,), **kwargs):
|
|
result = subprocess.run(args, capture_output=True, timeout=60, **kwargs)
|
|
assert result.returncode in allowed, name + ": " + result.stderr.decode()[:300]
|
|
report["checks"].append(dict(id=name, exit_code=result.returncode))
|
|
return result.stdout
|
|
|
|
with tempfile.TemporaryDirectory(prefix="bms-package-", dir=package.parent) as scratch:
|
|
root = Path(scratch)
|
|
run("control", ["dpkg-deb", "-e", str(package), str(root / "control")])
|
|
run("payload", ["dpkg-deb", "-x", str(package), str(root / "payload")])
|
|
for name in ["postinst", "prerm"]:
|
|
run("shell-" + name, ["/bin/sh", "-n", str(root / "control" / name)])
|
|
control = (root / "control/control").read_text()
|
|
assert "python3-dbus" in control and "bluez (>= 5.72)" in control
|
|
runtime = root / "payload/usr/lib/mission-core-jk-bms"
|
|
for path in runtime.rglob("*.py"):
|
|
compile(path.read_text(), str(path), "exec")
|
|
run(
|
|
"python-imports",
|
|
[
|
|
"/usr/bin/python3",
|
|
"-B",
|
|
"-c",
|
|
"import runtime.protocol, runtime.service, runtime.bluez, runtime.server; "
|
|
"from gi.repository import GLib",
|
|
],
|
|
cwd=runtime,
|
|
)
|
|
help_text = run(
|
|
"node-executable-help",
|
|
[str(root / "payload/usr/lib/mission-core-node/node-agent"), "--help"],
|
|
allowed=(0, 1, 2),
|
|
)
|
|
assert len(help_text) < 65536
|
|
run("apt-simulation", ["apt-get", "--simulate", "--no-remove", "install", str(package)])
|
|
report.update(
|
|
state="complete",
|
|
finished_at=datetime.now(UTC).isoformat(),
|
|
duration_seconds=time.monotonic() - report["monotonic_started"],
|
|
package_sha256=expected,
|
|
)
|
|
print(json.dumps(report))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument("package", type=Path)
|
|
parser.add_argument("sha256")
|
|
args = parser.parse_args()
|
|
qualify(args.package.resolve(), args.sha256)
|