Ravindra BagaleCourses & study guides Track your progress

Labs · DevOps

Lab: Add Gitleaks and Trivy Scan Stages to a Jenkins Pipeline So a Leaked Key or Critical CVE Fails the Build

Intermediate50 minJenkins with Docker available to the agent · Docker · Gitleaks and Trivy (run as containers)

Course: DevOps · Chapter 29: Scan Pipeline in Jenkins · Lesson: 29.11 One combined pipeline

Chapter 29 explains each scanner and why scans are gates, not decorations; this lab adds the two most common gates.

Chala mitrano! Scan cha report kunich vaachat nasel tar tyacha upyog shunya. Aaj scan fail zhala tar build pan fail.

Suppose we are…

Suppose we are on a Netflix-style platform team and a developer accidentally commits an AWS key in a test file. Or the base image has a critical vulnerability. If the pipeline only prints a warning, nobody reads it and the problem reaches production.

In this lab two scanners become gates: Gitleaks looks for secrets in the repository, Trivy scans the built image, and a finding stops the build.

Goal of this lab

By the end you will have:

  • A Jenkinsfile with Secret scan and Image scan stages.
  • One red build caused by a fake test key, then a green build after removing it.
  • Scan reports archived on the build page.

What you need

  • The Jenkins and repo from the earlier Jenkins labs, with a Dockerfile in the repo (the Docker lab Dockerfile works).
  • The Jenkins agent can run docker (a lab agent with Docker installed; on the laptop setup, run the agent on a machine with Docker).
  • 50 minutes.

Use only fake keys

Never test a scanner with a real key. The key below is made up for this lab and does not work anywhere.

Part 1: The scan stages

  1. Add these two stages to your Jenkinsfile, after Checkout and before any push or deploy stage:

    Jenkinsfile (stages to add)

        stage('Secret scan') {
          steps {
            sh '''
              docker run --rm -v "$PWD:/repo" zricethezav/gitleaks:latest \
                detect --source /repo --report-format json --report-path /repo/gitleaks.json --exit-code 1
            '''
          }
          post { always { archiveArtifacts artifacts: 'gitleaks.json', allowEmptyArchive: true } }
        }
        stage('Build image') {
          steps {
            sh 'docker build -t badge-api:${BUILD_NUMBER} .'
          }
        }
        stage('Image scan') {
          steps {
            sh '''
              docker run --rm -v /var/run/docker.sock:/var/run/docker.sock aquasec/trivy:latest \
                image --severity CRITICAL --exit-code 1 --no-progress badge-api:${BUILD_NUMBER}
            '''
          }
        }
    

    --exit-code 1 is the important part: it makes each scanner fail the stage when it finds something.

  2. Commit, push and build.

    What you should see: both scan stages green (or a real critical CVE in an old base image; if so, move to a newer base image tag and build again).

Part 2: Make the secret gate fire

  1. Add a file with a made-up key that only looks like an AWS access key id (it is not a real key):

    cat > leaked-config.txt <<'EOF'
    aws_access_key_id = AKIAZ7Q3LABTESTK4X2P
    EOF
    git add leaked-config.txt
    git commit -m "Test: add fake key to prove the secret gate"
    git push
    

    If GitHub push protection blocks this push, that is another gate doing its job. For this fake value you may choose the option that it is used in tests, or run the scan on a branch in a private repo.

  2. Build and open the console.

    What you should see: Gitleaks reports a finding and the stage fails:

    Finding:     aws_access_key_id = AKIAZ7Q3LABTESTK4X2P
    RuleID:      aws-access-token
    File:        leaked-config.txt
    leaks found: 1
    script returned exit code 1
    Finished: FAILURE
    
  3. Open Build Artifacts → gitleaks.json to read the finding as a report.

Part 3: Fix it the right way

  1. Remove the file and commit. Because the key is already in history, Gitleaks still finds it in old commits. For this lab, mark the fake key as allowed in a .gitleaksignore with the fingerprint from the report:

    git rm leaked-config.txt
    git commit -m "Remove fake key file"
    # copy the "Fingerprint" value from gitleaks.json into .gitleaksignore
    echo "PASTE-FINGERPRINT-HERE" > .gitleaksignore
    git add .gitleaksignore
    git commit -m "Allow the documented fake test key"
    git push
    

    With a real leaked key the order is: rotate (disable) the key in AWS first, then clean history. Removing the file alone is not enough.

  2. Build again.

    What you should see: no leaks found, Trivy passes, Finished: SUCCESS.

Ravindra Bagale's Tip

Put scans before the approval step. Then the approver never wastes time on a change that was going to be rejected anyway.

Common mistakes

MistakeWhat happensFix
No --exit-code 1Scanner prints findings but the build stays greenAdd --exit-code 1 to both scanners
docker: permission denied on the agentScan stages fail at startAdd the agent user to the docker group on a lab agent
Deleting the file and calling a real leak fixedKey still valid and still in historyRotate the key first, then clean history
Scanning only HIGH on day one with an old base imageEvery build red, team ignores itStart with CRITICAL, update the base image, then tighten

Self-check checklist

0 of 4 done

Try-at-home challenge

Add a third gate with Checkov for any Terraform folder (docker run --rm -v "$PWD:/src" bridgecrew/checkov -d /src --quiet) and see which rules fail on a bucket without a public access block.

Samajla ka? A scan that cannot fail the build is only decoration. Aata pudhe jaauya Terraform pipeline.