generated from oracle/template-repo
-
Notifications
You must be signed in to change notification settings - Fork 30
feat: add --existing-policy flag in verify-policy command for example policies #1189
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
Open
Demolus13
wants to merge
6
commits into
oracle:main
Choose a base branch
from
Demolus13:pgovale/example-policies
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d8e6020
feat: add --policy flag in verify-policy command for example policies
Demolus13 cf53381
feat: add policy template modification for standardization
Demolus13 75e5c42
feat: add sample policies
Demolus13 a55e050
chore: improve handling of policy templates and verify packageurl
Demolus13 ec11626
refactor: change -p (--policy) to -e (--existing-policy) for verify-p…
Demolus13 ff9fff7
chore: add simple tests for existing-policy flag
Demolus13 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
8 changes: 8 additions & 0 deletions
8
src/macaron/resources/policies/datalog/check-github-actions.dl.template
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,8 @@ | ||
| #include "prelude.dl" | ||
|
|
||
| Policy("github_actions_vulns", component_id, "GitHub Actions Vulnerability Detection") :- | ||
| check_passed(component_id, "mcn_githubactions_vulnerabilities_1"). | ||
|
|
||
| apply_policy_to("github_actions_vulns", component_id) :- | ||
| is_component(component_id, purl), | ||
| match("<PACKAGE_PURL>@.*", purl). |
10 changes: 10 additions & 0 deletions
10
src/macaron/resources/policies/datalog/malware-detection-dependencies.dl.template
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,10 @@ | ||
| #include "prelude.dl" | ||
|
|
||
| Policy("check-dependencies", component_id, "Check the dependencies of component.") :- | ||
| transitive_dependency(component_id, dependency), | ||
| check_passed(component_id, "mcn_detect_malicious_metadata_1"), | ||
| check_passed(dependency, "mcn_detect_malicious_metadata_1"). | ||
|
|
||
| apply_policy_to("check-dependencies", component_id) :- | ||
| is_component(component_id, purl), | ||
| match("<PACKAGE_PURL>@.*", purl). | ||
9 changes: 9 additions & 0 deletions
9
src/macaron/resources/policies/datalog/malware-detection.dl.template
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,9 @@ | ||
| #include "prelude.dl" | ||
|
|
||
| Policy("check-component", component_id, "Check component artifacts.") :- | ||
| check_passed(component_id, "mcn_detect_malicious_metadata_1"). | ||
|
|
||
|
|
||
| apply_policy_to("check-component", component_id) :- | ||
| is_component(component_id, purl), | ||
| match("<PACKAGE_PURL>@.*", purl). |
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,61 @@ | ||
| # Copyright (c) 2025 - 2025, Oracle and/or its affiliates. All rights reserved. | ||
| # Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/. | ||
|
|
||
| """This module tests the existing-policy flag supported by the policy engine.""" | ||
|
|
||
| import argparse | ||
| import os | ||
| from pathlib import Path | ||
| from unittest.mock import MagicMock, patch | ||
|
|
||
| from macaron.__main__ import verify_policy | ||
|
|
||
|
|
||
| def test_verify_existing_policy_success(tmp_path: Path) -> None: | ||
| """When an existing policy is provided and package-url is valid, verify_policy returns EX_OK.""" | ||
| db_file = tmp_path / "macaron.db" | ||
| db_file.write_text("") | ||
|
|
||
| # Use a MagicMock for the handler. | ||
| mock_handler = MagicMock() | ||
|
|
||
| # Fake run_policy_engine and generate_vsa that returns a fixed result. | ||
| fake_run = MagicMock(return_value={"passed_policies": [["check-component"]], "failed_policies": []}) | ||
| fake_generate_vsa = MagicMock(return_value=None) | ||
|
|
||
| # Fake PolicyReporter class: when called, returns an instance with generate method. | ||
| fake_policy_reporter_cls = MagicMock() | ||
| fake_policy_reporter_inst = MagicMock() | ||
| fake_policy_reporter_inst.generate.return_value = None | ||
| fake_policy_reporter_cls.return_value = fake_policy_reporter_inst | ||
|
|
||
| with ( | ||
| patch("macaron.__main__.run_policy_engine", fake_run), | ||
| patch("macaron.__main__.generate_vsa", fake_generate_vsa), | ||
| patch("macaron.__main__.access_handler.get_handler", return_value=mock_handler), | ||
| patch("macaron.__main__.PolicyReporter", fake_policy_reporter_cls), | ||
| ): | ||
| policy_args = argparse.Namespace( | ||
| database=str(db_file), | ||
| show_prelude=False, | ||
| file=None, | ||
| existing_policy="malware-detection", | ||
| package_url="pkg:pypi/django", | ||
| ) | ||
| result = verify_policy(policy_args) | ||
| assert result == os.EX_OK | ||
|
|
||
|
|
||
| def test_verify_existing_policy_not_found(tmp_path: Path) -> None: | ||
| """Requesting a non-existent policy returns usage error.""" | ||
| db_file = tmp_path / "macaron.db" | ||
| db_file.write_text("") | ||
| policy_args = argparse.Namespace( | ||
| database=str(db_file), | ||
| show_prelude=False, | ||
| file=None, | ||
| existing_policy="no-such-policy", | ||
| package_url="pkg:pypi/django", | ||
| ) | ||
| result = verify_policy(policy_args) | ||
| assert result == os.EX_USAGE |
Oops, something went wrong.
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.
What if the user provides a
PURLthat contains version as well? Then the policy won't match thePURL.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.
Does removing
@.*resolve the issue?from
to
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.
Yes, but that would mean we can't use wildcards, which are useful for creating more generic policies that apply to all versions. Alternatively, we could allow the wildcard within the
PURLargument itself 🤔