SlideShare a Scribd company logo
Compliance as Code
Emre Erkunt
terraform-compliance
> cat who_is_this_person.tf
resource “human_person” “me” {
name = “Emre Erkunt”
interests {
professional = [“DevOps”, “DevSecOps”, “Security”, “Automation”]
personal = [“Astrophotography”, “Aikido”, “Guitar”, “Apnea Diving”, “Gaming”]
}
recent_focus = [
“terraform”, “terraform-compliance”, “aws”, “tons of aws”, “serverless”,
“pipelines”, “cultural change”, “new ways of working”, “agile”, “#nobuzzwords”
]
twitter = “@3rkunt”
linkedin = “only person with this name and surname”
}
Compliance as Code terraform-compliance.com
> prediction
Compliance as Code terraform-compliance.com
Your main problem is not about testing terraform.
Infrastructure as Code :
What is was ;
A codified way of defining tasks that is required to perform CRUD operations in an IT Environment.
What it is now ;
A codified way of defining the latest state on target IT environment. Mostly used in for Cloud Environments.
Critical Requirement for both is IDEMPOTENCY and ideally IMMUTABILITY.
> terraform init
Compliance as Code terraform-compliance.com
“ “
“ “
Brilliant API Client that focus on desired state and has its own configuration language, HCL/HCL2.
Lots of providers actively maintained. Tons of modules that can be used from the registry.
Solid state management
.. and most importantly ;
> terraform init
Compliance as Code terraform-compliance.com
What is Compliance as Code ?
A codified way of defining compliance policies.
> terraform-compliance -h
Compliance as Code terraform-compliance.com
“ “
What is Compliance as Code ?
A codified way of defining compliance policies.
Requirements :
. A common language that defines the policy
. A language that defines the tests ( might be same with the policy )
. Ability to answer: What are we testing here ?
. Ability to answer: Why are we testing this ?
> terraform-compliance -h
Compliance as Code terraform-compliance.com
“ “
What is Compliance as Code ?
A codified way of defining compliance policies.
Requirements :
. A common language that defines the policy
. A language that defines the tests ( might be same with the policy )
. Ability to answer: What are we testing here ?
. Ability to answer: Why are we testing this ?
> terraform-compliance -h
Compliance as Code terraform-compliance.com
“ “
> terraform plan -out plan.out
Compliance as Code terraform-compliance.com
Implementation terraform plan terraform-compliance terraform apply
terraform plan -detailed-exitcode
> terraform-compliance ?
Compliance as Code terraform-compliance.com
. Based on Behaviour Driven Development. Why ?
. All interpolations and modules are supported. Why is this important ?
. Drilling down, just like another BDD step.
. Resource mounting are supported. Why is this important ?
. Can perform complex Security Group calculations.
. Mostly focused on negative testing. What is negative testing ?
. Filtering.
. Can be assumed as a free version of HashiCorp Sentinel. Really ?
. Runs in everywhere that can run Python or Docker.
. Needs PRs, Feature Requests, Bug Reporting and love just like every Open Source Project.
> Behaviour Driven Development
Compliance as Code terraform-compliance.com
. A branch of Test Driven Development/TDD
. Focus on end-to-end results, functional tests
. Features > Scenarios > Steps, Gherkin/Cucumber language
. Simple sentences with shared vocabulary while every step has a test code under the hood
. GIVEN, WHEN, THEN and AND
. Possible to translate the same tests as UAT, since every Scenario/Feature can be a Story/Task
. Usually takes longer time to run compared with Unit Tests
> terraform-compliance tests
Compliance as Code terraform-compliance.com
. Not an integration test, but still a functional test
. Runs against plan, and runs super-fast
. Same language structure like other BDD tests
. Can live in a separate git repository (strongly recommended!)
. Has its own - but quite universal - vocabulary for steps, e.g. ;
Scenario: Ensure all resources have tags
Given I have resource that supports tags defined
Then it must contain tags
And its value must not be null
> terraform-compliance tests
Compliance as Code terraform-compliance.com
> terraform-compliance tests: GIVEN
Compliance as Code terraform-compliance.com
. Defines the initial picture
. Every scenario has a GIVEN step
. Works as a filtering function
. Will SKIP the next steps if there is no match, so no failure if nothing is found
. Recommended to use terraform references instead of templated entities
. You can use it against resource(s), provider(s), data(s), variable(s) or output(s)
Scenario: Ensure all resources have tags
Given I have aws_s3_bucket defined
Then it must contain tags
And its value must not be null
> terraform-compliance tests: GIVEN
Compliance as Code terraform-compliance.com
. Defines the initial picture
. Every scenario has a GIVEN step
. Works as a filtering function
. Will SKIP the next steps if there is no match, so no failure if nothing is found
. Recommended to use terraform references instead of templated entities
. You can use it against resource(s), provider(s), data(s), variable(s) or output(s)
Scenario: Ensure all resources have tags
Given I have aws_s3_bucket defined
Then it must contain tags
And its value must not be null
resource “aws_s3_bucket” “some_bucket” {
bucket = “my-super-unique-bucket-name”
tags = {
cost_center = “0135134”
environment = “dev”
}
}
> terraform-compliance tests: WHEN
Compliance as Code terraform-compliance.com
. Works as a filtering function (mostly), defines the condition that you are searching for.
. Will SKIP the next steps if there is no match, so no failure if nothing is found, just like GIVEN
. Filtered data is used as the INPUT data for the next steps.
Scenario: Ensure we only allow a port range for ingress rule
Given I have aws_security_group defined
When it contains ingress
Then it must only have tcp protocol and port 22 for 0.0.0.0/0
> terraform-compliance tests: WHEN
Compliance as Code terraform-compliance.com
. Works as a filtering function (mostly), defines the condition that you are searching for.
. Will SKIP the next steps if there is no match, so no failure if nothing is found, just like GIVEN
. Filtered data is used as the INPUT data for the next steps.
Scenario: Ensure we only allow a port range for ingress rule
Given I have aws_security_group defined
When it contains ingress
Then it must only have tcp protocol and port 22 for 0.0.0.0/0
resource “aws_security_group” “some_group” {
name = “allow_ssh_publicly_because_we_are_just_crazy”
ingress {
from_port = 22
to_port = 22
protocol = “tcp”
cidr_blocks = [“0.0.0.0/0”]
}
}
> terraform-compliance tests: WHEN
Compliance as Code terraform-compliance.com
. Works as a filtering function (mostly), defines the condition that you are searching for.
. Will SKIP the next steps if there is no match, so no failure if nothing is found, just like GIVEN
. Filtered data is used as the INPUT data for the next steps.
Scenario: Ensure we only allow a port range for ingress rule
Given I have aws_security_group defined
When it contains ingress
Then it must only have tcp protocol and port 22 for 0.0.0.0/0
resource “aws_security_group_rule” “port_22_to_public” {
type = “ingress”
from_port = 22
to_port = 22
protocol = “tcp”
cidr_blocks = [“0.0.0.0/0”]
security_group_id = aws_security_group.some_group.id
}
> terraform-compliance tests: WHEN
Compliance as Code terraform-compliance.com
. Works as a filtering function (mostly), defines the condition that you are searching for.
. Will SKIP the next steps if there is no match, so no failure if nothing is found, just like GIVEN
. Filtered data is used as the INPUT data for the next steps.
Scenario: Ensure there is always 2 network_interfaces attached to instances
Given I have aws_instance defined
When it contains network_interface
And I count them
Then I expect the result is equal to 1
> terraform-compliance tests: WHEN
Compliance as Code terraform-compliance.com
. Works as a filtering function (mostly), defines the condition that you are searching for.
. Will SKIP the next steps if there is no match, so no failure if nothing is found, just like GIVEN
. Filtered data is used as the INPUT data for the next steps.
Scenario: Ensure there is always 2 network_interfaces attached to instances
Given I have aws_instance defined
When it contains network_interface
And I count them
Then I expect the result is equal to 1
resource “aws_instance” “monero_miner” {
ami = “ami-6d1c2007”
instance_type = “t2.micro”
network_interface {
device_index = “1”
network_interface_id = “eth0”
}
}
> terraform-compliance tests: WHEN
Compliance as Code terraform-compliance.com
. Works as a filtering function (mostly), defines the condition that you are searching for.
. Will SKIP the next steps if there is no match, so no failure if nothing is found, just like GIVEN
. Filtered data is used as the INPUT data for the next steps.
Scenario: Ensure we are using encryption on ALBs via ACM
Given I have aws_elb defined
When it contains listener
Then it must contain ssl_certificate_id
And its value must match the “.*acm.*” regex
> terraform-compliance tests: WHEN
Compliance as Code terraform-compliance.com
. Works as a filtering function (mostly), defines the condition that you are searching for.
. Will SKIP the next steps if there is no match, so no failure if nothing is found, just like GIVEN
. Filtered data is used as the INPUT data for the next steps.
Scenario: Ensure we are using encryption on ALBs via ACM
Given I have aws_elb defined
When it contains listener
Then it must contain ssl_certificate_id
And its value must match the “.*acm.*” regex
resource “aws_elb” “bar” {
name = “foo”
...
listener {
...
}
}
> terraform-compliance tests: THEN
Compliance as Code terraform-compliance.com
. Defines the matching criteria. Decision making step.
. FAILS if it not pass.
Scenario: Ensure we are using encryption on ALBs via ACM
Given I have aws_elb defined
When it contains listener
Then it must contain ssl_certificate_id
And its value must match the “.*acm.*” regex
> terraform-compliance tests: THEN
Compliance as Code terraform-compliance.com
. Defines the matching criteria. Decision making step.
. FAILS if it not pass.
Scenario: Ensure we are using encryption on ALBs via ACM
Given I have aws_elb defined
When it contains listener
Then it must contain ssl_certificate_id
And its value must match the “.*acm.*” regex
resource “aws_elb” “bar” {
name = “foo”
listener {
instance_port = 8000
...
ssl_certificate_id = “arn:aws:iam::123456789012:server-certificate/certName”
}
}
> terraform-compliance tests: THEN
Compliance as Code terraform-compliance.com
. Defines the matching criteria. Decision making step.
. FAILS if it not pass.
Scenario: Ensure we only allow a port range for ingress rule
Given I have aws_security_group defined
When it contains ingress
Then it must only have tcp protocol and port 22 for 0.0.0.0/0
> terraform-compliance tests: THEN
Compliance as Code terraform-compliance.com
. Defines the matching criteria. Decision making step.
. FAILS if it not pass.
Scenario: Ensure we only allow a port range for ingress rule
Given I have aws_security_group defined
When it contains ingress
Then it must only have tcp protocol and port 22 for 0.0.0.0/0
resource “aws_security_group_rule” “port_22_to_public” {
type = “ingress”
from_port = 22
to_port = 22
protocol = “tcp”
cidr_blocks = [“0.0.0.0/0”]
security_group_id = aws_security_group.some_group.id
}
> Workflow Examples
Compliance as Code terraform-compliance.com
> Workflow Examples
Compliance as Code terraform-compliance.com
> Workflow Examples
Compliance as Code terraform-compliance.com
> Workflow Examples
Compliance as Code terraform-compliance.com
...
Scenario: Image scan to be enabled on push.
Given I have aws_ecr_repository defined
Then it must contain image_scanning_configuration
And scan_on_push must be enabled
Failure: Resource aws_ecr_repository.repo does not have scan_on_push property enabled
(scan_on_push=None)
[Container] 2020/02/13 11:48:40 Phase complete: BUILD State: FAILED
> Workflow Examples
Compliance as Code terraform-compliance.com
> Workflow Examples
Compliance as Code terraform-compliance.com
> Workflow Examples
Compliance as Code terraform-compliance.com
> Workflow Examples
Compliance as Code terraform-compliance.com
> Workflow Examples
Compliance as Code terraform-compliance.com
> Workflow Examples
Compliance as Code terraform-compliance.com
> Workflow Examples
Compliance as Code terraform-compliance.com
> Workflow Examples
Compliance as Code terraform-compliance.com
Created a PR
Get peer review
approvals
CI failed due to
compliance test failures
Security Team already introduced
new compliance checks
Read the logs, understand
what failed
Fix compliance problems
CI Pass
Merge to masterCD runs without a failureGet final notification
Δt = ~15 minutes
> Workflow Examples
Compliance as Code terraform-compliance.com
Why it was important ?
. No retrospective checks.
. Feedback loop is near-instant while keeping segregation of duties.
. No complicated troubleshooting, problem was described in plain language.
. The PR was not about the failure, it was due to something created before.
. Nothing was deployed till it is fixed.
. Keep it green.
> Workflow Examples
Compliance as Code terraform-compliance.com
What do you need to achieve this workflow ?
. Trunk Based Development, please do not use GitFlow.
. Small incremental changes, instead of huge PRs. ( or worse having a release branch ... )
. Everybody is hands-on. Engineers (including Security) is the Governance.
. Engineers are the decision makers.
. Keep It Simple Stupid.
. VERY IMPORTANT: Good repositories structure.
. You build it, you run it
. #nobuzzwords
> prediction
Compliance as Code terraform-compliance.com
Your main problem is not about testing terraform, right ?
> terraform apply
Compliance as Code terraform-compliance.com
> cat who_is_this_person.tf
resource “human_person” “me” {
name = “Emre Erkunt”
interests {
professional = [“DevOps”, “DevSecOps”, “Security”, “Automation”]
personal = [“Astrophotography”, “Aikido”, “Guitar”, “Apnea Diving”, “Gaming”]
}
recent_focus = [
“terraform”, “terraform-compliance”, “aws”, “tons of aws”, “serverless”,
“pipelines”, “cultural change”, “new ways of working”, “agile”, “#nobuzzwords”
]
twitter = “@3rkunt”
linkedin = “only person with this name and surname”
}
Compliance as Code terraform-compliance.com

More Related Content

PDF
Practical White Hat Hacker Training - Passive Information Gathering(OSINT)
PDF
Casos reales usando osint
PPTX
JSON SQL Injection and the Lessons Learned
PDF
Suricata ile siber tehdit avcılığı
PDF
CNIT 126: Ch 2 & 3
PDF
Yazılım Güvenliği Temelleri
PPTX
Testing Terraform
PDF
CNCF London: Key Steps To a Good Quality Terraform Infrastructure Code
Practical White Hat Hacker Training - Passive Information Gathering(OSINT)
Casos reales usando osint
JSON SQL Injection and the Lessons Learned
Suricata ile siber tehdit avcılığı
CNIT 126: Ch 2 & 3
Yazılım Güvenliği Temelleri
Testing Terraform
CNCF London: Key Steps To a Good Quality Terraform Infrastructure Code

Similar to Compliance as Code with terraform-compliance (20)

PDF
DevOps Braga #9: Introdução ao Terraform
PDF
Terraform introduction
PPTX
RIMA-Infrastructure as a code with Terraform.pptx
PPTX
Testing & deploying terraform
PPTX
Terraform in production - experiences, best practices and deep dive- Piotr Ki...
PDF
My Hashitalk Indonesia April 2024 Presentation
PDF
AWS DevOps - Terraform, Docker, HashiCorp Vault
PDF
Head in the Clouds: Testing Infra as Code - Config Management 2020
PDF
Best Practices of Infrastructure as Code with Terraform
PDF
Terraform in action
PDF
CDK Meetup: Rule the World through IaC
PPTX
Terraform - The Road to Self-Service
PPTX
Terraform Abstractions for Safety and Power
PDF
OracleBeer_Terraform_soe.pdf
PDF
Atmosphere 2018: Wojciech Krysmann- INFRA AS CODE - TERRAFORM DEEP DIVE AND B...
PDF
Automated Testing for Terraform, Docker, Packer, Kubernetes, and More
PDF
Introductory Overview to Managing AWS with Terraform
PDF
The hitchhiker's guide to terraform your infrastructure
PDF
[2020 git lab commit] continuous infrastructure
PDF
Catch them all! Detection engineering and purple teaming in the cloud
DevOps Braga #9: Introdução ao Terraform
Terraform introduction
RIMA-Infrastructure as a code with Terraform.pptx
Testing & deploying terraform
Terraform in production - experiences, best practices and deep dive- Piotr Ki...
My Hashitalk Indonesia April 2024 Presentation
AWS DevOps - Terraform, Docker, HashiCorp Vault
Head in the Clouds: Testing Infra as Code - Config Management 2020
Best Practices of Infrastructure as Code with Terraform
Terraform in action
CDK Meetup: Rule the World through IaC
Terraform - The Road to Self-Service
Terraform Abstractions for Safety and Power
OracleBeer_Terraform_soe.pdf
Atmosphere 2018: Wojciech Krysmann- INFRA AS CODE - TERRAFORM DEEP DIVE AND B...
Automated Testing for Terraform, Docker, Packer, Kubernetes, and More
Introductory Overview to Managing AWS with Terraform
The hitchhiker's guide to terraform your infrastructure
[2020 git lab commit] continuous infrastructure
Catch them all! Detection engineering and purple teaming in the cloud
Ad

Recently uploaded (20)

PDF
R24 SURVEYING LAB MANUAL for civil enggi
PDF
III.4.1.2_The_Space_Environment.p pdffdf
PDF
Level 2 – IBM Data and AI Fundamentals (1)_v1.1.PDF
PDF
737-MAX_SRG.pdf student reference guides
PPTX
6ME3A-Unit-II-Sensors and Actuators_Handouts.pptx
PPTX
Internet of Things (IOT) - A guide to understanding
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PDF
BIO-INSPIRED HORMONAL MODULATION AND ADAPTIVE ORCHESTRATION IN S-AI-GPT
PPTX
Safety Seminar civil to be ensured for safe working.
PDF
PREDICTION OF DIABETES FROM ELECTRONIC HEALTH RECORDS
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PDF
PPT on Performance Review to get promotions
PDF
Categorization of Factors Affecting Classification Algorithms Selection
PPTX
Sustainable Sites - Green Building Construction
PDF
Artificial Superintelligence (ASI) Alliance Vision Paper.pdf
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPTX
Current and future trends in Computer Vision.pptx
PDF
Human-AI Collaboration: Balancing Agentic AI and Autonomy in Hybrid Systems
R24 SURVEYING LAB MANUAL for civil enggi
III.4.1.2_The_Space_Environment.p pdffdf
Level 2 – IBM Data and AI Fundamentals (1)_v1.1.PDF
737-MAX_SRG.pdf student reference guides
6ME3A-Unit-II-Sensors and Actuators_Handouts.pptx
Internet of Things (IOT) - A guide to understanding
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
BIO-INSPIRED HORMONAL MODULATION AND ADAPTIVE ORCHESTRATION IN S-AI-GPT
Safety Seminar civil to be ensured for safe working.
PREDICTION OF DIABETES FROM ELECTRONIC HEALTH RECORDS
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PPT on Performance Review to get promotions
Categorization of Factors Affecting Classification Algorithms Selection
Sustainable Sites - Green Building Construction
Artificial Superintelligence (ASI) Alliance Vision Paper.pdf
Embodied AI: Ushering in the Next Era of Intelligent Systems
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
Current and future trends in Computer Vision.pptx
Human-AI Collaboration: Balancing Agentic AI and Autonomy in Hybrid Systems
Ad

Compliance as Code with terraform-compliance

  • 1. Compliance as Code Emre Erkunt terraform-compliance
  • 2. > cat who_is_this_person.tf resource “human_person” “me” { name = “Emre Erkunt” interests { professional = [“DevOps”, “DevSecOps”, “Security”, “Automation”] personal = [“Astrophotography”, “Aikido”, “Guitar”, “Apnea Diving”, “Gaming”] } recent_focus = [ “terraform”, “terraform-compliance”, “aws”, “tons of aws”, “serverless”, “pipelines”, “cultural change”, “new ways of working”, “agile”, “#nobuzzwords” ] twitter = “@3rkunt” linkedin = “only person with this name and surname” } Compliance as Code terraform-compliance.com
  • 3. > prediction Compliance as Code terraform-compliance.com Your main problem is not about testing terraform.
  • 4. Infrastructure as Code : What is was ; A codified way of defining tasks that is required to perform CRUD operations in an IT Environment. What it is now ; A codified way of defining the latest state on target IT environment. Mostly used in for Cloud Environments. Critical Requirement for both is IDEMPOTENCY and ideally IMMUTABILITY. > terraform init Compliance as Code terraform-compliance.com “ “ “ “
  • 5. Brilliant API Client that focus on desired state and has its own configuration language, HCL/HCL2. Lots of providers actively maintained. Tons of modules that can be used from the registry. Solid state management .. and most importantly ; > terraform init Compliance as Code terraform-compliance.com
  • 6. What is Compliance as Code ? A codified way of defining compliance policies. > terraform-compliance -h Compliance as Code terraform-compliance.com “ “
  • 7. What is Compliance as Code ? A codified way of defining compliance policies. Requirements : . A common language that defines the policy . A language that defines the tests ( might be same with the policy ) . Ability to answer: What are we testing here ? . Ability to answer: Why are we testing this ? > terraform-compliance -h Compliance as Code terraform-compliance.com “ “
  • 8. What is Compliance as Code ? A codified way of defining compliance policies. Requirements : . A common language that defines the policy . A language that defines the tests ( might be same with the policy ) . Ability to answer: What are we testing here ? . Ability to answer: Why are we testing this ? > terraform-compliance -h Compliance as Code terraform-compliance.com “ “
  • 9. > terraform plan -out plan.out Compliance as Code terraform-compliance.com Implementation terraform plan terraform-compliance terraform apply terraform plan -detailed-exitcode
  • 10. > terraform-compliance ? Compliance as Code terraform-compliance.com . Based on Behaviour Driven Development. Why ? . All interpolations and modules are supported. Why is this important ? . Drilling down, just like another BDD step. . Resource mounting are supported. Why is this important ? . Can perform complex Security Group calculations. . Mostly focused on negative testing. What is negative testing ? . Filtering. . Can be assumed as a free version of HashiCorp Sentinel. Really ? . Runs in everywhere that can run Python or Docker. . Needs PRs, Feature Requests, Bug Reporting and love just like every Open Source Project.
  • 11. > Behaviour Driven Development Compliance as Code terraform-compliance.com . A branch of Test Driven Development/TDD . Focus on end-to-end results, functional tests . Features > Scenarios > Steps, Gherkin/Cucumber language . Simple sentences with shared vocabulary while every step has a test code under the hood . GIVEN, WHEN, THEN and AND . Possible to translate the same tests as UAT, since every Scenario/Feature can be a Story/Task . Usually takes longer time to run compared with Unit Tests
  • 12. > terraform-compliance tests Compliance as Code terraform-compliance.com . Not an integration test, but still a functional test . Runs against plan, and runs super-fast . Same language structure like other BDD tests . Can live in a separate git repository (strongly recommended!) . Has its own - but quite universal - vocabulary for steps, e.g. ; Scenario: Ensure all resources have tags Given I have resource that supports tags defined Then it must contain tags And its value must not be null
  • 13. > terraform-compliance tests Compliance as Code terraform-compliance.com
  • 14. > terraform-compliance tests: GIVEN Compliance as Code terraform-compliance.com . Defines the initial picture . Every scenario has a GIVEN step . Works as a filtering function . Will SKIP the next steps if there is no match, so no failure if nothing is found . Recommended to use terraform references instead of templated entities . You can use it against resource(s), provider(s), data(s), variable(s) or output(s) Scenario: Ensure all resources have tags Given I have aws_s3_bucket defined Then it must contain tags And its value must not be null
  • 15. > terraform-compliance tests: GIVEN Compliance as Code terraform-compliance.com . Defines the initial picture . Every scenario has a GIVEN step . Works as a filtering function . Will SKIP the next steps if there is no match, so no failure if nothing is found . Recommended to use terraform references instead of templated entities . You can use it against resource(s), provider(s), data(s), variable(s) or output(s) Scenario: Ensure all resources have tags Given I have aws_s3_bucket defined Then it must contain tags And its value must not be null resource “aws_s3_bucket” “some_bucket” { bucket = “my-super-unique-bucket-name” tags = { cost_center = “0135134” environment = “dev” } }
  • 16. > terraform-compliance tests: WHEN Compliance as Code terraform-compliance.com . Works as a filtering function (mostly), defines the condition that you are searching for. . Will SKIP the next steps if there is no match, so no failure if nothing is found, just like GIVEN . Filtered data is used as the INPUT data for the next steps. Scenario: Ensure we only allow a port range for ingress rule Given I have aws_security_group defined When it contains ingress Then it must only have tcp protocol and port 22 for 0.0.0.0/0
  • 17. > terraform-compliance tests: WHEN Compliance as Code terraform-compliance.com . Works as a filtering function (mostly), defines the condition that you are searching for. . Will SKIP the next steps if there is no match, so no failure if nothing is found, just like GIVEN . Filtered data is used as the INPUT data for the next steps. Scenario: Ensure we only allow a port range for ingress rule Given I have aws_security_group defined When it contains ingress Then it must only have tcp protocol and port 22 for 0.0.0.0/0 resource “aws_security_group” “some_group” { name = “allow_ssh_publicly_because_we_are_just_crazy” ingress { from_port = 22 to_port = 22 protocol = “tcp” cidr_blocks = [“0.0.0.0/0”] } }
  • 18. > terraform-compliance tests: WHEN Compliance as Code terraform-compliance.com . Works as a filtering function (mostly), defines the condition that you are searching for. . Will SKIP the next steps if there is no match, so no failure if nothing is found, just like GIVEN . Filtered data is used as the INPUT data for the next steps. Scenario: Ensure we only allow a port range for ingress rule Given I have aws_security_group defined When it contains ingress Then it must only have tcp protocol and port 22 for 0.0.0.0/0 resource “aws_security_group_rule” “port_22_to_public” { type = “ingress” from_port = 22 to_port = 22 protocol = “tcp” cidr_blocks = [“0.0.0.0/0”] security_group_id = aws_security_group.some_group.id }
  • 19. > terraform-compliance tests: WHEN Compliance as Code terraform-compliance.com . Works as a filtering function (mostly), defines the condition that you are searching for. . Will SKIP the next steps if there is no match, so no failure if nothing is found, just like GIVEN . Filtered data is used as the INPUT data for the next steps. Scenario: Ensure there is always 2 network_interfaces attached to instances Given I have aws_instance defined When it contains network_interface And I count them Then I expect the result is equal to 1
  • 20. > terraform-compliance tests: WHEN Compliance as Code terraform-compliance.com . Works as a filtering function (mostly), defines the condition that you are searching for. . Will SKIP the next steps if there is no match, so no failure if nothing is found, just like GIVEN . Filtered data is used as the INPUT data for the next steps. Scenario: Ensure there is always 2 network_interfaces attached to instances Given I have aws_instance defined When it contains network_interface And I count them Then I expect the result is equal to 1 resource “aws_instance” “monero_miner” { ami = “ami-6d1c2007” instance_type = “t2.micro” network_interface { device_index = “1” network_interface_id = “eth0” } }
  • 21. > terraform-compliance tests: WHEN Compliance as Code terraform-compliance.com . Works as a filtering function (mostly), defines the condition that you are searching for. . Will SKIP the next steps if there is no match, so no failure if nothing is found, just like GIVEN . Filtered data is used as the INPUT data for the next steps. Scenario: Ensure we are using encryption on ALBs via ACM Given I have aws_elb defined When it contains listener Then it must contain ssl_certificate_id And its value must match the “.*acm.*” regex
  • 22. > terraform-compliance tests: WHEN Compliance as Code terraform-compliance.com . Works as a filtering function (mostly), defines the condition that you are searching for. . Will SKIP the next steps if there is no match, so no failure if nothing is found, just like GIVEN . Filtered data is used as the INPUT data for the next steps. Scenario: Ensure we are using encryption on ALBs via ACM Given I have aws_elb defined When it contains listener Then it must contain ssl_certificate_id And its value must match the “.*acm.*” regex resource “aws_elb” “bar” { name = “foo” ... listener { ... } }
  • 23. > terraform-compliance tests: THEN Compliance as Code terraform-compliance.com . Defines the matching criteria. Decision making step. . FAILS if it not pass. Scenario: Ensure we are using encryption on ALBs via ACM Given I have aws_elb defined When it contains listener Then it must contain ssl_certificate_id And its value must match the “.*acm.*” regex
  • 24. > terraform-compliance tests: THEN Compliance as Code terraform-compliance.com . Defines the matching criteria. Decision making step. . FAILS if it not pass. Scenario: Ensure we are using encryption on ALBs via ACM Given I have aws_elb defined When it contains listener Then it must contain ssl_certificate_id And its value must match the “.*acm.*” regex resource “aws_elb” “bar” { name = “foo” listener { instance_port = 8000 ... ssl_certificate_id = “arn:aws:iam::123456789012:server-certificate/certName” } }
  • 25. > terraform-compliance tests: THEN Compliance as Code terraform-compliance.com . Defines the matching criteria. Decision making step. . FAILS if it not pass. Scenario: Ensure we only allow a port range for ingress rule Given I have aws_security_group defined When it contains ingress Then it must only have tcp protocol and port 22 for 0.0.0.0/0
  • 26. > terraform-compliance tests: THEN Compliance as Code terraform-compliance.com . Defines the matching criteria. Decision making step. . FAILS if it not pass. Scenario: Ensure we only allow a port range for ingress rule Given I have aws_security_group defined When it contains ingress Then it must only have tcp protocol and port 22 for 0.0.0.0/0 resource “aws_security_group_rule” “port_22_to_public” { type = “ingress” from_port = 22 to_port = 22 protocol = “tcp” cidr_blocks = [“0.0.0.0/0”] security_group_id = aws_security_group.some_group.id }
  • 27. > Workflow Examples Compliance as Code terraform-compliance.com
  • 28. > Workflow Examples Compliance as Code terraform-compliance.com
  • 29. > Workflow Examples Compliance as Code terraform-compliance.com
  • 30. > Workflow Examples Compliance as Code terraform-compliance.com ... Scenario: Image scan to be enabled on push. Given I have aws_ecr_repository defined Then it must contain image_scanning_configuration And scan_on_push must be enabled Failure: Resource aws_ecr_repository.repo does not have scan_on_push property enabled (scan_on_push=None) [Container] 2020/02/13 11:48:40 Phase complete: BUILD State: FAILED
  • 31. > Workflow Examples Compliance as Code terraform-compliance.com
  • 32. > Workflow Examples Compliance as Code terraform-compliance.com
  • 33. > Workflow Examples Compliance as Code terraform-compliance.com
  • 34. > Workflow Examples Compliance as Code terraform-compliance.com
  • 35. > Workflow Examples Compliance as Code terraform-compliance.com
  • 36. > Workflow Examples Compliance as Code terraform-compliance.com
  • 37. > Workflow Examples Compliance as Code terraform-compliance.com
  • 38. > Workflow Examples Compliance as Code terraform-compliance.com Created a PR Get peer review approvals CI failed due to compliance test failures Security Team already introduced new compliance checks Read the logs, understand what failed Fix compliance problems CI Pass Merge to masterCD runs without a failureGet final notification Δt = ~15 minutes
  • 39. > Workflow Examples Compliance as Code terraform-compliance.com Why it was important ? . No retrospective checks. . Feedback loop is near-instant while keeping segregation of duties. . No complicated troubleshooting, problem was described in plain language. . The PR was not about the failure, it was due to something created before. . Nothing was deployed till it is fixed. . Keep it green.
  • 40. > Workflow Examples Compliance as Code terraform-compliance.com What do you need to achieve this workflow ? . Trunk Based Development, please do not use GitFlow. . Small incremental changes, instead of huge PRs. ( or worse having a release branch ... ) . Everybody is hands-on. Engineers (including Security) is the Governance. . Engineers are the decision makers. . Keep It Simple Stupid. . VERY IMPORTANT: Good repositories structure. . You build it, you run it . #nobuzzwords
  • 41. > prediction Compliance as Code terraform-compliance.com Your main problem is not about testing terraform, right ?
  • 42. > terraform apply Compliance as Code terraform-compliance.com
  • 43. > cat who_is_this_person.tf resource “human_person” “me” { name = “Emre Erkunt” interests { professional = [“DevOps”, “DevSecOps”, “Security”, “Automation”] personal = [“Astrophotography”, “Aikido”, “Guitar”, “Apnea Diving”, “Gaming”] } recent_focus = [ “terraform”, “terraform-compliance”, “aws”, “tons of aws”, “serverless”, “pipelines”, “cultural change”, “new ways of working”, “agile”, “#nobuzzwords” ] twitter = “@3rkunt” linkedin = “only person with this name and surname” } Compliance as Code terraform-compliance.com