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
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:
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.Add your application to access the database like below:
Once you have added you can use the below code with
.with_aad_application_key_authentication
in the Ubuntu server.Code:
Output: