-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow for measuring the source up-to-dateness of Harbor JSON reports.
Closes #10609.
- Loading branch information
Showing
11 changed files
with
120 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
components/collector/src/source_collectors/harbor_json/base.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
"""Base classes for Harbor JSON collectors.""" | ||
|
||
from typing import Final, TypedDict | ||
|
||
REPORT_MIME_TYPE: Final = "application/vnd.security.vulnerability.report; version=1.1" | ||
|
||
|
||
class Vulnerability(TypedDict): | ||
"""A Harbor JSON vulnerability.""" | ||
|
||
id: str | ||
package: str | ||
version: str | ||
fix_version: str | ||
severity: str | ||
description: str | ||
links: list[str] | ||
|
||
|
||
class HarborJSONVulnerabilityReport(TypedDict): | ||
"""A Harbor JSON vulnerability report.""" | ||
|
||
generated_at: str | ||
vulnerabilities: list[Vulnerability] | ||
|
||
|
||
HarborJSON = TypedDict( | ||
"HarborJSON", | ||
{"application/vnd.security.vulnerability.report; version=1.1": HarborJSONVulnerabilityReport}, | ||
) |
28 changes: 2 additions & 26 deletions
28
components/collector/src/source_collectors/harbor_json/security_warnings.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
components/collector/src/source_collectors/harbor_json/source_up_to_dateness.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
"""Harbor JSON source up-to-dateness collector.""" | ||
|
||
from datetime import datetime | ||
from typing import Final | ||
|
||
from base_collectors import JSONFileSourceCollector, TimePassedCollector | ||
from collector_utilities.date_time import parse_datetime | ||
from collector_utilities.type import Response | ||
|
||
from .base import REPORT_MIME_TYPE | ||
|
||
|
||
class HarborJSONSourceUpToDateness(JSONFileSourceCollector, TimePassedCollector): | ||
"""Harbor JSON collector for source up-to-dateness.""" | ||
|
||
async def _parse_source_response_date_time(self, response: Response) -> datetime: | ||
"""Override to parse the date of the most recent analysis.""" | ||
return parse_datetime((await response.json())[REPORT_MIME_TYPE]["generated_at"]) |
38 changes: 38 additions & 0 deletions
38
components/collector/tests/source_collectors/harbor_json/base.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
"""Base classes for Harbor JSON collector unit tests.""" | ||
|
||
from typing import ClassVar | ||
|
||
from source_collectors.harbor_json.base import HarborJSON | ||
|
||
from tests.source_collectors.source_collector_test_case import SourceCollectorTestCase | ||
|
||
|
||
class HarborJSONCollectorTestCase(SourceCollectorTestCase): | ||
"""Base class for Harbor JSON collector unit tests.""" | ||
|
||
SOURCE_TYPE = "harbor_json" | ||
VULNERABILITIES_JSON: ClassVar[HarborJSON] = { | ||
"application/vnd.security.vulnerability.report; version=1.1": { | ||
"generated_at": "2023-08-26T16:32:21.923910328Z", | ||
"vulnerabilities": [ | ||
{ | ||
"id": "CVE-2011-3374", | ||
"package": "apt", | ||
"version": "2.2.4", | ||
"fix_version": "2.2.5", | ||
"severity": "Low", | ||
"description": "It was found that apt-key in apt, all versions, do not correctly validate ...", | ||
"links": ["https://avd.aquasec.com/nvd/cve-2011-3374"], | ||
}, | ||
{ | ||
"id": "CVE-2020-22218", | ||
"package": "libssh2-1", | ||
"version": "1.9.0-2", | ||
"fix_version": "", | ||
"severity": "High", | ||
"description": "An issue was discovered in function _libssh2_packet_add in libssh2 1.10.0 ...", | ||
"links": ["https://avd.aquasec.com/nvd/cve-2020-22218"], | ||
}, | ||
], | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
components/collector/tests/source_collectors/harbor_json/test_source_up_to_dateness.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
"""Unit tests for the Harbor JSON source up-to-dateness collector.""" | ||
|
||
from collector_utilities.date_time import days_ago, parse_datetime | ||
|
||
from .base import HarborJSONCollectorTestCase | ||
|
||
|
||
class HarborJSONSourceUpToDatenessTest(HarborJSONCollectorTestCase): | ||
"""Unit tests for the source up-to-dateness metric.""" | ||
|
||
METRIC_TYPE = "source_up_to_dateness" | ||
|
||
async def test_souce_up_to_dateness(self): | ||
"""Test the source up-to-dateness.""" | ||
response = await self.collect(get_request_json_return_value=self.VULNERABILITIES_JSON) | ||
expected_value = str(days_ago(parse_datetime("2023-08-26T16:32:21.923910328Z"))) | ||
self.assert_measurement(response, value=expected_value) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters