Ejemplo n.º 1
0
 def _get_client_for_cluster(cluster: str) -> KustoClient:
     # If we call 'with_az_cli_authentication' directly, in case of failure we will get an un-informative exception.
     # As a workaround, we first attempt to manually get the Azure CLI token, and see if it works.
     # Get rid of this workaround once this is resolved: https://github.com/Azure/azure-kusto-python/issues/240
     stored_token = _get_azure_cli_auth_token()
     if stored_token is None:
         _logger.info("Failed to get Azure CLI token, falling back to AAD device authentication")
         connection_string_builder = KustoConnectionStringBuilder.with_aad_device_authentication(cluster)
     else:
         connection_string_builder = KustoConnectionStringBuilder.with_az_cli_authentication(cluster)
     return KustoClient(connection_string_builder)
 def test_aad_device_login(self):
     """Checks kcsb that is created with AAD device login."""
     kcsb = KustoConnectionStringBuilder.with_aad_device_authentication(
         "localhost")
     assert kcsb.data_source == "localhost"
     assert kcsb.aad_federated_security
     assert kcsb.aad_user_id is None
     assert kcsb.password is None
     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;Authority Id=common"
     assert str(
         kcsb
     ) == "Data Source=localhost;AAD Federated Security=True;Authority Id=common"
Ejemplo n.º 3
0
def get_kusto_client(server: str, database: str) -> KustoClient:
    """
    Helper to get an authenticated KustoClient.
    Try to use Az CLI cached credentials, fall back to device code auth.
    :param server: The (short) name of a Kusto server cluster, not the full URI
    :param database: The name of the initial catalog to connect to
    """
    logger = logging.getLogger(__name__)
    server_uri = f"https://{server}.kusto.windows.net"
    try:
        kcsb = KustoConnectionStringBuilder.with_az_cli_authentication(
            server_uri)
        client = KustoClient(kcsb)
        # hit the server to force authentication
        client.execute_query(database, "print('hi')")
        return client
    except KustoAuthenticationError:
        kcsb = KustoConnectionStringBuilder.with_aad_device_authentication(
            server_uri)
        client = KustoClient(kcsb)
        client.execute_query(database, "print('hi')")
        return client
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]
Ejemplo n.º 5
0
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"
query = "StormEvents | take 10"

response = client.execute(db, query)

# iterating over rows is possible
Ejemplo n.º 6
0
from azure.kusto.data import KustoClient, KustoConnectionStringBuilder
from azure.kusto.data.exceptions import KustoServiceError
from azure.kusto.data.helpers import dataframe_from_result_table
import pandas as pd

AAD_TENANT_ID = "72f988bf-86f1-41af-91ab-2d7cd011db47"
KUSTO_CLUSTER = "https://help.kusto.windows.net/"
KUSTO_DATABASE = "Samples"

KCSB = KustoConnectionStringBuilder.with_aad_device_authentication(
    KUSTO_CLUSTER)
KCSB.authority_id = AAD_TENANT_ID

KUSTO_CLIENT = KustoClient(KCSB)
KUSTO_QUERY = "StormEvents | project StartTime,State, EventType| sort by StartTime desc | take 10"

try:
    RESPONSE = KUSTO_CLIENT.execute(KUSTO_DATABASE, KUSTO_QUERY)
except KustoServiceError as error:
    print("Error : ", error)

#print(RESPONSE.primary_results[0])
df = dataframe_from_result_table(RESPONSE.primary_results[0])
jsonData = df.to_json()
print(jsonData)