oncall-engine/engine/apps/user_management/middlewares.py
Michael Derynck 6fa4df0afe
Forward headers for Amazon SNS (#3326)
# What this PR does
Forward headers for Amazon SNS when forwarding requests for moved
organizations. Previous
[PR](https://github.com/grafana/oncall/pull/3315) missed this since the
test did not check mocked make_request for headers.

## Which issue(s) this PR fixes

## Checklist

- [x] Unit, integration, and e2e (if applicable) tests updated
- [x] Documentation added (or `pr:no public docs` PR label added if not
required)
- [x] `CHANGELOG.md` updated (or `pr:no changelog` PR label added if not
required)
2023-11-11 11:11:51 -07:00

62 lines
2.5 KiB
Python

import logging
import requests
from django.http import HttpResponse, JsonResponse
from django.utils.deprecation import MiddlewareMixin
from rest_framework import status
from common.api_helpers.utils import create_engine_url
from .exceptions import OrganizationDeletedException, OrganizationMovedException
logger = logging.getLogger(__name__)
class OrganizationMovedMiddleware(MiddlewareMixin):
def process_exception(self, request, exception):
if isinstance(exception, OrganizationMovedException):
region = exception.organization.migration_destination
if not region.oncall_backend_url:
return HttpResponse(
"Organization migration destination undefined URL", status=status.HTTP_500_INTERNAL_SERVER_ERROR
)
url = create_engine_url(request.path, override_base=region.oncall_backend_url)
if (v := request.META.get("QUERY_STRING", None)) is not None:
url = f"{url}?{v}"
headers = {}
if (v := request.META.get("CONTENT_TYPE", None)) is not None:
headers["Content-type"] = v
if (v := request.META.get("HTTP_AUTHORIZATION", None)) is not None:
headers["Authorization"] = v
if "amazon_sns" in request.path:
for k, v in request.META.items():
if k.startswith("x-amz-sns-"):
headers[k] = v
response = self.make_request(request.method, url, headers, request.body)
return HttpResponse(response.content, status=response.status_code)
def make_request(self, method, url, headers, body):
if method == "GET":
return requests.get(url, headers=headers)
elif method == "POST":
return requests.post(url, data=body, headers=headers)
elif method == "PUT":
return requests.put(url, data=body, headers=headers)
elif method == "DELETE":
return requests.delete(url, headers=headers)
elif method == "OPTIONS":
return requests.options(url, headers=headers)
elif method == "PATCH":
return requests.patch(url, data=body, headers=headers)
class OrganizationDeletedMiddleware(MiddlewareMixin):
def process_exception(self, request, exception):
if isinstance(exception, OrganizationDeletedException):
# Return drf-shaped not-found response to keep responses consistent
return JsonResponse(status=status.HTTP_404_NOT_FOUND, data={"detail": "Not found."})