Skip to content

Commit

Permalink
Adding --osv-url argument to allow use of private OSV vulnerability
Browse files Browse the repository at this point in the history
services

#805
  • Loading branch information
davidjmemmett committed Aug 8, 2024
1 parent 272c1a5 commit 5fea1a7
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ __pycache__/
html/
dist/
.python-version
/.pytest_cache/
20 changes: 16 additions & 4 deletions pip_audit/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,11 @@ class VulnerabilityServiceChoice(str, enum.Enum):
Osv = "osv"
Pypi = "pypi"

def to_service(self, timeout: int, cache_dir: Path | None) -> VulnerabilityService:
def to_service(self, **kwargs: dict) -> VulnerabilityService:
if self is VulnerabilityServiceChoice.Osv:
return OsvService(cache_dir, timeout)
return OsvService(**kwargs)
elif self is VulnerabilityServiceChoice.Pypi:
return PyPIService(cache_dir, timeout)
return PyPIService(**kwargs)
else:
assert_never(self) # pragma: no cover

Expand Down Expand Up @@ -241,6 +241,14 @@ def _parser() -> argparse.ArgumentParser: # pragma: no cover
VulnerabilityServiceChoice,
),
)
parser.add_argument(
"--osv-url",
type=str,
metavar="OSV_URL",
dest="osv_url",
default=os.environ.get("PIP_AUDIT_OSV_URL", OsvService.DEFAULT_OSV_URL),
help="URL to use for the OSV API instead of the default",
)
parser.add_argument(
"-d",
"--dry-run",
Expand Down Expand Up @@ -418,7 +426,11 @@ def audit() -> None: # pragma: no cover
parser = _parser()
args = _parse_args(parser)

service = args.vulnerability_service.to_service(args.timeout, args.cache_dir)
service = args.vulnerability_service.to_service(
timeout=args.timeout,
cache_dir=args.cache_dir,
osv_url=args.osv_url,
)
output_desc = args.desc.to_bool(args.format)
output_aliases = args.aliases.to_bool(args.format)
formatter = args.format.to_format(output_desc, output_aliases)
Expand Down
13 changes: 10 additions & 3 deletions pip_audit/_service/osv.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,14 @@ class OsvService(VulnerabilityService):
package vulnerability information.
"""

def __init__(self, cache_dir: Path | None = None, timeout: int | None = None):
DEFAULT_OSV_URL = "https://api.osv.dev/v1/query"

def __init__(
self,
cache_dir: Path | None = None,
timeout: int | None = None,
osv_url: str = DEFAULT_OSV_URL,
):
"""
Create a new `OsvService`.
Expand All @@ -43,6 +50,7 @@ def __init__(self, cache_dir: Path | None = None, timeout: int | None = None):
"""
self.session = caching_session(cache_dir, use_pip=False)
self.timeout = timeout
self.osv_url = osv_url

def query(self, spec: Dependency) -> tuple[Dependency, list[VulnerabilityResult]]:
"""
Expand All @@ -54,14 +62,13 @@ def query(self, spec: Dependency) -> tuple[Dependency, list[VulnerabilityResult]
return spec, []
spec = cast(ResolvedDependency, spec)

url = "https://api.osv.dev/v1/query"
query = {
"package": {"name": spec.canonical_name, "ecosystem": "PyPI"},
"version": str(spec.version),
}
try:
response: requests.Response = self.session.post(
url=url,
url=self.osv_url,
data=json.dumps(query),
timeout=self.timeout,
)
Expand Down
2 changes: 1 addition & 1 deletion pip_audit/_service/pypi.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class PyPIService(VulnerabilityService):
package vulnerability information.
"""

def __init__(self, cache_dir: Path | None = None, timeout: int | None = None) -> None:
def __init__(self, cache_dir: Path | None = None, timeout: int | None = None, **kwargs: dict) -> None:
"""
Create a new `PyPIService`.
Expand Down
2 changes: 1 addition & 1 deletion test/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def test_str(self):
class TestVulnerabilityServiceChoice:
def test_to_service_is_exhaustive(self, cache_dir):
for choice in VulnerabilityServiceChoice:
assert choice.to_service(0, cache_dir) is not None
assert choice.to_service(timeout=0, cache_dir=cache_dir) is not None

def test_str(self):
for choice in VulnerabilityServiceChoice:
Expand Down

0 comments on commit 5fea1a7

Please sign in to comment.