Implementing Secrets Scanning in CI/CD
Overview
This skill covers implementing automated secrets scanning in CI/CD pipelines using gitleaks and trufflehog. It enables security teams to detect API keys, tokens, passwords, and other credentials that have been accidentally committed to source code repositories, providing a CI gate that blocks deployments containing high-severity findings.
Gitleaks scans git repositories and directories for hardcoded secrets using regex patterns and entropy analysis. TruffleHog performs filesystem and git history scans with optional secret verification against live services. Together they provide comprehensive coverage for secrets detection.
When to Use
- When deploying or configuring implementing secrets scanning in ci cd capabilities in your environment
- When establishing security controls aligned to compliance requirements
- When building or improving security architecture for this domain
- When conducting security assessments that require this implementation
Prerequisites
- Python 3.9 or later
- gitleaks v8.x installed and available on PATH
- trufflehog v3.x installed and available on PATH
- A git repository or directory to scan
- Access to CI/CD platform (GitHub Actions, GitLab CI, Jenkins)
Steps
Install scanning tools: Install gitleaks via package manager or binary download. Install trufflehog via brew install trufflehog or download from GitHub releases.
Configure gitleaks: Create a .gitleaks.toml configuration file in the repository root to define custom rules, allowlists, and path exclusions. Use --config flag to point to custom configs.
Run gitleaks directory scan: Execute gitleaks dir --source . --report-format json --report-path gitleaks-report.json to scan the working directory and generate a JSON report.
Run trufflehog filesystem scan: Execute trufflehog filesystem /path/to/repo --json > trufflehog-report.json to scan files and output JSON findings to a report file.
Parse and filter findings: Use the agent script to parse both JSON reports, filter findings by severity (critical, high, medium, low), and determine whether the CI pipeline should pass or fail.
Integrate into CI pipeline: Add the scanning step to your GitHub Actions workflow, GitLab CI config, or Jenkins pipeline as a pre-deployment gate. Use --exit-code flag in gitleaks to control pipeline behavior.
Configure pre-commit hooks: Set up gitleaks as a pre-commit hook using gitleaks protect --staged to catch secrets before they are committed.
Review and triage findings: Examine the JSON output for false positives, add legitimate entries to .gitleaksignore, and rotate any confirmed leaked credentials immediately.
Expected Output
The agent script produces a JSON report containing:
- Total findings count from each scanner
- Findings grouped by severity level
- Individual finding details including file path, line number, rule ID, and redacted secret
- A CI gate verdict (pass/fail) based on the configured severity threshold
- Execution metadata including scan duration and tool versions
{
"scan_summary": {
"tool": "both",
"total_findings": 3,
"critical": 1,
"high": 1,
"medium": 1,
"low": 0,
"ci_gate": "FAIL",
"fail_reason": "Found 1 critical and 1 high severity findings"
},
"findings": [...]
}
1---
2name: implementing-secrets-scanning-in-ci-cd
3description: Integrate gitleaks and trufflehog into CI/CD pipelines to detect leaked
4 secrets before deployment
5domain: cybersecurity
6subdomain: devsecops
7tags:
8- secrets-scanning
9- gitleaks
10- trufflehog
11- ci-cd
12version: '1.0'
13author: mahipal
14license: Apache-2.0
15nist_csf:
16- PR.PS-01
17- GV.SC-07
18- ID.IM-04
19- PR.PS-04
20mitre_attack:
21- T1195
22- T1554
23- T1059.004
24source: https://github.com/mukul975/Anthropic-Cybersecurity-Skills
25source_commit: 04450304b12645cb2b974ab96d28c0664758a88d
26metadata:
27 source: https://skillmd.com/skills/yanacuti1121/implementing-secrets-scanning-in-ci-cd
28---
29
30
31# Implementing Secrets Scanning in CI/CD
32
33## Overview
34
35This skill covers implementing automated secrets scanning in CI/CD pipelines using gitleaks and trufflehog. It enables security teams to detect API keys, tokens, passwords, and other credentials that have been accidentally committed to source code repositories, providing a CI gate that blocks deployments containing high-severity findings.
36
37Gitleaks scans git repositories and directories for hardcoded secrets using regex patterns and entropy analysis. TruffleHog performs filesystem and git history scans with optional secret verification against live services. Together they provide comprehensive coverage for secrets detection.
38
39
40## When to Use
41
42- When deploying or configuring implementing secrets scanning in ci cd capabilities in your environment
43- When establishing security controls aligned to compliance requirements
44- When building or improving security architecture for this domain
45- When conducting security assessments that require this implementation
46
47## Prerequisites
48
49- Python 3.9 or later
50- gitleaks v8.x installed and available on PATH
51- trufflehog v3.x installed and available on PATH
52- A git repository or directory to scan
53- Access to CI/CD platform (GitHub Actions, GitLab CI, Jenkins)
54
55## Steps
56
571. **Install scanning tools**: Install gitleaks via package manager or binary download. Install trufflehog via `brew install trufflehog` or download from GitHub releases.
58
592. **Configure gitleaks**: Create a `.gitleaks.toml` configuration file in the repository root to define custom rules, allowlists, and path exclusions. Use `--config` flag to point to custom configs.
60
613. **Run gitleaks directory scan**: Execute `gitleaks dir --source . --report-format json --report-path gitleaks-report.json` to scan the working directory and generate a JSON report.
62
634. **Run trufflehog filesystem scan**: Execute `trufflehog filesystem /path/to/repo --json > trufflehog-report.json` to scan files and output JSON findings to a report file.
64
655. **Parse and filter findings**: Use the agent script to parse both JSON reports, filter findings by severity (critical, high, medium, low), and determine whether the CI pipeline should pass or fail.
66
676. **Integrate into CI pipeline**: Add the scanning step to your GitHub Actions workflow, GitLab CI config, or Jenkins pipeline as a pre-deployment gate. Use `--exit-code` flag in gitleaks to control pipeline behavior.
68
697. **Configure pre-commit hooks**: Set up gitleaks as a pre-commit hook using `gitleaks protect --staged` to catch secrets before they are committed.
70
718. **Review and triage findings**: Examine the JSON output for false positives, add legitimate entries to `.gitleaksignore`, and rotate any confirmed leaked credentials immediately.
72
73## Expected Output
74
75The agent script produces a JSON report containing:
76- Total findings count from each scanner
77- Findings grouped by severity level
78- Individual finding details including file path, line number, rule ID, and redacted secret
79- A CI gate verdict (pass/fail) based on the configured severity threshold
80- Execution metadata including scan duration and tool versions
81
82```json
83{
84 "scan_summary": {
85 "tool": "both",
86 "total_findings": 3,
87 "critical": 1,
88 "high": 1,
89 "medium": 1,
90 "low": 0,
91 "ci_gate": "FAIL",
92 "fail_reason": "Found 1 critical and 1 high severity findings"
93 },
94 "findings": [...]
95}
96```