Skip to content

Commit

Permalink
Merge pull request #151 from jjnicola/nvticache-1
Browse files Browse the repository at this point in the history
[1.0] Add set_nvticache_str().
  • Loading branch information
bjoernricks authored Nov 15, 2019
2 parents 1102abc + 48a06e2 commit 8b3383c
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions ospd_openvas/daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
43 changes: 42 additions & 1 deletion ospd_openvas/nvticache.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):

Expand All @@ -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.
"""
Expand Down
13 changes: 13 additions & 0 deletions tests/test_nvti_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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()

0 comments on commit 8b3383c

Please sign in to comment.