TP : Scanner du code IaC avec Checkov et Trivy
Prérequis
Environnement technique
- OpenTofu ou Terraform installé pour valider la syntaxe du code IaC.
- Checkov installé pour scanner les mauvaises configurations Terraform.
- Trivy installé pour scanner les configurations IaC et les secrets.
- jq installé pour lire et filtrer les rapports JSON.
- Accès à un terminal Bash ou Zsh.
Ce TP est réalisable sur Linux, macOS et Windows.
Il ne déploie aucune ressource cloud.
Aucune commande apply ne doit être exécutée.
Compatibilité Windows
Ce TP est conçu pour être exécuté dans un terminal Bash.
Sur Windows, l’environnement recommandé est WSL2 avec Ubuntu. Cela permet d’exécuter les mêmes commandes que sur Linux sans adaptation.
L’exécution directe en PowerShell est possible mais certaines commandes nécessitent une adaptation :
| Commande Unix | Équivalent PowerShell |
|---|---|
mkdir -p reports | New-Item -ItemType Directory -Force reports |
cat > fichier <<'EOF' | Here-string PowerShell |
rm -rf dossier | Remove-Item -Recurse -Force dossier |
export VAR=value | $env:VAR="value" |
source scripts/env.sh | Exécuter les variables manuellement |
Pour garder un TP homogène, utiliser WSL2 ou Git Bash sur Windows.
Installation des outils
Sur Debian / Ubuntu (ou WSL2 Ubuntu)
Mettre à jour les paquets :
sudo apt-get updatesudo apt-get install -y curl wget unzip git jq python3 python3-pip python3-venvInstaller OpenTofu :
curl --proto '=https' --tlsv1.2 -fsSL https://get.opentofu.org/install-opentofu.sh -o install-opentofu.shchmod +x install-opentofu.shsudo ./install-opentofu.sh --install-method debrm -f install-opentofu.shInstaller Checkov dans un environnement Python isolé :
python3 -m venv ~/.venvs/checkovsource ~/.venvs/checkov/bin/activatepip install --upgrade pippip install checkovdeactivateCréer un lien symbolique pour utiliser checkov directement :
sudo ln -sf ~/.venvs/checkov/bin/checkov /usr/local/bin/checkovInstaller Trivy :
sudo apt-get install -y wget apt-transport-https gnupg
wget -qO - https://aquasecurity.github.io/trivy-repo/deb/public.key \ | gpg --dearmor \ | sudo tee /usr/share/keyrings/trivy.gpg > /dev/null
echo "deb [signed-by=/usr/share/keyrings/trivy.gpg] https://aquasecurity.github.io/trivy-repo/deb generic main" \ | sudo tee /etc/apt/sources.list.d/trivy.list
sudo apt-get updatesudo apt-get install -y trivySur Fedora / RHEL / Rocky Linux / AlmaLinux
Installer les dépendances :
sudo dnf install -y curl wget unzip git jq python3 python3-pipInstaller OpenTofu :
curl --proto '=https' --tlsv1.2 -fsSL https://get.opentofu.org/install-opentofu.sh -o install-opentofu.shchmod +x install-opentofu.shsudo ./install-opentofu.sh --install-method rpmrm -f install-opentofu.shInstaller Checkov :
python3 -m pip install --user checkovAjouter le dossier utilisateur Python au PATH si nécessaire :
export PATH="$HOME/.local/bin:$PATH"Installer Trivy :
cat << 'EOF' | sudo tee /etc/yum.repos.d/trivy.repo[trivy]name=Trivy repositorybaseurl=https://aquasecurity.github.io/trivy-repo/rpm/releases/$basearch/gpgcheck=1enabled=1gpgkey=https://aquasecurity.github.io/trivy-repo/rpm/public.keyEOF
sudo dnf install -y trivySur macOS
Installer Homebrew si nécessaire :
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"Installer les outils :
brew install opentofubrew install checkovbrew install trivybrew install jqSur Windows avec PowerShell
Cette option fonctionne mais WSL2 reste recommandé.
Vérifier WSL et installer Ubuntu si nécessaire :
wsl --versionwsl --install -d UbuntuSi WSL n’est pas disponible, installer les outils via Winget :
winget install --exact --id OpenTofu.Tofuwinget install --exact --id Python.Python.3.12winget install --exact --id Git.Gitwinget install --exact --id AquaSecurity.Trivywinget install --exact --id jqlang.jqInstaller Checkov via pip :
python -m pip install --upgrade pippython -m pip install checkovFermer puis rouvrir PowerShell ou Git Bash pour recharger le PATH.
Si checkov n’est pas reconnu dans le PATH, utiliser :
python -m checkov --versionVérification des outils
Depuis Linux, macOS, WSL ou Git Bash :
tofu version || terraform versioncheckov --versiontrivy --versionjq --versionRésultat attendu : une version doit être affichée pour chaque outil.
Créer un fichier de diagnostic local :
mkdir -p reports
{ echo "# Diagnostic outils TP IaC" echo echo "## OpenTofu ou Terraform" tofu version 2>/dev/null || terraform version 2>/dev/null || echo "Non disponible" echo echo "## Checkov" checkov --version 2>/dev/null || echo "Non disponible" echo echo "## Trivy" trivy --version 2>/dev/null || echo "Non disponible" echo echo "## jq" jq --version 2>/dev/null || echo "Non disponible"} | tee reports/00-tools-diagnostic.txt