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 10, 2024
1 parent a47d85f commit a13417c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
Empty file.
Empty file.
39 changes: 39 additions & 0 deletions app/access/management/commands/manage_superuser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from typing import Any
from utils.command import CustomBaseCommand, CommandHandler
from django.contrib.auth.models import User

import environ

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:
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()

0 comments on commit a13417c

Please sign in to comment.