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

feat: Added CI-PRE-CHECKER for VENDOR_PRODUCT #3840

Open
wants to merge 28 commits into
base: main
Choose a base branch
from

Conversation

joydeep049
Copy link
Contributor

Closes #3628

@codecov-commenter
Copy link

codecov-commenter commented Feb 19, 2024

Codecov Report

Attention: Patch coverage is 17.77778% with 37 lines in your changes are missing coverage. Please review.

Project coverage is 80.06%. Comparing base (d6cbe40) to head (9bf7a8e).
Report is 68 commits behind head on main.

❗ Current head 9bf7a8e differs from pull request most recent head 426bfe3. Consider uploading reports for the commit 426bfe3 to get more accurate results

Files Patch % Lines
cve_bin_tool/ci_pre_checker.py 0.00% 37 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3840      +/-   ##
==========================================
+ Coverage   75.41%   80.06%   +4.64%     
==========================================
  Files         808      814       +6     
  Lines       11983    12223     +240     
  Branches     1598     1654      +56     
==========================================
+ Hits         9037     9786     +749     
+ Misses       2593     2013     -580     
- Partials      353      424      +71     
Flag Coverage Δ
longtests 80.06% <17.77%> (+4.64%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@terriko terriko left a comment

Choose a reason for hiding this comment

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

Looks like a good start!

In the checker action: you'll need to grab the database from the cache here. You probably want the same code as we use in the testing yaml:

- name: Get date
id: get-date
run: |
echo "date=$(/bin/date -u "+%Y%m%d")" >> $GITHUB_OUTPUT
echo "yesterday=$(/bin/date -d "-1 day" -u "+%Y%m%d")" >> $GITHUB_OUTPUT
- name: Print Cache Keys
run: |
echo "Today's Cache Key: Linux-cve-bin-tool-${{ steps.get-date.outputs.date }}"
echo "Yesterday's Cache Key: Linux-cve-bin-tool-${{ steps.get-date.outputs.yesterday }}"
- name: Get today's cached database
uses: actions/cache@13aacd865c20de90d75de3b17ebe84f7a17d57d2 # v4.0.0
id: todays-cache
with:
path: cache
key: Linux-cve-bin-tool-${{ steps.get-date.outputs.date }}
- name: Get yesterday's cached database if today's is not available
uses: actions/cache@13aacd865c20de90d75de3b17ebe84f7a17d57d2 # v4.0.0
if: steps.todays-cache.outputs.cache-hit != 'true'
with:
path: cache
key: Linux-cve-bin-tool-${{ steps.get-date.outputs.yesterday }}

(edit: missed some necessary lines)

I'd also like to see

  • at least 1 test
  • documentation for what's going on here added to the contributor docs so people can understand what it means if it throws an error and how to fix it.

I'm also wondering if we should just load the python code of the checker itself rather than parsing the file, but I need to think a bit about the security implications of doing so. You may have the right idea for safety's sake.

@joydeep049
Copy link
Contributor Author

Looks like a good start!

In the checker action: you'll need to grab the database from the cache here. You probably want the same code as we use in the testing yaml:

- name: Get date
id: get-date
run: |
echo "date=$(/bin/date -u "+%Y%m%d")" >> $GITHUB_OUTPUT
echo "yesterday=$(/bin/date -d "-1 day" -u "+%Y%m%d")" >> $GITHUB_OUTPUT
- name: Print Cache Keys
run: |
echo "Today's Cache Key: Linux-cve-bin-tool-${{ steps.get-date.outputs.date }}"
echo "Yesterday's Cache Key: Linux-cve-bin-tool-${{ steps.get-date.outputs.yesterday }}"
- name: Get today's cached database
uses: actions/cache@13aacd865c20de90d75de3b17ebe84f7a17d57d2 # v4.0.0
id: todays-cache
with:
path: cache
key: Linux-cve-bin-tool-${{ steps.get-date.outputs.date }}
- name: Get yesterday's cached database if today's is not available
uses: actions/cache@13aacd865c20de90d75de3b17ebe84f7a17d57d2 # v4.0.0
if: steps.todays-cache.outputs.cache-hit != 'true'
with:
path: cache
key: Linux-cve-bin-tool-${{ steps.get-date.outputs.yesterday }}

(edit: missed some necessary lines)

I'd also like to see

  • at least 1 test
  • documentation for what's going on here added to the contributor docs so people can understand what it means if it throws an error and how to fix it.

I'm also wondering if we should just load the python code of the checker itself rather than parsing the file, but I need to think a bit about the security implications of doing so. You may have the right idea for safety's sake.

Hello @terriko ,
Why do we need to load the database in the checker-action. Doesn't my script already load the database to query it?
I think I'm missing something here.

Also, I've been thinking about the nature of the test to write for this checker-action. Ideas and examples for that would be really appreciated.

@inosmeet
Copy link
Contributor

Why do we need to load the database in the checker-action. Doesn't my script already load the database to query it?

You have to load it in the pipeline for it to be accessible to your code

@joydeep049
Copy link
Contributor Author

joydeep049 commented Feb 26, 2024

Hello @terriko ,
As an extension of the Pre-Checker concept,
I was thinking of writing a script meant as an experiment which takes in all of the currently existing checkers, And prints whether each of their VENDOR_PRODUCT has associated CVEs or not.
If there are checkers which have such CPEs in VENDOR_PRODUCT, then I'll file issues for those checkers. How does this sound?

( I did a PR for this, please check)

@joydeep049
Copy link
Contributor Author

joydeep049 commented Mar 4, 2024

Hello @terriko @ffontaine @anthonyharrison @Rexbeast2,
Most of the work here is done. But it is not being able to load the database even after i loaded it in cache .
I am using the same technique as is done throughout the repo to access the database. It runs perfectly on my local as well.
Can you tell me where I'm going wrong?

Edit: The windows long tests are failing because i provided an empty VENDOR_PRODUCT to test the checker action. It should be ignored.

@joydeep049
Copy link
Contributor Author

joydeep049 commented Mar 4, 2024

The location of the database file in the environment of the repo is coming out to be /home/runner/.cache/cve-bin-tool/cve.db.
Is this correct? Or do I have some issues in loading cve.db in the cache?
@terriko @Rexbeast2 @anthonyharrison

@inosmeet
Copy link
Contributor

Location here is correct.
You should run the tool once after retrieving the cache, because you are retrieving it correctly but aren't moving it at desired location, that's why it can't find the file :)

@joydeep049
Copy link
Contributor Author

Location here is correct. You should run the tool once after retrieving the cache, because you are retrieving it correctly but aren't moving it at desired location, that's why it can't find the file :)

Does that mean I'll have to run the tool in the github action once after caching the database?

@anthonyharrison
Copy link
Contributor

Location here is correct. You should run the tool once after retrieving the cache, because you are retrieving it correctly but aren't moving it at desired location, that's why it can't find the file :)

Does that mean I'll have to run the tool in the github action once after caching the database?

Yes.

@joydeep049
Copy link
Contributor Author

Location here is correct. You should run the tool once after retrieving the cache, because you are retrieving it correctly but aren't moving it at desired location, that's why it can't find the file :)

Does that mean I'll have to run the tool in the github action once after caching the database?

Yes.

Okay. Will do. @terriko mentioned above that i shud add tests. IS the current test enough to show the working of the action? I'm making the action fail to show that if it does not find any valid CVE , it will return error code.
As a result of VENDOR_PRODUCT being empty in the test_checker_action file, some windows tests are failing. Is that okay or should I change it?

Comment on lines +45 to +53
- name: Install cve-bin-tool
if: env.sbom != 'true'
run: |
python -m pip install --upgrade pip
python -m pip install --upgrade setuptools
python -m pip install --upgrade wheel
python -m pip install --upgrade -r dev-requirements.txt
python -m pip install --upgrade .

Copy link
Contributor

Choose a reason for hiding this comment

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

I think you were given some bad advice above: since your script doesn't actually call anything out of cve-bin-tool you shouldn't need to install it or run it, merely copy the database to the expected location. If you had any dependencies in your script that weren't python packages you could install them here (example below), but probably you just want to remove this section.

Suggested change
- name: Install cve-bin-tool
if: env.sbom != 'true'
run: |
python -m pip install --upgrade pip
python -m pip install --upgrade setuptools
python -m pip install --upgrade wheel
python -m pip install --upgrade -r dev-requirements.txt
python -m pip install --upgrade .
- name: Install requirements for vendor product pre-check
run: |
python -m pip install YOUR_LIST_OF_REQUIREMENTS

Comment on lines +54 to +60
- name: Try single CLI run of tool
if: env.sbom != 'true'
run: |
[[ -e cache ]] && mkdir -p .cache && mv cache ~/.cache/cve-bin-tool
NO_EXIT_CVE_NUM=1 python -m cve_bin_tool.cli test/assets/test-kerberos-5-1.15.1.out
cp -r ~/.cache/cve-bin-tool cache

Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
- name: Try single CLI run of tool
if: env.sbom != 'true'
run: |
[[ -e cache ]] && mkdir -p .cache && mv cache ~/.cache/cve-bin-tool
NO_EXIT_CVE_NUM=1 python -m cve_bin_tool.cli test/assets/test-kerberos-5-1.15.1.out
cp -r ~/.cache/cve-bin-tool cache
- name: Copy cache to expected location
run: |
[[ -e cache ]] && mkdir -p .cache && mv cache ~/.cache/cve-bin-tool

We only need to keep the cache copy line here.

- name: Get changed files in checkers directory
id: changed-files
run: |
files=$(git diff --name-only ${{ github.sha }} ${{ github.event.before }} | grep '^cve_bin_tool/checkers/' | xargs)
Copy link
Contributor

Choose a reason for hiding this comment

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

This is generating an error when run and I don't know offhand what to do with it, so you're on your own for fixing it:

Run files=$(git diff --name-only 6abca452cae5546029a21f9b8db03a2a6ee5c822 9bf7a8e572bc4ab05248b861c5d271b48792631a | grep '^cve_bin_tool/checkers/' | xargs)
fatal: bad object 9bf7a8e572bc4ab05248b861c5d271b48792631a

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yepp. I tried changing the depth of the checkout code to 0 and it fetched all the checker files. I'm not able to fetch only the changed .py files in checkers.

Copy link
Contributor

Choose a reason for hiding this comment

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

We're using https://github.com/technote-space/get-diff-action on some of our other workflows; don't know if it'll be better but maybe worth a shot?

Copy link
Member

@mastersans mastersans Mar 29, 2024

Choose a reason for hiding this comment

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

@joydeep049 I am not sure how comparing commits is better then just comparing branches, i think you can give it a try, not sure of the top its best way to do it but it should work, alternatively you can try get-diff-action as @terriko mentioned but on my first glance i think it only check changes between commits so you have to read a little, hope it helps:

  - name: Fetch Main Branch
    run: git fetch origin main
    
  - name: Get changed files in checkers directory
    id: changed-files
    run: |
      files=$(git diff --name-only origin/main | grep '^cve_bin_tool/checkers/' | xargs)
      echo "::set-output name=files::$files"
 
    
  - name: Print changed files in checkers directory
    run: |
      echo "Changed files in checkers directory:"
      echo "${{ steps.changed-files.outputs.files }}"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@mastersans Thanx! I'll try this!
Actually that is basically the problem I'm facing. I just want to read the difference between the last commit and this one!
But it actually gives me the difference between current and some very old commit!

If it doesn't work then @terriko anyways said in the meet she would give it a look next week.
Thanx!

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, but one branch can have multiple commit so why not just check branches for the difference.

@terriko
Copy link
Contributor

terriko commented Apr 3, 2024

Just putting a note in to say that I looked at this today, but it's so low priority that I am not expecting to have time to spend on debugging the diff issue until after the 3,3 release is out. I'm going to mark this as blocked so I don't waste time looking at it again until after.

@terriko terriko added the blocked label Apr 3, 2024
@terriko terriko removed the blocked label Apr 25, 2024
Copy link
Contributor

@terriko terriko left a comment

Choose a reason for hiding this comment

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

Marking as unblocked now that the release is done.

It's still failing with the following message:

Run files=$(git diff --name-only 6abca452cae5546029a21f9b8db03a2a6ee5c822 9bf7a8e572bc4ab05248b861c5d271b48792631a | grep '^cve_bin_tool/checkers/' | xargs)
fatal: bad object 9bf7a8e572bc4ab05248b861c5d271b48792631a

And the suggestions have been to try comparing branches rather than commits, which may yield better results. Looking at the diff action workflow we use elsewhere is also a possible option.

@joydeep049
Copy link
Contributor Author

Marking as unblocked now that the release is done.

It's still failing with the following message:

Run files=$(git diff --name-only 6abca452cae5546029a21f9b8db03a2a6ee5c822 9bf7a8e572bc4ab05248b861c5d271b48792631a | grep '^cve_bin_tool/checkers/' | xargs)
fatal: bad object 9bf7a8e572bc4ab05248b861c5d271b48792631a

And the suggestions have been to try comparing branches rather than commits, which may yield better results. Looking at the diff action workflow we use elsewhere is also a possible option.

I'll try that!
It may take a bit since my end-semester exams are going on.
(I'll keep this as my agenda for next week)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

feat: CI pre-checker for VENDOR_PRODUCT
6 participants