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 636bb89
Show file tree
Hide file tree
Showing 11 changed files with 59 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.
3 changes: 3 additions & 0 deletions app/support/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
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.
39 changes: 39 additions & 0 deletions app/support/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()
Empty file.
3 changes: 3 additions & 0 deletions app/support/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.db import models

# Create your models here.
3 changes: 3 additions & 0 deletions app/support/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
3 changes: 3 additions & 0 deletions app/support/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.shortcuts import render

# Create your views here.

0 comments on commit 636bb89

Please sign in to comment.