Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ jobs:
)"
pre-commit run --from-ref "${BASE_SHA}" --to-ref HEAD

- name: Test decision approval binding
run: python roles/decision_consumer/tests/test_approval_binding.py

- name: Run ansible-lint on changed Ansible files
shell: bash
run: |
Expand Down
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

- No unreleased changes yet.
### Changed

- Bound decision requests to a deterministic SHA-256 identity of the approval-relevant artefact. The consumer verifies that identity before applying the request's approval posture, rejects invalid or stale approval and incomplete audit context, and preserves the approved identity in the execution record.

## [0.1.6] - 2026-06-14

Expand Down
28 changes: 28 additions & 0 deletions roles/decision_consumer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,31 @@
Installs the HybridOps decision consumer as a `systemd` service.

The consumer watches dispatch requests, waits for approval when required, and writes normalized execution records. It does not execute `hyops` in v1.

## Approval binding

When a dispatch request requires approval, the approval must identify the exact request artefact that was reviewed. The dispatcher writes:

- `approval_payload_version`
- `approval_artifact_sha256`

The approval file remains named `<dispatch_id>.approved.json`, but its contents must include the same identity:

```json
{
"dispatch_id": "dispatch-...",
"approval_payload_version": 1,
"approval_artifact_sha256": "<sha256>",
"approved_by": "operator@example.com",
"approved_at": "2026-09-12T12:00:00Z",
"note": "approved change window"
}
```

Immediately before promotion, the consumer recomputes the SHA-256 digest from the current approval-relevant request contents. Promotion is refused when the request identity is missing, when the approval references another request identity, or when the artefact no longer matches what was approved. A stale or invalid approval never produces `approved-ready`.

`approved_by` and `approved_at` must be non-empty strings. An approval without that audit context is not promoted.

Every request must retain the identity written by the dispatcher. Routes with `requires_approval: false` keep their existing no-approval behavior after that identity is verified. Their execution records still carry the current artefact identity for provenance.

The digest binds content; it does not authenticate filesystem writers. Access to the request and approval directories remains a trusted boundary.
112 changes: 107 additions & 5 deletions roles/decision_consumer/templates/decision-consumer.py.j2
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ maintainer: HybridOps
from __future__ import annotations

import datetime
import hashlib
import json
import time
import uuid
Expand All @@ -16,6 +17,7 @@ ROOT = Path("{{ decision_consumer_root }}")
CONFIG_PATH = ROOT / "config" / "config.json"
STATE_PATH = ROOT / "state" / "state.json"
LOG_PATH = ROOT / "logs" / "consumer.log"
APPROVAL_PAYLOAD_VERSION = 1


def _utc_now() -> str:
Expand All @@ -28,7 +30,8 @@ def _utc_now_token() -> str:

def _read_json(path: Path) -> dict:
try:
return json.loads(path.read_text(encoding="utf-8"))
payload = json.loads(path.read_text(encoding="utf-8"))
return payload if isinstance(payload, dict) else {}
except Exception:
return {}

Expand Down Expand Up @@ -56,7 +59,48 @@ def _sanitize_token(value: str) -> str:
return token or "execution"


def _emit_execution_record(*, request: dict, approval: dict, config: dict, executions_dir: Path) -> dict:
def _approval_payload(request: dict) -> dict:
return {
"version": APPROVAL_PAYLOAD_VERSION,
"source_decision_id": str(request.get("source_decision_id") or ""),
"decision_type": str(request.get("decision_type") or ""),
"execution_plane": str(request.get("execution_plane") or ""),
"target_kind": str(request.get("target_kind") or ""),
"target_ref": str(request.get("target_ref") or ""),
"target_env": str(request.get("target_env") or ""),
"requires_approval": bool(request.get("requires_approval", True)),
"rationale": str(request.get("rationale") or ""),
"checks": request.get("checks") if isinstance(request.get("checks"), list) else [],
}


def _approval_artifact_sha256(request: dict) -> str:
encoded = json.dumps(
_approval_payload(request),
ensure_ascii=False,
separators=(",", ":"),
sort_keys=True,
).encode("utf-8")
return hashlib.sha256(encoded).hexdigest()


def _is_sha256(value: object) -> bool:
token = value if isinstance(value, str) else ""
return len(token) == 64 and all(char in "0123456789abcdef" for char in token)


def _is_non_empty_string(value: object) -> bool:
return isinstance(value, str) and bool(value.strip())


def _emit_execution_record(
*,
request: dict,
approval: dict,
config: dict,
executions_dir: Path,
approval_artifact_sha256: str,
) -> dict:
dispatch_id = str(request.get("dispatch_id") or "")
execution_id = (
f"execution-{_utc_now_token()}-"
Expand All @@ -68,6 +112,8 @@ def _emit_execution_record(*, request: dict, approval: dict, config: dict, execu
"status": "approved-ready",
"execution_mode": str(config.get("execution_mode") or "approval-only"),
"source_dispatch_id": dispatch_id,
"approval_payload_version": APPROVAL_PAYLOAD_VERSION,
"approval_artifact_sha256": approval_artifact_sha256,
"decision_type": str(request.get("decision_type") or ""),
"execution_plane": str(request.get("execution_plane") or ""),
"target_kind": str(request.get("target_kind") or ""),
Expand All @@ -78,7 +124,11 @@ def _emit_execution_record(*, request: dict, approval: dict, config: dict, execu
"approval": {
"required": bool(request.get("requires_approval", config.get("require_approval", True))),
"approval_file": str(approval.get("_source_file") or ""),
"dispatch_id": str(approval.get("dispatch_id") or ""),
"payload_version": approval.get("approval_payload_version"),
"artifact_sha256": str(approval.get("approval_artifact_sha256") or ""),
"approved_by": str(approval.get("approved_by") or ""),
"approved_at": str(approval.get("approved_at") or ""),
"note": str(approval.get("note") or ""),
},
}
Expand Down Expand Up @@ -110,12 +160,39 @@ def _process(config: dict, prior: dict) -> dict:
if requests_dir.exists():
for path in sorted(requests_dir.glob("*.json")):
request = _read_json(path)
dispatch_id = str(request.get("dispatch_id") or path.stem).strip()
if not dispatch_id or dispatch_id in processed:
dispatch_id = str(request.get("dispatch_id") or "").strip()
if dispatch_id in processed:
continue
if not dispatch_id or dispatch_id != path.stem:
pending_count += 1
last_request_id = path.stem
last_reason = f"invalid-approval-identity:{path.stem}"
continue

last_request_id = dispatch_id
needs_approval = bool(request.get("requires_approval", require_approval))
approval_posture = request.get("requires_approval", require_approval)
if not isinstance(approval_posture, bool):
pending_count += 1
last_reason = f"invalid-approval-identity:{dispatch_id}"
continue

needs_approval = approval_posture
canonical_request = dict(request)
canonical_request["requires_approval"] = approval_posture
current_artifact_sha256 = _approval_artifact_sha256(canonical_request)
declared_version = request.get("approval_payload_version")
declared_artifact_sha256 = request.get("approval_artifact_sha256")
if declared_version != APPROVAL_PAYLOAD_VERSION or not _is_sha256(
declared_artifact_sha256
):
pending_count += 1
last_reason = f"invalid-approval-identity:{dispatch_id}"
continue
if declared_artifact_sha256 != current_artifact_sha256:
pending_count += 1
last_reason = f"stale-approval:{dispatch_id}"
continue

approval = {}
if needs_approval:
approval_path = approvals_dir / f"{dispatch_id}.approved.json"
Expand All @@ -126,16 +203,41 @@ def _process(config: dict, prior: dict) -> dict:
approval = _read_json(approval_path)
approval["_source_file"] = str(approval_path)

approval_dispatch_id = str(approval.get("dispatch_id") or "").strip()
approval_version = approval.get("approval_payload_version")
approval_artifact_sha256 = approval.get("approval_artifact_sha256")
if (
approval_dispatch_id != dispatch_id
or approval_version != APPROVAL_PAYLOAD_VERSION
or not _is_sha256(approval_artifact_sha256)
):
pending_count += 1
last_reason = f"invalid-approval-identity:{dispatch_id}"
continue
if approval_artifact_sha256 != current_artifact_sha256:
pending_count += 1
last_reason = f"stale-approval:{dispatch_id}"
continue
if not (
_is_non_empty_string(approval.get("approved_by"))
and _is_non_empty_string(approval.get("approved_at"))
):
pending_count += 1
last_reason = f"invalid-approval-context:{dispatch_id}"
continue

result = _emit_execution_record(
request=request,
approval=approval,
config=config,
executions_dir=executions_dir,
approval_artifact_sha256=current_artifact_sha256,
)
processed[dispatch_id] = {
"status": result.get("status"),
"execution_id": result.get("execution_id"),
"execution_file": result.get("execution_file"),
"approval_artifact_sha256": current_artifact_sha256,
"timestamp_utc": _utc_now(),
}
last_execution_id = str(result.get("execution_id") or "")
Expand Down
Loading