Initial ABSDEV deployment framework development preview
ABSDEV Framework CI / framework-validation (push) Canceled after 0s
ABSDEV Framework CI / framework-validation (push) Canceled after 0s
This commit is contained in:
@@ -0,0 +1,284 @@
|
||||
"""Git-backed verification of release-critical files."""
|
||||
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import stat
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
class ProvenanceError(RuntimeError):
|
||||
"""Release provenance could not be established."""
|
||||
|
||||
|
||||
DEPENDENCIES = (
|
||||
"composer.json",
|
||||
"composer.lock",
|
||||
"package.json",
|
||||
"package-lock.json",
|
||||
)
|
||||
|
||||
|
||||
def require(condition, message):
|
||||
|
||||
if not condition:
|
||||
raise ProvenanceError(message)
|
||||
|
||||
|
||||
def digest(data):
|
||||
|
||||
return hashlib.sha256(data).hexdigest()
|
||||
|
||||
|
||||
class ProvenanceVerifier:
|
||||
|
||||
def __init__(self, repository):
|
||||
|
||||
self.repository = Path(repository).absolute()
|
||||
|
||||
require(
|
||||
self.repository.is_dir()
|
||||
and self.repository.resolve() == self.repository,
|
||||
"Invalid Git repository path",
|
||||
)
|
||||
|
||||
self.git("rev-parse", "--git-dir")
|
||||
|
||||
def git(self, *args):
|
||||
|
||||
try:
|
||||
|
||||
# Prevent inherited Git environment variables
|
||||
# from redirecting commands to another repository.
|
||||
|
||||
environment = {
|
||||
key: value
|
||||
for key, value in os.environ.items()
|
||||
if not key.startswith("GIT_")
|
||||
}
|
||||
|
||||
environment["GIT_TERMINAL_PROMPT"] = "0"
|
||||
environment["GIT_NO_REPLACE_OBJECTS"] = "1"
|
||||
|
||||
return subprocess.check_output(
|
||||
[
|
||||
"git",
|
||||
"-C",
|
||||
str(self.repository),
|
||||
*args,
|
||||
],
|
||||
stdin=subprocess.DEVNULL,
|
||||
stderr=subprocess.DEVNULL,
|
||||
env=environment,
|
||||
timeout=30,
|
||||
)
|
||||
|
||||
except (
|
||||
OSError,
|
||||
subprocess.SubprocessError,
|
||||
):
|
||||
|
||||
raise ProvenanceError(
|
||||
"Git provenance command failed"
|
||||
) from None
|
||||
|
||||
def safe_file(self, root, relative):
|
||||
|
||||
relative = Path(relative)
|
||||
|
||||
require(
|
||||
not relative.is_absolute()
|
||||
and ".." not in relative.parts,
|
||||
"Unsafe release file path",
|
||||
)
|
||||
|
||||
path = root
|
||||
|
||||
for part in relative.parts:
|
||||
|
||||
path = path / part
|
||||
|
||||
require(
|
||||
not path.is_symlink(),
|
||||
"Symbolic links prohibited in provenance files",
|
||||
)
|
||||
|
||||
try:
|
||||
|
||||
details = path.lstat()
|
||||
|
||||
except OSError:
|
||||
|
||||
raise ProvenanceError(
|
||||
f"Missing release file: {relative}"
|
||||
) from None
|
||||
|
||||
require(
|
||||
stat.S_ISREG(details.st_mode),
|
||||
f"Invalid release file: {relative}",
|
||||
)
|
||||
|
||||
require(
|
||||
path.resolve().is_relative_to(root),
|
||||
"Release file escaped its directory",
|
||||
)
|
||||
|
||||
return path
|
||||
|
||||
def migration_inventory(self, root):
|
||||
|
||||
directory = root / "database/migrations"
|
||||
|
||||
require(
|
||||
directory.is_dir()
|
||||
and directory.resolve() == directory,
|
||||
"Invalid migration directory",
|
||||
)
|
||||
|
||||
result = []
|
||||
|
||||
for path in directory.rglob("*"):
|
||||
|
||||
require(
|
||||
not path.is_symlink(),
|
||||
"Symbolic link in migration directory",
|
||||
)
|
||||
|
||||
if path.suffix == ".php":
|
||||
|
||||
require(
|
||||
path.is_file(),
|
||||
"Invalid migration file",
|
||||
)
|
||||
|
||||
result.append(
|
||||
path.relative_to(root).as_posix()
|
||||
)
|
||||
|
||||
return sorted(result)
|
||||
|
||||
def verify(self, release):
|
||||
|
||||
root = Path(release).absolute()
|
||||
|
||||
require(
|
||||
root.is_dir()
|
||||
and root.resolve() == root,
|
||||
"Invalid release directory",
|
||||
)
|
||||
|
||||
revision = self.safe_file(
|
||||
root, "REVISION"
|
||||
).read_text().strip()
|
||||
|
||||
require(
|
||||
re.fullmatch(
|
||||
r"[a-f0-9]{40}", revision
|
||||
) is not None,
|
||||
"Invalid release revision",
|
||||
)
|
||||
|
||||
require(
|
||||
self.git(
|
||||
"cat-file",
|
||||
"-t",
|
||||
revision,
|
||||
).strip() == b"commit",
|
||||
"Release revision is not a Git commit",
|
||||
)
|
||||
|
||||
raw = self.git(
|
||||
"ls-tree",
|
||||
"-r",
|
||||
"-z",
|
||||
revision,
|
||||
)
|
||||
|
||||
entries = {}
|
||||
|
||||
for entry in raw.split(b"\x00"):
|
||||
|
||||
if not entry:
|
||||
continue
|
||||
|
||||
metadata, name = entry.split(b"\t", 1)
|
||||
|
||||
mode, kind, object_id = metadata.decode().split()
|
||||
|
||||
name = name.decode("utf-8")
|
||||
|
||||
entries[name] = {
|
||||
"mode": mode,
|
||||
"kind": kind,
|
||||
"object_id": object_id,
|
||||
}
|
||||
|
||||
migrations = sorted(
|
||||
name for name in entries
|
||||
if name.startswith("database/migrations/")
|
||||
and name.endswith(".php")
|
||||
)
|
||||
|
||||
require(
|
||||
self.migration_inventory(root) == migrations,
|
||||
"Migration inventory differs from Git revision",
|
||||
)
|
||||
|
||||
verified = {}
|
||||
|
||||
for name in (*migrations, *DEPENDENCIES):
|
||||
|
||||
require(
|
||||
name in entries,
|
||||
f"File missing from Git revision: {name}",
|
||||
)
|
||||
|
||||
entry = entries[name]
|
||||
|
||||
require(
|
||||
entry["kind"] == "blob"
|
||||
and entry["mode"] in ("100644", "100755"),
|
||||
f"Unsafe Git file type: {name}",
|
||||
)
|
||||
|
||||
expected = self.git(
|
||||
"cat-file",
|
||||
"blob",
|
||||
entry["object_id"],
|
||||
)
|
||||
|
||||
path = self.safe_file(root, name)
|
||||
|
||||
actual = path.read_bytes()
|
||||
|
||||
require(
|
||||
digest(actual) == digest(expected),
|
||||
f"Release file differs from Git revision: {name}",
|
||||
)
|
||||
|
||||
verified[name] = digest(actual)
|
||||
|
||||
result = {
|
||||
"format_version": 1,
|
||||
"revision": revision,
|
||||
"migrations": {
|
||||
name: verified[name]
|
||||
for name in migrations
|
||||
},
|
||||
"dependencies": {
|
||||
name: verified[name]
|
||||
for name in DEPENDENCIES
|
||||
},
|
||||
}
|
||||
|
||||
canonical = json.dumps(
|
||||
result,
|
||||
sort_keys=True,
|
||||
separators=(",", ":"),
|
||||
).encode()
|
||||
|
||||
result["fingerprint"] = digest(canonical)
|
||||
|
||||
return result
|
||||
Reference in New Issue
Block a user