Policy, security, and cost gates¶
Checks run at two moments, and they are not interchangeable. On commit, the pre-commit hooks check the source. On plan, the gates check the generated plan, where the Azure Verified Modules have expanded into real resources.
On commit: hooks and the pre-commit job¶
.pre-commit-config.yaml runs formatting, tflint, checkov, and the repository
invariants in .github/scripts/check-repo-invariants.sh. Wire it up once with
pre-commit install; the PR caller's pre-commit job runs the identical hooks
on every pull request and gates the dispatch, so --no-verify buys nothing:
a failed lint stops the plan and leaves the required merge gate uncreated.


The invariants catch what the linters cannot: a generated backend.tf,
providers.tf, or context.tf staged by hand (they carry the resolved tenant
and subscription ids), Terraform state, a terragrunt.stack.hcl, and a
directory holding Terraform but no terragrunt.hcl. The last two are the
silent failures: discovery skips them, so the PR plans nothing, every gate
passes with nothing to gate, and it merges green having deployed nothing. The
invariants also fail when the engine callers pin different tfpr
releases, because callers on different releases dispatch work to each other
across a contract change. The one mix they allow is the first half of a
two-PR bump: the ops and drift callers ahead of the PR, unlock, and review callers. The check also warns, without blocking, about a unit with no
.terraform.lock.hcl; see
Versions and upgrades.
checkov at this layer sees only the module calls, not what the modules expand to. The scan that matters is the plan-time one below, where it reads the real resources out of the plan JSON.
On plan: the gates¶
Every plan, and every drift plan, runs five checks against the Terraform plan;
an apply does not run them again on the plan it re-takes.
Findings appear in the PR run comment and in drift issues, one row per gate
below the plan output, titled with the hook's name from projects.yml and a
one-line verdict (for example Security scan (checkov) · 5 failed, 11 passed).
Each row is a compact list: checkov's failed checks and conftest's findings,
both grouped by resource, and infracost's monthly cost per priced resource.
A warned hook's row starts with "Report only: on_failure: warn, the merge is
not blocked." and its verdict reads N findings, report only · M passed. A row is collapsed when its gate passed and opens by itself
when the gate failed or warned, so a finding is never left hidden behind a
click. An open row's title shows warning in yellow or
failed in red before the verdict, so an advisory finding and a blocking
one look different.
| Gate | Tool | Checks | Config |
|---|---|---|---|
| Policy | conftest | policy/*.rego against the plan JSON |
$TFPR_ENGINE_DIR/scripts/conftest-gate.sh |
| Security | conftest | Azure/policy-library-avm avmsec rules, high severity only |
$TFPR_ENGINE_DIR/scripts/avm-policy-gate.sh avmsec |
| Reliability | conftest | Azure/policy-library-avm APRL rules | $TFPR_ENGINE_DIR/scripts/avm-policy-gate.sh aprl |
| Security | checkov | built-in checks against the plan JSON | $TFPR_ENGINE_DIR/scripts/checkov-gate.sh |
| Cost | infracost | monthly cost of the plan | $TFPR_ENGINE_DIR/scripts/infracost-gate.sh |
The gate scripts live in the engine, not in the customer repo. The post_plan
hooks in projects.yml run them from $TFPR_ENGINE_DIR, the engine runtime the
reusable workflow installs (or the workspace in vendored mode).
The AVM policy library¶
The Azure Verified Modules create many resources through the azapi provider,
as azapi_resource. checkov has no checks for those, so on an AVM plan it often
checks nothing; its verdict says so, for example
nothing checked, 0 of 4 resources. Two more conftest rows close part of that
gap with Azure/policy-library-avm,
Microsoft's Rego library for AVM plans, which checks both azapi_resource and
azurerm_* resources:
- Security scan (avmsec): security rules ported from checkov. Only high-severity rules run, through the library's own default exemption file.
- Reliability check (APRL): the Azure Proactive Resiliency Library rules (zones, redundant SKUs, autoscale). Every rule runs.
Both hooks call the engine's avm-policy-gate.sh with the bundle name
(avmsec or aprl). It runs the library the way the AVM modules' own CI
does: it downloads the pinned release (v1.0.0), checks its SHA256, and tests
the bundle with every rego namespace, because the rules sit in their own
packages. A failed download or a checksum mismatch is an error row, and the
bundle is not run. The library covers a limited set of resource types. It has
no Key Vault rules, for example, so a clean row is not a full security review.
To skip a rule, commit a Rego file with an exception rule naming it under
avm-policy-exceptions/avmsec/ or avm-policy-exceptions/aprl/ (the
library's README shows the form):
package Azure_Proactive_Resiliency_Library_v2
import rego.v1
exception contains rules if {
rules := ["storage_accounts_are_zone_or_region_redundant"]
}
The run log names every exemption file the gate adds and carries conftest's
own summary line (79 tests, 77 passed, 0 warnings, 1 failures, 1 exceptions),
so a skipped rule stays visible. The folder is read from the pull request's
checkout, like policy/, so an exemption is reviewed with the PR that adds
it. Other settings, on the hook's run line:
| Variable | Effect |
|---|---|
AVM_POLICY_ALL_SEVERITIES=1 |
run every avmsec rule, not only high severity |
AVM_POLICY_EXCEPTIONS_DIR |
another folder for the exemption files |
AVM_POLICY_LIBRARY_DIR |
a local copy of the library instead of the download |
AVM_POLICY_LIBRARY_REF and AVM_POLICY_LIBRARY_SHA256 |
another release and its tarball hash, always together |
conftest and checkov are baked into the self-hosted runner image, so there
TFPR_EXTRA_TOOLS only adds infracost. On a GitHub-hosted posture, set
TFPR_EXTRA_TOOLS to conftest checkov infracost to install all three per run.
infracost is a static binary in either case.
Each gate also writes a small JSON result for the engine, and the per-unit
check shows one line per gate from it: checkov and conftest pass, fail,
skipped and warning counts, and the infracost monthly total. The check also
lists every hook by name with its status, so a
warned checkov scan reads ⚠️ Security scan (checkov) (post_plan): 5 failed,
11 passed.
The counts come from the tools' JSON output, never from the rendered
markdown. A custom post_plan hook can add its own line by writing
$TFPR_RESULTS/<name>.json:
status is pass, fail, skipped or error; the counts,
monthly_cost, currency and verdict are optional. verdict is one plain
line, lowercase-first and with no full stop (for example
no policy violations, 2 passed), and becomes the hook row's summary.
Report-only by default¶
All the gates start quiet, so they surface findings without blocking a merge:
- conftest runs whatever matches
policy/*.rego, and the repository ships nothing matching, so the gate row reads "skipped, no policies". It is quiet by absence, not advisory by design: an active policy'sdenyrules block, itswarnrules do not. - avmsec and APRL run with
on_failure: warn, so a finding opens the row as a warning but never blocks merge or apply. - checkov runs with
on_failure: warnon its hook, so a failed check opens the row as a warning but never blocks merge or apply. - infracost is informational. The hook always exits zero and never gates.
This lets you turn the gates on against an existing tenant without a wall of findings blocking every PR on day one.
Enforcing a gate¶
Enforce deliberately, after reviewing the report-only findings.
- conftest. Nothing is active until you add a
.regofile topolicy/. The repository shipspolicy/tags.rego.example, a starter that denies untagged resource groups; activate it withgit mv policy/tags.rego.example policy/tags.rego, after reading what it denies. A deny fails the gate and blocks the PR. The example also documents the load-bearing plan-JSON patterns: match the right resource type,change.actionsis an array (test membership, never index zero),change.afteris null on a destroy (guard it, or the deny fails open), and an empty string counts as a present tag value. - avmsec and APRL. Drop
on_failure: warnfrom their hooks inprojects.ymlonce the findings are fixed or exempted underavm-policy-exceptions/. - checkov. Drop
on_failure: warnfrom the checkov hook inprojects.yml, or scope it withCHECKOV_HARD_FAIL_ON(check IDs) once findings are triaged. - infracost. Cost stays informational. Use it to review the monthly delta a PR introduces.
Writing a policy¶
conftest policies are Rego in package main. A warn rule surfaces a finding
without gating; a deny rule blocks. Run against terraform show -json, so you
match on the plan structure:
package main
import rego.v1
deny contains msg if {
resource := input.resource_changes[_]
resource.type == "azurerm_storage_account"
"create" in resource.change.actions
not resource.change.after.tags.owner
msg := sprintf("%s must have an owner tag", [resource.address])
}
Check which provider actually creates the resource
Matching on an azurerm_* type is the most common way to write a rule that
silently does nothing. Many Azure Verified Modules declare azapi_resource
instead, so a rule keyed on the azurerm type matches no resource, finds no
violation, and reports a clean gate. It looks identical to a policy that
passed.
Azure/avm-res-resources-resourcegroup is one of them. A rule written
against azurerm_resource_group reported no violations on a resource group
that carried no tags at all.
Confirm the type before trusting a rule. In the plan JSON an azapi resource
has type set to azapi_resource and the ARM type in change.after.type,
so match both shapes:
is_resource_group(resource) if resource.type == "azurerm_resource_group"
is_resource_group(resource) if {
resource.type == "azapi_resource"
startswith(resource.change.after.type, "Microsoft.Resources/resourceGroups@")
}
Test a new rule against a plan you know should fail it, before trusting a
pass. conftest test --policy policy --namespace main <plan.json> runs the
same check locally that the gate runs in CI.
Cost gate data residency¶
infracost sends per-resource region and SKU to its hosted pricing API to look up prices. The Terraform code itself does not leave. This is an accepted trade-off: the self-hosted pricing API that would avoid the lookup is behind a paid Infracost plan.
Upload of the breakdown itself is off, and the engine is what turns it off. The gate exports INFRACOST_ENABLE_CLOUD,
INFRACOST_ENABLE_CLOUD_UPLOAD, and INFRACOST_ENABLE_DASHBOARD as false on
every run. Left unset, infracost infers the answer from the Infracost Cloud
setting on whichever organization owns the API key, which in a customer's own
account is not NRIT's to control. That matters because the breakdown runs inside
the consumer's checkout, so its JSON carries the commit sha and the author's name,
email, and message. Setting the three per run is what keeps the accepted position,
region and SKU only, true regardless of the remote default.
Use the legacy infracost line (the static-key model). Get a static API key from the
Infracost dashboard, set it as the INFRACOST_API_KEY secret, add infracost
to TFPR_EXTRA_TOOLS, and name the key under extra_env in projects.yml on
the default branch (see
The extra_env block).
Until all three are in place, the gate notes it and skips.