def test_aad_user(self): """Checks kcsb that is created with AAD user credentials.""" user = "******" password = "******" kcsbs = [ KustoConnectionStringBuilder( "localhost;AAD User ID={0};password={1} ;AAD Federated Security=True " .format(user, password)), KustoConnectionStringBuilder( "Data Source=localhost ; AaD User ID={0}; Password ={1} ;AAD Federated Security=True" .format(user, password)), KustoConnectionStringBuilder( " Addr = localhost ; AAD User ID = {0} ; Pwd ={1} ;AAD Federated Security=True" .format(user, password)), KustoConnectionStringBuilder( "Network Address = localhost; AAD User iD = {0} ; Pwd = {1} ;AAD Federated Security= True " .format(user, password)), KustoConnectionStringBuilder.with_aad_user_password_authentication( "localhost", user, password), ] kcsb1 = KustoConnectionStringBuilder("Server=localhost") kcsb1[KustoConnectionStringBuilder.ValidKeywords.aad_user_id] = user kcsb1[KustoConnectionStringBuilder.ValidKeywords.password] = password kcsb1[KustoConnectionStringBuilder.ValidKeywords. aad_federated_security] = True kcsbs.append(kcsb1) kcsb2 = KustoConnectionStringBuilder("server=localhost") kcsb2["AAD User ID"] = user kcsb2["Password"] = password kcsb2["aad federated security"] = True kcsbs.append(kcsb2) for kcsb in kcsbs: assert kcsb.data_source == "localhost" assert kcsb.aad_federated_security assert kcsb.aad_user_id == user assert kcsb.password == password assert kcsb.application_client_id is None assert kcsb.application_key is None assert kcsb.authority_id == "common" assert repr( kcsb ) == "Data Source=localhost;AAD Federated Security=True;AAD User ID={0};Password={1};Authority Id=common".format( user, password) assert str( kcsb ) == "Data Source=localhost;AAD Federated Security=True;AAD User ID={0};Password={1};Authority Id=common".format( user, self.PASSWORDS_REPLACEMENT)
def test_unauthorized_exception(): """Test the exception thrown when authorization fails.""" cluster = "https://somecluster.kusto.windows.net" username = "******" kcsb = KustoConnectionStringBuilder.with_aad_user_password_authentication(cluster, username, "StrongestPasswordEver", "authorityName") aad_helper = _AadHelper(kcsb) try: aad_helper.acquire_authorization_header() except KustoAuthenticationError as error: assert error.authentication_method == AuthenticationMethod.aad_username_password.value assert error.authority == "https://login.microsoftonline.com/authorityName" assert error.kusto_cluster == cluster assert error.kwargs["username"] == username
def test_unauthorized_exception(): """Test the exception thrown when authorization fails.""" cluster = "https://somecluster.kusto.windows.net" username = "******" kcsb = KustoConnectionStringBuilder.with_aad_user_password_authentication( cluster, username, "StrongestPasswordEver", "authorityName") aad_helper = _AadHelper(kcsb, False) aad_helper.token_provider._init_resources() try: aad_helper.acquire_authorization_header() except KustoAuthenticationError as error: assert error.authentication_method == UserPassTokenProvider.name() assert error.authority == "https://login.microsoftonline.com/authorityName" assert error.kusto_cluster == cluster assert error.kwargs["username"] == username assert error.kwargs[ "client_id"] == CloudSettings.DEFAULT_CLOUD.kusto_client_app_id
def proxy_kcsb(request) -> Tuple[KustoConnectionStringBuilder, bool]: cluster = KustoClientTestsMixin.HOST user = "******" password = "******" authority_id = "13456" uuid = "11111111-1111-1111-1111-111111111111" key = "key of application" token = "The app hardest token ever" return { "user_password": (KustoConnectionStringBuilder.with_aad_user_password_authentication( cluster, user, password, authority_id), True), "application_key": (KustoConnectionStringBuilder.with_aad_application_key_authentication( cluster, uuid, key, "microsoft.com"), True), "application_token": (KustoConnectionStringBuilder. with_aad_application_token_authentication( cluster, application_token=token), False), "device": (KustoConnectionStringBuilder.with_aad_device_authentication(cluster), True), "user_token": (KustoConnectionStringBuilder.with_aad_user_token_authentication( cluster, user_token=token), False), "managed_identity": (KustoConnectionStringBuilder. with_aad_managed_service_identity_authentication(cluster), False), "token_provider": (KustoConnectionStringBuilder.with_token_provider( cluster, lambda x: x), False), "async_token_provider": (KustoConnectionStringBuilder.with_async_token_provider( cluster, lambda x: x), False), "az_cli": (KustoConnectionStringBuilder.with_az_cli_authentication(cluster), True), "interactive_login": (KustoConnectionStringBuilder.with_interactive_login(cluster), True), }[request.param]
def test_aad_user_with_authority(self): """Checks kcsb that is created with AAD user credentials.""" user = "******" password = "******" authority_id = "13456" kcsb = KustoConnectionStringBuilder.with_aad_user_password_authentication( "localhost", user, password, authority_id) assert kcsb.data_source == "localhost" assert kcsb.aad_federated_security assert kcsb.aad_user_id == user assert kcsb.password == password assert kcsb.application_client_id is None assert kcsb.application_key is None assert kcsb.authority_id == authority_id assert repr( kcsb ) == "Data Source=localhost;AAD Federated Security=True;AAD User ID={0};Password={1};Authority Id=13456".format( user, password) assert str( kcsb ) == "Data Source=localhost;AAD Federated Security=True;AAD User ID={0};Password={1};Authority Id=13456".format( user, self.PASSWORDS_REPLACEMENT)
# In case you want to authenticate with a System Assigned Managed Service Identity (MSI) kcsb = KustoConnectionStringBuilder.with_aad_managed_service_identity_authentication(cluster) # In case you want to authenticate with a User Assigned Managed Service Identity (MSI) user_assigned_client_id = "the AAD identity client id" kcsb = KustoConnectionStringBuilder.with_aad_managed_service_identity_authentication(cluster, client_id=user_assigned_client_id) # In case you want to authenticate with Azure CLI. # Users are required to be in a logged in state in az-cli, for this authentication method to succeed. Run `az login` to login to azure cli. kcsb = KustoConnectionStringBuilder.with_az_cli_authentication(cluster) # In case you want to authenticate with AAD username and password username = "******" password = "******" kcsb = KustoConnectionStringBuilder.with_aad_user_password_authentication(cluster, username, password, authority_id) # In case you want to authenticate with AAD device code. # Please note that if you choose this option, you'll need to authenticate for every new instance that is initialized. # It is highly recommended to create one instance and use it for all of your queries. kcsb = KustoConnectionStringBuilder.with_aad_device_authentication(cluster) # The authentication method will be taken from the chosen KustoConnectionStringBuilder. client = KustoClient(kcsb) ###################################################### ## QUERY ## ###################################################### # once authenticated, usage is as following db = "Samples"