Skip to content

Commit

Permalink
chore: update service account token auth organization setup check (#5354
Browse files Browse the repository at this point in the history
)

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.
  • Loading branch information
matiasb authored Dec 11, 2024
1 parent 59f531e commit ec87444
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
9 changes: 5 additions & 4 deletions engine/apps/auth_token/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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

Expand Down
4 changes: 2 additions & 2 deletions engine/apps/auth_token/tests/test_grafana_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit ec87444

Please sign in to comment.