Phase 22: Complete release provenance
This commit is contained in:
+21
-20
@@ -56,7 +56,7 @@ def provenance_fingerprint(data):
|
|||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
def validate_migration_path(value):
|
def validate_migration_path(value, *, modular=False):
|
||||||
|
|
||||||
require(
|
require(
|
||||||
isinstance(value, str),
|
isinstance(value, str),
|
||||||
@@ -70,7 +70,17 @@ def validate_migration_path(value):
|
|||||||
and path.as_posix() == value
|
and path.as_posix() == value
|
||||||
and ".." not in path.parts
|
and ".." not in path.parts
|
||||||
and len(path.parts) >= 3
|
and len(path.parts) >= 3
|
||||||
and path.parts[:2] == ("database", "migrations")
|
and (
|
||||||
|
path.parts[:2] == ("database", "migrations")
|
||||||
|
or (
|
||||||
|
modular and len(path.parts) >= 5 and (
|
||||||
|
(path.parts[0] == "Modules"
|
||||||
|
and path.parts[2:4] == ("Database", "Migrations"))
|
||||||
|
or (path.parts[0] == "modules"
|
||||||
|
and path.parts[2:4] == ("database", "migrations"))
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
and path.suffix == ".php"
|
and path.suffix == ".php"
|
||||||
and all(
|
and all(
|
||||||
re.fullmatch(
|
re.fullmatch(
|
||||||
@@ -84,23 +94,14 @@ def validate_migration_path(value):
|
|||||||
|
|
||||||
def validate_provenance(data):
|
def validate_provenance(data):
|
||||||
|
|
||||||
require(
|
fields = {"format_version", "revision", "migrations", "dependencies", "fingerprint"}
|
||||||
isinstance(data, dict)
|
require(isinstance(data, dict), "Invalid provenance evidence structure")
|
||||||
and set(data) == {
|
version = data.get("format_version")
|
||||||
"format_version",
|
require(type(version) is int and version in (1, 2), "Unsupported provenance format")
|
||||||
"revision",
|
if version == 2:
|
||||||
"migrations",
|
fields.add("source_tree")
|
||||||
"dependencies",
|
require(valid_hash(data.get("source_tree"), 64), "Invalid source tree checksum")
|
||||||
"fingerprint",
|
require(set(data) == fields, "Invalid provenance evidence structure")
|
||||||
},
|
|
||||||
"Invalid provenance evidence structure",
|
|
||||||
)
|
|
||||||
|
|
||||||
require(
|
|
||||||
type(data["format_version"]) is int
|
|
||||||
and data["format_version"] == 1,
|
|
||||||
"Unsupported provenance format",
|
|
||||||
)
|
|
||||||
|
|
||||||
require(
|
require(
|
||||||
valid_hash(data["revision"], 40),
|
valid_hash(data["revision"], 40),
|
||||||
@@ -120,7 +121,7 @@ def validate_provenance(data):
|
|||||||
|
|
||||||
for name, digest in data["migrations"].items():
|
for name, digest in data["migrations"].items():
|
||||||
|
|
||||||
validate_migration_path(name)
|
validate_migration_path(name, modular=version == 2)
|
||||||
|
|
||||||
require(
|
require(
|
||||||
valid_hash(digest, 64),
|
valid_hash(digest, 64),
|
||||||
|
|||||||
@@ -0,0 +1,71 @@
|
|||||||
|
"""Exact tracked-source and generated-artifact verification for dev.3."""
|
||||||
|
|
||||||
|
import hashlib
|
||||||
|
import os
|
||||||
|
import stat
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
from core.compatibility import canonical_hash, provenance_fingerprint, validate_provenance
|
||||||
|
from core.provenance import ProvenanceVerifier, require
|
||||||
|
|
||||||
|
|
||||||
|
def inventory(root):
|
||||||
|
root = Path(root)
|
||||||
|
result = {}
|
||||||
|
for directory, dirs, files in os.walk(root):
|
||||||
|
for name in dirs:
|
||||||
|
require(not (Path(directory) / name).is_symlink(), "Symlink in release tree")
|
||||||
|
for name in files:
|
||||||
|
path = Path(directory) / name
|
||||||
|
info = path.lstat()
|
||||||
|
require(stat.S_ISREG(info.st_mode) and info.st_nlink == 1, "Unsafe release file")
|
||||||
|
result[path.relative_to(root).as_posix()] = (
|
||||||
|
hashlib.sha256(path.read_bytes()).hexdigest(),
|
||||||
|
bool(info.st_mode & 0o111),
|
||||||
|
)
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
class CompleteProvenance(ProvenanceVerifier):
|
||||||
|
def __init__(self, repository, revisions, generated):
|
||||||
|
super().__init__(repository)
|
||||||
|
self.revisions = frozenset(revisions)
|
||||||
|
self.generated = dict(generated)
|
||||||
|
self._tracked = {}
|
||||||
|
|
||||||
|
def verify(self, release):
|
||||||
|
evidence = super().verify(release)
|
||||||
|
revision = evidence["revision"]
|
||||||
|
require(revision in self.revisions, "Release revision is not authorised")
|
||||||
|
if revision not in self._tracked:
|
||||||
|
expected = {}
|
||||||
|
for item in self.git("ls-tree", "-rz", revision).split(b"\0"):
|
||||||
|
if not item:
|
||||||
|
continue
|
||||||
|
metadata, name = item.split(b"\t", 1)
|
||||||
|
mode, kind, oid = metadata.decode().split()
|
||||||
|
require(kind == "blob" and mode in ("100644", "100755"),
|
||||||
|
"Unsupported tracked file type")
|
||||||
|
expected[name.decode()] = (
|
||||||
|
hashlib.sha256(self.git("cat-file", "blob", oid)).hexdigest(),
|
||||||
|
mode == "100755",
|
||||||
|
)
|
||||||
|
self._tracked[revision] = expected
|
||||||
|
tracked = self._tracked[revision]
|
||||||
|
marker = hashlib.sha256((revision + "\n").encode()).hexdigest(), False
|
||||||
|
require(not (tracked.keys() & self.generated.keys()) and "REVISION" not in tracked,
|
||||||
|
"Tracked/generated path collision")
|
||||||
|
expected = {**tracked, **self.generated, "REVISION": marker}
|
||||||
|
require(inventory(release) == expected,
|
||||||
|
"Full release tree differs from authorised source or generated artifacts")
|
||||||
|
for name, (digest, _) in tracked.items():
|
||||||
|
parts = Path(name).parts
|
||||||
|
if (len(parts) >= 5 and name.endswith(".php") and
|
||||||
|
(parts[0], parts[2], parts[3]) in (
|
||||||
|
("Modules", "Database", "Migrations"), ("modules", "database", "migrations")
|
||||||
|
)):
|
||||||
|
evidence["migrations"][name] = digest
|
||||||
|
evidence.update(format_version=2, source_tree=canonical_hash(expected))
|
||||||
|
evidence["fingerprint"] = provenance_fingerprint(evidence)
|
||||||
|
validate_provenance(evidence)
|
||||||
|
return evidence
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
import tempfile
|
||||||
|
import unittest
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
from core.dev3.provenance import inventory
|
||||||
|
from core.provenance import ProvenanceError
|
||||||
|
|
||||||
|
|
||||||
|
class InventoryTests(unittest.TestCase):
|
||||||
|
def test_inventory_captures_content_modes_and_rejects_symlinks(self):
|
||||||
|
with tempfile.TemporaryDirectory() as directory:
|
||||||
|
root = Path(directory)
|
||||||
|
script = root / "entry"
|
||||||
|
script.write_text("first")
|
||||||
|
original = inventory(root)
|
||||||
|
script.chmod(0o700)
|
||||||
|
self.assertNotEqual(inventory(root), original)
|
||||||
|
script.write_text("second")
|
||||||
|
self.assertNotEqual(inventory(root)["entry"][0], original["entry"][0])
|
||||||
|
(root / "link").symlink_to(script)
|
||||||
|
with self.assertRaises(ProvenanceError):
|
||||||
|
inventory(root)
|
||||||
Reference in New Issue
Block a user