diff --git a/CHANGELOG.md b/CHANGELOG.md index 49cdee1e..11637a88 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ### Added - Check the vt's preference value for type 'file'. [#130](https://github.com/greenbone/ospd-openvas/pull/130). +- Add set_nvticache_str(). [#151](https://github.com/greenbone/ospd-openvas/pull/151) ### Fixed - Improve redis clean out when stopping a scan. [#128](https://github.com/greenbone/ospd-openvas/pull/128) diff --git a/ospd_openvas/daemon.py b/ospd_openvas/daemon.py index ae66fb45..1f8e0308 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 285ac471..05e8045d 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 = ('11.0',) + class NVTICache(object): @@ -48,11 +56,44 @@ class NVTICache(object): 'default': '70', } - NVTICACHE_STR = 'nvticache11.0.0' + 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 2123f71a..3bea1c2f 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 @@ -270,3 +271,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 = '11.0.1\n'.encode() + self.nvti.set_nvticache_str() + self.assertEqual(self.nvti.NVTICACHE_STR, 'nvticache11.0.1') + + mock_subps.check_output.return_value = '20.04\n'.encode() + with self.assertRaises(SystemExit): + self.nvti.set_nvticache_str()