Compliance Testing and Validation
What You'll Learn
You'll learn how to build automated test suites that verify your infrastructure meets compliance requirements using InSpec, ServerSpec, and OPA testing frameworks.
Why Validate Compliance?
Writing compliance policies (like the ones you've built with OPA, Sentinel, or Cloud Custodian) is just the beginning. Testing validates that those policies actually work and that your infrastructure genuinely meets regulatory requirements. Think of it like code testing: you wouldn't deploy application code without unit tests, and you shouldn't deploy compliance policies without validation either.
Core Testing Frameworks
InSpec provides a compliance-focused testing language for infrastructure. You write tests that check system state against expected configurations:
# InSpec example
describe file('/etc/ssh/sshd_config') do
its('content') { should match /PasswordAuthentication no/ }
end
describe aws_s3_bucket('my-bucket') do
it { should have_default_encryption_enabled }
end
ServerSpec tests infrastructure from a server configuration perspective, verifying services, packages, and system settings.
OPA testing validates your Rego policies themselves—ensuring your compliance rules produce correct decisions:
# Test your OPA policy
test_deny_public_buckets {
deny["Public S3 buckets not allowed"] with input as {"bucket": {"public": true}}
}
The Testing Workflow
- Define expected state based on compliance requirements (PCI DSS, HIPAA, etc.)
- Write automated tests using your chosen framework
- Run tests continuously as part of CI/CD pipelines
- Generate compliance reports showing pass/fail evidence
- Remediate failures and re-test
This creates an audit trail proving continuous compliance, not just point-in-time snapshots.
Key Takeaway: Compliance testing frameworks like InSpec, ServerSpec, and OPA tests automate the validation of compliance policies, transforming manual audits into continuous, provable verification that generates evidence for auditors.