Skip to content

Commit

Permalink
Change: Use base enum class for most enums in python-gvm
Browse files Browse the repository at this point in the history
Some enums like ScannerType and AliveTest are special and don't need to
be derived from our base enum class. Using our base enum class for all
other enums allows to remove all from_string methods and therefore
avoids code duplication.
  • Loading branch information
bjoernricks committed Feb 27, 2024
1 parent 5e74178 commit 8236645
Show file tree
Hide file tree
Showing 24 changed files with 30 additions and 438 deletions.
52 changes: 1 addition & 51 deletions gvm/protocols/gmpv208/entities/alerts.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
# SPDX-License-Identifier: GPL-3.0-or-later
#

from enum import Enum
from typing import Any, Optional, Union

from gvm._enum import Enum
from gvm.errors import InvalidArgument, InvalidArgumentType, RequiredArgument

# if I use latest, I get circular import :/
Expand All @@ -24,23 +24,6 @@ class AlertEvent(Enum):
ASSIGNED_TICKET_CHANGED = "Assigned ticket changed"
OWNED_TICKET_CHANGED = "Owned ticket changed"

@classmethod
def from_string(
cls,
alert_event: Optional[str],
) -> Optional["AlertEvent"]:
"""Convert an alert event string into a AlertEvent instance"""
if not alert_event:
return None

try:
return cls[alert_event.replace(" ", "_").upper()]
except KeyError:
raise InvalidArgument(
argument="alert_event",
function=cls.from_string.__name__,
) from None


class AlertCondition(Enum):
"""Enum for alert condition types"""
Expand All @@ -52,22 +35,6 @@ class AlertCondition(Enum):
FILTER_COUNT_CHANGED = "Filter count changed"
FILTER_COUNT_AT_LEAST = "Filter count at least"

@classmethod
def from_string(
cls, alert_condition: Optional[str]
) -> Optional["AlertCondition"]:
"""Convert an alert condition string into a AlertCondition instance"""
if not alert_condition:
return None

try:
return cls[alert_condition.replace(" ", "_").upper()]
except KeyError:
raise InvalidArgument(
argument="alert_condition",
function=cls.from_string.__name__,
) from None


class AlertMethod(Enum):
"""Enum for alert method type"""
Expand All @@ -85,23 +52,6 @@ class AlertMethod(Enum):
TIPPINGPOINT_SMS = "TippingPoint SMS"
ALEMBA_VFIRE = "Alemba vFire"

@classmethod
def from_string(
cls,
alert_method: Optional[str],
) -> Optional["AlertMethod"]:
"""Convert an alert method string into a AlertCondition instance"""
if not alert_method:
return None

try:
return cls[alert_method.replace(" ", "_").upper()]
except KeyError:
raise InvalidArgument(
argument="alert_method",
function=cls.from_string.__name__,
) from None


def _check_event(
event: AlertEvent, condition: AlertCondition, method: AlertMethod
Expand Down
1 change: 0 additions & 1 deletion gvm/protocols/gmpv208/entities/audits.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
# SPDX-License-Identifier: GPL-3.0-or-later
#


from collections.abc import Mapping
from numbers import Integral
from typing import Any, List, Optional
Expand Down
75 changes: 2 additions & 73 deletions gvm/protocols/gmpv208/entities/credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
# SPDX-License-Identifier: GPL-3.0-or-later
#


from enum import Enum
from typing import Any, Optional

from gvm.errors import InvalidArgument, InvalidArgumentType, RequiredArgument
from gvm._enum import Enum
from gvm.errors import InvalidArgumentType, RequiredArgument
from gvm.utils import add_filter, to_bool
from gvm.xml import XmlCommand

Expand All @@ -21,22 +20,6 @@ class CredentialFormat(Enum):
EXE = "exe"
PEM = "pem"

@classmethod
def from_string(
cls,
credential_format: Optional[str],
) -> Optional["CredentialFormat"]:
if not credential_format:
return None

try:
return cls[credential_format.upper()]
except KeyError:
raise InvalidArgument(
argument="credential_format",
function=cls.from_string.__name__,
) from None


class CredentialType(Enum):
"""Enum for credential types"""
Expand All @@ -49,74 +32,20 @@ class CredentialType(Enum):
PGP_ENCRYPTION_KEY = "pgp"
PASSWORD_ONLY = "pw"

@classmethod
def from_string(
cls,
credential_type: Optional[str],
) -> Optional["CredentialType"]:
"""Convert a credential type string into a CredentialType instance"""
if not credential_type:
return None

try:
return cls[credential_type.upper()]
except KeyError:
raise InvalidArgument(
argument="credential_type",
function=cls.from_string.__name__,
) from None


class SnmpAuthAlgorithm(Enum):
"""Enum for SNMP auth algorithm"""

SHA1 = "sha1"
MD5 = "md5"

@classmethod
def from_string(
cls,
algorithm: Optional[str],
) -> Optional["SnmpAuthAlgorithm"]:
"""Convert a SNMP auth algorithm string into a
SnmpAuthAlgorithm instance"""
if not algorithm:
return None

try:
return cls[algorithm.upper()]
except KeyError:
raise InvalidArgument(
argument="algorithm",
function=cls.from_string.__name__,
) from None


class SnmpPrivacyAlgorithm(Enum):
"""Enum for SNMP privacy algorithm"""

AES = "aes"
DES = "des"

@classmethod
def from_string(
cls,
algorithm: Optional[str],
) -> Optional["SnmpPrivacyAlgorithm"]:
"""Convert a SNMP privacy algorithm string into a SnmpPrivacyAlgorithm
instance
"""
if not algorithm:
return None

try:
return cls[algorithm.upper()]
except KeyError:
raise InvalidArgument(
argument="algorithm",
function=cls.from_string.__name__,
) from None


class CredentialsMixin:
def clone_credential(self, credential_id: str) -> Any:
Expand Down
18 changes: 2 additions & 16 deletions gvm/protocols/gmpv208/entities/entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
# SPDX-License-Identifier: GPL-3.0-or-later
#


from enum import Enum
from typing import Optional

from gvm.errors import InvalidArgument
from gvm._enum import Enum


class EntityType(Enum):
Expand Down Expand Up @@ -58,9 +56,6 @@ def from_string(
Arguments:
entity_type: Entity type string to convert to a EntityType
"""
if not entity_type:
return None

if entity_type == "vuln":
return cls.VULNERABILITY

Expand All @@ -70,13 +65,4 @@ def from_string(
if entity_type == "config":
return cls.SCAN_CONFIG

if entity_type == "tls_certificate":
return cls.TLS_CERTIFICATE

try:
return cls[entity_type.upper()]
except KeyError:
raise InvalidArgument(
argument="entity_type",
function=cls.from_string.__name__,
) from None
return super().from_string(entity_type)
15 changes: 3 additions & 12 deletions gvm/protocols/gmpv208/entities/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
# SPDX-License-Identifier: GPL-3.0-or-later
#

from enum import Enum
from typing import Any, Optional

from gvm.errors import InvalidArgument, InvalidArgumentType, RequiredArgument
from gvm._enum import Enum
from gvm.errors import InvalidArgumentType, RequiredArgument
from gvm.utils import add_filter, to_bool
from gvm.xml import XmlCommand

Expand Down Expand Up @@ -50,9 +50,6 @@ def from_string(
Arguments:
filter_type (str): Filter type string to convert to a FilterType
"""
if not filter_type:
return None

if filter_type == "vuln":
return cls.VULNERABILITY

Expand All @@ -65,13 +62,7 @@ def from_string(
if filter_type == "secinfo":
return cls.ALL_SECINFO

try:
return cls[filter_type.upper()]
except KeyError:
raise InvalidArgument(
argument="filter_type",
function=cls.from_string.__name__,
) from None
return super().from_string(filter_type)


class FiltersMixin:
Expand Down
25 changes: 2 additions & 23 deletions gvm/protocols/gmpv208/entities/hosts.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
# SPDX-License-Identifier: GPL-3.0-or-later
#


from enum import Enum
from typing import Any, Optional

from gvm.errors import InvalidArgument, RequiredArgument
from gvm._enum import Enum
from gvm.errors import RequiredArgument
from gvm.utils import add_filter, to_bool
from gvm.xml import XmlCommand

Expand All @@ -19,26 +18,6 @@ class HostsOrdering(Enum):
RANDOM = "random"
REVERSE = "reverse"

@classmethod
def from_string(
cls,
hosts_ordering: Optional[str],
) -> Optional["HostsOrdering"]:
"""Convert a hosts ordering string to an actual HostsOrdering instance
Arguments:
hosts_ordering: Host ordering string to convert to a HostsOrdering
"""
if not hosts_ordering:
return None
try:
return cls[hosts_ordering.upper()]
except KeyError:
raise InvalidArgument(
argument="hosts_ordering",
function=cls.from_string.__name__,
) from None


class HostsMixin:
def create_host(self, name: str, *, comment: Optional[str] = None) -> Any:
Expand Down
1 change: 0 additions & 1 deletion gvm/protocols/gmpv208/entities/notes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
# SPDX-License-Identifier: GPL-3.0-or-later
#


from typing import Any, List, Optional

from gvm.errors import InvalidArgument, InvalidArgumentType, RequiredArgument
Expand Down
27 changes: 2 additions & 25 deletions gvm/protocols/gmpv208/entities/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
# SPDX-License-Identifier: GPL-3.0-or-later
#

from enum import Enum
from typing import Any, Optional

from gvm.errors import InvalidArgument, InvalidArgumentType, RequiredArgument
from gvm._enum import Enum
from gvm.errors import InvalidArgumentType, RequiredArgument
from gvm.protocols.gmpv208.entities.entities import EntityType
from gvm.utils import add_filter, to_bool
from gvm.xml import XmlCommand
Expand All @@ -19,29 +19,6 @@ class PermissionSubjectType(Enum):
GROUP = "group"
ROLE = "role"

@classmethod
def from_string(
cls,
subject_type: Optional[str],
) -> Optional["PermissionSubjectType"]:
"""Convert a permission subject type string to an actual
PermissionSubjectType instance
Arguments:
subject_type: Permission subject type string to convert to a
PermissionSubjectType
"""
if not subject_type:
return None

try:
return cls[subject_type.upper()]
except KeyError:
raise InvalidArgument(
argument="subject_type",
function=cls.from_string.__name__,
) from None


class PermissionsMixin:
def clone_permission(self, permission_id: str) -> Any:
Expand Down
Loading

0 comments on commit 8236645

Please sign in to comment.