Conftest — Test Structured Config with Open Policy Agent
A CLI tool for writing tests against structured configuration data using the Rego policy language. Conftest validates Kubernetes manifests, Terraform plans, Dockerfiles, and any structured format against custom policies.
What it is
Conftest is a command-line tool for writing tests against structured configuration data using the Rego policy language from Open Policy Agent (OPA). It validates Kubernetes manifests, Terraform plans, Dockerfiles, and any structured format (JSON, YAML, TOML, HCL) against custom policies you define.
Conftest is for platform engineers and DevOps teams who want to enforce configuration standards automatically. Instead of reviewing YAML files by hand for security misconfigurations or missing labels, you write policies once and run them in CI.
How it saves time or tokens
This workflow provides the installation command and a starter policy structure. Instead of learning OPA from scratch and figuring out how to integrate it with your config files, you get a working conftest setup with a deny policy template. Running conftest test against your configs takes seconds and catches issues that manual reviews miss.
How to use
- Install Conftest:
brew install conftest
- Create a policy directory and write your first Rego policy:
mkdir policy
cat > policy/deny.rego << 'EOF'
package main
deny[msg] {
input.kind == "Deployment"
not input.spec.template.spec.securityContext.runAsNonRoot
msg := "Deployments must set runAsNonRoot to true"
}
deny[msg] {
input.kind == "Deployment"
not input.spec.template.metadata.labels.app
msg := "Deployments must have an app label"
}
EOF
- Test your configuration files:
conftest test deployment.yaml
# FAIL - deployment.yaml - Deployments must set runAsNonRoot to true
Example
# policy/dockerfile.rego
package main
deny[msg] {
input[i].Cmd == "from"
val := input[i].Value[0]
val == "latest"
msg := "Do not use latest tag in FROM statements"
}
deny[msg] {
input[i].Cmd == "run"
contains(input[i].Value[0], "curl")
not contains(input[i].Value[0], "--fail")
msg := "curl commands should use --fail flag"
}
# Test a Dockerfile
conftest test --parser dockerfile Dockerfile
Related on TokRepo
- Security tools -- Policy enforcement and security scanning tools
- DevOps tools -- Infrastructure and configuration management tools
Common pitfalls
- Rego syntax is different from imperative languages. The biggest confusion is that Rego rules are declarative assertions, not if-else blocks. Read the OPA documentation on Rego basics before writing complex policies.
- Conftest expects policies in a
policy/directory by default. Use--policyflag to specify a different path. - Terraform plan output must be converted to JSON with
terraform show -json plan.tfplanbefore Conftest can test it. Raw plan files are not supported.
Frequently Asked Questions
Conftest supports JSON, YAML, TOML, HCL, INI, Dockerfile, CUE, and several other structured formats. Each format has a parser that converts the input into a JSON structure that Rego policies can evaluate.
Add conftest test to your CI pipeline after generating or modifying config files. The command returns a non-zero exit code when any deny rule matches, which fails the pipeline. Most CI systems (GitHub Actions, GitLab CI, Jenkins) can run it as a shell step.
Yes. Conftest supports pulling policies from OCI registries, Git repositories, and HTTP URLs using conftest pull. This lets you maintain a central policy repository that all teams reference.
OPA is the policy engine and Rego runtime. Conftest is a CLI wrapper specifically designed for testing configuration files against Rego policies. OPA handles runtime policy decisions for APIs and services, while Conftest focuses on static config validation.
Yes. Convert your Terraform plan to JSON with terraform show -json plan.tfplan, then run conftest test plan.json. Your Rego policies can inspect planned resource changes, provider configurations, and variable values.
Citations (3)
- Conftest GitHub— Conftest uses Rego policy language from Open Policy Agent
- Conftest Documentation— Supports Kubernetes manifests, Terraform plans, Dockerfiles and more
- OPA Rego Docs— Rego is a declarative policy language for OPA
Related on TokRepo
Discussion
Related Assets
Conda — Cross-Platform Package and Environment Manager
Install, update, and manage packages and isolated environments for Python, R, C/C++, and hundreds of other languages from a single tool.
Sphinx — Python Documentation Generator
Generate professional documentation from reStructuredText and Markdown with cross-references, API autodoc, and multiple output formats.
Neutralinojs — Lightweight Cross-Platform Desktop Apps
Build desktop applications with HTML, CSS, and JavaScript using a tiny native runtime instead of bundling Chromium.