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

events: fix race condition #10602

Merged
merged 1 commit into from
Jul 24, 2024
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
11 changes: 10 additions & 1 deletion authentik/events/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

_CTX_OVERWRITE_USER = ContextVar[User | None]("authentik_events_log_overwrite_user", default=None)
_CTX_IGNORE = ContextVar[bool]("authentik_events_log_ignore", default=False)
_CTX_REQUEST = ContextVar[HttpRequest | None]("authentik_events_log_request", default=None)


def should_log_model(model: Model) -> bool:
Expand Down Expand Up @@ -149,11 +150,13 @@
m2m_changed.disconnect(dispatch_uid=request.request_id)

def __call__(self, request: HttpRequest) -> HttpResponse:
_CTX_REQUEST.set(request)
self.connect(request)

response = self.get_response(request)

self.disconnect(request)
_CTX_REQUEST.set(None)
return response

def process_exception(self, request: HttpRequest, exception: Exception):
Expand All @@ -167,7 +170,7 @@
thread = EventNewThread(
EventAction.SUSPICIOUS_REQUEST,
request,
message=str(exception),
message=exception_to_string(exception),
)
thread.run()
elif before_send({}, {"exc_info": (None, exception, None)}) is not None:
Expand All @@ -192,6 +195,8 @@
return
if _CTX_IGNORE.get():
return
if request.request_id != _CTX_REQUEST.get().request_id:
return
user = self.get_user(request)

action = EventAction.MODEL_CREATED if created else EventAction.MODEL_UPDATED
Expand All @@ -205,6 +210,8 @@
return
if _CTX_IGNORE.get():
return
if request.request_id != _CTX_REQUEST.get().request_id:
return

Check warning on line 214 in authentik/events/middleware.py

View check run for this annotation

Codecov / codecov/patch

authentik/events/middleware.py#L214

Added line #L214 was not covered by tests
user = self.get_user(request)

EventNewThread(
Expand All @@ -230,6 +237,8 @@
return
if _CTX_IGNORE.get():
return
if request.request_id != _CTX_REQUEST.get().request_id:
return

Check warning on line 241 in authentik/events/middleware.py

View check run for this annotation

Codecov / codecov/patch

authentik/events/middleware.py#L241

Added line #L241 was not covered by tests
user = self.get_user(request)

EventNewThread(
Expand Down
2 changes: 2 additions & 0 deletions authentik/events/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,8 @@ def from_http(self, request: HttpRequest, user: User | None = None) -> "Event":
"args": cleanse_dict(QueryDict(request.META.get("QUERY_STRING", ""))),
"user_agent": request.META.get("HTTP_USER_AGENT", ""),
}
if hasattr(request, "request_id"):
self.context["http_request"]["request_id"] = request.request_id
# Special case for events created during flow execution
# since they keep the http query within a wrapped query
if QS_QUERY in self.context["http_request"]["args"]:
Expand Down
2 changes: 2 additions & 0 deletions authentik/stages/authenticator_validate/tests/test_duo.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from rest_framework.exceptions import ValidationError

from authentik.brands.utils import get_brand_for_request
from authentik.core.middleware import RESPONSE_HEADER_ID
from authentik.core.tests.utils import create_test_admin_user, create_test_flow
from authentik.events.models import Event, EventAction
from authentik.flows.models import FlowDesignation, FlowStageBinding
Expand Down Expand Up @@ -186,6 +187,7 @@ def test_full(self):
"method": "GET",
"path": f"/api/v3/flows/executor/{flow.slug}/",
"user_agent": "",
"request_id": response[RESPONSE_HEADER_ID],
},
},
)
Loading