Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1.0] Add set_nvticache_str(). #151

Merged
merged 5 commits into from
Nov 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()