-
Notifications
You must be signed in to change notification settings - Fork 10
293 lines (262 loc) · 11.3 KB
/
reusable-trufflehog.yml
File metadata and controls
293 lines (262 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
name: Reusable TruffleHog Secret Scan
on:
workflow_call:
inputs:
fail-on-verified:
description: "Fail workflow on verified secrets"
required: false
default: "true"
type: string
fail-on-unverified:
description: "Fail workflow on unverified secrets"
required: false
default: "false"
type: string
runs-on:
description: "The runner to use for the job"
required: false
default: "ubuntu-latest"
type: string
permissions:
contents: read
pull-requests: write
env:
# renovate: datasource=github-releases depName=trufflesecurity/trufflehog
TRUFFLEHOG_VERSION: 3.89.2
jobs:
trufflehog-scan:
runs-on: ${{ inputs.runs-on }}
steps:
- name: Checkout repository
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
with:
fetch-depth: 100
persist-credentials: false
- name: Install TruffleHog
run: |
# Download binary directly from GitHub releases for supply chain security
VERSION="v${{ env.TRUFFLEHOG_VERSION }}"
# Auto-detect architecture for cross-platform support
if [[ "$(uname -m)" == "aarch64" ]]; then
ARCH="linux_arm64"
else
ARCH="linux_amd64"
fi
BINARY_URL="https://github.com/trufflesecurity/trufflehog/releases/download/${VERSION}/trufflehog_${VERSION#v}_${ARCH}.tar.gz"
curl -sSfL "${BINARY_URL}" | tar -xz -C /tmp
sudo mv /tmp/trufflehog /usr/local/bin/trufflehog
sudo chmod +x /usr/local/bin/trufflehog
trufflehog --version
- name: Scan for secrets
id: scan
run: |
set +e
echo "[]" > results.json
if [[ "${{ github.event_name }}" == "pull_request" ]]; then
# PR: Scan only changed files
echo "Scanning changed files in PR..."
git diff --name-only ${{ github.event.pull_request.base.sha }}..${{ github.sha }} > changed-files.txt
if [[ -s changed-files.txt ]]; then
while IFS= read -r file; do
if [[ -f "${file}" ]]; then
echo "Scanning: ${file}"
trufflehog filesystem "${file}" --json --no-update --results=verified,unverified >> results.ndjson || true
fi
done < changed-files.txt
else
echo "No files changed"
fi
else
# Push to main: Scan current filesystem
echo "Scanning current filesystem..."
trufflehog filesystem . --json --no-update --results=verified,unverified > results.ndjson || true
fi
# Process results
if [[ -s results.ndjson ]]; then
grep -v '^$' results.ndjson | jq -s '.' > results.json 2>/dev/null || echo "[]" > results.json
else
# Ensure results.json exists even if no results
echo "[]" > results.json
fi
# Count secrets
if jq empty results.json 2>/dev/null; then
VERIFIED=$(jq '[.[] | select(.Verified==true)] | length' results.json 2>/dev/null || echo "0")
UNVERIFIED=$(jq '[.[] | select(.Verified==false)] | length' results.json 2>/dev/null || echo "0")
else
VERIFIED=0
UNVERIFIED=0
echo "[]" > results.json
fi
TOTAL=$((VERIFIED+UNVERIFIED))
echo "Found ${TOTAL} potential secrets (${VERIFIED} verified, ${UNVERIFIED} unverified)"
echo "verified=${VERIFIED}" >> "${GITHUB_OUTPUT}"
echo "unverified=${UNVERIFIED}" >> "${GITHUB_OUTPUT}"
echo "total=${TOTAL}" >> "${GITHUB_OUTPUT}"
- name: Generate PR comment
if: ${{ !cancelled() && github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository }}
id: comment-body
run: |
VERIFIED=${{ steps.scan.outputs.verified }}
UNVERIFIED=${{ steps.scan.outputs.unverified }}
TOTAL=$((VERIFIED+UNVERIFIED))
if [[ $TOTAL -eq 0 ]]; then
{
echo 'body<<EOF'
echo '**TruffleHog Scan Results**'
echo ''
echo 'No secrets detected in this PR.'
echo 'EOF'
} >> "$GITHUB_OUTPUT"
else
# Generate findings list
FINDINGS=$(jq -r '.[] |
"- " +
(if .Verified then "**VERIFIED SECRET**" else "**Possible secret**" end) +
" (" + .DetectorName + ") at `" +
((.SourceMetadata?.Data?.Filesystem?.file // .SourceMetadata?.Data?.Git?.file) // "unknown") +
":" +
((.SourceMetadata?.Data?.Filesystem?.line // .SourceMetadata?.Data?.Git?.line) | tostring) +
"` → `" +
(if (.Raw | length) > 8 then (.Raw[:4] + "***" + .Raw[-4:]) else "***" end) +
"`"' results.json 2>/dev/null || echo "- Error processing results")
ACTION_TEXT=""
if [[ $VERIFIED -gt 0 ]]; then
ACTION_TEXT="**ACTION REQUIRED:** Rotate verified credentials immediately."
else
ACTION_TEXT="**Review:** Check if unverified secrets are false positives."
fi
{
echo 'body<<EOF'
echo "**TruffleHog Scan Results**"
echo ''
echo "**Summary:** Found ${TOTAL} potential secrets (${VERIFIED} verified, ${UNVERIFIED} unverified)"
echo ''
echo "${FINDINGS}"
echo ''
echo "${ACTION_TEXT}"
echo 'EOF'
} >> "$GITHUB_OUTPUT"
fi
- name: Post PR comment
if: ${{ !cancelled() && github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository }}
uses: mshick/add-pr-comment@b8f338c590a895d50bcbfa6c5859251edc8952fc # v2.8.2
with:
# We retain the final trufflehog-secret-scan-comment marker in case we want to chang the action to another one.
message: |
${{ steps.comment-body.outputs.body }}
<!-- trufflehog-secret-scan-comment -->
message-id: trufflehog-secret-scan-comment
- name: Create scan report
env:
GITHUB_REF_NAME: ${{ github.ref_name }}
run: |
{
echo "TruffleHog Scan Report"
echo "====================="
echo "Date: $(date)"
echo "Repository: ${{ github.repository }}"
echo "Branch: ${GITHUB_REF_NAME}"
echo "Commit: ${{ github.sha }}"
echo ""
echo "Summary:"
echo "- Total secrets: ${{ steps.scan.outputs.total }}"
echo "- Verified: ${{ steps.scan.outputs.verified }}"
echo "- Unverified: ${{ steps.scan.outputs.unverified }}"
echo ""
echo "Detailed Results:"
if [[ -f "results.json" && -s "results.json" ]] && jq empty results.json 2>/dev/null; then
jq -r '.[] | "- " + (if .Verified then "VERIFIED" else "Unverified" end) + " " + .DetectorName + " at " + ((.SourceMetadata?.Data?.Filesystem?.file // .SourceMetadata?.Data?.Git?.file) // "unknown") + ":" + ((.SourceMetadata?.Data?.Filesystem?.line // .SourceMetadata?.Data?.Git?.line) | tostring) + " → " + (if (.Raw | length) > 8 then (.Raw[:4] + "***" + .Raw[-4:]) else "***" end)' results.json 2>/dev/null || echo "Error processing results"
else
echo "No secrets detected"
fi
} > trufflehog_scan.txt
- name: Verify JSON file exists
if: always()
run: |
if [[ -f "results.json" ]]; then
echo "✅ results.json exists"
echo "File size: $(wc -c < results.json) bytes"
echo "JSON validity check:"
if jq empty results.json 2>/dev/null; then
echo "✅ Valid JSON"
echo "Number of findings: $(jq '. | length' results.json)"
else
echo "❌ Invalid JSON"
fi
else
echo "❌ results.json does not exist"
fi
- name: Prepare artifacts for upload
if: always()
run: |
# Ensure results.json exists
if [[ ! -f "results.json" ]]; then
echo "[]" > results.json
echo "Created empty results.json"
fi
# Create artifacts directory and copy files
mkdir -p trufflehog-artifacts
cp trufflehog_scan.txt trufflehog-artifacts/ 2>/dev/null || echo "trufflehog_scan.txt not found"
cp results.json trufflehog-artifacts/ 2>/dev/null || echo "results.json not found"
echo "Files in artifacts directory:"
ls -la trufflehog-artifacts/
- name: Upload scan results
uses: actions/upload-artifact@50769540e7f4bd5e21e526ee35c689e35e0d6874 # v4.4.0
if: always()
with:
name: trufflehog_scan
path: trufflehog-artifacts/
retention-days: 30
- name: Setup Python for Grafana integration
if: ${{ !cancelled() && (secrets.LOKI_URL != '' || secrets.PROMETHEUS_PUSHGATEWAY_URL != '') }}
uses: actions/setup-python@0b6a380b5a7827e48e69b2e0e596c5c8c2b0e0b0 # v5.1.0
with:
python-version: '3.x'
- name: Install Python dependencies
if: ${{ !cancelled() && (secrets.LOKI_URL != '' || secrets.PROMETHEUS_PUSHGATEWAY_URL != '') }}
run: |
pip install requests
- name: Send findings to Loki
if: ${{ !cancelled() && secrets.LOKI_URL != '' }}
continue-on-error: true
env:
LOKI_URL: ${{ secrets.LOKI_URL }}
LOKI_USERNAME: ${{ secrets.LOKI_USERNAME }}
LOKI_PASSWORD: ${{ secrets.LOKI_PASSWORD }}
REPOSITORY: ${{ github.repository }}
COMMIT_SHA: ${{ github.sha }}
BRANCH: ${{ github.ref_name }}
TRUFFLEHOG_RESULTS_FILE: results.json
run: |
python trufflehog/send-to-loki.py
- name: Send metrics to Prometheus
if: ${{ !cancelled() && secrets.PROMETHEUS_PUSHGATEWAY_URL != '' }}
continue-on-error: true
env:
PROMETHEUS_PUSHGATEWAY_URL: ${{ secrets.PROMETHEUS_PUSHGATEWAY_URL }}
REPOSITORY: ${{ github.repository }}
COMMIT_SHA: ${{ github.sha }}
BRANCH: ${{ github.ref_name }}
TRUFFLEHOG_RESULTS_FILE: results.json
run: |
python trufflehog/send-to-prometheus.py
- name: Check failure policy
env:
FAIL_ON_VERIFIED: ${{ inputs.fail-on-verified }}
FAIL_ON_UNVERIFIED: ${{ inputs.fail-on-unverified }}
VERIFIED_COUNT: ${{ steps.scan.outputs.verified }}
UNVERIFIED_COUNT: ${{ steps.scan.outputs.unverified }}
run: |
SHOULD_FAIL=false
if [[ "${FAIL_ON_VERIFIED}" == "true" && "${VERIFIED_COUNT}" != "0" ]]; then
SHOULD_FAIL=true
fi
if [[ "${FAIL_ON_UNVERIFIED}" == "true" && "${UNVERIFIED_COUNT}" != "0" ]]; then
SHOULD_FAIL=true
fi
if [[ "${SHOULD_FAIL}" == "true" ]]; then
echo "Workflow failed due to secrets found (verified: ${VERIFIED_COUNT}, unverified: ${UNVERIFIED_COUNT})"
exit 1
else
echo "No action needed - secrets within configured thresholds"
fi