skip to Main Content

I am using free azure cluster to query my data using Azure Data Explorer. I am running following code to upload data to azure cluster. When running locally, I can do auth using device login method. In this method, it provides me an code when running this script and I needs to put that code by going to URL https://www.microsoft.com/devicelogin and do login to my account and it works.

import json

from azure.kusto.data.data_format import DataFormat
from azure.kusto.ingest import (
    IngestionProperties,
    QueuedIngestClient
)

from azure.kusto.data import KustoConnectionStringBuilder

cluster = ""
database = ""
table = ""

kcsb = KustoConnectionStringBuilder.with_aad_device_authentication(cluster)

client = QueuedIngestClient(kcsb)

ingestion_props = IngestionProperties(
    database=database,
    table=table,
    data_format=DataFormat.JSON,
    ingestion_mapping_reference= ""
)

def upload_to_azure_cluster(json_data):
    with open("managedDevices.tmp", "w") as file:
        file.write(json.dumps(json_data) + "n")

    client.ingest_from_file("managedDevices.tmp", ingestion_properties=ingestion_props)


upload_to_azure_cluster({'title' : 'abcd'})

Now I would like to run this script in background mode in my ubuntu server. When running this scrip with following command it doesn’t prompt code to do login. It gets stuck there.

nohup python3 azure_upload.py &

2

Answers


  1. You should create a Service Principal in Azure AD and use that with your background worker. This page shows multiple examples on different ways to authenticate.

    For example a certificate-based auth:

    kcsb = KustoConnectionStringBuilder.with_aad_application_certificate_authentication(cluster, client_id, PEM, thumbprint, authority_id)
    
    Login or Signup to reply.
  2. Now I would like to run this script in background mode on my Ubuntu server. When running this script with the following command it doesn’t prompt the code to do login. It gets stuck there.

    When you are running the script Ubuntu server(non-interactive server) you can use an Azure AD application and client secret to authenticate your connection.

    Your connection to Azure Data Explorer seems to be authenticated using Azure AD device authentication. On the alternative hand, the authentication flow you are using has been built for interactive use, enabling you to manually enter the code given by the device login mechanism.

    You can follow the MS-Document to create an Application for authentication.

    Create an application with the necessary API permission user_impersation to access azure data explorer and make sure to grant admin consent to your permission like below.

    enter image description here

    Add your application to access the database like below:

    enter image description here

    Once you have added you can use the below code with .with_aad_application_key_authentication in the Ubuntu server.

    Code:

    import json
    
    from azure.kusto.data.data_format import DataFormat
    from azure.kusto.ingest import (
        IngestionProperties,
        QueuedIngestClient
    )
    
    from azure.kusto.data import KustoConnectionStringBuilder
    
    cluster = ""
    database = ""
    table = ""
    
    client_id="your-application-id"
    client_secret="your-app-secret" 
    authority_id="your-tenant-id"
    
    kcsb = KustoConnectionStringBuilder.with_aad_application_key_authentication(cluster, client_id, client_secret, authority_id)
    client = QueuedIngestClient(kcsb)
    
    ingestion_props = IngestionProperties(
        database=database,
        table=table,
        data_format=DataFormat.JSON
    )
    
    def upload_to_azure_cluster(json_data):
        with open("managedDevices.tmp", "w") as file:
            file.write(json.dumps(json_data) + "n")
    
        client.ingest_from_file("managedDevices.tmp", ingestion_properties=ingestion_props)
    upload_to_azure_cluster({'title' : 'abcd'})
    print("Uploaded successfully")
    

    Output:

    Uploaded successfully
    

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search