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

[Refactor python-gvm API] Step 2: Detach Trashcan, Authentication, Version, (feed) #472

Merged
merged 16 commits into from
May 21, 2021
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
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ and this project adheres to [Calendar Versioning](https://calver.org)html).
* Introduced new explicit API calls for SecInfo: `get_nvt()`, `get_nvt_list()`, `get_cpe()`, `get_cpe_list()`, `get_cve()`, `get_cve_list()`, `get_cert_bund_advisory()`, `get_cert_bund_advisory_list()`, `get_dnf_cert_advisory()`, `get_dnf_cert_advisory_list()`, `get_oval_definition()`, `get_oval_definition_list()`. [#456](https://github.com/greenbone/python-gvm/pull/456)

### Changed
* API changes: `get_setting(s)` -> `get_user_setting(s)`, `modify_setting` -> `modify_user_setting`. [#472](https://github.com/greenbone/python-gvm/pull/472)
* Detached the Trashcan API calls from the GMP class into a new `TrashcanMixin`. [#472](https://github.com/greenbone/python-gvm/pull/472)
* Detached the Authentication API calls from the GMP class into a new `AuthenticationMixin`. [#472](https://github.com/greenbone/python-gvm/pull/472)
* Detached the Version API calls from the GMP class into a new `VersionMixin`. [#472](https://github.com/greenbone/python-gvm/pull/472)
* Changed `filter` to `filter_string` in getter functions. [#470](https://github.com/greenbone/python-gvm/pull/470) [#471](https://github.com/greenbone/python-gvm/pull/471)
* Detached the Preferences API calls from the GMP class into a new `PreferencesMixin`. [#471](https://github.com/greenbone/python-gvm/pull/471)
* Detached the Settings API calls from the GMP class into a new `UserSettingsMixin`. [#471](https://github.com/greenbone/python-gvm/pull/471)
Expand All @@ -26,7 +30,7 @@ and this project adheres to [Calendar Versioning](https://calver.org)html).
* Detached the Schedules API calls from the GMP class into a new `SchedulesMixin`. [#469](https://github.com/greenbone/python-gvm/pull/469)
* Detached the Filters API calls from the GMP class into a new `FiltersMixin`. [#469](https://github.com/greenbone/python-gvm/pull/469)
* Detached the Tags API calls from the GMP class into a new `TagsMixin`. [#468](https://github.com/greenbone/python-gvm/pull/468)
* Detached the Feeds API calls from the GMP class into a new `FeedsMixin`. [#468](https://github.com/greenbone/python-gvm/pull/468)
* Detached the Feeds API calls from the GMP class into a new `FeedMixin`. [#468](https://github.com/greenbone/python-gvm/pull/468) [#472](https://github.com/greenbone/python-gvm/pull/472)
* Detached the Aggregates API calls from the GMP class into a new `AggregatesMixin`. [#468](https://github.com/greenbone/python-gvm/pull/468)
* Detached the EntityType from the GMP types class into a new `entites` file. [#467](https://github.com/greenbone/python-gvm/pull/467)
* Detached the Users API calls from the GMP class into a new `UsersMixin`. [#467](https://github.com/greenbone/python-gvm/pull/467)
Expand Down Expand Up @@ -57,6 +61,7 @@ and this project adheres to [Calendar Versioning](https://calver.org)html).

### Deprecated
### Removed
* Removed `sync_feed`, `sync_scap`, and `sync_cert` calls. [#472](https://github.com/greenbone/python-gvm/pull/472)
* Removed `TimeUnit`. It was used for schedules before iCal and is not required anymore. [#469](https://github.com/greenbone/python-gvm/pull/469)
* Removed `Gmpv214Mixin`. [#467](https://github.com/greenbone/python-gvm/pull/467)
* Remove support of delete host/operating system by a report . [#459](https://github.com/greenbone/python-gvm/pull/459)
Expand Down
53 changes: 35 additions & 18 deletions gvm/protocols/gmpv208/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,6 @@
EntityType,
get_entity_type_from_string,
)
from gvm.protocols.gmpv208.entities.feeds import (
FeedType,
FeedsMixin,
get_feed_type_from_string,
)
from gvm.protocols.gmpv208.entities.filter import (
FiltersMixin,
FilterType,
Expand Down Expand Up @@ -130,25 +125,34 @@
get_ticket_status_from_string,
)
from gvm.protocols.gmpv208.entities.tls_certificates import TLSCertificateMixin
from gvm.protocols.gmpv208.entities.user_settings import UserSettingsMixin
from gvm.protocols.gmpv208.entities.users import (
UserAuthType,
UsersMixin,
get_user_auth_type_from_string,
)
from gvm.protocols.gmpv208.entities.vulnerabilities import VulnerabilitiesMixin
from gvm.connections import GvmConnection

PROTOCOL_VERSION = (20, 8)
from gvm.protocols.gmpv208.system.authentication import AuthenticationMixin
from gvm.protocols.gmpv208.system.feed import (
FeedType,
FeedMixin,
get_feed_type_from_string,
)
from gvm.protocols.gmpv208.system.trashcan import TrashcanMixin
from gvm.protocols.gmpv208.system.user_settings import UserSettingsMixin
from gvm.protocols.gmpv208.system.version import VersionMixin

from gvm.connections import GvmConnection


class Gmp(
GmpV208Mixin,
AggregatesMixin,
AlertsMixin,
AuditsMixin,
AuthenticationMixin,
CredentialsMixin,
FeedsMixin,
FeedMixin,
FiltersMixin,
GroupsMixin,
HostsMixin,
Expand All @@ -168,14 +172,36 @@ class Gmp(
TasksMixin,
TicketsMixin,
TLSCertificateMixin,
TrashcanMixin,
ScanConfigsMixin,
ScannersMixin,
SchedulesMixin,
SecInfoMixin,
UserSettingsMixin,
UsersMixin,
VersionMixin,
VulnerabilitiesMixin,
):
"""Python interface for Greenbone Management Protocol

This class implements the `Greenbone Management Protocol version 20.08`_

Arguments:
connection: Connection to use to talk with the gvmd daemon. See
:mod:`gvm.connections` for possible connection types.
transform: Optional transform `callable`_ to convert response data.
After each request the callable gets passed the plain response data
which can be used to check the data and/or conversion into different
representations like a xml dom.

See :mod:`gvm.transforms` for existing transforms.

.. _Greenbone Management Protocol version 20.08:
https://docs.greenbone.net/API/GMP/gmp-20.08.html
.. _callable:
https://docs.python.org/3/library/functions.html#callable
"""

def __init__(
self,
connection: GvmConnection,
Expand All @@ -186,12 +212,3 @@ def __init__(

# Is authenticated on gvmd
self._authenticated = False

@staticmethod
def get_protocol_version() -> tuple:
"""Determine the Greenbone Management Protocol version.

Returns:
tuple: Implemented version of the Greenbone Management Protocol
"""
return PROTOCOL_VERSION
167 changes: 1 addition & 166 deletions gvm/protocols/gmpv208/gmpv208.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,114 +30,20 @@

from typing import Any, Optional

from gvm.errors import InvalidArgument, RequiredArgument
from gvm.errors import InvalidArgument
from gvm.protocols.base import GvmProtocol


from gvm.utils import (
check_command_status,
to_bool,
)
from gvm.xml import XmlCommand

PROTOCOL_VERSION = (20, 8)


logger = logging.getLogger(__name__)


class GmpV208Mixin(GvmProtocol):
"""Python interface for Greenbone Management Protocol

This class implements the `Greenbone Management Protocol version 20.08`_

Arguments:
connection: Connection to use to talk with the gvmd daemon. See
:mod:`gvm.connections` for possible connection types.
transform: Optional transform `callable`_ to convert response data.
After each request the callable gets passed the plain response data
which can be used to check the data and/or conversion into different
representations like a xml dom.

See :mod:`gvm.transforms` for existing transforms.

.. _Greenbone Management Protocol version 20.08:
https://docs.greenbone.net/API/GMP/gmp-20.08.html
.. _callable:
https://docs.python.org/3/library/functions.html#callable
"""

def is_authenticated(self) -> bool:
"""Checks if the user is authenticated

If the user is authenticated privileged GMP commands like get_tasks
may be send to gvmd.

Returns:
bool: True if an authenticated connection to gvmd has been
established.
"""
return self._authenticated

def authenticate(self, username: str, password: str) -> Any:
"""Authenticate to gvmd.

The generated authenticate command will be send to server.
Afterwards the response is read, transformed and returned.

Arguments:
username: Username
password: Password

Returns:
Transformed response from server.
"""
cmd = XmlCommand("authenticate")

if not username:
raise RequiredArgument(
function=self.authenticate.__name__, argument='username'
)

if not password:
raise RequiredArgument(
function=self.authenticate.__name__, argument='password'
)

credentials = cmd.add_element("credentials")
credentials.add_element("username", username)
credentials.add_element("password", password)

self._send(cmd.to_string())
response = self._read()

if check_command_status(response):
self._authenticated = True

return self._transform(response)

def describe_auth(self) -> Any:
"""Describe authentication methods

Returns a list of all used authentication methods if such a list is
available.

Returns:
The response. See :py:meth:`send_command` for details.
"""
return self._send_xml_command(XmlCommand("describe_auth"))

def empty_trashcan(self) -> Any:
"""Empty the trashcan

Remove all entities from the trashcan. **Attention:** this command can
not be reverted

Returns:
The response. See :py:meth:`send_command` for details.
"""
return self._send_xml_command(XmlCommand("empty_trashcan"))

def get_system_reports(
self,
*,
Expand Down Expand Up @@ -189,13 +95,6 @@ def get_system_reports(

return self._send_xml_command(cmd)

def get_version(self) -> Any:
"""Get the Greenbone Manager Protocol version used by the remote gvmd
Returns:
The response. See :py:meth:`send_command` for details.
"""
return self._send_xml_command(XmlCommand("get_version"))

def help(
self, *, format: Optional[str] = None, help_type: Optional[str] = None
) -> Any:
Expand Down Expand Up @@ -230,67 +129,3 @@ def help(
cmd.set_attribute("format", format)

return self._send_xml_command(cmd)

def modify_auth(self, group_name: str, auth_conf_settings: dict) -> Any:
"""Modifies an existing auth.

Arguments:
group_name: Name of the group to be modified.
auth_conf_settings: The new auth config.

Returns:
The response. See :py:meth:`send_command` for details.
"""
if not group_name:
raise RequiredArgument(
function=self.modify_auth.__name__, argument='group_name'
)
if not auth_conf_settings:
raise RequiredArgument(
function=self.modify_auth.__name__,
argument='auth_conf_settings',
)
cmd = XmlCommand("modify_auth")
_xmlgroup = cmd.add_element("group", attrs={"name": str(group_name)})

for key, value in auth_conf_settings.items():
_xmlauthconf = _xmlgroup.add_element("auth_conf_setting")
_xmlauthconf.add_element("key", key)
_xmlauthconf.add_element("value", value)

return self._send_xml_command(cmd)

def restore(self, entity_id: str) -> Any:
"""Restore an entity from the trashcan

Arguments:
entity_id: ID of the entity to be restored from the trashcan

Returns:
The response. See :py:meth:`send_command` for details.
"""
if not entity_id:
raise RequiredArgument(
function=self.restore.__name__, argument='entity_id'
)

cmd = XmlCommand("restore")
cmd.set_attribute("id", entity_id)

return self._send_xml_command(cmd)

def sync_cert(self) -> Any:
"""Request a synchronization with the CERT feed service

Returns:
The response. See :py:meth:`send_command` for details.
"""
return self._send_xml_command(XmlCommand("sync_cert"))

def sync_scap(self) -> Any:
"""Request a synchronization with the SCAP feed service

Returns:
The response. See :py:meth:`send_command` for details.
"""
return self._send_xml_command(XmlCommand("sync_scap"))
17 changes: 17 additions & 0 deletions gvm/protocols/gmpv208/system/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# -*- coding: utf-8 -*-
# Copyright (C) 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/>.
Loading