-
Notifications
You must be signed in to change notification settings - Fork 0
567 lines (537 loc) ยท 19.8 KB
/
terraform-module-validation.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
---
name: Terraform Module Validation
on:
workflow_call:
secrets:
infracost-api-key:
description: "The API key for infracost"
required: false
appvia-actions-secret:
description: "Appvia App secret for GH"
required: false
appvia-actions-id:
description: "Appvia App ID for GH"
required: false
inputs:
cicd-repository:
default: "appvia/appvia-cicd-workflows"
description: "The repository to pull the CI/CD workflows from"
required: false
type: string
cicd-branch:
default: "main"
description: "The branch to pull the CI/CD workflows from"
required: false
type: string
enable-infracost:
default: false
description: "Whether to run infracost on the Terraform Plan (secrets.infracost-api-key must be set if enabled)"
required: false
type: boolean
enable-commitlint:
default: true
description: "Whether to run commitlint on the commit message"
required: false
type: boolean
enable-checkov:
default: true
description: "Whether to run checkov security scanning"
required: false
type: boolean
enable-private-access:
description: Optional flag to state if terraform requires pulling private modules
required: false
default: false
type: boolean
enable-terraform-tests:
default: true
description: "Whether to run terraform test"
required: false
type: boolean
organization-name:
default: "appvia"
description: "The name of the GitHub organization"
required: false
type: string
terraform-dir:
default: "."
description: "The directory to validate"
required: false
type: string
terraform-tests-dir:
default: "."
description: "The terraform test directory"
required: false
type: string
terraform-version:
default: "1.9.0"
description: "The version of terraform to use"
required: false
type: string
trivy-version:
default: "v0.56.2"
description: "The version of trivy to use"
required: false
type: string
working-directory:
default: "."
description: "Working directory"
required: false
type: string
env:
## Location of the AWS Web Identity Token file
AWS_WEB_IDENTITY_TOKEN_FILE: /tmp/web_identity_token_file
permissions:
contents: read
pull-requests: write
jobs:
commitlint:
name: "Commitlint"
if: github.event_name == 'pull_request' && inputs.enable-commitlint
runs-on: ubuntu-latest
defaults:
run:
working-directory: ${{ inputs.working-directory }}
outputs:
result: ${{ steps.commitlint.outcome }}
steps:
- name: Checkout Repository
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Setup node
uses: actions/setup-node@v4
with:
node-version: 16
- name: Install Dependencies
run: npm install @commitlint/config-conventional @commitlint/cli
- name: Retrieve Commit Configuration
run: |
wget https://raw.githubusercontent.com/${{ inputs.cicd-repository }}/${{ inputs.cicd-branch }}/config/commitlint.config.js -O commitlint.config.js
- name: Run Commitlint
id: commitlint
run: npx commitlint --config commitlint.config.js --from ${{ github.event.pull_request.base.sha }} --to ${{ github.event.pull_request.head.sha }}
terraform-format:
name: "Terraform Format"
runs-on: ubuntu-latest
defaults:
run:
working-directory: ${{ inputs.working-directory }}
outputs:
result: ${{ steps.format.outcome }}
steps:
- name: Checkout Repository
uses: actions/checkout@v4
- name: Setup Terraform
uses: hashicorp/setup-terraform@v3
with:
terraform_version: ${{ inputs.terraform-version }}
- name: Terraform Format
id: format
run: terraform -chdir=${{ inputs.terraform-dir }} fmt -check -recursive
terraform-lint:
name: "Terraform Lint"
runs-on: ubuntu-latest
defaults:
run:
working-directory: ${{ inputs.working-directory }}
outputs:
result: ${{ steps.lint.outcome }}
steps:
- name: Checkout Repository
uses: actions/checkout@v4
- name: Setup Linter
uses: terraform-linters/setup-tflint@v4
- name: Enable Private Modules
id: get_workflow_token
if: inputs.enable-private-access
uses: peter-murray/workflow-application-token-action@v4
with:
application_id: ${{ secrets.appvia-actions-id }}
application_private_key: ${{ secrets.appvia-actions-secret }}
organization: ${{ inputs.organization-name }}
revoke_token: true
- name: Setup Credentials
if: inputs.enable-private-access
run: |
git config --global url."https://x-access-token:${{steps.get_workflow_token.outputs.token}}@github.com/".insteadOf "https://github.com/"
- name: Setup Terraform
uses: hashicorp/setup-terraform@v3
with:
terraform_version: ${{ inputs.terraform-version }}
- name: Terraform Init
run: terraform -chdir=${{ inputs.terraform-dir }} init -backend=false
- name: Setup Linter
uses: terraform-linters/setup-tflint@v4
- name: Linter Initialize
run: tflint --init
- name: Linting Code
id: lint
run: tflint -f compact
terraform-tests:
name: "Terraform Unit Tests"
runs-on: ubuntu-latest
if: inputs.enable-terraform-tests
defaults:
run:
working-directory: ${{ inputs.working-directory }}
outputs:
result: ${{ steps.tests.outcome }}
steps:
- name: Checkout Repository
uses: actions/checkout@v4
- name: Setup Terraform
uses: hashicorp/setup-terraform@v3
with:
terraform_version: ${{ inputs.terraform-version }}
- name: Enable Private Modules
id: get_workflow_token
if: inputs.enable-private-access
uses: peter-murray/workflow-application-token-action@v4
with:
application_id: ${{ secrets.appvia-actions-id }}
application_private_key: ${{ secrets.appvia-actions-secret }}
organization: ${{ inputs.organization-name }}
revoke_token: true
- name: Setup Credentials
if: inputs.enable-private-access
run: |
git config --global url."https://x-access-token:${{steps.get_workflow_token.outputs.token}}@github.com/".insteadOf "https://github.com/"
- name: Terraform Init
run: terraform -chdir=${{ inputs.terraform-tests-dir }} init -backend=false
- name: Run Tests
id: tests
run: terraform -chdir=${{ inputs.terraform-tests-dir }} test
terraform-init:
name: "Terraform Init"
runs-on: ubuntu-latest
defaults:
run:
working-directory: ${{ inputs.working-directory }}
outputs:
result: ${{ steps.init.outcome }}
steps:
- name: Checkout Repository
uses: actions/checkout@v4
- name: Setup node
uses: actions/setup-node@v4
with:
node-version: 16
- name: Setup Terraform
uses: hashicorp/setup-terraform@v3
with:
terraform_version: ${{ inputs.terraform-version }}
- name: Enable Private Modules
id: get_workflow_token
if: inputs.enable-private-access
uses: peter-murray/workflow-application-token-action@v4
with:
application_id: ${{ secrets.appvia-actions-id }}
application_private_key: ${{ secrets.appvia-actions-secret }}
organization: ${{ inputs.organization-name }}
revoke_token: true
- name: Setup Credentials
if: inputs.enable-private-access
run: |
git config --global url."https://x-access-token:${{steps.get_workflow_token.outputs.token}}@github.com/".insteadOf "https://github.com/"
- name: Terraform Init
id: init
run: terraform -chdir=${{ inputs.terraform-dir }} init
terraform-validate:
name: "Terraform Validate"
runs-on: ubuntu-latest
defaults:
run:
working-directory: ${{ inputs.working-directory }}
outputs:
result: ${{ steps.validate.outcome }}
steps:
- name: Checkout Repository
uses: actions/checkout@v4
- name: Setup node
uses: actions/setup-node@v4
with:
node-version: 16
- name: Setup Terraform
uses: hashicorp/setup-terraform@v3
with:
terraform_version: ${{ inputs.terraform-version }}
- name: Enable Private Modules
id: get_workflow_token
if: inputs.enable-private-access
uses: peter-murray/workflow-application-token-action@v4
with:
application_id: ${{ secrets.appvia-actions-id }}
application_private_key: ${{ secrets.appvia-actions-secret }}
organization: ${{ inputs.organization-name }}
revoke_token: true
- name: Setup Credentials
if: inputs.enable-private-access
run: |
git config --global url."https://x-access-token:${{steps.get_workflow_token.outputs.token}}@github.com/".insteadOf "https://github.com/"
- name: Terraform Init
id: init
run: terraform -chdir=${{ inputs.terraform-dir }} init
- name: Terraform Validate
id: validate
run: terraform -chdir=${{ inputs.terraform-dir }} validate -no-color
terraform-security:
name: "Static Security Analysis"
runs-on: ubuntu-latest
outputs:
result: ${{ steps.security.outcome }}
steps:
- name: Checkout Repository
uses: actions/checkout@v4
- name: Check If Trivy Ignore File Exists
id: security-exceptions
run: |
if [ -e ".trivyignore" ]; then
echo filename=".trivyignore" >> "${GITHUB_OUTPUT}"
fi
- name: Setup Terraform
uses: hashicorp/setup-terraform@v3
with:
terraform_version: ${{ inputs.terraform-version }}
- name: Enable Private Modules
id: get_workflow_token
if: inputs.enable-private-access
uses: peter-murray/workflow-application-token-action@v4
with:
application_id: ${{secrets.appvia-actions-id}}
application_private_key: ${{secrets.appvia-actions-secret}}
organization: appvia
revoke_token: true
- name: Setup Credentials
if: inputs.enable-private-access
run: |
git config --global url."https://x-access-token:${{steps.get_workflow_token.outputs.token}}@github.com/".insteadOf "https://github.com/"
- name: Terraform Init
id: init
run: terraform -chdir=${{ inputs.terraform-dir }} init -backend=false
- name: Run Trivy vulnerability
id: security
uses: aquasecurity/[email protected]
with:
exit-code: "1"
format: "table"
ignore-unfixed: true
scan-type: "config"
scan-ref: ${{ inputs.working-directory }}
severity: "CRITICAL,HIGH"
trivyignores: ${{ steps.security-exceptions.outputs.filename }}
version: ${{ inputs.trivy-version }}
terraform-checkov:
name: "Static Security Analysis - Checkov"
runs-on: ubuntu-latest
if: inputs.enable-checkov
outputs:
result: ${{ steps.security.outcome }}
steps:
- name: Checkout Repository
uses: actions/checkout@v4
- name: Ensure Checkov Configuration
run: |
[ -f ".checkov.yml" ] || echo "{}" > .checkov.yml
- name: Setup Terraform
uses: hashicorp/setup-terraform@v3
with:
terraform_version: ${{ inputs.terraform-version }}
- name: Enable Private Modules
id: get_workflow_token
if: inputs.enable-private-access
uses: peter-murray/workflow-application-token-action@v4
with:
application_id: ${{ secrets.appvia-actions-id }}
application_private_key: ${{ secrets.appvia-actions-secret }}
organization: ${{ inputs.organization-name }}
revoke_token: true
- name: Setup Credentials
if: inputs.enable-private-access
run: |
git config --global url."https://x-access-token:${{steps.get_workflow_token.outputs.token}}@github.com/".insteadOf "https://github.com/"
- name: Terraform Init
id: init
run: terraform -chdir=${{ inputs.terraform-dir }} init -backend=false
- name: Run Checkov
id: security
uses: bridgecrewio/checkov-action@v12
with:
config_file: .checkov.yml
directory: ${{ inputs.working-directory }}
framework: terraform
output_format: cli,sarif
soft_fail: true
terraform-validate-examples:
name: "Terraform Validate Examples"
runs-on: ubuntu-latest
outputs:
result: ${{ steps.validate-examples.outcome }}
steps:
- name: Checkout Repository
uses: actions/checkout@v4
- name: Setup node
uses: actions/setup-node@v4
with:
node-version: 16
- name: Enable Private Modules
id: get_workflow_token
if: inputs.enable-private-access
uses: peter-murray/workflow-application-token-action@v4
with:
application_id: ${{ secrets.appvia-actions-id }}
application_private_key: ${{ secrets.appvia-actions-secret }}
organization: ${{ inputs.organization-name }}
revoke_token: true
- name: Setup Credentials
if: inputs.enable-private-access
run: |
git config --global url."https://x-access-token:${{steps.get_workflow_token.outputs.token}}@github.com/".insteadOf "https://github.com/"
- name: Setup Terraform
uses: hashicorp/setup-terraform@v3
with:
terraform_version: ${{ inputs.terraform-version }}
- name: Validate Examples
id: validate-examples
run: |
for dir in examples/*; do
if [ -d "$dir" ]; then
echo "--> Validating $dir"
terraform -chdir="${dir}" init -backend=false
terraform -chdir="${dir}" validate -no-color
fi
done
terraform-docs:
name: "Terraform Docs"
runs-on: ubuntu-latest
if: ${{ github.actor != 'dependabot[bot]' }}
defaults:
run:
working-directory: ${{ inputs.working-directory }}
outputs:
result: ${{ steps.docs.outcome }}
steps:
- name: Checkout Repository
uses: actions/checkout@v4
- name: Terraform Docs
id: docs
uses: terraform-docs/[email protected]
with:
fail-on-diff: true
working-dir: ${{ inputs.terraform-dir }}
terraform-infracost:
name: "Get Cost Estimate"
if: github.event_name == 'pull_request' && inputs.enable-infracost
runs-on: ubuntu-latest
defaults:
run:
working-directory: ${{ inputs.working-directory }}
steps:
- name: Setup Infracost
uses: infracost/actions/setup@v3
with:
api-key: ${{ secrets.infracost-api-key }}
currency: GBP
- name: Checkout PR Base Branch
uses: actions/checkout@v4
with:
ref: "${{ github.event.pull_request.base.ref }}"
- name: Check For TF Files
id: baseline
run: |
if ls ./*.tf 2>/dev/null; then
echo "exists=true" >> "${GITHUB_OUTPUT}"
else
echo "exists=false" >> "${GITHUB_OUTPUT}"
fi
- name: Generate Cost Estimate Baseline
if: steps.baseline.outputs.exists == 'true'
run: |
infracost breakdown --path="${TF_ROOT}" \
--format=json \
--out-file=/tmp/infracost-base.json
- name: Checkout PR branch
uses: actions/checkout@v4
- name: Generate Cost Estimate Difference
if: steps.baseline.outputs.exists == 'true'
run: |
infracost diff --path="${TF_ROOT}" \
--format=json \
--compare-to=/tmp/infracost-base.json \
--out-file=/tmp/infracost.json
- name: Generate Infracost Cost Estimate
if: steps.baseline.outputs.exists == 'false'
run: |
infracost breakdown --path="$}{TF_ROOT}" \
--format=json \
--out-file=/tmp/infracost.json
- name: Post Infracost comment
run: |
infracost comment github --path=/tmp/infracost.json \
--repo="${GITHUB_REPOSITORY}" \
--github-token=${{github.token}} \
--pull-request=${{github.event.pull_request.number}} \
--behavior=update
update-pr:
name: "Update PR"
if: github.event_name == 'pull_request' && (success() || failure())
runs-on: ubuntu-latest
defaults:
run:
working-directory: ${{ inputs.working-directory }}
needs:
- commitlint
- terraform-docs
- terraform-format
- terraform-init
- terraform-lint
- terraform-tests
- terraform-security
- terraform-checkov
- terraform-validate
- terraform-validate-examples
steps:
- name: Add PR Comment
uses: actions/github-script@v7
with:
script: |
// 1. Retrieve existing bot comments for the PR
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
})
const botComment = comments.find(comment => {
return comment.user.type === 'Bot' && comment.body.includes('Pull Request Review Status') && comment.body.includes('<b>Working Directory:</b> \`${{ inputs.working-directory }}\`')
})
// 2. Prepare format of the comment
const output = `### Pull Request Review Status
* ๐ <b>Terraform Format and Style:</b> \`${{ needs.terraform-format.outputs.result }}\`
* ๐ <b>Terraform Linting:</b> \`${{ needs.terraform-lint.outputs.result }}\`
* ๐ฎ <b>Terraform Security Check:</b> \`${{ needs.terraform-security.outputs.result }}\`
* ๐ฎ <b>Terraform Security Checkov:</b> \`${{ needs.terraform-checkov.outputs.result }}\`
* ๐ <b>Terraform Tests:</b> \`${{ needs.terraform-tests.outputs.result }}\`
* ๐ง <b>Terraform Initialisation:</b> \`${{ needs.terraform-init.outputs.result }}\`
* ๐ค <b>Terraform Validation:</b> \`${{ needs.terraform-validate.outputs.result }}\`
* ๐ค <b>Terraform Example Validation:</b> \`${{ needs.terraform-validate-examples.outputs.result }}\`
* ๐ <b>Terraform Documentation:</b> \`${{ needs.terraform-docs.outputs.result }}\`
* ๐ <b>Commitlint:</b> \`${{ needs.commitlint.outputs.result }}\`
*<b>Working Directory:</b> \`${{ inputs.working-directory }}\`*
*<b>Pusher:</b> @${{ github.actor }}, <b>Action:</b> \`${{ github.event_name }}\`*
*<b>Workflow Run Link:</b> ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}*`;
// 3. If we have a comment, update it, otherwise create a new one
if (botComment) {
github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: botComment.id,
body: output
})
} else {
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: output
})
}