docs(P42): merge phase 42 — stub implementation
---ci--- project: acdl phase: 42 milestone: v1.9 status: execute ---/ci--- Merged phase/42-stub-implementation into main. REQ-107..111 satisfied. 493 tests pass; run_ci.sh + run_platform.sh green.
This commit is contained in:
@@ -606,3 +606,17 @@ also closes P1-1 (adapter hardcoded defaults, deferred from v1.2).
|
||||
- Consumer guide documents per-env caller workflows + promotion-without-editing + HITL gates + interpolation reference.
|
||||
- `tests/test_per_env_contracts.py` + `tests/test_deploy_workflow_env_input.py` + `tests/test_consumer_guide_per_env_section.py` pass.
|
||||
- `pytest` 446 (was 406, +40); `run_ci.sh` exits 0; both deploy workflows byte-identical.
|
||||
|
||||
### Phase 42 — stub-implementation
|
||||
- **Description:** `route_halt_artifact` real (SNS publish + outbox fallback, REQ-107) + SNS topic `acdl-sod-halt` in `terraform/platform/main.tf`. HITL attestation gates (`core/hitl_gates.py`, REQ-108) — records approver to outbox, runs SoD on prod, invokes the attestation matrix; `run_platform.sh` calls `attest` before apply for qa/prod/dr (dev skips). 8-concern attestation matrix (`core/attestation_matrix.py`, REQ-109, D-084) — offline-testable concerns run for real; operator-supplied concerns accept signed evidence artifacts validated for freshness + schema; signature skip when `ACDL_ATTESTATION_SIGNING_KEY_ID` unset (D-089). Wiz real API client (`WizClient`, REQ-110) — GraphQL queries + pagination + graceful degrade. Kyverno translator fleshed out (REQ-111) — full PolicyReport mapping + skip-with-reason + inactive-for-TF guard + `--kube-version` stub.
|
||||
- **Status:** complete (v1.8.4)
|
||||
- **Depends on:** [41]
|
||||
- **Requirements:** REQ-107, REQ-108, REQ-109, REQ-110, REQ-111
|
||||
- **Success Criteria:**
|
||||
- `route_halt_artifact` publishes to SNS when ARN set; outbox fallback when unset; SNS topic in Terraform.
|
||||
- `hitl_gates.attest` records approver; SoD blocks on identity equality; dev skips; `run_platform.sh` has the HITL step.
|
||||
- `attestation_matrix.check` runs 8 concerns; offline concerns pass; operator-supplied missing → block for prod; expired → block; signature skip when key unset.
|
||||
- Wiz `WizClient` real client + pagination + graceful degrade; `fetch_and_adapt` translates.
|
||||
- Kyverno full mapping (pass/fail/skip/warn + severity + skip-with-reason + resource construction); inactive guard preserved; `--kube-version` parsed.
|
||||
- `tests/test_route_halt_artifact.py` + `test_hitl_gates.py` + `test_attestation_matrix.py` + `test_wiz_adapter_real_client.py` + expanded `test_kyverno_adapter.py` pass.
|
||||
- `pytest` 493 (was 446, +47); `run_ci.sh` exits 0; `run_platform.sh --check-only` exits 0.
|
||||
|
||||
@@ -4,16 +4,22 @@ Kyverno is a Kubernetes-native policy engine. It evaluates K8s manifests
|
||||
and produces PolicyReport resources. This adapter translates those results
|
||||
to the normalized PolicyCheckResult schema (engine: "kyverno").
|
||||
|
||||
D-053: the platform emits Terraform, not K8s manifests. This adapter is
|
||||
ready but inactive for Terraform-only stacks. It activates when the GitOps
|
||||
reconciler (roadmap) emits K8s manifests. Sample policies are included as
|
||||
documentation at adapters/kyverno/policies/.
|
||||
v1.9 (REQ-111): the translator is fleshed out — full PolicyReport →
|
||||
PolicyCheckResult mapping with severity + skip-with-reason handling. It
|
||||
remains inactive for Terraform-only stacks (guard preserved — emits a
|
||||
single SKIPPED `KYVERNO_INACTIVE_TF_STACK` record when no K8s manifests).
|
||||
A `--kube-version` stub is parsed but not yet used (for future GitOps).
|
||||
|
||||
CLI: kyverno_adapter.py <policyreport.json> <contract-id>
|
||||
D-053: the platform emits Terraform, not K8s manifests. This adapter
|
||||
activates when the GitOps reconciler (roadmap) emits K8s manifests.
|
||||
Sample policies are included as documentation at adapters/kyverno/policies/.
|
||||
|
||||
CLI: kyverno_adapter.py <policyreport.json> <contract-id> [--kube-version <ver>]
|
||||
"""
|
||||
|
||||
import datetime
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
|
||||
|
||||
@@ -23,14 +29,17 @@ SEVERITY_MAP = {
|
||||
"medium": "medium",
|
||||
"low": "low",
|
||||
"info": "info",
|
||||
"informational": "info",
|
||||
}
|
||||
|
||||
RESULT_MAP = {
|
||||
"pass": "pass",
|
||||
"fail": "fail",
|
||||
"warn": "skipped",
|
||||
"warning": "skipped",
|
||||
"error": "error",
|
||||
"skip": "skipped",
|
||||
"skipped": "skipped",
|
||||
}
|
||||
|
||||
|
||||
@@ -43,39 +52,85 @@ def _to_pcr(entry, contract_id):
|
||||
severity = SEVERITY_MAP.get(str(severity_raw).lower(), "info")
|
||||
result_raw = entry.get("result", "skip")
|
||||
result = RESULT_MAP.get(str(result_raw).lower(), "error")
|
||||
# Skip-with-reason: a skipped result carries a message that explains why.
|
||||
message = entry.get("message", "")
|
||||
if result == "skipped" and not message:
|
||||
message = entry.get("skipReason", entry.get("skippedMessage", "skipped (no reason)"))
|
||||
policy = entry.get("policy", "")
|
||||
rule = entry.get("rule", "")
|
||||
rule_id = f"{policy}/{rule}" if rule else (policy or "KYVERNO_UNKNOWN")
|
||||
resource = entry.get("resource", "")
|
||||
if not resource and entry.get("name"):
|
||||
# Construct a resource ref from kind/name/namespace when present.
|
||||
kind = entry.get("kind", "")
|
||||
ns = entry.get("namespace", "")
|
||||
resource = f"{kind}/{ns}/{entry.get('name')}" if kind else entry.get("name", "")
|
||||
return {
|
||||
"contractId": contract_id,
|
||||
"evaluatedAt": _iso8601_now(),
|
||||
"engine": "kyverno",
|
||||
"ruleId": entry.get("policy", "KYVERNO_UNKNOWN"),
|
||||
"ruleId": rule_id,
|
||||
"severity": severity,
|
||||
"result": result,
|
||||
"message": entry.get("message", ""),
|
||||
"message": message,
|
||||
"evidence": {
|
||||
"resource": entry.get("resource", ""),
|
||||
"resource": resource,
|
||||
"namespace": entry.get("namespace", ""),
|
||||
"kind": entry.get("kind", ""),
|
||||
"name": entry.get("name", ""),
|
||||
"policy": policy,
|
||||
"rule": rule,
|
||||
},
|
||||
"resourceRef": entry.get("resource", ""),
|
||||
"resourceRef": resource,
|
||||
}
|
||||
|
||||
|
||||
def adapt(policyreport_json_path, contract_id):
|
||||
def _emit_inactive_tf(contract_id):
|
||||
"""Emit a SKIPPED record when the platform emits Terraform, not K8s manifests."""
|
||||
return {
|
||||
"contractId": contract_id,
|
||||
"evaluatedAt": _iso8601_now(),
|
||||
"engine": "kyverno",
|
||||
"ruleId": "KYVERNO_INACTIVE_TF_STACK",
|
||||
"severity": "info",
|
||||
"result": "skipped",
|
||||
"message": "Kyverno inactive — the platform emits Terraform, not K8s manifests. Activates when the GitOps reconciler emits K8s manifests (D-053).",
|
||||
"evidence": {},
|
||||
"resourceRef": "",
|
||||
}
|
||||
|
||||
|
||||
def adapt(policyreport_json_path, contract_id, kube_version=None):
|
||||
with open(policyreport_json_path, "r", encoding="utf-8") as fh:
|
||||
data = json.load(fh)
|
||||
out = []
|
||||
# Kyverno PolicyReport has a .results[] array
|
||||
# Kyverno PolicyReport has a .results[] array.
|
||||
results = data.get("results", [])
|
||||
if not isinstance(results, list):
|
||||
results = []
|
||||
for entry in results:
|
||||
out.append(_to_pcr(entry, contract_id))
|
||||
if not out:
|
||||
out.append(_emit_inactive_tf(contract_id))
|
||||
# kube_version is parsed but not yet used (future GitOps reconciler).
|
||||
_ = kube_version
|
||||
return out
|
||||
|
||||
|
||||
def adapt_inactive(contract_id):
|
||||
"""Convenience: emit the inactive-for-TF record directly (no report file)."""
|
||||
return [_emit_inactive_tf(contract_id)]
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
if len(sys.argv) != 3:
|
||||
print("usage: kyverno_adapter.py <policyreport.json> <contract-id>", file=sys.stderr)
|
||||
kube_ver = None
|
||||
args = sys.argv[1:]
|
||||
if "--kube-version" in args:
|
||||
idx = args.index("--kube-version")
|
||||
if idx + 1 < len(args):
|
||||
kube_ver = args[idx + 1]
|
||||
args = args[:idx] + args[idx + 2:]
|
||||
if len(args) != 2:
|
||||
print("usage: kyverno_adapter.py <policyreport.json> <contract-id> [--kube-version <ver>]", file=sys.stderr)
|
||||
sys.exit(2)
|
||||
print(json.dumps(adapt(sys.argv[1], sys.argv[2]), indent=2))
|
||||
print(json.dumps(adapt(args[0], args[1], kube_version=kube_ver), indent=2))
|
||||
+105
-18
@@ -1,14 +1,16 @@
|
||||
"""Wiz adapter — translate Wiz API results to ACDL PolicyCheckResult records.
|
||||
|
||||
Wiz is a SaaS security platform with a REST API (issues, security graph
|
||||
queries). This adapter translates Wiz issue records to the normalized
|
||||
PolicyCheckResult schema (engine: "wiz"), matching the Checkov adapter
|
||||
pattern.
|
||||
Wiz is a SaaS security platform with a GraphQL API. This adapter
|
||||
translates Wiz issue records to the normalized PolicyCheckResult schema
|
||||
(engine: "wiz"), matching the Checkov adapter pattern.
|
||||
|
||||
D-052: stub + schema path. The adapter degrades gracefully when Wiz is
|
||||
not configured — it emits a single SKIPPED record (WIZ_NOT_CONFIGURED)
|
||||
so the confidence policy input stays non-empty. The pipeline invokes it
|
||||
optionally when WIZ_API_TOKEN is set.
|
||||
v1.9 (REQ-110): the adapter is a real API client. `WizClient` queries the
|
||||
Wiz GraphQL API (`<WIZ_API_URL>/graphql`, Bearer auth, `issues` query)
|
||||
and translates results → PolicyCheckResult records. It degrades
|
||||
gracefully (single `SKIPPED` `WIZ_NOT_CONFIGURED` record) when
|
||||
`WIZ_API_TOKEN` or `WIZ_API_URL` is unset (D-052). Pagination is handled
|
||||
via `pageInfo.hasNextPage` + `endCursor`. Offline tests use a recorded
|
||||
GraphQL fixture.
|
||||
|
||||
CLI: wiz_adapter.py <wiz_issues.json> <contract-id>
|
||||
"""
|
||||
@@ -24,6 +26,7 @@ SEVERITY_MAP = {
|
||||
"HIGH": "high",
|
||||
"MEDIUM": "medium",
|
||||
"LOW": "low",
|
||||
"INFORMATIONAL": "info",
|
||||
"INFO": "info",
|
||||
}
|
||||
|
||||
@@ -35,6 +38,24 @@ RESULT_MAP = {
|
||||
}
|
||||
|
||||
|
||||
_ISSUES_QUERY = """
|
||||
query IssuesQuery($filterBy: IssueFilter, $after: String) {
|
||||
issues(filterBy: $filterBy, after: $after) {
|
||||
nodes {
|
||||
id
|
||||
severity
|
||||
title
|
||||
status
|
||||
entity { id name type cloudPlatform }
|
||||
control { id name }
|
||||
createdAt
|
||||
}
|
||||
pageInfo { hasNextPage endCursor }
|
||||
}
|
||||
}
|
||||
"""
|
||||
|
||||
|
||||
def _iso8601_now():
|
||||
return datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")
|
||||
|
||||
@@ -44,22 +65,23 @@ def _to_pcr(wiz_issue, contract_id):
|
||||
severity = SEVERITY_MAP.get(str(severity_raw).upper(), "info")
|
||||
status = wiz_issue.get("status", "OPEN")
|
||||
result = RESULT_MAP.get(str(status).upper(), "error")
|
||||
control = wiz_issue.get("control", {})
|
||||
control = wiz_issue.get("control", {}) or {}
|
||||
entity = wiz_issue.get("entity", {}) or {}
|
||||
rule_id = control.get("name") or wiz_issue.get("id") or "WIZ_UNKNOWN"
|
||||
return {
|
||||
"contractId": contract_id,
|
||||
"evaluatedAt": _iso8601_now(),
|
||||
"engine": "wiz",
|
||||
"ruleId": wiz_issue.get("id", control.get("id", "WIZ_UNKNOWN")),
|
||||
"ruleId": rule_id,
|
||||
"severity": severity,
|
||||
"result": result,
|
||||
"message": wiz_issue.get("title", control.get("name", "")),
|
||||
"evidence": {
|
||||
"resource": wiz_issue.get("entity", {}).get("id"),
|
||||
"resource_name": wiz_issue.get("entity", {}).get("name"),
|
||||
"cloud_platform": wiz_issue.get("entity", {}).get("cloudPlatform"),
|
||||
"subscription_id": wiz_issue.get("entity", {}).get("subscriptionId"),
|
||||
"resource": entity.get("id"),
|
||||
"resource_name": entity.get("name"),
|
||||
"cloud_platform": entity.get("cloudPlatform"),
|
||||
},
|
||||
"resourceRef": wiz_issue.get("entity", {}).get("id", ""),
|
||||
"resourceRef": entity.get("id", ""),
|
||||
}
|
||||
|
||||
|
||||
@@ -71,19 +93,84 @@ def _emit_not_configured(contract_id):
|
||||
"ruleId": "WIZ_NOT_CONFIGURED",
|
||||
"severity": "info",
|
||||
"result": "skipped",
|
||||
"message": "Wiz adapter not configured (WIZ_API_TOKEN not set); degraded gracefully (D-052).",
|
||||
"message": "Wiz adapter not configured (WIZ_API_TOKEN or WIZ_API_URL not set); degraded gracefully (D-052).",
|
||||
"evidence": {},
|
||||
"resourceRef": "",
|
||||
}
|
||||
|
||||
|
||||
class WizClient:
|
||||
"""Real Wiz GraphQL API client (REQ-110).
|
||||
|
||||
Reads WIZ_API_TOKEN + WIZ_API_URL from the environment. `fetch_issues`
|
||||
queries the Wiz GraphQL API and returns a list of issue dicts.
|
||||
Pagination is handled via pageInfo.hasNextPage + endCursor.
|
||||
"""
|
||||
|
||||
def __init__(self, token=None, url=None):
|
||||
self.token = token or os.environ.get("WIZ_API_TOKEN", "")
|
||||
self.url = (url or os.environ.get("WIZ_API_URL", "")).rstrip("/")
|
||||
if not self.token or not self.url:
|
||||
raise RuntimeError("WizClient requires WIZ_API_TOKEN + WIZ_API_URL")
|
||||
|
||||
def _post(self, query, variables):
|
||||
import urllib.request
|
||||
endpoint = f"{self.url}/graphql"
|
||||
payload = json.dumps({"query": query, "variables": variables}).encode("utf-8")
|
||||
req = urllib.request.Request(
|
||||
endpoint,
|
||||
data=payload,
|
||||
headers={
|
||||
"Authorization": f"Bearer {self.token}",
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
method="POST",
|
||||
)
|
||||
with urllib.request.urlopen(req, timeout=30) as resp:
|
||||
return json.loads(resp.read().decode("utf-8"))
|
||||
|
||||
def fetch_issues(self, filter_by=None, max_pages=10):
|
||||
issues = []
|
||||
after = None
|
||||
for _ in range(max_pages):
|
||||
data = self._post(_ISSUES_QUERY, {"filterBy": filter_by or {}, "after": after})
|
||||
root = data.get("data", {}).get("issues", {})
|
||||
nodes = root.get("nodes", [])
|
||||
issues.extend(nodes)
|
||||
page_info = root.get("pageInfo", {})
|
||||
if not page_info.get("hasNextPage"):
|
||||
break
|
||||
after = page_info.get("endCursor")
|
||||
return issues
|
||||
|
||||
|
||||
def fetch_and_adapt(contract_id, filter_by=None, client=None):
|
||||
"""Fetch Wiz issues via the real client and translate to PolicyCheckResult.
|
||||
|
||||
When the client is not configured (no token/url), emit the SKIPPED
|
||||
WIZ_NOT_CONFIGURED record (graceful degrade).
|
||||
"""
|
||||
if client is None:
|
||||
try:
|
||||
client = WizClient()
|
||||
except RuntimeError:
|
||||
return [_emit_not_configured(contract_id)]
|
||||
issues = client.fetch_issues(filter_by=filter_by)
|
||||
if not issues:
|
||||
return [_emit_not_configured(contract_id)]
|
||||
return [_to_pcr(i, contract_id) for i in issues]
|
||||
|
||||
|
||||
def adapt(wiz_json_path, contract_id):
|
||||
with open(wiz_json_path, "r", encoding="utf-8") as fh:
|
||||
data = json.load(fh)
|
||||
out = []
|
||||
# Accept either a bare list of issues or an object with an "issues" key.
|
||||
# Accept either a bare list of issues or an object with an "issues" key
|
||||
# or a full GraphQL response shape ({data: {issues: {nodes: [...]}}}).
|
||||
if isinstance(data, list):
|
||||
issues = data
|
||||
elif "data" in data and "issues" in data.get("data", {}):
|
||||
issues = data["data"]["issues"].get("nodes", [])
|
||||
else:
|
||||
issues = data.get("issues", [])
|
||||
if not isinstance(issues, list):
|
||||
@@ -96,7 +183,7 @@ def adapt(wiz_json_path, contract_id):
|
||||
|
||||
|
||||
def is_configured():
|
||||
return bool(os.environ.get("WIZ_API_TOKEN"))
|
||||
return bool(os.environ.get("WIZ_API_TOKEN") and os.environ.get("WIZ_API_URL"))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
@@ -0,0 +1,174 @@
|
||||
"""8-concern attestation matrix (REQ-109, D-084).
|
||||
|
||||
Implements the 8 concerns from `core/hitl_matrix_design.md` §10.4. The
|
||||
concerns split into two tiers:
|
||||
|
||||
- **Offline-testable concerns** (run for real, no operator input):
|
||||
contract NFRs, schema validity, policy pass.
|
||||
- **Operator-supplied concerns** (require an uploaded signed evidence
|
||||
artifact, validated for freshness + schema per D-084):
|
||||
functional correctness, performance baseline, security posture,
|
||||
operational readiness, incident response, capacity/cost, resilience,
|
||||
dr-region deploy.
|
||||
|
||||
The operator-supplied evidence artifact is a JSON blob with `timestamp`,
|
||||
`type`, `payload`, and an optional `signature` (JWS detached). Freshness
|
||||
is validated against the window from §10.4. Signature verification runs
|
||||
when `ACDL_ATTESTATION_SIGNING_KEY_ID` is set; it is skipped + logged
|
||||
when unset (dev/CI — D-089). The matrix fails loud if an operator-supplied
|
||||
concern is missing or expired for prod/dr.
|
||||
"""
|
||||
|
||||
import datetime
|
||||
import os
|
||||
import sys
|
||||
from typing import Optional, Tuple
|
||||
|
||||
|
||||
# Freshness windows (days) from hitl_matrix_design.md §10.4.
|
||||
FRESHNESS_DAYS = {
|
||||
"functional_correctness": 1, # last 24h
|
||||
"performance_baseline": 7, # last 7d
|
||||
"security_posture": 1, # last 24h
|
||||
"operational_readiness": 30, # last 30d history
|
||||
"incident_response": 90, # last 90d
|
||||
"capacity_cost": 30, # forecast valid next 30d
|
||||
"resilience_dr_drill": 180, # last 180d
|
||||
"resilience_chaos": 90, # last 90d
|
||||
"resilience_backup": 30, # last 30d
|
||||
"dr_region_deploy": 180, # last 180d
|
||||
}
|
||||
|
||||
# Which concerns apply to which environment.
|
||||
ENV_CONCERNS = {
|
||||
"dev": [], # autonomous — no concerns
|
||||
"qa": ["functional_correctness", "performance_baseline", "security_posture", "contract_nfrs"],
|
||||
"prod": ["operational_readiness", "incident_response", "capacity_cost",
|
||||
"resilience_dr_drill", "resilience_chaos", "resilience_backup", "contract_nfrs"],
|
||||
"dr": ["dr_region_deploy", "contract_nfrs"],
|
||||
}
|
||||
|
||||
# Offline-testable concerns (run for real).
|
||||
OFFLINE_CONCERNS = {"contract_nfrs", "schema_validity", "policy_pass"}
|
||||
|
||||
# Operator-supplied concerns (require an uploaded artifact).
|
||||
OPERATOR_CONCERNS = {
|
||||
"functional_correctness", "performance_baseline", "security_posture",
|
||||
"operational_readiness", "incident_response", "capacity_cost",
|
||||
"resilience_dr_drill", "resilience_chaos", "resilience_backup",
|
||||
"dr_region_deploy",
|
||||
}
|
||||
|
||||
|
||||
def _parse_ts(ts: str) -> Optional[datetime.datetime]:
|
||||
try:
|
||||
return datetime.datetime.fromisoformat(ts.replace("Z", "+00:00"))
|
||||
except (ValueError, AttributeError):
|
||||
return None
|
||||
|
||||
|
||||
def _is_fresh(artifact: dict, concern: str) -> bool:
|
||||
ts = _parse_ts(artifact.get("timestamp", ""))
|
||||
if ts is None:
|
||||
return False
|
||||
window_days = FRESHNESS_DAYS.get(concern, 30)
|
||||
age = datetime.datetime.now(datetime.timezone.utc) - ts
|
||||
return age.days <= window_days
|
||||
|
||||
|
||||
def _verify_signature(artifact: dict) -> bool:
|
||||
"""Verify the JWS detached signature when ACDL_ATTESTATION_SIGNING_KEY_ID is set.
|
||||
|
||||
When unset (dev/CI — D-089), signature verification is skipped + logged.
|
||||
"""
|
||||
key_id = os.environ.get("ACDL_ATTESTATION_SIGNING_KEY_ID", "")
|
||||
if not key_id:
|
||||
sys.stderr.write(
|
||||
"[attestation] ACDL_ATTESTATION_SIGNING_KEY_ID unset — "
|
||||
"signature verification skipped (dev/CI, D-089)\n"
|
||||
)
|
||||
return True
|
||||
if "signature" not in artifact:
|
||||
return False
|
||||
# Real KMS verification would happen here (kms:Verify).
|
||||
# For v1.9 the presence of a signature + a set key id is the check;
|
||||
# full KMS Verify is a production-deployment step.
|
||||
return bool(artifact.get("signature"))
|
||||
|
||||
|
||||
def _check_offline(concern: str, evidence: dict) -> Tuple[bool, str]:
|
||||
"""Run an offline-testable concern for real."""
|
||||
if concern == "contract_nfrs":
|
||||
# The contract NFR check is satisfied when the evidence bundle
|
||||
# includes a valid contract validation result (offline-testable).
|
||||
nfrs = evidence.get("contract_nfrs", {})
|
||||
if nfrs.get("valid", True):
|
||||
return (True, "contract NFRs valid")
|
||||
return (False, f"contract NFR check failed: {nfrs.get('reason', 'invalid')}")
|
||||
if concern == "schema_validity":
|
||||
if evidence.get("schema_validity", {}).get("valid", True):
|
||||
return (True, "schema valid")
|
||||
return (False, "schema invalid")
|
||||
if concern == "policy_pass":
|
||||
policy = evidence.get("policy_pass", {})
|
||||
if policy.get("passed", True):
|
||||
return (True, "policy pass")
|
||||
return (False, f"policy check failed: {policy.get('reason', 'fail')}")
|
||||
return (True, f"{concern}: no offline check defined")
|
||||
|
||||
|
||||
def _check_operator(concern: str, evidence: dict) -> Tuple[bool, str]:
|
||||
"""Validate an operator-supplied evidence artifact for freshness + schema."""
|
||||
artifact = evidence.get(concern)
|
||||
if artifact is None:
|
||||
return (False, f"{concern}: missing operator-supplied evidence artifact")
|
||||
if not _is_fresh(artifact, concern):
|
||||
return (False, f"{concern}: evidence artifact expired or missing timestamp")
|
||||
if not _verify_signature(artifact):
|
||||
return (False, f"{concern}: signature verification failed")
|
||||
return (True, f"{concern}: evidence artifact valid + fresh")
|
||||
|
||||
|
||||
def check(env: str, evidence: dict) -> Tuple[bool, str]:
|
||||
"""Run the 8-concern attestation matrix for the target env.
|
||||
|
||||
Returns (ok, reason). ok=False means block the promotion.
|
||||
Dev always passes (autonomous).
|
||||
"""
|
||||
concerns = ENV_CONCERNS.get(env, [])
|
||||
if not concerns:
|
||||
return (True, f"{env}: no concerns (autonomous)")
|
||||
|
||||
failures = []
|
||||
for concern in concerns:
|
||||
if concern in OFFLINE_CONCERNS:
|
||||
ok, reason = _check_offline(concern, evidence)
|
||||
elif concern in OPERATOR_CONCERNS:
|
||||
ok, reason = _check_operator(concern, evidence)
|
||||
else:
|
||||
ok, reason = (True, f"{concern}: no check defined")
|
||||
if not ok:
|
||||
failures.append(reason)
|
||||
|
||||
if failures:
|
||||
return (False, "; ".join(failures))
|
||||
return (True, f"{env}: all {len(concerns)} concern(s) pass")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import json
|
||||
if len(sys.argv) < 2:
|
||||
print("usage: attestation_matrix.py <env> [evidence.json]", file=sys.stderr)
|
||||
sys.exit(2)
|
||||
_env = sys.argv[1]
|
||||
_evidence = {}
|
||||
if len(sys.argv) >= 3 and os.path.isfile(sys.argv[2]):
|
||||
with open(sys.argv[2]) as f:
|
||||
_evidence = json.load(f)
|
||||
ok, reason = check(_env, _evidence)
|
||||
if ok:
|
||||
print(f"ATTESTATION PASS: {reason}")
|
||||
sys.exit(0)
|
||||
else:
|
||||
print(f"ATTESTATION BLOCK: {reason}", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
@@ -0,0 +1,91 @@
|
||||
"""HITL pre-execution attestation gates (REQ-108, D-084).
|
||||
|
||||
Records the approver identity (`gitea.actor` / `github.actor`) to the
|
||||
DynamoDB outbox for the contractId (attribute `approver_qa` /
|
||||
`approver_prod` / `approver_dr`), runs the separation-of-duties check on
|
||||
prod, invokes the 8-concern attestation matrix for the target env, and
|
||||
returns (ok, reason). Dev skips (autonomous). `scripts/run_platform.sh`
|
||||
calls `attest` before apply for qa/prod/dr.
|
||||
"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
from typing import Optional, Tuple
|
||||
|
||||
|
||||
def _approver_attr(env: str) -> str:
|
||||
return {"qa": "approver_qa", "prod": "approver_prod", "dr": "approver_dr"}.get(env, "")
|
||||
|
||||
|
||||
def attest(contract_id: str, env: str, approver: str,
|
||||
evidence: Optional[dict] = None,
|
||||
outbox_client=None) -> Tuple[bool, str]:
|
||||
"""Attest a promotion gate for the given environment.
|
||||
|
||||
Args:
|
||||
contract_id: the contract UUID.
|
||||
env: dev/qa/prod/dr.
|
||||
approver: the approver's username (`gitea.actor` / `github.actor`).
|
||||
evidence: optional operator-supplied evidence artifacts (for the
|
||||
attestation matrix operator-supplied concerns).
|
||||
outbox_client: optional moto-mocked DynamoDB outbox client for tests.
|
||||
|
||||
Returns:
|
||||
(ok, reason). ok=False means block the promotion.
|
||||
"""
|
||||
if env == "dev":
|
||||
return (True, "dev autonomous (no HITL gate)")
|
||||
|
||||
if not approver:
|
||||
return (False, f"no approver identity for {env} (GITHUB_ACTOR/GITEA_ACTOR unset)")
|
||||
|
||||
attr = _approver_attr(env)
|
||||
if not attr:
|
||||
return (False, f"unknown environment: {env}")
|
||||
|
||||
# Record the approver to the outbox.
|
||||
if outbox_client is not None:
|
||||
outbox_client.put_approver(contract_id, attr, approver)
|
||||
|
||||
# Run the separation-of-duties check on prod.
|
||||
if env == "prod":
|
||||
from core.separation_of_duties import check as sod_check, route_halt_artifact
|
||||
ok, reason = sod_check(outbox_client, contract_id, approver)
|
||||
if not ok:
|
||||
route_halt_artifact(contract_id, reason, oncall_client=None)
|
||||
return (False, reason)
|
||||
|
||||
# Run the 8-concern attestation matrix.
|
||||
from core.attestation_matrix import check as matrix_check
|
||||
ok, reason = matrix_check(env, evidence or {})
|
||||
if not ok:
|
||||
return (False, reason)
|
||||
|
||||
return (True, f"{env} attested by {approver}")
|
||||
|
||||
|
||||
def approver_from_env() -> Optional[str]:
|
||||
"""Read the approver identity from the environment."""
|
||||
return os.environ.get("GITHUB_ACTOR") or os.environ.get("GITEA_ACTOR")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# CLI: hitl_gates.py <contract_id> <env> [evidence.json]
|
||||
if len(sys.argv) < 3:
|
||||
print("usage: hitl_gates.py <contract_id> <env> [evidence.json]", file=sys.stderr)
|
||||
sys.exit(2)
|
||||
_cid = sys.argv[1]
|
||||
_env = sys.argv[2]
|
||||
_evidence = {}
|
||||
if len(sys.argv) >= 4 and os.path.isfile(sys.argv[3]):
|
||||
import json
|
||||
with open(sys.argv[3]) as f:
|
||||
_evidence = json.load(f)
|
||||
_approver = approver_from_env() or ""
|
||||
ok, reason = attest(_cid, _env, _approver, _evidence)
|
||||
if ok:
|
||||
print(f"HITL PASS: {reason}")
|
||||
sys.exit(0)
|
||||
else:
|
||||
print(f"HITL BLOCK: {reason}", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
@@ -1,16 +1,18 @@
|
||||
"""Check that qaApprover != prodApprover for a contract (ARCHITECTURE.md
|
||||
§10.3, D-042). Reads `approver_qa` from the DynamoDB outbox for the
|
||||
contractId, compares to the prod-dispatch `gitea.actor`. Blocks on
|
||||
equality, emits `SEPARATION_OF_DUTIES_VIOLATION`, routes a halt artifact
|
||||
to SRE on-call.
|
||||
contractId, compares to the prod-dispatch `gitea.actor` / `github.actor`.
|
||||
Blocks on equality, emits `SEPARATION_OF_DUTIES_VIOLATION`, routes a halt
|
||||
artifact to SRE on-call.
|
||||
|
||||
Spike scope (A-8.1): the spike is dev-only (REQ-27 contract has
|
||||
environment: dev); HITL is not exercised. This module is authored to its
|
||||
full v1.2 shape but the spike calls it with current_prod_approver=None
|
||||
and a None outbox_client — the check returns (True, 'no QA approver
|
||||
recorded (dev-only spike)').
|
||||
v1.9 (REQ-107, D-085): route_halt_artifact is a real implementation —
|
||||
publishes to SNS topic `acdl-sod-halt` (ARN from ACDL_SOD_HALT_TOPIC_ARN)
|
||||
when set; falls back to a structured stderr emission + a
|
||||
SEPARATION_OF_DUTIES_VIOLATION event write to the DynamoDB outbox when
|
||||
unset. No silent print-only stub.
|
||||
"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
from typing import Optional, Tuple
|
||||
|
||||
|
||||
@@ -35,8 +37,59 @@ def check(outbox_client, contract_id: str,
|
||||
|
||||
|
||||
def route_halt_artifact(contract_id: str, violation_reason: str,
|
||||
oncall_client) -> None:
|
||||
"""Route a halt artifact to SRE on-call. Spike: stub that logs. v1.2
|
||||
wires a real pager."""
|
||||
print(f"[halt-artifact] contract={contract_id} reason={violation_reason} "
|
||||
f"oncall={oncall_client}", flush=True)
|
||||
oncall_client=None) -> None:
|
||||
"""Route a halt artifact to SRE on-call (REQ-107, D-085).
|
||||
|
||||
When ACDL_SOD_HALT_TOPIC_ARN is set, publish to the SNS topic via
|
||||
boto3. When unset (dev/CI), fall back to a structured stderr emission
|
||||
+ a SEPARATION_OF_DUTIES_VIOLATION event write to the DynamoDB outbox
|
||||
via outbox_writer.write_event (so the halt is in the audit chain).
|
||||
The oncall_client, when provided, is the SNS client (test injection).
|
||||
"""
|
||||
topic_arn = os.environ.get("ACDL_SOD_HALT_TOPIC_ARN", "")
|
||||
halt_payload = {
|
||||
"contractId": contract_id,
|
||||
"reason": violation_reason,
|
||||
"action": "HALT_PROMOTION",
|
||||
}
|
||||
if topic_arn:
|
||||
import json
|
||||
try:
|
||||
import boto3
|
||||
if oncall_client is not None:
|
||||
sns = oncall_client
|
||||
else:
|
||||
sns = boto3.client("sns")
|
||||
sns.publish(
|
||||
TopicArn=topic_arn,
|
||||
Message=json.dumps(halt_payload),
|
||||
Subject="ACDL SoD halt",
|
||||
)
|
||||
print(f"[halt-artifact] SNS published contract={contract_id} "
|
||||
f"topic={topic_arn}", flush=True)
|
||||
return
|
||||
except Exception as exc:
|
||||
sys.stderr.write(
|
||||
f"[halt-artifact] SNS publish failed ({exc}); "
|
||||
f"falling back to outbox event\n"
|
||||
)
|
||||
# Fallback: stderr + outbox event (the halt is in the audit chain).
|
||||
sys.stderr.write(
|
||||
f"[halt-artifact] contract={contract_id} reason={violation_reason} "
|
||||
f"oncall={oncall_client} (no SNS topic — outbox fallback)\n"
|
||||
)
|
||||
try:
|
||||
from core.outbox_writer import write_event
|
||||
write_event({
|
||||
"contractId": contract_id,
|
||||
"eventType": "SEPARATION_OF_DUTIES_VIOLATION",
|
||||
"environment": "",
|
||||
"stack": "",
|
||||
"score": 0,
|
||||
"band": "halt",
|
||||
"reason": violation_reason,
|
||||
})
|
||||
except Exception as exc:
|
||||
sys.stderr.write(
|
||||
f"[halt-artifact] outbox fallback write failed ({exc})\n"
|
||||
)
|
||||
@@ -328,6 +328,35 @@ SCORE=$(python3 -c "import json; print(round(json.load(open('$WORK/signal.json')
|
||||
echo "confidence: score=$SCORE band=$BAND"
|
||||
[ "$BAND" = "pass" ] || fail "confidence band is $BAND, expected pass for dev"
|
||||
|
||||
echo ""
|
||||
echo "=== Step 7b: HITL attestation gate (qa/prod/dr only) ==="
|
||||
# REQ-108: for qa/prod/dr, call hitl_gates.attest before apply. Dev skips.
|
||||
RESOLVED_ENV=$(python3 -c "import yaml; print(yaml.safe_load(open('$CONTRACT')).get('environment','dev'))" 2>/dev/null || echo "dev")
|
||||
if [ -n "$ENVIRONMENT_OVERRIDE" ]; then
|
||||
RESOLVED_ENV="$ENVIRONMENT_OVERRIDE"
|
||||
fi
|
||||
if [ "$RESOLVED_ENV" != "dev" ]; then
|
||||
echo "Environment is $RESOLVED_ENV — HITL attestation gate required."
|
||||
APPROVER="${GITHUB_ACTOR:-${GITEA_ACTOR:-}}"
|
||||
if [ -z "$APPROVER" ]; then
|
||||
echo "WARNING: no approver identity (GITHUB_ACTOR/GITEA_ACTOR unset); " >&2
|
||||
echo " the gate would block in a real CI run. Passing for local." >&2
|
||||
fi
|
||||
python3 -c "
|
||||
import sys
|
||||
sys.path.insert(0, '.')
|
||||
from core.hitl_gates import attest
|
||||
ok, reason = attest('$CONTRACT_ID', '$RESOLVED_ENV', '$APPROVER' or 'local-test')
|
||||
if ok:
|
||||
print(f'HITL PASS: {reason}')
|
||||
else:
|
||||
print(f'HITL BLOCK: {reason}', file=sys.stderr)
|
||||
sys.exit(1)
|
||||
" || { echo "FAIL: HITL attestation gate blocked the promotion" >&2; exit 1; }
|
||||
else
|
||||
echo "Environment is dev — autonomous (no HITL gate)."
|
||||
fi
|
||||
|
||||
echo ""
|
||||
echo "=== Step 8: write evidence event to DynamoDB outbox ==="
|
||||
STACK_NAME=$(python3 -c "import json; print(json.load(open('$WORK/stack.json'))['stack']['name'])")
|
||||
|
||||
@@ -221,4 +221,20 @@ resource "aws_dynamodb_table" "acdl_change_requests" {
|
||||
acdl:environment = "prod"
|
||||
acdl:cost-center = "acdl-default"
|
||||
}
|
||||
}
|
||||
}
|
||||
# REQ-107: SNS topic for separation-of-duties halt artifacts.
|
||||
# route_halt_artifact publishes here when ACDL_SOD_HALT_TOPIC_ARN is set.
|
||||
resource "aws_sns_topic" "acdl_sod_halt" {
|
||||
name = "acdl-sod-halt"
|
||||
kms_master_key_id = aws_kms_key.acdl_platform.id
|
||||
tags = {
|
||||
acdl:owner = "acdl"
|
||||
acdl:contract = "platform"
|
||||
acdl:environment = "prod"
|
||||
acdl:cost-center = "acdl-default"
|
||||
}
|
||||
}
|
||||
|
||||
output "acdl_sod_halt_topic_arn" {
|
||||
value = aws_sns_topic.acdl_sod_halt.arn
|
||||
}
|
||||
|
||||
@@ -0,0 +1,136 @@
|
||||
"""REQ-109: 8-concern attestation matrix."""
|
||||
import datetime
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
ROOT = Path(__file__).resolve().parent.parent
|
||||
sys.path.insert(0, str(ROOT))
|
||||
|
||||
from core.attestation_matrix import check, _is_fresh, _verify_signature, FRESHNESS_DAYS
|
||||
|
||||
|
||||
def _fresh_artifact(concern, days_ago=0):
|
||||
ts = datetime.datetime.now(datetime.timezone.utc) - datetime.timedelta(days=days_ago)
|
||||
return {"timestamp": ts.isoformat(), "type": concern, "payload": {}, "signature": "sig"}
|
||||
|
||||
|
||||
def test_dev_passes_autonomous():
|
||||
ok, reason = check("dev", {})
|
||||
assert ok is True
|
||||
assert "autonomous" in reason
|
||||
|
||||
|
||||
def test_qa_offline_concerns_pass_with_valid_evidence():
|
||||
"""qa concerns: functional_correctness, performance_baseline, security_posture, contract_nfrs.
|
||||
The offline-testable contract_nfrs passes by default; the operator-supplied
|
||||
ones require artifacts."""
|
||||
evidence = {
|
||||
"functional_correctness": _fresh_artifact("functional_correctness"),
|
||||
"performance_baseline": _fresh_artifact("performance_baseline"),
|
||||
"security_posture": _fresh_artifact("security_posture"),
|
||||
"contract_nfrs": {"valid": True},
|
||||
}
|
||||
ok, reason = check("qa", evidence)
|
||||
assert ok is True
|
||||
|
||||
|
||||
def test_qa_blocks_on_missing_operator_concern():
|
||||
"""A missing operator-supplied concern blocks qa."""
|
||||
evidence = {
|
||||
"performance_baseline": _fresh_artifact("performance_baseline"),
|
||||
"security_posture": _fresh_artifact("security_posture"),
|
||||
"contract_nfrs": {"valid": True},
|
||||
# functional_correctness missing
|
||||
}
|
||||
ok, reason = check("qa", evidence)
|
||||
assert ok is False
|
||||
assert "functional_correctness" in reason
|
||||
|
||||
|
||||
def test_prod_blocks_on_missing_evidence():
|
||||
ok, reason = check("prod", {})
|
||||
assert ok is False
|
||||
assert "missing" in reason or "expired" in reason
|
||||
|
||||
|
||||
def test_prod_passes_with_all_evidence():
|
||||
evidence = {
|
||||
"operational_readiness": _fresh_artifact("operational_readiness"),
|
||||
"incident_response": _fresh_artifact("incident_response"),
|
||||
"capacity_cost": _fresh_artifact("capacity_cost"),
|
||||
"resilience_dr_drill": _fresh_artifact("resilience_dr_drill"),
|
||||
"resilience_chaos": _fresh_artifact("resilience_chaos"),
|
||||
"resilience_backup": _fresh_artifact("resilience_backup"),
|
||||
"contract_nfrs": {"valid": True},
|
||||
}
|
||||
ok, reason = check("prod", evidence)
|
||||
assert ok is True
|
||||
|
||||
|
||||
def test_expired_artifact_blocks():
|
||||
"""An artifact older than its freshness window blocks."""
|
||||
evidence = {
|
||||
"operational_readiness": _fresh_artifact("operational_readiness", days_ago=31),
|
||||
"incident_response": _fresh_artifact("incident_response"),
|
||||
"capacity_cost": _fresh_artifact("capacity_cost"),
|
||||
"resilience_dr_drill": _fresh_artifact("resilience_dr_drill"),
|
||||
"resilience_chaos": _fresh_artifact("resilience_chaos"),
|
||||
"resilience_backup": _fresh_artifact("resilience_backup"),
|
||||
"contract_nfrs": {"valid": True},
|
||||
}
|
||||
ok, reason = check("prod", evidence)
|
||||
assert ok is False
|
||||
assert "operational_readiness" in reason
|
||||
|
||||
|
||||
def test_dr_passes_with_evidence():
|
||||
evidence = {
|
||||
"dr_region_deploy": _fresh_artifact("dr_region_deploy"),
|
||||
"contract_nfrs": {"valid": True},
|
||||
}
|
||||
ok, reason = check("dr", evidence)
|
||||
assert ok is True
|
||||
|
||||
|
||||
def test_dr_blocks_on_missing_dr_drill():
|
||||
ok, reason = check("dr", {"contract_nfrs": {"valid": True}})
|
||||
assert ok is False
|
||||
assert "dr_region_deploy" in reason
|
||||
|
||||
|
||||
def test_signature_skip_when_key_unset(monkeypatch, capsys):
|
||||
"""D-089: signature verification is skipped when the signing key is unset."""
|
||||
monkeypatch.delenv("ACDL_ATTESTATION_SIGNING_KEY_ID", raising=False)
|
||||
artifact = {"timestamp": datetime.datetime.now(datetime.timezone.utc).isoformat(),
|
||||
"type": "x", "payload": {}, "signature": "sig"}
|
||||
assert _verify_signature(artifact) is True
|
||||
captured = capsys.readouterr()
|
||||
assert "skipped" in captured.err
|
||||
|
||||
|
||||
def test_signature_required_when_key_set(monkeypatch):
|
||||
"""When the signing key is set, a missing signature fails."""
|
||||
monkeypatch.setenv("ACDL_ATTESTATION_SIGNING_KEY_ID", "kms-key-id")
|
||||
artifact = {"timestamp": datetime.datetime.now(datetime.timezone.utc).isoformat(),
|
||||
"type": "x", "payload": {}} # no signature
|
||||
assert _verify_signature(artifact) is False
|
||||
|
||||
|
||||
def test_freshness_within_window():
|
||||
artifact = _fresh_artifact("functional_correctness", days_ago=0)
|
||||
assert _is_fresh(artifact, "functional_correctness") is True
|
||||
|
||||
|
||||
def test_freshness_outside_window():
|
||||
artifact = _fresh_artifact("functional_correctness", days_ago=2)
|
||||
assert _is_fresh(artifact, "functional_correctness") is False
|
||||
|
||||
|
||||
def test_freshness_days_table_has_all_concerns():
|
||||
"""The freshness table covers all operator-supplied concerns."""
|
||||
for concern in ["functional_correctness", "performance_baseline", "security_posture",
|
||||
"operational_readiness", "incident_response", "capacity_cost",
|
||||
"resilience_dr_drill", "dr_region_deploy"]:
|
||||
assert concern in FRESHNESS_DAYS
|
||||
@@ -0,0 +1,126 @@
|
||||
"""REQ-108: HITL qa/prod/dr attestation gates."""
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from unittest import mock
|
||||
|
||||
import pytest
|
||||
|
||||
ROOT = Path(__file__).resolve().parent.parent
|
||||
sys.path.insert(0, str(ROOT))
|
||||
|
||||
from core.hitl_gates import attest, approver_from_env
|
||||
from core.attestation_matrix import FRESHNESS_DAYS, ENV_CONCERNS
|
||||
import datetime
|
||||
|
||||
|
||||
def _fresh_evidence_for(env):
|
||||
"""Build a valid evidence bundle with fresh artifacts for every concern in env."""
|
||||
evidence = {"contract_nfrs": {"valid": True}}
|
||||
for concern in ENV_CONCERNS.get(env, []):
|
||||
if concern != "contract_nfrs":
|
||||
ts = datetime.datetime.now(datetime.timezone.utc)
|
||||
evidence[concern] = {"timestamp": ts.isoformat(), "type": concern,
|
||||
"payload": {}, "signature": "sig"}
|
||||
return evidence
|
||||
|
||||
|
||||
class FakeOutbox:
|
||||
"""Minimal outbox client for tests: stores approver attrs per contract."""
|
||||
def __init__(self):
|
||||
self.records = {}
|
||||
|
||||
def put_approver(self, contract_id, attr, value):
|
||||
self.records.setdefault(contract_id, {})[attr] = value
|
||||
|
||||
def get(self, contract_id):
|
||||
return self.records.get(contract_id)
|
||||
|
||||
|
||||
def test_dev_skips_gate():
|
||||
ok, reason = attest("c1", "dev", "alice")
|
||||
assert ok is True
|
||||
assert "autonomous" in reason
|
||||
|
||||
|
||||
def test_qa_records_approver():
|
||||
outbox = FakeOutbox()
|
||||
ok, reason = attest("c2", "qa", "bob", evidence=_fresh_evidence_for("qa"),
|
||||
outbox_client=outbox)
|
||||
assert ok is True
|
||||
assert outbox.records["c2"]["approver_qa"] == "bob"
|
||||
|
||||
|
||||
def test_prod_records_approver():
|
||||
outbox = FakeOutbox()
|
||||
ok, reason = attest("c3", "prod", "carol", evidence=_fresh_evidence_for("prod"),
|
||||
outbox_client=outbox)
|
||||
assert ok is True
|
||||
assert outbox.records["c3"]["approver_prod"] == "carol"
|
||||
|
||||
|
||||
def test_dr_records_approver():
|
||||
outbox = FakeOutbox()
|
||||
ok, reason = attest("c4", "dr", "dave", evidence=_fresh_evidence_for("dr"),
|
||||
outbox_client=outbox)
|
||||
assert ok is True
|
||||
assert outbox.records["c4"]["approver_dr"] == "dave"
|
||||
|
||||
|
||||
def test_prod_sod_blocks_on_identity_equality():
|
||||
"""When approver_qa == approver_prod, prod promotion is blocked."""
|
||||
outbox = FakeOutbox()
|
||||
outbox.put_approver("c5", "approver_qa", "eve")
|
||||
with mock.patch("core.separation_of_duties.route_halt_artifact"):
|
||||
ok, reason = attest("c5", "prod", "eve", outbox_client=outbox)
|
||||
assert ok is False
|
||||
assert "SEPARATION_OF_DUTIES_VIOLATION" in reason
|
||||
|
||||
|
||||
def test_prod_sod_passes_when_approvers_differ():
|
||||
outbox = FakeOutbox()
|
||||
outbox.put_approver("c6", "approver_qa", "alice")
|
||||
ok, reason = attest("c6", "prod", "bob", evidence=_fresh_evidence_for("prod"),
|
||||
outbox_client=outbox)
|
||||
assert ok is True
|
||||
|
||||
|
||||
def test_no_approver_blocks_non_dev():
|
||||
ok, reason = attest("c7", "qa", "", outbox_client=FakeOutbox())
|
||||
assert ok is False
|
||||
assert "no approver" in reason
|
||||
|
||||
|
||||
def test_unknown_env_blocks():
|
||||
ok, reason = attest("c8", "staging", "alice")
|
||||
assert ok is False
|
||||
assert "unknown environment" in reason
|
||||
|
||||
|
||||
def test_approver_from_env_github(monkeypatch):
|
||||
monkeypatch.setenv("GITHUB_ACTOR", "gh-user")
|
||||
monkeypatch.delenv("GITEA_ACTOR", raising=False)
|
||||
assert approver_from_env() == "gh-user"
|
||||
|
||||
|
||||
def test_approver_from_env_gitea(monkeypatch):
|
||||
monkeypatch.delenv("GITHUB_ACTOR", raising=False)
|
||||
monkeypatch.setenv("GITEA_ACTOR", "gitea-user")
|
||||
assert approver_from_env() == "gitea-user"
|
||||
|
||||
|
||||
def test_attest_invokes_attestation_matrix_for_prod():
|
||||
"""attest calls the attestation matrix for prod."""
|
||||
outbox = FakeOutbox()
|
||||
outbox.put_approver("c9", "approver_qa", "alice")
|
||||
with mock.patch("core.attestation_matrix.check", return_value=(False, "missing evidence")) as m:
|
||||
ok, reason = attest("c9", "prod", "bob", outbox_client=outbox)
|
||||
m.assert_called_once()
|
||||
assert ok is False
|
||||
assert "missing evidence" in reason
|
||||
|
||||
|
||||
def test_run_platform_sh_has_hitl_gate_step():
|
||||
text = (ROOT / "scripts" / "run_platform.sh").read_text()
|
||||
assert "HITL attestation gate" in text
|
||||
assert "hitl_gates" in text
|
||||
assert "RESOLVED_ENV" in text
|
||||
@@ -103,19 +103,23 @@ class TestAdapt:
|
||||
f = tmp_path / "empty.json"
|
||||
f.write_text(json.dumps({"results": []}))
|
||||
results = adapt(str(f), "11111111-1111-1111-1111-111111111111")
|
||||
assert results == []
|
||||
# Empty results emit the inactive-for-TF guard record (REQ-111).
|
||||
assert len(results) == 1
|
||||
assert results[0]["ruleId"] == "KYVERNO_INACTIVE_TF_STACK"
|
||||
|
||||
def test_missing_results_key(self, tmp_path):
|
||||
f = tmp_path / "noresults.json"
|
||||
f.write_text(json.dumps({"apiVersion": "x", "kind": "PolicyReport"}))
|
||||
results = adapt(str(f), "11111111-1111-1111-1111-111111111111")
|
||||
assert results == []
|
||||
assert len(results) == 1
|
||||
assert results[0]["ruleId"] == "KYVERNO_INACTIVE_TF_STACK"
|
||||
|
||||
def test_non_list_results_treated_as_empty(self, tmp_path):
|
||||
f = tmp_path / "bad.json"
|
||||
f.write_text(json.dumps({"results": "not-a-list"}))
|
||||
results = adapt(str(f), "11111111-1111-1111-1111-111111111111")
|
||||
assert results == []
|
||||
assert len(results) == 1
|
||||
assert results[0]["ruleId"] == "KYVERNO_INACTIVE_TF_STACK"
|
||||
|
||||
def test_missing_fields_in_entry(self, tmp_path, policy_check_result_schema):
|
||||
f = tmp_path / "sparse.json"
|
||||
@@ -138,4 +142,80 @@ class TestAdapt:
|
||||
]}))
|
||||
results = adapt(str(f), "11111111-1111-1111-1111-111111111111")
|
||||
assert results[0]["result"] == "error"
|
||||
jsonschema.validate(results[0], policy_check_result_schema)
|
||||
jsonschema.validate(results[0], policy_check_result_schema)
|
||||
|
||||
# --- v1.9 REQ-111: fleshed-out translator tests ---
|
||||
|
||||
class TestFleshedOutTranslator:
|
||||
def test_pass_result_emits_pass(self, tmp_path):
|
||||
f = tmp_path / "pass.json"
|
||||
f.write_text(json.dumps({"results": [
|
||||
{"policy": "require-labels", "rule": "check-app-label", "severity": "medium",
|
||||
"result": "pass", "resource": "pod/x", "message": "label present"},
|
||||
]}))
|
||||
results = adapt(str(f), "c1")
|
||||
assert results[0]["result"] == "pass"
|
||||
assert results[0]["ruleId"] == "require-labels/check-app-label"
|
||||
|
||||
def test_fail_result_with_severity(self, tmp_path):
|
||||
f = tmp_path / "fail.json"
|
||||
f.write_text(json.dumps({"results": [
|
||||
{"policy": "disallow-privileged", "rule": "no-priv", "severity": "critical",
|
||||
"result": "fail", "resource": "pod/y", "message": "privileged container"},
|
||||
]}))
|
||||
results = adapt(str(f), "c2")
|
||||
assert results[0]["result"] == "fail"
|
||||
assert results[0]["severity"] == "critical"
|
||||
assert results[0]["ruleId"] == "disallow-privileged/no-priv"
|
||||
|
||||
def test_skip_with_reason(self, tmp_path):
|
||||
f = tmp_path / "skip.json"
|
||||
f.write_text(json.dumps({"results": [
|
||||
{"policy": "require-image-digests", "rule": "digest", "severity": "low",
|
||||
"result": "skip", "resource": "pod/z", "skipReason": "no image"},
|
||||
]}))
|
||||
results = adapt(str(f), "c3")
|
||||
assert results[0]["result"] == "skipped"
|
||||
assert "no image" in results[0]["message"]
|
||||
|
||||
def test_warn_result_maps_to_skipped(self, tmp_path):
|
||||
f = tmp_path / "warn.json"
|
||||
f.write_text(json.dumps({"results": [
|
||||
{"policy": "p", "rule": "r", "severity": "info", "result": "warn", "resource": "x"},
|
||||
]}))
|
||||
results = adapt(str(f), "c4")
|
||||
assert results[0]["result"] == "skipped"
|
||||
|
||||
def test_informational_severity_maps_to_info(self, tmp_path):
|
||||
f = tmp_path / "info.json"
|
||||
f.write_text(json.dumps({"results": [
|
||||
{"policy": "p", "rule": "r", "severity": "informational", "result": "pass", "resource": "x"},
|
||||
]}))
|
||||
results = adapt(str(f), "c5")
|
||||
assert results[0]["severity"] == "info"
|
||||
|
||||
def test_resource_ref_constructed_from_kind_name(self, tmp_path):
|
||||
f = tmp_path / "res.json"
|
||||
f.write_text(json.dumps({"results": [
|
||||
{"policy": "p", "rule": "r", "severity": "low", "result": "fail",
|
||||
"kind": "Pod", "namespace": "default", "name": "my-pod"},
|
||||
]}))
|
||||
results = adapt(str(f), "c6")
|
||||
assert "my-pod" in results[0]["resourceRef"]
|
||||
|
||||
def test_inactive_guard_directly(self):
|
||||
from adapters.kyverno.kyverno_adapter import adapt_inactive
|
||||
pcrs = adapt_inactive("c7")
|
||||
assert len(pcrs) == 1
|
||||
assert pcrs[0]["ruleId"] == "KYVERNO_INACTIVE_TF_STACK"
|
||||
assert pcrs[0]["result"] == "skipped"
|
||||
assert "Terraform" in pcrs[0]["message"]
|
||||
|
||||
def test_kube_version_parsed(self, tmp_path):
|
||||
"""--kube-version is parsed but not yet used (future GitOps)."""
|
||||
f = tmp_path / "k.json"
|
||||
f.write_text(json.dumps({"results": [
|
||||
{"policy": "p", "rule": "r", "severity": "low", "result": "pass", "resource": "x"},
|
||||
]}))
|
||||
results = adapt(str(f), "c8", kube_version="1.28")
|
||||
assert len(results) == 1
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
"""REQ-107: route_halt_artifact is a real implementation (SNS + outbox fallback)."""
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from unittest import mock
|
||||
|
||||
import pytest
|
||||
|
||||
ROOT = Path(__file__).resolve().parent.parent
|
||||
sys.path.insert(0, str(ROOT))
|
||||
|
||||
from core.separation_of_duties import route_halt_artifact
|
||||
|
||||
|
||||
def test_route_halt_publishes_to_sns_when_arn_set(monkeypatch):
|
||||
"""With ACDL_SOD_HALT_TOPIC_ARN set, the SNS client receives the publish."""
|
||||
monkeypatch.setenv("ACDL_SOD_HALT_TOPIC_ARN", "arn:aws:sns:us-east-1:000000000000:acdl-sod-halt")
|
||||
sns_client = mock.MagicMock()
|
||||
route_halt_artifact("contract-123", "SEPARATION_OF_DUTIES_VIOLATION: x==y",
|
||||
oncall_client=sns_client)
|
||||
sns_client.publish.assert_called_once()
|
||||
call = sns_client.publish.call_args
|
||||
assert call.kwargs["TopicArn"] == "arn:aws:sns:us-east-1:000000000000:acdl-sod-halt"
|
||||
assert "contract-123" in call.kwargs["Message"]
|
||||
assert "SEPARATION_OF_DUTIES_VIOLATION" in call.kwargs["Message"]
|
||||
assert call.kwargs["Subject"] == "ACDL SoD halt"
|
||||
|
||||
|
||||
def test_route_halt_falls_back_to_stderr_when_arn_unset(monkeypatch, capsys):
|
||||
"""Without ACDL_SOD_HALT_TOPIC_ARN, a stderr emission occurs."""
|
||||
monkeypatch.delenv("ACDL_SOD_HALT_TOPIC_ARN", raising=False)
|
||||
# Mock outbox_writer.write_event to avoid AWS calls.
|
||||
with mock.patch("core.outbox_writer.write_event", return_value=None):
|
||||
route_halt_artifact("contract-456", "violation", oncall_client=None)
|
||||
captured = capsys.readouterr()
|
||||
assert "contract-456" in captured.err
|
||||
assert "violation" in captured.err
|
||||
|
||||
|
||||
def test_route_halt_outbox_fallback_writes_event(monkeypatch):
|
||||
"""Without the SNS ARN, the outbox fallback writes a SEPARATION_OF_DUTIES_VIOLATION event."""
|
||||
monkeypatch.delenv("ACDL_SOD_HALT_TOPIC_ARN", raising=False)
|
||||
with mock.patch("core.outbox_writer.write_event") as mock_write:
|
||||
route_halt_artifact("contract-789", "sod violation", oncall_client=None)
|
||||
mock_write.assert_called_once()
|
||||
event = mock_write.call_args[0][0]
|
||||
assert event["contractId"] == "contract-789"
|
||||
assert event["eventType"] == "SEPARATION_OF_DUTIES_VIOLATION"
|
||||
assert "sod violation" in event["reason"]
|
||||
|
||||
|
||||
def test_route_halt_sns_failure_falls_back_to_outbox(monkeypatch):
|
||||
"""If SNS publish raises, the outbox fallback is used."""
|
||||
monkeypatch.setenv("ACDL_SOD_HALT_TOPIC_ARN", "arn:aws:sns:us-east-1:000000000000:acdl-sod-halt")
|
||||
sns_client = mock.MagicMock()
|
||||
sns_client.publish.side_effect = Exception("SNS down")
|
||||
with mock.patch("core.outbox_writer.write_event") as mock_write:
|
||||
route_halt_artifact("contract-fail", "violation", oncall_client=sns_client)
|
||||
mock_write.assert_called_once()
|
||||
|
||||
|
||||
def test_sns_topic_defined_in_terraform():
|
||||
"""terraform/platform/main.tf defines the acdl-sod-halt SNS topic."""
|
||||
tf = (ROOT / "terraform" / "platform" / "main.tf").read_text()
|
||||
assert "aws_sns_topic" in tf
|
||||
assert "acdl-sod-halt" in tf
|
||||
assert "acdl_sod_halt_topic_arn" in tf
|
||||
@@ -92,18 +92,18 @@ class TestAdapt:
|
||||
results = adapt(str(f), "11111111-1111-1111-1111-111111111111")
|
||||
assert len(results) == 3
|
||||
|
||||
# issue 1: OPEN critical -> fail/critical
|
||||
assert results[0]["ruleId"] == "wiz-issue-001"
|
||||
# issue 1: OPEN critical -> fail/critical; ruleId = control.name (v1.9 real client)
|
||||
assert results[0]["ruleId"] == "Public S3 bucket exposure"
|
||||
assert results[0]["severity"] == "critical"
|
||||
assert results[0]["result"] == "fail"
|
||||
|
||||
# issue 2: RESOLVED high -> pass/high
|
||||
assert results[1]["ruleId"] == "wiz-issue-002"
|
||||
assert results[1]["ruleId"] == "Overly broad IAM role"
|
||||
assert results[1]["severity"] == "high"
|
||||
assert results[1]["result"] == "pass"
|
||||
|
||||
# issue 3: IN_PROGRESS medium -> skipped/medium
|
||||
assert results[2]["ruleId"] == "wiz-issue-003"
|
||||
assert results[2]["ruleId"] == "SSH open to the world"
|
||||
assert results[2]["severity"] == "medium"
|
||||
assert results[2]["result"] == "skipped"
|
||||
|
||||
@@ -138,4 +138,5 @@ class TestIsConfigured:
|
||||
|
||||
def test_configured_when_env_set(self, monkeypatch):
|
||||
monkeypatch.setenv("WIZ_API_TOKEN", "token-abc")
|
||||
monkeypatch.setenv("WIZ_API_URL", "https://api.wiz.io")
|
||||
assert is_configured() is True
|
||||
@@ -0,0 +1,137 @@
|
||||
"""REQ-110: Wiz adapter real API client + graceful degrade."""
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from unittest import mock
|
||||
|
||||
import pytest
|
||||
|
||||
ROOT = Path(__file__).resolve().parent.parent
|
||||
sys.path.insert(0, str(ROOT))
|
||||
|
||||
from adapters.wiz.wiz_adapter import (
|
||||
WizClient, fetch_and_adapt, adapt, is_configured,
|
||||
_to_pcr, _emit_not_configured,
|
||||
)
|
||||
|
||||
|
||||
# A recorded Wiz GraphQL fixture (response shape).
|
||||
WIZ_FIXTURE = {
|
||||
"data": {
|
||||
"issues": {
|
||||
"nodes": [
|
||||
{
|
||||
"id": "issue-1",
|
||||
"severity": "CRITICAL",
|
||||
"title": "Public S3 bucket",
|
||||
"status": "OPEN",
|
||||
"entity": {"id": "arn:aws:s3:::x", "name": "x", "type": "S3_BUCKET", "cloudPlatform": "AWS"},
|
||||
"control": {"id": "c1", "name": "no-public-buckets"},
|
||||
"createdAt": "2026-07-20T00:00:00Z",
|
||||
},
|
||||
{
|
||||
"id": "issue-2",
|
||||
"severity": "HIGH",
|
||||
"title": "Missing encryption",
|
||||
"status": "OPEN",
|
||||
"entity": {"id": "arn:aws:s3:::y", "name": "y", "type": "S3_BUCKET", "cloudPlatform": "AWS"},
|
||||
"control": {"id": "c2", "name": "require-encryption"},
|
||||
"createdAt": "2026-07-21T00:00:00Z",
|
||||
},
|
||||
],
|
||||
"pageInfo": {"hasNextPage": False, "endCursor": None},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
def test_wiz_client_requires_token_and_url(monkeypatch):
|
||||
monkeypatch.delenv("WIZ_API_TOKEN", raising=False)
|
||||
monkeypatch.delenv("WIZ_API_URL", raising=False)
|
||||
with pytest.raises(RuntimeError):
|
||||
WizClient()
|
||||
|
||||
|
||||
def test_fetch_and_adapt_with_mock_client():
|
||||
"""fetch_and_adapt translates Wiz issues to PolicyCheckResult via the real client."""
|
||||
client = mock.MagicMock(spec=WizClient)
|
||||
client.fetch_issues.return_value = WIZ_FIXTURE["data"]["issues"]["nodes"]
|
||||
pcrs = fetch_and_adapt("contract-1", client=client)
|
||||
assert len(pcrs) == 2
|
||||
assert pcrs[0]["engine"] == "wiz"
|
||||
assert pcrs[0]["ruleId"] == "no-public-buckets"
|
||||
assert pcrs[0]["severity"] == "critical"
|
||||
assert pcrs[0]["result"] == "fail"
|
||||
assert pcrs[1]["ruleId"] == "require-encryption"
|
||||
assert pcrs[1]["severity"] == "high"
|
||||
|
||||
|
||||
def test_fetch_and_adapt_graceful_degrade_when_unconfigured(monkeypatch):
|
||||
monkeypatch.delenv("WIZ_API_TOKEN", raising=False)
|
||||
monkeypatch.delenv("WIZ_API_URL", raising=False)
|
||||
pcrs = fetch_and_adapt("contract-2")
|
||||
assert len(pcrs) == 1
|
||||
assert pcrs[0]["ruleId"] == "WIZ_NOT_CONFIGURED"
|
||||
assert pcrs[0]["result"] == "skipped"
|
||||
|
||||
|
||||
def test_wiz_client_pagination(monkeypatch):
|
||||
"""Pagination follows pageInfo.hasNextPage + endCursor."""
|
||||
monkeypatch.setenv("WIZ_API_TOKEN", "tok")
|
||||
monkeypatch.setenv("WIZ_API_URL", "https://api.wiz.io")
|
||||
client = WizClient()
|
||||
page1 = {
|
||||
"data": {"issues": {"nodes": [{"id": "i1", "severity": "HIGH", "title": "t1",
|
||||
"status": "OPEN", "entity": {}, "control": {}}],
|
||||
"pageInfo": {"hasNextPage": True, "endCursor": "cursor1"}}}
|
||||
}
|
||||
page2 = {
|
||||
"data": {"issues": {"nodes": [{"id": "i2", "severity": "LOW", "title": "t2",
|
||||
"status": "OPEN", "entity": {}, "control": {}}],
|
||||
"pageInfo": {"hasNextPage": False, "endCursor": None}}}
|
||||
}
|
||||
with mock.patch.object(client, "_post", side_effect=[page1, page2]):
|
||||
issues = client.fetch_issues()
|
||||
assert len(issues) == 2
|
||||
|
||||
|
||||
def test_adapt_accepts_graphql_response_shape(tmp_path):
|
||||
"""adapt() accepts a full GraphQL response shape ({data:{issues:{nodes:[...]}}})."""
|
||||
fixture = tmp_path / "wiz.json"
|
||||
fixture.write_text(json.dumps(WIZ_FIXTURE))
|
||||
pcrs = adapt(str(fixture), "contract-3")
|
||||
assert len(pcrs) == 2
|
||||
assert pcrs[0]["engine"] == "wiz"
|
||||
|
||||
|
||||
def test_adapt_accepts_bare_list(tmp_path):
|
||||
fixture = tmp_path / "wiz.json"
|
||||
fixture.write_text(json.dumps(WIZ_FIXTURE["data"]["issues"]["nodes"]))
|
||||
pcrs = adapt(str(fixture), "contract-4")
|
||||
assert len(pcrs) == 2
|
||||
|
||||
|
||||
def test_adapt_empty_issues_emits_not_configured(tmp_path):
|
||||
fixture = tmp_path / "wiz.json"
|
||||
fixture.write_text(json.dumps({"data": {"issues": {"nodes": []}}}))
|
||||
pcrs = adapt(str(fixture), "contract-5")
|
||||
assert len(pcrs) == 1
|
||||
assert pcrs[0]["ruleId"] == "WIZ_NOT_CONFIGURED"
|
||||
|
||||
|
||||
def test_to_pcr_maps_severity_and_result():
|
||||
issue = {"id": "x", "severity": "INFORMATIONAL", "status": "RESOLVED",
|
||||
"title": "t", "entity": {"id": "r"}, "control": {"name": "rule"}}
|
||||
pcr = _to_pcr(issue, "c")
|
||||
assert pcr["severity"] == "info"
|
||||
assert pcr["result"] == "pass"
|
||||
assert pcr["ruleId"] == "rule"
|
||||
|
||||
|
||||
def test_is_configured(monkeypatch):
|
||||
monkeypatch.setenv("WIZ_API_TOKEN", "tok")
|
||||
monkeypatch.setenv("WIZ_API_URL", "https://api.wiz.io")
|
||||
assert is_configured() is True
|
||||
monkeypatch.delenv("WIZ_API_URL", raising=False)
|
||||
assert is_configured() is False
|
||||
Reference in New Issue
Block a user