Debian Codegraph @

azure-kusto-data/azure/kusto/data/_cloud_settings.py

raw
1 import dataclasses
2 from threading import Lock
3 from typing import Optional, Dict
4 from urllib.parse import urlparse
5
6 import requests
7
8 from azure.core.tracing.decorator import distributed_trace
9 from azure.core.tracing import SpanKind
10
11 from .env_utils import get_env
12 from ._telemetry import Span, MonitoredActivity
13 from .exceptions import KustoServiceError, KustoNetworkError
14
15 METADATA_ENDPOINT = "v1/rest/auth/metadata"
16
17 DEFAULT_AUTH_ENV_VAR_NAME = "AadAuthorityUri"
18 DEFAULT_KUSTO_CLIENT_APP_ID = "db662dc1-0cfe-4e1c-a843-19a68e65be58"
19 DEFAULT_PUBLIC_LOGIN_URL = "https://login.microsoftonline.com"
20 DEFAULT_REDIRECT_URI = "http://localhost"
21 DEFAULT_KUSTO_SERVICE_RESOURCE_ID = "https://kusto.kusto.windows.net"
22 DEFAULT_DEV_KUSTO_SERVICE_RESOURCE_ID = "https://kusto.dev.kusto.windows.net"
23 DEFAULT_FIRST_PARTY_AUTHORITY_URL = "https://login.microsoftonline.com/f8cdef31-a31e-4b4a-93e4-5f571e91255a"
24
25
26 @dataclasses.dataclass
27 class CloudInfo:
28 """This class holds the data for a specific cloud instance."""
29
30 login_endpoint: str
31 login_mfa_required: bool
32 kusto_client_app_id: str
33 kusto_client_redirect_uri: str
34 kusto_service_resource_id: str
35 first_party_authority_url: str
36
37 def authority_uri(self, authority_id: Optional[str]):
38 return self.login_endpoint + "/" + (authority_id or "organizations")
39
40
41 class CloudSettings:
42 """This class holds data for all cloud instances, and returns the specific data instance by parsing the dns suffix from a URL"""
43
44 _cloud_info = None
45 _cloud_cache = {}
46 _cloud_cache_lock = Lock()
47
48 DEFAULT_CLOUD = CloudInfo(
49 login_endpoint=get_env(DEFAULT_AUTH_ENV_VAR_NAME, default=DEFAULT_PUBLIC_LOGIN_URL),
50 login_mfa_required=False,
51 kusto_client_app_id=DEFAULT_KUSTO_CLIENT_APP_ID,
52 kusto_client_redirect_uri=DEFAULT_REDIRECT_URI,
53 kusto_service_resource_id=DEFAULT_KUSTO_SERVICE_RESOURCE_ID,
54 first_party_authority_url=DEFAULT_FIRST_PARTY_AUTHORITY_URL,
55 )
56
57 @classmethod
58 @distributed_trace(name_of_span="CloudSettings.get_cloud_info", kind=SpanKind.CLIENT)
59 def get_cloud_info_for_cluster(cls, kusto_uri: str, proxies: Optional[Dict[str, str]] = None, session: requests.Session = None) -> CloudInfo:
60 normalized_authority = cls._normalize_uri(kusto_uri)
61
62 # tracing attributes for cloud info
63 Span.set_cloud_info_attributes(kusto_uri)
64
65 if normalized_authority in cls._cloud_cache: # Double-checked locking to avoid unnecessary lock access
66 return cls._cloud_cache[normalized_authority]
67
68 with cls._cloud_cache_lock:
69 if normalized_authority in cls._cloud_cache:
70 return cls._cloud_cache[normalized_authority]
71
72 url_parts = urlparse(kusto_uri)
73 url = f"{url_parts.scheme}://{url_parts.netloc}/{METADATA_ENDPOINT}"
74
75 try:
76 # trace http get call for result
77 result = MonitoredActivity.invoke(
78 lambda: (session or requests).get(url, proxies=proxies, allow_redirects=False),
79 name_of_span="CloudSettings.http_get",
80 tracing_attributes=Span.create_http_attributes(url=url, method="GET"),
81 )
82 except Exception as e:
83 raise KustoNetworkError(url) from e
84
85 if result.status_code == 200:
86 content = result.json()
87 if content is None or content == {}:
88 raise KustoServiceError("Kusto returned an invalid cloud metadata response", result)
89 root = content["AzureAD"]
90 if root is not None:
91 cls._cloud_cache[normalized_authority] = CloudInfo(
92 login_endpoint=root["LoginEndpoint"],
93 login_mfa_required=root["LoginMfaRequired"],
94 kusto_client_app_id=root["KustoClientAppId"],
95 kusto_client_redirect_uri=root["KustoClientRedirectUri"],
96 kusto_service_resource_id=root["KustoServiceResourceId"],
97 first_party_authority_url=root["FirstPartyAuthorityUrl"],
98 )
99 else:
100 cls._cloud_cache[normalized_authority] = cls.DEFAULT_CLOUD
101 elif result.status_code == 404:
102 # For now as long not all proxies implement the metadata endpoint, if no endpoint exists return public cloud data
103 cls._cloud_cache[normalized_authority] = cls.DEFAULT_CLOUD
104 else:
105 raise KustoServiceError("Kusto returned an invalid cloud metadata response", result)
106 return cls._cloud_cache[normalized_authority]
107
108 @classmethod
109 def add_to_cache(cls, url: str, cloud_info: CloudInfo):
110 with cls._cloud_cache_lock:
111 cls._cloud_cache[cls._normalize_uri(url)] = cloud_info
112
113 @classmethod
114 def _normalize_uri(cls, kusto_uri):
115 """Extracts and returns the authority part of the URI (schema, host, port)"""
116 url_parts = urlparse(kusto_uri)
117 # Return only the scheme and netloc (which contains host and port if present)
118 return f"{url_parts.scheme}://{url_parts.netloc}"