48 lines
2.3 KiB
Python
48 lines
2.3 KiB
Python
"""Verify the Docker archive identity and each layer's uncompressed rootfs digest."""
|
|
import argparse
|
|
import hashlib
|
|
import json
|
|
import tarfile
|
|
from pathlib import Path
|
|
|
|
|
|
def stream_sha(source):
|
|
value = hashlib.sha256()
|
|
for chunk in iter(lambda: source.read(4 * 1024 * 1024), b""):
|
|
value.update(chunk)
|
|
return value.hexdigest()
|
|
|
|
|
|
def main():
|
|
p = argparse.ArgumentParser()
|
|
p.add_argument("directory", type=Path)
|
|
args = p.parse_args()
|
|
records = json.loads((args.directory / "IMAGE_MANIFEST.json").read_text(encoding="utf-8"))
|
|
output = []
|
|
for record in records:
|
|
path = args.directory / record["archive"]
|
|
with path.open("rb") as source:
|
|
assert stream_sha(source) == record["archive_sha256"], "archive checksum mismatch"
|
|
with tarfile.open(path) as archive:
|
|
manifest = json.load(archive.extractfile("manifest.json"))[0]
|
|
config_bytes = archive.extractfile(manifest["Config"]).read()
|
|
assert "sha256:" + hashlib.sha256(config_bytes).hexdigest() == record["config"]["digest"]
|
|
cfg = json.loads(config_bytes)
|
|
assert cfg["architecture"] == "arm64" and cfg["os"] == "linux"
|
|
assert manifest["RepoTags"] == [record["name"] + ":" + record["tag"]]
|
|
assert len(manifest["Layers"]) == len(cfg["rootfs"]["diff_ids"])
|
|
for name, expected in zip(manifest["Layers"], cfg["rootfs"]["diff_ids"]):
|
|
assert "sha256:" + stream_sha(archive.extractfile(name)) == expected, "rootfs checksum mismatch"
|
|
versions = {x.split("=", 1)[0]: x.split("=", 1)[1] for x in cfg.get("config", {}).get("Env", [])
|
|
if x.startswith(("PYTHON_VERSION=", "REDIS_VERSION=", "PG_VERSION=", "CADDY_VERSION="))}
|
|
output.append({"image": manifest["RepoTags"][0], "platform": "linux/arm64", "versions": versions,
|
|
"layers_verified": len(manifest["Layers"]), "archive_bytes": path.stat().st_size})
|
|
report = {"status": "passed", "checks": "archive/config/rootfs SHA256 + linux/arm64 + tags", "images": output,
|
|
"runtime_test": "not run; local Docker daemon unavailable"}
|
|
(args.directory / "VERIFICATION.json").write_text(json.dumps(report, indent=2) + "\n", encoding="utf-8")
|
|
print(json.dumps(report, indent=2))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|