-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into web/cleanup/better-context-definitions
* main: (30 commits) web: maintenance: split tsconfig into “base” and “build” variants. (#9036) web: consistent style declarations internally (#9077) providers/oauth2: fix interactive device flow (#9076) website/docs: fix transports example (#9074) events: fix log_capture (#9075) web: bump the sentry group in /web with 2 updates (#9065) core: bump goauthentik.io/api/v3 from 3.2024022.6 to 3.2024022.7 (#9064) web: bump @codemirror/lang-python from 6.1.4 to 6.1.5 in /web (#9068) web: bump the eslint group in /web with 1 update (#9066) web: bump glob from 10.3.10 to 10.3.12 in /web (#9069) web: bump the rollup group in /web with 3 updates (#9067) web: bump the eslint group in /tests/wdio with 1 update (#9071) core: bump webauthn from 2.0.0 to 2.1.0 (#9070) core: bump sentry-sdk from 1.43.0 to 1.44.0 (#9073) core: bump requests-mock from 1.12.0 to 1.12.1 (#9072) web: bump API Client version (#9061) events: rework log messages returned from API and their rendering (#8770) website/docs: update airgapped config (#9049) website: bump @types/react from 18.2.72 to 18.2.73 in /website (#9052) web: bump the rollup group in /web with 3 updates (#9053) ...
- Loading branch information
Showing
53 changed files
with
755 additions
and
449 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
from collections.abc import Generator | ||
from contextlib import contextmanager | ||
from dataclasses import dataclass, field | ||
from datetime import datetime | ||
from typing import Any | ||
|
||
from django.utils.timezone import now | ||
from rest_framework.fields import CharField, ChoiceField, DateTimeField, DictField | ||
from structlog import configure, get_config | ||
from structlog.stdlib import NAME_TO_LEVEL, ProcessorFormatter | ||
from structlog.testing import LogCapture | ||
from structlog.types import EventDict | ||
|
||
from authentik.core.api.utils import PassiveSerializer | ||
from authentik.events.utils import sanitize_dict | ||
|
||
|
||
@dataclass() | ||
class LogEvent: | ||
|
||
event: str | ||
log_level: str | ||
logger: str | ||
timestamp: datetime = field(default_factory=now) | ||
attributes: dict[str, Any] = field(default_factory=dict) | ||
|
||
@staticmethod | ||
def from_event_dict(item: EventDict) -> "LogEvent": | ||
event = item.pop("event") | ||
log_level = item.pop("level").lower() | ||
timestamp = datetime.fromisoformat(item.pop("timestamp")) | ||
item.pop("pid", None) | ||
# Sometimes log entries have both `level` and `log_level` set, but `level` is always set | ||
item.pop("log_level", None) | ||
return LogEvent( | ||
event, log_level, item.pop("logger"), timestamp, attributes=sanitize_dict(item) | ||
) | ||
|
||
|
||
class LogEventSerializer(PassiveSerializer): | ||
"""Single log message with all context logged.""" | ||
|
||
timestamp = DateTimeField() | ||
log_level = ChoiceField(choices=tuple((x, x) for x in NAME_TO_LEVEL.keys())) | ||
logger = CharField() | ||
event = CharField() | ||
attributes = DictField() | ||
|
||
# TODO(2024.6?): This is a migration helper to return a correct API response for logs that | ||
# have been saved in an older format (mostly just list[str] with just the messages) | ||
def to_representation(self, instance): | ||
if isinstance(instance, str): | ||
instance = LogEvent(instance, "", "") | ||
elif isinstance(instance, list): | ||
instance = [LogEvent(x, "", "") for x in instance] | ||
return super().to_representation(instance) | ||
|
||
|
||
@contextmanager | ||
def capture_logs(log_default_output=True) -> Generator[list[LogEvent], None, None]: | ||
"""Capture log entries created""" | ||
logs = [] | ||
cap = LogCapture() | ||
# Modify `_Configuration.default_processors` set via `configure` but always | ||
# keep the list instance intact to not break references held by bound | ||
# loggers. | ||
processors: list = get_config()["processors"] | ||
old_processors = processors.copy() | ||
try: | ||
# clear processors list and use LogCapture for testing | ||
if ProcessorFormatter.wrap_for_formatter in processors: | ||
processors.remove(ProcessorFormatter.wrap_for_formatter) | ||
processors.append(cap) | ||
configure(processors=processors) | ||
yield logs | ||
for raw_log in cap.entries: | ||
logs.append(LogEvent.from_event_dict(raw_log)) | ||
finally: | ||
# remove LogCapture and restore original processors | ||
processors.clear() | ||
processors.extend(old_processors) | ||
configure(processors=processors) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.