Skip to content

sudpaw/sarif-third-party-sast-tools

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Terraform Security Scanning with tfsec

Overview

tfsec is a static analysis tool that scans Terraform code for security vulnerabilities and best practice violations.

This repository uses tfsec along with GitHub Actions to automatically scan Terraform configurations and upload security findings to GitHub Security Code Scanning.

Setup

To integrate tfsec into your GitHub workflow, follow these steps:

How to start this course

start-course

1. Add a GitHub Actions Workflow

Create a file .github/workflows/tfsec.yml with the following content:

name: Terraform Security Scan

on:
  push:
    branches:
      - main
  pull_request:

jobs:
  tfsec:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
      
      - name: Run tfsec with SARIF output
        uses: aquasecurity/[email protected]
        with:
          sarif_file: tfsec.sarif

      - name: Upload SARIF report to GitHub
        uses: github/codeql-action/upload-sarif@v3
        with:
          sarif_file: tfsec.sarif

2. Enable GitHub Code Scanning

Add this Terraform file with tha name [main.tf]

resource "aws_security_group_rule" "my-rule" {
    type        = "ingress"
    cidr_blocks = ["0.0.0.0/0"]
}

resource "aws_alb_listener" "my-valid-alb-listener"{
    port     = "80"
    protocol = "HTTPS"
}

resource "aws_alb_listener" "my-wrong-alb-listener"{
    port     = "80"
    protocol = "HTTP"
}

resource "aws_db_security_group" "my-group" {

}

variable "enableEncryption" {
    default = false
}

resource "azurerm_managed_disk" "source" {
    encryption_settings {
        enabled = var.enableEncryption
    }
}

3. Code Scanning Tab

  1. Click on the Security tab.
  2. Under Code scanning,
  3. Review all your security issues.

Running tfsec Locally

If you want to run tfsec locally before pushing changes, install it with:

brew install tfsec  # macOS (Homebrew)
# or
curl -s https://raw.githubusercontent.com/aquasecurity/tfsec/main/scripts/install_linux.sh | bash  # Linux
# or
scoop install tfsec  # Windows (Scoop)

Then scan your Terraform code:

tfsec .

Interpreting Results

  • High/Medium/Low Issues: tfsec categorizes findings by severity.
  • Fixing Issues: Follow recommendations provided in the scan output.
  • Suppressing False Positives: Use #tfsec:ignore:<rule-id> comments in your Terraform files.

References

About

GitHub Skills: Introduction to sarif

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages