Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix severity when report contains no vulnerabilities #92

Merged
merged 3 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion codegen/ast/fragments/scanner/harborvulnerabilityreport.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,23 @@ class HarborVulnerabilityReport(BaseModel):
description="The scanner used to generate the report.",
)
severity: Optional[Severity] = Field(
default=None,
default=Severity.unknown,
description="The overall severity of the vulnerabilities.",
)
vulnerabilities: List[VulnerabilityItem] = Field(
default_factory=list,
description="The list of vulnerabilities found.",
)

@field_validator("severity", mode="before")
@classmethod
def _severity_none_is_default(
cls, v: Optional[Severity], info: ValidationInfo
) -> Severity:
if not info.field_name:
raise ValueError("Validator is not attached to a field.")
return v or cls.model_fields[info.field_name].default

def __repr__(self) -> str:
return f"HarborVulnerabilityReport(generated_at={self.generated_at}, artifact={self.artifact}, scanner={self.scanner}, severity={self.severity}, vulnerabilities=list(len={len(self.vulnerabilities)}))"

Expand Down
12 changes: 11 additions & 1 deletion harborapi/models/scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,13 +401,23 @@ class HarborVulnerabilityReport(BaseModel):
default=None, description="The scanner used to generate the report."
)
severity: Optional[Severity] = Field(
default=None, description="The overall severity of the vulnerabilities."
default=Severity.unknown,
description="The overall severity of the vulnerabilities.",
)
vulnerabilities: List[VulnerabilityItem] = Field(
default_factory=list, description="The list of vulnerabilities found."
)
model_config = ConfigDict(ignored_types=(cached_property,))

@field_validator("severity", mode="before")
@classmethod
def _severity_none_is_default(
cls, v: Optional[Severity], info: ValidationInfo
) -> Severity:
if not info.field_name:
raise ValueError("Validator is not attached to a field.")
return v or cls.model_fields[info.field_name].default

def __repr__(self) -> str:
return f"HarborVulnerabilityReport(generated_at={self.generated_at}, artifact={self.artifact}, scanner={self.scanner}, severity={self.severity}, vulnerabilities=list(len={len(self.vulnerabilities)}))"

Expand Down
15 changes: 15 additions & 0 deletions tests/models/test_scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,21 @@ def test_vulnerability_item_severity_none() -> None:
assert v.severity is not None
assert v.severity == Severity.unknown

def test_harborvulnerabilityreport_severity_none() -> None:
"""Passing None to HarborVulnerabilityReport.severity should assign it Severity.unknown"""
v = HarborVulnerabilityReport(
severity=None,
)
assert v.severity is not None
assert v.severity == Severity.unknown

def test_harborvulnerabilityreport_severity_empty_str() -> None:
"""Passing empty string to HarborVulnerabilityReport.severity should assign it Severity.unknown"""
v = HarborVulnerabilityReport(
severity="",
)
assert v.severity is not None
assert v.severity == Severity.unknown

@pytest.mark.parametrize("scanner_name", ["Trivy", "Clair"])
def test_vulnerability_item_get_cvss_score(scanner_name: str) -> None:
Expand Down