2020-12-05 21:08:42 +00:00
|
|
|
"""authentik sentry integration"""
|
2021-04-10 21:42:42 +00:00
|
|
|
from typing import Optional
|
|
|
|
|
2020-11-11 21:35:40 +00:00
|
|
|
from aioredis.errors import ConnectionClosedError, ReplyError
|
2021-08-21 12:14:13 +00:00
|
|
|
from billiard.exceptions import SoftTimeLimitExceeded, WorkerLostError
|
2020-09-15 09:29:43 +00:00
|
|
|
from celery.exceptions import CeleryError
|
2021-04-27 14:21:44 +00:00
|
|
|
from channels.middleware import BaseMiddleware
|
2020-09-30 13:55:59 +00:00
|
|
|
from channels_redis.core import ChannelFull
|
2021-11-15 21:18:56 +00:00
|
|
|
from django.conf import settings
|
2021-08-03 15:45:16 +00:00
|
|
|
from django.core.exceptions import ImproperlyConfigured, SuspiciousOperation, ValidationError
|
2020-02-23 18:48:14 +00:00
|
|
|
from django.db import InternalError, OperationalError, ProgrammingError
|
2021-04-29 13:25:54 +00:00
|
|
|
from django.http.response import Http404
|
2020-02-23 18:48:14 +00:00
|
|
|
from django_redis.exceptions import ConnectionInterrupted
|
2021-04-08 20:03:10 +00:00
|
|
|
from docker.errors import DockerException
|
2021-11-04 12:16:10 +00:00
|
|
|
from h11 import LocalProtocolError
|
2020-09-19 13:25:17 +00:00
|
|
|
from ldap3.core.exceptions import LDAPException
|
2020-09-06 14:51:50 +00:00
|
|
|
from redis.exceptions import ConnectionError as RedisConnectionError
|
2020-10-28 18:00:11 +00:00
|
|
|
from redis.exceptions import RedisError, ResponseError
|
2020-02-23 18:48:14 +00:00
|
|
|
from rest_framework.exceptions import APIException
|
2021-04-27 14:21:44 +00:00
|
|
|
from sentry_sdk import Hub
|
|
|
|
from sentry_sdk.tracing import Transaction
|
2021-01-01 14:39:43 +00:00
|
|
|
from structlog.stdlib import get_logger
|
2020-09-30 13:55:59 +00:00
|
|
|
from websockets.exceptions import WebSocketException
|
2019-06-25 16:00:54 +00:00
|
|
|
|
2021-04-27 14:21:44 +00:00
|
|
|
from authentik.lib.utils.reflection import class_to_path
|
|
|
|
|
2019-10-04 08:08:53 +00:00
|
|
|
LOGGER = get_logger()
|
2019-04-29 17:16:49 +00:00
|
|
|
|
|
|
|
|
2021-04-27 14:21:44 +00:00
|
|
|
class SentryWSMiddleware(BaseMiddleware):
|
|
|
|
"""Sentry Websocket middleweare to set the transaction name based on
|
|
|
|
consumer class path"""
|
|
|
|
|
|
|
|
async def __call__(self, scope, receive, send):
|
|
|
|
transaction: Optional[Transaction] = Hub.current.scope.transaction
|
|
|
|
class_path = class_to_path(self.inner.consumer_class)
|
|
|
|
if transaction:
|
|
|
|
transaction.name = class_path
|
|
|
|
return await self.inner(scope, receive, send)
|
|
|
|
|
|
|
|
|
2020-02-20 20:37:14 +00:00
|
|
|
class SentryIgnoredException(Exception):
|
2020-02-24 13:40:12 +00:00
|
|
|
"""Base Class for all errors that are suppressed, and not sent to sentry."""
|
2020-02-20 20:37:14 +00:00
|
|
|
|
|
|
|
|
2021-04-10 21:42:42 +00:00
|
|
|
def before_send(event: dict, hint: dict) -> Optional[dict]:
|
2019-04-29 17:16:49 +00:00
|
|
|
"""Check if error is database error, and ignore if so"""
|
2021-08-21 13:12:18 +00:00
|
|
|
# pylint: disable=no-name-in-module
|
|
|
|
from psycopg2.errors import Error
|
|
|
|
|
2019-06-25 16:00:54 +00:00
|
|
|
ignored_classes = (
|
2020-10-28 18:00:11 +00:00
|
|
|
# Inbuilt types
|
|
|
|
KeyboardInterrupt,
|
|
|
|
ConnectionResetError,
|
|
|
|
OSError,
|
2021-04-08 20:03:10 +00:00
|
|
|
PermissionError,
|
2021-05-20 17:18:35 +00:00
|
|
|
# Django Errors
|
2021-08-21 12:14:13 +00:00
|
|
|
Error,
|
2021-05-20 17:18:35 +00:00
|
|
|
ImproperlyConfigured,
|
2019-04-29 17:16:49 +00:00
|
|
|
OperationalError,
|
2020-02-23 18:48:14 +00:00
|
|
|
InternalError,
|
|
|
|
ProgrammingError,
|
2021-04-22 18:17:00 +00:00
|
|
|
SuspiciousOperation,
|
2020-10-28 18:00:11 +00:00
|
|
|
ValidationError,
|
|
|
|
# Redis errors
|
|
|
|
RedisConnectionError,
|
2019-04-29 17:16:49 +00:00
|
|
|
ConnectionInterrupted,
|
2020-10-28 18:00:11 +00:00
|
|
|
RedisError,
|
|
|
|
ResponseError,
|
|
|
|
ReplyError,
|
2020-11-11 13:48:19 +00:00
|
|
|
ConnectionClosedError,
|
2020-10-28 18:00:11 +00:00
|
|
|
# websocket errors
|
|
|
|
ChannelFull,
|
|
|
|
WebSocketException,
|
2021-11-04 12:16:10 +00:00
|
|
|
LocalProtocolError,
|
2020-10-28 18:00:11 +00:00
|
|
|
# rest_framework error
|
2019-06-25 16:00:54 +00:00
|
|
|
APIException,
|
2020-10-28 18:00:11 +00:00
|
|
|
# celery errors
|
2019-06-25 16:00:54 +00:00
|
|
|
WorkerLostError,
|
2020-10-28 18:00:11 +00:00
|
|
|
CeleryError,
|
2021-08-21 12:14:13 +00:00
|
|
|
SoftTimeLimitExceeded,
|
2020-10-28 18:00:11 +00:00
|
|
|
# custom baseclass
|
2020-02-24 18:14:43 +00:00
|
|
|
SentryIgnoredException,
|
2020-10-28 18:00:11 +00:00
|
|
|
# ldap errors
|
2020-09-19 13:24:52 +00:00
|
|
|
LDAPException,
|
2021-04-08 20:03:10 +00:00
|
|
|
# Docker errors
|
|
|
|
DockerException,
|
2021-04-29 13:25:54 +00:00
|
|
|
# End-user errors
|
|
|
|
Http404,
|
2019-06-25 16:00:54 +00:00
|
|
|
)
|
2021-11-29 20:37:29 +00:00
|
|
|
exc_value = None
|
2019-12-31 11:51:16 +00:00
|
|
|
if "exc_info" in hint:
|
2020-07-07 12:02:20 +00:00
|
|
|
_, exc_value, _ = hint["exc_info"]
|
2020-02-24 18:14:43 +00:00
|
|
|
if isinstance(exc_value, ignored_classes):
|
2022-01-03 20:33:52 +00:00
|
|
|
LOGGER.debug("dropping exception", exc=exc_value)
|
2019-04-29 17:16:49 +00:00
|
|
|
return None
|
2021-04-10 21:20:20 +00:00
|
|
|
if "logger" in event:
|
2021-05-18 09:29:36 +00:00
|
|
|
if event["logger"] in [
|
|
|
|
"kombu",
|
|
|
|
"asyncio",
|
|
|
|
"multiprocessing",
|
|
|
|
"django_redis",
|
2021-11-29 20:37:29 +00:00
|
|
|
"django.security.DisallowedHost",
|
2021-12-20 12:12:14 +00:00
|
|
|
"django_redis.cache",
|
2021-12-20 18:44:08 +00:00
|
|
|
"celery.backends.redis",
|
2021-12-20 20:04:45 +00:00
|
|
|
"celery.worker",
|
2022-01-21 09:46:33 +00:00
|
|
|
"paramiko.transport",
|
2021-05-18 09:29:36 +00:00
|
|
|
]:
|
2021-04-10 21:20:20 +00:00
|
|
|
return None
|
2021-11-29 20:37:29 +00:00
|
|
|
LOGGER.debug("sending event to sentry", exc=exc_value, source_logger=event.get("logger", None))
|
2022-01-01 17:56:14 +00:00
|
|
|
if settings.DEBUG or settings.TEST:
|
2021-11-15 21:18:56 +00:00
|
|
|
return None
|
2019-04-29 17:16:49 +00:00
|
|
|
return event
|