-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathaction.yml
More file actions
469 lines (402 loc) · 16 KB
/
action.yml
File metadata and controls
469 lines (402 loc) · 16 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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
name: "Infisical secrets check"
description: "Run Infisical secrets check on a GitHub repository."
branding:
icon: "alert-triangle"
color: "green"
inputs:
GH_TOKEN:
description: 'GitHub token'
required: false
default: ${{ github.TOKEN }}
ADD_COMMENT:
description: 'Comment result in the pull request'
required: false
default: true
outputs:
secrets-leaked:
description: "The number of secrets leaked found by the Infisical CLI tool."
value: ${{ steps.count.outputs.secrets-leaked }}
runs:
using: "composite"
steps:
- name: Check if PR is from a fork
id: check_fork
shell: bash
run: |
if [[ "${{ github.event.pull_request.head.repo.fork }}" == "true" ]]; then
echo "FORKED=true" >> $GITHUB_ENV
else
echo "FORKED=false" >> $GITHUB_ENV
fi
- name: Checkout repo
uses: actions/checkout@v6
with:
fetch-depth: 0
- name: Cache pip packages
uses: actions/cache@v5
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-csvkit-v1
restore-keys: |
${{ runner.os }}-pip-
- name: Cache npm global packages
uses: actions/cache@v5
with:
path: ~/.cache/npm
key: ${{ runner.os }}-npm-csv2md-v1
restore-keys: |
${{ runner.os }}-npm-
- name: Get latest valid Infisical CLI version
id: infisical-version
shell: bash
run: |
echo "🔍 Fetching latest valid Infisical CLI version..."
# Fetch all releases (up to 30 most recent)
JSON=$(curl -s "https://api.github.com/repos/Infisical/cli/releases?per_page=100")
# Check if we got a valid response
if [[ "$JSON" == "null" || -z "$JSON" || "$JSON" == "[]" ]]; then
echo "❌ Failed to fetch releases from GitHub API"
echo "Response: $JSON"
echo "FETCH_FAILED=true" >> $GITHUB_ENV
exit 1
fi
ARCH="linux_amd64"
FOUND_VERSION=""
FOUND_TAG=""
# Find the first release that matches our criteria:
# 1. Tag name starts with "v"
# 2. Tag name does not contain "-postgres"
# 3. Is not a prerelease
# 4. Is not a draft
# 5. Has the expected CLI asset for this architecture
while IFS= read -r TAG_NAME; do
# Extract version from tag (remove "v" prefix)
VERSION=$(echo "$TAG_NAME" | sed 's|v||')
ASSET="cli_${VERSION}_${ARCH}.tar.gz"
# Check if the release actually contains the expected asset
HAS_ASSET=$(echo "$JSON" | jq -r --arg tag "$TAG_NAME" --arg asset "$ASSET" '
.[] | select(.tag_name == $tag) | .assets[] | select(.name == $asset) | .name
')
if [[ -n "$HAS_ASSET" ]]; then
echo "✅ Found release $TAG_NAME with asset $ASSET"
FOUND_VERSION="$VERSION"
FOUND_TAG="$TAG_NAME"
break
else
echo "⚠️ Release $TAG_NAME does not have asset $ASSET, skipping..."
fi
done < <(echo "$JSON" | jq -r '
.[] |
select(.draft == false) |
select(.prerelease == false) |
select(.tag_name | startswith("v")) |
select(.tag_name | contains("-postgres") | not) |
.tag_name |
select(. != null)
')
# Check if we found a valid tag
if [[ -z "$FOUND_VERSION" || -z "$FOUND_TAG" ]]; then
echo "❌ No valid Infisical CLI release found with the expected asset"
echo "Available releases:"
echo "$JSON" | jq -r '.[] | select(.draft == false) | .tag_name' | head -10
echo "FETCH_FAILED=true" >> $GITHUB_ENV
exit 1
fi
echo "🔍 Found valid CLI version: $FOUND_VERSION (tag: $FOUND_TAG)"
echo "version=$FOUND_VERSION" >> "$GITHUB_OUTPUT"
echo "tag_name=$FOUND_TAG" >> "$GITHUB_OUTPUT"
echo "FETCH_FAILED=false" >> $GITHUB_ENV
- name: Cache Infisical CLI
uses: actions/cache@v5
id: infisical-cache
if: env.FETCH_FAILED != 'true'
with:
path: .tools/infisical
key: ${{ runner.os }}-infisical-v${{ steps.infisical-version.outputs.version }}
- name: Install Infisical CLI if needed
if: env.FETCH_FAILED != 'true' && steps.infisical-cache.outputs.cache-hit != 'true'
shell: bash
run: |
VERSION=${{ steps.infisical-version.outputs.version }}
TAG_NAME=${{ steps.infisical-version.outputs.tag_name }}
ASSET="cli_${VERSION}_linux_amd64.tar.gz"
# URL encode the tag name for the download URL
ENCODED_TAG=$(echo "$TAG_NAME" | sed 's|/|%2F|g')
URL="https://github.com/Infisical/cli/releases/download/$ENCODED_TAG/$ASSET"
echo "⬇️ Downloading Infisical CLI $VERSION from $URL"
mkdir -p .tools/infisical
# Download and verify
if ! curl -sL "$URL" | tar -xz -C .tools/infisical; then
echo "❌ Failed to download or extract Infisical CLI"
echo "❌ URL attempted: $URL"
echo "❌ Asset expected: $ASSET"
echo "INSTALL_FAILED=true" >> $GITHUB_ENV
exit 1
fi
chmod +x .tools/infisical/infisical
echo "✅ Infisical CLI installed successfully"
echo "INSTALL_FAILED=false" >> $GITHUB_ENV
- name: Add Infisical to PATH
shell: bash
if: env.FETCH_FAILED != 'true' && env.INSTALL_FAILED != 'true'
run: echo "$GITHUB_WORKSPACE/.tools/infisical" >> $GITHUB_PATH
- name: Install tools
shell: bash
if: env.FETCH_FAILED != 'true' && env.INSTALL_FAILED != 'true'
run: |
version=$(pip show pip | grep Version: | cut -d' ' -f2)
lower=$(printf "23.1\n$version" | sort -V | head -n1)
if [ "$lower" == "23.1" ]; then
pip install csvkit --break-system-packages
else
pip install csvkit
fi
npm install -g csv-to-markdown-table
- name: Run scan
shell: bash
if: env.FETCH_FAILED != 'true' && env.INSTALL_FAILED != 'true'
run: |
echo "🔍 Running Infisical secrets scan..."
if infisical scan --redact -f csv -r secrets-result-raw.csv 2>&1 | tee >(sed -r 's/\x1b\[[0-9;]*m//g' >secrets-result.log); then
echo "SCAN_SUCCESS=true" >> $GITHUB_ENV
else
echo "SCAN_SUCCESS=false" >> $GITHUB_ENV
# Even if scan fails, we want to continue to check if it was due to secrets found
fi
- name: Check scan results and generate report
shell: bash
if: always() && env.FETCH_FAILED != 'true' && env.INSTALL_FAILED != 'true'
run: |
# Check if scan actually ran and produced results
if [[ -f "secrets-result.log" ]]; then
echo "SCAN_RAN=true" >> $GITHUB_ENV
# Check if secrets were found (file exists and has content)
if [[ -s "secrets-result-raw.csv" ]]; then
echo "SECRETS_FOUND=true" >> $GITHUB_ENV
echo "🚨 Secrets detected, generating report..."
# Process the CSV file
csvformat -M $'\r' secrets-result-raw.csv | sed -e ':a' -e 'N;$!ba' -e 's/\n/\\n/g' | tr '\r' '\n' > secrets.csv
head -n 11 < secrets.csv > secrets-result.csv
csv-to-markdown-table --delim , --headers < secrets-result.csv > secrets-result.md
awk -F, '{print $(NF-2)}' < secrets.csv | tail -n +2 > fingerprint.txt
else
echo "SECRETS_FOUND=false" >> $GITHUB_ENV
echo "✅ No secrets detected"
fi
else
echo "SCAN_RAN=false" >> $GITHUB_ENV
echo "❌ Scan did not run properly - no log file generated"
fi
- name: Count secrets leaked
shell: bash
if: always()
id: count
run: |
quantity=0
if [[ -s fingerprint.txt ]]; then
quantity=$(wc -l < fingerprint.txt)
fi
echo "secrets-leaked=$quantity" >> $GITHUB_OUTPUT
- name: Upload artifacts secrets-result.log
uses: actions/upload-artifact@v7
if: always() && env.SCAN_RAN == 'true'
with:
name: report-log
path: secrets-result.log
- name: Upload artifacts secrets.csv
uses: actions/upload-artifact@v7
if: env.SECRETS_FOUND == 'true'
id: secrets
with:
name: secrets-csv
path: secrets.csv
- name: Upload artifacts secrets-result.csv
uses: actions/upload-artifact@v7
if: env.SECRETS_FOUND == 'true'
with:
name: report-csv
path: secrets-result.csv
- name: Upload artifacts secrets-result.md
uses: actions/upload-artifact@v7
if: env.SECRETS_FOUND == 'true'
with:
name: report-md
path: secrets-result.md
- name: Upload artifacts fingerprint.txt
uses: actions/upload-artifact@v7
if: env.SECRETS_FOUND == 'true'
with:
name: fingerprint-txt
path: fingerprint.txt
- name: Read secrets-result.log
uses: guibranco/github-file-reader-action-v2@v2.3.2
if: always() && env.SCAN_RAN == 'true'
id: log
with:
path: secrets-result.log
- name: Read secrets-result.md
uses: guibranco/github-file-reader-action-v2@v2.3.2
if: env.SECRETS_FOUND == 'true'
id: report
with:
path: secrets-result.md
- name: Read fingerprint.txt
uses: guibranco/github-file-reader-action-v2@v2.3.2
if: env.SECRETS_FOUND == 'true'
id: fingerprint
with:
path: fingerprint.txt
- name: Check if .infisicalignore exists
id: ignorefile
shell: bash
if: env.SECRETS_FOUND == 'true'
run: |
if [[ -f ".infisicalignore" ]]; then
echo "exists=true" >> $GITHUB_OUTPUT
else
echo "exists=false" >> $GITHUB_OUTPUT
fi
- name: Prepare ignore file suggestion
id: ignore_suggestion
shell: bash
if: env.SECRETS_FOUND == 'true'
run: |
echo "Preparing suggestion block..."
if [[ "${{ steps.ignorefile.outputs.exists }}" == "true" ]]; then
echo "Appending fingerprints to existing .infisicalignore"
cat .infisicalignore > new-ignorefile.txt
echo "" >> new-ignorefile.txt
cat fingerprint.txt >> new-ignorefile.txt
else
echo "Creating new .infisicalignore"
cat fingerprint.txt > new-ignorefile.txt
fi
# Remove duplicates (optional improvement)
sort -u new-ignorefile.txt -o new-ignorefile.txt
echo "suggestion<<EOF" >> $GITHUB_OUTPUT
echo '```suggestion:.infisicalignore' >> $GITHUB_OUTPUT
cat new-ignorefile.txt >> $GITHUB_OUTPUT
echo '```' >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
- name: Create review suggestion for .infisicalignore
uses: actions/github-script@v7
if: env.SECRETS_FOUND == 'true' && env.FORKED == 'false' && inputs.ADD_COMMENT == 'true'
with:
github-token: ${{ inputs.GH_TOKEN }}
script: |
const fs = require('fs');
const suggestion =
fs.readFileSync('new-ignorefile.txt', 'utf8');
const suggestionBlock =
"```suggestion:.infisicalignore\n" +
suggestion +
"\n```";
const pr = context.payload.pull_request;
if (!pr) {
core.info("Not a PR event — skipping review suggestion.");
return;
}
await github.rest.pulls.createReview({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: pr.number,
event: "COMMENT",
body:
"### Suggested update for `.infisicalignore`\n\n" +
"Apply this suggestion to ignore detected fingerprints:\n\n" +
suggestionBlock
});
core.info("Review suggestion posted successfully.");
- name: Update PR with comment (success)
uses: mshick/add-pr-comment@v3
if: env.SCAN_RAN == 'true' && env.SECRETS_FOUND == 'false' && env.FORKED == 'false' && inputs.ADD_COMMENT == 'true'
with:
repo-token: ${{ inputs.GH_TOKEN }}
refresh-message-position: true
message-id: "secrets-result"
message: |
**Infisical secrets check:** ✅ No secrets leaked!
<details>
<summary>💻 Scan logs</summary>
```txt
${{ steps.log.outputs.contents }}
```
</details>
- name: Update PR with comment (failure - secrets found)
uses: mshick/add-pr-comment@v3
if: env.SECRETS_FOUND == 'true' && env.FORKED == 'false' && inputs.ADD_COMMENT == 'true'
with:
repo-token: ${{ inputs.GH_TOKEN }}
refresh-message-position: true
message-id: "secrets-result"
message: |
**Infisical secrets check:** 🚨 Secrets leaked!
> [!CAUTION]
> The Infisical CLI tool found secrets leaked in your repository.
> Please review the scan results and take the necessary actions.
> Secrets found: ${{ steps.count.outputs.secrets-leaked }}
<details>
<summary>💻 Scan logs</summary>
```txt
${{ steps.log.outputs.contents }}
```
</details>
---
<details>
<summary>🔎 Detected secrets in your GIT history</summary>
${{ steps.report.outputs.contents }}
</details>
> [!WARNING]
> The above table only displays the first 10 leaked secrets.
> You can find the full report here: [secrets.csv](${{ steps.secrets.outputs.artifact-url }})
---
<details>
<summary>🐾 Secrets fingerprint</summary>
```txt
${{ steps.fingerprint.outputs.contents }}
```
</details>
> [!TIP]
> If you want to ignore these leaked secrets, add the above **fingerprint** content to a file named `.infisicalignore` at the repository root level.
- name: Update PR with comment (tool failure)
uses: mshick/add-pr-comment@v3
if: (env.FETCH_FAILED == 'true' || env.INSTALL_FAILED == 'true' || env.SCAN_RAN == 'false') && env.FORKED == 'false' && inputs.ADD_COMMENT == 'true'
with:
repo-token: ${{ inputs.GH_TOKEN }}
refresh-message-position: true
message-id: "secrets-result"
message: |
**Infisical secrets check:** ⚠️ Tool failure - Unable to complete scan!
> [!WARNING]
> The Infisical secrets scan could not be completed due to a tool failure.
> This may be due to:
> - GitHub API rate limiting when fetching the latest CLI version
> - Network issues during CLI download
> - CLI execution problems
**What to do:**
1. **Re-run the workflow** - This is often a temporary issue
2. Check the [workflow logs](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}) for specific error details
3. If the issue persists, please report it to the action maintainer
> [!NOTE]
> This failure does **not** mean secrets were found - the scan simply couldn't run.
- name: Update PR with comment (cancelled)
uses: mshick/add-pr-comment@v3
if: cancelled() && env.FORKED == 'false' && inputs.ADD_COMMENT == 'true'
with:
repo-token: ${{ inputs.GH_TOKEN }}
refresh-message-position: true
message-id: "secrets-result"
message: |
**Infisical secrets check:** ⭕ Secrets check cancelled!
- name: Fail workflow if secrets found
shell: bash
if: env.SECRETS_FOUND == 'true'
run: |
echo "❌ Failing workflow due to secrets found"
exit 1
- name: Fail workflow if tool failed
shell: bash
if: env.FETCH_FAILED == 'true' || env.INSTALL_FAILED == 'true' || env.SCAN_RAN == 'false'
run: |
echo "❌ Failing workflow due to tool failure"
exit 1