def create_application_certificate_connection_string( cls, cluster_url: str) -> KustoConnectionStringBuilder: """ Generates Kusto Connection String based on 'AppCertificate' Authentication Mode. :param cluster_url: Url of cluster to connect to :return: AppCertificate Kusto Connection String """ # TODO (config - optional): App ID & tenant, path to public certificate and path to private certificate pem file to authenticate with app_id = os.environ.get("APP_ID") app_tenant = os.environ.get("APP_TENANT") private_key_pem_file_path = os.environ.get( "PRIVATE_KEY_PEM_FILE_PATH") cert_thumbprint = os.environ.get("CERT_THUMBPRINT") public_cert_file_path = os.environ.get( "PUBLIC_CERT_FILE_PATH" ) # Only used for "Subject Name and Issuer" auth public_certificate = None pem_certificate = None try: with open(private_key_pem_file_path, "r") as pem_file: pem_certificate = pem_file.read() except Exception as ex: Utils.error_handler( f"Failed to load PEM file from {private_key_pem_file_path}", ex) if public_cert_file_path: try: with open(public_cert_file_path, "r") as cert_file: public_certificate = cert_file.read() except Exception as ex: Utils.error_handler( f"Failed to load public certificate file from {public_cert_file_path}", ex) return KustoConnectionStringBuilder.with_aad_application_certificate_sni_authentication( cluster_url, app_id, pem_certificate, public_certificate, cert_thumbprint, app_tenant) else: return KustoConnectionStringBuilder.with_aad_application_certificate_authentication( cluster_url, app_id, pem_certificate, cert_thumbprint, app_tenant)
# 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>" # 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 application certificate Subject Name & Issuer filename = "path to a PEM certificate" with open(filename, "r") as pem_file: PEM = pem_file.read() filename = "path to a public certificate" with open(filename, "r") as cert_file: public_certificate = cert_file.read() thumbprint = "certificate's thumbprint" kcsb = KustoConnectionStringBuilder.with_aad_application_certificate_sni_authentication(cluster, client_id, PEM, public_certificate, thumbprint, authority_id) # No authentication - for rare cases where the cluster is defined to work without any need for auth. usually reserved for internal use.