Skip to content

Commit

Permalink
Add a little command to create or update our superuser
Browse files Browse the repository at this point in the history
The idea is to create a superuser via terraform and share it with SSM to
the cluster. That way we can make sure the user is always existing and
up-to-date with the parameter entry
  • Loading branch information
schtibe committed Dec 12, 2024
1 parent d31772a commit 73cc053
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 1 deletion.
3 changes: 2 additions & 1 deletion app/config/settings_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@
'distributions',
'access',
'cognito',
'bod'
'bod',
'support'
]

MIDDLEWARE = [
Expand Down
Empty file added app/support/__init__.py
Empty file.
6 changes: 6 additions & 0 deletions app/support/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class SupportConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'support'
Empty file.
Empty file.
42 changes: 42 additions & 0 deletions app/support/management/commands/manage_superuser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from typing import Any

import environ
from utils.command import CommandHandler
from utils.command import CustomBaseCommand

from django.contrib.auth import get_user_model

env = environ.Env()


class Handler(CommandHandler):
"""Create or update superuser from information from the environment
This command is used to make sure that the superuser is created and
configured. The data for it will be created centrally in terraform.
This will help with the password rotation.
"""

def run(self) -> None:
User = get_user_model() # pylint: disable=invalid-name
username = env.str('DJANGO_SUPERUSER_USERNAME')
email = env.str('DJANGO_SUPERUSER_EMAIL')
password = env.str('DJANGO_SUPERUSER_PASSWORD')

try:
admin = User.objects.get(username=username)
operation = 'Updated'
except User.DoesNotExist:
admin = User.objects.create(username=username, email=email)
operation = 'Created'

admin.set_password(password)
admin.save()
self.print_success('%s the superuser %s', operation, username)


class Command(CustomBaseCommand):
help = "Superuser management (creating or updating)"

def handle(self, *args: Any, **options: Any) -> None:
Handler(self, options).run()
Empty file.

0 comments on commit 73cc053

Please sign in to comment.