Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lifecycle: much improved debugging experience #12804

Merged
merged 7 commits into from
Feb 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"recommendations": [
"bashmish.es6-string-css",
"bpruitt-goddard.mermaid-markdown-syntax-highlighting",
"charliermarsh.ruff",
"dbaeumer.vscode-eslint",
"EditorConfig.EditorConfig",
"esbenp.prettier-vscode",
Expand All @@ -10,12 +11,12 @@
"Gruntfuggly.todo-tree",
"mechatroner.rainbow-csv",
"ms-python.black-formatter",
"charliermarsh.ruff",
"ms-python.black-formatter",
"ms-python.debugpy",
"ms-python.python",
"ms-python.vscode-pylance",
"ms-python.black-formatter",
"redhat.vscode-yaml",
"Tobermory.es6-string-html",
"unifiedjs.vscode-mdx"
"unifiedjs.vscode-mdx",
]
}
66 changes: 58 additions & 8 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,76 @@
"version": "0.2.0",
"configurations": [
{
"name": "Python: PDB attach Server",
"type": "python",
"name": "Debug: Attach Server Core",
"type": "debugpy",
"request": "attach",
"connect": {
"host": "localhost",
"port": 6800
"port": 9901
},
"justMyCode": true,
"pathMappings": [
{
"localRoot": "${workspaceFolder}",
"remoteRoot": "/"
}
],
"django": true
},
{
"name": "Python: PDB attach Worker",
"type": "python",
"name": "Debug: Attach Worker",
"type": "debugpy",
"request": "attach",
"connect": {
"host": "localhost",
"port": 6900
"port": 9901
},
"justMyCode": true,
"pathMappings": [
{
"localRoot": "${workspaceFolder}",
"remoteRoot": "/"
}
],
"django": true
},
{
"name": "Debug: Start Server Router",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/cmd/server",
"cwd": "${workspaceFolder}"
},
{
"name": "Debug: Start LDAP Outpost",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/cmd/ldap",
"cwd": "${workspaceFolder}"
},
{
"name": "Debug: Start Proxy Outpost",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/cmd/proxy",
"cwd": "${workspaceFolder}"
},
{
"name": "Debug: Start RAC Outpost",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/cmd/rac",
"cwd": "${workspaceFolder}"
},
{
"name": "Debug: Start Radius Outpost",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/cmd/radius",
"cwd": "${workspaceFolder}"
}
]
}
2 changes: 2 additions & 0 deletions authentik/core/management/commands/dev_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from daphne.management.commands.runserver import Command as RunServer
from daphne.server import Server

from authentik.lib.debug import start_debug_server
from authentik.root.signals import post_startup, pre_startup, startup


Expand All @@ -13,6 +14,7 @@ class SignalServer(Server):

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
start_debug_server()

def ready_callable():
pre_startup.send(sender=self)
Expand Down
6 changes: 2 additions & 4 deletions authentik/core/management/commands/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from structlog.stdlib import get_logger

from authentik.lib.config import CONFIG
from authentik.lib.debug import start_debug_server
from authentik.root.celery import CELERY_APP

LOGGER = get_logger()
Expand All @@ -28,10 +29,7 @@ def add_arguments(self, parser):
def handle(self, **options):
LOGGER.debug("Celery options", **options)
close_old_connections()
if CONFIG.get_bool("remote_debug"):
import debugpy

debugpy.listen(("0.0.0.0", 6900)) # nosec
start_debug_server()
worker: Worker = CELERY_APP.Worker(
no_color=False,
quiet=True,
Expand Down
2 changes: 1 addition & 1 deletion authentik/lib/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,4 +422,4 @@ def django_db_config(config: ConfigLoader | None = None) -> dict:
if len(argv) < 2: # noqa: PLR2004
print(dumps(CONFIG.raw, indent=4, cls=AttrEncoder))
else:
print(CONFIG.get(argv[1]))
print(CONFIG.get(argv[-1]))
26 changes: 26 additions & 0 deletions authentik/lib/debug.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from structlog.stdlib import get_logger

from authentik.lib.config import CONFIG

LOGGER = get_logger()


def start_debug_server(**kwargs) -> bool:
"""Attempt to start a debugpy server in the current process.
Returns true if the server was started successfully, otherwise false"""
if not CONFIG.get_bool("debug") and not CONFIG.get_bool("debugger"):
return
try:
import debugpy
except ImportError:
LOGGER.warning(

Check warning on line 16 in authentik/lib/debug.py

View check run for this annotation

Codecov / codecov/patch

authentik/lib/debug.py#L11-L16

Added lines #L11 - L16 were not covered by tests
"Failed to import debugpy. debugpy is not included "
"in the default release dependencies and must be installed manually"
)
return False

Check warning on line 20 in authentik/lib/debug.py

View check run for this annotation

Codecov / codecov/patch

authentik/lib/debug.py#L20

Added line #L20 was not covered by tests

listen: str = CONFIG.get("listen.listen_debug_py", "127.0.0.1:9901")
host, _, port = listen.rpartition(":")
debugpy.listen((host, int(port)), **kwargs) # nosec
LOGGER.debug("Starting debug server", host=host, port=port)
return True

Check warning on line 26 in authentik/lib/debug.py

View check run for this annotation

Codecov / codecov/patch

authentik/lib/debug.py#L22-L26

Added lines #L22 - L26 were not covered by tests
3 changes: 2 additions & 1 deletion authentik/lib/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ listen:
listen_radius: 0.0.0.0:1812
listen_metrics: 0.0.0.0:9300
listen_debug: 0.0.0.0:9900
listen_debug_py: 0.0.0.0:9901
trusted_proxy_cidrs:
- 127.0.0.0/8
- 10.0.0.0/8
Expand Down Expand Up @@ -57,7 +58,7 @@ cache:
# transport_options: ""

debug: false
remote_debug: false
debugger: false

log_level: info

Expand Down
1 change: 0 additions & 1 deletion internal/debug/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
func EnableDebugServer() {
l := log.WithField("logger", "authentik.go_debugger")
if !config.Get().Debug {
l.Info("not enabling debug server, set `AUTHENTIK_DEBUG` to `true` to enable it.")
return
}
h := mux.NewRouter()
Expand Down
8 changes: 6 additions & 2 deletions lifecycle/ak
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ function cleanup {
}

function prepare_debug {
# Only attempt to install debug dependencies if we're running in a container
if [ ! -d /ak-root ]; then
return
fi
export DEBIAN_FRONTEND=noninteractive
apt-get update
apt-get install -y --no-install-recommends krb5-kdc krb5-user krb5-admin-server libkrb5-dev gcc
Expand All @@ -63,7 +67,7 @@ function prepare_debug {
chown authentik:authentik /unittest.xml
}

if [[ "${AUTHENTIK_REMOTE_DEBUG}" == "true" ]]; then
if [[ "$(python -m authentik.lib.config debugger 2> /dev/null)" == "True" ]]; then
prepare_debug
fi

Expand Down Expand Up @@ -92,7 +96,7 @@ elif [[ "$1" == "test-all" ]]; then
elif [[ "$1" == "healthcheck" ]]; then
run_authentik healthcheck $(cat $MODE_FILE)
elif [[ "$1" == "dump_config" ]]; then
exec python -m authentik.lib.config
exec python -m authentik.lib.config $@
elif [[ "$1" == "debug" ]]; then
exec sleep infinity
else
Expand Down
7 changes: 2 additions & 5 deletions lifecycle/gunicorn.conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

from authentik import get_full_version
from authentik.lib.config import CONFIG
from authentik.lib.debug import start_debug_server
from authentik.lib.logging import get_logger_config
from authentik.lib.utils.http import get_http_session
from authentik.lib.utils.reflection import get_env
Expand Down Expand Up @@ -146,9 +147,5 @@ def post_worker_init(worker: DjangoUvicornWorker):
except Exception: # nosec
pass

if CONFIG.get_bool("remote_debug"):
import debugpy

debugpy.listen(("0.0.0.0", 6800)) # nosec

start_debug_server()
run_migrations()
Loading