-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgitlab-ci-cd-pipeline-example.yml
More file actions
313 lines (295 loc) · 9.15 KB
/
gitlab-ci-cd-pipeline-example.yml
File metadata and controls
313 lines (295 loc) · 9.15 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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
variables:
GIT_SUBMODULE_STRATEGY: recursive
PUSH_IMAGE: "true"
BUILD_ARGS: "kafka sqlite"
SCAN_IMAGE: "false"
EXTRA_TAGS: "${CI_COMMIT_TAG}"
CONTEXT_DIR_ETL: "k8s-infra/Docker_image_files/etl"
REGISTRY_IMAGE_PATH_ETL: "${CI_REGISTRY}/wp3/t3.5/self-security/etl"
CONTEXT_DIR_SURICATA: "k8s-infra/Docker_image_files/suricata"
REGISTRY_IMAGE_PATH_SURICATA: "${CI_REGISTRY}/wp3/t3.5/self-security/self-security"
DOCKER_FILE_NAME: "Dockerfile"
DOCKER_HOST: "tcp://docker:2375"
DOCKER_TLS_CERTDIR: ""
stages:
- secret_scanning
- sast
- sca - build_image - scan_image - deploy - dast
gitleaks:
stage: secret_scanning
image:
name: zricethezav/gitleaks
entrypoint: [""]
script:
- gitleaks detect --verbose --source . -f json -r detect_gitleaks.json
allow_failure: true
artifacts:
when: always
paths:
- detect_gitleaks.json
rules:
- if: '$CI_COMMIT_BRANCH == "develop"'
gitleaks_protect:
stage: secret_scanning
image:
name: zricethezav/gitleaks
entrypoint: [""]
script:
- gitleaks protect --verbose --source . -f json -r protect_gitleaks.json
allow_failure: true
artifacts:
when: always
paths:
- protect_gitleaks.json
dependencies:
- gitleaks
rules:
- if: '$CI_COMMIT_BRANCH == "develop"'
njsscan:
stage: sast
needs: ["gitleaks_protect"]
image: python
before_script:
- pip3 install --upgrade njsscan
script:
- njsscan --exit-warning . --sarif -o njsscan.sarif
allow_failure: true
artifacts:
when: always
paths:
- njsscan.sarif
rules:
- if: '$CI_COMMIT_BRANCH == "develop"'
semgrep:
stage: sast
needs: ["gitleaks_protect"]
image: returntocorp/semgrep
variables:
SEMGREP_RULES: p/javascript
script:
- semgrep ci --json --output semgrep.json
allow_failure: true
artifacts:
when: always
paths:
- semgrep.json
rules:
- if: '$CI_COMMIT_BRANCH == "develop"'
sonarqube-check:
stage: sast
needs: ["gitleaks_protect"]
image:
name: sonarsource/sonar-scanner-cli:5.0
entrypoint: [""]
variables:
SONAR_USER_HOME: "${CI_PROJECT_DIR}/.sonar"
GIT_DEPTH: "0"
cache:
key: "${CI_JOB_NAME}"
paths:
- .sonar/cache
script:
- sonar-scanner
allow_failure: true
rules:
- if: '$CI_COMMIT_BRANCH == "develop"'
sonarqube-vulnerability-report:
stage: sast
script:
- apt-get update && apt-get install -y curl- 'curl -u "${SONAR_TOKEN}:" "${SONAR_HOST_URL}/api/issues/gitlab_sast_export?projectKey=selft-security&branch=${CI_COMMIT_BRANCH}&pullRequest=${CI_MERGE_REQUEST_IID}" -o gl-sast-sonar-report.json'
allow_failure: true
rules:
- if: '$CI_COMMIT_BRANCH == "develop"'
artifacts:
expire_in: 1 day
reports:
sast: gl-sast-sonar-report.json
dependencies:
- sonarqube-check
retire:
stage: sca
needs: ["gitleaks_protect"]
script:
- echo "SCA ..."
build_image_self-security:
stage: build_image
needs: ["njsscan","semgrep","sonarqube-vulnerability-report","retire"]
image:
name: gcr.io/kaniko-project/executor:v1.14.0-debug
entrypoint: [""]
script:
- |
if [[ -z "${REGISTRY_USER}" || -z "${REGISTRY_PASSWORD}" ]]; then
REGISTRY_USER=${CI_REGISTRY_USER}
REGISTRY_PASSWORD=${CI_REGISTRY_PASSWORD
unset CI_REGISTRY_USER; unset CI_REGISTRY_PASSWORD;
fi
- |
if [ -z "${REGISTRY_IMAGE_PATH_SURICATA}" ]; then
echo "ERROR: CI variable REGISTRY_IMAGE_PATH_SURICATA is mandatory."
exit 1
fi
- REGISTRY=$(echo ${REGISTRY_IMAGE_PATH_SURICATA} | cut -d / -f 1)
- |
KANIKO_CONTEXT_DIR_SURICATA=${CI_PROJECT_DIR}/${CONTEXT_DIR_SURICATA}
- mkdir -p /kaniko/.docker
- |
echo "{\"auths\":{\"${REGISTRY}\":{\"auth\":\"$(printf "%s:%s" "${REGISTRY_USER}" "${REGISTRY_PASSWORD}" | base64 -w0)\"}}}" > /kaniko/.docker/config.json
- |
if [ "$(echo ${PUSH_IMAGE} | tr '[:upper:]' '[:lower:]')" = "true" ]; then
PUSH_IMAGE="" else echo "Info: defer pushing image to remote as PUSH_IMAGE is false" PUSH_IMAGE="--no-push" fi
- |
if [ -n "$EXTRA_TAGS" ]; then
IMAGE_WITHOUT_TAG=$(echo ${REGISTRY_IMAGE_PATH_SURICATA} | cut -d : -f 1)
for tag in $EXTRA_TAGS; do
KANIKO_EXTRA_TAGS="${KANIKO_EXTRA_TAGS} --destination ${IMAGE_WITHOUT_TAG}:${tag}"
done
fi
- /kaniko/executor
--context "${KANIKO_CONTEXT_DIR_SURICATA}"
--dockerfile "${KANIKO_CONTEXT_DIR_SURICATA}/${DOCKER_FILE_NAME}"
--build-arg optional_dependencies="${BUILD_ARGS}"
--destination "${REGISTRY_IMAGE_PATH_SURICATA}" ${KANIKO_EXTRA_TAGS} ${PUSH_IMAGE}
rules:
- if: '$CI_COMMIT_BRANCH == "develop"'
build_image_etl:
stage: build_image
needs: ["njsscan","semgrep","sonarqube-vulnerability-report","retire"]
image:
name: gcr.io/kaniko-project/executor:v1.14.0-debug
entrypoint: [""]
script:
- |
if [[ -z "${REGISTRY_USER}" || -z "${REGISTRY_PASSWORD}" ]]; then
REGISTRY_USER=${CI_REGISTRY_USER}
REGISTRY_PASSWORD=${CI_REGISTRY_PASSWORD}
unset CI_REGISTRY_USER; unset CI_REGISTRY_PASSWORD;
fi
- |
if [ -z "${REGISTRY_IMAGE_PATH_ETL}" ]; then
echo "ERROR: CI variable REGISTRY_IMAGE_PATH_ETL is mandatory."
exit 1
fi
- REGISTRY=$(echo ${REGISTRY_IMAGE_PATH_ETL} | cut -d / -f 1)
- |
KANIKO_CONTEXT_DIR_ETL=${CI_PROJECT_DIR}/${CONTEXT_DIR_ETL}
- mkdir -p /kaniko/.docker
- |
echo "{\"auths\":{\"${REGISTRY}\":{\"auth\":\"$(printf "%s:%s" "${REGISTRY_USER}" "${REGISTRY_PASSWORD}" | base64 -w0)\"}}}" > /kaniko/.docker/config.json
- |
if [ "$(echo ${PUSH_IMAGE} | tr '[:upper:]' '[:lower:]')" = "true" ]; then
PUSH_IMAGE=""
else
echo "Info: defer pushing image to remote as PUSH_IMAGE is false"
PUSH_IMAGE="--no-push"
fi
- |
if [ -n "$EXTRA_TAGS" ]; then
IMAGE_WITHOUT_TAG=$(echo ${REGISTRY_IMAGE_PATH_ETL} | cut -d : -f 1)
for tag in $EXTRA_TAGS; do
KANIKO_EXTRA_TAGS="${KANIKO_EXTRA_TAGS} --destination ${IMAGE_WITHOUT_TAG}:${tag}"
done
fi
- /kaniko/executor
--context "${KANIKO_CONTEXT_DIR_ETL}"
--dockerfile "${KANIKO_CONTEXT_DIR_ETL}/${DOCKER_FILE_NAME}"
--build-arg optional_dependencies="${BUILD_ARGS}"
--destination "${REGISTRY_IMAGE_PATH_ETL}" ${KANIKO_EXTRA_TAGS} ${PUSH_IMAGE}
rules:
- if: '$CI_COMMIT_BRANCH == "develop"'
scan_image_self-security:
stage: scan_image
needs: ["build_image_self-security"]
image: docker:24
services:
- name: docker:24-dind
alias: docker
before_script:
- apk --no-cache add curl python3 py3-pip
- curl -sfL https://raw.githubusercontent.com/aquasecurity/trivy/main/contrib/install.sh | sh -s -- -b /usr/local/bin
- echo $CI_REGISTRY_PASSWORD | docker login -u $CI_REGISTRY_USER --password-stdin $CI_REGISTRY
script:
- docker pull "${REGISTRY_IMAGE_PATH_SURICATA}:latest"
- trivy image --exit-code 1 "${REGISTRY_IMAGE_PATH_SURICATA}:latest"
rules:
- if: '$CI_COMMIT_BRANCH == "develop"'
scan_image_etl:
stage: scan_image
needs: ["build_image_etl"]
image: docker:24
services:
- name: docker:24-dind
alias: docker
before_script:
- apk --no-cache add curl python3 py3-pip
- curl -sfL https://raw.githubusercontent.com/aquasecurity/trivy/main/contrib/install.sh | sh -s -- -b /usr/local/bin
- echo $CI_REGISTRY_PASSWORD | docker login -u $CI_REGISTRY_USER --password-stdin $CI_REGISTRY
script:
- docker pull "${REGISTRY_IMAGE_PATH_ETL}:latest"
- trivy image --exit-code 1 "${REGISTRY_IMAGE_PATH_ETL}:latest"
rules:
- if: '$CI_COMMIT_BRANCH == "develop"'
deploy_self-security:
stage: deploy
needs: ["build_image_self-security", "scan_image_self-security"]
script:
- echo "Deploy self-Security ..."
rules:
- if: '$CI_COMMIT_BRANCH == "develop"'
deploy_etl:
stage: deploy
needs: ["build_image_etl", "scan_image_etl"]
script:
- echo "Deploy ETL ..."
rules:
- if: '$CI_COMMIT_BRANCH == "develop"'
zap_baseline_etl:
stage: dast
needs: ["deploy_etl"]
image: ghcr.io/zaproxy/zaproxy:stable
script:
- echo "zap"
rules:
- if: '$CI_COMMIT_BRANCH == "develop"'
zap_full_etl:
stage: dast
needs: ["deploy_etl"]
image: ghcr.io/zaproxy/zaproxy:stable
script:
- echo "zap"
rules:
- if: '$CI_COMMIT_BRANCH == "develop"'
zap_baseline_self-security:
stage: dast
needs: ["deploy_self-security"]
image: ghcr.io/zaproxy/zaproxy:stable
variables:
ZAP_TARGET: "http://URL:PORT/"
before_script:
- mkdir -p /zap/wrk
script:
- zap-baseline.py -t $ZAP_TARGET -g gen.conf -I -x baseline.xml
- cp /zap/wrk/baseline.xml baseline.xml
artifacts:
when: always
paths:
- baseline.xml
rules:
- if: '$CI_COMMIT_BRANCH == "develop"'
zap_full_self-security:
stage: dast
needs: ["deploy_self-security"]
image: ghcr.io/zaproxy/zaproxy:stable
variables:
ZAP_TARGET: "http://URL:PORT/"
before_script:
- mkdir -p /zap/wrk
script:
- zap-full-scan.py -t $ZAP_TARGET -g gen.conf -I -x full-zap.xml
- cp /zap/wrk/full-zap.xml full-zap.xml
artifacts:
when: always
paths:
- full-zap.xml
rules:
- if: '$CI_COMMIT_BRANCH == "develop"'