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

Update Gmp selection class #186

Merged
merged 5 commits into from
Dec 9, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Added
* Added DEFAULT_SSH_PORT and DEFAULT_HOSTNAME constants to `gmp.connection` [#185](https://github.com/greenbone/python-gvm/pull/185)
* Added `determine_remote_gmp_version` and `determine_supported_gmp` methods to
`gmp.protocols.gmp` module [#186](https://github.com/greenbone/python-gvm/pull/186)

### Changed
### Fixed

[Unreleased]: https://github.com/greenbone/python-gvm/compare/v1.1.0...master

## [1.1.0] - 2019-11-22

### Added
Expand Down
23 changes: 18 additions & 5 deletions gvm/protocols/gmp.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"""
Module for communication with gvmd
"""
from typing import Any, Optional, Callable
from typing import Any, Optional, Callable, Union

from gvm.errors import GvmError

Expand All @@ -33,6 +33,8 @@

from gvm.xml import XmlCommand

SupportedGmpVersion = Union[Gmpv7, Gmpv8, Gmpv9]


class Gmp(GvmProtocol):
"""Dynamically select supported GMP protocol of the remote manager daemon.
Expand Down Expand Up @@ -75,7 +77,9 @@ def __init__(
super().__init__(connection, transform=EtreeCheckCommandTransform())
self._gmp_transform = transform

def __enter__(self):
def determine_remote_gmp_version(self) -> str:
""" Determine the supported GMP version of the remote deamon
"""
self.connect()
resp = self._send_xml_command(XmlCommand("get_version"))
self.disconnect()
Expand All @@ -87,9 +91,14 @@ def __enter__(self):
'version information.'
)

version = version_el.text
major_version = int(version[0])
return version_el.text

def determine_supported_gmp(self) -> SupportedGmpVersion:
""" Determine supported GMP version of the remote deamon and return a
corresponding Gmp class instance
"""
version = self.determine_remote_gmp_version()
major_version = int(version[0])
if major_version == 7:
gmp_class = Gmpv7
elif major_version == 8:
Expand All @@ -102,7 +111,11 @@ def __enter__(self):
'The GMP version was {}.'.format(version)
)

gmp = gmp_class(self._connection, transform=self._gmp_transform)
return gmp_class(self._connection, transform=self._gmp_transform)

def __enter__(self):
gmp = self.determine_supported_gmp()

gmp.connect()

return gmp
12 changes: 12 additions & 0 deletions tests/protocols/gmp/test_context_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from gvm.protocols.gmp import Gmp
from gvm.protocols.gmpv7 import Gmp as Gmpv7
from gvm.protocols.gmpv8 import Gmp as Gmpv8
from gvm.protocols.gmpv9 import Gmp as Gmpv9


class GmpContextManagerTestCase(GmpTestCase):
Expand Down Expand Up @@ -53,6 +54,17 @@ def test_select_gmpv8(self):
self.assertEqual(gmp.get_protocol_version(), (8,))
self.assertIsInstance(gmp, Gmpv8)

def test_select_gmpv9(self):
self.connection.read.return_value(
'<get_version_response status="200" status_text="OK">'
'<version>9.0</version>'
'</get_version_response>'
)

with self.gmp as gmp:
self.assertEqual(gmp.get_protocol_version(), (9,))
self.assertIsInstance(gmp, Gmpv9)

def test_unknown_protocol(self):
self.connection.read.return_value(
'<get_version_response status="200" status_text="OK">'
Expand Down