OPA / Rego Snippet Helper

Pick a policy pattern, enter a few values and get a Rego v1 policy for RBAC, deny lists, Kubernetes admission or Terraform plan checks.

Use the tool

Why this tool exists

Rego is easy to read once you know it, but the first policy is hard to write from a blank file. The syntax changed with Rego v1, so older blog examples now fail without import rego.v1, and the contains and in keywords trip up people used to the earlier style.

This helper offers six common patterns and fills in your package name and values: roles for RBAC, a set of blocked names, required Kubernetes labels, a privileged container check, a public S3 ACL check on a Terraform plan and a simple HTTP method and path rule. Treat the output as a starting point and test it with opa eval or opa test.

How to use it

  1. Choose a template, such as Kubernetes: required labels.
  2. Enter the values the template needs, one per line, and set the package name.
  3. Copy the Rego code into a .rego file and test it with opa eval.

Frequently asked questions

Which Rego syntax does it produce?

Rego v1. Each policy starts with import rego.v1 and uses keywords such as if, contains and in, which current OPA releases expect.

What are the values for?

They feed the template. For RBAC they become role names, for a deny list the blocked values, and for required labels the label keys. Templates that need no values ignore the field.

Are the policies safe to deploy as they are?

No. They are examples. Check the input shape against what your system actually sends to OPA, and write tests before enforcing anything.

Can it write a policy from my own rules?

No. It only fills in the six built-in patterns. Edit the generated code by hand for anything custom.