Skip to content

Commit d4ffc3f

Browse files
committed
Refactor VulnerabilitySource to be in shared directory access
1 parent ccf9963 commit d4ffc3f

File tree

3 files changed

+35
-30
lines changed

3 files changed

+35
-30
lines changed

exasol/toolbox/security/__init__.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
asdict,
77
dataclass,
88
)
9+
from enum import Enum
910

1011
import typer
1112

@@ -78,3 +79,29 @@ def json_str(self) -> str:
7879
"""Converts to a string-encoded JSON"""
7980
issue_json = asdict(self)
8081
return json.dumps(issue_json)
82+
83+
84+
class VulnerabilitySource(str, Enum):
85+
CVE = "CVE"
86+
CWE = "CWE"
87+
GHSA = "GHSA"
88+
PYSEC = "PYSEC"
89+
90+
@classmethod
91+
def from_prefix(cls, name: str) -> VulnerabilitySource | None:
92+
for el in cls:
93+
if name.upper().startswith(el.value):
94+
return el
95+
return None
96+
97+
def get_link(self, package: str, vuln_id: str) -> str:
98+
if self == VulnerabilitySource.CWE:
99+
cwe_id = vuln_id.upper().replace(f"{VulnerabilitySource.CWE.value}-", "")
100+
return f"https://cwe.mitre.org/data/definitions/{cwe_id}.html"
101+
102+
map_link = {
103+
VulnerabilitySource.CVE: "https://nvd.nist.gov/vuln/detail/{vuln_id}",
104+
VulnerabilitySource.GHSA: "https://github.com/advisories/{vuln_id}",
105+
VulnerabilitySource.PYSEC: "https://github.com/pypa/advisory-database/blob/main/vulns/{package}/{vuln_id}.yaml",
106+
}
107+
return map_link[self].format(package=package, vuln_id=vuln_id)

exasol/toolbox/tools/security.py

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from exasol.toolbox.security import (
2222
GitHubVulnerabilityIssue,
2323
VulnerabilityIssue,
24+
VulnerabilitySource,
2425
)
2526

2627
stdout = print
@@ -81,32 +82,6 @@ def from_maven(report: str) -> Iterable[VulnerabilityIssue]:
8182
)
8283

8384

84-
class VulnerabilitySource(str, Enum):
85-
CVE = "CVE"
86-
CWE = "CWE"
87-
GHSA = "GHSA"
88-
PYSEC = "PYSEC"
89-
90-
@classmethod
91-
def from_prefix(cls, name: str) -> VulnerabilitySource | None:
92-
for el in cls:
93-
if name.upper().startswith(el.value):
94-
return el
95-
return None
96-
97-
def get_link(self, package: str, vuln_id: str) -> str:
98-
if self == VulnerabilitySource.CWE:
99-
cwe_id = vuln_id.upper().replace(f"{VulnerabilitySource.CWE.value}-", "")
100-
return f"https://cwe.mitre.org/data/definitions/{cwe_id}.html"
101-
102-
map_link = {
103-
VulnerabilitySource.CVE: "https://nvd.nist.gov/vuln/detail/{vuln_id}",
104-
VulnerabilitySource.GHSA: "https://github.com/advisories/{vuln_id}",
105-
VulnerabilitySource.PYSEC: "https://github.com/pypa/advisory-database/blob/main/vulns/{package}/{vuln_id}.yaml",
106-
}
107-
return map_link[self].format(package=package, vuln_id=vuln_id)
108-
109-
11085
def identify_pypi_references(
11186
references: list[str], package_name: str
11287
) -> tuple[list[str], list[str], list[str]]:

test/unit/security_test.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88

99
import pytest
1010

11-
from exasol.toolbox.security import GitHubVulnerabilityIssue
11+
from exasol.toolbox.security import (
12+
GitHubVulnerabilityIssue,
13+
VulnerabilitySource,
14+
)
1215
from exasol.toolbox.tools import security
1316

1417

@@ -493,14 +496,14 @@ def test_from_json(json_file, expected):
493496
[
494497
pytest.param("DUMMY", None, id="without_a_matching_prefix_returns_none"),
495498
pytest.param(
496-
f"{security.VulnerabilitySource.CWE.value.lower()}-1234",
497-
security.VulnerabilitySource.CWE,
499+
f"{VulnerabilitySource.CWE.value.lower()}-1234",
500+
VulnerabilitySource.CWE,
498501
id="with_matching_prefix_returns_vulnerability_source",
499502
),
500503
],
501504
)
502505
def test_from_prefix(prefix: str, expected):
503-
assert security.VulnerabilitySource.from_prefix(prefix) == expected
506+
assert VulnerabilitySource.from_prefix(prefix) == expected
504507

505508

506509
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)