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

Improve gmpv9 #166

Merged
merged 5 commits into from
Oct 16, 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
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Added ignore_pagination and details arguments for get_report [#163](https://github.com/greenbone/python-gvm/pull/163)
* Introduced Gmpv9 for [GMP 9](https://docs.greenbone.net/API/GMP/gmp-9.0.html)
support [#157](https://github.com/greenbone/python-gvm/pull/157),
[#165](https://github.com/greenbone/python-gvm/pull/165)
[#165](https://github.com/greenbone/python-gvm/pull/165),
[#166](https://github.com/greenbone/python-gvm/pull/166)
* Added new `create_audit` method, to create a task with the `usage_type` `audit` [#157](https://github.com/greenbone/python-gvm/pull/157)
* Added new `create_policy` method, to create a config with the `usage_type` `policy` [#157](https://github.com/greenbone/python-gvm/pull/157)
* Added the new methods `create_tls_certificate`, `modify_tls_certificate` and `clone_tls_certificate` to create, modify and copy TLS certificates [#157](https://github.com/greenbone/python-gvm/pull/157)
* Added the new method `get_tls_certificates`, to request TLS certificates from the server [#157](https://github.com/greenbone/python-gvm/pull/157)
* Added the new method `get_tls_certificate`, to request a single TLS certificate
from the server [#166](https://github.com/greenbone/python-gvm/pull/166)

### Changed
* Use Gmpv9 in gvm.protocols.latest module [#165](https://github.com/greenbone/python-gvm/pull/165)
Expand Down
46 changes: 37 additions & 9 deletions gvm/protocols/gmpv9/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
from .types import *
from .types import _UsageType as UsageType

_EMPTY_POLICY_ID = '085569ce-73ed-11df-83c3-002264764cea'

PROTOCOL_VERSION = (9,)


Expand All @@ -60,7 +62,7 @@ def get_protocol_version() -> tuple:
def create_audit(
self,
name: str,
audit_id: str,
policy_id: str,
target_id: str,
scanner_id: str,
*,
Expand All @@ -77,7 +79,7 @@ def create_audit(

Arguments:
name: Name of the new audit
audit_id: UUID of scan config to use by the audit
policy_id: UUID of policy to use by the audit
target_id: UUID of target to be scanned
scanner_id: UUID of scanner to use for scanning the target
comment: Comment for the audit
Expand All @@ -97,10 +99,10 @@ def create_audit(

return self.__create_task(
name=name,
config_id=audit_id,
config_id=policy_id,
target_id=target_id,
scanner_id=scanner_id,
usage_type=UsageType.AUDIT, # pylint: disable=W0212
usage_type=UsageType.AUDIT,
function=self.create_audit.__name__,
alterable=alterable,
hosts_ordering=hosts_ordering,
Expand Down Expand Up @@ -129,16 +131,19 @@ def create_config(self, config_id: str, name: str) -> Any:
function=self.create_config.__name__,
)

def create_policy(self, policy_id: str, name: str) -> Any:
def create_policy(self, name: str, *, policy_id: str = None) -> Any:
"""Create a new policy config

Arguments:
policy_id: UUID of the existing policy config
name: Name of the new scan config
name: Name of the new policy
policy_id: UUID of an existing policy as base. By default the empty
policy is used.

Returns:
The response. See :py:meth:`send_command` for details.
"""
if policy_id is None:
policy_id = _EMPTY_POLICY_ID
return self.__create_config(
config_id=policy_id,
name=name,
Expand Down Expand Up @@ -211,11 +216,11 @@ def create_tls_certificate(
"""Create a new TLS certificate

Arguments:
comment: Comment for the TLS certificate.
name: Name of the TLS certificate, defaulting to the MD5
fingerprint.
trust: Whether the certificate is trusted.
certificate: The Base64 encoded certificate data (x.509 DER or PEM).
comment: Comment for the TLS certificate.
trust: Whether the certificate is trusted.

Returns:
The response. See :py:meth:`send_command` for details.
Expand Down Expand Up @@ -255,6 +260,8 @@ def get_tls_certificates(
Arguments:
filter: Filter term to use for the query
filter_id: UUID of an existing filter to use for the query
include_certificate_data: Wether to include the certifacte data in
the response

Returns:
The response. See :py:meth:`send_command` for details.
Expand All @@ -271,6 +278,27 @@ def get_tls_certificates(

return self._send_xml_command(cmd)

def get_tls_certificate(self, tls_certificate_id: str) -> Any:
"""Request a single TLS certificate

Arguments:
tls_certificate_id: UUID of an existing TLS certificate

Returns:
The response. See :py:meth:`send_command` for details.
"""
if not tls_certificate_id:
raise RequiredArgument(
"get_tls_certificate requires tls_certificate_id argument"
)

cmd = XmlCommand("get_tls_certificates")
cmd.set_attribute("tls_certificate_id", tls_certificate_id)

# for single tls certificate always request cert data
cmd.set_attribute("include_certificate_data", "1")
return self._send_xml_command(cmd)

def modify_tls_certificate(
self,
tls_certificate_id: str,
Expand Down
62 changes: 31 additions & 31 deletions tests/protocols/gmpv9/test_create_audit.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
class GmpCreateAuditCommandTestCase(Gmpv9TestCase):
def test_create_task(self):
self.gmp.create_audit(
name='foo', audit_id='c1', target_id='t1', scanner_id='s1'
name='foo', policy_id='c1', target_id='t1', scanner_id='s1'
)

self.connection.send.has_been_called_with(
Expand All @@ -47,51 +47,51 @@ def test_create_task(self):
def test_create_audit_missing_name(self):
with self.assertRaises(RequiredArgument):
self.gmp.create_audit(
name=None, audit_id='c1', target_id='t1', scanner_id='s1'
name=None, policy_id='c1', target_id='t1', scanner_id='s1'
)

with self.assertRaises(RequiredArgument):
self.gmp.create_audit(
name='', audit_id='c1', target_id='t1', scanner_id='s1'
name='', policy_id='c1', target_id='t1', scanner_id='s1'
)

def test_create_audit_missing_audit_id(self):
def test_create_audit_missing_policy_id(self):
with self.assertRaises(RequiredArgument):
self.gmp.create_audit(
name='foo', audit_id=None, target_id='t1', scanner_id='s1'
name='foo', policy_id=None, target_id='t1', scanner_id='s1'
)

with self.assertRaises(RequiredArgument):
self.gmp.create_audit(
name='foo', audit_id='', target_id='t1', scanner_id='s1'
name='foo', policy_id='', target_id='t1', scanner_id='s1'
)

def test_create_audit_missing_target_id(self):
with self.assertRaises(RequiredArgument):
self.gmp.create_audit(
name='foo', audit_id='c1', target_id=None, scanner_id='s1'
name='foo', policy_id='c1', target_id=None, scanner_id='s1'
)

with self.assertRaises(RequiredArgument):
self.gmp.create_audit(
name='foo', audit_id='c1', target_id='', scanner_id='s1'
name='foo', policy_id='c1', target_id='', scanner_id='s1'
)

def test_create_audit_missing_scanner_id(self):
with self.assertRaises(RequiredArgument):
self.gmp.create_audit(
name='foo', audit_id='c1', target_id='t1', scanner_id=None
name='foo', policy_id='c1', target_id='t1', scanner_id=None
)

with self.assertRaises(RequiredArgument):
self.gmp.create_audit(
name='foo', audit_id='c1', target_id='t1', scanner_id=''
name='foo', policy_id='c1', target_id='t1', scanner_id=''
)

def test_create_audit_with_comment(self):
self.gmp.create_audit(
name='foo',
audit_id='c1',
policy_id='c1',
target_id='t1',
scanner_id='s1',
comment='bar',
Expand All @@ -115,7 +115,7 @@ def test_create_audit_single_alert(self):

self.gmp.create_audit(
name='foo',
audit_id='c1',
policy_id='c1',
target_id='t1',
scanner_id='s1',
alert_ids='a1', # will be removed in future
Expand All @@ -137,7 +137,7 @@ def test_create_audit_single_alert(self):

self.gmp.create_audit(
name='foo',
audit_id='c1',
policy_id='c1',
target_id='t1',
scanner_id='s1',
alert_ids=['a1'],
Expand All @@ -157,7 +157,7 @@ def test_create_audit_single_alert(self):
def test_create_audit_multiple_alerts(self):
self.gmp.create_audit(
name='foo',
audit_id='c1',
policy_id='c1',
target_id='t1',
scanner_id='s1',
alert_ids=['a1', 'a2', 'a3'],
Expand All @@ -179,7 +179,7 @@ def test_create_audit_multiple_alerts(self):
def test_create_audit_with_alterable(self):
self.gmp.create_audit(
name='foo',
audit_id='c1',
policy_id='c1',
target_id='t1',
scanner_id='s1',
alterable=True,
Expand All @@ -198,7 +198,7 @@ def test_create_audit_with_alterable(self):

self.gmp.create_audit(
name='foo',
audit_id='c1',
policy_id='c1',
target_id='t1',
scanner_id='s1',
alterable=False,
Expand All @@ -218,7 +218,7 @@ def test_create_audit_with_alterable(self):
def test_create_audit_with_hosts_ordering(self):
self.gmp.create_audit(
name='foo',
audit_id='c1',
policy_id='c1',
target_id='t1',
scanner_id='s1',
hosts_ordering=HostsOrdering.REVERSE,
Expand All @@ -239,7 +239,7 @@ def test_create_audit_invalid_hosts_ordering(self):
with self.assertRaises(InvalidArgument):
self.gmp.create_audit(
name='foo',
audit_id='c1',
policy_id='c1',
target_id='t1',
scanner_id='s1',
hosts_ordering='foo',
Expand All @@ -248,7 +248,7 @@ def test_create_audit_invalid_hosts_ordering(self):
def test_create_audit_with_schedule(self):
self.gmp.create_audit(
name='foo',
audit_id='c1',
policy_id='c1',
target_id='t1',
scanner_id='s1',
schedule_id='s1',
Expand All @@ -268,7 +268,7 @@ def test_create_audit_with_schedule(self):
def test_create_audit_with_schedule_and_schedule_periods(self):
self.gmp.create_audit(
name='foo',
audit_id='c1',
policy_id='c1',
target_id='t1',
scanner_id='s1',
schedule_id='s1',
Expand All @@ -289,7 +289,7 @@ def test_create_audit_with_schedule_and_schedule_periods(self):

self.gmp.create_audit(
name='foo',
audit_id='c1',
policy_id='c1',
target_id='t1',
scanner_id='s1',
schedule_id='s1',
Expand All @@ -312,7 +312,7 @@ def test_create_audit_with_schedule_and_invalid_schedule_periods(self):
with self.assertRaises(InvalidArgument):
self.gmp.create_audit(
name='foo',
audit_id='c1',
policy_id='c1',
target_id='t1',
scanner_id='s1',
schedule_id='s1',
Expand All @@ -322,7 +322,7 @@ def test_create_audit_with_schedule_and_invalid_schedule_periods(self):
with self.assertRaises(InvalidArgument):
self.gmp.create_audit(
name='foo',
audit_id='c1',
policy_id='c1',
target_id='t1',
scanner_id='s1',
schedule_id='s1',
Expand All @@ -332,7 +332,7 @@ def test_create_audit_with_schedule_and_invalid_schedule_periods(self):
def test_create_audit_with_observers(self):
self.gmp.create_audit(
name='foo',
audit_id='c1',
policy_id='c1',
target_id='t1',
scanner_id='s1',
observers=['u1', 'u2'],
Expand All @@ -353,7 +353,7 @@ def test_create_audit_invalid_observers(self):
with self.assertRaises(InvalidArgument):
self.gmp.create_audit(
name='foo',
audit_id='c1',
policy_id='c1',
target_id='t1',
scanner_id='s1',
observers='',
Expand All @@ -362,7 +362,7 @@ def test_create_audit_invalid_observers(self):
with self.assertRaises(InvalidArgument):
self.gmp.create_audit(
name='foo',
audit_id='c1',
policy_id='c1',
target_id='t1',
scanner_id='s1',
observers='foo',
Expand All @@ -371,7 +371,7 @@ def test_create_audit_invalid_observers(self):
def test_create_audit_with_preferences(self):
self.gmp.create_audit(
name='foo',
audit_id='c1',
policy_id='c1',
target_id='t1',
scanner_id='s1',
preferences=OrderedDict([('foo', 'bar'), ('lorem', 'ipsum')]),
Expand Down Expand Up @@ -401,7 +401,7 @@ def test_create_audit_invalid_preferences(self):
with self.assertRaises(InvalidArgument):
self.gmp.create_audit(
name='foo',
audit_id='c1',
policy_id='c1',
target_id='t1',
scanner_id='s1',
preferences='',
Expand All @@ -410,7 +410,7 @@ def test_create_audit_invalid_preferences(self):
with self.assertRaises(InvalidArgument):
self.gmp.create_audit(
name='foo',
audit_id='c1',
policy_id='c1',
target_id='t1',
scanner_id='s1',
preferences=['foo', 'bar'],
Expand All @@ -420,7 +420,7 @@ def test_create_audit_don_t_allow_container_task(self):
with self.assertRaises(InvalidArgument):
self.gmp.create_audit(
name='foo',
audit_id='c1',
policy_id='c1',
target_id='0',
scanner_id='s1',
observers='',
Expand All @@ -430,7 +430,7 @@ def test_create_audit_don_t_allow_container_task(self):
with self.assertRaises(RequiredArgument):
self.gmp.create_audit(
name='foo',
audit_id='c1',
policy_id='c1',
target_id=0,
scanner_id='s1',
observers='',
Expand Down
Loading