Pcornel
Automation · Automation Fundamentals

Policy as Code

Cheat sheet · https://pcornel.dk/topic.php?topic=5169

Back to the Topic

Answer

Policy as Code expresses governance, security or operational constraints in machine-testable definitions that can evaluate proposed or observed state consistently.

Remember

  • Policy test pass ≠ runtime enforcement; the enforcement point may not use the tested policy/version.
  • Policy as code ≠ desired infrastructure state; it constrains or evaluates state rather than necessarily creating it.
  • Deny result ≠ root cause; input normalization, exemptions and policy selection must be traceable.
  • Policy in Git ≠ active policy automatically; promotion/deployment and enforcement linkage are separate boundaries.

First checks

  • opa eval -d policy.rego -i input.json 'data.main.deny' — OPA policy test
  • conftest test manifest.yaml — policy check on files
  • checkov -d . — IaC policy scan

Verify in this order

change → policy evaluation → allow/deny with reason

Where it usually breaks

A policy evaluation proves what a specific policy version decided for specific input; it does not prove that decision was enforced at runtime.

Input/desired state → request accepted → job/task terminal status → target object read-back → effective runtime state → original workload → regression/rollback readiness. Preserve at least three independent checks: prerequisite/interface or identity/transport state; the owning control/service/policy state; then effective data-plane, backend or workload state. A healthy upstream boundary does not prove the downstream consumer or return path.

Policy as Code automation boundary map

BoundaryHealthy evidenceSuspicious evidence
Intent / inputsOwner, scope and authoritative inputs are explicit and versioned where applicableHidden defaults, stale inventory or competing sources of truth
Decision / executionPlan or decision is explainable and actions are bounded per targetBlind fan-out, implicit assumptions or success based only on executor status
State / verificationTarget is re-observed and acceptance criteria prove the intended outcomeNo read-back, perpetual drift, partial side effects or unresolved exceptions
Recovery / evidenceRetry, rollback or escalation behavior is defined and auditableUnknown partial completion, unsafe retry or evidence lost during remediation

Read-only verification commands unless stated otherwise. Confirm syntax for your platform and release. Full reference: https://pcornel.dk/topic.php?topic=5169

Confirmation

Confirm action

Continue?