SECURITY ENGINEERING · PART 9 · DEVSECOPS AND SUPPLY CHAIN · IAC · 132 OF 229

OPA Security Guardrails: Rego Rules That Block Risk

A guardrail is a rule that automatically blocks a risky action instead of relying on people to remember. This episode covers policy as code with Open Policy Agent: what OPA and Rego are, enforcing policy in CI and at Kubernetes admission, paved roads versus blockers, writing clear allow/deny rules, testing policies, and how guardrails scale a security team by making the safe path the default.

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 9
  9. 10
  10. 11
  11. 12
  12. 13
  13. 14
  14. 15
  15. 16
  16. 17
  17. 18
  18. 19
  19. 20
  20. 21
  21. 22
  22. 23
  23. 24
  24. 25
  25. 26
  26. 27
  27. 28
  28. 29
  29. 30
  30. 31
  31. 32
  32. 33
  33. 34
  34. 35
  35. 36
  36. 37
  37. 38
  38. 39
  39. 40
  40. 41
  41. 42
  42. 43
  43. 44
  44. 45
  45. 46
  46. 47
  47. 48
  48. 49
  49. 50
  50. 51
  51. 52
  52. 53
  53. 54
  54. 55
  55. 56
  56. 57
  57. 58
  58. 59
  59. 60
  60. 61
  61. 62
  62. 63
  63. 64
  64. 106
  65. 107
  66. 108
  67. 109
  68. 110
  69. 111
  70. 112
  71. 113
  72. 114
  73. 115
  74. 116
  75. 117
  76. 118
  77. 119
  78. 120
  79. 121
  80. 122
  81. 123
  82. 124
  83. 125
  84. 126
  85. 127
  86. 128
  87. 129
  88. 130
  89. 131
  90. 132
  91. 133
  92. 134
  93. 135
  94. 136
  95. 137
  96. 138
  97. 139
  98. 140
  99. 141
  100. 142
  101. 143
  102. 144
1 / 10

Press play to hear the narration, or step through at your own pace.

Key takeaways

  • Turn policy into code that blocks automatically
  • OPA/Rego in CI and at admission
  • warn then enforce
  • pave the road

Notes

What OPA is

Open Policy Agent, or OPA, is a general-purpose policy engine, deliberately separate from any one system. You send it some input, a deploy request, a resource definition, a Terraform plan, and it evaluates your rules and returns a decision: allow or deny, usually with a reason. The rules are written in a language called Rego. The point of a separate engine is reuse: the same tool and the same style of policy enforce rules in Kubernetes, in CI pipelines, in APIs and over infrastructure code, so you learn it once.

Open Policy Agent
  • a general-purpose policy engine, decoupled from any one system
  • you send it input (a request, a resource, a plan)
  • it evaluates your Rego rules and returns allow or deny
  • Rego: the policy language
  • one engine, many uses: K8s, CI, APIs, Terraform

At Kubernetes admission

The classic use is Kubernetes admission control, which the signing episode touched on. An admission controller calls OPA on every deployment and denies anything that breaks policy: an unsigned image, a container running as root, a privileged pod, an image tagged latest, a workload with no resource limits. Gatekeeper is the OPA-based option, and Kyverno is a popular YAML-based alternative. The effect is that the cluster itself enforces your standards on every deploy, not a reviewer catching violations.

The classic use
  • an admission controller calls OPA on every deploy
  • deny: no unsigned images, no running as root, no privileged pods, no latest tag, resource limits set
  • tools: Gatekeeper (OPA), Kyverno
  • the cluster enforces policy, not a human reviewer

In the pipeline

The same engine works earlier, in the pipeline. You evaluate Terraform plans, manifests and configuration files against policy in CI and block the pull request or the apply when they violate it. A tool called conftest runs OPA policies over config files. The nice property is reusing the same policies in CI and at admission: CI catches violations early for fast feedback, and admission enforces them at the last moment as a backstop, so a change that skips CI still cannot deploy. That is defence in depth for policy.

Guardrails in CI
  • evaluate Terraform plans, manifests, configs against policy in CI
  • block the PR or the apply on violations
  • conftest: run OPA policies over config files
  • same policies as admission: catch early AND enforce late
  • defence in depth

Writing a rule

Here is what a policy looks like. This Rego rule denies a pod if any container does not set run-as-non-root, and returns a clear message naming the container. The pattern is deny rules: allowed unless a rule objects, and each objection carries a human-readable reason. Good messages matter, because the developer who hits the guardrail needs to know what to fix. A guardrail that says access denied with no explanation makes a support ticket; one that says runs as root, set runAsNonRoot teaches.

a Rego guardrail
package main

deny[msg] {
  input.kind == "Pod"
  c := input.spec.containers[_]
  not c.securityContext.runAsNonRoot
  msg := sprintf("%s runs as root",
    [c.name])
}
# deny unless runAsNonRoot is set

Paved roads

The deepest idea here is the paved road. A guardrail that only blocks leaves a developer stuck, hunting for how to comply. A paved road pairs the block with a secure default that just works: a golden Terraform module, a base manifest, a template that passes policy out of the box. You make the compliant path the easiest path, so people choose it naturally, and the guardrail is only there for the rare cliff edge. Guardrails plus golden modules give secure-by-default infrastructure.

Guide, do not just block
  • blocker: "denied", developer is stuck and annoyed
  • paved road: a secure default template or module that just passes
  • make the compliant path the easiest path
  • guardrails + golden modules = secure by default
  • block the cliff edges, pave the common road

Test your policies

Treat policies as the code they are. Version them, review them, and test them: OPA has a test framework, and a policy that wrongly allows or blocks is a bug. Roll a new guardrail out in warn mode first, logging what it would block, before switching to enforce, so you do not break everyone on day one. Allow audited exceptions rather than silent bypasses. Write clear deny messages with the fix. And start with a handful of high-value rules, not hundreds, so the signal stays strong.

Operate guardrails well
  • policies are code: version, review and TEST them (OPA has a test framework)
  • roll out in warn mode first, then enforce
  • allow audited exceptions, not silent bypasses
  • clear deny messages with a fix
  • start with a few high-value rules, not hundreds

Common mistakes

Mistakes. Enforcing a brand-new policy with no warn period, which breaks every team at once and burns trust. Cryptic deny messages that do not say how to fix the problem. Blockers with no paved road, so developers are stuck. Untested policies that wrongly allow something dangerous or wrongly block something fine. Turning on hundreds of rules at once instead of a focused few. And allowing silent bypasses instead of audited, reviewed exceptions, which quietly hollows out the whole system.

Avoid these
  • enforcing a new policy with no warn period
  • cryptic deny messages, no fix given
  • blockers with no paved road
  • untested policies that allow or block wrongly
  • hundreds of rules at once
  • silent bypasses instead of audited exceptions

Say it like this

In the interview: I enforce policy as code with Open Policy Agent and Rego, reusing the same policies in CI with conftest and at Kubernetes admission with Gatekeeper, caught early and enforced late. I write deny rules with clear fix messages, roll each new guardrail out in warn mode before enforcing, and test policies like any code. Crucially, I pair blockers with paved roads, golden modules and templates that pass policy by default, so the secure path is the easy one, and I allow audited exceptions.

In the interview
  • "Policy as code with OPA and Rego: the same policies in CI
  • (conftest) and at Kubernetes
  • admission (Gatekeeper). Deny
  • rules with clear fix messages, rolled out warn-then-enforce, tested like code. I pair
  • blockers with paved roads so the secure path is the easy
  • one, and allow audited
  • exceptions."

Interview questions

Q1What is a security guardrail?

A rule enforced automatically by the system to block or constrain a risky action, rather than relying on people to remember and follow a policy. Guardrails make the secure behaviour the default and scale a security team's reach.

Q2What is Open Policy Agent (OPA)?

A general-purpose policy engine that evaluates input against rules written in the Rego language and returns allow or deny decisions. Being decoupled from any single system, it enforces policy across Kubernetes, CI pipelines, APIs and infrastructure code.

Q3What is policy as code?

Expressing organisational and security policies as versioned, testable code that an engine enforces automatically, rather than as documents people must follow manually. It makes policies reviewable, consistent and enforced at machine speed.

Q4How is OPA used with Kubernetes?

Through an admission controller, often OPA Gatekeeper, that calls the policy engine on every deploy and rejects workloads that violate policy, such as unsigned images, containers running as root, privileged pods or missing resource limits. Kyverno is a YAML-based alternative.

Q5How do you use the same policies in CI and at admission?

Write the policies once in Rego and evaluate them in CI against plans and manifests (for example with conftest) for early feedback, and again at admission as a backstop. This gives defence in depth so a change that bypasses CI still cannot deploy.

Q6What is a "paved road" in platform security?

A secure default path, such as a golden module, base template or starter that passes policy out of the box, so the compliant option is also the easiest. Pairing paved roads with guardrails makes infrastructure secure by default rather than merely blocked when wrong.

Q7Why roll out a new guardrail in warn mode first?

To observe what it would block without breaking anyone, allowing teams to fix violations before enforcement. Enforcing a new policy immediately can halt many teams at once and erode trust in the guardrail system.

Q8Why should policies be tested?

Because policies are code and can contain bugs: a faulty rule may wrongly allow a dangerous configuration or wrongly block a valid one. OPA provides a test framework, and testing ensures guardrails behave as intended before they gate real changes.