-
Notifications
You must be signed in to change notification settings - Fork 0
183 lines (169 loc) · 6.52 KB
/
sbom-generate.yml
File metadata and controls
183 lines (169 loc) · 6.52 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
name: 'Generate SBOM (Software Bill of Materials)'
on:
workflow_call:
inputs:
target-type:
description: 'Target type (image, directory, file)'
required: false
type: string
default: 'directory'
target:
description: 'Target to scan (image ref, directory path, or file path)'
required: false
type: string
default: '.'
format:
description: 'SBOM format (spdx-json, cyclonedx-json, syft-json)'
required: false
type: string
default: 'spdx-json'
output-file:
description: 'Output file name'
required: false
type: string
default: 'sbom.spdx.json'
upload-artifact:
description: 'Upload SBOM as workflow artifact'
required: false
type: boolean
default: true
upload-dependency-snapshot:
description: 'Upload to GitHub Dependency Graph (spdx-json only)'
required: false
type: boolean
default: true
scan-sbom:
description: 'Scan SBOM for vulnerabilities with Trivy'
required: false
type: boolean
default: true
runs-on:
description: 'Runner label to execute the job on'
required: false
type: string
default: 'homelab-runners'
secrets:
registry-username:
description: 'Registry username (for private images)'
required: false
registry-password:
description: 'Registry password/token (for private images)'
required: false
outputs:
sbom-path:
description: 'Path to generated SBOM file'
value: ${{ jobs.generate.outputs.sbom-path }}
vulnerability-count:
description: 'Number of vulnerabilities found in SBOM'
value: ${{ jobs.generate.outputs.vulnerabilities }}
permissions:
contents: write
packages: read
security-events: write
jobs:
generate:
name: Generate SBOM
runs-on: ${{ inputs.runs-on }}
timeout-minutes: 15
outputs:
sbom-path: ${{ inputs.output-file }}
vulnerabilities: ${{ steps.scan.outputs.findings }}
steps:
- name: Checkout repository
if: inputs.target-type == 'directory' || inputs.target-type == 'file'
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
- name: Log in to container registry
if: inputs.target-type == 'image' && secrets.registry-username != ''
uses: docker/login-action@9780b0c442fbb1117ed29e0efdff1e18412f7567 # v3.3.0
with:
registry: ${{ contains(inputs.target, 'ghcr.io') && 'ghcr.io' || 'docker.io' }}
username: ${{ secrets.registry-username }}
password: ${{ secrets.registry-password }}
- name: Generate SBOM
id: sbom
uses: anchore/sbom-action@7ccf588e3cf3cc2611714c2eeae48550fbc17552 # v0.17.10
with:
path: ${{ inputs.target-type == 'directory' && inputs.target || '' }}
file: ${{ inputs.target-type == 'file' && inputs.target || '' }}
image: ${{ inputs.target-type == 'image' && inputs.target || '' }}
format: ${{ inputs.format }}
output-file: ${{ inputs.output-file }}
upload-artifact: ${{ inputs.upload-artifact }}
upload-release-assets: false
artifact-name: sbom-${{ inputs.format }}
dependency-snapshot: ${{ inputs.upload-dependency-snapshot && inputs.format == 'spdx-json' }}
- name: Scan SBOM for vulnerabilities
if: inputs.scan-sbom
id: scan
uses: aquasecurity/trivy-action@b6643a29fecd7f34b3597bc6acb0a98b03d33ff8 # v0.33.1
with:
scan-type: 'sbom'
scan-ref: ${{ inputs.output-file }}
format: 'sarif'
output: 'trivy-sbom-results.sarif'
severity: 'HIGH,CRITICAL'
timeout: '10m'
- name: Upload SBOM scan results to GitHub Security
if: inputs.scan-sbom
uses: github/codeql-action/upload-sarif@48ab28a6f5dbc2a99bf1e0131198dd8f1df78169 # v3.28.0
with:
sarif_file: 'trivy-sbom-results.sarif'
category: 'sbom-scan'
- name: Count vulnerabilities
if: inputs.scan-sbom
id: count
shell: bash
run: |
if [ -f "trivy-sbom-results.sarif" ]; then
FINDINGS=$(jq '.runs[0].results | length' trivy-sbom-results.sarif)
echo "findings=$FINDINGS" >> "$GITHUB_OUTPUT"
echo "Found $FINDINGS vulnerabilities in SBOM"
else
echo "findings=0" >> "$GITHUB_OUTPUT"
fi
- name: Generate SBOM summary
shell: bash
run: |
{
echo "## 📋 SBOM Generation Summary"
echo ""
echo "**Target Type:** \`${{ inputs.target-type }}\`"
echo "**Target:** \`${{ inputs.target }}\`"
echo "**Format:** \`${{ inputs.format }}\`"
echo "**Output File:** \`${{ inputs.output-file }}\`"
echo ""
} >> "$GITHUB_STEP_SUMMARY"
if [ -f "${{ inputs.output-file }}" ]; then
echo "✅ **SBOM generated successfully**" >> "$GITHUB_STEP_SUMMARY"
# Count components in SBOM
if [ "${{ inputs.format }}" = "spdx-json" ]; then
COMPONENTS=$(jq '.packages | length' "${{ inputs.output-file }}")
{
echo ""
echo "📦 **Total Components:** $COMPONENTS"
} >> "$GITHUB_STEP_SUMMARY"
elif [ "${{ inputs.format }}" = "cyclonedx-json" ]; then
COMPONENTS=$(jq '.components | length' "${{ inputs.output-file }}")
{
echo ""
echo "📦 **Total Components:** $COMPONENTS"
} >> "$GITHUB_STEP_SUMMARY"
fi
if [ "${{ inputs.scan-sbom }}" = "true" ] && [ -f "trivy-sbom-results.sarif" ]; then
VULNS=$(jq '.runs[0].results | length' trivy-sbom-results.sarif)
echo "" >> "$GITHUB_STEP_SUMMARY"
if [ "$VULNS" -eq 0 ]; then
echo "✅ **No vulnerabilities found**" >> "$GITHUB_STEP_SUMMARY"
else
echo "⚠️ **Found $VULNS vulnerabilities**" >> "$GITHUB_STEP_SUMMARY"
fi
fi
if [ "${{ inputs.upload-dependency-snapshot }}" = "true" ]; then
{
echo ""
echo "✅ **Uploaded to GitHub Dependency Graph**"
} >> "$GITHUB_STEP_SUMMARY"
fi
else
echo "❌ **SBOM generation failed**" >> "$GITHUB_STEP_SUMMARY"
fi