-
Notifications
You must be signed in to change notification settings - Fork 1
209 lines (190 loc) · 7.16 KB
/
cicd-sec-trivy.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
name: CICD Trivy Scan - Javelin Python
on:
push:
branches:
- "main"
env:
env_var: ${{ vars.ENV_CONTEXT_VAR }}
DEVOPS_REPO: "javelin-cloud"
DEVOPS_BRANCH: "main"
GH_SEC_REPORT: false
TRIVY_REPORT_FILE: "trivy-scan-result"
jobs:
javelin-env:
permissions:
contents: 'read'
id-token: 'write'
runs-on: ubuntu-24.04
steps:
- name: Checkout
uses: actions/checkout@v4
with:
persist-credentials: false
- name: Setting up Repo Env
id: repo_env_setup
shell: bash
run: |-
echo "repository=$(basename ${{ github.repository }})" >> ${GITHUB_OUTPUT}
echo "shortsha=$(git rev-parse --short=7 HEAD)" >> ${GITHUB_OUTPUT}
- name: Set Lowercase Repo Name
id: lc_repository
env:
REPO_NAME: ${{ steps.repo_env_setup.outputs.repository }}
shell: bash
run: echo "name=${REPO_NAME,,}" >> ${GITHUB_OUTPUT}
- name: DevOps Repository Checkout
uses: actions/checkout@v4
with:
repository: "${{ github.repository_owner }}/${{ env.DEVOPS_REPO }}"
token: ${{ secrets.DEVOPS_GITHUB_TOKEN }}
fetch-depth: 0
persist-credentials: false
ref: ${{ env.DEVOPS_BRANCH }}
path: ${{ env.DEVOPS_REPO }}
- name: Get Build Config
id: build_config
shell: bash
run: |-
trivy_severity=$(cat ${{ env.DEVOPS_REPO }}/app-config/javelin-default/sec-config.json | jq -r '.trivy.severity')
slack_scan_channel_id=$(cat ${{ env.DEVOPS_REPO }}/app-config/javelin-default/notify-config.json | jq -r '.slack.scan.channel_id')
echo "trivy_severity=${trivy_severity}" >> ${GITHUB_OUTPUT}
echo "slack_scan_channel_id=${slack_scan_channel_id}" >> ${GITHUB_OUTPUT}
outputs:
svc_name: ${{ steps.lc_repository.outputs.name }}
short_sha: ${{ steps.repo_env_setup.outputs.shortsha }}
trivy_severity: ${{ steps.build_config.outputs.trivy_severity }}
slack_scan_channel_id: ${{ steps.build_config.outputs.slack_scan_channel_id }}
javelin-trivy-scan:
needs:
- javelin-env
permissions:
contents: 'read'
id-token: 'write'
actions: 'read'
security-events: 'write'
runs-on: ubuntu-24.04
env:
TRIVY_SEVERITY: ${{ needs.javelin-env.outputs.trivy_severity }}
steps:
- name: Checkout
uses: actions/checkout@v4
with:
persist-credentials: true
- name: Trivy Scan - GitHub Security Report
if: ${{ env.GH_SEC_REPORT == 'true' }}
uses: aquasecurity/[email protected]
with:
ignore-unfixed: true
scan-type: "fs"
cache: "true"
format: "sarif"
output: "${{ env.TRIVY_REPORT_FILE }}.sarif"
severity: "${{ env.TRIVY_SEVERITY }}"
- name: Upload Report - GitHub Security Report
if: ${{ env.GH_SEC_REPORT == 'true' }}
uses: github/codeql-action/upload-sarif@v3
with:
sarif_file: "${{ env.TRIVY_REPORT_FILE }}.sarif"
- name: Trivy Scan - Text Security Report
if: ${{ env.GH_SEC_REPORT == 'false' }}
uses: aquasecurity/[email protected]
with:
ignore-unfixed: true
scan-type: "fs"
cache: "true"
format: "table"
output: "${{ env.TRIVY_REPORT_FILE }}.txt"
severity: "${{ env.TRIVY_SEVERITY }}"
- name: Report Check - Text Security Report
if: ${{ env.GH_SEC_REPORT == 'false' }}
id: report_check
shell: bash
run: |-
if [[ -s ${{ env.TRIVY_REPORT_FILE }}.txt ]] ; then
echo "report_file=available" >> ${GITHUB_OUTPUT}
else
echo "report_file=unavailable" >> ${GITHUB_OUTPUT}
fi
cat ${{ env.TRIVY_REPORT_FILE }}.txt
- name: Upload Report - Text Security Report
if: ${{ env.GH_SEC_REPORT == 'false' && steps.report_check.outputs.report_file == 'available' }}
uses: actions/upload-artifact@v4
with:
name: "${{ env.TRIVY_REPORT_FILE }}"
path: "${{ env.TRIVY_REPORT_FILE }}.txt"
if-no-files-found: error
retention-days: 1
outputs:
report_status: ${{ steps.report_check.outputs.report_file }}
javelin-trivy-notify:
needs:
- javelin-env
- javelin-trivy-scan
permissions:
contents: 'read'
id-token: 'write'
runs-on: ubuntu-24.04
if: |
always() && (needs.javelin-trivy-scan.outputs.report_status == 'available')
env:
SVC_NAME: ${{ needs.javelin-env.outputs.svc_name }}
JOB_STATUS: "failure"
REPORT_INFO: "Please check the attachment"
JOB_STATUS_MARK: ":x:"
COMMIT_AUTHOR: ${{ github.event.commits[0].author.name }}
COMMIT_SHA: ${{ needs.javelin-env.outputs.short_sha }}
SLACK_CHANNEL_ID: ${{ needs.javelin-env.outputs.slack_scan_channel_id }}
COMMIT_URL: ${{ github.event.head_commit.url }}
BUILD_URL: "${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"
GH_SEC_URL: "${{ github.server_url }}/${{ github.repository }}/security"
SLACK_PAYLOAD_JSON: slack-trivy-scan-payload.json
steps:
- name: DevOps Repository Checkout
uses: actions/checkout@v4
with:
repository: "${{ github.repository_owner }}/${{ env.DEVOPS_REPO }}"
token: ${{ secrets.DEVOPS_GITHUB_TOKEN }}
fetch-depth: 0
persist-credentials: false
ref: ${{ env.DEVOPS_BRANCH }}
path: ${{ env.DEVOPS_REPO }}
- name: Download Report - Text Security Report
if: ${{ env.GH_SEC_REPORT == 'false' }}
uses: actions/download-artifact@v4
with:
name: "${{ env.TRIVY_REPORT_FILE }}"
- name: Slack Payload Template
id: slack_template
shell: bash
run: |-
if [[ ${{ env.GH_SEC_REPORT }} == 'true' ]] ; then
export PAYLOAD_JSON="slack-trivy-scan-sec-payload.json"
else
export PAYLOAD_JSON="slack-trivy-scan-file-payload.json"
fi
envsubst < ${{ env.DEVOPS_REPO }}/slack-notify/${PAYLOAD_JSON} > ${{ env.SLACK_PAYLOAD_JSON }}
cat ${{ env.SLACK_PAYLOAD_JSON }}
- name: Slack Notification
uses: slackapi/[email protected]
id: slack_notify
with:
method: chat.postMessage
token: ${{ secrets.SLACK_BOT_TOKEN }}
payload-file-path: "${{ env.SLACK_PAYLOAD_JSON }}"
- name: Upload Report Slack - Text Security Report
if: ${{ env.GH_SEC_REPORT == 'false' }}
uses: slackapi/[email protected]
with:
method: files.uploadV2
token: ${{ secrets.SLACK_BOT_TOKEN }}
payload: |
channel_id: "${{ env.SLACK_CHANNEL_ID }}"
thread_ts: ${{ steps.slack_notify.outputs.ts }}
initial_comment: "The Trivy Scan Result (${{ env.COMMIT_SHA }})"
file: "${{ env.TRIVY_REPORT_FILE }}.txt"
filename: "${{ env.TRIVY_REPORT_FILE }}-${{ env.COMMIT_SHA }}.txt"
- name: Failing the Job
shell: bash
run: |-
echo "Vulnerabilities Found.....!"
exit 1