Skip to content

Commit

Permalink
Merge pull request #496 from y0urself/import-policy
Browse files Browse the repository at this point in the history
Add import_policy
  • Loading branch information
y0urself authored Jun 1, 2021
2 parents 68f7f36 + f8a8748 commit 8e23dd0
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Calendar Versioning](https://calver.org)html).

## [Unreleased]
### Added
Add `import_policy` API call [#496](https://github.com/greenbone/python-gvm/pull/496)

### Changed
### Deprecated
### Removed
Expand Down
27 changes: 27 additions & 0 deletions gvm/protocols/gmpv208/entities/policies.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.

from typing import Any, List, Optional, Tuple
from lxml.etree import XMLSyntaxError

from gvm.errors import RequiredArgument, InvalidArgument, InvalidArgumentType
from gvm.utils import add_filter, to_base64, to_bool, is_list_like
Expand Down Expand Up @@ -173,6 +174,32 @@ def get_policy(

return self._send_xml_command(cmd)

def import_policy(self, policy: str) -> Any:
"""Import a policy from XML
Arguments:
policy: Policy XML as string to import. This XML must
contain a :code:`<get_configs_response>` root element.
Returns:
The response. See :py:meth:`send_command` for details.
"""
if not policy:
raise RequiredArgument(
function=self.import_policy.__name__, argument='policy'
)

cmd = XmlCommand("create_config")

try:
cmd.append_xml_str(policy)
except XMLSyntaxError as e:
raise InvalidArgument(
function=self.import_policy.__name__, argument='policy'
) from e

return self._send_xml_command(cmd)

def modify_policy_set_nvt_preference(
self,
policy_id: str,
Expand Down
1 change: 1 addition & 0 deletions tests/protocols/gmpv208/entities/policies/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from .test_delete_policy import GmpDeletePolicyTestMixin
from .test_get_policy import GmpGetPolicyTestMixin
from .test_get_policies import GmpGetPoliciesTestMixin
from .test_import_policy import GmpImportPolicyTestMixin
from .test_modify_policy_set_comment import (
GmpModifyPolicySetCommentTestMixin,
)
Expand Down
51 changes: 51 additions & 0 deletions tests/protocols/gmpv208/entities/policies/test_import_policy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# -*- 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/>.

from gvm.errors import RequiredArgument, InvalidArgument


class GmpImportPolicyTestMixin:

POLICY_XML_STRING = (
'<get_configs_response status="200" status_text="OK">'
'<config id="c4aa21e4-23e6-4064-ae49-c0d425738a98">'
'<name>Foobar</name>'
'<comment>Foobar config</comment>'
'<creation_time>2018-11-09T10:48:03Z</creation_time>'
'<modification_time>2018-11-09T10:48:03Z</modification_time>'
'</config>'
'</get_configs_response>'
)

def test_import_policy(self):
self.gmp.import_policy(self.POLICY_XML_STRING)

self.connection.send.has_been_called_with(
'<create_config>' f'{self.POLICY_XML_STRING}' '</create_config>'
)

def test_import_missing_policy_xml(self):
with self.assertRaises(RequiredArgument):
self.gmp.import_policy(None)

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

def test_import_invalid_xml(self):
with self.assertRaises(InvalidArgument):
self.gmp.import_policy('abcdef')
5 changes: 5 additions & 0 deletions tests/protocols/gmpv208/entities/test_policies.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
GmpDeletePolicyTestMixin,
GmpGetPolicyTestMixin,
GmpGetPoliciesTestMixin,
GmpImportPolicyTestMixin,
GmpModifyPolicySetCommentTestMixin,
GmpModifyPolicySetFamilySelectionTestMixin,
GmpModifyPolicySetNameTestMixin,
Expand Down Expand Up @@ -52,6 +53,10 @@ class Gmpv208GetPoliciesTestCase(GmpGetPoliciesTestMixin, Gmpv208TestCase):
pass


class Gmpv208ImportPolicyTestCase(GmpImportPolicyTestMixin, Gmpv208TestCase):
pass


class Gmpv208ModifyPolicySetCommentTestCase(
GmpModifyPolicySetCommentTestMixin, Gmpv208TestCase
):
Expand Down
5 changes: 5 additions & 0 deletions tests/protocols/gmpv214/entities/test_policies.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
GmpDeletePolicyTestMixin,
GmpGetPolicyTestMixin,
GmpGetPoliciesTestMixin,
GmpImportPolicyTestMixin,
GmpModifyPolicySetCommentTestMixin,
GmpModifyPolicySetFamilySelectionTestMixin,
GmpModifyPolicySetNameTestMixin,
Expand Down Expand Up @@ -52,6 +53,10 @@ class Gmpv214GetPoliciesTestCase(GmpGetPoliciesTestMixin, Gmpv214TestCase):
pass


class Gmpv214ImportPolicyTestCase(GmpImportPolicyTestMixin, Gmpv214TestCase):
pass


class Gmpv214ModifyPolicySetCommentTestCase(
GmpModifyPolicySetCommentTestMixin, Gmpv214TestCase
):
Expand Down

0 comments on commit 8e23dd0

Please sign in to comment.