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 predefined 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
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
db7ac94
feat: add --policy flag in verify-policy command for example policies
Demolus13 3126b92
feat: add policy template modification for standardization
Demolus13 ed94784
feat: add sample policies
Demolus13 c604c61
chore: improve handling of policy templates and verify packageurl
Demolus13 ec09564
refactor: change -p (--policy) to -e (--existing-policy) for verify-p…
Demolus13 a017638
chore: add simple tests for existing-policy flag
Demolus13 4b9f002
refactor: add new tutorial for existing-policy flag
Demolus13 1118df8
refactor: add --list-policies flag to check for available policies.
Demolus13 d31c0cb
refactor: add --list-policies flag to check for available policies.
Demolus13 311845e
chore: add descriptions for each existing policy and a test to verify…
Demolus13 0fd55ba
refactor: improve UI to show the available policies.
Demolus13 c349423
docs: update tutorial to show the use of wildcard.
Demolus13 94feadc
refactor: handle action_args for verify-policy.
Demolus13 9010582
refactor: improve list-policies UI.
Demolus13 2bf5a58
docs: correct usage in tutorial.
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
86 changes: 86 additions & 0 deletions
86
docs/source/pages/tutorials/verify_with_existing_policy.rst
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,86 @@ | ||
| =================================================================== | ||
| How to use the policy engine to verify with our predefined policies | ||
| =================================================================== | ||
|
|
||
| This tutorial shows how to use the ``--existing-policy`` flag with the ``verify-policy`` subcommand to run one of the predefined policies that ship with Macaron. | ||
|
|
||
| -------- | ||
| Use case | ||
| -------- | ||
|
|
||
| Use ``--existing-policy`` when you want to run one of the built-in policies by name instead of providing a local policy file with ``--file``. Pre-defined policies are useful for quick checks or automated examples/tests. | ||
|
|
||
| ------- | ||
| Example | ||
| ------- | ||
|
|
||
| Run the ``malware-detection`` policy against a package URL: | ||
|
|
||
| .. code-block:: shell | ||
|
|
||
| ./run_macaron.sh analyze -purl pkg:pypi/[email protected] | ||
|
|
||
| .. note:: By default, Macaron clones the repositories and creates output files under the ``output`` directory. To understand the structure of this directory please see :ref:`Output Files Guide <output_files_guide>`. | ||
|
|
||
| .. code-block:: shell | ||
|
|
||
| ./run_macaron.sh verify-policy \ | ||
| --database output/macaron.db \ | ||
| --existing-policy malware-detection \ | ||
| --package-url "pkg:pypi/[email protected]" | ||
|
|
||
| The result of this command should show that the policy succeeds with a zero exit code (if a policy fails to pass, Macaron returns a none-zero error code): | ||
|
|
||
| .. code-block:: shell | ||
|
|
||
| Components Satisfy Policy | ||
| 1 pkg:pypi/[email protected] check-component | ||
|
|
||
| Components Violate Policy None | ||
|
|
||
| Passed Policies check-component | ||
| Failed Policies None | ||
| Policy Report output/policy_report.json | ||
| Verification Summary Attestation output/vsa.intoto.jsonl | ||
| Decode and Inspect the Content cat output/vsa.intoto.jsonl | jq -r '.payload' | base64 -d | jq | ||
|
|
||
| Run the ``malware-detection`` policy using wildcard: | ||
|
|
||
| .. code-block:: shell | ||
|
|
||
| ./run_macaron.sh analyze -purl pkg:pypi/[email protected] | ||
| ./run_macaron.sh analyze -purl pkg:pypi/[email protected] | ||
|
|
||
| .. note:: By default, Macaron clones the repositories and creates output files under the ``output`` directory. To understand the structure of this directory please see :ref:`Output Files Guide <output_files_guide>`. | ||
|
|
||
| .. code-block:: shell | ||
|
|
||
| ./run_macaron.sh verify-policy \ | ||
| --database output/macaron.db \ | ||
| --existing-policy malware-detection \ | ||
| --package-url "pkg:pypi/django@.*" | ||
|
|
||
| It uses the wildcard '*' to checks for components satisfying the expression "pkg:pypi/django@.*". | ||
| The result of this command should show that the policy succeeds with a zero exit code (if a policy fails to pass, Macaron returns a none-zero error code): | ||
|
|
||
| .. code-block:: shell | ||
|
|
||
| Components Satisfy Policy | ||
| 1 pkg:pypi/[email protected] check-component | ||
| 2 pkg:pypi/[email protected] check-component | ||
|
|
||
| Components Violate Policy None | ||
|
|
||
| Passed Policies check-component | ||
| Failed Policies None | ||
| Policy Report output/policy_report.json | ||
| Verification Summary Attestation output/vsa.intoto.jsonl | ||
| Decode and Inspect the Content cat output/vsa.intoto.jsonl | jq -r '.payload' | base64 -d | jq | ||
|
|
||
| ----------------- | ||
| Related tutorials | ||
| ----------------- | ||
|
|
||
| - :doc:`detect_malicious_package` — shows what the malware-detection policy does in this tutorial. | ||
| - :doc:`use_verification_summary_attestation` — how to consume an attestation | ||
| produced by Macaron. |
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,12 @@ | ||
| Policies | ||
| ======= | ||
|
|
||
| This directory contains policy resources used by Macaron. Policies in this folder are packaged as templates that the verify-policy command can use. | ||
|
|
||
| Common files and conventions | ||
| --------------------------- | ||
| - `*.dl.template` - datalog policy templates. | ||
| - `*.description` - short descriptions that explain the policy's intent. | ||
| - `*.cue.template` - CUE-based expectation templates used by the GDK. | ||
|
|
||
| Example policies are exposed to the user via Macaron commands `verify-policy --existing-policy <policy-name>`. |
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,13 @@ | ||
| Datalog policy templates | ||
| ========================= | ||
|
|
||
| - This folder contains Datalog-based policy templates and accompanying `.description` files used by Macaron's policy engine. | ||
|
|
||
| - These `.dl.template` templates are intended as examples and starting points. They can be used by name using `--existing-policy` flag. | ||
|
|
||
| - `*.description` - descriptions for each template. These are intended to be shown in UIs or documentation to help users choose an appropriate example policy. | ||
|
|
||
| Extending or adding templates | ||
| ----------------------------- | ||
| - Add a new `.dl.template` file and a matching `.description` file. | ||
| - Update documentation or the tutorials page if you add new example policies that should be exposed to users. |
1 change: 1 addition & 0 deletions
1
src/macaron/resources/policies/datalog/check-github-actions.description
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 @@ | ||
| Detects whether a component was built using GitHub Actions that are known to be vulnerable or otherwise unsafe. The policy evaluates a check named `mcn_githubactions_vulnerabilities_1` and reports a passed/failed result for the component when applied. |
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). |
1 change: 1 addition & 0 deletions
1
src/macaron/resources/policies/datalog/malware-detection-dependencies.description
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 @@ | ||
| Checks the component and its transitive dependencies for indicators of malicious or suspicious content. The policy ensures the component and each dependency pass the `mcn_detect_malicious_metadata_1` check. |
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). |
1 change: 1 addition & 0 deletions
1
src/macaron/resources/policies/datalog/malware-detection.description
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 @@ | ||
| Checks a component for indicators of malicious or suspicious content. The policy evaluates a check named mcn_detect_malicious_metadata_1 and reports a passed/failed result for the component when applied. |
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,24 @@ | ||
| # 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/. | ||
|
|
||
| """Tests that every Datalog template has a matching .description file.""" | ||
|
|
||
| from pathlib import Path | ||
|
|
||
| import macaron | ||
|
|
||
|
|
||
| def test_datalog_templates_have_descriptions() -> None: | ||
| """Verify each ``*.dl.template`` has a corresponding ``*.description``.""" | ||
| datalog_dir = Path(macaron.__file__).resolve().parent.joinpath("resources", "policies", "datalog") | ||
| templates = sorted(datalog_dir.glob("*.dl.template")) | ||
|
|
||
| missing = [] | ||
| for tmpl in templates: | ||
| expected_desc = datalog_dir.joinpath(tmpl.name.replace(".dl.template", ".description")) | ||
| if not expected_desc.exists(): | ||
| missing.append((tmpl.name, expected_desc)) | ||
|
|
||
| if templates and missing: | ||
| missing_list = ", ".join(f"{t} -> {d}" for t, d in missing) | ||
| raise AssertionError("Missing .description files for the following templates: " + missing_list) |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.