: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(warehouse_sources): say whether an Azure scope or a role is wrong (#95771)
Tom Owers21553453420d
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/azure_cost_management/azure_cost_management.py b/products/warehouse_sources/backend/temporal/data_imports/sources/azure_cost_management/azure_cost_management.pyindex 62117d2335d0..79b64035cf88 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/azure_cost_management/azure_cost_management.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/azure_cost_management/azure_cost_management.py@@ -89,6 +89,11 @@ def normalize_scope(scope: str) -> str:trimmed = raw.strip("/")if not trimmed:raise ValueError("Azure Cost Management scope is required")+ # Every Cost Management scope names a collection and an id (`subscriptions/<id>`,+ # `providers/Microsoft.Billing/billingAccounts/<id>`). A single segment is usually a bare+ # subscription id copied out of the portal, and it can only 404 once it reaches Azure.+ if "/" not in trimmed:+ raise ValueError("Azure Cost Management scope must be an ARM path such as subscriptions/<subscription id>")return trimmed@@ -387,6 +392,26 @@ class AzureCostManagementClient:return response.json()+def _scope_probe_failure_message(error: Exception) -> str:+ """Name the one thing to fix. Azure denies a scope the service principal may not read (403)+ separately from a scope it cannot find (400/404), and the two need opposite fixes."""+ status = getattr(getattr(error, "response", None), "status_code", None)+ if status == 403:+ return (+ "The service principal cannot read Cost Management on this scope. "+ "Give it the Cost Management Reader role on the scope, then connect again."+ )+ if status in (400, 404):+ return (+ "Azure has no Cost Management scope at this path. Check it is an Azure Resource "+ "Manager path such as subscriptions/<subscription id>, then connect again."+ )+ return (+ "Could not read Azure Cost Management for that scope. Check the scope path and that the "+ "service principal has the Cost Management Reader role on it."+ )++def validate_credentials(tenant_id: str, client_id: str, client_secret: str, scope: str, api_version: str) -> tuple[bool, Optional[str]]:@@ -405,11 +430,8 @@ def validate_credentials(try:client.request("GET", _endpoint_url(normalized_scope, "dimensions", api_version))- except Exception:- return (- False,- "Could not read Azure Cost Management for that scope. Check the scope path and that the service principal has the Cost Management Reader role on it.",- )+ except Exception as error:+ return False, _scope_probe_failure_message(error)return True, Nonediff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/azure_cost_management/tests/test_azure_cost_management.py b/products/warehouse_sources/backend/temporal/data_imports/sources/azure_cost_management/tests/test_azure_cost_management.pyindex b391f28e9648..189a6289cca2 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/azure_cost_management/tests/test_azure_cost_management.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/azure_cost_management/tests/test_azure_cost_management.py@@ -176,6 +176,8 @@ class TestNormalizeScope:("query_delimiter", "subscriptions/abc/resources?api-version=2021-04-01"),("fragment_delimiter", "subscriptions/abc/resources#"),("backslash", "subscriptions\\abc"),+ # A bare subscription id, without the collection segment Azure needs in front of it.+ ("single_segment", "00000000-0000-0000-0000-000000000000"),])def test_rejects_non_path_scopes(self, _name: str, raw: str) -> None:@@ -518,6 +520,16 @@ class TestValidateCredentials:assert valid is Falseassert message is not None and "Cost Management Reader" in message+ @parameterized.expand([(400,), (404,)])+ def test_unknown_scope_reports_the_path_not_the_role(self, status: int) -> None:+ session = _FakeSession([_token_response(), _FakeResponse(status, reason="Not Found")])+ with mock.patch(f"{TRANSPORT_MODULE}.make_tracked_session", return_value=session):+ valid, message = validate_credentials("tenant", "client", "secret", "subscriptions/abc", "2025-03-01")++ assert valid is False+ assert message is not None and "Azure Resource Manager path" in message+ assert "Cost Management Reader" not in message+def _run_rows(session: _FakeSession,