Hello, env0 fans! As some of you know, we have almost unlimited extensibility with 3rd party tools, using our custom workflows. You can hook in pretty much any tool, in any phase of the deployment. Today, weâre going to talk about how to prevent cloud misconfigurations before they start. Weâre going to do this by chaining a tool in the deployment after the terraform plan phase. This is where our friends at Bridgecrew come in. Just like we at env0 have open-sourced the Terratag module of our platform, Bridgecrew has open-sourced Checkov!
Checkov
Checkov is a static code analysis tool for infrastructure-as-code. It scans cloud infrastructure managed in Terraform, Cloudformation, Kubernetes, Arm templates, or Serverless Framework and detects misconfigurations.
Setup
For illustration purposes, weâre going to use Bridgecrewâs demo application called TerraGoat. TerraGoat is Bridgecrewâs âVulnerable by Designâ Terraform repository. TerraGoat is a learning and training project that demonstrates how common configuration errors can find their way into production cloud environments.
DISCLAIMER: DO NOT ACTUALLY DEPLOY THIS APPLICATION INTO YOUR CLOUD INFRASTRUCTURE. IT IS PURPOSELY COMPROMISED.
I have created a template of TerraGoat inside of env0 and linked it to our Bridgecrew Demo project.
The only other thing we have to do is to actually call Checkov to do the check during the deployment. We need to do this after the Terraform plan phase, so that we have a plan to check. Here is what the env0.yml file will look like:
version: 2
deploy:
steps:
terraformPlan:
after:
- name: "Checkov Install"
run: |
pip3 install checkov==${CHECKOV_VERSION:-3.1.70} 1>&2 # supress with 2>&-
- name: "Checkov Security Scan"
run: |
checkov -f $ENV0_TF_PLAN_JSON --quiet ${SOFT_FAIL} 2>&1
This adds 3 commands that run after the Terraform Plan, and before Terraform Apply. We put it here so that the Apply doesnât run in case of failures. We donât want to see the errors after the resources are applied. We want the deployment to fail if there are errors.
pip3 install checkov
This command installs Checkov into our runtime environment using the pip3 package installer so we can run it against our Terraform plan.
terraform show -json .tf-plan | jq â.â > tf.json
This command essentially formats our .tf-plan file into tf.json so that it can be parsed and run against Checkov.
checkov -f tf.json â quiet || (echo âBridgecrew security checks failed â â $(checkov -f tf.json â quiet | grep âPassed checks:â) 1>&2 && exit 1)
This command has a lot going on and is in 2 parts. First, it quietly executes Checkov against our tf.json (the reformatted tf.plan file) and looks for a 0 exit code. The double pipe || tells bash to only execute the 2nd command if the exit code of the first command is not 0. So if your Checkov results are clear, your deployment gets the 0 exit code and continues on with the deployment.
If not, then the second part of the command runs. Knowing if this part runs, it is because of a failure, weâre just going to format our error message here. We run Checkov again so we can pipe the error with the echoed error notification text to the console. The 1>&2 routs stdout to stderror, and the exit 1 code tells env0 that the stage failed, and to end the deployment run.
The env0 platform will parse the error, and give you the clear error printed on the Environment deployment page. But, if you want the full logs from Checkov, you can find those in the After: Terraform Plan deployment logs.
And thatâs it! A little bit of YAML, and youâve implemented Checkov to protect yourself against the deployment of misconfigured cloud resources. That is instantly added value to your organization by shifting the security left in your deployment process with env0.
You can find more information on Checkov here. You can find the open-source repository on GitHub. And be sure to see how you can automate your infrastructure security from commit to cloud at Bridgecrew.io.
â
Hello, env0 fans! As some of you know, we have almost unlimited extensibility with 3rd party tools, using our custom workflows. You can hook in pretty much any tool, in any phase of the deployment. Today, weâre going to talk about how to prevent cloud misconfigurations before they start. Weâre going to do this by chaining a tool in the deployment after the terraform plan phase. This is where our friends at Bridgecrew come in. Just like we at env0 have open-sourced the Terratag module of our platform, Bridgecrew has open-sourced Checkov!
Checkov
Checkov is a static code analysis tool for infrastructure-as-code. It scans cloud infrastructure managed in Terraform, Cloudformation, Kubernetes, Arm templates, or Serverless Framework and detects misconfigurations.
Setup
For illustration purposes, weâre going to use Bridgecrewâs demo application called TerraGoat. TerraGoat is Bridgecrewâs âVulnerable by Designâ Terraform repository. TerraGoat is a learning and training project that demonstrates how common configuration errors can find their way into production cloud environments.
DISCLAIMER: DO NOT ACTUALLY DEPLOY THIS APPLICATION INTO YOUR CLOUD INFRASTRUCTURE. IT IS PURPOSELY COMPROMISED.
I have created a template of TerraGoat inside of env0 and linked it to our Bridgecrew Demo project.
The only other thing we have to do is to actually call Checkov to do the check during the deployment. We need to do this after the Terraform plan phase, so that we have a plan to check. Here is what the env0.yml file will look like:
version: 2
deploy:
steps:
terraformPlan:
after:
- name: "Checkov Install"
run: |
pip3 install checkov==${CHECKOV_VERSION:-3.1.70} 1>&2 # supress with 2>&-
- name: "Checkov Security Scan"
run: |
checkov -f $ENV0_TF_PLAN_JSON --quiet ${SOFT_FAIL} 2>&1
This adds 3 commands that run after the Terraform Plan, and before Terraform Apply. We put it here so that the Apply doesnât run in case of failures. We donât want to see the errors after the resources are applied. We want the deployment to fail if there are errors.
pip3 install checkov
This command installs Checkov into our runtime environment using the pip3 package installer so we can run it against our Terraform plan.
terraform show -json .tf-plan | jq â.â > tf.json
This command essentially formats our .tf-plan file into tf.json so that it can be parsed and run against Checkov.
checkov -f tf.json â quiet || (echo âBridgecrew security checks failed â â $(checkov -f tf.json â quiet | grep âPassed checks:â) 1>&2 && exit 1)
This command has a lot going on and is in 2 parts. First, it quietly executes Checkov against our tf.json (the reformatted tf.plan file) and looks for a 0 exit code. The double pipe || tells bash to only execute the 2nd command if the exit code of the first command is not 0. So if your Checkov results are clear, your deployment gets the 0 exit code and continues on with the deployment.
If not, then the second part of the command runs. Knowing if this part runs, it is because of a failure, weâre just going to format our error message here. We run Checkov again so we can pipe the error with the echoed error notification text to the console. The 1>&2 routs stdout to stderror, and the exit 1 code tells env0 that the stage failed, and to end the deployment run.
The env0 platform will parse the error, and give you the clear error printed on the Environment deployment page. But, if you want the full logs from Checkov, you can find those in the After: Terraform Plan deployment logs.
And thatâs it! A little bit of YAML, and youâve implemented Checkov to protect yourself against the deployment of misconfigured cloud resources. That is instantly added value to your organization by shifting the security left in your deployment process with env0.
You can find more information on Checkov here. You can find the open-source repository on GitHub. And be sure to see how you can automate your infrastructure security from commit to cloud at Bridgecrew.io.
â