Allow admin API key to be configured via ADMIN_API_KEY env var

Agent-Logs-Url: https://github.com/christianlouis/dmarq/sessions/27db6d87-70db-4979-a23c-dd376f1c3b9a

Co-authored-by: christianlouis <361235+christianlouis@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot]
2026-03-29 23:10:36 +00:00
parent f2dd2333b6
commit ccb3f3fb7e
4 changed files with 79 additions and 15 deletions
+24 -15
View File
@@ -295,21 +295,30 @@ def create_app() -> FastAPI:
# Ensure all tables exist (no-op if already present)
Base.metadata.create_all(bind=engine)
# Generate and provide admin API key
api_key = generate_api_key()
add_api_key(api_key)
# Security: Log only last 8 characters for reference
logger.warning(
"%s\nIMPORTANT: Admin API Key Generated\n"
"API Key (last 8 chars): ...%s\n"
"Full key stored securely in memory.\n"
"For production, retrieve the key through secure configuration management.\n"
"Use this key in the X-API-Key header for admin endpoints.\n%s",
"=" * 80,
api_key[-8:],
"=" * 80,
)
# Load or generate the admin API key
if settings.ADMIN_API_KEY:
api_key = settings.ADMIN_API_KEY
add_api_key(api_key)
key_suffix = api_key[-8:] if len(api_key) >= 8 else api_key
logger.info(
"Admin API key loaded from ADMIN_API_KEY environment variable "
"(ends with: ...%s).",
key_suffix,
)
else:
api_key = generate_api_key()
add_api_key(api_key)
# Security: Log only last 8 characters for reference
logger.warning(
"%s\nIMPORTANT: Admin API Key Generated\n"
"API Key (last 8 chars): ...%s\n"
"Full key stored securely in memory.\n"
"Set ADMIN_API_KEY in your environment to use a fixed key across restarts.\n"
"Use this key in the X-API-Key header for admin endpoints.\n%s",
"=" * 80,
api_key[-8:],
"=" * 80,
)
# One-time migration: if IMAP_* env vars are set and no mail sources exist,
# create an initial MailSource from those settings so existing deployments