From 61b4c802acf2d4d40c835fd7b0a4cc22988d3e1c Mon Sep 17 00:00:00 2001 From: Juan Jose Nicola Date: Tue, 12 Nov 2019 11:40:42 +0100 Subject: [PATCH 1/5] Add set_nvticache_str(). The nvticache name in redis (dependent on the gvm-libs) will be checked for compatibility against a list of supported nvticache versions. --- ospd_openvas/daemon.py | 1 + ospd_openvas/nvticache.py | 42 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/ospd_openvas/daemon.py b/ospd_openvas/daemon.py index 5907c6f6..075f0879 100644 --- a/ospd_openvas/daemon.py +++ b/ospd_openvas/daemon.py @@ -278,6 +278,7 @@ def __init__(self, *, niceness=None, **kwargs): self.openvas_db = OpenvasDB() self.nvti = NVTICache(self.openvas_db) + self.nvti.set_nvticache_str() self.pending_feed = None diff --git a/ospd_openvas/nvticache.py b/ospd_openvas/nvticache.py index 75a93a89..ea3f290a 100644 --- a/ospd_openvas/nvticache.py +++ b/ospd_openvas/nvticache.py @@ -20,14 +20,22 @@ """ Provide functions to handle NVT Info Cache. """ import logging +import subprocess +import sys + +from distutils.version import StrictVersion from ospd_openvas.db import NVT_META_FIELDS +from ospd_openvas.errors import OspdOpenvasError + logger = logging.getLogger(__name__) LIST_FIRST_POS = 0 LIST_LAST_POS = -1 +SUPPORTED_NVTICACHE_VERSION = ['20.4'] + class NVTICache(object): @@ -48,11 +56,43 @@ class NVTICache(object): 'default': '70', } - NVTICACHE_STR = 'nvticache20.4' + NVTICACHE_STR = None def __init__(self, openvas_db): self._openvas_db = openvas_db + def set_nvticache_str(self): + """Set nvticache name""" + try: + result = subprocess.check_output( + ['pkg-config', '--modversion', 'libgvm_util'], + stderr=subprocess.STDOUT, + ) + except (subprocess.CalledProcessError, PermissionError) as e: + raise OspdOpenvasError( + "Error setting nvticache. " + "Not possible to get the installed " + "gvm-libs version. %s" % e + ) + + installed_lib = StrictVersion(str(result.decode('utf-8'))).version + + for supported_item in SUPPORTED_NVTICACHE_VERSION: + supported_lib = StrictVersion(supported_item).version + if ( + installed_lib >= supported_lib + and installed_lib[0] == supported_lib[0] + ): + NVTICache.NVTICACHE_STR = ( + "nvticache" + result.decode('utf-8').rstrip() + ) + return + + logger.error( + "Error setting nvticache. " "Incompatible nvticache version." + ) + sys.exit(1) + def get_feed_version(self): """ Get feed version. """ From 4a74cc66627b2a7e19f91f0acab71ed345f47106 Mon Sep 17 00:00:00 2001 From: Juan Jose Nicola Date: Tue, 12 Nov 2019 16:51:34 +0100 Subject: [PATCH 2/5] Add test --- tests/test_nvti_cache.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/test_nvti_cache.py b/tests/test_nvti_cache.py index 787d856d..a42e7b3e 100644 --- a/tests/test_nvti_cache.py +++ b/tests/test_nvti_cache.py @@ -23,6 +23,7 @@ from unittest import TestCase from unittest.mock import patch + from ospd_openvas.db import OpenvasDB from ospd_openvas.nvticache import NVTICache @@ -271,3 +272,15 @@ def test_get_nvt_tag(self, mock_redis): resp = self.nvti.get_nvt_tag(mock_redis(), '1.2.3.4') self.assertEqual(out_dict, resp) + + @patch('ospd_openvas.nvticache.subprocess') + def test_set_nvticache_str(self, mock_subps, mock_redis): + self.assertIsNone(self.nvti.NVTICACHE_STR) + + mock_subps.check_output.return_value = '20.10\n'.encode() + self.nvti.set_nvticache_str() + self.assertEqual(self.nvti.NVTICACHE_STR, 'nvticache20.10') + + mock_subps.check_output.return_value = '11.0.1\n'.encode() + with self.assertRaises(SystemExit): + self.nvti.set_nvticache_str() From 22d9482e5d7d50254cb7a62dc79be8994f2355fa Mon Sep 17 00:00:00 2001 From: Juan Jose Nicola Date: Tue, 12 Nov 2019 16:59:06 +0100 Subject: [PATCH 3/5] Update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6d181731..6b570328 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ### Added - Add solution method to solution of vt object. [#131](https://github.com/greenbone/ospd-openvas/pull/131) +- Add set_nvticache_str(). [#150](https://github.com/greenbone/ospd-openvas/pull/150) ## [1.0.1] (unreleased) From 91d5d6d09288f782824add5a3ebfb40260e3581f Mon Sep 17 00:00:00 2001 From: Juan Jose Nicola Date: Thu, 14 Nov 2019 16:28:32 +0100 Subject: [PATCH 4/5] Rename SUPPORTED_NVTICACHE_VERSIONS and make it a tuple. --- ospd_openvas/nvticache.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ospd_openvas/nvticache.py b/ospd_openvas/nvticache.py index ea3f290a..226c49b8 100644 --- a/ospd_openvas/nvticache.py +++ b/ospd_openvas/nvticache.py @@ -34,7 +34,7 @@ LIST_FIRST_POS = 0 LIST_LAST_POS = -1 -SUPPORTED_NVTICACHE_VERSION = ['20.4'] +SUPPORTED_NVTICACHE_VERSIONS = ('20.4',) class NVTICache(object): @@ -77,7 +77,7 @@ def set_nvticache_str(self): installed_lib = StrictVersion(str(result.decode('utf-8'))).version - for supported_item in SUPPORTED_NVTICACHE_VERSION: + for supported_item in SUPPORTED_NVTICACHE_VERSIONS: supported_lib = StrictVersion(supported_item).version if ( installed_lib >= supported_lib From fa547400a2a797ef13aaf51367ac04c48bdda60e Mon Sep 17 00:00:00 2001 From: Juan Jose Nicola Date: Fri, 15 Nov 2019 09:25:57 +0100 Subject: [PATCH 5/5] Use pkg_resources instead of distutils --- ospd_openvas/nvticache.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/ospd_openvas/nvticache.py b/ospd_openvas/nvticache.py index 226c49b8..6738a733 100644 --- a/ospd_openvas/nvticache.py +++ b/ospd_openvas/nvticache.py @@ -23,7 +23,7 @@ import subprocess import sys -from distutils.version import StrictVersion +from pkg_resources import parse_version from ospd_openvas.db import NVT_META_FIELDS from ospd_openvas.errors import OspdOpenvasError @@ -75,13 +75,14 @@ def set_nvticache_str(self): "gvm-libs version. %s" % e ) - installed_lib = StrictVersion(str(result.decode('utf-8'))).version + installed_lib = parse_version(str(result.decode('utf-8'))) for supported_item in SUPPORTED_NVTICACHE_VERSIONS: - supported_lib = StrictVersion(supported_item).version + supported_lib = parse_version(supported_item) if ( installed_lib >= supported_lib - and installed_lib[0] == supported_lib[0] + and installed_lib.base_version.split('.')[0] + == supported_lib.base_version.split('.')[0] ): NVTICache.NVTICACHE_STR = ( "nvticache" + result.decode('utf-8').rstrip()