Checkov.md

October 22, 2024 ยท View on GitHub

Checkov is a static code analysis tool that helps secure infrastructure-as-code (IaC) by scanning Terraform, AWS CloudFormation, Kubernetes, Helm charts, Dockerfiles, and other IaC frameworks for security misconfigurations, compliance violations, and policy adherence. Here's how Checkov can help you in code scanning:

Key Features:

  1. Security Best Practices:

    • Checkov scans IaC code against known security misconfigurations (like open security groups, unused security controls, or improper IAM roles) and ensures your infrastructure aligns with security best practices.
  2. Policy-as-Code:

    • It allows you to define custom policies or use built-in policies to enforce security controls within your IaC files.
  3. Multi-Cloud and Multi-Tool Support:

    • Supports major cloud providers (AWS, Azure, Google Cloud) and multiple IaC frameworks, ensuring your entire cloud setup is covered.
  4. Compliance Checks:

    • Checkov offers out-of-the-box compliance checks for major standards such as CIS Benchmarks, SOC 2, PCI-DSS, and HIPAA, helping you stay compliant.
  5. Shift Left Security:

    • By integrating Checkov into your CI/CD pipelines (like GitHub Actions, Jenkins, or GitLab CI), you can catch security issues before they go into production, promoting a "shift left" approach to security.
  6. Terraform Plan File Scanning:

    • Checkov can scan your Terraform Plan and State files to validate runtime configurations, providing more accurate security insights.
  7. Kubernetes and Helm Support:

    • You can scan Kubernetes manifests and Helm charts to catch misconfigurations, ensuring secure cluster setups.
  8. Visualize in IDE:

    • Checkov integrates with IDEs like VSCode, showing potential issues as you write your code.

How to Use Checkov:

  1. Install Checkov:

    pip install checkov
    
  2. Run Checkov on Terraform Code:

    checkov -d /path/to/terraform/code
    
  3. Run Checkov in CI/CD Pipeline: Integrate Checkov into your pipeline by adding it as a step in your CI file:

    - name: Run Checkov
      run: checkov -d /path/to/iac/code
    
  4. Scan Specific IaC Frameworks: You can specify the framework to scan:

    checkov -d /path/to/iac/code --framework terraform
    
  5. Custom Policies: Create your own policies to enforce custom security requirements:

    checkov -f /path/to/iac/code --check <CUSTOM_POLICY>
    

Yes, Checkov can output results in JSON format. This is useful for integrating the results with other tools or for logging purposes. Additionally, you can set up Checkov as part of a pre-commit hook to automatically scan your Infrastructure-as-Code (IaC) files for security issues before committing the code. Below are the steps to achieve both:

1. JSON Output for Checkov

To get the output of a Checkov scan in JSON format, you can use the --output flag followed by json. For example:

checkov -d /path/to/iac/code --output json

This will produce a JSON report of all the checks that were run, along with their results. You can redirect this output to a file if needed:

checkov -d /path/to/iac/code --output json > checkov_report.json

2. Integrating Checkov with Pre-Commit Hooks

To integrate Checkov as part of your pre-commit hook, you can leverage the pre-commit framework. Here's how to set it up:

Step-by-Step Pre-Commit Hook Integration

  1. Install Pre-Commit: First, ensure that pre-commit is installed on your system:

    pip install pre-commit
    
  2. Create a .pre-commit-config.yaml file: In the root of your repository, create a .pre-commit-config.yaml file and add the following Checkov hook configuration:

    repos:
      - repo: https://github.com/bridgecrewio/checkov.git
        rev: <latest_version>  # You can check the latest version on GitHub or PyPI
        hooks:
          - id: checkov
            name: Checkov IaC Scan
            entry: checkov
            language: python
            types: [terraform, yaml, json]  # Specify the file types to scan
            files: ^(.*\.(tf|json|yaml|yml))$
            args: ["--output", "json"]  # Use json output format
    
  3. Install the Pre-Commit Hook: Once the .pre-commit-config.yaml file is created, install the hooks:

    pre-commit install
    

    This will configure the pre-commit hook to run Checkov every time a git commit is made.

  4. Run Pre-Commit Manually (Optional): You can also manually trigger the pre-commit checks on all files:

    pre-commit run --all-files
    

Customizing the Pre-Commit Hook

  • File Type Filtering: The files parameter in the .pre-commit-config.yaml allows you to specify which file types to scan (e.g., Terraform, JSON, YAML files).

  • Output Format: As shown in the example, you can customize the output to be in JSON format using the args flag.