Skip to content

Commit 2b199dd

Browse files
Fix secret scan timestamps (#190)
* Improve secret-scan timestamps * comments * more resiliency * change always to not cancelled
1 parent 27e4914 commit 2b199dd

1 file changed

Lines changed: 35 additions & 4 deletions

File tree

.github/workflows/secret-scan.yml

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,42 @@ jobs:
6060
fi
6161
- name: Send Alert to SIEM
6262
id: alert
63+
env:
64+
SIEM_WEBHOOK_URL: ${{ vars.SECRET_SCAN_SIEM_WEBHOOK_URL }}
65+
SCAN_OUTCOME: ${{ steps.scan.outcome }}
66+
REPO: ${{ github.repository }}
67+
PR_NUMBER: ${{ github.event.pull_request.number }}
68+
PR_CREATED_AT: ${{ github.event.pull_request.created_at }}
69+
PR_ACTOR: ${{ github.event.pull_request.user.login }}
70+
EVENT_ACTOR: ${{ github.actor }}
6371
run: |
64-
if [[ -n "${{vars.SECRET_SCAN_SIEM_WEBHOOK_URL}}" ]]; then
65-
curl "${{vars.SECRET_SCAN_SIEM_WEBHOOK_URL}}" \
66-
--data '{"event":"github_secret_scanning", "status":"${{steps.scan.outcome}}", "createdAt":"${{ github.event.pull_request.created_at }}", "repo":"${{ github.repository }}","pull_request":"https://github.com/${{ github.repository }}/pull/${{ github.event.pull_request.number }}","actor":"${{ github.event.pull_request.user.login }}"}'
72+
if [[ -z "$SIEM_WEBHOOK_URL" ]]; then
73+
exit 0
6774
fi
75+
76+
# On merge_group events there is no pull_request context, so PR-derived
77+
# fields are empty. Use current timestamp as next best effort.
78+
created_at="${PR_CREATED_AT:-$(date -u +%Y-%m-%dT%H:%M:%SZ)}"
79+
actor="${PR_ACTOR:-$EVENT_ACTOR}"
80+
if [[ -n "$PR_NUMBER" ]]; then
81+
pull_request="https://github.com/${REPO}/pull/${PR_NUMBER}"
82+
else
83+
pull_request=""
84+
fi
85+
86+
jq -n \
87+
--arg event "github_secret_scanning" \
88+
--arg status "$SCAN_OUTCOME" \
89+
--arg createdAt "$created_at" \
90+
--arg repo "$REPO" \
91+
--arg pull_request "$pull_request" \
92+
--arg actor "$actor" \
93+
'{event: $event, status: $status, createdAt: $createdAt, repo: $repo, pull_request: $pull_request, actor: $actor}' \
94+
| curl --fail --silent --show-error \
95+
-H "Content-Type: application/json" \
96+
--data @- \
97+
"$SIEM_WEBHOOK_URL" \
98+
|| echo "::warning::SIEM alert failed (non-blocking)"
6899
- name: Fail workflow if secret detected
69-
if: steps.scan.outcome != 'success'
100+
if: ${{ !cancelled() && steps.scan.outcome != 'success' }}
70101
run: exit 1

0 commit comments

Comments
 (0)