AI agent policy as code: Policy as Code for Shell and Apply

Day 69: Policy as Code for Shell and Apply
10 views
This guide focuses on AI agent policy as code for production systems, with practical trade-offs for reliability, security, and cost. Prompt text saying “be careful with prod” is not a control. Put OPA/Cedar (or similar) in front of every risky tool — shell, apply, terraform, IAM — so policy is code reviewed and testable.
⚡ TL;DR: Tools call authorize(input) before side effects. Policies live in git. Unit-test allow/deny.

AI agent policy as code: production guidance

python
# policy/gate.py
import subprocess, json

def authorize(action: str, input_doc: dict) -> bool:
    p = subprocess.run(
        ["opa", "eval", "-f", "values", "-d", "policy/", "data.agent.allow",
         "-i", "/dev/stdin"],
        input=json.dumps({"action": action, **input_doc}).encode(),
        capture_output=True,
    )
    out = json.loads(p.stdout or "[]")
    return bool(out and out[0] is True)
rego
# policy/shell.rego
package agent

default allow = false

allow {
  input.action == "run_shell"
  input.role == "implementer"
  startswith(input.cwd, "/workspace")
  not contains(input.cmd, "curl")
}
typescript
export async function runShell(role: string, cmd: string, cwd: string) {
  if (!authorize("run_shell", { role, cmd, cwd })) throw new Error("policy_deny");
  // proceed
}
❌ Soft-prompt “don’t run curl” while still exposing unrestricted shell.

Closing checklist

  • [ ] Policy-as-code in git
  • [ ] Authorize before side effects
  • [ ] Unit tests for allow/deny tables
  • [ ] Break-glass path with audit
  • [ ] Same gate for apply/terraform/IAM tools

Series navigation

Day 68: Abuse and Cost Attacks · Day 70: Project: Secure Coding-Agent Sandbox on ECS Fargate Last updated September 11, 2026 Related: Policy-as-Code for AI: OPA Checks Before Every Shell Execution; Day 51: Multi-Agent Roles: Planner, Implementer, Critic; Day 53: Avoiding Agent Ping-Pong.

Watch: 100 Days of AI on YouTube

Short videos from CheatCoders, one AI engineering topic per day.

Open the playlist on YouTube · CheatCoders on YouTube

Deep-dive PDF

Get the expanded guide for this post — extra diagrams-style checklists, failure modes, and a production walkthrough. Free when you subscribe to CheatCoders.

Already subscribed? or open the subscribe page.


Discover more from CheatCoders

Subscribe to get the latest posts sent to your email.

Comments

No comments yet. Why don’t you start the discussion?

Leave a comment

No account needed. Name and email are optional.