-
-
Notifications
You must be signed in to change notification settings - Fork 127
Add compliance support based on OpenSSF Scorecard score #1800
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 6 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
9a1ffa0
Add compliance_thresholds.py and related support files
NucleonGodX f88a57d
fix tests
NucleonGodX e63a9ab
integrate scorecard compliance
NucleonGodX c6b591b
fix tests
NucleonGodX efa0f4b
minor fix
NucleonGodX 5ab30e7
merge main
NucleonGodX 8c9dc8f
create a new pipe for compliancing
NucleonGodX dd1d7d5
fix code format
NucleonGodX 8b83b30
add documentation and changelog
NucleonGodX 7d7323f
add changelog
NucleonGodX 7d657fe
add test for integration of scorecard-compliance
NucleonGodX 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 |
|---|---|---|
|
|
@@ -25,6 +25,7 @@ | |
|
|
||
| from scanpipe.models import DiscoveredPackageScore | ||
| from scanpipe.pipelines import Pipeline | ||
| from scanpipe.pipes.compliance_thresholds import get_project_scorecard_thresholds | ||
|
|
||
|
|
||
| class FetchScores(Pipeline): | ||
|
|
@@ -58,9 +59,26 @@ def check_scorecode_service_availability(self): | |
|
|
||
| def fetch_packages_scorecode_info(self): | ||
| """Fetch ScoreCode information for each of the project's discovered packages.""" | ||
| scorecard_policy = get_project_scorecard_thresholds(self.project) | ||
| worst_alert = None | ||
|
|
||
| for package in self.project.discoveredpackages.all(): | ||
| if scorecard_data := ossf_scorecard.fetch_scorecard_info(package=package): | ||
| DiscoveredPackageScore.create_from_package_and_scorecard( | ||
| scorecard_data=scorecard_data, | ||
| package=package, | ||
| ) | ||
|
|
||
| if scorecard_policy and scorecard_data.score is not None: | ||
| try: | ||
| score = float(scorecard_data.score) | ||
| alert = scorecard_policy.get_alert_for_score(score) | ||
| except Exception: | ||
| alert = "error" | ||
|
|
||
| order = {"ok": 0, "warning": 1, "error": 2} | ||
| if worst_alert is None or order[alert] > order.get(worst_alert, -1): | ||
| worst_alert = alert | ||
|
|
||
| if worst_alert is not None: | ||
| self.project.update_extra_data({"scorecard_compliance_alert": worst_alert}) | ||
|
||
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 |
|---|---|---|
|
|
@@ -1252,6 +1252,49 @@ def test_scanpipe_management_command_check_both_compliance_and_clarity(self): | |
| ) | ||
| self.assertEqual(expected, out_value) | ||
|
|
||
| def test_scanpipe_management_command_check_scorecard_compliance_only(self): | ||
| project = make_project(name="my_project_scorecard") | ||
|
|
||
| project.extra_data = {"scorecard_compliance_alert": "error"} | ||
| project.save(update_fields=["extra_data"]) | ||
|
|
||
| out = StringIO() | ||
| options = ["--project", project.name] | ||
| with self.assertRaises(SystemExit) as cm: | ||
| call_command("check-compliance", options, stderr=out) | ||
| self.assertEqual(cm.exception.code, 1) | ||
| out_value = out.getvalue().strip() | ||
| expected = "1 compliance issues detected.\n[scorecard compliance]\n > ERROR" | ||
| self.assertEqual(expected, out_value) | ||
|
|
||
| def test_scanpipe_management_command_check_all_compliance_types(self): | ||
| project = make_project(name="my_project_all") | ||
|
|
||
| make_package( | ||
| project, | ||
| package_url="pkg:generic/[email protected]", | ||
| compliance_alert=CodebaseResource.Compliance.ERROR, | ||
| ) | ||
| project.extra_data = { | ||
| "license_clarity_compliance_alert": "warning", | ||
| "scorecard_compliance_alert": "error", | ||
| } | ||
| project.save(update_fields=["extra_data"]) | ||
|
|
||
| out = StringIO() | ||
| options = ["--project", project.name, "--fail-level", "WARNING"] | ||
| with self.assertRaises(SystemExit) as cm: | ||
| call_command("check-compliance", options, stderr=out) | ||
| self.assertEqual(cm.exception.code, 1) | ||
| out_value = out.getvalue().strip() | ||
| expected = ( | ||
| "3 compliance issues detected." | ||
| "\n[packages]\n > ERROR: 1" | ||
| "\n[license clarity]\n > WARNING" | ||
| "\n[scorecard compliance]\n > ERROR" | ||
| ) | ||
| self.assertEqual(expected, out_value) | ||
|
|
||
| def test_scanpipe_management_command_check_compliance_vulnerabilities(self): | ||
| project = make_project(name="my_project") | ||
| package1 = make_package(project, package_url="pkg:generic/[email protected]") | ||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs a new entry in the REST API documentation, see
_rest_api_license_clarity_compliance