Skip to content

Commit

Permalink
Merge pull request #362 from y0urself/fix-get-info-list-208
Browse files Browse the repository at this point in the history
Fix get info list 208
  • Loading branch information
nichtsfrei authored Dec 14, 2020
2 parents e213f90 + 1982ce8 commit 2a8191d
Show file tree
Hide file tree
Showing 9 changed files with 175 additions and 22 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Deprecated
### Removed
### Fixed
* added `get_info_list` to v20.08, so it works as expected with new `InfoType` [#362](https://github.com/greenbone/python-gvm/pull/362)

[Unreleased]: https://github.com/greenbone/python-gvm/compare/v20.11.3...HEAD

Expand Down
51 changes: 50 additions & 1 deletion gvm/protocols/gmpv208/gmpv208.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
from gvm.utils import deprecation
from gvm.xml import XmlCommand

from gvm.protocols.gmpv7.gmpv7 import _to_bool, _to_comma_list
from gvm.protocols.gmpv7.gmpv7 import _to_bool, _to_comma_list, _add_filter
from gvm.connections import GvmConnection
from gvm.errors import InvalidArgumentType, RequiredArgument

Expand Down Expand Up @@ -140,6 +140,55 @@ def get_agent(self, agent_id: str) -> None:
)
)

def get_info_list(
self,
info_type: InfoType,
*,
filter: Optional[str] = None,
filter_id: Optional[str] = None,
name: Optional[str] = None,
details: Optional[bool] = None
) -> Any:
"""Request a list of security information
Arguments:
info_type: Type must be either CERT_BUND_ADV, CPE, CVE,
DFN_CERT_ADV, OVALDEF, NVT or ALLINFO
filter: Filter term to use for the query
filter_id: UUID of an existing filter to use for the query
name: Name or identifier of the requested information
details: Whether to include information about references to this
information
Returns:
The response. See :py:meth:`send_command` for details.
"""
if not info_type:
raise RequiredArgument(
function=self.get_info_list.__name__, argument='info_type'
)

if not isinstance(info_type, InfoType):
raise InvalidArgumentType(
function=self.get_info_list.__name__,
argument='info_type',
arg_type=InfoType.__name__,
)

cmd = XmlCommand("get_info")

cmd.set_attribute("type", info_type.value)

_add_filter(cmd, filter, filter_id)

if name:
cmd.set_attribute("name", name)

if details is not None:
cmd.set_attribute("details", _to_bool(details))

return self._send_xml_command(cmd)

def get_info(self, info_id: str, info_type: InfoType) -> Any:
"""Request a single secinfo
Expand Down
5 changes: 4 additions & 1 deletion tests/protocols/gmpv208/test_new_gmpv208.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,17 @@ class Gmpv208CreateTargetCommandTestCase(


class Gmpv208GetAggregatesTestCase(GmpGetAggregatesTestCase, Gmpv208TestCase):

pass


class Gmpv208GetInfoTestCase(GmpGetInfoTestCase, Gmpv208TestCase):
pass


class Gmpv208GetInfoListTestCase(GmpGetInfoListTestCase, Gmpv208TestCase):
pass


class Gmpv208ModifyFilterTestCase(GmpModifyFilterTestCase, Gmpv208TestCase):
pass

Expand Down
4 changes: 0 additions & 4 deletions tests/protocols/gmpv208/test_v7_from_gmpv208.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,10 +290,6 @@ class Gmpv208GetGroupsTestCase(GmpGetGroupsTestCase, Gmpv208TestCase):
pass


class Gmpv208GetInfoListTestCase(GmpGetInfoListTestCase, Gmpv208TestCase):
pass


class Gmpv208GetNoteTestCase(GmpGetNoteTestCase, Gmpv208TestCase):
pass

Expand Down
1 change: 1 addition & 0 deletions tests/protocols/gmpv208/testcmds/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from .test_create_target import GmpCreateTargetCommandTestCase
from .test_get_aggregates import GmpGetAggregatesTestCase
from .test_get_info import GmpGetInfoTestCase
from .test_get_info_list import GmpGetInfoListTestCase
from .test_modify_filter import GmpModifyFilterTestCase
from .test_modify_permission import GmpModifyPermissionTestCase
from .test_modify_report_format import GmpModifyReportFormatTestCase
Expand Down
4 changes: 2 additions & 2 deletions tests/protocols/gmpv208/testcmds/test_get_aggregates.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,13 +219,13 @@ def test_get_aggregates_invalid_sort_criteria(self):
with self.assertRaises(InvalidArgument):
self.gmp.get_aggregates(
resource_type=EntityType.ALERT,
sort_criteria=[{'stat': 'INVALID'}]
sort_criteria=[{'stat': 'INVALID'}],
)

with self.assertRaises(InvalidArgument):
self.gmp.get_aggregates(
resource_type=EntityType.ALERT,
sort_criteria=[{'order': 'INVALID'}]
sort_criteria=[{'order': 'INVALID'}],
)

def test_get_aggregates_group_limits(self):
Expand Down
115 changes: 115 additions & 0 deletions tests/protocols/gmpv208/testcmds/test_get_info_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2018 Greenbone Networks GmbH
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import unittest

from gvm.errors import RequiredArgument, InvalidArgumentType

from gvm.protocols.gmpv208 import InfoType


class GmpGetInfoListTestCase:
def test_get_info_list(self):
self.gmp.get_info_list(InfoType.CERT_BUND_ADV)

self.connection.send.has_been_called_with(
'<get_info type="CERT_BUND_ADV"/>'
)

self.gmp.get_info_list(InfoType.CPE)

self.connection.send.has_been_called_with('<get_info type="CPE"/>')

self.gmp.get_info_list(info_type=InfoType.CPE)

self.connection.send.has_been_called_with('<get_info type="CPE"/>')

self.gmp.get_info_list(InfoType.CVE)

self.connection.send.has_been_called_with('<get_info type="CVE"/>')

self.gmp.get_info_list(InfoType.DFN_CERT_ADV)

self.connection.send.has_been_called_with(
'<get_info type="DFN_CERT_ADV"/>'
)

self.gmp.get_info_list(InfoType.OVALDEF)

self.connection.send.has_been_called_with('<get_info type="OVALDEF"/>')

self.gmp.get_info_list(InfoType.NVT)

self.connection.send.has_been_called_with('<get_info type="NVT"/>')

with self.assertRaises(AttributeError):
self.gmp.get_info_list(
InfoType.ALLINFO # pylint: disable=no-member
)

def test_get_info_list_missing_info_type(self):
with self.assertRaises(RequiredArgument):
self.gmp.get_info_list(info_type=None)

with self.assertRaises(RequiredArgument):
self.gmp.get_info_list(info_type='')

with self.assertRaises(RequiredArgument):
self.gmp.get_info_list('')

def test_get_info_list_invalid_info_type(self):
with self.assertRaises(InvalidArgumentType):
self.gmp.get_info_list(info_type='foo')

def test_get_info_list_with_filter(self):
self.gmp.get_info_list(InfoType.CPE, filter='foo=bar')

self.connection.send.has_been_called_with(
'<get_info type="CPE" filter="foo=bar"/>'
)

def test_get_info_list_with_filter_id(self):
self.gmp.get_info_list(info_type=InfoType.CPE, filter_id='f1')

self.connection.send.has_been_called_with(
'<get_info type="CPE" filt_id="f1"/>'
)

def test_get_info_list_with_name(self):
self.gmp.get_info_list(info_type=InfoType.CPE, name='foo')

self.connection.send.has_been_called_with(
'<get_info type="CPE" name="foo"/>'
)

def test_get_info_list_with_details(self):
self.gmp.get_info_list(info_type=InfoType.CPE, details=True)

self.connection.send.has_been_called_with(
'<get_info type="CPE" details="1"/>'
)

self.gmp.get_info_list(info_type=InfoType.CPE, details=False)

self.connection.send.has_been_called_with(
'<get_info type="CPE" details="0"/>'
)


if __name__ == '__main__':
unittest.main()
12 changes: 0 additions & 12 deletions tests/protocols/gmpv208/testtypes/test_aggregate_statistic.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,6 @@
)


class GetAggregateStatisticFromStringTestCase(unittest.TestCase):
def test_invalid(self):
with self.assertRaises(InvalidArgument):
get_aggregate_statistic_from_string('foo')

def test_none_or_empty(self):
ct = get_aggregate_statistic_from_string(None)
self.assertIsNone(ct)
ct = get_aggregate_statistic_from_string('')
self.assertIsNone(ct)


class GetAggregateStatisticFromStringTestCase(unittest.TestCase):
def test_invalid(self):
with self.assertRaises(InvalidArgument):
Expand Down
4 changes: 2 additions & 2 deletions tests/protocols/gmpv9/testcmds/test_get_aggregates.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,13 +219,13 @@ def test_get_aggregates_invalid_sort_criteria(self):
with self.assertRaises(InvalidArgument):
self.gmp.get_aggregates(
resource_type=EntityType.ALERT,
sort_criteria=[{'stat': 'INVALID'}]
sort_criteria=[{'stat': 'INVALID'}],
)

with self.assertRaises(InvalidArgument):
self.gmp.get_aggregates(
resource_type=EntityType.ALERT,
sort_criteria=[{'order': 'INVALID'}]
sort_criteria=[{'order': 'INVALID'}],
)

def test_get_aggregates_group_limits(self):
Expand Down

0 comments on commit 2a8191d

Please sign in to comment.