Static Code Analyses - Terrascan, Terraform and Azure DevOps
Tags: azure, devops, security
Published: 2021-05-10 06:27:17

In my [previous post](https://jamescook.dev/codeanalyses-checkov-terraform-azuredevops) I looked at Static Code Analyses with two of the three tools I am going to use in this post. We are now going to look at [Terrascan](https://github.com/accurics/terrascan) as our analyses tool and have it running from CI/CD platform Azure DevOps which will also host the Terraform code we want to review.

To follow with the post in configuring this setup, you will need the above mentioned tools with permissions on Azure DevOps to be able to create a Pipeline, add extensions from a marketplace and commit to a repository.

## The Code

As per my previous post, I will be reusing the example Terraform configuration file which contains bad practices like password in plain text. This will allow me to test the tool as it should flag some of these bad practices.

This is the code example:

![code.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1619359048086/_nMzNoobR.png)

In my example, this is stored in a Azure DevOps repository but you can use a third party repository like GitHub as an alternative.

## Pipeline Configuration

We will create a separate pipeline within Azure DevOps rather than use the same one we used in the previous post (you can combine them but I will cover this later). This will be used to run Terrascan to analyse the code. You will want to open your project within Azure DevOps and go into **Pipelines**.

![1-pipelines.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1619363601470/3IVJn3d6C.png)

Now you want to create a pipeline for this. Select the new pipeline option and within the new window select **Use the classic editor**.

![2-classiceditor.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1619363733896/_vfeOLXBz.png)

Here you want to select the repository where the configuration file is stored. I have stored it in an Azure DevOps repository so will select this as my location.

![3-demorepo.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1619363876562/LKhvYUEUo.png)

Once selected, you will then need to select **Empty job** as the template option for this pipeline.

![4-emptyjob.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1619363999506/38kKmnKic.png)

The first fields will appear asking you to give the pipeline a name and select the agent pools you want to use. For this demo, I have selected to use Hosted Agents where I will run Terrascan on an Ubuntu OS. Below are the configurations I set.

![devops_name.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1620457346436/3glSyAWc1.png)

After all fields are filled, you want to select the **Run on agent** option and configure the agent job name. I opted to calling the agent **Terrascan Analyses** as it seemed appropriate for what it is doing.

![agent_name.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1620457500596/KIm_E0CA0.png)

Now we are going to select the **plus icon** on the run on agent field to add a job. You will be asked to select something from your currently installed extensions or from the marketplace. We will initially need to install Terraform as this is a prerequisite of Terrascan, so we will need to use the **Terraform extension** from the marketplace (you may have this already so skip this step).

![7-terraformgetmarket.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1619364620421/3xN2AXqZ9.png)

Once acquired from the marketplace you can then select to **install Terraform**.

![8-installterraform.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1619364882704/3_XNaLmon.png)

Within the Terraform configuration window of the extension, select the version of Terraform you want to run on the Hosted Agent (as of writing this, v0.15.0 of Terraform has a bug that stops the initialisation, this may cause Terrascan not to function so use an earlier version).

![terraformconfigversion.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1619364981261/atDE04-A2.png)

Select the **plus icon** on the **Run on agent** and select the **Bash** extension.

![9-bash.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1619365206606/Hb3hZ4zKm.png)

Here you want to install Terrascan using the inline function. Here is what I used to install the software.

![terrascan install.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1620459155102/H4WpSzuXf.png)

Repeat the process of adding another **Bash** extension to the pipeline and this time we are configuring the inline so Terrascan can run the analyses and output the results into an xml file. Make sure to also tick under **Control Options** heading the **Continue on error** option or it will fail the pipeline run.

![terrascan.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1620459328127/oScV4vWyI.png)

Again, select the **plus icon** on **Run on agent** and select the **Publish Test Results** extension.

![11-publishtestresult.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1619365705659/EsZfM8LO7.png)

Now we are importing the xml output from Terrascan into the test results feature in Azure DevOps. Here is the configurations I used to import.

![results config.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1620459414733/NEBbmSqNs.png)

Once all configured, select **Save** on the Pipeline.

## Pipeline Run

You are now ready to run the pipeline. All you need to do is select the **Run** option under the three dotted icon next to the pipeline name. The pipeline will report a failure if Terrascan flags something in its analyses, if nothing is flagged the pipeline will succeed.

In my code, I have been flagged by Terrascan which has set the status of the pipeline build as failed.

![status.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1620459759523/mwnD7Beq6.png)

## Code Analyses Report

Now we have the pipeline running and the report being published into the Azure DevOps test reports, we can review these reports in two location. The first is within the pipeline build, select the pipeline job and open the tab **Tests**. Here you will see the tests than was ran by Terrascan, what passed and failed and reasons for this.

![pipeline results.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1620459874390/YYoRycEzH.png)

Clicking on the flagged test failure, you will see more details as to why it failed.

![results details.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1620459955423/PDNw1-No7.png)

Alternatively, you can view the test reports via the side menu under **Test Plans** and **Runs**

![runs results.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1620460084653/n_vVCe5xV.png)

You can do more with Terrascan but this will not be covered in this post but future posts on the topic. In the meantime, checkout the [Terrascan GitHub](https://github.com/accurics/terrascan) page for more information.