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¶
-
Command-line arguments (highest priority)
-
Environment variables
-
Default values (lowest priority)
- Username:
admin - Password:
admin - Email:
admin@localhost.local
Quick Start Guide¶
For Development¶
-
Start containers (uses defaults):
-
Access admin panel:
- URL: http://localhost:8000/admin/
- Username:
admin -
Password:
admin -
View saved credentials:
For Production¶
-
Create
.envfile: -
Set strong credentials in
.env: -
Start containers:
-
Access admin panel:
- URL: http://your-domain.com/admin/
-
Use credentials from step 2
-
Delete credentials file (optional, recommended):
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:
- Multiple Admin Users - Support creating multiple admins from config
- Password Validation - Enforce password complexity requirements
- Credential Rotation - Automatic password rotation on schedule
- Audit Logging - Log all admin credential changes
- Email Notifications - Alert when admin credentials are created/changed
- MFA Integration - Automatically enable 2FA for admin users
- Credential Encryption - Encrypt credentials file at rest
Related Files¶
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:
- Review Configuration Guide
- Check Troubleshooting section
- Review container logs:
docker-compose logs - 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