From 3a83d89088bee0a8aa3d2675a09c89d5f3ea2e25 Mon Sep 17 00:00:00 2001 From: Daniele Maglie Date: Wed, 7 Feb 2024 14:54:52 +0100 Subject: [PATCH] Various fixes. --- .../forensic/archive/rule.py | 4 +- .../ticket/item/model.py | 2 +- src/piracyshield_data_model/ticket/model.py | 2 +- src/piracyshield_data_model/ticket/rule.py | 2 +- .../ticket/status/model.py | 4 + .../whitelist/genre/__init__.py | 1 + .../whitelist/genre/model.py | 17 ++++ .../whitelist/model.py | 93 +++++++++++++++++-- src/piracyshield_data_model/whitelist/rule.py | 2 +- 9 files changed, 114 insertions(+), 13 deletions(-) create mode 100644 src/piracyshield_data_model/whitelist/genre/__init__.py create mode 100644 src/piracyshield_data_model/whitelist/genre/model.py diff --git a/src/piracyshield_data_model/forensic/archive/rule.py b/src/piracyshield_data_model/forensic/archive/rule.py index 316873f..dbd5c9d 100644 --- a/src/piracyshield_data_model/forensic/archive/rule.py +++ b/src/piracyshield_data_model/forensic/archive/rule.py @@ -10,6 +10,6 @@ class ForensicArchiveRule: NAME = [ Required(), - String(allowed = '.-_'), - Length(minimum = 6, maximum = 128) + String(allowed = ' .-_'), + Length(minimum = 6, maximum = 320) ] diff --git a/src/piracyshield_data_model/ticket/item/model.py b/src/piracyshield_data_model/ticket/item/model.py index 8399325..6f160e1 100644 --- a/src/piracyshield_data_model/ticket/item/model.py +++ b/src/piracyshield_data_model/ticket/item/model.py @@ -205,7 +205,7 @@ class TicketItemModel(BaseModel): validator.validate() if not validator.is_valid(): - raise TicketItemModelProviderIdentifierException(validator.errors) + raise TicketItemModelProviderIdentifierNonValidException(validator.errors) return value diff --git a/src/piracyshield_data_model/ticket/model.py b/src/piracyshield_data_model/ticket/model.py index a0da702..12cc1ea 100644 --- a/src/piracyshield_data_model/ticket/model.py +++ b/src/piracyshield_data_model/ticket/model.py @@ -72,7 +72,7 @@ class TicketModel(BaseModel): self.assigned_to = self._validate_assigned_to(assigned_to) if assigned_to else None # ticket initial status - self.status = TicketStatusModel.CREATED.value + self.status = TicketStatusModel.CREATING.value # this is a blocking ticket self.genre = TicketGenreModel.BLOCKING.value diff --git a/src/piracyshield_data_model/ticket/rule.py b/src/piracyshield_data_model/ticket/rule.py index bb24a91..36e154a 100644 --- a/src/piracyshield_data_model/ticket/rule.py +++ b/src/piracyshield_data_model/ticket/rule.py @@ -19,7 +19,7 @@ class TicketRule: DESCRIPTION = [ Required(), - String(' .,-_@'), + String(' .,-_@/\'"'), Length(minimum = 3, maximum = 255) ] diff --git a/src/piracyshield_data_model/ticket/status/model.py b/src/piracyshield_data_model/ticket/status/model.py index 31c89a1..57b17ae 100644 --- a/src/piracyshield_data_model/ticket/status/model.py +++ b/src/piracyshield_data_model/ticket/status/model.py @@ -6,8 +6,12 @@ class TicketStatusModel(Enum): Ticket status types with relative identifier. """ + CREATING = 'creating' + CREATED = 'created' OPEN = 'open' CLOSED = 'closed' + + FAILED = 'failed' diff --git a/src/piracyshield_data_model/whitelist/genre/__init__.py b/src/piracyshield_data_model/whitelist/genre/__init__.py new file mode 100644 index 0000000..8d1c8b6 --- /dev/null +++ b/src/piracyshield_data_model/whitelist/genre/__init__.py @@ -0,0 +1 @@ + diff --git a/src/piracyshield_data_model/whitelist/genre/model.py b/src/piracyshield_data_model/whitelist/genre/model.py new file mode 100644 index 0000000..7e052d2 --- /dev/null +++ b/src/piracyshield_data_model/whitelist/genre/model.py @@ -0,0 +1,17 @@ +from enum import Enum + +class WhitelistGenreModel(Enum): + + """ + Whitelist item genres. + """ + + FQDN = 'fqdn' + + IPV4 = 'ipv4' + + IPV6 = 'ipv6' + + CIDR_IPV4 = 'cidr_ipv4' + + CIDR_IPV6 = 'cidr_ipv6' diff --git a/src/piracyshield_data_model/whitelist/model.py b/src/piracyshield_data_model/whitelist/model.py index 83fe6d2..f3d7c70 100644 --- a/src/piracyshield_data_model/whitelist/model.py +++ b/src/piracyshield_data_model/whitelist/model.py @@ -2,8 +2,7 @@ from piracyshield_data_model.base import BaseModel from piracyshield_component.validation.validator import Validator -from piracyshield_data_model.ticket.item.genre.model import TicketItemGenreModel - +from piracyshield_data_model.whitelist.genre.model import WhitelistGenreModel from piracyshield_data_model.whitelist.rule import WhitelistRule from piracyshield_data_model.ticket.rule import TicketRule @@ -27,26 +26,34 @@ class WhitelistModel(BaseModel): """ Validates the parameters. - :param genre: FQDN, IPv4 or IPv6 type. + :param genre: FQDN, IPv4, IPv6 or a CIDR IPv4/IPv6 class. :param value: value of the item. :param is_active: if the item is already active or not. :param registrar: registrar of the FQDN item. - :param as_code: AS code of the IPv4 or IPv6 item. + :param as_code: AS code of the IPv4, IPv6 or CIDR classes. """ match genre: - case TicketItemGenreModel.FQDN.value: + case WhitelistGenreModel.FQDN.value: self.value = self._validate_fqdn(value) self.registrar = self._validate_registrar(registrar) - case TicketItemGenreModel.IPV4.value: + case WhitelistGenreModel.IPV4.value: self.value = self._validate_ipv4(value) self.as_code = self._validate_as_code(as_code) - case TicketItemGenreModel.IPV6.value: + case WhitelistGenreModel.IPV6.value: self.value = self._validate_ipv6(value) self.as_code = self._validate_as_code(as_code) + case WhitelistGenreModel.CIDR_IPV4.value: + self.value = self._validate_cidr_ipv4(value) + self.as_code = self._validate_as_code(as_code) + + case WhitelistGenreModel.CIDR_IPV6.value: + self.value = self._validate_cidr_ipv6(value) + self.as_code = self._validate_as_code(as_code) + case _: raise WhitelistModelGenreException() @@ -114,6 +121,46 @@ class WhitelistModel(BaseModel): return value + def _validate_cidr_ipv4(self, value: str) -> str | Exception: + """ + Validates the CIDR IPv4 class. + + :param value: a valid CIDR IPv4 class. + :return: the same value. + """ + + if not value or not len(value): + raise WhitelistModelCIDRIPv4MissingException() + + validator = Validator(value, WhitelistRule.CIDR_IPV4) + + validator.validate() + + if not validator.is_valid(): + raise WhitelistModelCIDRIPv4NonValidException(validator.errors) + + return value + + def _validate_cidr_ipv6(self, value: str) -> str | Exception: + """ + Validates the CIDR IPv6 class. + + :param value: a valid CIDR IPv6 class. + :return: the same value. + """ + + if not value or not len(value): + raise WhitelistModelCIDRIPv6MissingException() + + validator = Validator(value, WhitelistRule.CIDR_IPV6) + + validator.validate() + + if not validator.is_valid(): + raise WhitelistModelCIDRIPv6NonValidException(validator.errors) + + return value + def _validate_registrar(self, value: str) -> str | Exception: """ Validates the registrar of a FQDN item. @@ -226,6 +273,38 @@ class WhitelistModelIPv6NonValidException(Exception): pass +class WhitelistModelCIDRIPv4MissingException(Exception): + + """ + No CIDR IPv4 class passed. + """ + + pass + +class WhitelistModelCIDRIPv4NonValidException(Exception): + + """ + Non valid CIDR IPv4 class. + """ + + pass + +class WhitelistModelCIDRIPv6MissingException(Exception): + + """ + No CIDR IPv6 class passed. + """ + + pass + +class WhitelistModelCIDRIPv6NonValidException(Exception): + + """ + Non valid CIDR IPv6 class. + """ + + pass + class WhitelistModelRegistrarNonValidException(Exception): """ diff --git a/src/piracyshield_data_model/whitelist/rule.py b/src/piracyshield_data_model/whitelist/rule.py index c827c9a..86afa50 100644 --- a/src/piracyshield_data_model/whitelist/rule.py +++ b/src/piracyshield_data_model/whitelist/rule.py @@ -13,7 +13,7 @@ class WhitelistRule: REGISTRAR = [ Required(), - String(allowed = ' -'), + String(allowed = ' .,-_'), Length(minimum = 3, maximum = 255) ]