Interview Prep/DevSecOps/Compliance as Code, Threat Modeling & Incident Response

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.

All DevSecOps questions Roadmap: Policy as code

More on Compliance as Code, Threat Modeling & Incident Response