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

Adding GMPv9 functionality to python-gvm #157

Merged
merged 38 commits into from
Sep 25, 2019
Merged
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
4c597f6
Added functionality for gmpv9.
y0urself Sep 19, 2019
3504e4b
Init test files
y0urself Sep 19, 2019
63d17d3
removed some import issues
y0urself Sep 19, 2019
417eaef
Ordering matters.
y0urself Sep 19, 2019
1a3e933
More import issues fixed
y0urself Sep 19, 2019
6c877ca
First Test Script passed
y0urself Sep 19, 2019
194422b
create_audit tests passed
y0urself Sep 19, 2019
b5ffc25
Tests for Create Config and Policy
y0urself Sep 19, 2019
f5f5529
Added some missing defaults, missing optional arguments, ...
y0urself Sep 19, 2019
9d35348
Added tests for create_tls_certificate
y0urself Sep 19, 2019
186c854
Reordered arguments comments
y0urself Sep 19, 2019
ad63037
Fixes in test_create_tls_certificate.py, added tests for modify_tls_c…
y0urself Sep 19, 2019
48c1040
used set_attribute instead of add_element for include_certificate_data
y0urself Sep 19, 2019
d1899d5
Added tests for get_tls_certificates
y0urself Sep 19, 2019
6a63569
Minor refactoring, removed import
y0urself Sep 19, 2019
282f537
linting
y0urself Sep 19, 2019
d0db493
suppressing protected-access
y0urself Sep 19, 2019
ab455f6
running black again
y0urself Sep 19, 2019
e1d03ec
whitespace cancer ...
y0urself Sep 19, 2019
c82f777
removed duplicated code
y0urself Sep 19, 2019
2bfd970
corrected comments
y0urself Sep 20, 2019
44d6d0d
changed import
y0urself Sep 20, 2019
bb4cff5
formatting ...
y0urself Sep 20, 2019
d4ab8ff
Corrected naming of variables and comments.
y0urself Sep 24, 2019
1628db8
Deleted bandit file again
y0urself Sep 24, 2019
ebe7f92
Updated tests
y0urself Sep 24, 2019
5bc98bf
Added Filter, Tag support for TLS Certificate, Audit, Policy ...
y0urself Sep 24, 2019
2e40e3b
Wildcard imports are deprecated ...
y0urself Sep 24, 2019
927b8e0
Added Tests for EntityType and FilterType
y0urself Sep 24, 2019
54f1dc2
Reformatting
y0urself Sep 24, 2019
ca4c255
Renamed ENUM-Type to
y0urself Sep 24, 2019
aa63111
Introduced clone_tls_certificate method to copy existing certificates…
y0urself Sep 24, 2019
5a78ee5
Formatting
y0urself Sep 24, 2019
ca45d94
Removed the types POLICY and AUDIT from EntityType and FilterType
y0urself Sep 25, 2019
e8606a5
Updated CHANGELOG.md
y0urself Sep 25, 2019
7ce2363
formatting :/
y0urself Sep 25, 2019
99c6091
Edited CHANGELOG.md
y0urself Sep 25, 2019
05575b2
Merge branch 'master' into master
bjoernricks Sep 25, 2019
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
454 changes: 454 additions & 0 deletions gvm/protocols/gmpv9/__init__.py

Large diffs are not rendered by default.

277 changes: 277 additions & 0 deletions gvm/protocols/gmpv9/types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,277 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2019 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 enum import Enum

from typing import Optional

from gvm.errors import InvalidArgument

from gvm.protocols.gmpv8.types import (
AlertCondition,
AlertEvent,
AlertMethod,
AliveTest,
AssetType,
CredentialFormat,
CredentialType,
FeedType,
HostsOrdering,
InfoType,
PermissionSubjectType,
PortRangeType,
ScannerType,
SeverityLevel,
SnmpAuthAlgorithm,
SnmpPrivacyAlgorithm,
TicketStatus,
TimeUnit,
get_alert_condition_from_string,
get_alert_event_from_string,
get_alert_method_from_string,
get_alive_test_from_string,
get_asset_type_from_string,
get_credential_format_from_string,
get_credential_type_from_string,
get_feed_type_from_string,
get_hosts_ordering_from_string,
get_info_type_from_string,
get_permission_subject_type_from_string,
get_port_range_type_from_string,
get_severity_level_from_string,
get_scanner_type_from_string,
get_snmp_auth_algorithm_from_string,
get_snmp_privacy_algorithm_from_string,
get_ticket_status_from_string,
)


__all__ = [
"AlertCondition",
"AlertEvent",
"AlertMethod",
"AliveTest",
"AssetType",
"CredentialFormat",
"CredentialType",
"EntityType",
"FeedType",
"FilterType",
"HostsOrdering",
"InfoType",
"PermissionSubjectType",
"PortRangeType",
"ScannerType",
"SeverityLevel",
"SnmpAuthAlgorithm",
"SnmpPrivacyAlgorithm",
"TicketStatus",
"TimeUnit",
"get_alert_condition_from_string",
"get_alert_event_from_string",
"get_alert_method_from_string",
"get_alive_test_from_string",
"get_asset_type_from_string",
"get_credential_format_from_string",
"get_credential_type_from_string",
"get_entity_type_from_string",
"get_feed_type_from_string",
"get_filter_type_from_string",
"get_hosts_ordering_from_string",
"get_info_type_from_string",
"get_permission_subject_type_from_string",
"get_port_range_type_from_string",
"get_scanner_type_from_string",
"get_severity_level_from_string",
"get_snmp_auth_algorithm_from_string",
"get_snmp_privacy_algorithm_from_string",
"get_ticket_status_from_string",
]


class EntityType(Enum):
""" Enum for entity types """

AGENT = "note"
ALERT = "alert"
ASSET = "asset"
AUDIT = "audit"
CERT_BUND_ADV = "cert_bund_adv"
CPE = "cpe"
CREDENTIAL = "credential"
CVE = "cve"
DFN_CERT_ADV = "dfn_cert_adv"
FILTER = "filter"
GROUP = "group"
HOST = "host"
INFO = "info"
NOTE = "note"
NVT = "nvt"
OPERATING_SYSTEM = "os"
OVALDEF = "ovaldef"
OVERRIDE = "override"
PERMISSION = "permission"
POLICY_CONFIG = "policy"
y0urself marked this conversation as resolved.
Show resolved Hide resolved
PORT_LIST = "port_list"
REPORT = "report"
REPORT_FORMAT = "report_format"
RESULT = "result"
ROLE = "role"
SCAN_CONFIG = "config"
SCANNER = "scanner"
SCHEDULE = "schedule"
TAG = "tag"
TARGET = "target"
TASK = "task"
TICKET = "ticket"
TLS_CERTIFICATE = "tls_certificate"
USER = "user"
VULNERABILITY = "vuln"


def get_entity_type_from_string(
entity_type: Optional[str]
) -> Optional[EntityType]:
""" Convert a entity type string to an actual EntityType instance

Arguments:
entity_type: Entity type string to convert to a EntityType
"""
if not entity_type:
return None

if entity_type == 'vuln':
return EntityType.VULNERABILITY

if entity_type == 'os':
return EntityType.OPERATING_SYSTEM

if entity_type == 'config':
return EntityType.SCAN_CONFIG

if entity_type == 'policy':
return EntityType.POLICY_CONFIG

if entity_type == 'tls_certificate':
return EntityType.TLS_CERTIFICATE

try:
return EntityType[entity_type.upper()]
except KeyError:
raise InvalidArgument(
argument='entity_type',
function=get_entity_type_from_string.__name__,
)


class FilterType(Enum):
""" Enum for filter types """

AGENT = "agent"
ALERT = "alert"
ASSET = "asset"
AUDIT = "audit"
SCAN_CONFIG = "config"
CREDENTIAL = "credential"
FILTER = "filter"
GROUP = "group"
HOST = "host"
NOTE = "note"
OPERATING_SYSTEM = "os"
OVERRIDE = "override"
PERMISSION = "permission"
PORT_LIST = "port_list"
POLICY_CONFIG = "policy"
y0urself marked this conversation as resolved.
Show resolved Hide resolved
REPORT = "report"
REPORT_FORMAT = "report_format"
RESULT = "result"
ROLE = "role"
SCHEDULE = "schedule"
ALL_SECINFO = "secinfo"
TAG = "tag"
TARGET = "target"
TASK = "task"
TICKET = "ticket"
TLS_CERTIFICATE = "tls_certificate"
USER = "user"
VULNERABILITY = "vuln"


def get_filter_type_from_string(
filter_type: Optional[str]
) -> Optional[FilterType]:
""" Convert a filter type string to an actual FilterType instance

Arguments:
filter_type (str): Filter type string to convert to a FilterType
"""
if not filter_type:
return None

if filter_type == 'vuln':
return FilterType.VULNERABILITY

if filter_type == 'os':
return FilterType.OPERATING_SYSTEM

if filter_type == 'config':
return FilterType.SCAN_CONFIG

if filter_type == 'secinfo':
return FilterType.ALL_SECINFO

if filter_type == 'policy':
return FilterType.POLICY_CONFIG

if filter_type == 'tls_certificate':
return FilterType.TLS_CERTIFICATE

try:
return FilterType[filter_type.upper()]
except KeyError:
raise InvalidArgument(
argument='filter_type',
function=get_filter_type_from_string.__name__,
)


class _UsageType(Enum):
""" Enum for usage types """

AUDIT = "audit"
POLICY = "policy"
SCAN = "scan"


def __get_usage_type_from_string(
usage_type: Optional[str]
) -> Optional[_UsageType]:
""" Convert a usage type string to an actual _UsageType instance

Arguments:
entity_type: Usage type string to convert to a _UsageType
"""
if not usage_type:
return None

try:
return _UsageType[usage_type.upper()]
except KeyError:
raise InvalidArgument(
argument='usage_type',
function=__get_usage_type_from_string.__name__,
)
24 changes: 19 additions & 5 deletions tests/protocols/gmpv8/test_create_tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

from . import Gmpv8TestCase


class GmpCreateTagTestCase(Gmpv8TestCase):
def test_create_tag_missing_name(self):
with self.assertRaises(RequiredArgument):
Expand Down Expand Up @@ -112,7 +113,9 @@ def test_create_tag_missing_resource_type(self):

def test_create_tag_with_resource_filter(self):
self.gmp.create_tag(
name='foo', resource_filter='name=foo', resource_type=EntityType.TASK
name='foo',
resource_filter='name=foo',
resource_type=EntityType.TASK,
)

self.connection.send.has_been_called_with(
Expand Down Expand Up @@ -140,7 +143,9 @@ def test_create_tag_with_resource_ids(self):
)

self.gmp.create_tag(
name='foo', resource_ids=['foo', 'bar'], resource_type=EntityType.TASK
name='foo',
resource_ids=['foo', 'bar'],
resource_type=EntityType.TASK,
)

self.connection.send.has_been_called_with(
Expand Down Expand Up @@ -175,7 +180,10 @@ def test_create_tag_with_comment(self):

def test_create_tag_with_value(self):
self.gmp.create_tag(
name='foo', resource_ids=['foo'], resource_type=EntityType.TASK, value='bar'
name='foo',
resource_ids=['foo'],
resource_type=EntityType.TASK,
value='bar',
)

self.connection.send.has_been_called_with(
Expand All @@ -191,7 +199,10 @@ def test_create_tag_with_value(self):

def test_create_tag_with_active(self):
self.gmp.create_tag(
name='foo', resource_ids=['foo'], resource_type=EntityType.TASK, active=True
name='foo',
resource_ids=['foo'],
resource_type=EntityType.TASK,
active=True,
)

self.connection.send.has_been_called_with(
Expand All @@ -206,7 +217,10 @@ def test_create_tag_with_active(self):
)

self.gmp.create_tag(
name='foo', resource_ids=['foo'], resource_type=EntityType.TASK, active=False
name='foo',
resource_ids=['foo'],
resource_type=EntityType.TASK,
active=False,
)

self.connection.send.has_been_called_with(
Expand Down
4 changes: 3 additions & 1 deletion tests/protocols/gmpv8/test_modify_tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ def test_modify_tag_with_active(self):

def test_modify_tag_with_resource_filter_and_type(self):
self.gmp.modify_tag(
tag_id='t1', resource_filter='name=foo', resource_type=EntityType.TASK
tag_id='t1',
resource_filter='name=foo',
resource_type=EntityType.TASK,
)

self.connection.send.has_been_called_with(
Expand Down
26 changes: 26 additions & 0 deletions tests/protocols/gmpv9/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2019 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.protocols.gmpv9 import Gmp

from .. import GmpTestCase


class Gmpv9TestCase(GmpTestCase):

gmp_class = Gmp
Loading