Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(managed-warehouse): answer unmapped hogtower adapter routes with 501 (#113811)

Benjamin Knofe-Viderff250a7af273
diff --git a/products/managed_warehouse/backend/presentation/hogtower.py b/products/managed_warehouse/backend/presentation/hogtower.py
index f416029a6471..1e68474ec397 100644
--- a/products/managed_warehouse/backend/presentation/hogtower.py
+++ b/products/managed_warehouse/backend/presentation/hogtower.py
@@ -43,6 +43,7 @@ _WAREHOUSE_STILL_EXISTS = (
)
_TEAM_PATH = re.compile(r"/teams/(?P<team_id>[^/]+)")
+_SERVICE_GRANT_PATH = re.compile(r"/service-grants/(?P<credential_id>[^/]+)")
def is_configured() -> bool:
@@ -371,6 +372,25 @@ def _refresh_credential(call: _Call) -> TranslatedResponse:
)
+def _revoke_credential(call: _Call, credential_id: str) -> TranslatedResponse:
+ """v1 DELETE /orgs/:id/service-grants/:cid -> v2 DELETE /warehouses/:id/service-credentials/:cid.
+
+ Both answer {"revoked": "<cid>"} on success, so the body passes through.
+ """
+ return call.send("DELETE", f"{call.warehouse}/service-credentials/{quote(credential_id, safe='')}")
+
+
+def _not_supported(method: str, path: str) -> TranslatedResponse:
+ """A v1 call this adapter has no v2 mapping for: 501, never forwarded to hogtower.
+
+ 501 (not 500) because nothing failed: the hogtower control plane simply does not serve
+ this v1 operation, which is the same status ``_request`` uses when no control plane is
+ configured at all. Only the method and path are logged; bodies may carry secrets.
+ """
+ logger.warning("hogtower_adapter_unmapped_route", method=method, path=path or "/")
+ return TranslatedResponse(501, {"error": f"not supported by the hogtower control plane: {method} {path or '/'}"})
+
+
# --- passthroughs -----------------------------------------------------------------------
@@ -437,9 +457,11 @@ def request(
return _update_team(call, team_id)
if method == "DELETE":
return _delete_team(call, team_id)
+ grant = _SERVICE_GRANT_PATH.fullmatch(path)
+ if handler is None and grant is not None and method == "DELETE":
+ return _revoke_credential(call, grant.group("credential_id"))
else:
handler = _GLOBAL_ROUTES.get((method, path))
if handler is None:
- logger.error("hogtower_adapter_unmapped_route", method=method, path=path)
- return TranslatedResponse(500, {"error": f"No hogtower mapping for {method} {path or '/'}"})
+ return _not_supported(method, path)
return handler(call)
diff --git a/products/managed_warehouse/backend/presentation/views.py b/products/managed_warehouse/backend/presentation/views.py
index db733a86617b..62e3dfef1f49 100644
--- a/products/managed_warehouse/backend/presentation/views.py
+++ b/products/managed_warehouse/backend/presentation/views.py
@@ -996,9 +996,10 @@ def deprovision_for_org_deletion(organization_id: UUID | str) -> None:
status_code=resp.status_code,
)
return
- if resp.status_code == status.HTTP_501_NOT_IMPLEMENTED:
+ if resp.status_code == status.HTTP_501_NOT_IMPLEMENTED and not hogtower.is_configured():
# Neither HOGTOWER_API_URL nor DUCKGRES_API_URL is configured (e.g. a
# dev/env-var-backed DuckgresServer row): there is no control plane to deprovision against.
+ # A 501 from the hogtower adapter means an unmapped route instead, and must not skip.
logger.warning(
"Managed warehouse deprovisioning skipped: provisioning API not configured",
organization_id=org_id,
diff --git a/products/managed_warehouse/backend/tests/api/test_presentation.py b/products/managed_warehouse/backend/tests/api/test_presentation.py
index 7ed268ac14b5..bfa70b8c3427 100644
--- a/products/managed_warehouse/backend/tests/api/test_presentation.py
+++ b/products/managed_warehouse/backend/tests/api/test_presentation.py
@@ -1242,6 +1242,19 @@ def test_deprovision_for_org_deletion_treats_converged_states_as_done(
mock_deprovision.assert_called_once_with(org.id, require_enabled=False, triggered_by="system:organization-deletion")
+@override_settings(HOGTOWER_API_URL="http://hogtower.invalid")
+@patch("products.managed_warehouse.backend.presentation.views.deprovision")
+def test_deprovision_for_org_deletion_does_not_skip_a_hogtower_501(mock_deprovision: MagicMock) -> None:
+ # With hogtower configured, 501 is the adapter's "unmapped route", not "no control plane":
+ # skipping would let the org cascade drop the pointer to a live warehouse.
+ org, _team, _server = _provisioned_org()
+ mock_deprovision.return_value = Response({"error": "not supported"}, status=501)
+
+ with pytest.raises(RuntimeError):
+ managed_warehouse.deprovision_for_org_deletion(org.id)
+
+
@pytest.mark.django_db
@patch("products.managed_warehouse.backend.presentation.views._request")
def test_provision_rejected_for_org_pending_deletion(mock_request: MagicMock) -> None:
diff --git a/products/managed_warehouse/backend/tests/test_hogtower.py b/products/managed_warehouse/backend/tests/test_hogtower.py
index 5a43bc9a5da8..4eb0c535bf6d 100644
--- a/products/managed_warehouse/backend/tests/test_hogtower.py
+++ b/products/managed_warehouse/backend/tests/test_hogtower.py
@@ -147,11 +147,29 @@ class TestTransport:
assert (out.status_code, out.json()) == (502, {"error": "<html>bad gateway</html>"})
- def test_unmapped_route_fails_without_calling_hogtower(self, fake: FakeHogtower) -> None:
- out = _mapped("PATCH", "/teams")
+ @pytest.mark.parametrize(
+ "method,path,shown",
+ [
+ ("PATCH", "/teams", "PATCH /teams"),
+ ("GET", "/service-grants", "GET /service-grants"),
+ ("POST", "/service-grants/svc_x", "POST /service-grants/svc_x"),
+ ("PUT", "", "PUT /"),
+ ("POST", "warehouses", "POST warehouses"),
+ ],
+ )
+ def test_unmapped_route_is_501_without_calling_hogtower(
+ self, fake: FakeHogtower, method: str, path: str, shown: str
+ ) -> None:
+ with patch("products.managed_warehouse.backend.presentation.hogtower.logger") as logger:
+ out = _mapped(method, path, json_body={"credential_secret": _FAKE_SECRET})
- assert out.status_code == 500
+ assert (out.status_code, out.json()) == (
+ 501,
+ {"error": f"not supported by the hogtower control plane: {shown}"},
+ )
assert fake.calls == []
+ logger.warning.assert_called_once_with("hogtower_adapter_unmapped_route", method=method, path=path or "/")
+ assert _FAKE_SECRET not in str(logger.mock_calls)
def test_is_configured_follows_the_setting(self) -> None:
with override_settings(HOGTOWER_API_URL=None):
@@ -430,6 +448,20 @@ class TestServiceCredentials:
assert credential.credential_secret == _FAKE_SECRET
assert fake.calls[0]["json"] == {"ttl_seconds": 900, "rotate_secret": False}
+ @_routes({("DELETE", f"{W}/service-credentials/{_CID}"): (200, {"revoked": _CID})})
+ def test_revoke_maps_the_v1_service_grant_delete(self, fake: FakeHogtower) -> None:
+ out = _mapped("DELETE", f"/service-grants/{_CID}")
+
+ assert (out.status_code, out.json()) == (200, {"revoked": _CID})
+ assert fake.paths == [("DELETE", f"{W}/service-credentials/{_CID}")]
+ assert fake.calls[0]["json"] is None
+
+ def test_revoke_unknown_credential_passes_the_error_through(self, fake: FakeHogtower) -> None:
+ out = _mapped("DELETE", "/service-grants/svc_gone")
+
+ assert (out.status_code, out.json()) == (404, {"error": "not found"})
+ assert fake.paths == [("DELETE", f"{W}/service-credentials/svc_gone")]
+
@patch("products.managed_warehouse.backend.presentation.views.is_enabled", return_value=True)
class TestViewsUseHogtower:
@@ -453,6 +485,15 @@ class TestViewsUseHogtower:
assert (resp.status_code, resp.data) == (504, {"error": "Provisioning service timed out"})
+ def test_unmapped_route_is_a_501_response(self, _enabled: MagicMock, fake: FakeHogtower) -> None:
+ resp = managed_warehouse._request("GET", ORG, "/service-grants")
+
+ assert (resp.status_code, resp.data) == (
+ 501,
+ {"error": "not supported by the hogtower control plane: GET /service-grants"},
+ )
+ assert fake.calls == []
+
@_routes({("GET", "/database-names/acme"): (200, {"name": "acme", "available": False, "reason": ""})})
def test_check_name(self, _enabled: MagicMock, fake: FakeHogtower) -> None:
resp = managed_warehouse.check_name(ORG, "acme")