-
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 Trivy JSON reports.
Closes #10608.
- Loading branch information
Showing
11 changed files
with
182 additions
and
116 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
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,49 @@ | ||
"""Base classes for Trivy JSON collectors.""" | ||
|
||
from typing import TypedDict | ||
|
||
# The types below are based on https://aquasecurity.github.io/trivy/v0.45/docs/configuration/reporting/#json. | ||
# That documentation says: "VulnerabilityID, PkgName, InstalledVersion, and Severity in Vulnerabilities are always | ||
# filled with values, but other fields might be empty." This unfortunately does not tell us whether empty means | ||
# an empty string or null. It's also unclear whether keys may be missing. For now we assume all keys are always | ||
# present and missing values are empty strings. | ||
|
||
|
||
class TrivyJSONVulnerability(TypedDict): | ||
"""Trivy JSON for one vulnerability.""" | ||
|
||
VulnerabilityID: str | ||
Title: str | ||
Description: str | ||
Severity: str | ||
PkgName: str | ||
InstalledVersion: str | ||
FixedVersion: str | ||
References: list[str] | ||
|
||
|
||
class TrivyJSONResult(TypedDict): | ||
"""Trivy JSON for one dependency repository.""" | ||
|
||
Target: str | ||
Vulnerabilities: list[TrivyJSONVulnerability] | None # The examples in the Trivy docs show this key can be null | ||
|
||
|
||
# Trivy JSON reports come in two different forms, following schema version 1 or schema version 2. | ||
# Schema version 1 is not explicitly documented as a schema. The Trivy docs only give an example report. | ||
# See https://aquasecurity.github.io/trivy/v0.55/docs/configuration/reporting/#json. | ||
# Schema version 2 is not explicitly documented as a schema either. The only thing available seems to be a GitHub | ||
# discussion: https://github.com/aquasecurity/trivy/discussions/1050. | ||
# Issue to improve the documentation: https://github.com/aquasecurity/trivy/discussions/7552 | ||
|
||
TriviJSONSchemaVersion1 = list[TrivyJSONResult] | ||
|
||
|
||
class TrivyJSONSchemaVersion2(TypedDict): | ||
"""Trivy JSON conform schema version 2.""" | ||
|
||
SchemaVersion: int | ||
Results: list[TrivyJSONResult] | ||
|
||
|
||
TrivyJSON = TriviJSONSchemaVersion1 | TrivyJSONSchemaVersion2 |
48 changes: 2 additions & 46 deletions
48
components/collector/src/source_collectors/trivy/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
22 changes: 22 additions & 0 deletions
22
components/collector/src/source_collectors/trivy/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,22 @@ | ||
"""Trivy JSON collector.""" | ||
|
||
from datetime import datetime | ||
|
||
from base_collectors import JSONFileSourceCollector, TimePassedCollector | ||
from collector_utilities.date_time import parse_datetime | ||
from collector_utilities.exceptions import CollectorError | ||
from collector_utilities.type import Response | ||
|
||
|
||
class TrivyJSONSourceUpToDateness(JSONFileSourceCollector, TimePassedCollector): | ||
"""Trivy 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.""" | ||
json = await response.json() | ||
try: | ||
created_at = json["CreatedAt"] | ||
except TypeError as error: | ||
message = "Measuring source up-to-dateness is not supported with Trivy JSON schema version 1" | ||
raise CollectorError(message) from error | ||
return parse_datetime(created_at) |
61 changes: 61 additions & 0 deletions
61
components/collector/tests/source_collectors/trivy/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,61 @@ | ||
"""Base class for Trivy JSON collector unit tests.""" | ||
|
||
from tests.source_collectors.source_collector_test_case import SourceCollectorTestCase | ||
|
||
|
||
class TrivyJSONTestCase(SourceCollectorTestCase): | ||
"""Base class for Trivy JSON Unit tests.""" | ||
|
||
SOURCE_TYPE = "trivy_json" | ||
SCHEMA_VERSIONS = (1, 2) | ||
|
||
def vulnerabilities_json(self, schema_version: int = 2): | ||
"""Return the Trivy Vulnerabilities JSON.""" | ||
results = [ | ||
{ | ||
"Target": "php-app/composer.lock", | ||
"Vulnerabilities": None, | ||
}, | ||
{ | ||
"Target": "trivy-ci-test (alpine 3.7.1)", | ||
"Vulnerabilities": [ | ||
{ | ||
"VulnerabilityID": "CVE-2018-16840", | ||
"PkgName": "curl", | ||
"InstalledVersion": "7.61.0-r0", | ||
"FixedVersion": "7.61.1-r1", | ||
"Title": 'curl: Use-after-free when closing "easy" handle in Curl_close()', | ||
"Description": "A heap use-after-free flaw was found in curl versions from 7.59.0 through ...", | ||
"Severity": "HIGH", | ||
"References": [ | ||
"https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2018-16840", | ||
], | ||
}, | ||
{ | ||
"VulnerabilityID": "CVE-2019-3822", | ||
"PkgName": "curl", | ||
"InstalledVersion": "7.61.1-r0", | ||
"FixedVersion": "", | ||
"Title": "curl: NTLMv2 type-3 header stack buffer overflow", | ||
"Description": "libcurl versions from 7.36.0 to before 7.64.0 are vulnerable to ...", | ||
"Severity": "MEDIUM", | ||
"References": [ | ||
"https://curl.haxx.se/docs/CVE-2019-3822.html", | ||
"https://lists.apache.org/thread.html", | ||
], | ||
}, | ||
{ | ||
"VulnerabilityID": "CVE-2024-5432", | ||
"PkgName": "python", | ||
"InstalledVersion": "3.13.1", | ||
"Title": "Vulnerability without fixed version", | ||
"Description": "This vulnerability has no fixed version field.", | ||
"Severity": "LOW", | ||
"References": ["https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2024-5432"], | ||
}, | ||
], | ||
}, | ||
] | ||
if schema_version == 1: | ||
return results | ||
return {"SchemaVersion": 2, "CreatedAt": "2024-12-26T21:58:15.943876+05:30", "Results": results} |
57 changes: 2 additions & 55 deletions
57
components/collector/tests/source_collectors/trivy/test_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
23 changes: 23 additions & 0 deletions
23
components/collector/tests/source_collectors/trivy/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,23 @@ | ||
"""Unit tests for the Trivy JSON source up-to-dateness collector.""" | ||
|
||
from collector_utilities.date_time import days_ago, parse_datetime | ||
|
||
from .base import TrivyJSONTestCase | ||
|
||
|
||
class TrivyJSONSourceUpToDatenessTest(TrivyJSONTestCase): | ||
"""Unit tests for the source up-to-dateness metric.""" | ||
|
||
METRIC_TYPE = "source_up_to_dateness" | ||
|
||
async def test_source_up_to_dateness_schema_version_1(self): | ||
"""Test the source up-to-dateness.""" | ||
response = await self.collect(get_request_json_return_value=self.vulnerabilities_json(1)) | ||
expected_error = "Measuring source up-to-dateness is not supported with Trivy JSON schema version 1" | ||
self.assert_measurement(response, parse_error=expected_error) | ||
|
||
async def test_source_up_to_dateness_schema_version_2(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("2024-12-26T21:58:15.943876+05:30"))) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -479,6 +479,7 @@ | |
"sonarqube", | ||
"testng", | ||
"trello", | ||
"trivy_json", | ||
"visual_studio_trx", | ||
], | ||
tags=[Tag.CI], | ||
|
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