tfsec Documentation

repository·master·Indexed 27 days ago

https://github.com/aquasecurity/tfsec

tfsec is a static analysis tool used to detect potential security misconfigurations in Terraform code across major cloud providers. It features a processing pipeline consisting of a parser, adapters, and a scanner to identify issues. The tool supports multiple output formats (including JSON, SARIF, and CSV), provides mechanisms to ignore specific warnings with optional expiration dates, and can be deployed via package managers or Docker images.

Tokens
103.6K
Snippets
339
Records
431
Agent score
91%

What's inside tfsec

  1. Overview of tfsec

    master
    tfsec is a static analysis security scanner specifically designed for Terraform code. It uses static analysis and deep integration with the official HCL parser to detect security misconfigurations in Terraform templates before infrastructure changes are applied. It is designed for both local development and CI/CD pipelines.
  2. Understand the tfsec execution pipeline

    master

    tfsec follows a linear processing pipeline to scan Terraform files for security misconfigurations:

    1. Parser: Consumes plaintext .tf files and produces logical abstractions (modules, blocks, and attributes).
    2. Adapters: Converts these Terraform abstractions into a common data format representing specific cloud resources (e.g., an AWS S3 bucket struct).
    3. Scanner: Runs security rules against the adapted cloud resources.
    4. Results: Produces the final list of security findings.
  3. Understand Custom Checks in tfsec

    master
    tfsec includes a wide range of built-in security checks for AWS, Azure, and GCP providers. However, organizations often have specific compliance or security requirements that fall outside general use cases (e.g., requiring a specific CostCentre tag on all EC2 instances). Custom checks allow you to inject these organization-specific rules into your tfsec configuration.
  4. AWS EKS security checks in tfsec

    master
    tfsec includes a suite of security checks specifically for Amazon Elastic Kubernetes Service (EKS) configurations. These checks validate that EKS clusters are configured with appropriate logging, encryption, and network access controls to prevent unauthorized access and data exposure.
  5. Compare Trivy and tfsec features

    master

    As Aqua Security moves towards Trivy for configuration scanning, note the following functional differences:

    FeatureTrivytfsec
    Policy DistributionEmbedded and Updated via RegistryEmbedded
    Custom PoliciesRegoRego, JSON, and YAML
    Supported FormatsDockerfile, JSON, YAML, Terraform, CloudFormation etc.Terraform Only
  6. Write custom Rego policies for tfsec

    master

    tfsec allows you to implement custom security policies using the Rego language. This is useful for enforcing organization-specific security standards or best practices.

    Policy Requirements

    • Namespace: The package name must start with the custom namespace (e.g., package custom.aws.s3.rules) for tfsec to recognize it.
    • Rule Naming: Rule names must be exactly deny or must start with the prefix deny_ to ensure issues are highlighted during scans.
    • Input Structure: The input variable contains cloud resources organized by provider and service (e.g., input.aws.s3.buckets). All property names are converted to lower-case.
    • Metadata Handling: Properties (like bucket.name) are objects containing metadata. To check the actual value, use the .value suffix (e.g., bucket.name.value).
    • Reporting Results: Use the result.new(msg, source) function to create the result object. This ensures tfsec can report the correct file path and line number.
      • msg: A string explaining the issue.
      • source: The property or object where the violation occurred.
      • Note: If no meaningful source exists, you can return a simple string instead.

    Inspecting the Input Schema

    To understand the structure of the input variable for your specific project, run tfsec with the --print-rego-input flag. You can pipe this to jq to explore the JSON structure:

    tfsec --print-rego-input | jq '.aws.s3.buckets[0].name'
    package custom.aws.s3.no_insecure_buckets
    
    import data.lib.result
    
    deny[res] {
        bucket := input.aws.s3.buckets[_]
        bucket.name.value == "insecure-bucket"
        msg := "Bucket name should not be 'insecure-bucket'"
        res := result.new(msg, bucket.name)
    }
  7. Integrate tfsec into CI pipelines

    master
    You can add tfsec to any CI pipeline. The tool uses exit codes to signal results: a non-zero exit status indicates that problems were found (which can be used to break the build), while a zero exit status indicates no problems were found. For GitHub users, a dedicated GitHub Action is available that also uploads results to the GitHub code scanning UI.