Skip to content
Open
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
16 changes: 6 additions & 10 deletions api_app/analyzers_manager/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import requests
from django.conf import settings

from api_app.decorators import classproperty
from certego_saas.apps.user.models import User

from ..choices import Classification, PythonModuleBasePaths
Expand Down Expand Up @@ -67,8 +68,7 @@ def create_data_model(self):
return data_model
return None

@classmethod
@property
@classproperty
def config_exception(cls):
"""Returns the AnalyzerConfigurationException class."""
return AnalyzerConfigurationException
Expand All @@ -78,14 +78,12 @@ def analyzer_name(self) -> str:
"""Returns the name of the analyzer."""
return self._config.name

@classmethod
@property
@classproperty
def report_model(cls):
"""Returns the AnalyzerReport model."""
return AnalyzerReport

@classmethod
@property
@classproperty
def config_model(cls):
"""Returns the AnalyzerConfig model."""
return AnalyzerConfig
Expand Down Expand Up @@ -183,8 +181,7 @@ def config(self, runtime_configuration: Dict):
self.observable_name = self._job.analyzable.name
self.observable_classification = self._job.analyzable.classification

@classmethod
@property
@classproperty
def python_base_path(cls):
return PythonModuleBasePaths.ObservableAnalyzer.value

Expand Down Expand Up @@ -232,8 +229,7 @@ def config(self, runtime_configuration: Dict):
self.__filepath = None
self.file_mimetype = self._job.analyzable.mimetype

@classmethod
@property
@classproperty
def python_base_path(cls) -> PosixPath:
return PythonModuleBasePaths[FileAnalyzer.__name__].value

Expand Down
10 changes: 4 additions & 6 deletions api_app/analyzers_manager/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from api_app.choices import TLP, Classification, PythonModuleBasePaths
from api_app.data_model_manager.fields import SetField
from api_app.data_model_manager.models import BaseDataModel
from api_app.decorators import classproperty
from api_app.fields import ChoiceArrayField
from api_app.models import AbstractReport, PythonConfig, PythonModule

Expand Down Expand Up @@ -300,8 +301,7 @@ class AnalyzerConfig(PythonConfig):
blank=True,
)

@classmethod
@property
@classproperty
def serializer_class(cls):
from api_app.analyzers_manager.serializers import AnalyzerConfigSerializer

Expand Down Expand Up @@ -341,13 +341,11 @@ def clean(self):
self.clean_observable_supported()
self.clean_filetypes()

@classmethod
@property
@classproperty
def plugin_type(cls) -> str:
return "1"

@classmethod
@property
@classproperty
def config_exception(cls):
return AnalyzerConfigurationException

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

from api_app.analyzers_manager import classes
from api_app.analyzers_manager.exceptions import AnalyzerRunException
from api_app.decorators import classproperty
from api_app.mixins import AbuseCHMixin
from api_app.models import PluginConfig

Expand All @@ -27,15 +28,13 @@ class Feodo_Tracker(AbuseCHMixin, classes.ObservableAnalyzer):
use_recommended_url: bool
update_on_run: bool = True

@classmethod
@property
@classproperty
def recommend_locations(cls) -> Tuple[str, str]:
db_name = "feodotracker_abuse_ipblocklist.json"
url = "https://feodotracker.abuse.ch/downloads/ipblocklist_recommended.json"
return f"{settings.MEDIA_ROOT}/{db_name}", url

@classmethod
@property
@classproperty
def default_locations(cls) -> Tuple[str, str]:
db_name = "feodotracker_abuse_ipblocklist_recommended.json"
url = "https://feodotracker.abuse.ch/downloads/ipblocklist.json"
Expand Down
4 changes: 2 additions & 2 deletions api_app/analyzers_manager/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from rest_framework import mixins
from rest_framework.exceptions import NotFound

from api_app.decorators import classproperty
from api_app.models import PluginConfig

from ..permissions import isPluginActionsPermission
Expand Down Expand Up @@ -40,8 +41,7 @@ def get_permissions(self):


class AnalyzerActionViewSet(PythonReportActionViewSet):
@classmethod
@property
@classproperty
def report_model(cls):
return AnalyzerReport

Expand Down
16 changes: 5 additions & 11 deletions api_app/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from django.utils.functional import cached_property
from requests import HTTPError

from api_app.decorators import abstractclassproperty, classproperty
from api_app.models import AbstractReport, Job, PythonConfig, PythonModule
from certego_saas.apps.user.models import User

Expand Down Expand Up @@ -52,9 +53,7 @@ def name(self):
"""
return self._config.name

@classmethod
@property
@abstractmethod
@abstractclassproperty
def python_base_path(cls) -> PosixPath:
NotImplementedError()

Expand Down Expand Up @@ -243,18 +242,14 @@ def after_run_failed(self, e: Exception):
if settings.STAGE_CI:
raise e

@classmethod
@property
@abstractmethod
@abstractclassproperty
def report_model(cls) -> typing.Type[AbstractReport]:
"""
Returns Model to be used for *init_report_object*
"""
raise NotImplementedError()

@classmethod
@property
@abstractmethod
@abstractclassproperty
def config_model(cls) -> typing.Type[PythonConfig]:
"""
Returns Model to be used for *init_report_object*
Expand Down Expand Up @@ -321,8 +316,7 @@ def _monkeypatch(cls, patches: list = None) -> None:
for mock_fn in patches:
cls.start = mock_fn(cls.start)

@classmethod
@property
@classproperty
def python_module(cls) -> PythonModule:
"""
Get the Python module associated with the plugin.
Expand Down
11 changes: 5 additions & 6 deletions api_app/connectors_manager/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import logging
from typing import Type

from api_app.decorators import classproperty

from ..choices import PythonModuleBasePaths, ReportStatus
from ..classes import Plugin
from .exceptions import ConnectorConfigurationException, ConnectorRunException
Expand All @@ -20,18 +22,15 @@ class Connector(Plugin, metaclass=abc.ABCMeta):
and `run(self)` functions.
"""

@classmethod
@property
@classproperty
def python_base_path(cls):
return PythonModuleBasePaths.Connector.value

@classmethod
@property
@classproperty
def report_model(cls) -> Type[ConnectorReport]:
return ConnectorReport

@classmethod
@property
@classproperty
def config_model(cls) -> Type[ConnectorConfig]:
return ConnectorConfig

Expand Down
10 changes: 4 additions & 6 deletions api_app/connectors_manager/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from api_app.choices import TLP, PythonModuleBasePaths
from api_app.connectors_manager.exceptions import ConnectorConfigurationException
from api_app.connectors_manager.queryset import ConnectorReportQuerySet
from api_app.decorators import classproperty
from api_app.models import AbstractReport, PythonConfig, PythonModule


Expand Down Expand Up @@ -35,18 +36,15 @@ class ConnectorConfig(PythonConfig):
"api_app.OrganizationPluginConfiguration", related_name="%(class)s"
)

@classmethod
@property
@classproperty
def plugin_type(cls) -> str:
return "2"

@classmethod
@property
@classproperty
def config_exception(cls):
return ConnectorConfigurationException

@classmethod
@property
@classproperty
def serializer_class(cls):
from api_app.connectors_manager.serializers import ConnectorConfigSerializer

Expand Down
5 changes: 3 additions & 2 deletions api_app/connectors_manager/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

import logging

from api_app.decorators import classproperty

from ..views import PluginConfigViewSet, PythonConfigViewSet, PythonReportActionViewSet
from .models import ConnectorConfig, ConnectorReport
from .serializers import ConnectorConfigSerializer
Expand All @@ -21,8 +23,7 @@ class ConnectorConfigViewSet(PythonConfigViewSet):


class ConnectorActionViewSet(PythonReportActionViewSet):
@classmethod
@property
@classproperty
def report_model(cls):
return ConnectorReport

Expand Down
20 changes: 20 additions & 0 deletions api_app/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,26 @@
logger = logging.getLogger(__name__)


class classproperty(property):
"""Read-only class property descriptor.
Replacement for deprecated @classmethod + @property.
"""

def __get__(self, obj, owner=None):
return self.fget(owner if owner is not None else type(obj))


class abstractclassproperty(property):
"""Abstract read-only class property.
Enforced when the class uses ABCMeta metaclass.
"""

__isabstractmethod__ = True

def __get__(self, obj, owner=None):
return self.fget(owner if owner is not None else type(obj))


def deprecated_endpoint(deprecation_date=None, end_of_life_date=None):
"""
Returns a decorator which informs requester that
Expand Down
11 changes: 5 additions & 6 deletions api_app/ingestors_manager/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

from django.utils.functional import cached_property

from api_app.decorators import classproperty

from ..choices import TLP, PythonModuleBasePaths
from ..classes import Plugin
from .exceptions import IngestorConfigurationException, IngestorRunException
Expand All @@ -25,22 +27,19 @@ class Ingestor(Plugin, metaclass=abc.ABCMeta):
def __init__(self, config: IngestorConfig, **kwargs):
super().__init__(config, **kwargs)

@classmethod
@property
@classproperty
def python_base_path(cls):
return PythonModuleBasePaths.Ingestor.value

@abc.abstractmethod
def run(self) -> typing.Iterator[Any]:
raise NotImplementedError()

@classmethod
@property
@classproperty
def report_model(cls) -> Type[IngestorReport]:
return IngestorReport

@classmethod
@property
@classproperty
def config_model(cls) -> Type[IngestorConfig]:
return IngestorConfig

Expand Down
10 changes: 4 additions & 6 deletions api_app/ingestors_manager/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from django_celery_beat.models import CrontabSchedule, PeriodicTask

from api_app.choices import PythonModuleBasePaths
from api_app.decorators import classproperty
from api_app.ingestors_manager.exceptions import IngestorConfigurationException
from api_app.ingestors_manager.queryset import IngestorQuerySet, IngestorReportQuerySet
from api_app.interfaces import CreateJobsFromPlaybookInterface
Expand Down Expand Up @@ -123,18 +124,15 @@ class IngestorConfig(PythonConfig, CreateJobsFromPlaybookInterface):
def disabled_in_organizations(self) -> QuerySet:
return OrganizationPluginConfiguration.objects.none()

@classmethod
@property
@classproperty
def plugin_type(cls) -> str:
return "4"

@classmethod
@property
@classproperty
def config_exception(cls):
return IngestorConfigurationException

@classmethod
@property
@classproperty
def serializer_class(cls):
from api_app.ingestors_manager.serializers import IngestorConfigSerializer

Expand Down
Loading