Conftest
repository·master·Indexed 25 days ago
https://github.com/open-policy-agent/conftestA utility for testing structured configuration files, such as Kubernetes manifests, Terraform, and other YAML/JSON configurations, using the Rego policy language from Open Policy Agent (OPA). It allows users to enforce policies, manage exceptions, and generate documentation from policy annotations.
What's inside conftest
- Conftest is a utility designed for writing tests against structured configuration data. It allows you to validate configurations such as Kubernetes manifests, Tekton pipeline definitions, Terraform code, Serverless configurations, or any other structured data formats. Conftest uses the Rego language from Open Policy Agent (OPA) to define and execute these policies.
Verify and test policies
masterTo run unit tests for your policies, follow the convention of naming your test files
<policy_name>_test.rego. Use theconftest verifycommand to execute them.conftest verify --policy <path_to_policy_dir>conftest verify --policy ./policyBuild, test, and run Conftest workflows with Make
masterConftest usesmakecommands to manage build and testing workflows. Use these commands to build the project, run unit tests, run acceptance tests, or execute all workflows at once.Debug policies using the --trace flag
masterWhen working with complex Rego queries, you can use the
--traceflag to see exactly how a policy is applied. This outputs a detailed execution trace from Open Policy Agent, showing rule evaluations, indexing, and failures.$ conftest test --trace deployment.yamlInstall Conftest from source
masterIf you have a working Go environment, you can install Conftest from source. The binary will be installed to your configured
$GOPATH/binfolder.CGO_ENABLED=0 go install github.com/open-policy-agent/conftest@latestUse --trace with other output formats
masterThe
--traceflag can be combined with any output format (e.g.,--output=tableor--output=json). When used this way, the trace information is written tostderr, while the formatted policy results are written tostdout.# Output trace to stderr and table format to stdout $ conftest test --trace --output=table deployment.yamlPull policies from a Git repository
masterYou can pull policies from Git repositories using the
git::protocol prefix. You can specify a subfolder within the repository using the//delimiter after the.gitsuffix.Format:
git::https://<host>/<Organization>/<Repository>.git//<sub/folder>conftest pull git::https://github.com/<Organization>/<Repository>.git//sub/folderRun tests against configuration files with Conftest
masterTo test a configuration file, write your assertions in Rego files (e.g.,
policy/deployment.rego) and then use theconftest testcommand followed by the path to your configuration file. Conftest will evaluate the configuration against your policies and report failures.package main deny contains msg if { input.kind == "Deployment" not input.spec.template.spec.securityContext.runAsNonRoot msg := "Containers must not run as root" } deny contains msg if { input.kind == "Deployment" not input.spec.selector.matchLabels.app msg := "Containers must provide app label for pod selectors" }$ conftest test deployment.yaml FAIL - deployment.yaml - Containers must not run as root FAIL - deployment.yaml - Containers must provide app label for pod selectors 2 tests, 0 passed, 0 warnings, 2 failures, 0 exceptionsIntegrate Conftest with pre-commit
masterYou can use Conftest as a pre-commit hook to validate configuration files and run policy unit tests before committing.
Add the following configuration to your
.pre-commit-config.yamlfile. Useconftest-testto validate configurations andconftest-verifyto run unit tests for your policies.repos: - repo: https://github.com/open-policy-agent/conftest rev: v0.64.0 # Use a specific tag or 'HEAD' for the latest commit hooks: - id: conftest-test args: [--policy, path/to/your/policies] # Specify your policy directory # Optional: Add the verify hook to run policy unit tests - id: conftest-verify args: [--policy, path/to/your/policies]Use installed Conftest plugins
masterOnce a plugin is installed, Conftest automatically loads all available plugins from the cache during the next execution. The plugin becomes available as a subcommand in the Conftest CLI based on the
namedefined in the plugin'splugin.yaml.For example, if a plugin is named
kubectl, you can invoke it usingconftest kubectlfollowed by the plugin's specific arguments and policies.conftest kubectl deployment <deployment-id> --policy examples/kubernetes/policyInstall Conftest via Homebrew
masterOn macOS or Linux, you can install Conftest using the Homebrew package manager.
brew install conftestDocument policies using METADATA annotations
masterYou can provide structured documentation for your OPA policies by using
METADATAannotations directly in your policy files. To ensure meaningful generated documentation, packages should include at least atitlefield, and rules should include bothtitleanddescriptionfields.# METADATA # title: My rule # description: A rule that determines if x is allowed. # authors: # - John Doe <john@example.com> # entrypoint: true allow if { ... }