Show a conftest policy that checks a Terraform plan for unencrypted S3 buckets.
AdvancedProductionCompliance as Code, Threat Modeling & Incident Response
Answer
# policy/s3.rego
package main
import rego.v1
deny contains msg if {
rc := input.resource_changes[_]
rc.type == "aws_s3_bucket"
rc.change.actions[_] == "create"
not encrypted(rc.change.after.bucket)
msg := sprintf("S3 bucket %s has no SSE configuration", [rc.change.after.bucket])
}
encrypted(name) if {
rc := input.resource_changes[_]
rc.type == "aws_s3_bucket_server_side_encryption_configuration"
rc.change.after.bucket == name
}
terraform plan -out tfplan && terraform show -json tfplan > tfplan.json
conftest test tfplan.json --policy policy/
Since S3 encrypts by default with SSE-S3, real policies usually require SSE-KMS for buckets tagged as sensitive.
More on Compliance as Code, Threat Modeling & Incident Response
How do you implement compliance as code across the delivery pipeline?IntermediateHow do you automate audit evidence collection?BeginnerHow do you make threat modelling routine for product teams?IntermediateWhat does "threat model as code" mean, and is it worth it?AdvancedHow do you serve SOC 2, ISO 27001 and PCI DSS with one control set?IntermediateHow does GitOps help with change management for audits?Beginner