Skip to content
This repository has been archived by the owner on Nov 2, 2024. It is now read-only.

Commit

Permalink
fix duplicated ingestor users (intelowlproject#2412)
Browse files Browse the repository at this point in the history
* fix

* removed if condition and added defaults

* removed .title()

* fixed test due to .title() change

* fixed test due to .title() change
  • Loading branch information
federicofantini authored and Michalsus committed Oct 11, 2024
1 parent 9cd0a3c commit 1247cd7
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from django.conf import settings
from django.db import migrations


def migrate(apps, schema_editor):
User = apps.get_model(*settings.AUTH_USER_MODEL.split("."))
UserProfile = apps.get_model("authentication", "UserProfile")
IngestorConfig = apps.get_model("ingestors_manager", "IngestorConfig")
PluginConfig = apps.get_model("api_app", "PluginConfig")

users = User.objects.filter(username__endswith="Ingestor")
for u in users:
username = u.username.removesuffix("Ingestor")
if username != username.title():
correct_user = User.objects.get_or_create(
username=f"{username.title()}Ingestor"
)[0]
correct_user.profile = UserProfile()
correct_user.profile.task_priority = 7
correct_user.profile.is_robot = True
correct_user.profile.save()

related_ingestor = IngestorConfig.objects.get(name__iexact=username)
related_ingestor.user = correct_user
related_ingestor.save()

for pc in PluginConfig.objects.filter(owner=u):
pc.owner = correct_user
pc.save()

u.delete()


def reverse_migrate(apps, schema_editor):
pass


class Migration(migrations.Migration):
atomic = False
dependencies = [
("api_app", "0062_alter_parameter_python_module"),
("ingestors_manager", "0021_ingestor_fix_malwarebazaar_threatfox"),
]

operations = [migrations.RunPython(migrate, reverse_migrate)]
16 changes: 14 additions & 2 deletions api_app/ingestors_manager/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from api_app.ingestors_manager.models import IngestorConfig
from api_app.signals import migrate_finished
from authentication.models import UserProfile
from certego_saas.apps.user.models import User
from intel_owl.celery import get_queue_name

Expand All @@ -19,16 +20,27 @@
def pre_save_ingestor_config(sender, instance: IngestorConfig, *args, **kwargs):
from intel_owl.tasks import execute_ingestor

user = User.objects.get_or_create(username=f"{instance.name.title()}Ingestor")[0]
user = User.objects.get_or_create(
username__iexact=f"{instance.name}Ingestor",
defaults={
"username": f"{instance.name}Ingestor",
},
)[0]

# in case the user has been created
if not hasattr(user, "profile"):
user.profile = UserProfile()

user.profile.task_priority = 7
user.profile.is_robot = True
user.profile.save()
instance.user = user

periodic_task = PeriodicTask.objects.update_or_create(
name=f"{instance.name.title()}Ingestor",
name__iexact=f"{instance.name}Ingestor",
task=f"{execute_ingestor.__module__}.{execute_ingestor.__name__}",
defaults={
"name": f"{instance.name}Ingestor",
"crontab": instance.schedule,
"queue": instance.queue,
"kwargs": json.dumps({"config_name": instance.name}),
Expand Down
3 changes: 2 additions & 1 deletion api_app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1260,9 +1260,10 @@ def generate_health_check_periodic_task(self):
and self.python_module.health_check_schedule
):
periodic_task = PeriodicTask.objects.update_or_create(
name=f"{self.name.title()}HealthCheck{self.__class__.__name__}",
name__iexact=f"{self.name}HealthCheck{self.__class__.__name__}",
task=f"{health_check.__module__}.{health_check.__name__}",
defaults={
"name": f"{self.name}HealthCheck{self.__class__.__name__}",
"crontab": self.python_module.health_check_schedule,
"queue": self.queue,
"enabled": not self.disabled,
Expand Down
4 changes: 2 additions & 2 deletions tests/api_app/ingestors_manager/test_signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ def test_pre_save_ingestor_config(self):
playbook_to_execute=PlaybookConfig.objects.first(),
)
self.assertIsNotNone(ic.periodic_task)
self.assertEqual(ic.periodic_task.name, "TestIngestor")
self.assertEqual(ic.periodic_task.name, "testIngestor")
self.assertEqual(ic.periodic_task.task, "intel_owl.tasks.execute_ingestor")
self.assertFalse(ic.periodic_task.enabled)
self.assertEqual(ic.periodic_task.crontab, crontab)
self.assertEqual(ic.periodic_task.queue, "default")
self.assertEqual(json.loads(ic.periodic_task.kwargs)["config_name"], ic.name)
self.assertIsNotNone(ic.user)
self.assertEqual(ic.user.username, "TestIngestor")
self.assertEqual(ic.user.username, "testIngestor")
ic.delete()
if created:
crontab.delete()
Expand Down

0 comments on commit 1247cd7

Please sign in to comment.