CtrlK
BlogDocsLog inGet started
Tessl Logo

nicholasjackson/golang-opa-rego-language

Rego is the declarative policy language used by Open Policy Agent (OPA) for writing and enforcing policies across cloud-native stacks, featuring data-driven rules, comprehensions, and 200+ built-in functions for infrastructure, security, and compliance automation.

Overall
score

97%

Overview
Eval results
Files

task.mdevals/scenario-3/

Task: Kubernetes Admission Control Policy

Create a Kubernetes admission control policy in Rego that validates Pod specifications and produces descriptive denial messages.

Requirements

Policy file

Create admission.rego with package kubernetes.admission and import rego.v1.

The policy evaluates a Kubernetes AdmissionReview input. Containers are accessible at input.request.object.spec.containers.

Define a multi-value deny rule (a set of strings) — do not use a boolean allow/deny pattern. Each violation should produce a distinct, human-readable message string.

Implement the following checks:

1. Approved image registry

All container images must be pulled from registry.company.com. If a container's image does not start with registry.company.com/, produce a message of the form:

container <name> uses image <image> from an unapproved registry

2. Non-root execution

Every container's securityContext.runAsNonRoot must be true. If it is absent or false, produce a message of the form:

container <name> must set securityContext.runAsNonRoot = true

3. Resource limits

Every container must define both resources.limits.cpu and resources.limits.memory. If either is missing, produce a message of the form:

container <name> must define resource limits for cpu and memory

Test file

Create admission_test.rego with package kubernetes.admission_test and import rego.v1.

Use the with input as { ... } keyword to inject mock Kubernetes objects. Include tests for:

  • A fully compliant pod (all containers pass all checks) — deny must be an empty set
  • A pod with a container using an image from an unapproved registry — deny must contain the expected message
  • A pod with a container missing runAsNonRoot: truedeny must contain the expected message
  • A pod with a container missing resource limits — deny must contain the expected message

Run the tests

Run opa test . -v and confirm all tests pass.

Install with Tessl CLI

npx tessl i nicholasjackson/golang-opa-rego-language

README.md

tile.json