Skip to content

Commit

Permalink
Merge pull request #165 from bjoernricks/gmpv9-improvements
Browse files Browse the repository at this point in the history
Gmpv9 improvements
  • Loading branch information
bjoernricks authored Oct 16, 2019
2 parents fbe1e9a + 63a9e62 commit 2f01677
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 34 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
* 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)
support [#157](https://github.com/greenbone/python-gvm/pull/157),
[#165](https://github.com/greenbone/python-gvm/pull/165)
* 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)

### Changed
* Use Gmpv9 in gvm.protocols.latest module [#165](https://github.com/greenbone/python-gvm/pull/165)
* Added type `TLS_CERTIFICATE` to `EntityType` and `FilterType` [#157](https://github.com/greenbone/python-gvm/pull/157)
* Changed the `DEFAULT_UNIX_SOCKET_PATH` [#119](https://github.com/greenbone/python-gvm/pull/162)

Expand Down
92 changes: 92 additions & 0 deletions docs/api/gmpv9.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
.. _gmpv9:

GMP v9
^^^^^^

.. automodule:: gvm.protocols.gmpv9

Enums
-----

.. autoclass:: AlertCondition
:members:
:undoc-members:

.. autoclass:: AlertEvent
:members:
:undoc-members:

.. autoclass:: AlertMethod
:members:
:undoc-members:

.. autoclass:: AliveTest
:members:
:undoc-members:

.. autoclass:: AssetType
:members:
:undoc-members:

.. autoclass:: CredentialFormat
:members:
:undoc-members:

.. autoclass:: CredentialType
:members:
:undoc-members:

.. autoclass:: EntityType
:members:
:undoc-members:

.. autoclass:: FeedType
:members:
:undoc-members:

.. autoclass:: FilterType
:members:
:undoc-members:

.. autoclass:: HostsOrdering
:members:
:undoc-members:

.. autoclass:: InfoType
:members:
:undoc-members:

.. autoclass:: PermissionSubjectType
:members:
:undoc-members:

.. autoclass:: ScannerType
:members:
:undoc-members:

.. autoclass:: PortRangeType
:members:
:undoc-members:

.. autoclass:: SeverityLevel
:members:
:undoc-members:

.. autoclass:: SnmpAuthAlgorithm
:members:
:undoc-members:

.. autoclass:: SnmpPrivacyAlgorithm
:members:
:undoc-members:

.. autoclass:: TicketStatus
:members:
:undoc-members:

Protocol
--------

.. autoclass:: Gmp
:members:
:inherited-members:
1 change: 1 addition & 0 deletions docs/api/protocols.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Protocols

gmpv7
gmpv8
gmpv9
ospv1

Dynamic
Expand Down
7 changes: 5 additions & 2 deletions gvm/protocols/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@
"""
Package for supported Greenbone Protocol versions.
Currently `GMP version 7`_, `GMP version 8`_ and `OSP version 1`_ are supported.
Currently `GMP version 7`_, `GMP version 8`_, `GMP version 9`_ and
`OSP version 1`_ are supported.
.. _GMP version 7:
https://docs.greenbone.net/API/GMP/gmp-7.0.html
.. _GMP version 8:
https://docs.greenbone.net/API/GMP/gmp-8.0.html
.. _GMP version 9:
https://docs.greenbone.net/API/GMP/gmp-9.0.html
.. _OSP version 1:
https://docs.greenbone.net/API/OSP/osp-1.1.html
https://docs.greenbone.net/API/OSP/osp-1.2.html
"""
11 changes: 7 additions & 4 deletions gvm/protocols/gmp.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

from gvm.protocols.gmpv7 import Gmp as Gmpv7
from gvm.protocols.gmpv8 import Gmp as Gmpv8
from gvm.protocols.gmpv9 import Gmp as Gmpv9

from gvm.transforms import EtreeCheckCommandTransform

Expand All @@ -46,9 +47,9 @@ class Gmp(GvmProtocol):
from gvm.protocols.gmp import Gmp
with Gmp(connection) as gmp:
# gmp can be an instance of gvm.protocols.gmpv7.Gmp or
# gvm.protocols.gmpv8.Gmp depending on the supported GMP
# version of the remote manager daemon
# gmp can be an instance of gvm.protocols.gmpv7.Gmp,
# gvm.protocols.gmpv8.Gmp or gvm.protocols.gmpv9.Gmp depending
# on the supported GMP version of the remote manager daemon
resp = gmp.get_tasks()
Attributes:
Expand Down Expand Up @@ -91,8 +92,10 @@ def __enter__(self):

if major_version == 7:
gmp_class = Gmpv7
elif major_version >= 8:
elif major_version == 8:
gmp_class = Gmpv8
elif major_version >= 9:
gmp_class = Gmpv9
else:
raise GvmError(
'Remote manager daemon uses an unsupported version of GMP. '
Expand Down
37 changes: 15 additions & 22 deletions gvm/protocols/gmpv9/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@
from gvm.protocols.gmpv7 import _is_list_like, _to_comma_list

from . import types
from .types import HostsOrdering, EntityType, FilterType
from .types import get_entity_type_from_string, get_filter_type_from_string
from .types import *
from .types import _UsageType as UsageType

PROTOCOL_VERSION = (9,)
Expand Down Expand Up @@ -96,7 +95,7 @@ def create_audit(
The response. See :py:meth:`send_command` for details.
"""

self.__create_task(
return self.__create_task(
name=name,
config_id=audit_id,
target_id=target_id,
Expand All @@ -115,33 +114,35 @@ def create_audit(

def create_config(self, config_id: str, name: str) -> Any:
"""Create a new scan config
Arguments:
config_id: UUID of the existing scan config
name: Name of the new scan config
Returns:
The response. See :py:meth:`send_command` for details.
"""
self.__create_config(
return self.__create_config(
config_id=config_id,
name=name,
usage_type=UsageType.SCAN, # pylint: disable=W0212
usage_type=UsageType.SCAN,
function=self.create_config.__name__,
)

def create_policy(self, policy_id: str, name: str) -> Any:
"""Create a new policy config
Arguments:
policy_id: UUID of the existing policy config
name: Name of the new scan config
Returns:
The response. See :py:meth:`send_command` for details.
"""
self.__create_config(
return self.__create_config(
config_id=policy_id,
name=name,
usage_type=UsageType.POLICY, # pylint: disable=W0212
usage_type=UsageType.POLICY,
function=self.create_policy.__name__,
)

Expand Down Expand Up @@ -182,12 +183,12 @@ def create_task(
Returns:
The response. See :py:meth:`send_command` for details.
"""
self.__create_task(
return self.__create_task(
name=name,
config_id=config_id,
target_id=target_id,
scanner_id=scanner_id,
usage_type=UsageType.SCAN, # pylint: disable=W0212
usage_type=UsageType.SCAN,
function=self.create_task.__name__,
alterable=alterable,
hosts_ordering=hosts_ordering,
Expand All @@ -205,20 +206,19 @@ def create_tls_certificate(
certificate: str,
*,
comment: Optional[str] = None,
copy: Optional[str] = None,
trust: Optional[bool] = None
) -> Any:
"""Create a new TLS certificate
Arguments:
comment: Comment for the TLS certificate.
name: Name of the TLS certificate, defaulting to the MD5 fingerprint
copy: The UUID of an existing 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).
Returns:
The response. See :py:meth:`send_command`for details.
The response. See :py:meth:`send_command` for details.
"""
if not name:
raise RequiredArgument(
Expand All @@ -235,9 +235,6 @@ def create_tls_certificate(
if comment:
cmd.add_element("comment", comment)

if copy:
cmd.add_element("copy", copy)

cmd.add_element("name", name)
cmd.add_element("certificate", certificate)

Expand Down Expand Up @@ -340,7 +337,7 @@ def __create_task(
config_id: str,
target_id: str,
scanner_id: str,
usage_type: types._UsageType, # pylint: disable=W0212
usage_type: UsageType,
function: str,
*,
alterable: Optional[bool] = None,
Expand Down Expand Up @@ -449,11 +446,7 @@ def __create_task(
return self._send_xml_command(cmd)

def __create_config(
self,
config_id: str,
name: str,
usage_type: types._UsageType, # pylint: disable=W0212
function: str,
self, config_id: str, name: str, usage_type: UsageType, function: str
) -> Any:
if not name:
raise RequiredArgument("{} requires name argument".format(function))
Expand Down
4 changes: 2 additions & 2 deletions gvm/protocols/latest.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@
:py:mod:`gvm.protocols`.
Exports:
- :py:class:`gvm.protocols.gmpv8.Gmp`
- :py:class:`gvm.protocols.gmpv9.Gmp`
- :py:class:`gvm.protocols.ospv1.Osp`
.. _Greenbone Management Protocol:
https://docs.greenbone.net/API/GMP/gmp.html
"""

from .gmpv8 import (
from .gmpv9 import (
Gmp,
AlertCondition,
AlertEvent,
Expand Down
3 changes: 1 addition & 2 deletions tests/protocols/gmpv9/test_create_tls_certificate.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,11 @@

class GmpCreateTLSCertificateTestCase(Gmpv9TestCase):
def test_create_tls_certificate(self):
self.gmp.create_tls_certificate('foo', 'c1', copy='a1', comment='bar')
self.gmp.create_tls_certificate('foo', 'c1', comment='bar')

self.connection.send.has_been_called_with(
'<create_tls_certificate>'
'<comment>bar</comment>'
'<copy>a1</copy>'
'<name>foo</name>'
'<certificate>c1</certificate>'
'</create_tls_certificate>'
Expand Down
2 changes: 1 addition & 1 deletion tests/protocols/test_latest.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

class LatestProtocolsTestCase(unittest.TestCase):
def test_gmp_version(self):
self.assertEqual(Gmp.get_protocol_version(), (8,))
self.assertEqual(Gmp.get_protocol_version(), (9,))

def test_osp_version(self):
self.assertEqual(Osp.get_protocol_version(), (1, 2))
Expand Down

0 comments on commit 2f01677

Please sign in to comment.