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

Handle Amazon SNS headers for moved #3371

Merged
merged 4 commits into from
Nov 16, 2023
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
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
Loading