From ec874440ba43087dd604c10a976d75c1131a9f36 Mon Sep 17 00:00:00 2001 From: Matias Bordese Date: Wed, 11 Dec 2024 11:50:49 -0300 Subject: [PATCH] chore: update service account token auth organization setup check (#5354) Ignore setup organization response (for now, since it can return a 400 when a sync is/was recently in progress) and base response on organization being available or not instead. --- engine/apps/auth_token/auth.py | 9 +++++---- engine/apps/auth_token/tests/test_grafana_auth.py | 4 ++-- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/engine/apps/auth_token/auth.py b/engine/apps/auth_token/auth.py index af1fc2c6b4..85126ef64d 100644 --- a/engine/apps/auth_token/auth.py +++ b/engine/apps/auth_token/auth.py @@ -361,7 +361,7 @@ def authenticate(self, request): organization = self.get_organization(request, auth) if not organization: - raise exceptions.AuthenticationFailed("Invalid organization.") + raise exceptions.AuthenticationFailed("Organization not found.") if organization.is_moved: raise OrganizationMovedException(organization) if organization.deleted_at: @@ -374,9 +374,10 @@ def get_organization(self, request, auth): if grafana_url: organization = Organization.objects.filter(grafana_url=grafana_url).first() if not organization: - success = setup_organization(grafana_url, auth) - if not success: - raise exceptions.AuthenticationFailed("Invalid Grafana URL.") + # trigger a request to sync the organization + # (ignore response since we can get a 400 if sync was already triggered; + # if organization exists, we are good) + setup_organization(grafana_url, auth) organization = Organization.objects.filter(grafana_url=grafana_url).first() return organization diff --git a/engine/apps/auth_token/tests/test_grafana_auth.py b/engine/apps/auth_token/tests/test_grafana_auth.py index 3cb01727f5..950e63e1fa 100644 --- a/engine/apps/auth_token/tests/test_grafana_auth.py +++ b/engine/apps/auth_token/tests/test_grafana_auth.py @@ -93,7 +93,7 @@ def test_grafana_authentication_missing_org(): with pytest.raises(exceptions.AuthenticationFailed) as exc: GrafanaServiceAccountAuthentication().authenticate(request) - assert exc.value.detail == "Invalid organization." + assert exc.value.detail == "Organization not found." @pytest.mark.django_db @@ -112,7 +112,7 @@ def test_grafana_authentication_invalid_grafana_url(): with pytest.raises(exceptions.AuthenticationFailed) as exc: GrafanaServiceAccountAuthentication().authenticate(request) - assert exc.value.detail == "Invalid Grafana URL." + assert exc.value.detail == "Organization not found." @pytest.mark.django_db