1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736
|
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License
import abc
import asyncio
import inspect
import time
from datetime import datetime
from threading import Lock
from typing import Callable, Coroutine, List, Optional, Any
import requests
from azure.core.exceptions import ClientAuthenticationError
from azure.core.tracing import SpanKind
from azure.core.tracing.decorator import distributed_trace
from azure.core.tracing.decorator_async import distributed_trace_async
from azure.identity import AzureCliCredential, ManagedIdentityCredential, DeviceCodeCredential
from msal import ConfidentialClientApplication, PublicClientApplication
from ._cloud_settings import CloudInfo, CloudSettings
from ._telemetry import MonitoredActivity
from .exceptions import KustoAioSyntaxError, KustoAsyncUsageError, KustoClientError
DeviceCallbackType = Callable[[str, str, datetime], None]
"""A callback enabling control of how authentication
instructions are presented. Must accept arguments (``verification_uri``, ``user_code``, ``expires_on``):
- ``verification_uri`` (str) the URL the user must visit
- ``user_code`` (str) the code the user must enter there
- ``expires_on`` (datetime.datetime) the UTC time at which the code will expire
If this argument isn't provided, the credential will print instructions to stdout."""
try:
from asgiref.sync import sync_to_async
except ImportError:
def sync_to_async(f):
raise KustoAioSyntaxError()
try:
from azure.identity.aio import (
ManagedIdentityCredential as AsyncManagedIdentityCredential,
AzureCliCredential as AsyncAzureCliCredential,
DefaultAzureCredential as AsyncDefaultAzureCredential,
)
from azure.core.credentials_async import AsyncTokenCredential
except ImportError:
# These are here in case the user doesn't have the aio optional dependency installed, but still tries to use async.
# They will give them a useful error message, and will appease linters.
class AsyncManagedIdentityCredential:
def __init__(self):
raise KustoAioSyntaxError()
class AsyncAzureCliCredential:
def __init__(self):
raise KustoAioSyntaxError()
class AsyncDefaultAzureCredential:
def __init__(self):
raise KustoAioSyntaxError()
class AsyncTokenCredential:
def __init__(self):
raise KustoAioSyntaxError()
# constant key names and values used throughout the code
class TokenConstants:
BEARER_TYPE = "Bearer"
MSAL_TOKEN_TYPE = "token_type"
MSAL_ACCESS_TOKEN = "access_token"
MSAL_ERROR = "error"
MSAL_ERROR_DESCRIPTION = "error_description"
MSAL_PRIVATE_CERT = "private_key"
MSAL_THUMBPRINT = "thumbprint"
MSAL_PUBLIC_CERT = "public_certificate"
MSAL_DEVICE_MSG = "message"
MSAL_DEVICE_URI = "verification_uri"
MSAL_INTERACTIVE_PROMPT = "select_account"
AZ_TOKEN_TYPE = "tokenType"
AZ_ACCESS_TOKEN = "accessToken"
class TokenProviderBase(abc.ABC):
"""
This base class abstracts token acquisition for all implementations.
The class is build for Lazy initialization, so that the first call, take on instantiation of 'heavy' long-lived class members
"""
_initialized: bool = False
_resources_initialized: bool = False
def __init__(self, is_async: bool = False):
self._proxy_dict: Optional[str, str] = None
self._session: Optional[requests.Session] = None
self.is_async = is_async
if is_async:
self._async_lock = asyncio.Lock()
else:
self._lock = Lock()
def close(self):
pass
async def close_async(self):
pass
def _init_once(self, init_only_resources=False):
if self._initialized:
return
with self._lock:
if self._initialized:
return
if not self._resources_initialized:
self._init_resources()
self._resources_initialized = True
if init_only_resources:
return
self._init_impl()
self._initialized = True
async def _init_once_async(self, init_only_resources=False):
if self._initialized:
return
async with self._async_lock:
if self._initialized:
return
if not self._resources_initialized:
await sync_to_async(self._init_resources)()
self._resources_initialized = True
if init_only_resources:
return
self._init_impl()
self._initialized = True
def _init_resources(self):
pass
def get_token(self):
"""Get a token silently from cache or authenticate if cached token is not found"""
@distributed_trace(name_of_span=f"{self.name()}.get_token", tracing_attributes=self.context(), kind=SpanKind.CLIENT)
def _get_token():
if self.is_async:
raise KustoAsyncUsageError("get_token", self.is_async)
self._init_once()
token = self._get_token_from_cache_impl()
if token is None:
with self._lock:
token = MonitoredActivity.invoke(self._get_token_impl, name_of_span=f"{self.name()}.get_token_impl", tracing_attributes=self.context())
return self._valid_token_or_throw(token)
return _get_token()
def context(self) -> dict:
if self.is_async:
raise KustoAsyncUsageError("context", self.is_async)
self._init_once(init_only_resources=True)
return self._context_impl()
async def context_async(self) -> dict:
if not self.is_async:
raise KustoAsyncUsageError("context_async", self.is_async)
await self._init_once_async(init_only_resources=True)
return self._context_impl()
async def get_token_async(self):
"""Get a token asynchronously silently from cache or authenticate if cached token is not found"""
context = await self.context_async()
@distributed_trace_async(name_of_span=f"{self.name()}.get_token_async", tracing_attributes=context, kind=SpanKind.CLIENT)
async def _get_token_async():
if not self.is_async:
raise KustoAsyncUsageError("get_token_async", self.is_async)
await self._init_once_async()
token = self._get_token_from_cache_impl()
if token is None:
async with self._async_lock:
token = await MonitoredActivity.invoke_async(
self._get_token_impl_async, name_of_span=f"{self.name()}.get_token_impl_async", tracing_attributes=context
)
return self._valid_token_or_throw(token)
return await _get_token_async()
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.close()
async def __aenter__(self):
return self
async def __aexit__(self, exc_type, exc_val, exc_tb):
await self.close_async()
@staticmethod
@abc.abstractmethod
def name() -> str:
"""return the provider class name"""
pass
@abc.abstractmethod
def _context_impl(self) -> dict:
"""return a secret-free context for error reporting"""
pass
@abc.abstractmethod
def _init_impl(self):
"""Implement any "heavy" first time initializations here"""
pass
@abc.abstractmethod
def _get_token_impl(self) -> Optional[dict]:
"""implement actual token acquisition here"""
pass
async def _get_token_impl_async(self) -> Optional[dict]:
"""implement actual token acquisition here"""
return await sync_to_async(self._get_token_impl)()
@abc.abstractmethod
def _get_token_from_cache_impl(self) -> Optional[dict]:
"""Implement cache checks here, return None if cache check fails"""
pass
@staticmethod
def _valid_token_or_none(token: dict) -> Optional[dict]:
if token is None or TokenConstants.MSAL_ERROR in token:
return None
return token
def _valid_token_or_throw(self, token: dict, context: str = "") -> dict:
if token is None:
raise KustoClientError(self.name() + " - failed to obtain a token. " + context)
if TokenConstants.MSAL_ERROR in token:
message = self.name() + " - failed to obtain a token. " + context + "\n" + token[TokenConstants.MSAL_ERROR]
if TokenConstants.MSAL_ERROR_DESCRIPTION in token:
message = message + "\n" + token[TokenConstants.MSAL_ERROR_DESCRIPTION]
raise KustoClientError(message)
return token
def set_proxy(self, proxy_url: str):
self._proxy_dict = {"http": proxy_url, "https": proxy_url}
def set_session(self, session: requests.Session):
self._session = session
class CloudInfoTokenProvider(TokenProviderBase, abc.ABC):
_cloud_info: Optional[CloudInfo]
_scopes = List[str]
_kusto_uri: str
def __init__(self, kusto_uri: str, is_async: bool = False):
super().__init__(is_async)
self._kusto_uri = kusto_uri
def _init_resources(self):
if self._kusto_uri is not None:
self._cloud_info = CloudSettings.get_cloud_info_for_cluster(self._kusto_uri, self._proxy_dict, self._session)
resource_uri = self._cloud_info.kusto_service_resource_id
if self._cloud_info.login_mfa_required:
resource_uri = resource_uri.replace(".kusto.", ".kustomfa.")
self._scopes = [resource_uri + "/.default"]
class BasicTokenProvider(TokenProviderBase):
"""Basic Token Provider keeps and returns a token received on construction"""
def __init__(self, token: str, is_async: bool = False):
super().__init__(is_async)
self._token = token
@staticmethod
def name() -> str:
return "BasicTokenProvider"
def _context_impl(self) -> dict:
return {"authority": self.name()}
def _init_impl(self):
pass
def _get_token_impl(self) -> Optional[dict]:
return None
def _get_token_from_cache_impl(self) -> dict:
return {TokenConstants.MSAL_TOKEN_TYPE: TokenConstants.BEARER_TYPE, TokenConstants.MSAL_ACCESS_TOKEN: self._token}
class CallbackTokenProvider(TokenProviderBase):
"""Callback Token Provider generates a token based on a callback function provided by the caller"""
def __init__(
self, token_callback: Optional[Callable[[], str]], async_token_callback: Optional[Callable[[], Coroutine[None, None, str]]], is_async: bool = False
):
super().__init__(is_async)
self._token_callback = token_callback
self._async_token_callback = async_token_callback
@staticmethod
def name() -> str:
return "CallbackTokenProvider"
def _context_impl(self) -> dict:
return {"authority": self.name()}
def _init_impl(self):
pass
@staticmethod
def _build_response(caller_token) -> dict:
if not isinstance(caller_token, str):
raise KustoClientError("Token provider returned something that is not a string [" + str(type(caller_token)) + "]")
return {TokenConstants.MSAL_TOKEN_TYPE: TokenConstants.BEARER_TYPE, TokenConstants.MSAL_ACCESS_TOKEN: caller_token}
def _get_token_impl(self) -> Optional[dict]:
if self._token_callback is None:
raise KustoClientError("token_callback is None, can't retrieve token")
return self._build_response(self._token_callback())
async def _get_token_impl_async(self) -> Optional[dict]:
if self._async_token_callback is None:
return await super()._get_token_impl_async()
return self._build_response(await self._async_token_callback())
def _get_token_from_cache_impl(self) -> Optional[dict]:
return None
class MsiTokenProvider(CloudInfoTokenProvider):
"""
MSI Token Provider obtains a token from the MSI endpoint
The args parameter is a dictionary conforming with the ManagedIdentityCredential initializer API arguments
"""
def __init__(self, kusto_uri: str, msi_args: dict = None, is_async: bool = False):
super().__init__(kusto_uri, is_async)
self._msi_args: dict = msi_args
self._msi_auth_context: Optional[ManagedIdentityCredential] = None
self._msi_auth_context_async: Optional[AsyncManagedIdentityCredential] = None
@staticmethod
def name() -> str:
return "MsiTokenProvider"
def _context_impl(self) -> dict:
context = self._msi_args.copy()
context["authority"] = self.name()
return context
def _init_impl(self):
pass
def _get_token_impl(self) -> Optional[dict]:
try:
if self._msi_auth_context is None:
self._msi_auth_context = ManagedIdentityCredential(**self._msi_args)
msi_token = self._msi_auth_context.get_token(self._scopes[0])
return {TokenConstants.MSAL_TOKEN_TYPE: TokenConstants.BEARER_TYPE, TokenConstants.MSAL_ACCESS_TOKEN: msi_token.token}
except ClientAuthenticationError as e:
raise KustoClientError("Failed to initialize MSI ManagedIdentityCredential with [{0}]\n{1}".format(self._msi_args, e))
except Exception as e:
raise KustoClientError("Failed to obtain MSI token for '{0}' with [{1}]\n{2}".format(self._kusto_uri, self._msi_args, e))
async def _get_token_impl_async(self) -> Optional[dict]:
try:
if self._msi_auth_context_async is None:
self._msi_auth_context_async = AsyncManagedIdentityCredential(**self._msi_args)
msi_token = await self._msi_auth_context_async.get_token(self._scopes[0])
return {TokenConstants.MSAL_TOKEN_TYPE: TokenConstants.BEARER_TYPE, TokenConstants.MSAL_ACCESS_TOKEN: msi_token.token}
except ClientAuthenticationError as e:
raise KustoClientError("Failed to initialize MSI async ManagedIdentityCredential with [{0}]\n{1}".format(self._msi_args, e))
except Exception as e:
raise KustoClientError("Failed to obtain MSI token for '{0}' with [{1}]\n{2}".format(self._kusto_uri, self._msi_args, e))
def _get_token_from_cache_impl(self) -> Optional[dict]:
return None
def close(self):
if self._msi_auth_context is not None:
self._msi_auth_context.close()
if self._msi_auth_context_async is not None:
raise KustoAsyncUsageError("Can't close async token provider with sync close", self.is_async)
async def close_async(self):
if self._msi_auth_context is not None:
await sync_to_async(self._msi_auth_context.close())
if self._msi_auth_context_async is not None:
await self._msi_auth_context_async.close()
class AzCliTokenProvider(CloudInfoTokenProvider):
"""AzCli Token Provider obtains a refresh token from the AzCli cache and uses it to authenticate with MSAL"""
def __init__(self, kusto_uri: str, is_async: bool = False):
super().__init__(kusto_uri, is_async)
self._az_auth_context = None
self._az_auth_context_async = None
self._az_token = None
@staticmethod
def name() -> str:
return "AzCliTokenProvider"
def _context_impl(self) -> dict:
return {"authority:": self.name()}
def _init_impl(self):
pass
def _get_token_impl(self) -> Optional[dict]:
try:
if self._az_auth_context is None:
self._az_auth_context = AzureCliCredential()
self._az_token = self._az_auth_context.get_token(self._scopes[0])
return {TokenConstants.AZ_TOKEN_TYPE: TokenConstants.BEARER_TYPE, TokenConstants.AZ_ACCESS_TOKEN: self._az_token.token}
except Exception as e:
raise KustoClientError(
"Failed to obtain Az Cli token for '{0}'.\nPlease be sure AzCli version 2.3.0 and above is intalled.\n{1}".format(self._kusto_uri, e)
)
async def _get_token_impl_async(self) -> Optional[dict]:
try:
if self._az_auth_context_async is None:
self._az_auth_context_async = AsyncAzureCliCredential()
self._az_token = await self._az_auth_context_async.get_token(self._scopes[0])
return {TokenConstants.AZ_TOKEN_TYPE: TokenConstants.BEARER_TYPE, TokenConstants.AZ_ACCESS_TOKEN: self._az_token.token}
except Exception as e:
raise KustoClientError(
"Failed to obtain Az Cli token for '{0}'.\nPlease be sure AzCli version 2.3.0 and above is installed.\n{1}".format(self._kusto_uri, e)
)
def _get_token_from_cache_impl(self) -> Optional[dict]:
if self._az_token is not None:
# A token is considered valid if it is due to expire in no less than 10 minutes
cur_time = time.time()
if (self._az_token.expires_on - 600) > cur_time:
return {TokenConstants.MSAL_TOKEN_TYPE: TokenConstants.BEARER_TYPE, TokenConstants.MSAL_ACCESS_TOKEN: self._az_token.token}
return None
def close(self):
if self._az_auth_context is not None:
self._az_auth_context.close()
if self._az_auth_context_async is not None:
raise KustoAsyncUsageError("Can't close async token provider with sync close", self.is_async)
async def close_async(self):
if self._az_auth_context is not None:
await sync_to_async(self._az_auth_context.close())
if self._az_auth_context_async is not None:
await self._az_auth_context_async.close()
class UserPassTokenProvider(CloudInfoTokenProvider):
"""Acquire a token from MSAL with username and password"""
def __init__(self, kusto_uri: str, authority_id: str, username: str, password: str, is_async: bool = False):
super().__init__(kusto_uri, is_async)
self._msal_client = None
self._auth = authority_id
self._user = username
self._pass = password
@staticmethod
def name() -> str:
return "UserPassTokenProvider"
def _context_impl(self) -> dict:
return {"authority": self._cloud_info.authority_uri(self._auth), "client_id": self._cloud_info.kusto_client_app_id, "username": self._user}
def _init_impl(self):
self._msal_client = PublicClientApplication(
client_id=self._cloud_info.kusto_client_app_id, authority=self._cloud_info.authority_uri(self._auth), proxies=self._proxy_dict
)
def _get_token_impl(self) -> Optional[dict]:
token = self._msal_client.acquire_token_by_username_password(username=self._user, password=self._pass, scopes=self._scopes)
return self._valid_token_or_throw(token)
def _get_token_from_cache_impl(self) -> dict:
account = None
if self._user is not None:
accounts = self._msal_client.get_accounts(self._user)
if len(accounts) > 0:
account = accounts[0]
token = self._msal_client.acquire_token_silent(scopes=self._scopes, account=account)
return self._valid_token_or_none(token)
class InteractiveLoginTokenProvider(CloudInfoTokenProvider):
"""Acquire a token from MSAL with Device Login flow"""
def __init__(
self,
kusto_uri: str,
authority_id: str,
login_hint: Optional[str] = None,
domain_hint: Optional[str] = None,
is_async: bool = False,
):
super().__init__(kusto_uri, is_async)
self._msal_client = None
self._auth = authority_id
self._login_hint = login_hint
self._domain_hint = domain_hint
self._account = None
@staticmethod
def name() -> str:
return "InteractiveLoginTokenProvider"
def _context_impl(self) -> dict:
return {"authority": self._cloud_info.authority_uri(self._auth), "client_id": self._cloud_info.kusto_client_app_id}
def _init_impl(self):
self._msal_client = PublicClientApplication(
client_id=self._cloud_info.kusto_client_app_id, authority=self._cloud_info.authority_uri(self._auth), proxies=self._proxy_dict
)
def _get_token_impl(self) -> Optional[dict]:
token = self._msal_client.acquire_token_interactive(
scopes=self._scopes, prompt=TokenConstants.MSAL_INTERACTIVE_PROMPT, login_hint=self._login_hint, domain_hint=self._domain_hint
)
return self._valid_token_or_throw(token)
def _get_token_from_cache_impl(self) -> dict:
account = None
accounts = self._msal_client.get_accounts(self._login_hint)
if len(accounts) > 0:
account = accounts[0]
token = self._msal_client.acquire_token_silent(scopes=self._scopes, account=account)
return self._valid_token_or_none(token)
class ApplicationKeyTokenProvider(CloudInfoTokenProvider):
"""Acquire a token from MSAL with application Id and Key"""
def __init__(self, kusto_uri: str, authority_id: str, app_client_id: str, app_key: str, is_async: bool = False):
super().__init__(kusto_uri, is_async)
self._msal_client = None
self._app_client_id = app_client_id
self._app_key = app_key
self._auth = authority_id
@staticmethod
def name() -> str:
return "ApplicationKeyTokenProvider"
def _context_impl(self) -> dict:
return {"authority": self._cloud_info.authority_uri(self._auth), "client_id": self._app_client_id}
def _init_impl(self):
self._msal_client = ConfidentialClientApplication(
client_id=self._app_client_id, client_credential=self._app_key, authority=self._cloud_info.authority_uri(self._auth), proxies=self._proxy_dict
)
def _get_token_impl(self) -> Optional[dict]:
token = self._msal_client.acquire_token_for_client(scopes=self._scopes)
return self._valid_token_or_throw(token)
def _get_token_from_cache_impl(self) -> None:
return None
class ApplicationCertificateTokenProvider(CloudInfoTokenProvider):
"""
Acquire a token from MSAL using application certificate
Passing the public certificate is optional and will result in Subject Name & Issuer Authentication
"""
def __init__(
self,
kusto_uri: str,
client_id: str,
authority_id: str,
private_cert: str,
thumbprint: str,
public_cert: str = None,
is_async: bool = False,
):
super().__init__(kusto_uri, is_async)
self._msal_client = None
self._auth = authority_id
self._client_id = client_id
self._cert_credentials = {TokenConstants.MSAL_PRIVATE_CERT: private_cert, TokenConstants.MSAL_THUMBPRINT: thumbprint}
if public_cert is not None:
self._cert_credentials[TokenConstants.MSAL_PUBLIC_CERT] = public_cert
@staticmethod
def name() -> str:
return "ApplicationCertificateTokenProvider"
def _context_impl(self) -> dict:
return {
"authority": self._cloud_info.authority_uri(self._auth),
"client_id": self._client_id,
"thumbprint": self._cert_credentials[TokenConstants.MSAL_THUMBPRINT],
}
def _init_impl(self):
self._msal_client = ConfidentialClientApplication(
client_id=self._client_id, client_credential=self._cert_credentials, authority=self._cloud_info.authority_uri(self._auth), proxies=self._proxy_dict
)
def _get_token_impl(self) -> Optional[dict]:
token = self._msal_client.acquire_token_for_client(scopes=self._scopes)
return self._valid_token_or_throw(token)
def _get_token_from_cache_impl(self) -> None:
return None
class AzureIdentityTokenCredentialProvider(CloudInfoTokenProvider):
"""Acquire a token using an Azure Identity credential"""
def __init__(
self,
kusto_uri: str,
is_async: bool = False,
credential: Optional[Any] = None,
credential_from_login_endpoint: Optional[Callable[[str], Any]] = None,
):
super().__init__(kusto_uri, is_async)
self.credential = credential
self.credential_from_login_endpoint = credential_from_login_endpoint
if self.credential is None and self.credential_from_login_endpoint is None:
raise KustoClientError("Either a credential or a credential_from_login_endpoint must be provided")
@staticmethod
def name() -> str:
return "AzureIdentityTokenProvider"
def _context_impl(self) -> dict:
return {"credential": self.credential}
def _init_impl(self):
if self.credential is None:
self.credential = self.credential_from_login_endpoint(self._cloud_info.login_endpoint)
def _get_token_impl(self) -> Optional[dict]:
t = self.credential.get_token(self._scopes[0])
return {TokenConstants.MSAL_TOKEN_TYPE: TokenConstants.BEARER_TYPE, TokenConstants.MSAL_ACCESS_TOKEN: t.token}
async def _get_token_impl_async(self) -> Optional[dict]:
# check if get_token is async
if inspect.iscoroutinefunction(self.credential.get_token):
t = await self.credential.get_token(self._scopes[0])
else:
t = await sync_to_async(self.credential.get_token)(self._scopes[0])
return {TokenConstants.MSAL_TOKEN_TYPE: TokenConstants.BEARER_TYPE, TokenConstants.MSAL_ACCESS_TOKEN: t.token}
def _get_token_from_cache_impl(self) -> Optional[dict]:
return None
def close(self):
if self.credential is not None:
if inspect.iscoroutinefunction(self.credential.close):
raise KustoAsyncUsageError("Can't close async token provider with sync close", self.is_async)
else:
self.credential.close()
self.credential = None
self.credential_from_login_endpoint = None
async def close_async(self):
if self.credential is not None:
if inspect.iscoroutinefunction(self.credential.close):
await self.credential.close()
else:
await sync_to_async(self.credential.close)()
self.credential = None
self.credential_from_login_endpoint = None
class DeviceLoginTokenProvider(AzureIdentityTokenCredentialProvider):
"""Acquire a token from MSAL with Device Login flow"""
def __init__(self, kusto_uri: str, authority_id: str, device_code_callback: DeviceCallbackType = None, is_async: bool = False):
self._msal_client = None
self._auth = authority_id
self._account = None
self._device_code_callback = device_code_callback
def credential_from_login_endpoint(endpoint: str):
cred = DeviceCodeCredential(
authority=endpoint,
tenant_id=self._auth,
client_id=self._cloud_info.kusto_client_app_id,
prompt_callback=self._device_code_callback,
)
return cred
super().__init__(kusto_uri, is_async, credential_from_login_endpoint=credential_from_login_endpoint)
@staticmethod
def name() -> str:
return "DeviceLoginTokenProvider"
def _context_impl(self) -> dict:
return {"authority": self._cloud_info.authority_uri(self._auth), "client_id": self._cloud_info.kusto_client_app_id}
|