Support new API token format

This commit is contained in:
Tobias Genannt 2026-01-07 19:40:41 +01:00
parent c0ead010ec
commit c3dcc6c59f
3 changed files with 24 additions and 17 deletions

View file

@ -76,6 +76,7 @@ COPY docker/configuration.docker.py /opt/netbox/netbox/netbox/configuration.py
COPY docker/ldap_config.docker.py /opt/netbox/netbox/netbox/ldap_config.py COPY docker/ldap_config.docker.py /opt/netbox/netbox/netbox/ldap_config.py
COPY docker/docker-entrypoint.sh /opt/netbox/docker-entrypoint.sh COPY docker/docker-entrypoint.sh /opt/netbox/docker-entrypoint.sh
COPY docker/launch-netbox.sh /opt/netbox/launch-netbox.sh COPY docker/launch-netbox.sh /opt/netbox/launch-netbox.sh
COPY docker/super_user.py /opt/netbox/super_user.py
COPY configuration/ /etc/netbox/config/ COPY configuration/ /etc/netbox/config/
COPY docker/granian.py /opt/netbox/netbox/netbox/granian.py COPY docker/granian.py /opt/netbox/netbox/netbox/granian.py
COPY VERSION /opt/netbox/VERSION COPY VERSION /opt/netbox/VERSION

View file

@ -71,26 +71,10 @@ else
SUPERUSER_API_TOKEN='0123456789abcdef0123456789abcdef01234567' SUPERUSER_API_TOKEN='0123456789abcdef0123456789abcdef01234567'
fi fi
./manage.py shell --interface python <<END ./manage.py shell --interface python < /opt/netbox/super_user.py
from users.models import Token, User
if not User.objects.filter(username='${SUPERUSER_NAME}'):
u = User.objects.create_superuser('${SUPERUSER_NAME}', '${SUPERUSER_EMAIL}', '${SUPERUSER_PASSWORD}')
Token.objects.create(user=u, key='${SUPERUSER_API_TOKEN}')
END
echo "💡 Superuser Username: ${SUPERUSER_NAME}, E-Mail: ${SUPERUSER_EMAIL}"
fi fi
./manage.py shell --interface python <<END
from users.models import Token
try:
old_default_token = Token.objects.get(key="0123456789abcdef0123456789abcdef01234567")
if old_default_token:
print("⚠️ Warning: You have the old default admin API token in your database. This token is widely known; please remove it. Log in as your superuser and check API Tokens in your user menu.")
except Token.DoesNotExist:
pass
END
echo "✅ Initialisation is done." echo "✅ Initialisation is done."
# Launch whatever is passed by docker # Launch whatever is passed by docker

22
docker/super_user.py Normal file
View file

@ -0,0 +1,22 @@
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")
su_email = environ.get("SUPERUSER_EMAIL")
su_password = environ.get("SUPERUSER_PASSWORD")
su_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)
msg = ""
if not settings.API_TOKEN_PEPPERS:
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
)
msg = f"💡 Superuser Username: {su_name}, E-Mail: {su_email}, API Token: {t} (use with '{t.get_auth_header_prefix()}<Your token>')"
print(msg)