362 lines
14 KiB
Python
362 lines
14 KiB
Python
"""Offline contracts for the isolated GCP leoclean no-send service image."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import copy
|
|
import json
|
|
import os
|
|
import shutil
|
|
import subprocess
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from ops import gcp_leoclean_nosend_package as package
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
REVISION = "1" * 40
|
|
IMAGE_DIGEST = "2" * 64
|
|
CLAIM_CEILING = "Offline package proof only; live GCP identity, database access, restart, and parity remain unproven."
|
|
|
|
|
|
def write_json(path: Path, value: object, mode: int = 0o600) -> None:
|
|
path.write_text(json.dumps(value, indent=2, sort_keys=True) + "\n", encoding="utf-8")
|
|
path.chmod(mode)
|
|
|
|
|
|
def artifact_fixture(tmp_path: Path) -> tuple[Path, Path]:
|
|
artifact = tmp_path / "artifact"
|
|
artifact.mkdir()
|
|
(artifact / "bootstrap.py").write_text("#!/usr/bin/env python3\n", encoding="utf-8")
|
|
(artifact / "bootstrap.py").chmod(0o755)
|
|
write_json(
|
|
artifact / "runtime-lock.json",
|
|
{
|
|
"base_images": {
|
|
"python": package.PYTHON_IMAGE.removeprefix("docker.io/library/"),
|
|
"uv": package.UV_IMAGE,
|
|
},
|
|
"hermes": {
|
|
"commit": package.HERMES_COMMIT,
|
|
"uv_lock_sha256": "3" * 64,
|
|
},
|
|
},
|
|
mode=0o644,
|
|
)
|
|
profile = artifact / "profile-template"
|
|
profile.mkdir()
|
|
(profile / "config.yaml").write_text("model: fixture\n", encoding="utf-8")
|
|
(profile / "config.yaml").chmod(0o600)
|
|
content = package.tree_manifest(artifact)
|
|
manifest = {
|
|
"schema": package.ARTIFACT_MANIFEST_SCHEMA,
|
|
"teleo_git_head": REVISION,
|
|
"hermes_commit": package.HERMES_COMMIT,
|
|
"claim_ceiling": CLAIM_CEILING,
|
|
"content": content,
|
|
}
|
|
write_json(artifact / "artifact-manifest.json", manifest, mode=0o644)
|
|
receipt = tmp_path / "receipt.json"
|
|
write_json(
|
|
receipt,
|
|
{
|
|
"schema": package.ARTIFACT_RECEIPT_SCHEMA,
|
|
"status": "pass",
|
|
"verification_mode": "release",
|
|
"teleo_git_head": REVISION,
|
|
"artifact_sha256": content["sha256"],
|
|
"hermes_commit": package.HERMES_COMMIT,
|
|
"hermes_source_sha256": "4" * 64,
|
|
"claim_ceiling": CLAIM_CEILING,
|
|
},
|
|
mode=0o644,
|
|
)
|
|
return artifact, receipt
|
|
|
|
|
|
def identity_fixture(tmp_path: Path) -> Path:
|
|
identity = tmp_path / "identity"
|
|
identity.mkdir(parents=True)
|
|
soul = identity / "SOUL.md"
|
|
soul.write_text("# Leo fixture\n", encoding="utf-8")
|
|
soul.chmod(0o600)
|
|
stable = {
|
|
"schema": package.IDENTITY_MANIFEST_SCHEMA,
|
|
"identity_inputs": {
|
|
"canonical_database": {
|
|
"authority": "synthetic_noncanonical_fixture",
|
|
"canonical_authority_granted": False,
|
|
}
|
|
},
|
|
"identity_inputs_sha256": "5" * 64,
|
|
"compiled_views": {
|
|
"SOUL.md": {"sha256": package.sha256_file(soul)},
|
|
"identity.json": {"sha256": "6" * 64},
|
|
},
|
|
}
|
|
manifest = {**stable, "manifest_sha256": package.canonical_sha256(stable)}
|
|
write_json(identity / "identity-manifest.json", manifest)
|
|
write_json(
|
|
identity / "identity-lock.json",
|
|
{
|
|
"schema": package.IDENTITY_LOCK_SCHEMA,
|
|
"manifest_sha256": manifest["manifest_sha256"],
|
|
"identity_inputs_sha256": manifest["identity_inputs_sha256"],
|
|
"compiled_views": {
|
|
"SOUL.md": {"sha256": package.sha256_file(soul)},
|
|
"identity.json": {"sha256": "6" * 64},
|
|
},
|
|
"session_boundary": {"classification": "temporary_noncanonical"},
|
|
},
|
|
)
|
|
return identity
|
|
|
|
|
|
def image_input_fixture(tmp_path: Path) -> dict[str, object]:
|
|
artifact, receipt = artifact_fixture(tmp_path)
|
|
identity = identity_fixture(tmp_path)
|
|
return package.build_image_input(
|
|
artifact=artifact,
|
|
receipt=receipt,
|
|
identity=identity,
|
|
ca=ROOT / "ops" / "gcp-teleo-pgvector-standby-server-ca.pem",
|
|
source_revision=REVISION,
|
|
dockerfile=ROOT / "docker" / "leoclean-nosend" / "Dockerfile",
|
|
entrypoint=ROOT / "docker" / "leoclean-nosend" / "entrypoint.py",
|
|
package_contract=ROOT / "ops" / "gcp_leoclean_nosend_package.py",
|
|
)
|
|
|
|
|
|
def test_image_input_binds_exact_runtime_identity_and_staging_target(tmp_path: Path) -> None:
|
|
value = image_input_fixture(tmp_path)
|
|
package.validate_image_input(value)
|
|
|
|
assert value["target"] == {
|
|
"project": "teleo-501523",
|
|
"zone": "europe-west6-a",
|
|
"instance": "teleo-staging-1",
|
|
"service": "leoclean-gcp-nosend.service",
|
|
"service_account": "sa-teleo-staging-vm@teleo-501523.iam.gserviceaccount.com",
|
|
"platform": "linux/amd64",
|
|
}
|
|
assert value["runtime"]["python_version"] == "3.11.9"
|
|
assert value["runtime"]["postgres_version"] == "16.14"
|
|
assert value["identity"]["canonical_authority_granted"] is False
|
|
assert value["base_images"]["postgres"].endswith(
|
|
"@sha256:92620daddcd947f8d5ab5ba66e848702fe443d87fed30c4cea8e389fd78dfc55"
|
|
)
|
|
|
|
|
|
def test_artifact_requires_release_receipt_exact_revision_and_unchanged_content(tmp_path: Path) -> None:
|
|
artifact, receipt = artifact_fixture(tmp_path)
|
|
package.validate_artifact(artifact, receipt, REVISION)
|
|
|
|
development = json.loads(receipt.read_text(encoding="utf-8"))
|
|
development["verification_mode"] = "development"
|
|
write_json(receipt, development, mode=0o644)
|
|
with pytest.raises(package.PackageError, match="not release"):
|
|
package.validate_artifact(artifact, receipt, REVISION)
|
|
|
|
development["verification_mode"] = "release"
|
|
write_json(receipt, development, mode=0o644)
|
|
(artifact / "bootstrap.py").write_text("mutated\n", encoding="utf-8")
|
|
(artifact / "bootstrap.py").chmod(0o755)
|
|
with pytest.raises(package.PackageError, match="content manifest drifted"):
|
|
package.validate_artifact(artifact, receipt, REVISION)
|
|
|
|
|
|
def test_identity_bundle_rejects_drift_extra_files_and_false_authority(tmp_path: Path) -> None:
|
|
identity = identity_fixture(tmp_path)
|
|
package.validate_identity_bundle(identity)
|
|
|
|
(identity / "SOUL.md").write_text("drift\n", encoding="utf-8")
|
|
(identity / "SOUL.md").chmod(0o600)
|
|
with pytest.raises(package.PackageError, match="SOUL content differs"):
|
|
package.validate_identity_bundle(identity)
|
|
|
|
identity = identity_fixture(tmp_path / "second")
|
|
(identity / "extra.txt").write_text("extra\n", encoding="utf-8")
|
|
with pytest.raises(package.PackageError, match="entry set"):
|
|
package.validate_identity_bundle(identity)
|
|
|
|
identity = identity_fixture(tmp_path / "third")
|
|
manifest_path = identity / "identity-manifest.json"
|
|
manifest = json.loads(manifest_path.read_text(encoding="utf-8"))
|
|
manifest["identity_inputs"]["canonical_database"] = {
|
|
"authority": "synthetic_noncanonical_fixture",
|
|
"canonical_authority_granted": True,
|
|
}
|
|
stable = {key: value for key, value in manifest.items() if key != "manifest_sha256"}
|
|
manifest["manifest_sha256"] = package.canonical_sha256(stable)
|
|
write_json(manifest_path, manifest)
|
|
lock_path = identity / "identity-lock.json"
|
|
lock = json.loads(lock_path.read_text(encoding="utf-8"))
|
|
lock["manifest_sha256"] = manifest["manifest_sha256"]
|
|
write_json(lock_path, lock)
|
|
with pytest.raises(package.PackageError, match="authority"):
|
|
package.validate_identity_bundle(identity)
|
|
|
|
|
|
def test_release_requires_exact_digest_repository_and_strict_schema(tmp_path: Path) -> None:
|
|
image_input = image_input_fixture(tmp_path)
|
|
reference = f"{package.IMAGE_REPOSITORY}@sha256:{IMAGE_DIGEST}"
|
|
release = package.build_release_descriptor(image_input, reference)
|
|
package.validate_release_descriptor(release)
|
|
|
|
assert release["image"]["reference"] == reference
|
|
assert release["image"]["digest"] == f"sha256:{IMAGE_DIGEST}"
|
|
assert release["image"]["platform"] == "linux/amd64"
|
|
|
|
with pytest.raises(package.PackageError, match="sha256 digest"):
|
|
package.build_release_descriptor(image_input, f"{package.IMAGE_REPOSITORY}:latest")
|
|
with pytest.raises(package.PackageError, match="staging repository"):
|
|
package.build_release_descriptor(
|
|
image_input,
|
|
f"europe-west6-docker.pkg.dev/teleo-501523/teleo/other@sha256:{IMAGE_DIGEST}",
|
|
)
|
|
|
|
extra = copy.deepcopy(release)
|
|
extra["unexpected"] = True
|
|
with pytest.raises(package.PackageError, match="fields are not exact"):
|
|
package.validate_release_descriptor(extra)
|
|
|
|
wrong_label = copy.deepcopy(release)
|
|
wrong_label["image"]["labels"]["livingip.artifact.sha256"] = "9" * 64
|
|
stable = {key: value for key, value in wrong_label.items() if key != "release_sha256"}
|
|
wrong_label["release_sha256"] = package.canonical_sha256(stable)
|
|
with pytest.raises(package.PackageError, match="labels are not exact"):
|
|
package.validate_release_descriptor(wrong_label)
|
|
|
|
|
|
def test_rendered_unit_is_isolated_digest_pinned_and_has_no_credential_or_transport_surface(tmp_path: Path) -> None:
|
|
image_input = image_input_fixture(tmp_path)
|
|
release = package.build_release_descriptor(
|
|
image_input,
|
|
f"{package.IMAGE_REPOSITORY}@sha256:{IMAGE_DIGEST}",
|
|
)
|
|
unit = package.render_systemd_unit(release)
|
|
|
|
assert "leoclean-gcp-nosend" in unit
|
|
assert f"@sha256:{IMAGE_DIGEST}" in unit
|
|
assert "--pull=never" in unit
|
|
assert "--read-only" in unit
|
|
assert "--cap-drop=ALL" in unit
|
|
assert "--cap-add=CHOWN" in unit
|
|
assert "--cap-add=SETGID" in unit
|
|
assert "--cap-add=SETUID" in unit
|
|
assert "--cap-add=SETPCAP" in unit
|
|
assert "--network=bridge" in unit
|
|
assert "--tmpfs=/run/leoclean-profile:" in unit
|
|
assert "--tmpfs=/var/lib/postgresql/data:ro,noexec,nosuid,nodev,size=64k,uid=0,gid=0,mode=0000" in unit
|
|
assert "NoNewPrivileges=yes" in unit
|
|
assert "EnvironmentFile" not in unit
|
|
assert "LoadCredential" not in unit
|
|
assert "docker.sock" not in unit
|
|
assert "--publish" not in unit
|
|
assert "teleo-prod-1" not in unit
|
|
assert "77.42.65.182" not in unit
|
|
assert "telegram" not in unit.casefold()
|
|
|
|
|
|
def test_prepare_context_is_secret_free_normalized_and_idempotently_refuses_overwrite(tmp_path: Path) -> None:
|
|
artifact, receipt = artifact_fixture(tmp_path)
|
|
identity = identity_fixture(tmp_path)
|
|
output = tmp_path / "context"
|
|
result = package.prepare_context(
|
|
repo_root=ROOT,
|
|
artifact=artifact,
|
|
receipt=receipt,
|
|
identity=identity,
|
|
output=output,
|
|
source_revision=REVISION,
|
|
)
|
|
|
|
package.validate_image_input(result)
|
|
assert sorted(path.name for path in output.iterdir()) == [
|
|
"Dockerfile",
|
|
"artifact",
|
|
"artifact-receipt.json",
|
|
"build-args.json",
|
|
"cloudsql-server-ca.pem",
|
|
"entrypoint.py",
|
|
"identity",
|
|
"image-input.json",
|
|
"package_contract.py",
|
|
]
|
|
assert all((output / "identity" / name).stat().st_mode & 0o777 == 0o600 for name in package.IDENTITY_FILES)
|
|
encoded = (output / "image-input.json").read_text(encoding="utf-8").casefold()
|
|
assert "telegram_bot_token" not in encoded
|
|
assert "postgres-password" not in encoded
|
|
|
|
with pytest.raises(package.PackageError, match="already exists"):
|
|
package.prepare_context(
|
|
repo_root=ROOT,
|
|
artifact=artifact,
|
|
receipt=receipt,
|
|
identity=identity,
|
|
output=output,
|
|
source_revision=REVISION,
|
|
)
|
|
|
|
|
|
def test_dockerfile_has_only_digest_pinned_bases_and_no_floating_package_install() -> None:
|
|
dockerfile = (ROOT / "docker" / "leoclean-nosend" / "Dockerfile").read_text(encoding="utf-8")
|
|
from_lines = [line for line in dockerfile.splitlines() if line.startswith("FROM ")]
|
|
|
|
assert len(from_lines) == 3
|
|
assert all("@sha256:" in line for line in from_lines)
|
|
assert "postgres:16.14-bookworm@sha256:92620d" in dockerfile
|
|
assert "python:3.11.9-slim-bookworm@sha256:8fb099" in dockerfile
|
|
assert "uv:0.9.30@sha256:538e0b" in dockerfile
|
|
assert "apt-get" not in dockerfile
|
|
assert "pip install" not in dockerfile
|
|
assert "uv sync" in dockerfile
|
|
assert "--frozen" in dockerfile
|
|
assert "--no-editable" in dockerfile
|
|
assert "HEALTHCHECK" in dockerfile
|
|
assert "verify-build" in dockerfile
|
|
|
|
|
|
def test_entrypoint_prepares_exact_profile_drops_privileges_and_health_checks_pid_one() -> None:
|
|
source = (ROOT / "docker" / "leoclean-nosend" / "entrypoint.py").read_text(encoding="utf-8")
|
|
|
|
assert "os.setgroups([])" in source
|
|
assert "os.setgid(contract.RUNTIME_GID)" in source
|
|
assert "os.setuid(contract.RUNTIME_UID)" in source
|
|
assert "pr_capbset_drop = 24" in source
|
|
assert "reverse=True" in source
|
|
assert source.index("os.chown(runtime_identity, 0, 0)") < source.index(
|
|
"os.chown(PROFILE, contract.RUNTIME_UID, contract.RUNTIME_GID)"
|
|
)
|
|
assert 'value.get("process_id") == 1' in source
|
|
assert "expected_binfmt = [expected_native[0], *expected_native]" in source
|
|
assert "cmdline in (expected_native, expected_binfmt)" in source
|
|
assert 'surface.get("send_message_present") is False' in source
|
|
assert "gateway_adapter_count" in source
|
|
assert "os.execve" in source
|
|
assert "OPENROUTER_API_KEY" not in source
|
|
assert "TELEGRAM_BOT_TOKEN" not in source
|
|
|
|
|
|
def test_entrypoint_loads_adjacent_contract_with_python_safe_path(tmp_path: Path) -> None:
|
|
image_root = tmp_path / "image"
|
|
image_root.mkdir()
|
|
entrypoint = image_root / "entrypoint.py"
|
|
shutil.copy2(ROOT / "docker" / "leoclean-nosend" / "entrypoint.py", entrypoint)
|
|
shutil.copy2(ROOT / "ops" / "gcp_leoclean_nosend_package.py", image_root / "package_contract.py")
|
|
environment = {**os.environ, "PYTHONSAFEPATH": "1"}
|
|
|
|
result = subprocess.run(
|
|
[sys.executable, "-P", str(entrypoint), "verify"],
|
|
cwd=tmp_path,
|
|
env=environment,
|
|
check=False,
|
|
capture_output=True,
|
|
text=True,
|
|
timeout=10,
|
|
)
|
|
|
|
assert result.returncode == 65
|
|
assert json.loads(result.stderr) == {"error": "container_contract_failed", "status": "fail"}
|
|
assert "Traceback" not in result.stderr
|