Compare commits

..

5 commits

Author SHA1 Message Date
Tobias Genannt 2127d2156a Missing defaults 2026-01-08 10:45:24 +01:00
Tobias Genannt 8c5cf9e26b Formatting 2026-01-08 09:10:05 +01:00
Tobias Genannt dde39aec65 Formatting 2026-01-08 09:06:10 +01:00
Tobias Genannt 14f669fe0d Formatting 2026-01-08 09:03:46 +01:00
Tobias Genannt 0cd10dd3b5 Review comments addressed 2026-01-08 08:14:59 +01:00
3 changed files with 27 additions and 30 deletions

View file

@ -4,4 +4,4 @@ extend-ignore = E203, W503
per-file-ignores = per-file-ignores =
configuration/*:E131,E251,E266,E302,E305,E501,E722 configuration/*:E131,E251,E266,E302,E305,E501,E722
startup_scripts/startup_script_utils/__init__.py:F401 startup_scripts/startup_script_utils/__init__.py:F401
docker/*:E266,E722 docker/*:E266,E722,E501

View file

@ -54,25 +54,8 @@ fi
if [ "$SKIP_SUPERUSER" == "true" ]; then if [ "$SKIP_SUPERUSER" == "true" ]; then
echo "↩️ Skip creating the superuser" echo "↩️ Skip creating the superuser"
else else
if [ -z ${SUPERUSER_NAME+x} ]; then ./manage.py shell --no-startup --no-imports --interface python \
SUPERUSER_NAME='admin' </opt/netbox/super_user.py
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
fi fi
echo "✅ Initialisation is done." echo "✅ Initialisation is done."

View file

@ -1,12 +1,28 @@
from os import environ from os import environ
from users.models import Token, User
from users.choices import TokenVersionChoices
from django.conf import settings
su_name = environ.get("SUPERUSER_NAME") from django.conf import settings
su_email = environ.get("SUPERUSER_EMAIL") from users.choices import TokenVersionChoices
su_password = environ.get("SUPERUSER_PASSWORD") from users.models import Token, User
su_api_token = environ.get("SUPERUSER_API_TOKEN")
# 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", "admin")
su_email = environ.get("SUPERUSER_EMAIL", "admin@example.com")
su_password = _read_secret("superuser_password", environ.get("SUPERUSER_PASSWORD", "admin"))
su_api_token = _read_secret(
"superuser_api_token",
environ.get("SUPERUSER_API_TOKEN", "0123456789abcdef0123456789abcdef01234567"),
)
if not User.objects.filter(username=su_name): if not User.objects.filter(username=su_name):
u = User.objects.create_superuser(su_name, su_email, su_password) u = User.objects.create_superuser(su_name, su_email, su_password)
@ -15,8 +31,6 @@ if not User.objects.filter(username=su_name):
print("⚠️ No API token will be created as API_TOKEN_PEPPERS is not set") print("⚠️ No API token will be created as API_TOKEN_PEPPERS is not set")
msg = f"💡 Superuser Username: {su_name}, E-Mail: {su_email}" msg = f"💡 Superuser Username: {su_name}, E-Mail: {su_email}"
else: else:
t = Token.objects.create( t = Token.objects.create(user=u, token=su_api_token, version=TokenVersionChoices.V2)
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>')" msg = f"💡 Superuser Username: {su_name}, E-Mail: {su_email}, API Token: {t} (use with '{t.get_auth_header_prefix()}<Your token>')"
print(msg) print(msg)