|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import argparse |
| 4 | +import importlib.util |
| 5 | +import json |
| 6 | +import sys |
| 7 | +from collections.abc import Callable |
| 8 | +from pathlib import Path |
| 9 | +from typing import Any, cast |
| 10 | + |
| 11 | + |
| 12 | +def _ensure_repo_root_on_syspath() -> None: |
| 13 | + repo_root = Path(__file__).resolve().parents[2] |
| 14 | + root = str(repo_root) |
| 15 | + if root not in sys.path: |
| 16 | + sys.path.insert(0, root) |
| 17 | + |
| 18 | + |
| 19 | +def _build_request() -> object: |
| 20 | + _ensure_repo_root_on_syspath() |
| 21 | + from predicate_contracts import ( # pylint: disable=import-error |
| 22 | + ActionRequest, |
| 23 | + ActionSpec, |
| 24 | + PrincipalRef, |
| 25 | + StateEvidence, |
| 26 | + VerificationEvidence, |
| 27 | + ) |
| 28 | + |
| 29 | + return ActionRequest( |
| 30 | + principal=PrincipalRef(principal_id="agent:root"), |
| 31 | + action_spec=ActionSpec( |
| 32 | + action="task.delegate", |
| 33 | + resource="worker:queue/main", |
| 34 | + intent="delegate processing to worker agent", |
| 35 | + ), |
| 36 | + state_evidence=StateEvidence(source="delegate.py", state_hash="sha256:delegate"), |
| 37 | + verification_evidence=VerificationEvidence(), |
| 38 | + ) |
| 39 | + |
| 40 | + |
| 41 | +def _run_worker( |
| 42 | + worker_script: str, |
| 43 | + token: str, |
| 44 | + secret_key: str, |
| 45 | + revocation_file: str, |
| 46 | + policy_file: str, |
| 47 | +) -> dict[str, object]: |
| 48 | + worker_run = _load_worker_runner(worker_script) |
| 49 | + payload = worker_run( |
| 50 | + token=token, |
| 51 | + secret_key=secret_key, |
| 52 | + revocation_file=revocation_file, |
| 53 | + policy_file=policy_file, |
| 54 | + ) |
| 55 | + if not isinstance(payload, dict): |
| 56 | + raise RuntimeError("worker payload must be an object") |
| 57 | + return cast(dict[str, object], payload) |
| 58 | + |
| 59 | + |
| 60 | +def _load_worker_runner(worker_script: str) -> Callable[..., Any]: |
| 61 | + worker_path = Path(worker_script).resolve() |
| 62 | + spec = importlib.util.spec_from_file_location("delegation_worker_runtime", worker_path) |
| 63 | + if spec is None or spec.loader is None: |
| 64 | + raise RuntimeError(f"Unable to load worker module from path: {worker_script}") |
| 65 | + module = importlib.util.module_from_spec(spec) |
| 66 | + spec.loader.exec_module(module) |
| 67 | + run_callable = getattr(module, "run", None) |
| 68 | + if not callable(run_callable): |
| 69 | + raise RuntimeError("Worker module must expose callable run(...) function.") |
| 70 | + return cast(Callable[..., Any], run_callable) |
| 71 | + |
| 72 | + |
| 73 | +def run( |
| 74 | + policy_file: str, |
| 75 | + worker_script: str, |
| 76 | + revocation_file: str, |
| 77 | + secret_key: str, |
| 78 | +) -> dict[str, object]: |
| 79 | + _ensure_repo_root_on_syspath() |
| 80 | + from predicate_authority import AuthorityClient # pylint: disable=import-error |
| 81 | + |
| 82 | + context = AuthorityClient.from_policy_file( |
| 83 | + policy_file=policy_file, |
| 84 | + secret_key=secret_key, |
| 85 | + ttl_seconds=120, |
| 86 | + ) |
| 87 | + client = context.client |
| 88 | + |
| 89 | + decision = client.authorize(_build_request()) |
| 90 | + if not decision.allowed or decision.mandate is None: |
| 91 | + return { |
| 92 | + "root_allowed": False, |
| 93 | + "worker_allowed_before_revoke": False, |
| 94 | + "worker_allowed_after_revoke": False, |
| 95 | + } |
| 96 | + |
| 97 | + token = decision.mandate.token |
| 98 | + Path(revocation_file).write_text( |
| 99 | + json.dumps({"revoked_principal_ids": []}, indent=2), |
| 100 | + encoding="utf-8", |
| 101 | + ) |
| 102 | + before = _run_worker(worker_script, token, secret_key, revocation_file, policy_file) |
| 103 | + |
| 104 | + client.revoke_principal("agent:root") |
| 105 | + Path(revocation_file).write_text( |
| 106 | + json.dumps({"revoked_principal_ids": ["agent:root"]}, indent=2), |
| 107 | + encoding="utf-8", |
| 108 | + ) |
| 109 | + after = _run_worker(worker_script, token, secret_key, revocation_file, policy_file) |
| 110 | + |
| 111 | + return { |
| 112 | + "root_allowed": True, |
| 113 | + "root_delegation_depth": decision.mandate.claims.delegation_depth, |
| 114 | + "root_chain_hash": decision.mandate.claims.delegation_chain_hash, |
| 115 | + "worker_allowed_before_revoke": bool(before.get("allowed", False)), |
| 116 | + "worker_allowed_after_revoke": bool(after.get("allowed", False)), |
| 117 | + "worker_delegation_depth_before_revoke": before.get("delegation_depth"), |
| 118 | + "worker_chain_verified_before_revoke": bool(before.get("chain_verified", False)), |
| 119 | + "before_reason": before.get("reason"), |
| 120 | + "after_reason": after.get("reason"), |
| 121 | + } |
| 122 | + |
| 123 | + |
| 124 | +def main() -> None: |
| 125 | + parser = argparse.ArgumentParser( |
| 126 | + description="Delegation simulation for local authority runtime." |
| 127 | + ) |
| 128 | + parser.add_argument( |
| 129 | + "--policy-file", |
| 130 | + default="examples/delegation/policy.yaml", |
| 131 | + help="Path to policy file for the root delegating agent.", |
| 132 | + ) |
| 133 | + parser.add_argument( |
| 134 | + "--worker-script", |
| 135 | + default="examples/delegation/worker.py", |
| 136 | + help="Path to worker.py.", |
| 137 | + ) |
| 138 | + parser.add_argument( |
| 139 | + "--revocation-file", |
| 140 | + default="examples/delegation/revocations.json", |
| 141 | + help="Path to revocation state shared with worker.", |
| 142 | + ) |
| 143 | + parser.add_argument("--secret-key", default="dev-secret") |
| 144 | + args = parser.parse_args() |
| 145 | + payload = run( |
| 146 | + policy_file=args.policy_file, |
| 147 | + worker_script=args.worker_script, |
| 148 | + revocation_file=args.revocation_file, |
| 149 | + secret_key=args.secret_key, |
| 150 | + ) |
| 151 | + print(json.dumps(payload, indent=2, sort_keys=True)) |
| 152 | + |
| 153 | + |
| 154 | +if __name__ == "__main__": |
| 155 | + main() |
0 commit comments