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

Add import_policy #496

Merged
merged 5 commits into from
Jun 1, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
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 scan config from XML

Arguments:
policy: Scan Config 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