Skip to content

Commit

Permalink
added migrations to setup
Browse files Browse the repository at this point in the history
  • Loading branch information
drosetti committed Nov 22, 2024
1 parent 614213c commit e98f48b
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from django.db import migrations

from api_app.analyzers_manager.constants import ObservableTypes, TypeChoices
from api_app.choices import TLP


def migrate(apps, schema_editor):
PythonModule = apps.get_model("api_app", "PythonModule")
AnalyzerConfig = apps.get_model("analyzers_manager", "AnalyzerConfig")
AnalyzerConfig.objects.create(
name="VirusTotalv3SampleDownload",
description="Download sample from VT.",
type=TypeChoices.OBSERVABLE.value,
maximum_tlp=TLP.AMBER.value,
python_module=PythonModule.objects.get(
module="vt.vt3_sample_download.VirusTotalv3SampleDownload"
),
observable_supported=[ObservableTypes.HASH.value],
)


def reverse_migrate(apps, schema_editor):
AnalyzerConfig = apps.get_model("analyzers_manager", "AnalyzerConfig")
AnalyzerConfig.objects.get(name="VirusTotalv3SampleDownload").delete()


class Migration(migrations.Migration):

dependencies = [
("analyzers_manager", "0130_analyzer_config_nvd_cve"),
("api_app", "0064_vt_sample_download"),
]

# operations = [migrations.RunPython(migrate, reverse_migrate)]
60 changes: 60 additions & 0 deletions api_app/migrations/0064_vt_sample_download.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
from django.db import migrations

from api_app.choices import PythonModuleBasePaths


def migrate(apps, schema_editor):
PythonModule = apps.get_model("api_app", "PythonModule")
Parameter = apps.get_model("api_app", "Parameter")

# analyzer python module
vt_sample_analyzer_python_module, _ = PythonModule.objects.get_or_create(
module="vt.vt3_sample_download.VirusTotalv3SampleDownload",
base_path=PythonModuleBasePaths.ObservableAnalyzer.value,
)

# visualizer python module
vt_sample_visualizer_python_module, _ = PythonModule.objects.get_or_create(
module="sample_download.SampleDownload",
base_path=PythonModuleBasePaths.Visualizer.value,
)

# analyzer parameter
try:
Parameter.objects.get(
name="api_key_name", python_module=vt_sample_analyzer_python_module
)
except Parameter.DoesNotExist:
p = Parameter(
name="api_key_name",
type="str",
description="VT API key",
is_secret=True,
required=True,
python_module=vt_sample_analyzer_python_module,
)
p.full_clean()
p.save()


def reverse_migrate(apps, schema_editor):
PythonModule = apps.get_model("api_app", "PythonModule")

vt_sample_analyzer_python_module = PythonModule.objects.get(
module="vt.vt3_sample_download.VirusTotalv3SampleDownload"
)
print(f"{vt_sample_analyzer_python_module.connectorconfigs.all()=}")
vt_sample_analyzer_python_module.delete()
vt_sample_visualizer_python_module = PythonModule.objects.get(
module="sample_download.SampleDownload"
)
vt_sample_visualizer_python_module.delete()


class Migration(migrations.Migration):

dependencies = [
("api_app", "0063_singleton_and_elastic_report"),
]

operations = [migrations.RunPython(migrate, reverse_migrate)]
38 changes: 38 additions & 0 deletions api_app/playbooks_manager/migrations/0056_download_sample_vt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import datetime

from django.db import migrations

from api_app.analyzers_manager.constants import AllTypes
from api_app.choices import TLP


def migrate(apps, schema_editor):
AnalyzerConfig = apps.get_model("analyzers_manager", "AnalyzerConfig")
PlaybookConfig = apps.get_model("playbooks_manager", "PlaybookConfig")
playbook_download_sample_vt, _ = PlaybookConfig.objects.get_or_create(
name="Download_File_VT",
description="Download a sample from VT",
type=[AllTypes.HASH.value],
tlp=TLP.AMBER.value,
scan_check_time=datetime.timedelta(days=14),
)
vt_download_file_analyzer = AnalyzerConfig.objects.get(
name="VirusTotalv3SampleDownload"
)
playbook_download_sample_vt.analyzers.set([vt_download_file_analyzer])
playbook_download_sample_vt.save()


def reverse_migrate(apps, schema_editor):
PlaybookConfig = apps.get_model("playbooks_manager", "PlaybookConfig")
PlaybookConfig.objects.get(name="Download_File_VT").delete()


class Migration(migrations.Migration):

dependencies = [
("playbooks_manager", "0055_playbook_config_phishingextractor"),
("analyzers_manager", "0131_analyzer_config_vt_sample_download"),
]

# operations = [migrations.RunPython(migrate, reverse_migrate)]
38 changes: 38 additions & 0 deletions api_app/visualizers_manager/migrations/0039_sample_download.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from django.db import migrations


def migrate(apps, schema_editor):
PythonModule = apps.get_model("api_app", "PythonModule")
AnalyzerConfig = apps.get_model("analyzers_manager", "AnalyzerConfig")
PlaybookConfig = apps.get_model("playbooks_manager", "PlaybookConfig")
VisualizerConfig = apps.get_model("visualizers_manager", "VisualizerConfig")

visualizer_download_sample, _ = VisualizerConfig.objects.get_or_create(
name="Download_File",
description="Download a sample",
python_module=PythonModule.objects.get(module="sample_download.SampleDownload"),
)
visualizer_download_sample.playbooks.set(
PlaybookConfig.objects.filter(
analyzers=AnalyzerConfig.objects.get(name="DownloadFileFromUri")
)
+ PlaybookConfig.objects.filter(
analyzers=AnalyzerConfig.objects.get(name="VirusTotalv3SampleDownload")
)
)
visualizer_download_sample.save()


def reverse_migrate(apps, schema_editor):
VisualizerConfig = apps.get_model("visualizers_manager", "VisualizerConfig")
VisualizerConfig.objects.get(name="Download_File").delete()


class Migration(migrations.Migration):

dependencies = [
("visualizers_manager", "0038_visualizer_config_passive_dns"),
("playbooks_manager", "0056_download_sample_vt"),
]

# operations = [migrations.RunPython(migrate, reverse_migrate)]

0 comments on commit e98f48b

Please sign in to comment.