|
| 1 | +--- |
| 2 | +description: Use checkov for static analysis of a Nitric project deployed with Terraform |
| 3 | +tags: |
| 4 | + - Terraform |
| 5 | + - Testing |
| 6 | +published_at: 2025-01-09 |
| 7 | +--- |
| 8 | + |
| 9 | +# Static analysis of Terraform with Checkov |
| 10 | + |
| 11 | +This guide will walk you through generating a report with [Checkov](https://www.checkov.io/) from a Nitric project. |
| 12 | + |
| 13 | +## How Checkov works |
| 14 | + |
| 15 | +[Checkov](https://www.checkov.io/) is a static code analysis tool for scanning infrastructure as code (IaC) files for misconfigurations that may lead to security or compliance problems. |
| 16 | + |
| 17 | +This guide assumes that you have already [installed Checkov](https://www.checkov.io/1.Welcome/Quick%20Start.html#install-checkov-from-pypi) by following their installation guide. |
| 18 | + |
| 19 | +## What we'll be doing |
| 20 | + |
| 21 | +1. Create and set up your application. |
| 22 | +2. Deploying to AWS with a Terraform provider. |
| 23 | +3. Run Checkov. |
| 24 | + |
| 25 | +## Create and set up your application |
| 26 | + |
| 27 | +Checkov can be used with any Nitric project that you intend to deploy with Terraform. We'll be using a basic starter template in this guide, however, you can use your own Nitric project or an [example project](https://github.com/nitrictech/examples). |
| 28 | + |
| 29 | +Let's start by creating a new project from a Nitric template, this will provide a base to start building the API. |
| 30 | + |
| 31 | +```typescript |
| 32 | +nitric new my-profile-api ts-starter |
| 33 | +``` |
| 34 | + |
| 35 | +Next, open the project in your editor of choice and make sure all dependencies are resolved: |
| 36 | + |
| 37 | +Using NPM: |
| 38 | + |
| 39 | +```bash |
| 40 | +npm install |
| 41 | +``` |
| 42 | + |
| 43 | +You can test the project to verify everything is working as expected: |
| 44 | + |
| 45 | +```bash |
| 46 | +nitric start |
| 47 | +``` |
| 48 | + |
| 49 | +## Deploying to AWS with a Terraform provider |
| 50 | + |
| 51 | +To deploy your application with Terraform you'll need to use Nitric's Terraform providers. You can learn more about using Nitric with Terraform here. |
| 52 | + |
| 53 | +```bash |
| 54 | +nitric stack new dev aws-tf |
| 55 | +``` |
| 56 | + |
| 57 | +Update this newly created stack file to include your target region: |
| 58 | + |
| 59 | +```yaml title:nitric.dev.yaml |
| 60 | +# The nitric provider to use |
| 61 | +provider: nitric/[email protected] |
| 62 | + |
| 63 | +# The target aws region to deploy to |
| 64 | +region: us-east-2 |
| 65 | +``` |
| 66 | +
|
| 67 | +The Nitric Terraform providers are currently in preview, to enable them you'll need to enable beta-providers in your Nitric project. You can do this by adding the following to your project's nitric.yaml file: |
| 68 | +
|
| 69 | +```yaml title:nitric.yaml |
| 70 | +preview: |
| 71 | + - beta-providers |
| 72 | +``` |
| 73 | +
|
| 74 | +Once you've created your stack file, you can generate the Terraform code by running the following command: |
| 75 | +
|
| 76 | +```bash |
| 77 | +nitric up |
| 78 | +``` |
| 79 | + |
| 80 | +This will generate Terraform code which can deploy your application. The output will be in a folder named cdktf.out by default. |
| 81 | + |
| 82 | +## Run checkov |
| 83 | + |
| 84 | +Use the Terraform CLI to generate a terraform plan expressed in a json file and then run Checkov on this file. |
| 85 | + |
| 86 | +```bash |
| 87 | +terraform init |
| 88 | +terraform plan --out tfplan.binary |
| 89 | +terraform show -json tfplan.binary | jq > tfplan.json |
| 90 | + |
| 91 | +checkov -f tfplan.json |
| 92 | +``` |
| 93 | + |
| 94 | +## Analysing the results |
| 95 | + |
| 96 | +Checkov comes with some great default checks, however, they do need to be aligned with the requirements of your application. |
| 97 | + |
| 98 | +Here is an example: |
| 99 | + |
| 100 | +The Checkov policy ‘CKV_AWS_136‘ checks specifically for SSE-KMS using a customer-managed KMS key (or at least AWS-managed KMS key). Thus, Checkov will fail if it doesn’t see a KMS key reference, even though your ECR repository is still encrypted by SSE-S3 automatically. |
| 101 | + |
| 102 | +This finding might not always be relevant because, by default, Amazon ECR encrypts container images at rest using Amazon S3 server-side encryption (SSE-S3). That means your images are always encrypted, even if you don’t explicitly configure a KMS key. |
| 103 | + |
| 104 | +If you have any concerns, please don't hesitate to [reach out](https://discord.com/invite/Webemece5C). |
0 commit comments