Write a policy
Optional: hand this page to your coding agentThe steps work by hand too.Show the whole prompt
Read https://intentius.io/terragucci/guides/write-a-policy/.
Write a Rego rule under the policy directory that denies the case I name, with a test, run `conftest verify --policy policy`,
add the `policy:` key to terragucci.yml, run `npx terragucci config check --json` and `npx terragucci init`, and open a pull request.
Never apply, approve (a pull request review or `terragucci approve`), override a policy denial (`terragucci override`), use `--mode apply`, or merge; never touch `.chant/allowed_signers` or `chant/lifecycle`.Result
Section titled “Result”A rule in your repo that fails tf-plan for each root whose plan it denies, names the denial in the plan note, and keeps a denied wave from applying.
Prerequisites
Section titled “Prerequisites”| You need | Why |
|---|---|
| The pipeline from Get your first plan note | the plan, check and apply jobs run the policy |
| conftest on your machine, to run the tests before you push | the jobs download the engine themselves |
-
Write the rule in
policy/. It reads one root’s plan,show -json, asinput:policy/sqs.rego package mainimport rego.v1deny_long_retention contains msg if {some rc in input.resource_changesrc.type == "aws_sqs_queue"rc.change.after.message_retention_seconds > 345600msg := sprintf("%s keeps messages longer than four days", [rc.address])}deny,violationand anydeny_<name>fail the root;warnonly warns. Policy lists the rules and the input. -
Test it beside the rule and run the tests:
policy/sqs_test.rego package mainimport rego.v1test_denies_a_week if {count(deny_long_retention) == 1 with input as {"resource_changes": [{"address": "aws_sqs_queue.jobs", "type": "aws_sqs_queue", "change": {"after": {"message_retention_seconds": 604800}}}]}}Terminal window conftest verify --policy policytf-checkruns the same tests on the default branch’s policy and fails on a failing one. -
Turn policy on in
terragucci.yml:policy:engine: conftestpath: policy -
Check the file and write the pipeline again:
Terminal window npx terragucci config checknpx terragucci init -
Open a pull request with the rule, its test, the key and the pipeline, and merge it.
A pull request is checked against the
policykey and directory on its base branch. Until they merge, a change that edits or deletes the policy cannot loosen it. -
Open a pull request with a change the rule denies. The plan job fails, and the note names the root and the denial.
The denial and its rule id, such as
main.deny_long_retention, are inroots[].policyof the report. A sensitive value in a message is redacted before it reaches the note or the log. -
Clear the denial. Change the code until the rule passes, or change the rule in a pull request your reviewers approve and merge.
Override a denial
Section titled “Override a denial”When a denied plan has to go out, a person you list can let that one plan through tf-apply. Name them at base:
policy:
engine: conftest
path: policy
override: [github:alice, github:bob]The wave records the denial and prints a terragucci override command for the root. Override a policy denial has the steps, and Overriding a denial what an override binds. tf-plan still fails the root; the override counts only in the apply wave, and only for that plan and those rules.
Allow only listed sources
Section titled “Allow only listed sources”A policy reads the plan once the root’s modules and providers have run. sources acts earlier: a root that uses a module source or provider address no entry matches is refused before init downloads anything for it. What init then fetched is checked again before plan.
sources:
modules:
- "./**"
- "../modules/**"
- "git::https://git.example.com/platform/**"
- "registry.example.com/acme/*/*"
providers:
- "hashicorp/*"
- "registry.example.com/acme/*"| What is read | Where | When |
|---|---|---|
each module block’s source |
the root’s files, and each local module it calls, down the tree | before init |
each required_providers entry |
the same files; a provider used with no entry is read as hashicorp/<name> |
before init |
terraform { source } |
a Terragrunt unit’s terragrunt.hcl |
before init |
a unit block’s remote source |
the terragrunt.stack.hcl that generates the unit |
before the unit’s init |
each module call inside a module init fetched |
.terraform/modules/modules.json, matched as written in the module that makes the call |
after init, before plan |
each provider init installed |
.terraform.lock.hcl, named by the module that uses it |
after init, before plan |
| the same two files for a Terragrunt unit | the unit’s working directory, where Terragrunt inited it | after Terragrunt’s init, before its plan |
| Matching | |
|---|---|
* |
within one /-separated part |
** |
across parts |
| a module source | as written, without its ? query (?ref=, ?version=, ?tag=) |
an oci:// source |
with its scheme, so oci://ghcr.io/acme/** matches it and ghcr.io/acme/** does not |
an address with no host (hashicorp/aws, acme/vpc/aws) |
also as registry.terraform.io/... and registry.opentofu.org/... |
an address at registry.terraform.io or registry.opentofu.org |
also without the host, so hashicorp/* matches what the lock file names |
| a provider address | in lower case |
A list you leave out checks nothing of its kind. A refused root fails the stage that reads it (tf-plan, tf-drift or a tf-apply wave) without running init, or without running plan when the source came in through init. The error in the report names the module path, the source and the entries it was checked against:
refused: envs/dev/app: module.vpc (envs/dev/app/main.tf) uses module source git::https://git.unlisted.example/acme/vpc.git?ref=v1, which no entry of sources.modules matches (sources.modules: ./**, ../modules/**, ...)
refused: envs/dev/app: module.net > module.subnets (envs/dev/app/.terraform/modules/net/main.tf) uses module source git::https://git.unlisted.example/acme/subnets.git, which no entry of sources.modules matches (sources.modules: ./**, ../modules/**, ...)A Terragrunt unit’s refusal names the unit and the module path from the unit’s source, with the file in its working directory:
refused: live/dev/app: module.subnets (live/dev/app/.terragrunt-cache/sources/k1/k2/main.tf) uses module source git::https://git.unlisted.example/acme/subnets.git, which no entry of sources.modules matches (sources.modules: ./**, ../**, ...)Run terragucci init after you add the key. The check job it writes then runs terragucci check-sources on each root, and refuses the root before its init. In a Terragrunt repo it runs terragucci check-sources --units, which checks every unit before terragrunt hcl validate fetches any unit’s source.
The list is read at base, as policy is. A pull request that adds an entry, or deletes the key, is checked against the base’s list; the entry counts for the changes after it merges. A base with no sources key leaves the pull request’s own in force. The threat model lists what the check reads and what it does not.
terragucci’s CI images carry the HCL parser the check reads with; elsewhere, npm i -D @cdktn/hcl2json.
Sources by root glob
Section titled “Sources by root glob”A repo-wide list has to allow a provider everywhere to keep the one root that needs it. Write sources as a map of root glob to lists instead, and a provider can be allowed where it is used and refused elsewhere:
sources:
"projects/prow/**":
modules: ["./**"]
providers: ["hashicorp/google", "hashicorp/http"]
"projects/**":
providers: ["hashicorp/google"]
default:
modules: ["./**"]
providers: ["hashicorp/google"]A root takes the first glob in the file that matches it, as version does, so the narrow globs go first. An entry holds modules, providers or both. An instance of a directory that instances runs more than once takes the entry its own name or its directory matches.
default is required. It serves a root no glob matches and a root the pull request adds: a root with no Terraform file in its directory at the base takes default whatever glob its path matches. So a pull request cannot give a new root a longer list than the base gave the existing ones, and a root moved to a new directory is checked as new until the move merges.
proxies stays one list for the repo, written beside the globs. A top-level modules or providers cannot sit beside root globs; put those under default. A root directory called modules, providers, proxies or default takes a glob with a trailing slash, such as "modules/".
terragucci config check refuses a map with no default and an entry with no list. The plan-time execution section of the threat model says which data sources and provisioners run code or reach the network, and which of them sources stops.
Sources through a registry proxy
Section titled “Sources through a registry proxy”A team that reads the public registry through Artifactory or Nexus names the proxy under proxies. A source written through it then matches the entries as the upstream source it stands for, so one entry covers both forms:
sources:
modules:
- "registry.terraform.io/terraform-aws-modules/*/*"
proxies:
- artifactory: acme.jfrog.io # acme.jfrog.io/tf-remote__terraform-aws-modules/vpc/aws
repo: tf-remote
- nexus: https://nexus.example.com # behind a CLI host "registry.terraform.io" block
repo: terraform-proxyupstream names another upstream host than registry.terraform.io, and a Nexus entry’s host names the label of its CLI host block when it is not the upstream. Under modules.require: attested, a pin written through a proxy is checked as the upstream’s release and read through the proxy, with the token Terraform would send the proxy’s host.
- Policy has a shared policy source,
opa, HCP Terraform policy sets, cost and review input. - The audit trail keeps every override with its reason.
terragucciHelp setting it up: Intentius
These docs count page views and clicks with PostHog. They set no cookies, store nothing in your browser, and send nothing when your browser asks not to be tracked.