Skip to content

Commit fca5f5d

Browse files
committed
feat: [SRE-830] add trivy docker scan configuration and matrix output support
1 parent a6a88f0 commit fca5f5d

File tree

2 files changed

+137
-18
lines changed

2 files changed

+137
-18
lines changed

README.md

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Github Action for running trivy scans
44
# Overview
55
stordco/actions-trivy is used to run [trivy](https://github.com/aquasecurity/trivy) scans with various scan types. The current scan types supported:
66
1. [Filesystem](https://aquasecurity.github.io/trivy/v0.52/docs/target/filesystem/)
7-
7+
1. [Images](https://aquasecurity.github.io/trivy/v0.52/docs/target/container_image/)
88

99
# Filesystem scans
1010
## Usage
@@ -19,13 +19,62 @@ stordco/actions-trivy is used to run [trivy](https://github.com/aquasecurity/tri
1919
slack-bot-token: ${{ secrets.SLACK_BOT_TOKEN }}
2020
```
2121
22-
## Inputs
2322
<!-- {x-release-please-end} -->
23+
## Inputs
2424
2525
| name | description | default value |
2626
| --- | --- | --- |
27+
| `github-token` | (optional) Should be set to `secrets.GH_PERSONAL_ACCESS_TOKEN` in order to interact with Github API. If not set, then PR comments will not be uploaded with the scan output. | "" |
2728
| `scan-type` | (required) Specifies the type of scan to be perforemed (e.g., `fs` for filesystem scan). | |
29+
| `slack-bot-token` | (optional) Should be set to `secrets.SLACK_BOT_TOKEN` to send messages through `Github Actions`. If not set, then slack messages will not be posted. | "" |
30+
31+
32+
## Outputs
33+
34+
| name | description | default value |
35+
| --- | --- | --- |
36+
| `artifact-url` | Returns link to trivy scan artifact. Main branch artifacts are retained for 90 days while others are retained for 1 day. | |
37+
38+
39+
## General Information
40+
41+
For default usage:
42+
1. When a merge into the `main` branch occurs that contains `CRITICAL` vulnerabilities, a notification will be sent to the `#trivy-alerts` Slack channel containing the number of critical vulnerabilities detected and a link to the full trivy scan report artifact.
43+
1. When any vulnerabilities `(UNKNOWN, LOW, MEDIUM, HIGH, CRITICAL)` are detected on PR builds, a comment will be posted to the PR including the full output of the OS and library vulnerabilities detected based on the `mix.lock` dependencies.
44+
45+
# Image scans
46+
## Usage
47+
<!-- {x-release-please-start-version} -->
48+
### Simple
49+
```yaml
50+
- name: Trivy Image Scan
51+
uses: stordco/[email protected]
52+
with:
53+
scan-type: image
54+
image-ref: gcr.io/stord-ci/app-base:2024.06.25_d5cd08e
55+
github-token: ${{ secrets.GH_PERSONAL_ACCESS_TOKEN }}
56+
slack-bot-token: ${{ secrets.SLACK_BOT_TOKEN }}
57+
```
58+
### Matrix Jobs
59+
```yaml
60+
- name: Trivy Image Scan
61+
uses: stordco/[email protected]
62+
with:
63+
scan-type: image
64+
image-ref: gcr.io/stord-ci/app-base:2024.06.25_d5cd08e
65+
matrix-id: unique-identifier
66+
github-token: ${{ secrets.GH_PERSONAL_ACCESS_TOKEN }}
67+
slack-bot-token: ${{ secrets.SLACK_BOT_TOKEN }}
68+
```
69+
<!-- {x-release-please-end} -->
70+
## Inputs
71+
72+
| name | description | default value |
73+
| --- | --- | --- |
2874
| `github-token` | (optional) Should be set to `secrets.GH_PERSONAL_ACCESS_TOKEN` in order to interact with Github API. If not set, then PR comments will not be uploaded with the scan output. | "" |
75+
| `image-ref` | (optional) Specifies the Docker image to be scanned | "" |
76+
| `matrix-id` | (optional) If matrix jobs are being leveraged, add in an unique matrix job identifier to be leveraged for the notifications. | "" |
77+
| `scan-type` | (required) Specifies the type of scan to be perforemed (e.g., `image` for container image scan). | |
2978
| `slack-bot-token` | (optional) Should be set to `secrets.SLACK_BOT_TOKEN` to send messages through `Github Actions`. If not set, then slack messages will not be posted. | "" |
3079

3180

@@ -39,8 +88,8 @@ stordco/actions-trivy is used to run [trivy](https://github.com/aquasecurity/tri
3988
## General Information
4089

4190
For default usage:
42-
1. When a merge into the `main` branch occurs that contains `Critical` vulnerabilities, a notification will be sent to the #trivy-alerts Slack channel containing the number of critical vulnerabilities detected and a link to the full trivy scan report artifact.
43-
1. When any vulnerabilities `(UNKNOWN, LOW, MEDIUM, HIGH, CRITICAL)` are detected on PR builds, a comment will be posted to the PR including the full output of the OS vulnerabilities detected based on the `mix.lock` dependencies.
91+
1. When a merge into the `main` branch occurs that contains `CRITICAL` vulnerabilities, a notification will be sent to the `#trivy-alerts` Slack channel containing the number of critical vulnerabilities detected and a link to the full trivy scan report artifact.
92+
1. When any vulnerabilities `(UNKNOWN, LOW, MEDIUM, HIGH, CRITICAL)` are detected on PR builds, a comment will be posted to the PR including the full output of the OS, library vulnerabilities and secrets detected found on the container image.
4493

4594
# Releasing
4695

action.yml

Lines changed: 84 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,23 @@ description: >-
88
GitHub Composite Action for running Trivy scans
99
1010
inputs:
11-
scan-type:
12-
description: >-
13-
"Specify the type of scan to be perforemed (e.g., 'fs' for filesystem scan)."
14-
type: string
15-
required: true
1611
github-token:
1712
description: "GitHub token for authentication."
1813
type: string
1914
required: false
15+
image-ref:
16+
description: "Specify the local Docker image to be scanned. This value needs to be set if the scan-type = image."
17+
type: string
18+
required: false
19+
matrix-id:
20+
description: "Specify the matrix indicator to be leveraged on notification steps."
21+
type: string
22+
required: false
23+
scan-type:
24+
description: >-
25+
"Specify the type of scan to be perforemed (e.g., 'fs' for filesystem scan, `image` for image scan)."
26+
type: string
27+
required: true
2028
slack-bot-token:
2129
description: "Slackbot token for sending notifications."
2230
type: string
@@ -36,13 +44,17 @@ runs:
3644
if [[ '${{ inputs.scan-type }}' == 'fs' ]]; then
3745
echo "config_file_type=fs" >> "$GITHUB_OUTPUT"
3846
fi
47+
if [[ '${{ inputs.scan-type }}' == 'image' ]]; then
48+
echo "config_file_type=image" >> "$GITHUB_OUTPUT"
49+
fi
3950
shell: bash
4051

4152
- name: Run Trivy vulnerability scanner in ${{ inputs.scan-type }} mode
4253
id: trivy_scan
4354
uses: aquasecurity/[email protected]
4455
with:
4556
scan-type: ${{ inputs.scan-type }}
57+
image-ref: ${{ inputs.image-ref }}
4658
trivy-config: .trivy/${{ steps.configuration_file.outputs.config_file_type }}-config.yaml
4759
continue-on-error: true
4860

@@ -56,17 +68,22 @@ runs:
5668
echo "Keep trivy artifact for 1 day on PR builds"
5769
echo "days=1" >> "$GITHUB_OUTPUT"
5870
fi
59-
echo "sha_short=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT
71+
72+
sha_short=$(git rev-parse --short HEAD)
6073
sanitized_ref_name=$(echo "${{ github.ref_name }}" | sed 's/[\\\/:*?<>|]/-/g')
6174
62-
echo "ref_name=${sanitized_ref_name}" >> $GITHUB_OUTPUT
75+
if [[ -z '${{ inputs.matrix-id }}' ]]; then
76+
echo "name=trivy-${{ inputs.scan-type }}-results-${sha_short}-${sanitized_ref_name}" >> $GITHUB_OUTPUT
77+
else
78+
echo "name=trivy-${{ inputs.scan-type }}-results-${sha_short}-${sanitized_ref_name}-${{ inputs.matrix-id }}" >> $GITHUB_OUTPUT
79+
fi
6380
shell: bash
6481

6582
- name: Upload Trivy report to artifacts
6683
uses: actions/upload-artifact@v4
6784
id: trivy_artifact_upload
6885
with:
69-
name: trivy-${{ inputs.scan-type }}-results-${{ steps.artifact_metadata.outputs.sha_short }}-${{ steps.artifact_metadata.outputs.ref_name }}
86+
name: ${{ steps.artifact_metadata.outputs.name }}
7087
path: trivy-${{ inputs.scan-type }}-results.json
7188
retention-days: ${{ steps.artifact_metadata.outputs.days }}
7289
if-no-files-found: ignore
@@ -79,6 +96,7 @@ runs:
7996
env:
8097
GITHUB_TOKEN: ${{ inputs.github-token }}
8198
SCAN_TYPE: ${{ inputs.scan-type }}
99+
MATRIX_ID: ${{ inputs.matrix-id }}
82100
with:
83101
script: |
84102
const fs = require('fs');
@@ -87,7 +105,12 @@ runs:
87105
const owner = context.repo.owner;
88106
const repo = context.repo.repo;
89107
90-
const commentIdentifier = `<!-- trivyReportComment-${process.env.SCAN_TYPE}-->`;
108+
let commentIdentifier;
109+
if (!process.env.MATRIX_ID) {
110+
commentIdentifier = `<!-- trivyReportComment-${process.env.SCAN_TYPE}-->`;
111+
} else {
112+
commentIdentifier = `<!-- trivyReportComment-${process.env.SCAN_TYPE}-${process.env.MATRIX_ID}-->`;
113+
}
91114
92115
const filePath = path.join(process.env.GITHUB_WORKSPACE, `trivy-${process.env.SCAN_TYPE}-results.json`);
93116
const fileContent = fs.readFileSync(filePath, 'utf8');
@@ -142,9 +165,11 @@ runs:
142165
const botComment = comments.data.find(comment => comment.body.includes(commentIdentifier));
143166
core.info(`Parsed Vulnerabilities: ${JSON.stringify(vulnerabilities)}`)
144167
168+
const scanTypeName = process.env.SCAN_TYPE === 'fs' ? 'filesystem' : 'image';
169+
145170
if (vulnerabilities.flatMap(result => result.Vulnerabilities).length === 0) {
146171
if (botComment) {
147-
const noErrorsComment = `No vulnerabilities to be reported.\n${commentIdentifier}`;
172+
const noErrorsComment = `No Trivy ${scanTypeName} vulnerabilities to be reported${process.env.MATRIX_ID ? ` for ${process.env.MATRIX_ID}` : ''}.\n${commentIdentifier}`;
148173
149174
await github.rest.issues.updateComment({
150175
owner,
@@ -172,15 +197,16 @@ runs:
172197
173198
if (formattedContent.length > MAX_COMMENT_LENGTH) {
174199
fullCommentBody = `
175-
The Trivy scan report is too large to display here. Please view the detailed output from the job:
200+
The Trivy ${scanTypeName} scan report${process.env.MATRIX_ID ? ` for ${process.env.MATRIX_ID}` : ''} too large to display here. Please view the detailed output from the job:
176201
177202
[View Trivy Report](https://github.com/${owner}/${repo}/actions/runs/${context.runId})
178203
179204
${commentIdentifier}
180205
`;
181206
} else {
207+
182208
fullCommentBody = `
183-
View the Trivy scan report below. Click on the dropdown to expand the report.
209+
View the Trivy ${scanTypeName} scan report${process.env.MATRIX_ID ? ` for ${process.env.MATRIX_ID}` : ''} below. Click on the dropdown to expand the report.
184210
${formattedContent}
185211
`;
186212
}
@@ -205,7 +231,47 @@ runs:
205231
}
206232
207233
- name: Notify Slack of critical vulnerabilities
208-
if: ${{ steps.trivy_report_notification.outputs.critical_vulnerabilities_count != '0' && github.ref_name == 'main' && inputs.slack-bot-token }}
234+
if: ${{ steps.trivy_report_notification.outputs.critical_vulnerabilities_count != '0' && github.ref_name == 'main' && inputs.slack-bot-token && !inputs.matrix-id }}
235+
uses: slackapi/[email protected]
236+
env:
237+
SLACK_BOT_TOKEN: ${{ inputs.slack-bot-token }}
238+
with:
239+
channel-id: 'C078TPMGC21'
240+
payload: |
241+
{
242+
"blocks": [
243+
{
244+
"type": "section",
245+
"text": {
246+
"type": "mrkdwn",
247+
"text": ":alert: *Critical vulnerabilities detected* in Trivy ${{ inputs.scan-type }} scan. `${{ steps.trivy_report_notification.outputs.critical_vulnerabilities_count }}` critical vulnerabilities detected."
248+
}
249+
},
250+
{
251+
"type": "section",
252+
"fields": [
253+
{
254+
"type": "mrkdwn",
255+
"text": "*Repository:*\n`${{ github.repository }}`"
256+
},
257+
{
258+
"type": "mrkdwn",
259+
"text": "*Branch:*\n`${{ github.ref_name }}`"
260+
}
261+
]
262+
},
263+
{
264+
"type": "section",
265+
"text": {
266+
"type": "mrkdwn",
267+
"text": "<${{ steps.trivy_artifact_upload.outputs.artifact-url }}|View Artifacts>"
268+
}
269+
}
270+
]
271+
}
272+
273+
- name: Notify Slack of critical vulnerabilities (${{ inputs.matrix-id }})
274+
if: ${{ steps.trivy_report_notification.outputs.critical_vulnerabilities_count != '0' && github.ref_name == 'main' && inputs.slack-bot-token && inputs.matrix-id }}
209275
uses: slackapi/[email protected]
210276
env:
211277
SLACK_BOT_TOKEN: ${{ inputs.slack-bot-token }}
@@ -218,7 +284,7 @@ runs:
218284
"type": "section",
219285
"text": {
220286
"type": "mrkdwn",
221-
"text": ":alert: *Critical vulnerabilities detected* in Trivy ${{ inputs.scan-type }} scan. ${{ steps.trivy_report_notification.outputs.critical_vulnerabilities_count }} critical vulnerabilities detected."
287+
"text": ":alert: *Critical vulnerabilities detected* in Trivy ${{ inputs.scan-type }} scan. `${{ steps.trivy_report_notification.outputs.critical_vulnerabilities_count }}` critical vulnerabilities detected."
222288
}
223289
},
224290
{
@@ -231,6 +297,10 @@ runs:
231297
{
232298
"type": "mrkdwn",
233299
"text": "*Branch:*\n`${{ github.ref_name }}`"
300+
},
301+
{
302+
"type": "mrkdwn",
303+
"text": "*Matrix ID:*\n`${{ inputs.matrix-id }}`"
234304
}
235305
]
236306
},

0 commit comments

Comments
 (0)