|
| 1 | +# Adding vulnerability report for plugins |
| 2 | + |
| 3 | + |
| 4 | +!!! warning |
| 5 | + This feature is provided as a tech preview and could change in backwards incompatible |
| 6 | + ways in the future. |
| 7 | + |
| 8 | + |
| 9 | +Pulp provides a way to verify the `content units` against the [osv.dev](https://osv.dev) database. |
| 10 | +Each plugin will need to implement a function to construct the [package payload](https://google.github.io/osv.dev/post-v1-query/#parameters) |
| 11 | +that will be used to query osv.dev database. |
| 12 | + |
| 13 | +!!! note |
| 14 | + As of now, querying by osv.dev `commit` is not supported (use `package` instead). |
| 15 | + |
| 16 | +The first step in writing a vulnerability report for a Pulp `content unit` is to identify the |
| 17 | +package [`ecosystem`](https://google.github.io/osv.dev/post-v1-query/#parameters) by checking |
| 18 | +[https://ossf.github.io/osv-schema/#defined-ecosystems](https://ossf.github.io/osv-schema/#defined-ecosystems). |
| 19 | + |
| 20 | +After identifying the `ecosystem`, run the `pulpcore.app.tasks.vulnerability_report.build_osv_data` |
| 21 | +function that will return a dictionary with the expected osv.dev payload format. |
| 22 | + |
| 23 | +The next step is to create a function that will be run as a Pulp task. This function should add the |
| 24 | +dictionary, created in the previous step, to the `pulpcore.app.tasks.vulnerability_report.content_queue` |
| 25 | +queue. |
| 26 | + |
| 27 | +Here is an example of a function that, based on a `RepositoryVersion pk`, adds each content found |
| 28 | +in the Repository in `content_queue` after identifying the `ecosystem` and creating a dictionary |
| 29 | +with the osv.dev expected format: |
| 30 | + |
| 31 | +!!! note |
| 32 | + For the following example, we are using the `pulpcore.app.tasks.vulnerability_report.identify_package_ecosystem` |
| 33 | + helper function, but plugin writers are encouraged to write their own function to better identify |
| 34 | + the ecosystem. |
| 35 | + |
| 36 | +```python |
| 37 | +from pulpcore.app.tasks.vulnerability_report import build_osv_data, content_queue, identify_package_ecosystem |
| 38 | + |
| 39 | +def get_content_from_repo_version(repo_version_pk: str): |
| 40 | + """ |
| 41 | + Populate content_queue Queue with the content_units found in RepositoryVersion |
| 42 | + """ |
| 43 | + repo_version = RepositoryVersion.objects.get(pk=repo_version_pk) |
| 44 | + for content_unit in repo_version.content: |
| 45 | + content = content_unit.cast() |
| 46 | + ecosystems = identify_package_ecosystem(content, repo_version.repository) |
| 47 | + for ecosystem in ecosystems: |
| 48 | + osv_data = build_osv_data(content.name, ecosystem, content.version) |
| 49 | + content_queue.put(osv_data) |
| 50 | + content_queue.put(None) # signal that there are no more content_units |
| 51 | +``` |
| 52 | + |
| 53 | + |
| 54 | +Now that we have the function to populate the `content_queue` queue, we need to create a `ViewSet` |
| 55 | +to dispatch a task with it: |
| 56 | + |
| 57 | +```python |
| 58 | +from pulpcore.plugin.models import VulnerabilityReport |
| 59 | +from pulpcore.app.serializers.vulnerability_report import VulnerabilityReportSerializer |
| 60 | +from pulpcore.app.tasks.vulnerability_report import check_content |
| 61 | +from pulpcore.app.viewsets.vulnerability_report import VulnerabilityReport as VulnerabilityReportView |
| 62 | + |
| 63 | +class MyPluginVulnerabilityReport(VulnerabilityReportView): |
| 64 | + |
| 65 | + endpoint_name = "vuln_report" |
| 66 | + queryset = VulnerabilityReport.objects.all() |
| 67 | + serializer_class = VulnerabilityReportSerializer |
| 68 | + |
| 69 | + @extend_schema( |
| 70 | + request=MyPluginContentVulnerabilityReportSerializer, |
| 71 | + description="Trigger a task to generate the package vulnerability report", |
| 72 | + summary="Generate vulnerability report", |
| 73 | + responses={202: AsyncOperationResponseSerializer}, |
| 74 | + ) |
| 75 | + def create(self, request): |
| 76 | + serializer = MyPluginContentVulnerabilityReportSerializer(data=request.data, context={"request": request}) |
| 77 | + serializer.is_valid(raise_exception=True) |
| 78 | + shared_resources = [repo_version.repository] |
| 79 | + dispatch_task, kwargs = check_content, {"repo_version_pk": repo_version.pk} |
| 80 | + task = dispatch(dispatch_task, shared_resources=shared_resources, args=[get_content_from_repo_version], kwargs=kwargs) |
| 81 | + return OperationPostponedResponse(task, request) |
| 82 | +``` |
| 83 | + |
| 84 | +Here is a sample for the `MyPluginContentVulnerabilityReportSerializer` where we serialize the |
| 85 | +`RepositoryVersion` string into a `RepositoryVersionRelatedField` object: |
| 86 | + |
| 87 | +```python |
| 88 | +from rest_framework import serializers |
| 89 | +from pulpcore.plugin.serializers import ValidateFieldsMixin, RepositoryVersionRelatedField |
| 90 | + |
| 91 | +class MyPluginContentVulnerabilityReportSerializer(serializers.Serializer, ValidateFieldsMixin): |
| 92 | + |
| 93 | + repo_version = RepositoryVersionRelatedField( |
| 94 | + required=False, |
| 95 | + allow_null=True, |
| 96 | + help_text=_("RepositoryVersion HREF with the packages to be checked."), |
| 97 | + ) |
| 98 | +``` |
0 commit comments