96 lines
4.4 KiB
Python
96 lines
4.4 KiB
Python
"""Assemble the passive Windows test release from independently checked evidence."""
|
|
|
|
import argparse
|
|
import hashlib
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
from tempfile import TemporaryDirectory
|
|
|
|
|
|
HERE = Path(__file__).resolve().parent
|
|
CASE = "1d61d88b-ab17-49b1-9a8d-2427348313b4"
|
|
AGENT_SHA256 = "78c16dcaee53fe9a04d4d84c7a1475330c5a1af30c104f8f9d6f56b5b7408513"
|
|
BUILD_SHA256 = "44ac22b926b4f860da2280dbcefe0d2b3f22fb07c3ad37e856e435296a75a6b3"
|
|
LINUX_FIXTURE_SHA256 = "54aa6430c0b1eb2415ee3f888b015c05faa536d07d96f4bacf01152f1236b7db"
|
|
WINDOWS_FIXTURE_SHA256 = "4228c05aafb3cbf5d0cbbc06cb211bfb82b0d084e8c509db3afea159bef82c7f"
|
|
CARGO_INPUTS_SHA256 = "e35a907ebd0c4a9af70bb93460e3490aa67ab9f6c94b6e8b77059cb17c027812"
|
|
CROSS_CRATE_INPUTS_SHA256 = "9bb9aa28b9b02e2a05a90546ef0208757ff2c856c848fd1ed33b64a2a313b0ef"
|
|
PAIR = "Set-PassiveJobPair.ps1"
|
|
|
|
|
|
def digest(data: bytes) -> str:
|
|
return hashlib.sha256(data).hexdigest()
|
|
|
|
|
|
def public_input(path: Path, expected: str, limit: int) -> bytes:
|
|
if path.is_symlink() or not path.is_file() or not 0 < path.stat().st_size <= limit:
|
|
raise ValueError(f"Unexpected public input at {path}")
|
|
data = path.read_bytes()
|
|
if digest(data) != expected:
|
|
raise ValueError(f"Digest mismatch at {path}")
|
|
return data
|
|
|
|
|
|
def assemble(args: argparse.Namespace) -> None:
|
|
# The media can be mounted read-only. No credentials or network access are needed.
|
|
files = {
|
|
"rmm-agent-windows.exe": public_input(args.agent, AGENT_SHA256, 96 << 20),
|
|
"agent-build.json": public_input(args.build_receipt, BUILD_SHA256, 1 << 20),
|
|
"linux-fixture.json": public_input(args.linux_receipt, LINUX_FIXTURE_SHA256, 1 << 20),
|
|
"windows-fixture.json": public_input(args.windows_receipt, WINDOWS_FIXTURE_SHA256, 1 << 20),
|
|
"cargo-inputs.json": public_input(args.build_receipt.with_name("cargo-inputs.json"), CARGO_INPUTS_SHA256, 1 << 20),
|
|
"cross-crate-inputs.json": public_input(args.build_receipt.with_name("cross-crate-inputs.json"), CROSS_CRATE_INPUTS_SHA256, 1 << 20),
|
|
}
|
|
for name, data in files.items():
|
|
if name.endswith(".json") and not isinstance(json.loads(data), dict):
|
|
raise ValueError(f"Expected a public JSON receipt in {name}")
|
|
manifest = {
|
|
"kind": "rmm.windows.echo-passive-build.v1",
|
|
"case_id": CASE,
|
|
"agent_sha256": AGENT_SHA256,
|
|
"agent_build_sha256": BUILD_SHA256,
|
|
"linux_fixture_sha256": LINUX_FIXTURE_SHA256,
|
|
"windows_fixture_sha256": WINDOWS_FIXTURE_SHA256,
|
|
"cargo_inputs_sha256": CARGO_INPUTS_SHA256,
|
|
"cross_crate_inputs_sha256": CROSS_CRATE_INPUTS_SHA256,
|
|
}
|
|
files["passive-build.json"] = json.dumps(manifest, sort_keys=True, separators=(",", ":")).encode()
|
|
manifest_sha = digest(files["passive-build.json"])
|
|
template = (HERE / PAIR).read_bytes()
|
|
files[PAIR] = template
|
|
if template.count(manifest_sha.encode()) != 1:
|
|
raise ValueError("Passive installer does not bind this exact manifest")
|
|
for name in ["Publish-JobPairTimeProfile.ps1", "Read-JobPairTimeSamples.ps1"]:
|
|
files[name] = (HERE / name).read_bytes()
|
|
if files[name].count(digest(template).encode()) != 1:
|
|
raise ValueError(f"Passive installer pin changed in {name}")
|
|
reader = "Read-PassiveTimeSamples.ps1"
|
|
files[reader] = (HERE / reader).read_bytes()
|
|
if files["Read-JobPairTimeSamples.ps1"].count(digest(files[reader]).encode()) != 1:
|
|
raise ValueError("Native time reader pin mismatch")
|
|
checksums = "".join(f"{digest(data)} {name}\n" for name, data in sorted(files.items()))
|
|
files["SHA256SUMS"] = checksums.encode()
|
|
with TemporaryDirectory(dir=args.output.parent) as temporary:
|
|
stage = Path(temporary) / "media"
|
|
stage.mkdir(mode=0o700)
|
|
for name, data in files.items():
|
|
(stage / name).write_bytes(data)
|
|
# A completed destination is always nonempty. Never merge or update it.
|
|
if args.output.exists() or args.output.is_symlink():
|
|
raise FileExistsError(args.output)
|
|
os.rename(stage, args.output)
|
|
print(f"passive-build.json {manifest_sha}")
|
|
print(f"SHA256SUMS {digest(files['SHA256SUMS'])}")
|
|
|
|
|
|
def main() -> None:
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
for name in ("agent", "build-receipt", "linux-receipt", "windows-receipt", "output"):
|
|
parser.add_argument("--" + name, type=Path, required=True)
|
|
assemble(parser.parse_args())
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|