Skip to content

Commit

Permalink
Handle Amazon SNS headers for moved (#3371)
Browse files Browse the repository at this point in the history
# What this PR does
Previous PR #3326 test and forwarding code was not representative of
actual request. This fixes forwarding of Amazon SNS 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)
  • Loading branch information
mderynck authored Nov 16, 2023
1 parent 1020810 commit 609da80
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

### Fixed

- Fixes forwarding of Amazon SNS headers @mderynck ([#3371](https://github.com/grafana/oncall/pull/3371))

## v1.3.59 (2023-11-16)

### Added
Expand Down
12 changes: 9 additions & 3 deletions engine/apps/user_management/middlewares.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@

logger = logging.getLogger(__name__)

AMAZON_SNS_HEADERS = [
"x-amz-sns-subscription-arn",
"x-amz-sns-topic-arn",
"x-amz-sns-message-id",
"x-amz-sns-message-type",
]


class OrganizationMovedMiddleware(MiddlewareMixin):
def process_exception(self, request, exception):
Expand All @@ -33,9 +40,8 @@ def process_exception(self, request, exception):
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
for k in AMAZON_SNS_HEADERS:
headers[k] = request.headers.get(k)

response = self.make_request(request.method, url, headers, request.body)
return HttpResponse(response.content, status=response.status_code)
Expand Down
14 changes: 9 additions & 5 deletions engine/apps/user_management/tests/test_region.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from apps.integrations.views import AlertManagerAPIView, AmazonSNS
from apps.schedules.models import OnCallScheduleWeb
from apps.user_management.exceptions import OrganizationMovedException
from apps.user_management.middlewares import AMAZON_SNS_HEADERS


@pytest.mark.django_db
Expand Down Expand Up @@ -254,10 +255,10 @@ def test_organization_moved_middleware_amazon_sns_headers(
)

expected_sns_headers = {
"x-amz-sns-subscription-arn": "arn:aws:sns:xxxxxxxxxx:467989492352:oncall-test:3aab6edb-0c5e-4fa9-b876-64409d1f6c63",
"x-amz-sns-topic-arn": "arn:aws:sns:xxxxxxxxxx:467989492352:oncall-test",
"x-amz-sns-message-id": "473efe1d-8ea4-5252-8124-a3d5ff7408c5",
"x-amz-sns-message-type": "Notification",
"HTTP_X_AMZ_SNS_SUBSCRIPTION_ARN": "arn:aws:sns:xxxxxxxxxx:467989492352:oncall-test:3aab6edb-0c5e-4fa9-b876-64409d1f6c63",
"HTTP_X_AMZ_SNS_TOPIC_ARN": "arn:aws:sns:xxxxxxxxxx:467989492352:oncall-test",
"HTTP_X_AMZ_SNS_MESSAGE_ID": "473efe1d-8ea4-5252-8124-a3d5ff7408c5",
"HTTP_X_AMZ_SNS_MESSAGE_TYPE": "Notification",
}
expected_message = bytes(f"Redirected to {region.oncall_backend_url}", "utf-8")
mocked_make_request.return_value = HttpResponse(expected_message, status=status.HTTP_200_OK)
Expand All @@ -268,6 +269,9 @@ def test_organization_moved_middleware_amazon_sns_headers(
data = {"value": "test"}
response = client.post(url, data, format="json", **expected_sns_headers)
assert mocked_make_request.called
assert expected_sns_headers.items() <= mocked_make_request.call_args.args[2].items()
for k in AMAZON_SNS_HEADERS:
assert expected_sns_headers.get(f'HTTP_{k.upper().replace("-","_")}') == mocked_make_request.call_args.args[
2
].get(k)
assert response.content == expected_message
assert response.status_code == status.HTTP_200_OK

0 comments on commit 609da80

Please sign in to comment.