fix(ci): verify tracked zero-replica declarations and classify Compose normalization safely

This commit is contained in:
victorsteele428 committed 2026-11-23 13:09:49 +00:00
1 parent 798f878644
commit 45a2edb854
1 file changed
+106 -4
+106 -4
View File
@@ -173,6 +173,72 @@ def diagnose_normalized_runtime_fields(c: object) -> None:
)
def verify_tracked_zero_replica_fixture() -> None:
"""Prove every one of the five unchanged synthetic source blocks says zero.
Compose may normalize a numeric zero away. Never infer that an absent
normalized key is safe unless the exact checked-in source proves it.
This tiny indentation check is intentionally stricter than general YAML.
Docker Compose config separately parses/normalizes the complete fixture.
"""
raw = (ROOT / COMPOSE_PATH).read_text(encoding="utf-8")
check(raw.count("\nservices:\n") == 1 and
raw.count("\nnetworks:\n") == 1, "SOURCE_ZERO_REPLICA_STRUCTURE")
section = raw.split("\nservices:\n", 1)[1].split("\nnetworks:\n", 1)[0]
blocks = re.split(r"(?m)^ ([a-z][a-z0-9-]*):\s*\n", section)
check(len(blocks) == 1 + 2 * len(ENVS) and
not blocks[0].strip(), "SOURCE_ZERO_REPLICA_SERVICES")
names = blocks[1::2]
check(len(names) == len(ENVS) and set(names) == set(ENVS),
"SOURCE_ZERO_REPLICA_SERVICES")
for block in blocks[2::2]:
check(len(re.findall(r"(?m)^ deploy:\s*$", block)) == 1 and
len(re.findall(r"(?m)^ replicas:\s*0\s*$", block)) == 1 and
len(re.findall(r"(?m)^\s+replicas\s*:", block)) == 1 and
re.search(r"(?m)^ deploy:\s*\n replicas:\s*0\s*$", block)
is not None, "SOURCE_ZERO_REPLICA_DECLARATION")
print("ZITPLANE_P08_SOURCE_ALL_FIVE_REPLICAS_ZERO_PASS", flush=True)
def diagnose_normalized_deploy(c: object) -> None:
"""Known service IDs and fixed safe shape classes, never field values."""
if type(c) is not dict or type(c.get("services")) is not dict:
return
for name in ENVS:
svc = c["services"].get(name)
if type(svc) is not dict:
continue
prefix = "ZITPLANE_P08_NORMALIZED_DEPLOY_" + name.replace("-", "_").upper()
if "deploy" not in svc:
print(prefix + "_MISSING", flush=True)
continue
deploy = svc["deploy"]
if deploy is None:
print(prefix + "_NULL", flush=True)
continue
if type(deploy) is not dict:
print(prefix + "_UNEXPECTED_TYPE", flush=True)
continue
print(prefix + ("_EMPTY_OBJECT" if not deploy else "_OBJECT"), flush=True)
if set(deploy) - {"replicas"}:
print(prefix + "_UNEXPECTED_FIELDS_PRESENT", flush=True)
if "replicas" not in deploy:
print(prefix + "_REPLICAS_MISSING", flush=True)
continue
replicas = deploy["replicas"]
if type(replicas) is int:
state = "INT_ZERO" if replicas == 0 else "INT_NONZERO"
elif replicas is None:
state = "NULL"
elif type(replicas) is bool:
state = "BOOL"
elif type(replicas) is str:
state = "STRING_ZERO" if replicas == "0" else "STRING_OTHER"
else:
state = "OTHER_TYPE"
print(prefix + "_REPLICAS_" + state, flush=True)
def service_secret_refs(service: dict) -> list[str]:
entries = service.get("secrets", [])
check(type(entries) is list, "SERVICE_SECRET_REFS")
@@ -251,9 +317,22 @@ def validate_compose(c: object, p: dict) -> None:
"postgres": "postgres"}[name] + ":never-deploy",
"SYNTHETIC_IMAGE")
deploy = service.get("deploy")
check(type(deploy) is dict and set(deploy) == {"replicas"} and
type(deploy["replicas"]) is int and deploy["replicas"] == 0,
"DEPLOY_DISABLED")
# Source verification (once before validation) independently requires
# the exact checked-in YAML's five zero replica declarations. If
# Compose JSON omits zero through normalization, only an absent/null
# deploy or an empty mapping is compatible with that proven source.
# Any explicit replicas value MUST be a real JSON integer zero.
if deploy is None:
check("deploy" not in service or service["deploy"] is None,
"DEPLOY_DISABLED")
else:
check(type(deploy) is dict and
set(deploy).issubset({"replicas"}), "DEPLOY_DISABLED")
if "replicas" in deploy:
check(type(deploy["replicas"]) is int and
deploy["replicas"] == 0, "DEPLOY_DISABLED")
# An omitted deploy section is never generally safe: validation
# is allowed only after verify_tracked_zero_replica_fixture() ran.
snet = service.get("networks")
check((type(snet) is dict and set(snet) == {"isolated"}) or
snet == ["isolated"], "SERVICE_NETWORK")
@@ -300,7 +379,7 @@ def negative_cases(p: dict, c: dict) -> None:
("PLAINTEXT_SECRET", "INLINE_SECRET", lambda p,c: c["services"]["account"]["environment"].__setitem__("AUTH_SECRET", "dummy-not-a-real-secret")),
("DOCKER_SOCKET", "DOCKER_SOCKET", lambda p,c: c["services"]["ingress-policy"].__setitem__("volumes", ["/var/run/docker.sock:/var/run/docker.sock:ro"])),
("PRIVILEGED", "PRIVILEGED", lambda p,c: c["services"]["zitadel-api"].__setitem__("privileged", True)),
("ACTIVATE_REPLICAS", "DEPLOY_DISABLED", lambda p,c: c["services"]["zitadel-login"]["deploy"].__setitem__("replicas", 1)),
("ACTIVATE_REPLICAS", "DEPLOY_DISABLED", lambda p,c: c["services"]["zitadel-login"].__setitem__("deploy", {"replicas": 1})),
("EXTERNAL_NETWORK", "NETWORK_PRIVATE", lambda p,c: c["networks"]["isolated"].__setitem__("internal", False)),
("DEPLOYABLE_IMAGE", "SYNTHETIC_IMAGE", lambda p,c: c["services"]["zitadel-api"].__setitem__("image", "ghcr.io/zitadel/zitadel:v4.20.4")),
]
@@ -355,6 +434,26 @@ def negative_cases(p: dict, c: dict) -> None:
+ "_REJECTED_PASS", flush=True)
print("ZITPLANE_P08_ALL_4_RUNTIME_NEGATIVE_CASES_PASS", flush=True)
# Different hostile replica encodings must not be reinterpreted as zero.
replica_cases = [
("STRING_NONZERO", {"replicas": "1"}),
("BOOL_FALSE", {"replicas": False}),
("EXTRA_DEPLOY_KEY", {"replicas": 0, "mode": "global"}),
]
for label, deploy_value in replica_cases:
bad_compose = copy.deepcopy(c)
bad_compose["services"]["postgres"]["deploy"] = deploy_value
try:
validate_all(p, bad_compose)
except ContractError as error:
check(error.code == "DEPLOY_DISABLED",
"REPLICA_NEGATIVE_WRONG_CLASS_" + label)
else:
raise ContractError("REPLICA_NEGATIVE_NOT_REJECTED_" + label)
print("ZITPLANE_P08_REPLICA_NEGATIVE_" + label
+ "_REJECTED_PASS", flush=True)
print("ZITPLANE_P08_ALL_3_REPLICA_NEGATIVE_CASES_PASS", flush=True)
def workflow_safety() -> None:
body = WORKFLOW_PATH.read_text(encoding="utf-8")
@@ -391,8 +490,11 @@ def main() -> int:
return 0
policy = json.loads(POLICY_PATH.read_text(encoding="utf-8"))
compose = json.load(sys.stdin)
verify_tracked_zero_replica_fixture()
diagnose_normalized_runtime_fields(compose)
diagnose_normalized_deploy(compose)
validate_all(policy, compose)
print("ZITPLANE_P08_NORMALIZED_ZERO_REPLICA_POLICY_PASS", flush=True)
print("ZITPLANE_P08_NORMALIZED_RUNTIME_DEFAULTS_POLICY_PASS", flush=True)
print("ZITPLANE_P08_CANONICAL_HTTPS_ISSUER_AND_CALLBACK_PASS", flush=True)
print("ZITPLANE_P08_LOGIN_CONSOLE_API_H2C_PRIVATE_ROUTING_PASS", flush=True)