Skip to content

Commit cc63ec3

Browse files
authored
fix: return a throttled response if org is being synced for the first time during auth (#5374)
Related to https://github.com/grafana/oncall-private/issues/2826 When Terraform triggers multiple requests and org needs to be synced in OnCall, the first request will wait for sync to complete but others will get an immediate response, before a 403, with these changes a 429 indicating to retry (Terraform [client](https://github.com/grafana/amixr-api-go-client/blob/main/client.go#L310) will handle the response and perform a retry).
1 parent 2503eaf commit cc63ec3

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

engine/apps/auth_token/auth.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,9 @@ def get_organization(self, request, auth):
381381
# if organization exists, we are good)
382382
setup_organization(url, auth)
383383
organization = Organization.objects.filter(grafana_url=url).first()
384+
if organization is None:
385+
# sync may still be in progress, client should retry
386+
raise exceptions.Throttled(detail="Organization being synced, please retry.")
384387
return organization
385388

386389
if settings.LICENSE == settings.CLOUD_LICENSE_NAME:

engine/apps/auth_token/tests/test_grafana_auth.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,9 @@ def test_grafana_authentication_no_org_grafana_url():
110110
request_sync_url = f"{grafana_url}/api/plugins/{PluginID.ONCALL}/resources/plugin/sync?wait=true&force=true"
111111
httpretty.register_uri(httpretty.POST, request_sync_url, status=404)
112112

113-
with pytest.raises(exceptions.AuthenticationFailed) as exc:
113+
with pytest.raises(exceptions.Throttled) as exc:
114114
GrafanaServiceAccountAuthentication().authenticate(request)
115-
assert exc.value.detail == "Organization not found."
115+
assert exc.value.detail == "Organization being synced, please retry."
116116

117117

118118
@pytest.mark.parametrize("grafana_url", ["null;", "foo", ""])

0 commit comments

Comments
 (0)