|
| 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 store known vulnerabilities from OSV for `content units` within a specified `RepositoryVersion`. |
| 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 | +The next step is to create an async function at the top level of the module (so it can be |
| 21 | +loaded in pulpcore) that will be run as a Pulp task. This async function should return a generator |
| 22 | +object with a dictionary containing the `osv_data` (created through `build_osv_data` function in the following sample), |
| 23 | +and also the `Content` and `RepositoryVersion` objects. |
| 24 | + |
| 25 | +Here is an example of a function with the above steps: |
| 26 | + |
| 27 | +```python |
| 28 | +async def get_content_from_repo_version(repo_version_pk: str): |
| 29 | + repo_version = await sync_to_async(RepositoryVersion.objects.get)(pk=repo_version_pk) |
| 30 | + content_units = await sync_to_async(list)(repo_version.content.all()) |
| 31 | + |
| 32 | + for content_unit in content_units: |
| 33 | + content = await sync_to_async(content_unit.cast)() |
| 34 | + content_name = await sync_to_async(lambda: content.name)() |
| 35 | + content_version = await sync_to_async(lambda: content.version)() |
| 36 | + ecosystem = "PyPI" |
| 37 | + repo_content_osv_data = _build_osv_data(content_name, ecosystem, content_version) |
| 38 | + repo_content_osv_data["repo_version"] = repo_version |
| 39 | + repo_content_osv_data["content"] = content |
| 40 | + yield repo_content_osv_data |
| 41 | + |
| 42 | +def _build_osv_data(name, ecosystem, version=None): |
| 43 | + osv_data = {"package": {"name": name, "ecosystem": ecosystem}} |
| 44 | + if version: |
| 45 | + osv_data["version"] = version |
| 46 | + return osv_data |
| 47 | +``` |
| 48 | + |
| 49 | + |
| 50 | +Now that we have the async generator function, we need to create a `ViewSet` to dispatch a task with it: |
| 51 | + |
| 52 | +!!! note |
| 53 | + In the following sample, we are not defining the permissions to access the endpoint. |
| 54 | + Plugin writters should define them according to each plugin needs. |
| 55 | + |
| 56 | +```python |
| 57 | +from rest_framework.views import APIView |
| 58 | + |
| 59 | +from pulpcore.plugin.models import VulnerabilityReport |
| 60 | +from pulpcore.plugin.serializers import AsyncOperationResponseSerializer, VulnerabilityReportSerializer |
| 61 | +from pulpcore.plugin.tasking import check_content, dispatch |
| 62 | +from pulpcore.plugin.viewsets import OperationPostponedResponse |
| 63 | + |
| 64 | +from my_plugin.app.serializers import MyPluginVulnerabilityReportSerializer |
| 65 | +from my_plugin.app.utils import get_content_from_repo_version |
| 66 | + |
| 67 | +class MyPluginVulnerabilityReport(APIView): |
| 68 | + |
| 69 | + queryset = VulnerabilityReport.objects.all() |
| 70 | + serializer_class = VulnerabilityReportSerializer |
| 71 | + |
| 72 | + @extend_schema( |
| 73 | + request=MyPluginVulnerabilityReportSerializer, |
| 74 | + description="Trigger a task to generate the package vulnerability report", |
| 75 | + summary="Generate vulnerability report", |
| 76 | + responses={202: AsyncOperationResponseSerializer}, |
| 77 | + ) |
| 78 | + def create(self, request): |
| 79 | + serializer = MyPluginVulnerabilityReportSerializer(data=request.data, context={"request": request}) |
| 80 | + serializer.is_valid(raise_exception=True) |
| 81 | + repo_version = serializer.validated_data["repo_version"] |
| 82 | + |
| 83 | + # we need to pass the function as string because dispatch() args only accepts JSON serializable content |
| 84 | + func = f"{get_content_from_repo_version.__module__}.{get_content_from_repo_version.__name__}" |
| 85 | + |
| 86 | + task = dispatch( |
| 87 | + check_content, |
| 88 | + shared_resources=[repo_version.repository], |
| 89 | + args = [func, [repo_version.pk]], |
| 90 | + ) |
| 91 | + return OperationPostponedResponse(task, request) |
| 92 | +``` |
| 93 | + |
| 94 | +Here is a sample for the `MyPluginVulnerabilityReportSerializer` where we serialize the |
| 95 | +`RepositoryVersion` string into a `RepositoryVersionRelatedField` object: |
| 96 | + |
| 97 | +```python |
| 98 | +from rest_framework import serializers |
| 99 | +from pulpcore.plugin.serializers import ValidateFieldsMixin, RepositoryVersionRelatedField |
| 100 | + |
| 101 | +class MyPluginVulnerabilityReportSerializer(serializers.Serializer, ValidateFieldsMixin): |
| 102 | + |
| 103 | + repo_version = RepositoryVersionRelatedField( |
| 104 | + required=True, |
| 105 | + allow_null=False, |
| 106 | + help_text=_("RepositoryVersion HREF with the packages to be checked."), |
| 107 | + ) |
| 108 | +``` |
0 commit comments