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) 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..6738a733 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 pkg_resources import parse_version 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_VERSIONS = ('20.4',) + class NVTICache(object): @@ -48,11 +56,44 @@ 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 = parse_version(str(result.decode('utf-8'))) + + for supported_item in SUPPORTED_NVTICACHE_VERSIONS: + supported_lib = parse_version(supported_item) + if ( + installed_lib >= supported_lib + and installed_lib.base_version.split('.')[0] + == supported_lib.base_version.split('.')[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. """ 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()