|
| 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 | +from asgiref.sync import sync_to_async |
| 29 | +from pulpcore.plugin.models import RepositoryVersion |
| 30 | +from pulpcore.plugin.sync import sync_to_async_iterable |
| 31 | +from myplugin.app.models import MyPluginContent |
| 32 | + |
| 33 | +async def get_content_from_repo_version(repo_version_pk: str): |
| 34 | + repo_version = await sync_to_async(RepositoryVersion.objects.get)(pk=repo_version_pk) |
| 35 | + content_units = MyPluginContent.objects.filter(pk__in=repo_version.content) |
| 36 | + ecosystem = "MyContentUnitEcosystem" # Content unit ecosystem from osv.dev (for ex "PyPI" for python content unit) |
| 37 | + async for content in sync_to_async_iterable(content_units): |
| 38 | + repo_content_osv_data = _build_osv_data(content.name, ecosystem, content.version) |
| 39 | + repo_content_osv_data["repo_version"] = repo_version |
| 40 | + repo_content_osv_data["content"] = content |
| 41 | + yield repo_content_osv_data |
| 42 | + |
| 43 | +def _build_osv_data(name, ecosystem, version=None): |
| 44 | + osv_data = {"package": {"name": name, "ecosystem": ecosystem}} |
| 45 | + if version: |
| 46 | + osv_data["version"] = version |
| 47 | + return osv_data |
| 48 | +``` |
| 49 | + |
| 50 | + |
| 51 | +Now that we have the async generator function, we need to create a new method in the plugin |
| 52 | +RepositoryVersionViewSet subclass (the plugin class that inherits from core.RepositoryVersionViewSet) |
| 53 | +that will be used to dispatch the `vulnerability report` task. |
| 54 | + |
| 55 | +!!! note |
| 56 | + In the following sample, we are not defining the permissions to access the endpoint. |
| 57 | + Plugin writters should define them according to each plugin needs. |
| 58 | + |
| 59 | +```python |
| 60 | +from drf_spectacular.utils import extend_schema |
| 61 | +from rest_framework.decorators import action |
| 62 | + |
| 63 | +from pulpcore.plugin import viewsets as core_viewsets |
| 64 | +from pulpcore.plugin.tasking import check_content, dispatch |
| 65 | + |
| 66 | +class MyPluginRepositoryVersionViewSet(core_viewsets.RepositoryVersionViewSet): |
| 67 | + parent_viewset = MyPluginRepositoryViewSet |
| 68 | + |
| 69 | + @extend_schema(summary="Generate vulnerability report", responses={202: AsyncOperationResponseSerializer}) |
| 70 | + @action(detail=True, methods=["post"]) |
| 71 | + def scan(self, request, repository_pk, **kwargs): |
| 72 | + repository_version = self.get_object() |
| 73 | + func = f"{get_content_from_repo_version.__module__}.{get_content_from_repo_version.__name__}" |
| 74 | + task = dispatch( |
| 75 | + check_content, |
| 76 | + shared_resources=[repository_version.repository], |
| 77 | + args=[func, [repository_version.pk]], |
| 78 | + ) |
| 79 | + return core_viewsets.OperationPostponedResponse(task, request) |
| 80 | +``` |
0 commit comments