forked from platform9/cluster-api-provider-bringyourownhost
-
Notifications
You must be signed in to change notification settings - Fork 0
195 lines (172 loc) · 7.37 KB
/
Copy pathsecurity-scan.yml
File metadata and controls
195 lines (172 loc) · 7.37 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
name: Go Security scan
on:
push:
branches:
- main
pull_request:
jobs:
setup:
name: Shared Setup
runs-on: ubuntu-latest
outputs:
go-version: '1.22'
steps:
- name: Checkout Code
uses: actions/checkout@v3
- name: Export Go Version
run: echo "go-version=1.22" >> $GITHUB_OUTPUT
gosec_scan:
name: Gosec Security Scan (Full)
needs: setup
runs-on: ubuntu-latest
outputs:
gosec_high_found: ${{ steps.scan.outputs.gosec_high_found }}
permissions:
contents: write
pull-requests: write
steps:
- name: Checkout Code
uses: actions/checkout@v3
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '${{ needs.setup.outputs.go-version }}'
- name: Install gosec
run: |
go install github.com/securego/gosec/v2/cmd/gosec@latest
echo "$(go env GOPATH)/bin" >> $GITHUB_PATH
- name: Sanitize branch name
run: echo "SAFE_REF_NAME=${GITHUB_REF_NAME//\//-}" >> $GITHUB_ENV
- name: Run Gosec Scan
id: scan
run: |
echo "Running Gosec scan..."
mkdir -p tmp
gosec -fmt=json -severity=medium -out=tmp/gosec-report.json ./... || true
cat tmp/gosec-report.json || echo '{"Issues":[]}'
count=$(jq '[.Issues[] | select(.severity == "HIGH" or .severity == "CRITICAL")] | length' tmp/gosec-report.json || echo 0)
if [[ "$count" -gt 0 ]]; then
echo "gosec_high_found=true" >> "$GITHUB_OUTPUT"
else
echo "gosec_high_found=false" >> "$GITHUB_OUTPUT"
fi
- name: Upload Gosec Report
uses: actions/upload-artifact@v4
with:
name: gosec-json-${{ env.SAFE_REF_NAME }}
path: tmp/gosec-report.json
- name: Generate PR Body (if vulnerabilities found)
if: ${{ steps.scan.outputs.gosec_high_found == 'true' }}
run: |
echo "# 🚨 Gosec Vulnerability Report for branch \`${GITHUB_REF_NAME}\`" > tmp/pr-body.md
jq -r '
.Issues[]
| select(.severity == "HIGH" or .severity == "CRITICAL")
| "* File: \(.file)\n • Line: \(.line)\n • Rule ID: \(.rule_id)\n • Details: \(.details)\n • Confidence: \(.confidence)\n • Severity: \(.severity)\n"
' tmp/gosec-report.json >> tmp/pr-body.md
- name: Create Pull Request (if vulnerabilities found)
if: ${{ github.event_name == 'push' && steps.scan.outputs.gosec_high_found == 'true' }}
uses: peter-evans/create-pull-request@v5
with:
commit-message: 'chore: vulnerabilities detected by Gosec (HIGH/CRITICAL)'
title: 'Gosec Vulnerability Report for branch ${{ github.ref_name }}'
body-path: tmp/pr-body.md
branch: auto/gosec-scan/${{ env.SAFE_REF_NAME }}
base: ${{ github.ref_name }}
delete-branch: true
- name: Close Stale Vulnerability PR (if clean)
if: ${{ github.event_name == 'push' && steps.scan.outputs.gosec_high_found == 'false' }}
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
BRANCH="auto/gosec-scan/${{ env.SAFE_REF_NAME }}"
PR_NUMBER=$(gh pr list --repo "${{ github.repository }}" --head "$BRANCH" --state open --json number --jq '.[0].number // empty')
if [[ -n "$PR_NUMBER" ]]; then
gh pr close "$PR_NUMBER" --repo "${{ github.repository }}" --comment "No HIGH/CRITICAL vulnerabilities found in latest scan on \`${{ github.ref_name }}\`. Closing report."
fi
- name: Fail Job If Vulnerabilities Found
if: ${{ steps.scan.outputs.gosec_high_found == 'true' }}
run: exit 1
trivy_scan:
name: Trivy Security Scan (Full)
needs: setup
runs-on: ubuntu-latest
outputs:
trivy_high_found: ${{ steps.scan.outputs.trivy_high_found }}
permissions:
contents: write
pull-requests: write
steps:
- name: Checkout Code
uses: actions/checkout@v3
- name: Install Trivy
env:
TRIVY_VERSION: "0.71.0"
run: |
sudo apt update
sudo apt install -y jq
curl -sSL "https://github.com/aquasecurity/trivy/releases/download/v${TRIVY_VERSION}/trivy_${TRIVY_VERSION}_Linux-64bit.deb" -o trivy.deb
sudo dpkg -i trivy.deb
rm trivy.deb
- name: Sanitize branch name
run: echo "SAFE_REF_NAME=${GITHUB_REF_NAME//\//-}" >> $GITHUB_ENV
- name: Run Trivy Filesystem Scan
id: scan
run: |
echo "Running Trivy scan (HIGH/CRITICAL)..."
mkdir -p tmp
trivy fs --format json --severity HIGH,CRITICAL --output tmp/trivy-report.json .
[[ -f tmp/trivy-report.json ]] || echo '{"Results":[]}' > tmp/trivy-report.json
count=$(jq -e '
(.Results // [])
| map(.Vulnerabilities? // [])
| add
| map(select(.Severity=="HIGH" or .Severity=="CRITICAL"))
| length
' tmp/trivy-report.json || echo 0)
if [[ "$count" -gt 0 ]]; then
echo "trivy_high_found=true" >> "$GITHUB_OUTPUT"
else
echo "trivy_high_found=false" >> "$GITHUB_OUTPUT"
fi
- name: Upload Trivy Report
uses: actions/upload-artifact@v4
with:
name: trivy-json-${{ env.SAFE_REF_NAME }}
path: tmp/trivy-report.json
- name: Generate PR Body (if vulnerabilities found)
if: ${{ steps.scan.outputs.trivy_high_found == 'true' }}
run: |
echo "# 🛡️ Trivy Scan Report for branch \`${GITHUB_REF_NAME}\`" > tmp/pr-body.md
jq -r '
(.Results // [])
| .[]
| .Target as $file
| (.Vulnerabilities? // [])
| map(select(.Severity=="HIGH" or .Severity=="CRITICAL"))
| .[]
| "* File: \($file)\n • Vulnerability ID: \(.VulnerabilityID)\n • Pkg: \(.PkgName) \(.InstalledVersion)\n • Severity: \(.Severity)\n • Title: \(.Title)\n"
' tmp/trivy-report.json >> tmp/pr-body.md
- name: Create Pull Request (if vulnerabilities found)
if: ${{ github.event_name == 'push' && steps.scan.outputs.trivy_high_found == 'true' }}
uses: peter-evans/create-pull-request@v5
with:
commit-message: 'chore: vulnerabilities detected by Trivy (HIGH/CRITICAL)'
title: 'Trivy Vulnerability Report for branch ${{ github.ref_name }}'
body-path: tmp/pr-body.md
branch: auto/trivy-scan/${{ env.SAFE_REF_NAME }}
base: ${{ github.ref_name }}
delete-branch: true
- name: Close Stale Vulnerability PR (if clean)
if: ${{ github.event_name == 'push' && steps.scan.outputs.trivy_high_found == 'false' }}
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
BRANCH="auto/trivy-scan/${{ env.SAFE_REF_NAME }}"
PR_NUMBER=$(gh pr list --repo "${{ github.repository }}" --head "$BRANCH" --state open --json number --jq '.[0].number // empty')
if [[ -n "$PR_NUMBER" ]]; then
gh pr close "$PR_NUMBER" --repo "${{ github.repository }}" --comment "No HIGH/CRITICAL vulnerabilities found in latest scan on \`${{ github.ref_name }}\`. Closing report."
fi
- name: Fail Job If Vulnerabilities Found
if: ${{ steps.scan.outputs.trivy_high_found == 'true' }}
run: exit 1