-
Notifications
You must be signed in to change notification settings - Fork 0
280 lines (269 loc) · 8.98 KB
/
devsecops.yaml
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
# DevSecOps Workflow Definition
name: DevSecOps Workflow
on:
push:
permissions:
contents: read
jobs:
# Secret scanning job to detect secrets in codebase
secret-scanning:
runs-on: ubuntu-latest
permissions:
contents: read
steps:
- name: Checkout code
uses: actions/checkout@v4 # Check out the repository content to the runner
with:
fetch-depth: 0
- name: Run Gitleaks Scan
# Running Gitleaks to scan the code for secrets
run: |
docker run --rm -v $(pwd):/code -u $(id -u):$(id -g) zricethezav/gitleaks:v8.18.4@sha256:75bdb2b2f4db213cde0b8295f13a88d6b333091bbfbf3012a4e083d00d31caba -s /code detect -v
# Software Composition Analysis (SCA) to find vulnerabilities in project dependencies
sca:
runs-on: ubuntu-latest
continue-on-error: true
permissions:
contents: read
actions: read
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Run Trivy vulnerability scanner in fs mode
# Running Trivy to scan the filesystem for vulnerabilities
uses: aquasecurity/trivy-action@master
with:
scan-type: "fs"
scan-ref: "."
exit-code: '1'
ignore-unfixed: true
severity: "CRITICAL,HIGH"
iac:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Python 3.8
uses: actions/setup-python@v5
with:
python-version: 3.8
- name: Test with Checkov
id: checkov
uses: bridgecrewio/checkov-action@master
with:
directory: .
quiet: true
# Static Application Security Testing (SAST) to identify security vulnerabilities in source code
sast:
runs-on: ubuntu-latest
permissions:
contents: read
actions: read
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Run Semgrep
# Running Semgrep for static code analysis to identify security issues
uses: docker://returntocorp/semgrep
with:
args: semgrep scan /github/workspace
# Docker image build job
build-image:
permissions:
packages: write
contents: read
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/')
steps:
- uses: actions/checkout@v4
- name: Set IMAGE_TAG if tagged
# Setting the image tag if the push is a tag push
run: |
export IMAGE_TAG="ghcr.io/${{ github.repository }}:${GITHUB_REF#refs/tags/}"
export IMAGE_TAG=$(echo $IMAGE_TAG | tr '[:upper:]' '[:lower:]')
echo "IMAGE_TAG=$IMAGE_TAG" >> $GITHUB_ENV
echo $IMAGE_TAG > image-tag.txt
if: startsWith(github.ref, 'refs/tags/')
- name: Set IMAGE_TAG if not tagged
run: |
branch=${GITHUB_REF##*/}
sha=${GITHUB_SHA::8}
ts=$(date +%s)
export IMAGE_TAG="ghcr.io/${{ github.repository }}:${branch}-${sha}-${ts}"
export IMAGE_TAG=$(echo $IMAGE_TAG | tr '[:upper:]' '[:lower:]')
echo "IMAGE_TAG=$IMAGE_TAG" >> $GITHUB_ENV
echo $IMAGE_TAG > image-tag.txt
if: github.ref == 'refs/heads/main'
- name: Build Docker image with Kaniko
# Building the Docker image using Kaniko
id: build_image
uses: docker://gcr.io/kaniko-project/executor:v1.23.2
with:
args: --destination=${{ env.IMAGE_TAG }} --context=/github/workspace --dockerfile=/github/workspace/Dockerfile --no-push --tarPath /github/workspace/image.tar
- name: Setup crane
uses: imjasonh/[email protected]
- name: Use crane to get the digest
run: crane digest --tarball=image.tar > digest.txt
- name: Upload artifact
# Uploading the built Docker image as an artifact
uses: actions/upload-artifact@v4
with:
name: docker-image
path: image.tar
- name: Upload digest
# Uploading the built Docker image digest as an artifact
uses: actions/upload-artifact@v4
with:
name: digest
path: digest.txt
- name: Upload image tag
uses: actions/upload-artifact@v4
with:
name: image-tag
path: image-tag.txt
# Image scanning job to detect vulnerabilities in the built Docker image
image-scanning:
needs: build-image
continue-on-error: true
permissions:
contents: read
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/')
steps:
- uses: actions/checkout@v4
- uses: actions/download-artifact@v4
with:
name: docker-image
path: .
- name: Run Trivy vulnerability scanner in tarball mode
# Running Trivy to scan the Docker image for vulnerabilities
uses: aquasecurity/trivy-action@master
with:
input: /github/workspace/image.tar
exit-code: '1'
ignore-unfixed: true
severity: "CRITICAL,HIGH"
# Publish job to push the Docker image to a registry
publish:
needs: [build-image, image-scanning, secret-scanning, sca, sast, iac]
runs-on: ubuntu-latest
permissions:
packages: write
contents: read
if: github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/')
steps:
- uses: actions/download-artifact@v4
with:
name: docker-image
path: .
- uses: actions/download-artifact@v4
with:
name: image-tag
path: .
- name: set IMAGE_TAG env
run: |
export IMAGE_TAG=$(cat image-tag.txt)
echo "IMAGE_TAG=$IMAGE_TAG" >> $GITHUB_ENV
- uses: imjasonh/[email protected]
- name: Push Docker image to GitHub image Registry
# Pushing the Docker image to GitHub Container Registry
run: crane push image.tar ${{ env.IMAGE_TAG }}
sbom:
runs-on: ubuntu-latest # Specify the runner
needs: [build-image]
container:
image: alpine:3.20@sha256:77726ef6b57ddf65bb551896826ec38bc3e53f75cdde31354fbffb4f25238ebd
steps:
- name: Install curl
run: apk add --update curl
- name: Install Syft
run: curl -sSfL https://raw.githubusercontent.com/anchore/syft/main/install.sh | sh -s -- -b /usr/local/bin
- name: Download artifact from previous build
uses: actions/download-artifact@v4
with:
name: docker-image
- name: Generate SBOM with Syft
run: syft ./image.tar -o cyclonedx-json=sbom.json
- name: Upload SBOM as an artifact
uses: actions/upload-artifact@v4
with:
name: sbom
path: sbom.json
sign-image:
permissions:
packages: write
contents: read
runs-on: ubuntu-latest
needs: publish
if: github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/')
steps:
- name: cosign-installer
uses: sigstore/[email protected]
with:
cosign-release: 'v2.2.3' # optional
- name: Download digest
uses: actions/download-artifact@v4
with:
name: digest
path: .
- name: Download image tag
uses: actions/download-artifact@v4
with:
name: image-tag
path: .
- name: Read digest and image tag from file
run: |
export IMAGE_DIGEST=$(cat digest.txt)
echo "IMAGE_DIGEST=$IMAGE_DIGEST" >> $GITHUB_ENV
export IMAGE_TAG=$(cat image-tag.txt)
echo "IMAGE_TAG=$IMAGE_TAG" >> $GITHUB_ENV
- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Write signing key to disk (only needed for `cosign sign --key`)
run: echo "${{ secrets.COSIGN_PRIVATE_KEY }}" > cosign.key
- name: Sign the published Docker image
env:
COSIGN_PASSWORD: ${{ secrets.COSIGN_PASSWORD }}
run: cosign sign --yes --key cosign.key "$(cat image-tag.txt)@${{ env.IMAGE_DIGEST }}"
sign-sbom:
runs-on: ubuntu-latest
permissions:
packages: write
contents: read
needs: publish
if: github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/') || github.ref == 'refs/heads/devsecops-ci'
steps:
- name: cosign-installer
uses: sigstore/[email protected]
with:
cosign-release: 'v2.2.3' # optional
- name: Download digest
uses: actions/download-artifact@v4
with:
name: digest
path: .
- name: Download image tag
uses: actions/download-artifact@v4
with:
name: image-tag
path: .
- name: Download sbom tag
uses: actions/download-artifact@v4
with:
name: sbom
path: .
- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Write signing key to disk (only needed for `cosign sign --key`)
run: echo "${{ secrets.COSIGN_PRIVATE_KEY }}" > cosign.key
- name: Sign SBOM
run: cosign attest --yes --predicate ./sbom.json --key cosign.key "$(cat image-tag.txt)@$(cat digest.txt)"
env:
COSIGN_PASSWORD: ${{ secrets.COSIGN_PASSWORD }}