From 140a700b00c392e2b7bd75e2bf9b5079979d5e2d Mon Sep 17 00:00:00 2001 From: Giulia Naponiello Date: Oct 10 2019 07:54:33 +0000 Subject: Use SFM2 API for the impact and affected pkgs from CVE The Bugzilla whitelist field was removed due to security reasons. The security team introduced new API for Freshmaker to get the impact of CVE and the list of components affected by the CVE. This replaces the Bugzilla whitelist field completely. Signed-off-by: Giulia Naponiello --- diff --git a/freshmaker/bugzilla.py b/freshmaker/bugzilla.py deleted file mode 100644 index d626b80..0000000 --- a/freshmaker/bugzilla.py +++ /dev/null @@ -1,180 +0,0 @@ -# -*- coding: utf-8 -*- -# Copyright (c) 2018 Red Hat, Inc. -# -# Permission is hereby granted, free of charge, to any person obtaining a copy -# of this software and associated documentation files (the "Software"), to deal -# in the Software without restriction, including without limitation the rights -# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -# copies of the Software, and to permit persons to whom the Software is -# furnished to do so, subject to the following conditions: -# -# The above copyright notice and this permission notice shall be included in -# all copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -# SOFTWARE. -# -# Written by Jan Kaluza -# Ralph Bean -impact={impact} - -""" - -xml_with_empty_status = """ - - - -""" - -xml_with_affected_pkgs = """ - -impact={impact},{packages} - -""" - -xml_without_status = """""" -xml_with_empty_bug = """""" - -xml_with_severity = """ - -{severity} - -""" - - -class TestBugzillaAPI(helpers.FreshmakerTestCase): - - @patch("freshmaker.bugzilla.requests.get") - def test_fetch_cve_metadata(self, requests_get): - impacts = ["Low", "Moderate", "Important", "Critical"] - bugzilla = BugzillaAPI() - for num_of_cves in range(1, 4): - requests_get.side_effect = [ - MockResponse(xml_with_status.format(impact=impact)) - for impact in impacts] - highest_cve_severity, _ = bugzilla.fetch_cve_metadata(["CVE-1"] * num_of_cves) - self.assertEqual(highest_cve_severity, impacts[num_of_cves - 1].lower()) - - @patch("freshmaker.bugzilla.requests.get") - def test_fetch_cve_metadata_empty_list(self, requests_get): - bugzilla = BugzillaAPI() - highest_cve_severity, _ = bugzilla.fetch_cve_metadata([]) - self.assertEqual(highest_cve_severity, None) - requests_get.assert_not_called() - - @patch("freshmaker.bugzilla.requests.get") - def test_fetch_cve_metadata_no_status(self, requests_get): - bugzilla = BugzillaAPI() - requests_get.return_value = MockResponse(xml_without_status) - highest_cve_severity, _ = bugzilla.fetch_cve_metadata(["CVE-1"]) - self.assertEqual(highest_cve_severity, None) - - @patch("freshmaker.bugzilla.requests.get") - def test_fetch_cve_metadata_empty_status(self, requests_get): - bugzilla = BugzillaAPI() - requests_get.return_value = MockResponse(xml_with_empty_status) - highest_cve_severity, _ = bugzilla.fetch_cve_metadata(["CVE-1"]) - self.assertEqual(highest_cve_severity, None) - - @patch("freshmaker.bugzilla.requests.get") - def test_fetch_cve_metadata_empty_bug(self, requests_get): - bugzilla = BugzillaAPI() - requests_get.return_value = MockResponse(xml_with_empty_bug) - highest_cve_severity, _ = bugzilla.fetch_cve_metadata(["CVE-1"]) - self.assertEqual(highest_cve_severity, None) - - @patch("freshmaker.bugzilla.requests.get") - def test_fetch_cve_metadata_unknown_impact(self, requests_get): - impacts = ["Low", "unknown"] - requests_get.side_effect = [ - MockResponse(xml_with_status.format(impact=impact)) - for impact in impacts] - bugzilla = BugzillaAPI() - highest_cve_severity, _ = bugzilla.fetch_cve_metadata(["CVE-1", "CVE-2"]) - self.assertEqual(highest_cve_severity, "low") - - @patch("freshmaker.bugzilla.requests.get") - def test_fetch_cve_metadata_with_affected_pkgs(self, requests_get): - impacts = ["Low"] - packages = "openshift-enterprise-3.11/atomic-openshift=affected,openshift-enterprise-4.1/openshift=notaffected" - requests_get.side_effect = [ - MockResponse(xml_with_affected_pkgs.format(impact=impact, packages=packages)) - for impact in impacts] - bugzilla = BugzillaAPI() - highest_cve_severity, affected_pkgs = bugzilla.fetch_cve_metadata(["CVE-1"]) - self.assertEqual(highest_cve_severity, "low") - self.assertEqual(affected_pkgs[0]['product'], 'openshift-enterprise-3.11') - self.assertEqual(affected_pkgs[0]['pkg_name'], 'atomic-openshift') - - @patch("freshmaker.bugzilla.requests.get") - def test_fetch_cve_metadata_with_severity(self, requests_get): - severities = ["low", "medium", "high", "critical"] - impacts = ["low", "moderate", "important", "critical"] - bugzilla = BugzillaAPI() - for i in range(0, 4): - requests_get.side_effect = [MockResponse(xml_with_severity.format(severity=severities[i]))] - highest_cve_severity, _ = bugzilla.fetch_cve_metadata(["CVE-1"]) - self.assertEqual(highest_cve_severity, impacts[i]) diff --git a/tests/test_errata.py b/tests/test_errata.py index 148b723..1e1dbc4 100644 --- a/tests/test_errata.py +++ b/tests/test_errata.py @@ -162,7 +162,7 @@ class TestErrata(helpers.FreshmakerTestCase): self.errata = Errata("https://localhost/") self.patcher = helpers.Patcher( - 'freshmaker.errata.BugzillaAPI.') + 'freshmaker.errata.SFM2API.') self.patcher.patch("fetch_cve_metadata", return_value=["moderate", {}]) diff --git a/tests/test_sfm2.py b/tests/test_sfm2.py new file mode 100644 index 0000000..d0e322c --- /dev/null +++ b/tests/test_sfm2.py @@ -0,0 +1,162 @@ +# -*- coding: utf-8 -*- +# +# Copyright (c) 2017 Red Hat, Inc. +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +from mock import patch, MagicMock + +from freshmaker.sfm2 import SFM2API +from tests import helpers +from requests.exceptions import HTTPError + + +class MockResponse(object): + def __init__(self, text): + self.text = text + + def json(self): + return self.text + + def raise_for_status(self): + pass + + +class TestSFM2API(helpers.FreshmakerTestCase): + + @patch("freshmaker.sfm2.requests.get") + def test_fetch_cve_metadata(self, requests_get): + impacts = ["low", "moderate", "important", "critical"] + sfm2 = SFM2API() + for num_of_cves in range(1, 4): + requests_get.side_effect = [MockResponse([{'affects': [], 'impact': impacts[num_of_cves - 1]}])] + highest_cve_severity, _ = sfm2.fetch_cve_metadata(["CVE-%s" % num_of_cves]) + self.assertEqual(highest_cve_severity, impacts[num_of_cves - 1].lower()) + + @patch("freshmaker.sfm2.requests.get") + def test_fetch_cve_metadata_empty_list(self, requests_get): + sfm2 = SFM2API() + highest_cve_severity, _ = sfm2.fetch_cve_metadata([]) + self.assertEqual(highest_cve_severity, None) + requests_get.assert_not_called() + + @patch("freshmaker.sfm2.requests.get") + def test_fetch_cve_metadata_empty_affects_and_impact(self, requests_get): + sfm2 = SFM2API() + requests_get.return_value = MockResponse([{'affects': [], 'impact': None}]) + highest_cve_severity, affected_pkgs = sfm2.fetch_cve_metadata(["CVE-1"]) + self.assertEqual(highest_cve_severity, None) + self.assertEqual(affected_pkgs, []) + + @patch("freshmaker.sfm2.requests.get") + def test_fetch_cve_metadata_unspecified_impact(self, requests_get): + impacts = ["low", "unspecified", "none"] + requests_get.side_effect = [MockResponse([{'affects': [], 'impact': impact}]) for impact in impacts] + sfm2 = SFM2API() + highest_cve_severity, _ = sfm2.fetch_cve_metadata(["CVE-1", "CVE-2"]) + self.assertEqual(highest_cve_severity, "low") + + @patch("freshmaker.sfm2.requests.get") + def test_fetch_cve_metadata_unspecified_impact_only(self, requests_get): + impacts = ["unspecified", "none"] + requests_get.side_effect = [MockResponse([{'affects': [], 'impact': impact}]) for impact in impacts] + sfm2 = SFM2API() + highest_cve_severity, _ = sfm2.fetch_cve_metadata(["CVE-1", "CVE-2"]) + self.assertEqual(highest_cve_severity, None) + + @patch("freshmaker.sfm2.requests.get") + def test_fetch_cve_metadata_with_affected_pkgs(self, requests_get): + response_impact_and_affected_pkgs = [{'affects': [{ + 'affected': 'affected', + 'cvss2': None, + 'cvss3': None, + 'impact': None, + 'ps_component': 'openssl', + 'ps_module': 'rhel-6', + 'resolution': 'fix' + }, { + 'affected': 'affected', + 'cvss2': None, + 'cvss3': None, + 'impact': None, + 'ps_component': 'openssl', + 'ps_module': 'rhel-7.1.z', + 'resolution': 'fix' + }, { + 'affected': None, + 'cvss2': None, + 'cvss3': None, + 'impact': None, + 'ps_component': 'openssl097a', + 'ps_module': 'rhel-5', + 'resolution': 'wontfix' + }, { + 'affected': 'notaffected', + 'cvss2': None, + 'cvss3': None, + 'impact': None, + 'ps_component': 'nss', + 'ps_module': 'rhel-5', + 'resolution': None + }], 'impact': 'important'}] + requests_get.side_effect = [MockResponse(response_impact_and_affected_pkgs)] + sfm2 = SFM2API() + highest_cve_severity, affected_pkgs = sfm2.fetch_cve_metadata(["CVE-1"]) + self.assertEqual(highest_cve_severity, "important") + self.assertEqual(affected_pkgs[0]['product'], 'rhel-6') + self.assertEqual(affected_pkgs[0]['pkg_name'], 'openssl') + self.assertEqual(affected_pkgs[1]['product'], 'rhel-7.1.z') + self.assertEqual(len(affected_pkgs), 2) + + @patch("freshmaker.sfm2.requests.get") + def test_fetch_cve_metadata_with_not_affected_pkgs(self, requests_get): + response_impact_and_affected_pkgs = [{'affects': [{ + 'affected': None, + 'cvss2': None, + 'cvss3': None, + 'impact': None, + 'ps_component': 'openssl097a', + 'ps_module': 'rhel-5', + 'resolution': 'wontfix' + }, { + 'affected': 'notaffected', + 'cvss2': None, + 'cvss3': None, + 'impact': None, + 'ps_component': 'nss', + 'ps_module': 'rhel-5', + 'resolution': None + }], 'impact': 'important'}] + requests_get.side_effect = [MockResponse(response_impact_and_affected_pkgs)] + sfm2 = SFM2API() + highest_cve_severity, affected_pkgs = sfm2.fetch_cve_metadata(["CVE-1"]) + self.assertEqual(highest_cve_severity, "important") + self.assertEqual(affected_pkgs, []) + + @patch("freshmaker.sfm2.requests.get") + def test_fetch_cve_metadata_with_error(self, requests_get): + for status_code in [400, 500]: + error_response = MagicMock() + error_response.status_code = status_code + error_response.raise_for_status.side_effect = HTTPError( + "Expected exception", response=error_response) + sfm2 = SFM2API() + highest_cve_severity, affected_pkgs = sfm2.fetch_cve_metadata(["CVE-1"]) + self.assertEqual(highest_cve_severity, None) + self.assertEqual(affected_pkgs, [])