views.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. import json
  2. import logging
  3. import random
  4. import string
  5. import uuid
  6. from urllib.parse import urlparse
  7. from django.conf import settings
  8. from django.core.exceptions import SuspiciousOperation, ValidationError
  9. from django.db.models import Exists, OuterRef
  10. from django.db.utils import IntegrityError
  11. from django.http import HttpResponse
  12. from django.test import RequestFactory
  13. from rest_framework import exceptions, permissions, status
  14. from rest_framework.response import Response
  15. from rest_framework.views import APIView
  16. from sentry_sdk import capture_exception, set_context, set_level
  17. from difs.models import DebugInformationFile
  18. from difs.tasks import difs_run_resolve_stacktrace
  19. from performance.serializers import TransactionEventSerializer
  20. from projects.models import Project
  21. from sentry.utils.auth import parse_auth_header
  22. from .negotiation import IgnoreClientContentNegotiation
  23. from .parsers import EnvelopeParser
  24. from .serializers import (
  25. EnvelopeHeaderSerializer,
  26. StoreCSPReportSerializer,
  27. StoreDefaultSerializer,
  28. StoreErrorSerializer,
  29. )
  30. logger = logging.getLogger(__name__)
  31. def test_event_view(request):
  32. """
  33. This view is used only to test event store performance
  34. It requires DEBUG to be True
  35. """
  36. factory = RequestFactory()
  37. request = request = factory.get(
  38. "/api/6/store/?sentry_key=244703e8083f4b16988c376ea46e9a08"
  39. )
  40. with open("events/test_data/py_hi_event.json") as json_file:
  41. data = json.load(json_file)
  42. data["event_id"] = uuid.uuid4()
  43. data["message"] = "".join(
  44. random.choices(string.ascii_uppercase + string.digits, k=8)
  45. )
  46. request.data = data
  47. EventStoreAPIView().post(request, id=6)
  48. return HttpResponse("<html><body></body></html>")
  49. class BaseEventAPIView(APIView):
  50. permission_classes = [permissions.AllowAny]
  51. authentication_classes = []
  52. content_negotiation_class = IgnoreClientContentNegotiation
  53. http_method_names = ["post"]
  54. @classmethod
  55. def auth_from_request(cls, request):
  56. # Accept both sentry or glitchtip prefix.
  57. # Prefer glitchtip when not using a sentry SDK but support both.
  58. result = {
  59. k: request.GET[k]
  60. for k in request.GET.keys()
  61. if k[:7] == "sentry_" or k[:10] == "glitchtip_"
  62. }
  63. if request.META.get("HTTP_X_SENTRY_AUTH", "")[:7].lower() == "sentry ":
  64. if result:
  65. raise SuspiciousOperation(
  66. "Multiple authentication payloads were detected."
  67. )
  68. result = parse_auth_header(request.META["HTTP_X_SENTRY_AUTH"])
  69. elif request.META.get("HTTP_AUTHORIZATION", "")[:7].lower() == "sentry ":
  70. if result:
  71. raise SuspiciousOperation(
  72. "Multiple authentication payloads were detected."
  73. )
  74. result = parse_auth_header(request.META["HTTP_AUTHORIZATION"])
  75. if not result:
  76. if (
  77. isinstance(request.data, list)
  78. and len(request.data)
  79. and "dsn" in request.data[0]
  80. ):
  81. dsn = urlparse(request.data[0]["dsn"])
  82. if username := dsn.username:
  83. return username
  84. raise exceptions.NotAuthenticated(
  85. "Unable to find authentication information"
  86. )
  87. return result.get("sentry_key", result.get("glitchtip_key"))
  88. def get_project(self, request, project_id):
  89. sentry_key = BaseEventAPIView.auth_from_request(request)
  90. difs_subquery = DebugInformationFile.objects.filter(project_id=OuterRef("pk"))
  91. try:
  92. project = (
  93. Project.objects.filter(id=project_id, projectkey__public_key=sentry_key)
  94. .annotate(has_difs=Exists(difs_subquery))
  95. .select_related("organization")
  96. .only("id", "first_event", "organization__is_accepting_events")
  97. .first()
  98. )
  99. except ValidationError as err:
  100. raise exceptions.AuthenticationFailed({"error": "Invalid api key"}) from err
  101. if not project:
  102. if Project.objects.filter(id=project_id).exists():
  103. raise exceptions.AuthenticationFailed({"error": "Invalid api key"})
  104. raise exceptions.ValidationError("Invalid project_id: %s" % project_id)
  105. if not project.organization.is_accepting_events:
  106. raise exceptions.Throttled(detail="event rejected due to rate limit")
  107. return project
  108. def get_event_serializer_class(self, data=None):
  109. """Determine event type and return serializer"""
  110. if data is None:
  111. data = []
  112. if "exception" in data and data["exception"]:
  113. return StoreErrorSerializer
  114. if "platform" not in data:
  115. return StoreCSPReportSerializer
  116. return StoreDefaultSerializer
  117. def process_event(self, data, request, project):
  118. set_context("incoming event", data)
  119. serializer = self.get_event_serializer_class(data)(
  120. data=data, context={"request": self.request, "project": project}
  121. )
  122. try:
  123. serializer.is_valid(raise_exception=True)
  124. except exceptions.ValidationError as err:
  125. set_level("warning")
  126. capture_exception(err)
  127. logger.warning("Invalid event %s", serializer.errors)
  128. return Response()
  129. event = serializer.save()
  130. if event.data.get("exception") is not None and project.has_difs:
  131. difs_run_resolve_stacktrace(event.event_id)
  132. return Response({"id": event.event_id_hex})
  133. class EventStoreAPIView(BaseEventAPIView):
  134. def post(self, request, *args, **kwargs):
  135. if settings.MAINTENANCE_EVENT_FREEZE:
  136. return Response(
  137. {
  138. "message": "Events are not currently being accepted due to database maintenance."
  139. },
  140. status=status.HTTP_503_SERVICE_UNAVAILABLE,
  141. )
  142. if settings.EVENT_STORE_DEBUG:
  143. print(json.dumps(request.data))
  144. try:
  145. project = self.get_project(request, kwargs.get("id"))
  146. except exceptions.AuthenticationFailed as err:
  147. # Replace 403 status code with 401 to match OSS Sentry
  148. return Response(err.detail, status=401)
  149. return self.process_event(request.data, request, project)
  150. class CSPStoreAPIView(EventStoreAPIView):
  151. pass
  152. class EnvelopeAPIView(BaseEventAPIView):
  153. parser_classes = [EnvelopeParser]
  154. def get_serializer_class(self):
  155. return TransactionEventSerializer
  156. def post(self, request, *args, **kwargs):
  157. if settings.MAINTENANCE_EVENT_FREEZE:
  158. return Response(
  159. {
  160. "message": "Events are not currently being accepted due to database maintenance."
  161. },
  162. status=status.HTTP_503_SERVICE_UNAVAILABLE,
  163. )
  164. if settings.EVENT_STORE_DEBUG:
  165. print(json.dumps(request.data))
  166. project = self.get_project(request, kwargs.get("id"))
  167. data = request.data
  168. if len(data) < 2:
  169. logger.warning("Envelope has no headers %s", data)
  170. raise exceptions.ValidationError("Envelope has no headers")
  171. event_header_serializer = EnvelopeHeaderSerializer(data=data.pop(0))
  172. event_header_serializer.is_valid(raise_exception=True)
  173. # Multi part envelopes are not yet supported
  174. message_header = data.pop(0)
  175. if message_header.get("type") == "transaction":
  176. serializer = self.get_serializer_class()(
  177. data=data.pop(0), context={"request": self.request, "project": project}
  178. )
  179. try:
  180. serializer.is_valid(raise_exception=True)
  181. except exceptions.ValidationError as err:
  182. logger.warning("Invalid envelope payload", exc_info=True)
  183. raise err
  184. try:
  185. event = serializer.save()
  186. except IntegrityError as err:
  187. logger.warning("Duplicate event id", exc_info=True)
  188. raise exceptions.ValidationError("Duplicate event id") from err
  189. return Response({"id": event.event_id_hex})
  190. elif message_header.get("type") == "event":
  191. event_data = data.pop(0)
  192. return self.process_event(event_data, request, project)
  193. elif message_header.get("type") == "session":
  194. return Response(
  195. {"message": "Session events are not supported at this time."},
  196. status=status.HTTP_501_NOT_IMPLEMENTED,
  197. )
  198. return Response(status=status.HTTP_501_NOT_IMPLEMENTED)