Skip to content

Commit

Permalink
Merge pull request #434 from bjoernricks/feed-type-gvmd-data
Browse files Browse the repository at this point in the history
Override FeedType and get_feed for 20.08
  • Loading branch information
y0urself authored Apr 26, 2021
2 parents 75746a7 + f15eed4 commit 59849f2
Show file tree
Hide file tree
Showing 10 changed files with 185 additions and 10 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ and this project adheres to [Calendar Versioning](https://calver.org)html).
## [Unreleased]
### Added
### Changed
* `get_feed` can also be requested with `GVMD_DATA` for 20.08 and newer, added `GVMD_DATA` to the FeedType and updated API call [#434](https://github.com/greenbone/python-gvm/pull/434)

### Deprecated
### Removed
### Fixed
Expand Down
26 changes: 26 additions & 0 deletions gvm/protocols/gmpv208/gmpv208.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,3 +354,29 @@ def create_target(
cmd.add_element("port_list", attrs={"id": port_list_id})

return self._send_xml_command(cmd)

def get_feed(self, feed_type: Optional[FeedType]) -> Any:
"""Request a single feed
Arguments:
feed_type: Type of single feed to get: NVT, CERT or SCAP
Returns:
The response. See :py:meth:`send_command` for details.
"""
if not feed_type:
raise RequiredArgument(
function=self.get_feed.__name__, argument='feed_type'
)

if not isinstance(feed_type, FeedType):
raise InvalidArgumentType(
function=self.get_feed.__name__,
argument='feed_type',
arg_type=FeedType.__name__,
)

cmd = XmlCommand("get_feeds")
cmd.set_attribute("type", feed_type.value)

return self._send_xml_command(cmd)
24 changes: 22 additions & 2 deletions gvm/protocols/gmpv208/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
AssetType,
CredentialFormat,
CredentialType,
FeedType,
HostsOrdering,
PermissionSubjectType,
PortRangeType,
Expand All @@ -52,7 +51,6 @@
get_asset_type_from_string,
get_credential_format_from_string,
get_credential_type_from_string,
get_feed_type_from_string,
get_hosts_ordering_from_string,
get_permission_subject_type_from_string,
get_port_range_type_from_string,
Expand Down Expand Up @@ -191,6 +189,28 @@ def get_entity_type_from_string(
) from None


class FeedType(Enum):
"""Enum for feed types"""

NVT = "NVT"
CERT = "CERT"
SCAP = "SCAP"
GVMD_DATA = "GVMD_DATA"


def get_feed_type_from_string(feed_type: Optional[str]) -> Optional[FeedType]:
"""Convert a feed type string into a FeedType instance"""
if not feed_type:
return None

try:
return FeedType[feed_type.upper()]
except KeyError:
raise InvalidArgument(
argument='feed_type', function=get_feed_type_from_string.__name__
) from None


class FilterType(Enum):
"""Enum for filter types"""

Expand Down
4 changes: 4 additions & 0 deletions tests/protocols/gmpv208/test_new_gmpv208.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,7 @@ class Gmpv208ModifyReportFormatTestCase(

class Gmpv208ModifyTagTestCase(GmpModifyTagTestCase, Gmpv208TestCase):
pass


class Gmpv208GetFeedTestCase(GmpGetFeedTestCase, Gmpv208TestCase):
pass
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 @@ -266,10 +266,6 @@ class Gmpv208GetCredentialsTestCase(GmpGetCredentialsTestCase, Gmpv208TestCase):
pass


class Gmpv208GetFeedTestCase(GmpGetFeedTestCase, Gmpv208TestCase):
pass


class Gmpv208GetFeedsTestCase(GmpGetFeedsTestCase, 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 @@ -30,3 +30,4 @@
from .test_modify_permission import GmpModifyPermissionTestCase
from .test_modify_report_format import GmpModifyReportFormatTestCase
from .test_modify_tag import GmpModifyTagTestCase
from .test_get_feed import GmpGetFeedTestCase
72 changes: 72 additions & 0 deletions tests/protocols/gmpv208/testcmds/test_get_feed.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2018-2021 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 FeedType


class GmpGetFeedTestCase:
def test_get_feed(self):
"""
Test basic get_feed calls with only resource_type except special
cases for audit, policy, scan_config and task.
"""
self.gmp.get_feed(FeedType.NVT)

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

self.gmp.get_feed(FeedType.CERT)

self.connection.send.has_been_called_with('<get_feeds type="CERT"/>')

self.gmp.get_feed(FeedType.SCAP)

self.connection.send.has_been_called_with('<get_feeds type="SCAP"/>')

self.gmp.get_feed(FeedType.GVMD_DATA)

self.connection.send.has_been_called_with(
'<get_feeds type="GVMD_DATA"/>'
)

def test_get_feed_missing_type(self):
"""
Test get_feed calls with missing resource_type
"""
with self.assertRaises(RequiredArgument):
self.gmp.get_feed(feed_type=None)

with self.assertRaises(RequiredArgument):
self.gmp.get_feed(feed_type='')

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

def test_get_feed_invalid_type(self):
"""
Test get_feed calls with invalid resource_type
"""
with self.assertRaises(InvalidArgumentType):
self.gmp.get_feed('foo')


if __name__ == '__main__':
unittest.main()
54 changes: 54 additions & 0 deletions tests/protocols/gmpv208/testtypes/test_feed_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2019-2021 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 InvalidArgument
from gvm.protocols.gmpv208 import FeedType, get_feed_type_from_string


class GetFeedTypeFromStringTestCase(unittest.TestCase):
def test_invalid(self):
with self.assertRaises(InvalidArgument):
get_feed_type_from_string('foo')

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

def test_nvt(self):
ct = get_feed_type_from_string('nvt')
self.assertEqual(ct, FeedType.NVT)

def test_cert(self):
ct = get_feed_type_from_string('cert')
self.assertEqual(ct, FeedType.CERT)

def test_scap(self):
ct = get_feed_type_from_string('scap')
self.assertEqual(ct, FeedType.SCAP)

def test_gvmd_data(self):
ct = get_feed_type_from_string('gvmd_data')
self.assertEqual(ct, FeedType.GVMD_DATA)


if __name__ == '__main__':
unittest.main()
4 changes: 4 additions & 0 deletions tests/protocols/gmpv214/test_v208_from_gmpv214.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,7 @@ class Gmpv214ModifyReportFormatTestCase(

class Gmpv214ModifyTagTestCase(GmpModifyTagTestCase, Gmpv214TestCase):
pass


class Gmpv214GetFeedTestCase(GmpGetFeedTestCase, Gmpv214TestCase):
pass
4 changes: 0 additions & 4 deletions tests/protocols/gmpv214/test_v7_from_gmpv214.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,10 +258,6 @@ class Gmpv214GetCredentialsTestCase(GmpGetCredentialsTestCase, Gmpv214TestCase):
pass


class Gmpv214GetFeedTestCase(GmpGetFeedTestCase, Gmpv214TestCase):
pass


class Gmpv214GetFeedsTestCase(GmpGetFeedsTestCase, Gmpv214TestCase):
pass

Expand Down

0 comments on commit 59849f2

Please sign in to comment.