Skip to content

Commit 145c2ac

Browse files
author
Trong Nhan Mai
committed
chore: update sphinx documentation about the feature
Signed-off-by: Trong Nhan Mai <[email protected]>
1 parent a21086a commit 145c2ac

File tree

7 files changed

+158
-13
lines changed

7 files changed

+158
-13
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/* Copyright (c) 2023 - 2023, Oracle and/or its affiliates. All rights reserved. */
2+
/* Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/. */
3+
4+
5+
#include "prelude.dl"
6+
7+
/**
8+
* This is an example policy for OCI Micronaut project using PURL as the identifier.
9+
* See:
10+
* https://github.com/oracle-quickstart/oci-micronaut
11+
* https://github.com/package-url/purl-spec
12+
*/
13+
14+
Policy("oci_micronaut_dependencies", parent, "") :-
15+
check_passed(parent, "mcn_build_service_1"),
16+
!violating_dependencies(parent, "mcn_build_service_1"), // There should not be any violating dependencies.
17+
verify_provenance(dependency, "micronaut-projects/micronaut-core").
18+
19+
// Projects that violate an expected check result.
20+
.decl violating_dependencies(parent: number, property: symbol)
21+
violating_dependencies(parent, property) :-
22+
is_check(property),
23+
transitive_dependency(parent, dependency), // note that since macaron by default does not traverse
24+
// to transitive, dependencies, in most cases this is
25+
// identical to `dependency(parent, repo)`.
26+
!check_passed(dependency, property),
27+
!exception_dependencies(dependency).
28+
29+
// Exceptions for violating dependencies.
30+
.decl exception_dependencies(dependency: number)
31+
exception_dependencies(dependency) :-
32+
is_repo(dependency, "github.com/mapstruct/mapstruct", _).
33+
34+
exception_dependencies(dependency) :-
35+
is_repo(dependency, "github.com/mysql/mysql-connector-j", _).
36+
37+
exception_dependencies(dependency) :-
38+
is_repo(dependency, "github.com/aws/aws-msk-iam-auth", _).
39+
40+
exception_dependencies(dependency) :-
41+
is_repo(dependency, "github.com/h2database/h2database", _).
42+
43+
// Projects that we expect to generate a provenance.
44+
.decl verify_provenance(repo_num: number, repo_name: symbol)
45+
verify_provenance(repo_num, repo_name) :-
46+
is_repo(repo_num, repo_name, _),
47+
check_passed(repo_num, "mcn_provenance_level_three_1"),
48+
check_passed(repo_num, "mcn_provenance_expectation_1").
49+
50+
// Apply the policy.
51+
apply_policy_to("oci_micronaut_dependencies", component_id) :- is_component(component_id, "<target_software_component_purl>").
File renamed without changes.

docs/source/conf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"sphinx.ext.autosectionlabel",
3636
"sphinx_autodoc_typehints",
3737
"numpydoc",
38+
"sphinx_tabs.tabs",
3839
]
3940
autosectionlabel_prefix_document = True
4041
autosectionlabel_maxdepth = 2

docs/source/pages/cli_usage/action_analyze.rst

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Usage
2020
.. code-block:: shell
2121
2222
usage: ./run_macaron.sh analyze
23-
[-h] [-sbom SBOM_PATH] [-rp REPO_PATH] [-b BRANCH]
23+
[-h] [-sbom SBOM_PATH] [-purl PURL] [-rp REPO_PATH] [-b BRANCH]
2424
[-d DIGEST] [-pe PROVENANCE_EXPECTATION] [-c CONFIG_PATH]
2525
[--skip-deps] [-g TEMPLATE_PATH]
2626
@@ -36,11 +36,14 @@ Options
3636

3737
The path to the SBOM of the analysis target.
3838

39+
.. option:: -purl PACKAGE_URL, --package-url PACKAGE_URL
40+
41+
The PURL string as the unique identifier of the analysis target.
42+
3943
.. option:: -rp REPO_PATH, --repo-path REPO_PATH
4044

4145
The path to the repository, can be local or remote
4246

43-
4447
.. option:: -b BRANCH, --branch BRANCH
4548

4649
The branch of the repository that we want to checkout. If not set, Macaron will use the default branch

docs/source/pages/output_files.rst

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,35 @@ The report files of Macaron (from using the :ref:`analyze action <analyze-action
4141
Unique result path
4242
''''''''''''''''''
4343

44-
For each target repository, Macaron creates a directory under ``reports`` to store the report files. This directory
45-
path is formed from the git host name (e.g ``github.com``), the owner and the name of that
46-
repository. The final path is created using the following template:
44+
For each target software component, Macaron creates a directory under ``reports`` to store the report files. This directory
45+
path is formed from the PURL string of that component. The final path is created using the following template:
4746

4847
.. code-block::
4948
50-
<path_to_output>/reports/<git_service_name>/<owner>/<repo_name>
49+
<path_to_output>/reports/<purl_type>/<purl_namespace>/<purl_name>
5150
52-
.. note:: The git host name has all occurrence of ``.`` in the URL replaced by ``_``.
51+
For more information on the three fields ``type``, ``namespace`` and ``name`` of a PURL string, please see
52+
`PURL Specification <https://github.com/package-url/purl-spec/blob/master/PURL-SPECIFICATION.rst>`_.
5353

54-
For example, the reports for `<https://github.com/micronaut-projects/micronaut-core>`_ repository will be stored under
54+
Typically, when a repository path is provided as the main software component of the :ref:`analyze action <analyze-action-cli>`,
55+
a PURL is generated from the repository path, which is then later used in generating the unique report path.
56+
57+
For example, when run this command:
58+
59+
.. code-block::
60+
61+
./run_macaron.sh analyze -rp https://github.com/micronaut-projects/micronaut-core
62+
63+
The report files will be stored into:
64+
65+
.. code-block::
66+
67+
<path_to_ouput>/reports/github_com/micronaut-projects/micronaut-core
68+
69+
.. note:: In the unique path, only ASCII letters, digits and ``-`` are allowed. Prohibited characters are changed into
70+
``_``. No changes to the letter case are made.
71+
72+
For example, the reports for `<https://github.com/micronaut-projects/micronaut-core>`_ will be stored under
5573
``<path_to_output>/reports/github_com/micronaut-projects/micronaut-core``.
5674

5775
''''''''''''

docs/source/pages/using.rst

Lines changed: 76 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,50 @@ To analyze a repository on a self-hosted GitLab instance, you need to do the fol
9090
9191
- Obtain a GitLab access token having at least the ``read_repository`` permission and store it into the ``MCN_SELF_HOSTED_GITLAB_TOKEN`` environment variable. For more detailed instructions, see `GitLab documentation <https://docs.gitlab.com/ee/user/profile/personal_access_tokens.html#create-a-personal-access-token>`_.
9292

93+
''''''''''''''''''''''''''''''''''''''''''''''''''''
94+
Providing a PURL string instead of a repository path
95+
''''''''''''''''''''''''''''''''''''''''''''''''''''
96+
97+
Instead of providing the repository path for the analysis software component, a `PURL <https://github.com/package-url/purl-spec/blob/master/PURL-SPECIFICATION.rst>`_. string
98+
for the target git repository could be used.
99+
100+
To begin with, we follow the same steps for the configuration if needed (especially for self-hosted GitLab instances). The PURL string for a git repository could be created using the following format:
101+
102+
.. code-block::
103+
104+
pkg:<git_service_domain>/<organization>/<name>
105+
106+
The list bellow shows examples for the corresponding PURL string for different git repositories:
107+
108+
.. list-table:: Example of PURL strings for git repositories.
109+
:widths: 50 50
110+
:header-rows: 1
111+
112+
* - Repository path
113+
- PURL string
114+
* - ``https://github.com/micronaut-projects/micronaut-core``
115+
- Both ``pkg:github/micronaut-projects/micronaut-core`` and ``pkg:github.com/micronaut-projects/micronaut-core`` are applicable as ``github`` is a pre-defined type as mentioned `here <https://github.com/package-url/purl-spec/blob/master/PURL-TYPES.rst>`_.
116+
* - ``https://bitbucket.org/snakeyaml/snakeyaml``
117+
- Both ``pkg:github/micronaut-projects/micronaut-core`` and ``pkg:github.com/micronaut-projects/micronaut-core`` are applicable as ``bitbucket`` is a pre-defined type as mentioned `here <https://github.com/package-url/purl-spec/blob/master/PURL-TYPES.rst>`_.
118+
* - ``https://internal.gitlab.com/foo/bar``
119+
- ``pkg:internal.gitlab.com/foo/bar``
120+
* - ``https://gitlab.com/gitlab-org/gitlab``
121+
- ``pkg:gitlab.com/gitlab-org/gitlab``
122+
123+
After the PURL string has been obtained, the analysis could be run with:
124+
125+
.. code-block:: shell
126+
127+
./run_macaron.sh analyze -purl <purl_string>
128+
129+
You can also provide the PURL string together with the repository path. In this case, the PURL string will be used as the unique identifier to the analysis target:
130+
131+
.. code-block:: shell
132+
133+
./run_macaron.sh analyze -purl <purl_string> -rp <repo_path> -b <branch> -d <digest>
134+
135+
.. note:: When provide the PURL and the repository path, both branch name and commit digest must be provided as well.
136+
93137
-------------------------------------------------
94138
Verifying provenance expectations in CUE language
95139
-------------------------------------------------
@@ -218,8 +262,13 @@ Running the policy engine
218262
Macaron's policy engine accepts policies specified in `Datalog <https://en.wikipedia.org/wiki/Datalog>`_. An example policy
219263
can verify if a project and all its dependencies pass certain checks. We use `Soufflé <https://souffle-lang.github.io/index.html>`_
220264
as the Datalog engine in Macaron. Once you run the checks on a target project as described :ref:`here <analyze-action>`,
221-
the check results will be stored in ``macaron.db`` in the output directory. We can pass the check results to the policy
222-
engine and provide a Datalog policy file to be enforced by the policy engine.
265+
the check results will be stored in ``macaron.db`` in the output directory. Because the check results of different software component can be stored into the database at ``macaron.db``, we must specify the target software component in the Datalog policy file to be enforced by the policy engine. There are two ways that we could
266+
specify the target software component in the Datalog policy file:
267+
268+
#. Using the complete name of the target component (e.g. ``github.com/oracle-quickstart/oci-micronaut``)
269+
#. Using the PURL string of the target component (e.g. ``pkg:github.com/oracle-quickstart/oci-micronaut@<commit_sha>``).
270+
271+
However, the two methods would mostly share the same policy definition in the Datalog files.
223272

224273
We use `Micronaut MuShop <https://github.com/oracle-quickstart/oci-micronaut>`_ project as a case study to show how to run the policy engine.
225274
Micronaut MuShop is a cloud-native microservices example for Oracle Cloud Infrastructure. When we run Macaron on the Micronaut MuShop GitHub
@@ -233,13 +282,35 @@ Now we can run the policy engine over these results and enforce a policy:
233282

234283
.. code-block:: shell
235284
236-
./run_macaron.sh verify-policy -o outputs -d outputs/macaron.db --file oci-micronaut.dl
285+
./run_macaron.sh verify-policy -o outputs -d outputs/macaron.db --file <policy_file>
286+
287+
In this example, the Datalog policy files for both ways (as mentioned previously) are provided in `oci-micronaut-repo.dl <../_static/examples/oracle-quickstart/oci-micronaut/policies/oci-micronaut-repo.dl>`__ and `oci-micronaut-purl.dl <../_static/examples/oracle-quickstart/oci-micronaut/policies/oci-micronaut-purl.dl>`__.
288+
289+
The differences between the two policy files can be observed below:
290+
291+
.. tabs::
292+
293+
.. code-tab:: prolog Using repository complete name
294+
295+
apply_policy_to("oci_micronaut_dependencies", repo_id) :- is_repo(repo_id, "github.com/oracle-quickstart/oci-micronaut", _).
296+
297+
.. code-tab:: prolog Using PURL string
298+
299+
apply_policy_to("oci_micronaut_dependencies", component_id) :- is_component(component_id, "<target_software_component_purl>").
300+
301+
The PURL string for the target software component could be obtained when the :ref:`analyze command <analyze-action>` finishes. For example:
302+
303+
.. code:: shell
304+
305+
> ./run_macaron.sh analyze -rp https://github.com/oracle-quickstart/oci-micronaut
306+
> ...
307+
> 2023-08-15 14:36:56,672 [INFO] PURL of the main target software component is 'pkg:github.com/oracle-quickstart/oci-micronaut@3ebe0c9520a25feeae983eac6eb956de7da29ead'.
308+
> 2023-08-15 14:36:56,672 [INFO] Analysis Completed!
237309
238-
In this example, the Datalog policy file is provided in `oci-micronaut.dl <../_static/examples/oracle-quickstart/oci-micronaut/policies/oci-micronaut.dl>`__.
239310
This example policy can verify if the Micronaut MuShop project and all its dependencies pass the ``build_service`` check
240311
and the Micronaut provenance documents meets the expectation provided as a `CUE file <../_static/examples/micronaut-projects/micronaut-core/policies/micronaut-core.cue>`__.
241312

242-
Thanks to Datalogs expressive language model, its easy to add exception rules if certain dependencies do not meet a
313+
Thanks to Datalog's expressive language model, it's easy to add exception rules if certain dependencies do not meet a
243314
requirement. For example, `the Mysql Connector/J <https://slsa.dev/spec/v0.1/requirements#build-service>`_ dependency in
244315
the Micronaut MuShop project does not pass the ``build_service`` check, but can be manually investigated and exempted if trusted. Overall, policies expressed in Datalog can be
245316
enforced by Macaron as part of your CI/CD pipeline to detect regressions or unexpected behavior.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ docs = [
7575
"sphinx-autodoc-typehints >=1.19.4,<2.0.0",
7676
"sphinx-rtd-theme >=1.0.0,<2.0.0",
7777
"numpydoc >=1.5.0,<2.0.0",
78+
"sphinx_tabs >=3.4.1,<4.0.0",
7879
]
7980
hooks = [
8081
"pre-commit >=3.0.0,<3.4.0",

0 commit comments

Comments
 (0)