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)
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. kcsb = KustoConnectionStringBuilder() # Managed Identity - automatically injected into your machine by azure when running on an azure service. # It's the best way for any code that does such - it's automatic, and requires no saving of secrets. # 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)