Roadmaps / Cybersecurity / Lab 6

Lab 6 of 8 · Cloud security and DevSecOps

Catch leaked secrets and risky cloud settings automatically

Find a planted fake token with Gitleaks, find and fix risky AWS settings in Terraform with Checkov, and run both checks on every push.

Time
60–90 minutes
Cost
Free. Nothing is deployed to AWS: the Terraform is only scanned.

What you learn

In the cloud, most incidents come from leaked keys and misconfigured settings, not clever hacks. DevSecOps catches both automatically, before code is merged.

  • Secret scanning before and after commits
  • Infrastructure-as-code scanning for risky cloud settings
  • Least privilege for people and services
  • Running security checks in CI

In the AI era: AI coding tools sometimes paste keys into code or open security groups to the whole internet. Automated checks in the pipeline catch those mistakes whoever made them.

Before you start

  • Finish lab 2. Docker must be running.

Step by step

  1. 1 Plant a fake secret (do not commit it)

    This creates a file containing a random, fake GitHub-style token, so you can see the scanner catch it.

    Run this
    mkdir -p scratch
    echo "GITHUB_TOKEN = \"ghp_$(openssl rand -hex 18)\"" > scratch/config.py
    cat scratch/config.py
  2. 2 Scan the folder

    Gitleaks looks for hundreds of key formats. Running it before you commit is the cheapest protection there is.

    Run this
    docker run --rm -v "$PWD:/repo" zricethezav/gitleaks:latest dir /repo/scratch -v
    You should see:

    A finding with RuleID: github-pat and leaks found: 1.

  3. 3 Remove it and scan your Git history

    Secrets that were ever committed stay in history, so scan that too. If a real key is ever found, revoke it first, then clean up.

    Run this
    rm -rf scratch
    docker run --rm -v "$PWD:/repo" zricethezav/gitleaks:latest git /repo -v
    You should see:

    no leaks found.

  4. 4 Write risky Terraform on purpose

    This describes an S3 bucket and a firewall rule that opens SSH to the whole internet. It is only scanned, never applied.

    File: terraform/main.tf
    resource "aws_s3_bucket" "reports" {
      bucket = "home-soc-lab-incident-reports-example"
    }
    
    resource "aws_security_group" "admin" {
      name        = "admin-access"
      description = "Admin access"
    
      ingress {
        description = "SSH from anywhere"
        from_port   = 22
        to_port     = 22
        protocol    = "tcp"
        cidr_blocks = ["0.0.0.0/0"]
      }
    }
    
  5. 5 Scan it with Checkov

    Checkov compares infrastructure code with hundreds of security rules.

    Run this
    docker run --rm -v "$PWD/terraform:/tf" bridgecrew/checkov -d /tf --compact --quiet
    You should see:

    FAILED checks including CKV_AWS_24 (SSH open to the internet) and several S3 bucket checks.

  6. 6 Fix the SSH rule and record the bucket risks

    Restrict SSH to one admin network (here a documentation range), then scan again: CKV_AWS_24 should pass. In notes/06-cloud.md, list the remaining S3 findings and what you would change (block public access, encryption, versioning, logging).

    File: terraform/main.tf
    resource "aws_s3_bucket" "reports" {
      bucket = "home-soc-lab-incident-reports-example"
    }
    
    resource "aws_s3_bucket_public_access_block" "reports" {
      bucket                  = aws_s3_bucket.reports.id
      block_public_acls       = true
      block_public_policy     = true
      ignore_public_acls      = true
      restrict_public_buckets = true
    }
    
    resource "aws_security_group" "admin" {
      name        = "admin-access"
      description = "Admin access from the office network only"
    
      ingress {
        description = "SSH from the admin network"
        from_port   = 22
        to_port     = 22
        protocol    = "tcp"
        cidr_blocks = ["198.51.100.0/24"]
      }
    }
    
  7. 7 Run the checks on every push

    Your tests, secret scanning and the Terraform scan now run automatically. --soft-fail keeps the documented S3 findings visible without blocking you; remove it once they are fixed.

    File: .github/workflows/security.yml
    name: Security checks
    
    on:
      push:
        branches: [main]
      pull_request:
    
    jobs:
      checks:
        runs-on: ubuntu-latest
        steps:
          - uses: actions/checkout@v4
            with:
              fetch-depth: 0
          - uses: actions/setup-python@v5
            with:
              python-version: "3.12"
          - run: pip install -r requirements.txt
          - run: python -m pytest -q
          - name: Secret scan
            run: docker run --rm -v "$PWD:/repo" zricethezav/gitleaks:latest git /repo -v
          - name: Terraform scan
            run: docker run --rm -v "$PWD/terraform:/tf" bridgecrew/checkov -d /tf --compact --soft-fail
    
  8. 8 Commit and push

    Open the Actions tab and check the run is green.

    Run this
    git add terraform notes .github
    git commit -m "Add secret scanning and Terraform security checks"
    git push

You are done when

  • Gitleaks caught the planted fake token, and your history is clean.

  • Checkov no longer reports SSH open to the internet.

  • The security workflow runs on every push.

Finished? Get your proof page

We check your public GitHub repository and give you a page to share with recruiters, and can add the project to your resume.