Skip to content

Admin Credentials Auto-Setup - Implementation Summary

Overview

This document summarizes the automatic Django admin credential creation system implemented for Barodybroject.

Date Implemented: December 20, 2025 Version: 1.0.0

What Was Implemented

1. Management Command: ensure_admin

Location: src/parodynews/management/commands/ensure_admin.py

Features: - Automatically creates or updates Django superuser - Reads credentials from environment variables with fallback defaults - Saves credentials to file for reference - Displays warnings when using default credentials - Sets restrictive file permissions (600) on credentials file

Usage:

# Use environment variables or defaults
python manage.py ensure_admin

# Override with command-line arguments
python manage.py ensure_admin --username admin --password secret123

# Skip saving credentials to file
python manage.py ensure_admin --no-save-credentials

2. Environment Configuration

File: .env.example

New Variables: - DJANGO_SUPERUSER_USERNAME (default: admin) - DJANGO_SUPERUSER_EMAIL (default: admin@localhost.local) - DJANGO_SUPERUSER_PASSWORD (default: admin) - ADMIN_CREDENTIALS_FILE (default: setup_data/admin_credentials.txt)

3. Docker Integration

Development Environment

File: .devcontainer/docker-compose_dev.yml

Changes: - Added credential environment variables to python service - Added python manage.py ensure_admin to startup command - Runs after migrations, before starting dev server

Production Environment

File: docker-compose.yml

Changes: - Added credential environment variables to web-prod service - Created docker-entrypoint.sh script for production startup - Updated Dockerfile to use entrypoint script

File: src/docker-entrypoint.sh

Features: - Waits for database to be ready - Runs migrations - Creates/updates admin user automatically - Collects static files - Starts Gunicorn server

File: src/Dockerfile

Changes: - Copies entrypoint script to container - Sets executable permissions - Configures as container entrypoint

4. Security

File: .gitignore

Added: - setup_data/admin_credentials.txt - **/admin_credentials.txt

Ensures credentials files are never committed to version control.

5. Documentation

Updated Files: - README.md - Added "Docker Admin Credentials" section - docs/configuration/admin-credentials.md - Comprehensive configuration guide - docs/ADMIN_CREDENTIALS_SETUP.md - This implementation summary

How It Works

Startup Flow

┌─────────────────────────────────────────────────────────────┐
│ 1. Docker Container Starts                                  │
└─────────────────────────────────────────────────────────────┘
                          ↓
┌─────────────────────────────────────────────────────────────┐
│ 2. Environment Variables Loaded                             │
│    - From .env file (if exists)                            │
│    - From docker-compose environment section                │
│    - Defaults used if not set                              │
└─────────────────────────────────────────────────────────────┘
                          ↓
┌─────────────────────────────────────────────────────────────┐
│ 3. Database Migrations Run                                  │
│    python manage.py migrate                                 │
└─────────────────────────────────────────────────────────────┘
                          ↓
┌─────────────────────────────────────────────────────────────┐
│ 4. ensure_admin Command Runs                                │
│    python manage.py ensure_admin                            │
│                                                             │
│    - Reads DJANGO_SUPERUSER_* env vars                     │
│    - Creates user if doesn't exist                         │
│    - Updates user if exists (password, permissions)        │
│    - Saves credentials to file                             │
└─────────────────────────────────────────────────────────────┘
                          ↓
┌─────────────────────────────────────────────────────────────┐
│ 5. Application Starts                                       │
│    - Dev: Django dev server                                │
│    - Prod: Gunicorn                                        │
└─────────────────────────────────────────────────────────────┘
                          ↓
┌─────────────────────────────────────────────────────────────┐
│ 6. Admin Access Available                                   │
│    http://localhost:8000/admin/                            │
│    Username: admin (or custom)                             │
│    Password: admin (or custom)                             │
└─────────────────────────────────────────────────────────────┘

Credential Resolution Order

  1. Command-line arguments (highest priority)

    python manage.py ensure_admin --username myuser --password mypass
    

  2. Environment variables

    export DJANGO_SUPERUSER_USERNAME=myuser
    export DJANGO_SUPERUSER_PASSWORD=mypass
    

  3. Default values (lowest priority)

  4. Username: admin
  5. Password: admin
  6. Email: admin@localhost.local

Quick Start Guide

For Development

  1. Start containers (uses defaults):

    docker-compose -f .devcontainer/docker-compose_dev.yml up -d
    

  2. Access admin panel:

  3. URL: http://localhost:8000/admin/
  4. Username: admin
  5. Password: admin

  6. View saved credentials:

    cat setup_data/admin_credentials.txt
    

For Production

  1. Create .env file:

    cp .env.example .env
    

  2. Set strong credentials in .env:

    DJANGO_SUPERUSER_USERNAME=prodadmin
    DJANGO_SUPERUSER_EMAIL=admin@example.com
    DJANGO_SUPERUSER_PASSWORD=YourStrongPassword123!
    

  3. Start containers:

    docker-compose up -d
    

  4. Access admin panel:

  5. URL: http://your-domain.com/admin/
  6. Use credentials from step 2

  7. Delete credentials file (optional, recommended):

    docker-compose exec web-prod rm /app/setup_data/admin_credentials.txt
    

Testing the Implementation

Test 1: Default Credentials

# Start fresh
docker-compose -f .devcontainer/docker-compose_dev.yml down -v
docker-compose -f .devcontainer/docker-compose_dev.yml up -d

# Check logs
docker-compose -f .devcontainer/docker-compose_dev.yml logs python | grep ensure_admin

# Expected output:
# ✅ Created admin superuser: admin
# ⚠️  WARNING: Using default credentials

# View credentials file
cat setup_data/admin_credentials.txt

# Test login
open http://localhost:8000/admin/
# Username: admin
# Password: admin

Test 2: Custom Credentials via .env

# Create .env
cat > .env << EOF
DJANGO_SUPERUSER_USERNAME=testadmin
DJANGO_SUPERUSER_EMAIL=test@example.com
DJANGO_SUPERUSER_PASSWORD=TestPass123!
EOF

# Start containers
docker-compose -f .devcontainer/docker-compose_dev.yml down -v
docker-compose -f .devcontainer/docker-compose_dev.yml up -d

# Check logs
docker-compose -f .devcontainer/docker-compose_dev.yml logs python | grep ensure_admin

# Expected output:
# ✅ Created admin superuser: testadmin

# Test login
open http://localhost:8000/admin/
# Username: testadmin
# Password: TestPass123!

Test 3: Manual Command Execution

# Start containers
docker-compose -f .devcontainer/docker-compose_dev.yml up -d

# Run command manually
docker-compose -f .devcontainer/docker-compose_dev.yml exec python \
  python manage.py ensure_admin \
    --username manualadmin \
    --email manual@example.com \
    --password ManualPass123!

# Expected output:
# ✅ Updated admin superuser: manualadmin

Test 4: Production Entrypoint

# Build production image
docker-compose build web-prod

# Start production
docker-compose up -d

# Check logs
docker-compose logs web-prod | grep -A 10 "Ensuring admin user"

# Expected output:
# 👤 Ensuring admin user exists...
# ✅ Created admin superuser: admin

Benefits

For Developers

✅ Zero Configuration - Works out of the box with sensible defaults ✅ No Manual Steps - Admin user created automatically on first run ✅ Credentials Saved - Easy to reference in setup_data/admin_credentials.txt ✅ Consistent - Same credentials across container restarts ✅ Flexible - Easy to customize via environment variables

For DevOps/Production

✅ Secrets Management - Integrates with environment variables ✅ CI/CD Friendly - Works with GitHub Secrets, Azure Key Vault ✅ Automated Deployments - No manual intervention required ✅ Security Warnings - Alerts when using default credentials ✅ Idempotent - Safe to run multiple times

For Security

✅ Gitignored - Credentials file never committed ✅ File Permissions - Restrictive permissions (600) on Unix ✅ Environment-Based - Production uses secrets management ✅ Visible Warnings - Clear alerts about default credentials ✅ Documented - Security best practices included

Migration from Old System

Before (Manual Process)

# Start containers
docker-compose up -d

# Manually create admin
docker-compose exec web-prod python manage.py createsuperuser
# Enter username: admin
# Enter email: admin@example.com
# Enter password: ********
# Confirm password: ********

# Remember credentials (write them down somewhere)

After (Automated Process)

# Set credentials in .env (one time)
echo "DJANGO_SUPERUSER_PASSWORD=MySecurePass123!" >> .env

# Start containers (admin created automatically)
docker-compose up -d

# Credentials saved automatically to setup_data/admin_credentials.txt
cat setup_data/admin_credentials.txt

Troubleshooting

Issue: Admin user not created

Solution:

# Check logs
docker-compose logs web-prod | grep ensure_admin

# Manually run command
docker-compose exec web-prod python manage.py ensure_admin

Issue: Can't find credentials file

Solution:

# Check if file exists
docker-compose exec web-prod ls -la /app/setup_data/

# Recreate by running command
docker-compose exec web-prod python manage.py ensure_admin

Issue: Password not working

Solution:

# Check environment variable
docker-compose exec web-prod env | grep DJANGO_SUPERUSER_PASSWORD

# Reset password
docker-compose exec web-prod python manage.py changepassword admin

Future Enhancements

Potential improvements for future versions:

  1. Multiple Admin Users - Support creating multiple admins from config
  2. Password Validation - Enforce password complexity requirements
  3. Credential Rotation - Automatic password rotation on schedule
  4. Audit Logging - Log all admin credential changes
  5. Email Notifications - Alert when admin credentials are created/changed
  6. MFA Integration - Automatically enable 2FA for admin users
  7. Credential Encryption - Encrypt credentials file at rest
barodybroject/
├── .env.example                                    # Environment template
├── .gitignore                                      # Ignores credentials file
├── docker-compose.yml                              # Production config
├── .devcontainer/
│   └── docker-compose_dev.yml                      # Development config
├── src/
│   ├── Dockerfile                                  # Production image
│   ├── docker-entrypoint.sh                        # Production startup script
│   └── parodynews/
│       └── management/
│           └── commands/
│               └── ensure_admin.py                 # Main command
├── setup_data/
│   └── admin_credentials.txt                       # Auto-generated (gitignored)
└── docs/
    ├── configuration/
    │   └── admin-credentials.md                    # Configuration guide
    └── ADMIN_CREDENTIALS_SETUP.md                  # This file

Support

For questions or issues:

  1. Review Configuration Guide
  2. Check Troubleshooting section
  3. Review container logs: docker-compose logs
  4. Open GitHub issue with details

Changelog

Version 1.0.0 (2025-12-20)

Added: - ensure_admin management command - Automatic credential creation in Docker - Environment variable support - Credentials file generation - Comprehensive documentation - Security warnings and best practices

Changed: - Updated docker-compose files with credential env vars - Updated Dockerfile with entrypoint script - Updated README with credential section

Security: - Added credentials file to .gitignore - Set restrictive file permissions (600) - Added warnings for default credentials