-
Notifications
You must be signed in to change notification settings - Fork 4
feat: impl grype src code scan #55
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b5c90f3
feat: impl grype src code scan
gibaros ac738d2
feat: fix lint errors
gibaros 804954f
feat: fix lint error
gibaros 6279dd0
feat: update versions
gibaros fcff5c5
feat: refactor, merge grype image and src scans
gibaros ca34e31
feat: update logic, check gyrpe install
gibaros 2728ba8
feat: fix lint errors
gibaros da786bc
feat: fix lint errors
gibaros 5cbac57
feat: update versions
gibaros File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| description: | | ||
| This job scans the source code for vulnerabilities using Grype. | ||
| executor: | ||
| name: machine | ||
| resource_class: << parameters.resource_class >> | ||
| environment: | ||
| MAIN_BRANCH_NAME: main | ||
| BASH_ENV: /etc/profile ## Ref: https://circleci.com/docs/env-vars/#alpine-linux | ||
| ENV: ~/.profile | ||
| NVM_ARCH_UNOFFICIAL_OVERRIDE: x64-musl ## Ref: https://github.com/nvm-sh/nvm/issues/1102#issuecomment-550572252 | ||
| parameters: | ||
| resource_class: | ||
| type: enum | ||
| enum: ["small", "medium", "medium+", "large", "xlarge", "2xlarge", "2xlarge+"] | ||
| default: medium | ||
| steps: | ||
| - checkout | ||
| - run: | ||
| name: Check for Grype config and scan type | ||
| command: | | ||
| if [ ! -f .grype.yaml ]; then | ||
| echo "Error: .grype.yaml configuration file not found in repository at root level." | ||
| echo "Please add a .grype.yaml file with appropriate vulnerability ignore rules." | ||
| exit 1 | ||
| fi | ||
|
|
||
| # Check if scan is disabled | ||
| if grep -q "disabled: true" .grype.yaml; then | ||
| echo "Grype source scan is disabled in .grype.yaml" | ||
| circleci-agent step halt | ||
| fi | ||
|
|
||
| # Determine if source scan should run | ||
| SHOULD_RUN_SOURCE="false" | ||
|
|
||
| # Check explicit scan-type configuration | ||
| if grep -q "scan-type: source" .grype.yaml; then | ||
| SHOULD_RUN_SOURCE="true" | ||
| echo "scan-type: source found in .grype.yaml - running source scan" | ||
| elif grep -q "scan-type: image" .grype.yaml; then | ||
| echo "scan-type: image found in .grype.yaml - skipping source scan" | ||
| circleci-agent step halt | ||
| else | ||
| # Auto-detect based on Dockerfile presence | ||
| if [ ! -f Dockerfile ]; then | ||
| SHOULD_RUN_SOURCE="true" | ||
| echo "No Dockerfile found and no scan-type specified - running source scan" | ||
| else | ||
| echo "Dockerfile found and no scan-type specified - skipping source scan (image scan will run)" | ||
| circleci-agent step halt | ||
| fi | ||
| fi | ||
| - run: | ||
| name: Check dependencies | ||
| command: | | ||
| if ! command -v jq &> /dev/null; then | ||
| echo "jq could not be found, installing..." | ||
| sudo apt-get update && sudo apt-get install -y jq | ||
| fi | ||
| - run: | ||
| name: Install Grype | ||
| command: | | ||
| curl -sSfL https://raw.githubusercontent.com/anchore/grype/main/install.sh | sudo sh -s -- -b /usr/local/bin | ||
| - run: | ||
| name: Run Grype source code scan with custom config | ||
| command: | | ||
| echo "Scanning source code in current directory" | ||
| # Use the config file in your repo to scan the source code | ||
| grype dir:. -c .grype.yaml -o table > grype-results.txt | ||
| grype dir:. -c .grype.yaml -o json > grype-results.json | ||
| cat grype-results.txt | ||
| cat grype-results.json | ||
| - run: | ||
| name: Check for critical, high and medium vulnerabilities | ||
| command: | | ||
| # Count vulnerabilities in the filtered results | ||
| CRITICAL_COUNT=$(cat grype-results.json | jq '[.matches[] | select(.vulnerability.severity == "Critical")] | length') | ||
| HIGH_COUNT=$(cat grype-results.json | jq '[.matches[] | select(.vulnerability.severity == "High")] | length') | ||
| MEDIUM_COUNT=$(cat grype-results.json | jq '[.matches[] | select(.vulnerability.severity == "Medium")] | length') | ||
|
|
||
| echo "Critical vulnerabilities found: $CRITICAL_COUNT" | ||
| echo "High vulnerabilities found: $HIGH_COUNT" | ||
| echo "Medium vulnerabilities found: $MEDIUM_COUNT" | ||
|
|
||
| # List remaining critical, high and medium vulnerabilities for awareness | ||
| echo "Critical severity vulnerabilities:" | ||
| cat grype-results.json | jq -r '.matches[] | select(.vulnerability.severity == "Critical") | "- \(.artifact.name) \(.artifact.version): \(.vulnerability.id)"' | ||
|
|
||
| echo "High severity vulnerabilities:" | ||
| cat grype-results.json | jq -r '.matches[] | select(.vulnerability.severity == "High") | "- \(.artifact.name) \(.artifact.version): \(.vulnerability.id)"' | ||
|
|
||
| echo "Medium severity vulnerabilities:" | ||
| cat grype-results.json | jq -r '.matches[] | select(.vulnerability.severity == "Medium") | "- \(.artifact.name) \(.artifact.version): \(.vulnerability.id)"' | ||
|
|
||
| # Fail if any critical, high, or medium vulnerabilities are found | ||
| if [ "$CRITICAL_COUNT" -gt 0 ] || [ "$HIGH_COUNT" -gt 0 ] || [ "$MEDIUM_COUNT" -gt 0 ]; then | ||
| echo "Critical, High, or Medium vulnerabilities found. Failing the build." | ||
| exit 1 | ||
| fi | ||
| - store_artifacts: | ||
| path: grype-results.json | ||
| destination: grype-scan/scan-results.json | ||
| - store_artifacts: | ||
| path: grype-results.txt | ||
| destination: grype-scan/scan-results.txt |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.