-
Notifications
You must be signed in to change notification settings - Fork 0
282 lines (233 loc) · 8.27 KB
/
build-and-attest.yml
File metadata and controls
282 lines (233 loc) · 8.27 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
name: Build and Attest Container
on:
push:
tags:
- 'v*'
workflow_dispatch:
inputs:
version:
description: 'Version to build (e.g., 0.1.0)'
required: true
type: string
permissions:
contents: write
packages: write
id-token: write
attestations: write
actions: read
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
VERSION: ${{ github.event.inputs.version || github.ref_name }}
jobs:
build-and-scan:
runs-on: ubuntu-latest
outputs:
digest: ${{ steps.push.outputs.digest }}
image: ${{ steps.meta.outputs.tags }}
steps:
- uses: actions/checkout@v6
- name: Set up Python
uses: actions/setup-python@v6
with:
python-version: '3.11'
- name: Embed version
run: |
VERSION="${{ env.VERSION }}"
VERSION="${VERSION#v}"
echo "__version__ = \"${VERSION}\"" > _version.py
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to Container Registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata
id: meta
uses: docker/metadata-action@v6
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=sha,prefix=
type=raw,value=latest,enable={{is_default_branch}}
- name: Build container image (local)
id: build
uses: docker/build-push-action@v6
with:
context: .
load: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
- name: Scan for critical vulnerabilities
run: |
# Install grype
curl -sSfL https://raw.githubusercontent.com/anchore/grype/main/install.sh | sh -s -- -b /usr/local/bin
# Get the first tag for scanning
IMAGE_TAG=$(echo "${{ steps.meta.outputs.tags }}" | head -n1)
echo "=== Scanning image for vulnerabilities ==="
grype "$IMAGE_TAG" --output table
echo "=== Checking for critical vulnerabilities ==="
if ! grype "$IMAGE_TAG" --fail-on critical; then
echo "::error::Critical vulnerabilities detected! Image will NOT be pushed."
exit 1
fi
echo "No critical vulnerabilities found. Proceeding with push."
- name: Push container image
id: push
uses: docker/build-push-action@v6
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
provenance: true
sbom: true
- name: Output image digest
run: |
echo "Image digest: ${{ steps.push.outputs.digest }}"
echo "${{ steps.push.outputs.digest }}" > image-digest.txt
- name: Upload digest artifact
uses: actions/upload-artifact@v6
with:
name: image-digest
path: image-digest.txt
retention-days: 90
sign-and-attest:
needs: build-and-scan
runs-on: ubuntu-latest
permissions:
packages: write
id-token: write
attestations: write
steps:
- uses: actions/checkout@v6
- name: Log in to Container Registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Install cosign
uses: sigstore/cosign-installer@v3
- name: Install syft for SBOM generation
uses: anchore/sbom-action/download-syft@v0
- name: Sign container image with Sigstore
env:
DIGEST: ${{ needs.build-and-scan.outputs.digest }}
run: |
IMAGE="${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}@${DIGEST}"
echo "Signing image: $IMAGE"
cosign sign --yes "$IMAGE"
echo "Image signed successfully"
- name: Generate SBOM for container image
env:
DIGEST: ${{ needs.build-and-scan.outputs.digest }}
run: |
IMAGE="${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}@${DIGEST}"
# Generate SBOM in CycloneDX format
syft "$IMAGE" -o cyclonedx-json > sbom.cdx.json
echo "SBOM generated: sbom.cdx.json"
- name: Attest SBOM to container image
env:
DIGEST: ${{ needs.build-and-scan.outputs.digest }}
run: |
IMAGE="${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}@${DIGEST}"
cosign attest --yes \
--predicate sbom.cdx.json \
--type cyclonedx \
"$IMAGE"
echo "SBOM attestation attached to image"
- name: Upload SBOM artifact
uses: actions/upload-artifact@v6
with:
name: container-sbom
path: sbom.cdx.json
retention-days: 90
# SLSA Level 3 provenance generation using official SLSA generator
# This runs in an isolated, hardened environment for stronger security guarantees
provenance:
needs: build-and-scan
permissions:
actions: read
id-token: write
packages: write
uses: slsa-framework/slsa-github-generator/.github/workflows/generator_container_slsa3.yml@v2.1.0
with:
image: ghcr.io/${{ github.repository }}
digest: ${{ needs.build-and-scan.outputs.digest }}
registry-username: ${{ github.actor }}
secrets:
registry-password: ${{ secrets.GITHUB_TOKEN }}
release:
needs: [build-and-scan, sign-and-attest, provenance]
runs-on: ubuntu-latest
if: startsWith(github.ref, 'refs/tags/')
permissions:
contents: write
steps:
- uses: actions/checkout@v6
- name: Download artifacts
uses: actions/download-artifact@v6
with:
name: image-digest
path: .
- name: Download SBOM
uses: actions/download-artifact@v6
with:
name: container-sbom
path: .
- name: Create GitHub Release
uses: softprops/action-gh-release@v2
with:
files: |
image-digest.txt
sbom.cdx.json
deploy/deploy.sh
deploy/verify.sh
generate_release_notes: true
body: |
## Container Image
```
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}@${{ needs.build-and-scan.outputs.digest }}
```
## Quick Deploy
```bash
# Download deploy script
curl -fsSL -o deploy.sh https://github.com/${{ github.repository }}/releases/latest/download/deploy.sh
chmod +x deploy.sh
# Deploy (requires Docker or Podman)
sudo ./deploy.sh ${{ github.ref_name }}
```
## Verification
### Verify image signature
```bash
cosign verify ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}@${{ needs.build-and-scan.outputs.digest }} \
--certificate-identity-regexp ".*" \
--certificate-oidc-issuer "https://token.actions.githubusercontent.com"
```
### Verify SBOM attestation
```bash
cosign verify-attestation ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}@${{ needs.build-and-scan.outputs.digest }} \
--type cyclonedx \
--certificate-identity-regexp ".*" \
--certificate-oidc-issuer "https://token.actions.githubusercontent.com"
```
### Verify SLSA provenance (Level 3)
```bash
slsa-verifier verify-image ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}@${{ needs.build-and-scan.outputs.digest }} \
--source-uri github.com/${{ github.repository }}
```
### Verify runtime integrity
After deployment, the `image_digest` from the API should match:
```bash
curl -s http://localhost:8000/ping | jq -r '.image_digest'
# Should return: ${{ needs.build-and-scan.outputs.digest }}
```