Skip to content

Commit 5c12e59

Browse files
committed
Move VulnerabilitySource to audit.py for shared usage
1 parent 543620e commit 5c12e59

File tree

4 files changed

+45
-42
lines changed

4 files changed

+45
-42
lines changed

exasol/toolbox/tools/security.py

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@
1818
from functools import partial
1919
from inspect import cleandoc
2020
from pathlib import Path
21-
from typing import Optional
2221

2322
import typer
2423

24+
from exasol.toolbox.util.dependencies.audit import VulnerabilitySource
25+
2526
stdout = print
2627
stderr = partial(print, file=sys.stderr)
2728

@@ -104,32 +105,6 @@ def from_maven(report: str) -> Iterable[Issue]:
104105
)
105106

106107

107-
class VulnerabilitySource(str, Enum):
108-
CVE = "CVE"
109-
CWE = "CWE"
110-
GHSA = "GHSA"
111-
PYSEC = "PYSEC"
112-
113-
@classmethod
114-
def from_prefix(cls, name: str) -> VulnerabilitySource | None:
115-
for el in cls:
116-
if name.upper().startswith(el.value):
117-
return el
118-
return None
119-
120-
def get_link(self, package: str, vuln_id: str) -> str:
121-
if self == VulnerabilitySource.CWE:
122-
cwe_id = vuln_id.upper().replace(f"{VulnerabilitySource.CWE.value}-", "")
123-
return f"https://cwe.mitre.org/data/definitions/{cwe_id}.html"
124-
125-
map_link = {
126-
VulnerabilitySource.CVE: "https://nvd.nist.gov/vuln/detail/{vuln_id}",
127-
VulnerabilitySource.GHSA: "https://github.com/advisories/{vuln_id}",
128-
VulnerabilitySource.PYSEC: "https://github.com/pypa/advisory-database/blob/main/vulns/{package}/{vuln_id}.yaml",
129-
}
130-
return map_link[self].format(package=package, vuln_id=vuln_id)
131-
132-
133108
def identify_pypi_references(
134109
references: list[str], package_name: str
135110
) -> tuple[list[str], list[str], list[str]]:

exasol/toolbox/util/dependencies/audit.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import subprocess # nosec
55
import tempfile
66
from dataclasses import dataclass
7+
from enum import Enum
78
from pathlib import Path
89
from re import search
910
from typing import (
@@ -34,6 +35,32 @@ def __init__(self, subprocess_output: subprocess.CompletedProcess) -> None:
3435
self.stderr = subprocess_output.stderr
3536

3637

38+
class VulnerabilitySource(str, Enum):
39+
CVE = "CVE"
40+
CWE = "CWE"
41+
GHSA = "GHSA"
42+
PYSEC = "PYSEC"
43+
44+
@classmethod
45+
def from_prefix(cls, name: str) -> VulnerabilitySource | None:
46+
for el in cls:
47+
if name.upper().startswith(el.value):
48+
return el
49+
return None
50+
51+
def get_link(self, package: str, vuln_id: str) -> str:
52+
if self == VulnerabilitySource.CWE:
53+
cwe_id = vuln_id.upper().replace(f"{VulnerabilitySource.CWE.value}-", "")
54+
return f"https://cwe.mitre.org/data/definitions/{cwe_id}.html"
55+
56+
map_link = {
57+
VulnerabilitySource.CVE: "https://nvd.nist.gov/vuln/detail/{vuln_id}",
58+
VulnerabilitySource.GHSA: "https://github.com/advisories/{vuln_id}",
59+
VulnerabilitySource.PYSEC: "https://github.com/pypa/advisory-database/blob/main/vulns/{package}/{vuln_id}.yaml",
60+
}
61+
return map_link[self].format(package=package, vuln_id=vuln_id)
62+
63+
3764
class Vulnerability(Package):
3865
id: str
3966
aliases: list[str]

test/unit/security_test.py

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -330,21 +330,6 @@ def test_from_json(json_input, expected):
330330
assert list(actual) == [expected_issue]
331331

332332

333-
@pytest.mark.parametrize(
334-
"prefix,expected",
335-
[
336-
pytest.param("DUMMY", None, id="without_a_matching_prefix_returns_none"),
337-
pytest.param(
338-
f"{security.VulnerabilitySource.CWE.value.lower()}-1234",
339-
security.VulnerabilitySource.CWE,
340-
id="with_matching_prefix_returns_vulnerability_source",
341-
),
342-
],
343-
)
344-
def test_from_prefix(prefix: str, expected):
345-
assert security.VulnerabilitySource.from_prefix(prefix) == expected
346-
347-
348333
@pytest.mark.parametrize(
349334
"reference, expected",
350335
[

test/unit/util/dependencies/audit_test.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
PipAuditException,
1111
Vulnerabilities,
1212
Vulnerability,
13+
VulnerabilitySource,
1314
audit_poetry_files,
1415
get_vulnerabilities,
1516
get_vulnerabilities_from_latest_tag,
@@ -138,6 +139,21 @@ def test_security_issue_dict(sample_vulnerability):
138139
assert result == [sample_vulnerability.security_issue_entry]
139140

140141

142+
@pytest.mark.parametrize(
143+
"prefix,expected",
144+
[
145+
pytest.param("DUMMY", None, id="without_a_matching_prefix_returns_none"),
146+
pytest.param(
147+
f"{VulnerabilitySource.CWE.value.lower()}-1234",
148+
VulnerabilitySource.CWE,
149+
id="with_matching_prefix_returns_vulnerability_source",
150+
),
151+
],
152+
)
153+
def test_from_prefix(prefix: str, expected):
154+
assert VulnerabilitySource.from_prefix(prefix) == expected
155+
156+
141157
class TestGetVulnerabilities:
142158
def test_with_mock(self, sample_vulnerability):
143159
with mock.patch(

0 commit comments

Comments
 (0)