Geavanceerd
Zoeken inHow-to-gidsen
Zo zoek je
  • Typ de taak in je eigen woorden (poort vinden, firewall openen, schijf vol) of plak een foutmelding tussen aanhalingstekens.
  • Kies "Snel antwoord" om alleen de commando's te zien, of een gebied om in één vakgebied te blijven.
  • "twee woorden" vindt precies die zin.
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken
Probeer:

How-to 210Security · IaC

How to scan Terraform code with Checkov

A public S3 bucket, a security group open to 0.0.0.0/0, an unencrypted database — these mistakes are easy to write in Terraform and cheap to catch before apply. Checkov reads Terraform (and Kubernetes, Dockerfiles, GitHub Actions …) and reports hundreds of known misconfigurations with a check ID and a fix link. This how-to scans a folder and a plan, skips a check with a written reason, and fails the pipeline on new problems.

  • 5 Lessen
  • 3 Commando's

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

$ als gewone gebruiker# als root (of met sudo)

210.1

The quick answer

python3 -m venv ~/.venvs/checkov && ~/.venvs/checkov/bin/pip install checkov
~/.venvs/checkov/bin/checkov -d ./terraform --framework terraform --compact

Commando's in deze les

210.2

Step by step

  1. Install in a virtual environment (quick answer), or run the container: docker run --rm -v "$PWD:/tf" bridgecrew/checkov -d /tf.
  2. Scan the code — each finding shows the check ID (for example CKV_AWS_18 "S3 bucket access logging"), the resource and the file line. --compact hides the code snippets; --quiet shows only failures.
  3. Scan the plan too — the plan has the real values (variables, module outputs):
terraform plan -out=tfplan && terraform show -json tfplan > tfplan.json
~/.venvs/checkov/bin/checkov -f tfplan.json --framework terraform_plan
  1. Fix, or skip with a reason — fix most findings. When a check does not fit, skip it in the code, next to the resource, with a reason a reviewer can judge:
main.tf
resource "aws_s3_bucket" "logs" {
  #checkov:skip=CKV_AWS_18:This is the access log bucket itself
  bucket = "example-logs"
}
  1. Team settings in a file — frameworks, skipped checks for the whole repo, output:
.checkov.yaml
framework:
  - terraform
compact: true
skip-check:
  - CKV_AWS_144
  1. In CI — fail the job on findings, and upload SARIF so findings show in the pull request (GitHub code scanning):
checkov -d ./terraform -o cli -o sarif --output-file-path console,results.sarif
  1. Existing projects with many findings — create a baseline once (--create-baseline), then CI fails only on new findings (--baseline terraform/.checkov.baseline); fix the old ones step by step.

210.3

Check that it worked

The scan ends with Passed checks: N, Failed checks: 0, Skipped checks: M; every skip in the code has a reason; a test change that opens port 22 to 0.0.0.0/0 makes the CI job fail with the check ID in the log.

210.4

Common problems

ProblemCauseFix
Hundreds of findings in an old projectNever scanned before.Baseline first; fix the high-risk ones; block new ones.
Finding in a module you do not ownCheckov scans downloaded modules.--skip-path .terraform or --download-external-modules false; report upstream.
Variables shown as unknownStatic scan does not know the values.Scan the plan JSON too.
Skip comment ignoredWrong place or format.#checkov:skip=ID:reason inside the resource block.
CI green despite findings--soft-fail set.Remove it (or use it only during the baseline phase).
pip install breaks system PythonInstalled globally.Use a venv or the container.

Commando's in deze les

210.5

Go further

1Why scan the Terraform plan and not only the code?

The plan contains the real values of variables and modules, so checks can see settings that the code alone does not show.

2What makes a good Checkov skip?

It sits next to the resource with the check ID and a clear reason, so reviewers can judge it.

Alle commando's in dit hoofdstuk