-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a little command to create or update our superuser
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
Showing
7 changed files
with
50 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,7 +47,8 @@ | |
'distributions', | ||
'access', | ||
'cognito', | ||
'bod' | ||
'bod', | ||
'support' | ||
] | ||
|
||
MIDDLEWARE = [ | ||
|
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.