Skip to content

Commit 8fa21a7

Browse files
authored
Project policies #386 (#1440)
Signed-off-by: tdruez <tdruez@nexb.com>
1 parent a7c1bf5 commit 8fa21a7

27 files changed

+484
-170
lines changed

CHANGELOG.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ v34.9.0 (unreleased)
1313
- Refactor the policies related code to its own module.
1414
https://github.com/aboutcode-org/scancode.io/issues/386
1515

16+
- Add support for project-specific license policies and compliance alerts.
17+
Enhance Project model to handle policies from local settings, project input
18+
"policies.yml" files, or global app settings.
19+
https://github.com/aboutcode-org/scancode.io/issues/386
20+
1621
v34.8.3 (2024-10-30)
1722
--------------------
1823

docs/command-line-interface.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,9 @@ your outputs on the host machine when running with Docker.
285285
.. tip:: To specify a CycloneDX spec version (default to latest), use the syntax
286286
``cyclonedx:VERSION`` as format value. For example: ``--format cyclonedx:1.5``.
287287

288+
289+
.. _cli_check_compliance:
290+
288291
`$ scanpipe check-compliance --project PROJECT`
289292
-----------------------------------------------
290293

docs/faq.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,3 +278,8 @@ data older than 7 days::
278278

279279
See :ref:`command_line_interface` chapter for more information about the scanpipe
280280
command.
281+
282+
How can I provide my license policies ?
283+
---------------------------------------
284+
285+
For detailed information about the policies system, refer to :ref:`policies`.

docs/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ you’ll find information on:
4444
custom-pipelines
4545
scanpipe-pipes
4646
project-configuration
47+
policies
4748
data-models
4849
output-files
4950
command-line-interface

docs/policies.rst

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
.. _policies:
2+
3+
License Policies and Compliance Alerts
4+
======================================
5+
6+
ScanCode.io enables you to define **license policies** that check your projects
7+
against a **compliance system**.
8+
9+
Creating Policies Files
10+
-----------------------
11+
12+
A valid policies file is required to **enable compliance-related features**.
13+
14+
The policies file, by default named ``policies.yml``, is a **YAML file** with a
15+
structure similar to the following:
16+
17+
.. code-block:: yaml
18+
19+
license_policies:
20+
- license_key: mit
21+
label: Approved License
22+
compliance_alert: ''
23+
- license_key: mpl-2.0
24+
label: Restricted License
25+
compliance_alert: warning
26+
- license_key: gpl-3.0
27+
label: Prohibited License
28+
compliance_alert: error
29+
30+
- In the example above, licenses are referenced by the ``license_key``,
31+
such as `mit` and `gpl-3.0`, which represent the ScanCode license keys used to
32+
match against licenses detected in scan results.
33+
- Each policy is defined with a ``label`` and a ``compliance_alert``.
34+
You can customize the labels as desired.
35+
- The ``compliance_alert`` field accepts three values:
36+
37+
- ``''`` (empty string)
38+
- ``warning``
39+
- ``error``
40+
41+
App Policies
42+
------------
43+
44+
Policies can be enabled for the entire ScanCode.io app instance or on a per-project
45+
basis.
46+
47+
By default, ScanCode.io will look for a ``policies.yml`` file in the root of its
48+
application codebase.
49+
50+
Alternatively, you can specify the location of your policies file in your ``.env`` file
51+
using the :ref:`scancodeio_settings_policies_file` setting.
52+
53+
If a policies file is found at this location, those policies will be applied to
54+
all projects in the ScanCode.io instance.
55+
56+
.. tip::
57+
Refer to the :ref:`scancodeio_settings` section for a full list of settings,
58+
including the policies file setting.
59+
60+
Per-Project Policies
61+
--------------------
62+
63+
Project-specific policies can be provided via a ``policies.yml`` file as one of the
64+
project inputs or by defining the ``policies`` value in the
65+
:ref:`project_configuration`.
66+
67+
Compliance Alerts Ranking
68+
-------------------------
69+
70+
The compliance system uses a ``Precedence of Policies`` principle, which ensures the
71+
highest-priority policy is applied in cases where resources or packages have complex
72+
license expressions:
73+
74+
- **error > warning > missing > '' (empty string)**
75+
76+
This principle means that if a resource has an ``error``, ``warning``, and ``''``
77+
in its license expression, the overall compliance alert for that resource would be
78+
``error``.
79+
80+
.. warning::
81+
The ``missing`` compliance alert value is applied for licenses not included in the
82+
policies file.
83+
84+
Web UI
85+
------
86+
87+
Compliance alerts are shown directly in the Web user interface in the following
88+
locations:
89+
90+
* A summary panel in the project detail view:
91+
92+
.. image:: images/tutorial-policies-compliance-alerts-panel.png
93+
94+
* A dedicated column in the Packages and Resources list tables:
95+
96+
.. image:: images/tutorial-policies-compliance-alerts-column.png
97+
98+
REST API
99+
--------
100+
101+
For more details on retrieving compliance data through the REST API, see the
102+
:ref:`rest_api_compliance` section.
103+
104+
Command Line Interface
105+
----------------------
106+
107+
A dedicated ``check-compliance`` management command is available. See the
108+
:ref:`cli_check_compliance` section for more information.

docs/project-configuration.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,3 +163,15 @@ You can provide ``VCID`` from VulnerableCode or any aliases such as ``CVE`` or
163163
- OSV-2020-871
164164
- BIT-django-2024-24680
165165
- PYSEC-2024-28
166+
167+
.. _project_configuration_settings_policies:
168+
169+
policies
170+
^^^^^^^^
171+
172+
For detailed information about the policies system, refer to :ref:`policies`.
173+
174+
Instead of providing a separate ``policies.yml`` file, policies can be directly
175+
defined within the project configuration.
176+
This can be done through the web UI, see :ref:`user_interface_project_settings`,
177+
or by using a ``scancode-config.yml`` file.

docs/tutorial_license_policies.rst

Lines changed: 38 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -3,111 +3,53 @@
33
License Policies and Compliance Alerts
44
======================================
55

6-
In this tutorial, we'll introduce ScanCode.io's **license policies** and **compliance
7-
alerts** system and use the **results of a pipeline run** to demonstrate an example
8-
of the license policies and compliance alerts output.
6+
In this tutorial, we'll introduce ScanCode.io's **license policies** and
7+
**compliance alerts** system and use the **results of a pipeline run** to demonstrate
8+
an example of the license policies and compliance alerts output.
99

10-
As already mentioned, ScanCode.io automates the process of **Software Composition
11-
Analysis "SCA"** to identify existing open source components and their license
12-
compliance data in an application's codebase.
10+
As already mentioned, ScanCode.io automates the process of
11+
**Software Composition Analysis "SCA"** to identify existing open source components
12+
and their license compliance data in an application's codebase.
1313

1414
ScanCode.io also gives users the ability to define a set of **license policies** to
1515
have their projects checked against with a **compliance system**.
1616

17-
Creating Policies Files
18-
-----------------------
17+
Refer to :ref:`policies` for details about the policies system.
1918

20-
A valid policies file is required to **enable compliance-related features**.
19+
Instructions
20+
------------
2121

22-
The policies file, by default ``policies.yml``, is a **YAML file** with a structure
23-
similar to the following:
22+
Create a ``policies.yml`` file with the following content:
2423

2524
.. code-block:: yaml
2625
2726
license_policies:
2827
- license_key: mit
2928
label: Approved License
3029
compliance_alert: ''
31-
- license_key: mpl-2.0
32-
label: Restricted License
33-
compliance_alert: warning
3430
- license_key: gpl-3.0
3531
label: Prohibited License
3632
compliance_alert: error
3733
38-
- In the above policies file, licenses are referenced by the ``license_key``,
39-
such as mit and gpl-3.0, which represents the ScanCode license key to match
40-
against detected licenses in the scan results.
41-
- A policy is defined with a ``label`` and a ``compliance_alert``.
42-
The labels can be customized to your preferred wording.
43-
- The ``compliance_alert`` accepts 3 values:
44-
45-
- ``''`` (empty string)
46-
- ``warning``
47-
- ``error``
48-
49-
Policies File Location
50-
----------------------
51-
52-
By default, ScanCode.io will look for a ``policies.yml`` file at the root of its
53-
app codebase.
54-
55-
Alternatively, you can configure the location of policies files using the
56-
dedicated :ref:`scancodeio_settings_policies_file` setting in your ``.env`` file.
57-
58-
.. tip::
59-
Check out our :ref:`scancodeio_settings` section for a comprehensive list of
60-
settings including policies file setting.
61-
62-
How Does The Compliance Alert Work?
63-
-----------------------------------
64-
65-
The compliance system works by following a ``Precedence of Policies`` principal
66-
allowing the highest precedence policy to be applied in case of resources or
67-
packages with complex license expressions:
68-
69-
- **error > warning > missing > '' (empty string)**
70-
71-
This principal means a given resource with ``error AND warning AND ''``
72-
license expression would have an overall compliance alert of ``error``.
73-
74-
.. warning::
75-
The ``missing`` compliance alert value is applied for licenses not present in the
76-
policies file.
77-
78-
Example Output
79-
--------------
80-
81-
Create a ``policies.yml`` file in the root directory of your ScanCode.io codebase, with
82-
the following content:
83-
84-
.. code-block:: yaml
85-
86-
license_policies:
87-
- license_key: mit
88-
label: Approved License
89-
compliance_alert: ''
90-
- license_key: gpl-3.0
91-
label: Prohibited License
92-
compliance_alert: error
93-
94-
Run the following command to create a project and run the ``scan_codebase`` pipeline:
34+
Run the following command to create a project and run the ``scan_codebase`` pipeline
35+
(make sure to use the proper path for the policies.yml file):
9536

9637
.. code-block:: bash
9738
9839
$ scanpipe create-project cuckoo-filter-with-policies \
9940
--input-url https://files.pythonhosted.org/packages/75/fc/f5b2e466d763dcc381d5127b73ffc265e8cdaf39ddafa422b7896e625432/cuckoo_filter-1.0.6.tar.gz \
41+
--input-file policies.yml \
10042
--pipeline scan_codebase \
10143
--execute
10244
10345
Generate results:
10446

10547
.. code-block:: bash
10648
107-
$ scanpipe output --project cuckoo-filter-with-policies
49+
$ scanpipe output --print --project cuckoo-filter-with-policies
10850
10951
The computed compliance alerts are now included in the results, available for each
110-
detected licenses, and computed at the codebase resource level, for example:
52+
detected license, and computed at the codebase resource level, for example:
11153

11254
.. code-block:: json
11355
@@ -123,38 +65,46 @@ detected licenses, and computed at the codebase resource level, for example:
12365
"label": "Recommended License",
12466
"compliance_alert": ""
12567
},
126-
}
68+
},
12769
{
12870
"key": "gpl-3.0",
12971
"name": "GNU General Public License 3.0",
13072
"policy": {
13173
"label": "Prohibited License",
13274
"compliance_alert": "error"
133-
},
75+
}
76+
}
13477
],
13578
"license_expressions": [
136-
"mit OR gpl-3.0",
79+
"mit OR gpl-3.0"
13780
],
13881
"status": "scanned",
13982
"name": "README",
14083
"[...]": "[...]"
14184
}
14285
143-
Web UI
144-
------
86+
Run the ``check-compliance`` command
87+
------------------------------------
14588

146-
Compliance alerts are visible directly in the Web user interface through the following:
89+
Run the ``check-compliance`` command to get a listing of the compliance alerts detected
90+
in the project:
14791

148-
* A summary panel in the project detail view:
149-
150-
.. image:: images/tutorial-policies-compliance-alerts-panel.png
92+
.. code-block:: bash
15193
152-
* A dedicated column within the Packages and Resources list tables:
94+
$ scanpipe check-compliance --project cuckoo-filter-with-policies --verbosity 2
15395
154-
.. image:: images/tutorial-policies-compliance-alerts-column.png
96+
.. code-block:: bash
15597
156-
REST API
157-
--------
98+
4 compliance issues detected on this project.
99+
[packages]
100+
> ERROR: 3
101+
pkg:pypi/cuckoo-filter@.
102+
pkg:pypi/cuckoo-filter@1.0.6
103+
pkg:pypi/cuckoo-filter@1.0.6
104+
[resources]
105+
> ERROR: 1
106+
cuckoo_filter-1.0.6.tar.gz-extract/cuckoo_filter-1.0.6/README.md
158107
159-
For more details on retrieving compliance data via the REST API, refer to the
160-
:ref:`rest_api_compliance` section.
108+
.. tip::
109+
In case of compliance alerts, the command returns a non-zero exit code which
110+
may be useful to trigger a failure in an automated process.

scanpipe/apps.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -236,11 +236,6 @@ def set_policies(self):
236236
else:
237237
logger.debug(style.WARNING("Policies file not found."))
238238

239-
@property
240-
def policies_enabled(self):
241-
"""Return True if the policies were provided and loaded properly."""
242-
return bool(self.license_policies_index)
243-
244239
def sync_runs_and_jobs(self):
245240
"""Synchronize ``QUEUED`` and ``RUNNING`` Run with their related Jobs."""
246241
logger.info("Synchronizing QUEUED and RUNNING Run with their related Jobs...")

0 commit comments

Comments
 (0)