Files
pgadmin4/pkg/docker/gunicorn_config.py
T
Jeremy Schneider 0c6740f146 feat: include authenticated user identity in HTTP access log (#9991)
Set an X-Remote-User response header containing the authenticated username
on every request when the LOG_AUTHENTICATED_USER config option is enabled
(disabled by default). This allows the HTTP access log to be configured to
include user identity via standard log format directives
(%({x-remote-user}o)s in gunicorn, %{X-Remote-User}o in Apache) without
requiring any changes to pgAdmin's session or authentication behaviour. The
default gunicorn access log format is updated to surface the header.

The username is sanitised to a header-safe value: it is transliterated to
Latin-1 (HTTP headers are Latin-1 only) and any non-printable characters,
including CR/LF, are stripped, so unusual usernames cannot cause a 500 on
every response.
2026-06-19 09:26:32 +01:00

54 lines
1.7 KiB
Python

import gunicorn
# Can be resolved because of how Dockerfile organizes the code during build
from config import JSON_LOGGER, CONSOLE_LOG_LEVEL, CONSOLE_LOG_FORMAT_JSON
gunicorn.SERVER_SOFTWARE = "Python"
# Include the authenticated user identity in the access log.
# %({x-remote-user}o)s reads the X-Remote-User response header set by pgAdmin
# for authenticated requests; unauthenticated requests log '-'.
access_log_format = (
'%(h)s %(l)s %({x-remote-user}o)s %(t)s "%(r)s" %(s)s %(b)s '
'"%(f)s" "%(a)s"'
)
if JSON_LOGGER:
logconfig_dict = {
"version": 1,
"disable_existing_loggers": False,
"root": {"level": CONSOLE_LOG_LEVEL, "handlers": []},
"loggers": {
"gunicorn.error": {
"level": CONSOLE_LOG_LEVEL,
"handlers": ["error_console"],
"propagate": True,
"qualname": "gunicorn.error",
},
"gunicorn.access": {
"level": CONSOLE_LOG_LEVEL,
"handlers": ["console"],
"propagate": True,
"qualname": "gunicorn.access",
},
},
"handlers": {
"console": {
"class": "logging.StreamHandler",
"formatter": "json",
"stream": "ext://sys.stdout",
},
"error_console": {
"class": "logging.StreamHandler",
"formatter": "json",
"stream": "ext://sys.stderr",
},
},
"formatters": {
"json": {
"class": "jsonformatter.JsonFormatter",
"format": CONSOLE_LOG_FORMAT_JSON,
},
},
}