Skip to content

Commit

Permalink
api: update cache for vulnerabilities
Browse files Browse the repository at this point in the history
We are now caching the vulnerability result as well.
That means that if we detect that there is now new
vulnerability JSON data to be used and no dpkg related changes
on the system, we can simply reuse the old results.
  • Loading branch information
lucasmoura committed Sep 20, 2024
1 parent 46b8bc5 commit 177d696
Show file tree
Hide file tree
Showing 8 changed files with 317 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ def test_get_vulnerabilities_for_installed_pkgs(
assert (
parser.get_vulnerabilities_for_installed_pkgs(
vulnerabilities_data, installed_pkgs_by_source
)
).vulnerabilities
== expected_result
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
)

M_PATH = "uaclient.api.u.pro.security.vulnerabilities.cve.v1."
M_VULN_COMMON_PATH = "uaclient.api.u.pro.security.vulnerabilities._common.v1."

VULNEBILITIES_DATA = {
"published_at": "2024-06-24T13:19:16",
Expand Down Expand Up @@ -260,19 +261,26 @@ class TestCVEVulnerabilities:
),
),
)
@mock.patch(
M_VULN_COMMON_PATH + "VulnerabilityResultCache.save_result_cache"
)
@mock.patch(M_PATH + "get_apt_cache_datetime")
@mock.patch(M_PATH + "VulnerabilityData.get")
@mock.patch(M_PATH + "SourcePackages.get")
@mock.patch(M_VULN_COMMON_PATH + "VulnerabilityData.get")
@mock.patch(M_VULN_COMMON_PATH + "VulnerabilityData.is_cache_valid")
@mock.patch(M_VULN_COMMON_PATH + "SourcePackages.get")
def test_parse_data(
self,
m_get_source_pkgs,
m_vulnerability_data_is_cache_valid,
m_vulnerability_data_get,
m_get_apt_cache_datetime,
_m_vulnerability_result_save_cache,
vulnerabilities_data,
installed_pkgs_by_source,
cve_options,
expected_result,
):
m_vulnerability_data_is_cache_valid.return_value = (False, None)
m_get_source_pkgs.return_value = installed_pkgs_by_source
m_vulnerability_data_get.return_value = vulnerabilities_data
m_get_apt_cache_datetime.return_value = datetime.datetime(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
)

M_PATH = "uaclient.api.u.pro.security.vulnerabilities.usn.v1."
M_VULN_COMMON_PATH = "uaclient.api.u.pro.security.vulnerabilities._common.v1."

VULNEBILITIES_DATA = {
"published_at": "2024-06-24T13:19:16",
Expand Down Expand Up @@ -308,19 +309,26 @@ class TestUSNVulnerabilities:
),
),
)
@mock.patch(
M_VULN_COMMON_PATH + "VulnerabilityResultCache.save_result_cache"
)
@mock.patch(M_PATH + "get_apt_cache_datetime")
@mock.patch(M_PATH + "VulnerabilityData.get")
@mock.patch(M_PATH + "SourcePackages.get")
@mock.patch(M_VULN_COMMON_PATH + "VulnerabilityData.get")
@mock.patch(M_VULN_COMMON_PATH + "VulnerabilityData.is_cache_valid")
@mock.patch(M_VULN_COMMON_PATH + "SourcePackages.get")
def test_parse_data(
self,
m_get_source_pkgs,
m_vulnerability_data_is_cache_valid,
m_vulnerability_data_get,
m_get_apt_cache_datetime,
_m_vulnerability_result_save_cache,
vulnerabilities_data,
installed_pkgs_by_source,
usn_options,
expected_result,
):
m_vulnerability_data_is_cache_valid.return_value = (False, None)
m_get_source_pkgs.return_value = installed_pkgs_by_source
m_vulnerability_data_get.return_value = vulnerabilities_data
m_get_apt_cache_datetime.return_value = datetime.datetime(
Expand Down
Loading

0 comments on commit 177d696

Please sign in to comment.