Checkov valider vos templates d'infrastructure as code et cloud public

Présentation de Checkov

checkov

Checkov est un outil Python permettant d’analyser le code de votre infrastructure as code et cloud public. Il scanne le code généré par Terraform, CloudFormation et Kubernetes afin de détecter si les bonnes pratiques sont bien suivies, y compris lié à la sécurité.

Grossièrement il fait comme TfSec, Terrascan, cfn_nag et Kube-Scan, etc. mais dans un seul outil.

Fonctionnalités

  • Plus de 350 règles pour AWS, Azure, Google Cloud, Kubernetes, Terraform, CloudFormation
  • On peut créer des règles spécifiques, ou des providers
  • Scan des accréditations (access key et secret key) dans vos EC2 Userdata, Lamba, mais aussi vos providers Terraform
  • Liste blanche via un système de commentaire pour CloudFormation et Kubernetes
  • Génération de log via la CLI, Json ou JUnit XML
  • Intégration facilitée et documentée avec GitLab, Jenkins, Github, Kubernetes, etc.

Installation via Pip

pip install checkov

Scanner un fichier

checkov -d /home/danvops/tf/test.tf

Scanner un répertoire

checkov -d /home/danvops/tf

Générer un fichier de sortie en Json

checkov -d /home/danvops/tf -o json

Paramètres de lancement

 -h, --help            show this help message and exit
  -v, --version         version
  -d DIRECTORY, --directory DIRECTORY
                        IaC root directory (can not be used together with
                        --file). Can be repeated
  -f FILE, --file FILE  IaC file(can not be used together with --directory)
  --external-checks-dir EXTERNAL_CHECKS_DIR
                        Directory for custom checks to be loaded. Can be
                        repeated
  -l, --list            List checks
  -o [{cli,json,junitxml,github_failed_only}], --output [{cli,json,junitxml,github_failed_only}]
                        Report output format
  --quiet               in case of CLI output, display only failed checks
  --framework {cloudformation,terraform,kubernetes,all}
                        filter scan to run only on a specific infrastructure
                        code frameworks
  -c CHECK, --check CHECK
                        filter scan to run only on a specific check
                        identifier(allowlist), You can specify multiple checks
                        separated by comma delimiter
  --skip-check SKIP_CHECK
                        filter scan to run on all check but a specific check
                        identifier(denylist), You can specify multiple checks
                        separated by comma delimiter
  -s, --soft-fail       Runs checks but suppresses error code
  --bc-api-key BC_API_KEY
                        Bridgecrew API key
  --repo-id REPO_ID     Identity string of the repository, with form
                        <repo_owner>/<repo_name>
  -b BRANCH, --branch BRANCH
                        Selected branch of the persisted repository. Only has
                        effect when using the --bc-api-key flag

Intégration dans vos pipelines Gitlab-CI avec une image Docker

stages:
    - validate

checkov:
  image:
    name: bridgecrew/checkov:latest
    entrypoint:
      - '/usr/bin/env'
      - 'PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin'
  stage: validate
  script:
    - checkov -d .

Si Checkov est en échec, le stage sera KO évidemment.

NB : On peut imaginer de générer un JSON, le parser, le stocker en artefact, puis de comparer ce json avec le précédent build. Ou bien d’avoir un taux d’echec, et en fonction passer ou non au step suivant.

Whitelister un bloc de code

CloudFormation

checkov:skip=<check_id>:<suppression_comment>

    <check_id> is one of the available check scanners
    <suppression_comment> is an optional suppression reason to be included in the output

Exemple

resource "aws_s3_bucket" "danvops-bucket" {
  region        = eu-west-3
    #checkov:skip=CKV_AWS_20:The bucket is a public static content host
  bucket        = danvops-public
  acl           = "pub-read"
}

Kubernetes (via les annotations)

checkov.io/skip#: <check_id>=<suppression_comment>

Exemple

apiVersion: v1
kind: Pod
metadata:
  name: mypod
  annotations:
    checkov.io/skip1: CKV_K8S_20=I don't care about Privilege Escalation :-O
    checkov.io/skip2: CKV_K8S_14
    checkov.io/skip3: CKV_K8S_11=I have not set CPU limits as I want BestEffort QoS
spec:
  containers:
...

Conclusion

Utiliser Checkov dans vos pipeline peut être une très bonne alternative aux outils plus classique.

Notamment car il intègre à lui seul plusieurs providers. L’outil est tout jeune, mais il est prometteur.

Vous pouvez aussi l’utiliser en pré-commit, ce qui vous fera gagner encore plus de temps en phase de dev.

Ressources officielles