Conftest
Conftest support was improved at tfaction v1.8.0.
About Conftest, please see https://www.conftest.dev/ .
https://www.openpolicyagent.org/docs/latest/terraform/
tfaction supports validating files using Conftest. Any violation is notified as pull request comment.
tfaction doesn't provide any Conftest Policy. Please write your Conftest Policy freely.
We recommend writing the document about Conftest Policy per policy.
policy/
github_issue_label_description.rego # Policy
github_issue_label_description_test.rego # Policy Test
github_issue_label_description.md # Policy Document
Settings
By default, tfaction runs Conftest if the directory policy
exists in the repository root directory.
conftest_policy_directory
tfaction >= v1.1.0:
You can change the directory by the setting conftest_policy_directory
in tfaction-root.yaml
.
conftest_policy_directory: terraform/policy
If you configure the conftest
field, conftest_policy_directory
is ignored.
You should migrate conftest_policy_directory
to conftest
.
conftest:
- policy: terraform/policy
plan: true
conftest
tfaction >= v1.8.0:
You can configure policies at three layers.
- tfaction.yaml
- target_group in tfaction-root.yaml
- root in tfaction-root.yaml
tfaction-root.yaml:
conftest:
policies:
- policy: policy/plan
plan: true
id: plan
target_groups:
- working_directory: aws/
# ...
conftest:
disable_all: true
# ...
tfaction.yaml:
conftest:
policies:
- id: plan
enabled: false
- policy: policy/combine/tf
tf: true
combine: true
data: data
Basically, tfaction joins conftest.policies
and runs conftest test
by policy.
Using id
field, you can also overwrite the existing policy.
conftest
:
disable_all
: Boolean. If this is true, settings in previous layers are disabledpolicies
: A list of policies
conftest.policies[]
:
- tfaction specific options:
id
: unique id of policy. This is optional. This is used to overwrite the settingplan
: boolean. Whether this policy is for plan files. The default is falsetf
: boolean. Whether this policy is for *.tf and *.tf.json. The default is falseenabled
: boolean. Whether this policy is enabled. The default is true
- conftest options:
policy
: A list or a string of relative paths to a policy directory from the repository root directorydata
: A list or a string of conftest test's-data
option. A relative path to a data directory from the repository root directorycombine
: boolean. conftest test's-combine
option. The default isfalse
fail_on_warn
: boolean. conftest test's-fail-on-warn
option. The default isfalse
no_fail
: boolean. conftest test's-no-fail
option. The default isfalse
all_namespaces
: boolean. conftest test's-all-namespaces
option. The default isfalse
quiet
: boolean. conftest test's-quiet
option. The default isfalse
trace
: boolean. conftest test's-trace
option. The default isfalse
strict
: boolean. conftest test's-strict
option. The default isfalse
show_builtin_errors
: boolean. conftest test's-show-builtin-errors
option. The default isfalse
junit_hide_message
: boolean. conftest test's-junit-hide-message
option. The default isfalse
suppress_exceptions
: boolean. conftest test's-suppress-exceptions
option. The default isfalse
tls
: boolean. conftest test's-tls
option. The default isfalse
parser
: string. conftest test's-parser
optionoutput
: string. conftest test's-output
optionnamespaces
: A list of strings. conftest test's-namespace
option
paths
: A list of tested file paths. glob is available.
conftest:
policies:
- policy: # array or string
- policy/terraform
data: # array or string
- data/data.yaml
fail_on_warn: true
no_fail: true
all_namespaces: true
quiet: true
trace: true
strict: true
show_builtin_errors: true
junit_hide_message: true
suppress_exceptions: true
tls: true
parser: hcl
output: json
namespaces:
- main
Refer target
and working_directory
in policies by -data
option
tfaction creates a special data file temporarily and pass it to your policies by --data option.
The data file includes target
and working_directory
, so you can refer them in policies.
e.g.
import data.tfaction
target := tfaction.target
working_directory := tfaction.working_directory
Example
conftest:
policies:
- policy: policy/tf
id: tf
tf: true
- policy: policy/combine/tf
combine: true
tf: true
- policy: policy/plan
plan: true
- policy: policy/tfaction
paths:
- tfaction.yaml
- policy: policy/json
paths:
- "*.json"
disable_all
:
conftest:
disable_all: true # Disable settings of previous layers
policies:
- policy: policy/tf
tf: true
enabled: false
: Disable specific policies.
conftest:
policies:
- id: tf
enabled: false
- policy: policy/plan
plan: true