Skip to content

Commit 3d9625a

Browse files
committed
Add support to vuln report for pulp_python plugin
closes: #1272
1 parent 9be8871 commit 3d9625a

File tree

8 files changed

+97
-1
lines changed

8 files changed

+97
-1
lines changed

CHANGES/1272.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added support to vulnerability report for pulp_python plugin.

pulp-glue/pulp_glue/common/context.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,6 +1284,9 @@ def needs_capability(self, capability: str) -> None:
12841284
)
12851285
)
12861286

1287+
def scan(self) -> t.Any:
1288+
return self.call("scan", parameters={self.HREF: self.pulp_href})
1289+
12871290

12881291
class PulpRemoteContext(PulpEntityContext):
12891292
"""

pulp-glue/pulp_glue/core/context.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,3 +613,11 @@ def find(self, **kwargs: t.Any) -> t.Any:
613613

614614
def replicate(self) -> t.Any:
615615
return self.call("replicate", parameters={self.HREF: self.pulp_href})
616+
617+
618+
class PulpVulnerabilityReportContext(PulpEntityContext):
619+
ENTITY = _("vulnerability report")
620+
ENTITIES = _("vulnerability reports")
621+
ID_PREFIX = "vuln_report"
622+
HREF = "vulnerability_report_href"
623+
NEEDS_PLUGINS = [PluginRequirement("core", specifier=">=3.85.3")]

pulp-glue/pulp_glue/python/context.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ class PulpPythonRepositoryVersionContext(PulpRepositoryVersionContext):
101101
HREF = "python_python_repository_version_href"
102102
ID_PREFIX = "repositories_python_python_versions"
103103
NEEDS_PLUGINS = [PluginRequirement("python", specifier=">=3.1.0")]
104+
CAPABILITIES = {"scan": [PluginRequirement("python", specifier=">=3.21.0")]}
104105

105106

106107
class PulpPythonRepositoryContext(PulpRepositoryContext):

pulp_cli/generic.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1521,11 +1521,33 @@ def callback(entity_ctx: PulpEntityContext, /) -> None:
15211521
return callback
15221522

15231523

1524+
def scan_command(**kwargs: t.Any) -> click.Command:
1525+
"""A factory that creates a scan command."""
1526+
1527+
kwargs.setdefault("name", "scan")
1528+
kwargs.setdefault("help", _("Verify repository version package vulnerabilities."))
1529+
decorators = kwargs.pop("decorators", [])
1530+
1531+
@pulp_command(**kwargs)
1532+
@pass_entity_context
1533+
def callback(entity_ctx: PulpEntityContext, /) -> None:
1534+
"""
1535+
Scan a {entity}.
1536+
"""
1537+
entity_ctx.needs_capability("scan")
1538+
entity_ctx.scan()
1539+
1540+
for option in decorators:
1541+
# Decorate callback
1542+
callback = option(callback)
1543+
return callback
1544+
1545+
15241546
def version_command(**kwargs: t.Any) -> click.Command:
15251547
"""
15261548
A factory that creates a repository version command group.
15271549
1528-
This group contains `list`, `show`, `destroy` and `repair` subcommands.
1550+
This group contains `list`, `show`, `destroy`, `repair` and `scan` subcommands.
15291551
If `list_only=True` is passed, only the `list` command will be instantiated.
15301552
Repository lookup options can be provided in `decorators`.
15311553
"""
@@ -1545,6 +1567,7 @@ def callback(ctx: click.Context, repository_ctx: PulpRepositoryContext, /) -> No
15451567
if not list_only:
15461568
callback.add_command(show_command(decorators=decorators + [version_option]))
15471569
callback.add_command(destroy_command(decorators=decorators + [version_option]))
1570+
callback.add_command(scan_command(decorators=decorators + [version_option]))
15481571

15491572
@callback.command()
15501573
@repository_lookup_option

pulpcore/cli/core/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from pulpcore.cli.core.upload import upload
2626
from pulpcore.cli.core.upstream_pulp import upstream_pulp
2727
from pulpcore.cli.core.user import user
28+
from pulpcore.cli.core.vulnerability_report import vulnerability_report
2829
from pulpcore.cli.core.worker import worker
2930

3031

@@ -52,6 +53,7 @@ def mount(main: click.Group, **kwargs: t.Any) -> None:
5253
main.add_command(upload)
5354
main.add_command(upstream_pulp)
5455
main.add_command(user)
56+
main.add_command(vulnerability_report)
5557
main.add_command(worker)
5658

5759
_orig_get_command = main.get_command
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import click
2+
from pulp_glue.common.i18n import get_translation
3+
from pulp_glue.core.context import PulpVulnerabilityReportContext
4+
5+
from pulpcore.cli.common.generic import (
6+
PulpCLIContext,
7+
href_option,
8+
list_command,
9+
pass_pulp_context,
10+
pulp_group,
11+
show_command,
12+
)
13+
14+
translation = get_translation(__package__)
15+
_ = translation.gettext
16+
17+
lookup_options = [href_option]
18+
19+
20+
@pulp_group()
21+
@pass_pulp_context
22+
@click.pass_context
23+
def vulnerability_report(ctx: click.Context, pulp_ctx: PulpCLIContext, /) -> None:
24+
ctx.obj = PulpVulnerabilityReportContext(pulp_ctx)
25+
26+
27+
vulnerability_report.add_command(list_command())
28+
vulnerability_report.add_command(show_command(decorators=lookup_options))
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/bin/bash
2+
3+
set -eu
4+
# shellcheck source=tests/scripts/config.source
5+
. "$(dirname "$(dirname "$(realpath "$0")")")"/config.source
6+
7+
cleanup() {
8+
pulp python repository destroy --name python || true
9+
pulp python remote destroy --name python || true
10+
pulp orphan cleanup --protection-time=0
11+
}
12+
trap cleanup EXIT
13+
14+
pulp debug has-plugin --name "core" --specifier ">=3.85.3" || exit 0
15+
pulp debug has-plugin --name "python" --specifier ">=3.21.0" || exit 0
16+
17+
# create a test repository
18+
pulp python repository create --name python
19+
pulp python remote create --name python --url "https://pypi.org/" --includes '["django==5.2.1"]'
20+
pulp python repository sync --name python --remote python
21+
22+
expect_succ pulp python repository version scan --repository python
23+
expect_succ pulp vulnerability-report list
24+
25+
VULN_REPORT=$(pulp vulnerability-report list --field pulp_href --limit 1|jq .[0].pulp_href -r)
26+
expect_succ pulp vulnerability-report show --href "$VULN_REPORT"
27+
28+
# test with non-implemented content type
29+
pulp file repository create --name file-repo
30+
expect_fail pulp file repository version scan --repository file-repo

0 commit comments

Comments
 (0)