Review comments addressed

This commit is contained in:
Tobias Genannt 2026-01-08 08:14:59 +01:00
parent c3dcc6c59f
commit 0cd10dd3b5
2 changed files with 17 additions and 24 deletions

View file

@ -54,25 +54,8 @@ fi
if [ "$SKIP_SUPERUSER" == "true" ]; then
echo "↩️ Skip creating the superuser"
else
if [ -z ${SUPERUSER_NAME+x} ]; then
SUPERUSER_NAME='admin'
fi
if [ -z ${SUPERUSER_EMAIL+x} ]; then
SUPERUSER_EMAIL='admin@example.com'
fi
if [ -f "/run/secrets/superuser_password" ]; then
SUPERUSER_PASSWORD="$(</run/secrets/superuser_password)"
elif [ -z ${SUPERUSER_PASSWORD+x} ]; then
SUPERUSER_PASSWORD='admin'
fi
if [ -f "/run/secrets/superuser_api_token" ]; then
SUPERUSER_API_TOKEN="$(</run/secrets/superuser_api_token)"
elif [ -z ${SUPERUSER_API_TOKEN+x} ]; then
SUPERUSER_API_TOKEN='0123456789abcdef0123456789abcdef01234567'
fi
./manage.py shell --interface python < /opt/netbox/super_user.py
./manage.py shell --no-startup --no-imports --interface python \
< /opt/netbox/super_user.py
fi
echo "✅ Initialisation is done."

View file

@ -3,10 +3,22 @@ from users.models import Token, User
from users.choices import TokenVersionChoices
from django.conf import settings
# Read secret from file
def _read_secret(secret_name: str, default: str | None = None) -> str | None:
try:
f = open("/run/secrets/" + secret_name, "r", encoding="utf-8")
except EnvironmentError:
return default
else:
with f:
return f.readline().strip()
su_name = environ.get("SUPERUSER_NAME")
su_email = environ.get("SUPERUSER_EMAIL")
su_password = environ.get("SUPERUSER_PASSWORD")
su_api_token = environ.get("SUPERUSER_API_TOKEN")
su_password = _read_secret("superuser_password", environ.get("SUPERUSER_PASSWORD"))
su_api_token = _read_secret("superuser_api_token", environ.get("SUPERUSER_API_TOKEN"))
if not User.objects.filter(username=su_name):
u = User.objects.create_superuser(su_name, su_email, su_password)
@ -15,8 +27,6 @@ if not User.objects.filter(username=su_name):
print("⚠️ No API token will be created as API_TOKEN_PEPPERS is not set")
msg = f"💡 Superuser Username: {su_name}, E-Mail: {su_email}"
else:
t = Token.objects.create(
user=u, token=su_api_token, version=TokenVersionChoices.V2
)
t = Token.objects.create(user=u, token=su_api_token, version=TokenVersionChoices.V2)
msg = f"💡 Superuser Username: {su_name}, E-Mail: {su_email}, API Token: {t} (use with '{t.get_auth_header_prefix()}<Your token>')"
print(msg)