def __init__( self, kusto_cluster, client_id=None, client_secret=None, username=None, password=None, certificate=None, certificate_thumbprint=None, authority=None, ): """ Kusto Client constructor. Parameters ---------- kusto_cluster : str Kusto cluster endpoint. Example: https://help.kusto.windows.net client_id : str The AAD application ID of the application making the request to Kusto client_secret : str The AAD application key of the application making the request to Kusto. if this is given, then username/password should not be. username : str The username of the user making the request to Kusto. if this is given, then password must follow and the client_secret should not be given. password : str The password matching the username of the user making the request to Kusto authority : 'microsoft.com', optional In case your tenant is not microsoft please use this param. """ if all([username, password]): kcsb = KustoConnectionStringBuilder.with_aad_user_password_authentication( kusto_cluster, username, password) elif all([client_id, client_secret]): kcsb = KustoConnectionStringBuilder.with_aad_application_key_authentication( kusto_cluster, client_id, client_secret) elif all([client_id, certificate, certificate_thumbprint]): kcsb = KustoConnectionStringBuilder.with_aad_application_certificate_authentication( kusto_cluster, client_id, certificate, certificate_thumbprint) else: kcsb = KustoConnectionStringBuilder.with_aad_device_authentication( kusto_cluster) if authority: kcsb.authority_id = authority self.client = KustoClient(kcsb) # replace aadhelper to use remote browser in interactive mode self.client._aad_helper = _MyAadHelper(kcsb) self.mgmt_endpoint_version = "v2" if self.client._mgmt_endpoint.endswith( "v2/rest/query") else "v1" self.query_endpoint_version = "v2" if self.client._query_endpoint.endswith( "v2/rest/query") else "v1"
def get_conn(self) -> KustoClient: """Return a KustoClient object.""" conn = self.get_connection(self.conn_id) cluster = conn.host if not cluster: raise AirflowException('Host connection option is required') def get_required_param(name: str) -> str: """Extract required parameter from extra JSON, raise exception if not found""" value = conn.extra_dejson.get(name) if not value: raise AirflowException( f'Extra connection option is missing required parameter: `{name}`' ) return value auth_method = get_required_param('auth_method') or get_required_param( 'extra__azure_data_explorer__auth_method') if auth_method == 'AAD_APP': tenant = get_required_param('tenant') or get_required_param( 'extra__azure_data_explorer__tenant') kcsb = KustoConnectionStringBuilder.with_aad_application_key_authentication( cluster, conn.login, conn.password, tenant) elif auth_method == 'AAD_APP_CERT': certificate = get_required_param( 'certificate') or get_required_param( 'extra__azure_data_explorer__certificate') thumbprint = get_required_param( 'thumbprint') or get_required_param( 'extra__azure_data_explorer__thumbprint') tenant = get_required_param('tenant') or get_required_param( 'extra__azure_data_explorer__tenant') kcsb = KustoConnectionStringBuilder.with_aad_application_certificate_authentication( cluster, conn.login, certificate, thumbprint, tenant, ) elif auth_method == 'AAD_CREDS': tenant = get_required_param('tenant') or get_required_param( 'extra__azure_data_explorer__tenant') kcsb = KustoConnectionStringBuilder.with_aad_user_password_authentication( cluster, conn.login, conn.password, tenant) elif auth_method == 'AAD_DEVICE': kcsb = KustoConnectionStringBuilder.with_aad_device_authentication( cluster) else: raise AirflowException( f'Unknown authentication method: {auth_method}') return KustoClient(kcsb)
def __init__(self, conn_kv): """ Kusto Client constructor. Parameters ---------- kusto_cluster : str Kusto cluster endpoint. Example: https://help.kusto.windows.net client_id : str The AAD application ID of the application making the request to Kusto client_secret : str The AAD application key of the application making the request to Kusto. if this is given, then username/password should not be. username : str The username of the user making the request to Kusto. if this is given, then password must follow and the client_secret should not be given. password : str The password matching the username of the user making the request to Kusto authority : 'microsoft.com', optional In case your tenant is not microsoft please use this param. """ kusto_cluster = "https://{0}.kusto.windows.net".format(conn_kv["cluster"]) if all([conn_kv.get("username"), conn_kv.get("password")]): kcsb = KustoConnectionStringBuilder.with_aad_user_password_authentication(kusto_cluster, conn_kv.get("username"), conn_kv.get("password")) if conn_kv.get("tenant") is not None: kcsb.authority_id = conn_kv.get("tenant") elif all([conn_kv.get("clientid"), conn_kv.get("clientsecret")]): kcsb = KustoConnectionStringBuilder.with_aad_application_key_authentication( kusto_cluster, conn_kv.get("clientid"), conn_kv.get("clientsecret"), conn_kv.get("tenant")) elif all([conn_kv.get("clientid"), conn_kv.get("certificate"), conn_kv.get("certificate_thumbprint")]): kcsb = KustoConnectionStringBuilder.with_aad_application_certificate_authentication( kusto_cluster, conn_kv.get("clientid"), conn_kv.get("certificate"), conn_kv.get("certificate_thumbprint", conn_kv.get("tenant")) ) else: kcsb = KustoConnectionStringBuilder.with_aad_device_authentication(kusto_cluster) if conn_kv.get("tenant") is not None: kcsb.authority_id = conn_kv.get("tenant") self.client = KustoClient(kcsb) # replace aadhelper to use remote browser in interactive mode self.client._aad_helper = _MyAadHelper(kcsb, self._DEFAULT_CLIENTID) self.mgmt_endpoint_version = "v2" if self.client._mgmt_endpoint.endswith("v2/rest/query") else "v1" self.query_endpoint_version = "v2" if self.client._query_endpoint.endswith("v2/rest/query") else "v1"
def get_conn(self) -> KustoClient: """Return a KustoClient object.""" conn = self.get_connection(self.conn_id) cluster = conn.host if not cluster: raise AirflowException('Host connection option is required') def get_required_param(name): """Extract required parameter from extra JSON, raise exception if not found""" value = conn.extra_dejson.get(name) if not value: raise AirflowException( 'Extra connection option is missing required parameter: `{}`' .format(name)) return value auth_method = get_required_param('auth_method') if auth_method == 'AAD_APP': kcsb = KustoConnectionStringBuilder.with_aad_application_key_authentication( cluster, conn.login, conn.password, get_required_param('tenant')) elif auth_method == 'AAD_APP_CERT': kcsb = KustoConnectionStringBuilder.with_aad_application_certificate_authentication( cluster, conn.login, get_required_param('certificate'), get_required_param('thumbprint'), get_required_param('tenant'), ) elif auth_method == 'AAD_CREDS': kcsb = KustoConnectionStringBuilder.with_aad_user_password_authentication( cluster, conn.login, conn.password, get_required_param('tenant')) elif auth_method == 'AAD_DEVICE': kcsb = KustoConnectionStringBuilder.with_aad_device_authentication( cluster) else: raise AirflowException( 'Unknown authentication method: {}'.format(auth_method)) return KustoClient(kcsb)
# read more at https://docs.microsoft.com/en-us/onedrive/find-your-office-365-tenant-id authority_id = "<insert here your tenant id>" kcsb = KustoConnectionStringBuilder.with_aad_application_key_authentication( cluster, client_id, client_secret, authority_id ) # In case you want to authenticate with AAD application certificate. filename = "path to a PEM certificate" with open(filename, "r") as pem_file: PEM = pem_file.read() thumbprint = "certificate's thumbprint" kcsb = KustoConnectionStringBuilder.with_aad_application_certificate_authentication( cluster, client_id, PEM, thumbprint, authority_id ) # 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 autenticate 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 = KustoIngestClient(kcsb)
KUSTO_CLUSTER = "https://help.kusto.windows.net" # In case you want to authenticate with AAD application. CLIENT_ID = "<insert here your AAD application id>" CLIENT_SECRET = "<insert here your AAD application key>" KCSB = KustoConnectionStringBuilder.with_aad_application_key_authentication( KUSTO_CLUSTER, CLIENT_ID, CLIENT_SECRET) # In case you want to authenticate with AAD application certificate. FILENAME = "path to a PEM certificate" with open(FILENAME, "r") as pem_file: PEM = pem_file.read() THUMBPRINT = "certificate's thumbprint" KCSB = KustoConnectionStringBuilder.with_aad_application_certificate_authentication( KUSTO_CLUSTER, CLIENT_ID, PEM, THUMBPRINT) KUSTO_CLIENT = KustoClient(KCSB) # In case you want to authenticate with the logged in AAD user. KUSTO_CLIENT = KustoClient(KUSTO_CLUSTER) KUSTO_DATABASE = "Samples" KUSTO_QUERY = "StormEvents | take 10" RESPONSE = KUSTO_CLIENT.execute(KUSTO_DATABASE, KUSTO_QUERY) for row in RESPONSE.primary_results[0]: print(row[0], " ", row["EventType"]) # Query is too big to be executed KUSTO_QUERY = "StormEvents"