-
Notifications
You must be signed in to change notification settings - Fork 113
/
.gitlab-ci.yml
748 lines (712 loc) · 24.7 KB
/
.gitlab-ci.yml
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
# Jobs defined here use the idp/ci docker image from ECR by default. To find
# other available images:
# aws ecr describe-repositories | jq '.repositories[].repositoryUri'
# Images are built via the identity-devops GitLab pipeline.
variables:
GITLAB_CI: 'true'
FF_SCRIPT_SECTIONS: 'true'
JUNIT_OUTPUT: 'true'
ECR_REGISTRY: '${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com'
IDP_CI_SHA: 'sha256:5c4953f8efba18b7a6d6a9a961cb77ba7143059cbb2176499432b4275fbe67db'
PKI_IMAGE_TAG: 'main'
DASHBOARD_IMAGE_TAG: 'main'
APPLICATION_MANIFEST: dockerfiles/application.yaml
default:
image: '${ECR_REGISTRY}/idp/ci@${IDP_CI_SHA}'
.bundle_install: &bundle_install
- bundle check || bundle install --deployment --jobs=4 --retry=3 --without deploy development doc production --path vendor/ruby
.yarn_install: &yarn_install
- yarn install --frozen-lockfile --ignore-engines --cache-folder .yarn-cache
.yarn_production_install: &yarn_production_install
- yarn install --production --frozen-lockfile --ignore-engines --cache-folder .yarn-cache
.build_cache:
- &ruby_cache
key:
files:
- Gemfile.lock
paths:
- vendor/ruby
policy: pull
- &yarn_cache
key:
files:
- yarn.lock
paths:
- .yarn-cache/
policy: pull
- &yarn_production_cache
key:
files:
- yarn.lock
paths:
- .yarn-cache/
policy: pull
- &assets_cache
key: $CI_COMMIT_SHA
paths:
- tmp/cache/assets
- public/assets
- public/packs
policy: pull
stages:
- build
- test
- after_test
- review
- scan
- deploy_eks
workflow:
rules:
- if: '$CI_PIPELINE_SOURCE == "merge_request_event" || $CI_PIPELINE_SOURCE == "external_pull_request_event" || $CI_PIPELINE_SOURCE == "schedule"'
- if: '$CI_PIPELINE_SOURCE == "merge_request_event" || $CI_PIPELINE_SOURCE == "external_pull_request_event" || $CI_PIPELINE_SOURCE == "web"'
- if: '$CI_COMMIT_BRANCH == "main" || $CI_COMMIT_BRANCH == "stages/prod"'
- if: '$CI_MERGE_REQUEST_IID || $CI_EXTERNAL_PULL_REQUEST_IID'
install:
stage: build
variables:
RAILS_ENV: test
SKIP_YARN_INSTALL: 'true'
cache:
- <<: *ruby_cache
policy: pull-push
- <<: *yarn_cache
policy: pull-push
- <<: *assets_cache
policy: push
script:
- *bundle_install
- *yarn_install
- bundle exec rake assets:precompile
# Build a container image async, and don't block CI tests
# Cache intermediate images for 1 week (168 hours)
build-idp-image:
stage: review
needs: []
interruptible: true
variables:
BRANCH_TAGGING_STRING: ''
rules:
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
variables:
BRANCH_TAGGING_STRING: '--destination ${ECR_REGISTRY}/identity-idp/idp:main'
- if: $CI_COMMIT_BRANCH != $CI_DEFAULT_BRANCH
- if: $CI_PIPELINE_SOURCE != "merge_request_event"
when: never
tags:
- build-pool
image:
name: gcr.io/kaniko-project/executor:debug
entrypoint: ['']
script:
- mkdir -p /kaniko/.docker
- echo ${CI_ENVIRONMENT_SLUG}
- echo $CI_ENVIRONMENT_SLUG
- echo $CI_COMMIT_BRANCH
- echo $CI_COMMIT_SHA
- |-
KANIKOCFG="\"credsStore\":\"ecr-login\""
if [ "x${http_proxy}" != "x" -o "x${https_proxy}" != "x" ]; then
KANIKOCFG="${KANIKOCFG}, \"proxies\": { \"default\": { \"httpProxy\": \"${http_proxy}\", \"httpsProxy\": \"${https_proxy}\", \"noProxy\": \"${no_proxy}\"}}"
fi
KANIKOCFG="{ ${KANIKOCFG} }"
echo "${KANIKOCFG}" > /kaniko/.docker/config.json
- >-
/kaniko/executor
--context "${CI_PROJECT_DIR}"
--dockerfile "${CI_PROJECT_DIR}/dockerfiles/idp_deploy.Dockerfile"
--destination "${ECR_REGISTRY}/identity-idp/idp:${CI_COMMIT_SHA}"
${BRANCH_TAGGING_STRING}
--cache-repo="${ECR_REGISTRY}/identity-idp/idp/cache"
--cache-ttl=168h
--cache=true
--snapshot-mode=redo
--compressed-caching=false
--build-arg "http_proxy=${http_proxy}"
--build-arg "https_proxy=${https_proxy}"
--build-arg "no_proxy=${no_proxy}"
--build-arg "ARG_CI_ENVIRONMENT_SLUG=${CI_ENVIRONMENT_SLUG}"
--build-arg "ARG_CI_COMMIT_BRANCH=${CI_COMMIT_BRANCH}"
--build-arg "ARG_CI_COMMIT_SHA=${CI_COMMIT_SHA}"
--build-arg "LARGE_FILES_TOKEN=${LARGE_FILES_TOKEN}"
--build-arg "LARGE_FILES_USER=${LARGE_FILES_USER}"
--build-arg "SERVICE_PROVIDERS_KEY=${SERVICE_PROVIDERS_KEY}"
build-nginx-image:
stage: review
needs: []
interruptible: true
variables:
BRANCH_TAGGING_STRING: ''
rules:
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
variables:
BRANCH_TAGGING_STRING: '--destination ${ECR_REGISTRY}/identity-idp/nginx:main'
- if: $CI_COMMIT_BRANCH != $CI_DEFAULT_BRANCH
- if: $CI_PIPELINE_SOURCE != "merge_request_event"
when: never
tags:
- build-pool
image:
name: gcr.io/kaniko-project/executor:debug
entrypoint: ['']
script:
- mkdir -p /kaniko/.docker
- echo ${CI_ENVIRONMENT_SLUG}
- echo $CI_ENVIRONMENT_SLUG
- echo $CI_COMMIT_BRANCH
- echo $CI_COMMIT_SHA
- |-
KANIKOCFG="\"credsStore\":\"ecr-login\""
if [ "x${http_proxy}" != "x" -o "x${https_proxy}" != "x" ]; then
KANIKOCFG="${KANIKOCFG}, \"proxies\": { \"default\": { \"httpProxy\": \"${http_proxy}\", \"httpsProxy\": \"${https_proxy}\", \"noProxy\": \"${no_proxy}\"}}"
fi
KANIKOCFG="{ ${KANIKOCFG} }"
echo "${KANIKOCFG}" > /kaniko/.docker/config.json
- >-
/kaniko/executor
--context "${CI_PROJECT_DIR}"
--dockerfile "${CI_PROJECT_DIR}/dockerfiles/nginx.Dockerfile"
--destination "${ECR_REGISTRY}/identity-idp/nginx:${CI_COMMIT_SHA}"
${BRANCH_TAGGING_STRING}
--cache-repo="${ECR_REGISTRY}/identity-idp/idp/cache"
--cache-ttl=168h
--cache=true
--snapshot-mode=redo
--compressed-caching=false
--build-arg "http_proxy=${http_proxy}"
--build-arg "https_proxy=${https_proxy}"
--build-arg "no_proxy=${no_proxy}"
--build-arg "ARG_CI_ENVIRONMENT_SLUG=${CI_ENVIRONMENT_SLUG}"
--build-arg "ARG_CI_COMMIT_BRANCH=${CI_COMMIT_BRANCH}"
--build-arg "ARG_CI_COMMIT_SHA=${CI_COMMIT_SHA}"
--build-arg "LARGE_FILES_TOKEN=${LARGE_FILES_TOKEN}"
--build-arg "LARGE_FILES_USER=${LARGE_FILES_USER}"
--build-arg "SERVICE_PROVIDERS_KEY=${SERVICE_PROVIDERS_KEY}"
check_changelog:
stage: test
variables:
GIT_DEPTH: 100
script:
- echo "$CI_PIPELINE_SOURCE"
- |
if [ "$CI_MERGE_REQUEST_TARGET_BRANCH_NAME" == "main" ]
then
git fetch origin --quiet
./scripts/changelog_check.rb -b origin/"${CI_MERGE_REQUEST_TARGET_BRANCH_NAME}" -s origin/"${CI_MERGE_REQUEST_SOURCE_BRANCH_NAME}"
elif [ "$CI_EXTERNAL_PULL_REQUEST_TARGET_BRANCH_NAME" == "main" ]
then
git fetch origin --quiet
./scripts/changelog_check.rb -b origin/"${CI_EXTERNAL_PULL_REQUEST_TARGET_BRANCH_NAME}" -s origin/"${CI_EXTERNAL_PULL_REQUEST_SOURCE_BRANCH_NAME}"
else
echo "Skipping because this is not a PR or is not targeting main"
exit 0
fi
migrate:
stage: test
needs:
- job: install
cache:
- <<: *ruby_cache
variables:
DOCKER_DB_HOST: db-postgres
POSTGRES_DB: identity_idp_test
POSTGRES_USER: postgres_user
POSTGRES_PASSWORD: postgres_password
POSTGRES_HOST_AUTH_METHOD: trust
RAILS_ENV: test
services:
- name: public.ecr.aws/docker/library/postgres:16.4
alias: db-postgres
command: ['--fsync=false', '--synchronous_commit=false', '--full_page_writes=false']
script:
- *bundle_install
- bundle exec rake db:create db:migrate --trace
- git diff db/
- make lint_database_schema_files
specs:
stage: test
needs:
- job: install
parallel: 22
cache:
- <<: *ruby_cache
- <<: *yarn_cache
- <<: *assets_cache
variables:
AWS_ACCESS_KEY_ID: test
AWS_DEFAULT_REGION: us-west-2
AWS_REGION: us-west-2
AWS_SECRET_ACCESS_KEY: test
CAPYBARA_WAIT_TIME_SECONDS: 5
COVERAGE: 'true'
DOCKER_DB_HOST: db-postgres
POSTGRES_DB: identity_idp_test
POSTGRES_USER: postgres_user
POSTGRES_PASSWORD: postgres_password
POSTGRES_HOST_AUTH_METHOD: trust
RAILS_ENV: test
services:
- name: public.ecr.aws/docker/library/postgres:16.4
alias: db-postgres
command: ['--fsync=false', '--synchronous_commit=false', '--full_page_writes=false']
- name: public.ecr.aws/docker/library/redis:7.0
alias: db-redis
artifacts:
expire_in: 31d
when: always
paths:
- coverage/
- rspec.xml
- rspec_json/
reports:
junit: rspec.xml
script:
- *bundle_install
- *yarn_install
- cp config/application.yml.default config/application.yml
- cp config/service_providers.localdev.yml config/service_providers.yml
- cp config/agencies.localdev.yml config/agencies.yml
- cp config/iaa_gtcs{.localdev,}.yml
- cp config/iaa_orders{.localdev,}.yml
- cp config/iaa_statuses{.localdev,}.yml
- cp config/integration_statuses{.localdev,}.yml
- cp config/integrations{.localdev,}.yml
- cp config/partner_account_statuses{.localdev,}.yml
- cp config/partner_accounts{.localdev,}.yml
- cp -a keys.example keys
- cp -a certs.example certs
- cp pwned_passwords/pwned_passwords.txt.sample pwned_passwords/pwned_passwords.txt
- "echo -e \"test:\n redis_url: 'redis://db-redis:6379/0'\n redis_throttle_url: 'redis://db-redis:6379/1'\" > config/application.yml"
- bundle exec rake db:create db:migrate --trace
- bundle exec rake db:seed
- bundle exec rake knapsack:rspec["--format documentation --format RspecJunitFormatter --out rspec.xml --format json --out rspec_json/${CI_NODE_INDEX}.json"]
lint:
stage: test
needs:
- job: install
cache:
- <<: *ruby_cache
- <<: *yarn_cache
- <<: *assets_cache
script:
- *bundle_install
- *yarn_install
- make lint
artifacts:
expire_in: 31d
when: always
paths:
- rubocop.xml
reports:
junit: rubocop.xml
js_build:
stage: test
needs:
- job: install
cache:
- <<: *ruby_cache
- <<: *yarn_production_cache
variables:
NODE_ENV: 'production'
SKIP_YARN_INSTALL: 'true'
script:
- *bundle_install
- *yarn_production_install
- bundle exec rake assets:precompile
- make lint_asset_bundle_size
js_tests:
stage: test
needs:
- job: install
cache:
- <<: *yarn_cache
script:
- *yarn_install
- yarn test
prepare_deploy:
# Runs in parallel with tests so we can deploy more quickly after passing
stage: test
needs:
- job: install
tags:
- build-pool
variables:
NODE_ENV: 'production'
RAILS_ENV: 'production'
script:
- cp config/application.yml.default.ci config/application.yml
- ./deploy/build
- ./deploy/build-post-config
- bundle exec rails zeitwerk:check
- make build_artifact ARTIFACT_DESTINATION_FILE='./tmp/idp.tar.gz'
- bundle exec ./scripts/artifact-upload './tmp/idp.tar.gz'
coverage:
stage: after_test
cache:
- <<: *ruby_cache
script:
- *bundle_install
- bundle exec spec/simplecov_merger.rb
- mv coverage/coverage/* coverage/
coverage: '/LOC \(\d+.\d+\%\) covered/'
artifacts:
reports:
coverage_report:
coverage_format: cobertura
path: coverage/coverage.xml
name: coverage
expire_in: 31d
paths:
- rspec_json/rspec.json
- knapsack_rspec_report.json
- coverage/index.html
- coverage/assets/
- coverage/coverage.xml
build-ci-image:
stage: build
interruptible: true
needs: []
tags:
- build-pool
image:
name: gcr.io/kaniko-project/executor:debug
entrypoint: ['']
rules:
# Build when there are changes to the Dockerfile
- if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH || $CI_PIPELINE_SOURCE == "merge_request_event" || $CI_PIPELINE_SOURCE == "external_pull_request_event" || $CI_PIPELINE_SOURCE == "web"'
changes:
compare_to: 'refs/heads/main'
paths:
- dockerfiles/idp_ci.Dockerfile
script:
- mkdir -p /kaniko/.docker
- |-
KANIKOCFG="\"credsStore\":\"ecr-login\""
if [ "x${http_proxy}" != "x" -o "x${https_proxy}" != "x" ]; then
KANIKOCFG="${KANIKOCFG}, \"proxies\": { \"default\": { \"httpProxy\": \"${http_proxy}\", \"httpsProxy\": \"${https_proxy}\", \"noProxy\": \"${no_proxy}\"}}"
fi
KANIKOCFG="{ ${KANIKOCFG} }"
echo "${KANIKOCFG}" > /kaniko/.docker/config.json
- >-
/kaniko/executor
--context "${CI_PROJECT_DIR}"
--dockerfile "${CI_PROJECT_DIR}/dockerfiles/idp_ci.Dockerfile"
--destination "${ECR_REGISTRY}/idp/ci:latest"
--destination "${ECR_REGISTRY}/idp/ci:${CI_COMMIT_SHA}"
--build-arg "http_proxy=${http_proxy}" --build-arg "https_proxy=${https_proxy}" --build-arg "no_proxy=${no_proxy}"
# Triggers devops CD to deploy to dev
trigger_devops:
stage: after_test
rules:
- if: $CI_COMMIT_BRANCH == "main"
trigger: lg/identity-devops
.deploy:
image:
name: dtzar/helm-kubectl:latest
script:
- kubectl config get-contexts
- export CONTEXT=$(kubectl config get-contexts | grep reviewapp | awk '{print $1}' | head -1)
- kubectl config use-context "$CONTEXT"
- export SANITIZED_BRANCH_NAME=$(echo "$CI_COMMIT_REF_NAME" | tr '/' '-' | tr -c '[:alnum:]-_' '-' | sed 's/-*$//')
- echo "${CI_COMMIT_REF_NAME}"
- echo "${SANITIZED_BRANCH_NAME}"
#TODO put in kustomize based deploy
# Dynamically populate review environment settings
- sed -i "s|{{ENVIRONMENT}}|${CI_ENVIRONMENT_SLUG}|g" ${APPLICATION_MANIFEST}
- sed -i "s|{{SANITIZED_BRANCH_NAME}}|${SANITIZED_BRANCH_NAME}|g" ${APPLICATION_MANIFEST}
- sed -i "s|{{IDP_CONTAINER_TAG}}|${CI_COMMIT_SHA}|g" ${APPLICATION_MANIFEST}
- sed -i "s|{{DASHBOARD_CONTAINER_TAG}}|${DASHBOARD_IMAGE_TAG}|g" ${APPLICATION_MANIFEST}
- sed -i "s|{{PIVCAC_CONTAINER_TAG}}|${PKI_IMAGE_TAG}|g" ${APPLICATION_MANIFEST}
- sed -i "s|{{ECR_REGISTRY}}|${ECR_REGISTRY}|g" ${APPLICATION_MANIFEST}
- sed -i "s|{{CI_PROJECT_NAME}}|${CI_PROJECT_NAME}|g" ${APPLICATION_MANIFEST}
- cat ${APPLICATION_MANIFEST}
# Apply our ArgoCD Application
- kubectl apply -f ${APPLICATION_MANIFEST} -n argocd
- echo "View your applications deployment progress at https://argocd.reviewapp.identitysandbox.gov/applications/argocd/${CI_ENVIRONMENT_SLUG}?view=tree&resource="
- echo "DNS may take a while to propagate, so be patient if it doesn't show up right away"
- echo "To access the rails console, first run 'aws-vault exec sandbox-power -- aws eks update-kubeconfig --name reviewapp'"
- echo "Then run aws-vault exec sandbox-power -- kubectl exec -it service/$CI_ENVIRONMENT_SLUG-idp -n review-apps -- /app/bin/rails console"
- echo "Address of IDP review app:"
- echo https://$CI_ENVIRONMENT_SLUG.reviewapps.identitysandbox.gov
- echo "Address of PIVCAC review app:"
- echo https://$CI_ENVIRONMENT_SLUG.pivcac.reviewapps.identitysandbox.gov
- echo "Address of Dashboard review app:"
- echo https://$CI_ENVIRONMENT_SLUG-dashboard.reviewapps.identitysandbox.gov
review-app:
stage: review
allow_failure: true
needs:
- job: build-idp-image
resource_group: $CI_ENVIRONMENT_SLUG.reviewapps.identitysandbox.gov
extends: .deploy
environment:
name: review/$CI_COMMIT_REF_NAME
url: https://$CI_ENVIRONMENT_SLUG.reviewapps.identitysandbox.gov
on_stop: stop-review-app
auto_stop_in: 2 days
rules:
- if: $CI_COMMIT_BRANCH != $CI_DEFAULT_BRANCH
- if: $CI_PIPELINE_SOURCE != "merge_request_event"
when: never
stop-review-app:
resource_group: $CI_ENVIRONMENT_SLUG.reviewapps.identitysandbox.gov
script:
- export CONTEXT=$(kubectl config get-contexts | grep reviewapp | awk '{print $1}' | head -1)
- kubectl config use-context "$CONTEXT"
- kubectl delete application "$CI_ENVIRONMENT_SLUG" -n argocd
- kubectl delete application "$CI_ENVIRONMENT_SLUG-db" -n argocd
stage: review
image:
name: dtzar/helm-kubectl:latest
needs:
- job: review-app
environment:
name: review/$CI_COMMIT_REF_NAME
action: stop
when: manual
rules:
- if: $CI_COMMIT_BRANCH != $CI_DEFAULT_BRANCH
- if: $CI_PIPELINE_SOURCE != "merge_request_event"
when: never
include:
- template: Jobs/SAST.gitlab-ci.yml
- template: Jobs/Dependency-Scanning.gitlab-ci.yml
- template: Security/Secret-Detection.gitlab-ci.yml
secret_detection:
allow_failure: false
needs: []
variables:
SECRET_DETECTION_EXCLUDED_PATHS: 'keys.example,config/artifacts.example,public/acuant/*/opencv.min.js,tmp/0.0.0.0-3000.key'
SECRET_DETECTION_REPORT_FILE: 'gl-secret-detection-report.json'
rules:
- if: $SECRET_DETECTION_DISABLED
when: never
- if: '$CI_COMMIT_BRANCH || $CI_COMMIT_TAG'
- if: $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "main"
variables:
SECRET_DETECTION_LOG_OPTIONS: origin/${CI_MERGE_REQUEST_TARGET_BRANCH_NAME}..HEAD
- if: $CI_MERGE_REQUEST_TARGET_BRANCH_NAME != "main" && $CI_EXTERNAL_PULL_REQUEST_TARGET_BRANCH_NAME == "main"
variables:
SECRET_DETECTION_LOG_OPTIONS: origin/${CI_EXTERNAL_PULL_REQUEST_TARGET_BRANCH_NAME}..HEAD
before_script:
- apk add --no-cache jq
- git fetch origin --quiet
script:
- |
if [ -z "$SECRET_DETECTION_LOG_OPTIONS" ]; then
/analyzer run
if [ -f "$SECRET_DETECTION_REPORT_FILE" ]; then
# check if '{ "vulnerabilities": [], ..' is empty in the report file if it exists
if [ "$(jq ".vulnerabilities | length" $SECRET_DETECTION_REPORT_FILE)" -gt 0 ]; then
echo "Vulnerabilities detected. Please analyze the artifact $SECRET_DETECTION_REPORT_FILE produced by the 'secret-detection' job."
echo "Check the \"Security\" tab on the overall pipeline run to download the report for more information."
exit 80
fi
else
echo "Artifact $SECRET_DETECTION_REPORT_FILE does not exist. The 'secret-detection' job likely didn't create one. Hence, no evaluation can be performed."
fi
else
echo "Skipping because this is not a PR or is not targeting main"
exit 0
fi
.container_scan_template:
interruptible: true
allow_failure: true
tags:
- build-pool
image:
name: amazon/aws-cli
entrypoint: ['']
before_script:
- curl -LO https://github.com/jqlang/jq/releases/download/jq-1.6/jq-linux64
- chmod +x jq-linux64
- mv jq-linux64 /usr/local/bin/jq
script:
- >
while true; do
SCAN_STATUS=$(aws ecr describe-image-scan-findings --repository-name ${ecr_repo} --image-id imageTag=$CI_COMMIT_SHA --query 'imageScanStatus.status' --output text || true)
if echo "$SCAN_STATUS" | grep -q "ACTIVE"; then
echo "Scan Complete"
break
elif echo "$SCAN_STATUS" | grep -q "FAILED"; then
echo "ECR scan failed"
exit 1
else
echo "Waiting for ECR scan to complete"
sleep 15
fi
done
- SCAN_FINDINGS=$(aws ecr describe-image-scan-findings --repository-name ${ecr_repo} --image-id imageTag=$CI_COMMIT_SHA)
- echo $SCAN_FINDINGS
- >
echo $SCAN_FINDINGS |
jq -r 'if (.imageScanFindings.enhancedFindings | length > 0) then
{
"version": "15.0.4",
"scan": {
"start_time": (.imageScanFindings.imageScanCompletedAt | sub("\\.[0-9]+"; "") | strptime("%Y-%m-%dT%H:%M:%S%z") | strftime("%Y-%m-%dT%H:%M:%S")),
"end_time": (.imageScanFindings.imageScanCompletedAt | sub("\\.[0-9]+"; "") | strptime("%Y-%m-%dT%H:%M:%S%z") | strftime("%Y-%m-%dT%H:%M:%S")),
"scanner": {
"id": "clair",
"name": "Amazon ECR Image Scan",
"version": "1.0.0",
"vendor": {
"name": "Amazon Web Services"
}
},
"analyzer": {
"id": "clair",
"name": "Amazon ECR Image Scan",
"version": "1.0.0",
"vendor": {
"name": "Amazon Web Services"
}
},
"status": "success",
"type": "container_scanning"
},
"vulnerabilities": [
.imageScanFindings.enhancedFindings[] |
{
"id": .packageVulnerabilityDetails.vulnerabilityId,
"name": .title,
"description": .description,
"severity": (if .severity == "HIGH" then "High"
elif .severity == "MEDIUM" then "Medium"
elif .severity == "LOW" then "Low"
elif .severity == "CRITICAL" then "Critical"
elif .severity == "INFORMATIONAL" then "Info"
elif .severity == "UNTRIAGED" then "Info"
else "Unknown" end),
"solution": .remediation.recommendation.text,
"identifiers": [
{
"type": "cve",
"name": .packageVulnerabilityDetails.vulnerabilityId,
"url": .packageVulnerabilityDetails.sourceUrl,
"value": .packageVulnerabilityDetails.vulnerabilityId
}
],
"links": [
{
"name": .packageVulnerabilityDetails.vulnerabilityId,
"url": .packageVulnerabilityDetails.sourceUrl
}
],
"location": {
"dependency": {
"package": {
"name": .packageVulnerabilityDetails.vulnerablePackages[0].name
},
"version": .packageVulnerabilityDetails.vulnerablePackages[0].version
},
"operating_system": .resources[0].details.awsEcrContainerImage.platform,
"image": .resources[0].id
}
}
]
}
else
{
"version": "15.0.4",
"scan": {
"start_time": (now | strftime("%Y-%m-%dT%H:%M:%S")),
"end_time": (now | strftime("%Y-%m-%dT%H:%M:%S")),
"scanner": {
"id": "clair",
"name": "Amazon ECR Image Scan",
"version": "1.0.0",
"vendor": {
"name": "Amazon Web Services"
}
},
"analyzer": {
"id": "clair",
"name": "Amazon ECR Image Scan",
"version": "1.0.0",
"vendor": {
"name": "Amazon Web Services"
}
},
"status": "success",
"type": "container_scanning"
},
"vulnerabilities": []
}
end' > gl-container-scanning-report.json
artifacts:
paths:
- gl-container-scanning-report.json
reports:
container_scanning: gl-container-scanning-report.json
# Export the automated ECR scan results into a format Gitlab can use
# Report schema https://gitlab.com/gitlab-org/security-products/security-report-schemas/-/blob/master/dist/container-scanning-report-format.json
ecr-scan-ci:
extends: .container_scan_template
rules:
- if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH || $CI_PIPELINE_SOURCE == "merge_request_event" || $CI_PIPELINE_SOURCE == "external_pull_request_event" || $CI_PIPELINE_SOURCE == "web"'
changes:
compare_to: 'refs/heads/main'
paths:
- dockerfiles/idp_ci.Dockerfile
needs:
- job: build-ci-image
stage: scan
variables:
ecr_repo: idp/ci
pinpoint_check_scheduled:
needs:
- job: install
cache:
- <<: *ruby_cache
- <<: *yarn_cache
script:
- *bundle_install
- *yarn_install
- make lint_country_dialing_codes
after_script:
- |-
if [ "$CI_JOB_STATUS" != "success" ]; then
./scripts/notify-slack \
--icon ":gitlab:" \
--username "gitlab-notify" \
--channel "#login-appdev" \
--webhook "${SLACK_WEBHOOK}" \
--raise \
--text "$(printf "Pinpoint supported countries check in GitLab failed.\nBuild Results: ${CI_JOB_URL}.\nCheck results locally with 'make lint_country_dialing_codes'")""
fi
rules:
- if: $CI_PIPELINE_SOURCE == "schedule"
audit_packages_scheduled:
needs:
- job: install
cache:
- <<: *ruby_cache
- <<: *yarn_cache
script:
- *bundle_install
- *yarn_install
- make audit
after_script:
- |-
if [ "$CI_JOB_STATUS" != "success" ]; then
./scripts/notify-slack \
--icon ":gitlab:" \
--username "gitlab-notify" \
--channel "#login-appdev" \
--webhook "${SLACK_WEBHOOK}" \
--raise \
--text "$(printf "Dependencies audit in GitLab failed.\nBuild Results: ${CI_JOB_URL}\nCheck results locally with 'make audit'")"
fi
rules:
- if: $CI_PIPELINE_SOURCE == "schedule"
# EKS deployment
deploy_eks:
trigger:
project: lg-public/identity-eks-control
branch: main
stage: deploy_eks
variables:
APP: idp
IMAGE_TAG: $CI_COMMIT_SHA
rules:
- if: $CI_COMMIT_BRANCH == "main"