Integtation backsync endpoint (#4082)

# What this PR does
Adds endpoint for integration backsync
Related to https://github.com/grafana/oncall-private/issues/2542

## 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] Added the relevant release notes label (see labels prefixed w/
`release:`). These labels dictate how your PR will
    show up in the autogenerated release notes.
This commit is contained in:
Yulya Artyukhina 2024-03-20 12:26:33 +01:00 committed by GitHub
parent 5074a16861
commit ed60b67884
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 95 additions and 0 deletions

View file

@ -0,0 +1,62 @@
from unittest.mock import PropertyMock, patch
import pytest
from django.core.cache import cache
from django.urls import reverse
from rest_framework import status
from rest_framework.test import APIClient
@pytest.mark.django_db
def test_integration_backsync_endpoint(
make_organization,
make_alert_receive_channel,
make_token_for_integration,
):
organization = make_organization()
alert_receive_channel = make_alert_receive_channel(organization=organization)
_, token = make_token_for_integration(alert_receive_channel, organization)
client = APIClient()
url = reverse("integrations:integration_backsync")
response = client.post(url, format="json", HTTP_AUTHORIZATION=token)
assert response.status_code == status.HTTP_200_OK
@pytest.mark.django_db
def test_integration_backsync_endpoint_wrong_token(
make_organization,
make_alert_receive_channel,
):
client = APIClient()
url = reverse("integrations:integration_backsync")
response = client.post(url, format="json", HTTP_AUTHORIZATION="randomtesttoken")
assert response.status_code == status.HTTP_403_FORBIDDEN
@pytest.mark.django_db
def test_integration_backsync_endpoint_throttling(
make_organization,
make_alert_receive_channel,
make_token_for_integration,
):
organization = make_organization()
alert_receive_channel = make_alert_receive_channel(organization=organization)
_, token = make_token_for_integration(alert_receive_channel, organization)
client = APIClient()
url = reverse("integrations:integration_backsync")
cache.clear()
with patch(
"apps.integrations.throttlers.integration_backsync_throttler.BacksyncRateThrottle.rate",
new_callable=PropertyMock,
) as mocked_rate:
mocked_rate.return_value = "1/m"
response = client.post(url, format="json", HTTP_AUTHORIZATION=token)
assert response.status_code == status.HTTP_200_OK
response = client.post(url, format="json", HTTP_AUTHORIZATION=f"{token}")
assert response.status_code == status.HTTP_429_TOO_MANY_REQUESTS

View file

@ -0,0 +1,14 @@
from rest_framework.throttling import SimpleRateThrottle
class BacksyncRateThrottle(SimpleRateThrottle):
"""
Integration backsync rate limit
"""
scope = "backsync"
rate = "300/m"
def get_cache_key(self, request, view):
ident = request.auth.alert_receive_channel.pk
return self.cache_format % {"scope": self.scope, "ident": ident}

View file

@ -11,6 +11,7 @@ from .views import (
AmazonSNS,
GrafanaAlertingAPIView,
GrafanaAPIView,
IntegrationBacksyncAPIView,
IntegrationHeartBeatAPIView,
UniversalAPIView,
)
@ -32,6 +33,8 @@ urlpatterns = [
path("alertmanager/<str:alert_channel_key>/", AlertManagerAPIView.as_view(), name="alertmanager"),
path("amazon_sns/<str:alert_channel_key>/", AmazonSNS.as_view(), name="amazon_sns"),
path("<str:integration_type>/<str:alert_channel_key>/", UniversalAPIView.as_view(), name="universal"),
# integration backsync
path("backsync/", IntegrationBacksyncAPIView.as_view(), name="integration_backsync"),
]
if settings.FEATURE_INBOUND_EMAIL_ENABLED:

View file

@ -8,10 +8,12 @@ from django.utils import timezone
from django.utils.decorators import method_decorator
from django.views.decorators.csrf import csrf_exempt
from django_sns_view.views import SNSEndpoint
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.views import APIView
from apps.alerts.models import AlertReceiveChannel
from apps.auth_token.auth import IntegrationBacksyncAuthentication
from apps.heartbeat.tasks import process_heartbeat_task
from apps.integrations.legacy_prefix import has_legacy_prefix
from apps.integrations.mixins import (
@ -22,6 +24,7 @@ from apps.integrations.mixins import (
is_ratelimit_ignored,
)
from apps.integrations.tasks import create_alert, create_alertmanager_alerts
from apps.integrations.throttlers.integration_backsync_throttler import BacksyncRateThrottle
from apps.user_management.exceptions import OrganizationDeletedException, OrganizationMovedException
from common.api_helpers.utils import create_engine_url
@ -353,3 +356,16 @@ class IntegrationHeartBeatAPIView(AlertChannelDefiningMixin, IntegrationHeartBea
process_heartbeat_task.apply_async(
(alert_receive_channel.pk,),
)
class IntegrationBacksyncAPIView(APIView):
authentication_classes = (IntegrationBacksyncAuthentication,)
permission_classes = (IsAuthenticated,)
throttle_classes = (BacksyncRateThrottle,)
def post(self, request):
alert_receive_channel = request.auth.alert_receive_channel
integration_backsync_func = getattr(alert_receive_channel.config, "integration_backsync", None)
if integration_backsync_func:
integration_backsync_func(alert_receive_channel, request.data)
return Response(status=200)