Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scan for Vulnerabilities + make PRs automatically #675

Merged
merged 2 commits into from
Dec 7, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions .github/workflows/snyk.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
---
name: Check for Snyk Vulnerabilities

on: # yamllint disable-line rule:truthy
workflow_dispatch:
# schedule:
# - cron: '0 12 * * *' # every day at 12pm UTC

jobs:
snyk:
name: snyk test
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Install Dependencies
run: >
npm install snyk -g;
pip install -r requirements.txt;
- name: Run Snyk Scan
run: >
# Run scan
snyk test --file=requirements.txt --json-file-output=scan.json;

# Exit if no vulnerabilities
[[ "$(jq '.ok' scan.json)" == "true" ]] && exit 0;
while read -r fix
do
package=$(echo $fix | cut -d "=" -f 1)
sed -i "/${package}.*/d" ckan/requirements.in
sed -i "/${package}.*/d" ckan/requirements.txt
echo $fix >> ckan/requirements.in
done <<< $(jq -r '.vulnerabilities[] | .moduleName,.fixedIn[]'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if there's no fix, does this for loop run?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there's no fix, we are testing what will happen, most likely a bad PR will be created.. But in that case, we'd have to make a PR to ignore it, we can add the functionality in the future. But I feel like that doesn't happen often.

scan.json | sed 'N;s/\n/>=/');

make update-dependencies;
exit 1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we exiting 1 here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exiting 1 to fail and ensure that the PR is created. Otherwise, everything would return 0 and no PR would be created.

- name: Create Pull Request
if: ${{ failure() }}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we want to do this on success, not failure?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous command succeeds if there are no vulnerabilities and fails if there are vulnerabilities.

id: scpr
uses: peter-evans/create-pull-request@v4
with:
token: ${{ secrets.ADD_TO_PROJECT_PAT }}
commit-message: Update Pip Requirements
committer: Data.gov Github <[email protected]>
author: ${{ github.actor }} <[email protected]>
signoff: false
branch: example-patches
delete-branch: true
title: '[Snyk + GH Actions] Update requirements'
body: |
Update requirements
- Updated `requirements.in` + `requirements.txt`
- Auto-generated by [snyk.yml][1]

[1]: https://github.com/gsa/catalog.data.gov/\
.github/workflows/snyk.yml
labels: |
requirements
automated pr
snyk
reviewers: GSA/data-gov-team
team-reviewers: |
owners
maintainers
draft: false