Skip to content

Commit 9dac487

Browse files
authored
Add DefaultAzureCredential auth for ADX service (#33627)
1 parent 6130993 commit 9dac487

File tree

2 files changed

+38
-1
lines changed
  • airflow/providers/microsoft/azure/hooks
  • tests/providers/microsoft/azure/hooks

2 files changed

+38
-1
lines changed

airflow/providers/microsoft/azure/hooks/adx.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import warnings
2929
from typing import Any
3030

31+
from azure.identity import DefaultAzureCredential
3132
from azure.kusto.data import ClientRequestProperties, KustoClient, KustoConnectionStringBuilder
3233
from azure.kusto.data.exceptions import KustoServiceError
3334
from azure.kusto.data.response import KustoResponseDataSetV2
@@ -105,7 +106,7 @@ def get_ui_field_behaviour() -> dict[str, Any]:
105106
"placeholders": {
106107
"login": "Varies with authentication method",
107108
"password": "Varies with authentication method",
108-
"auth_method": "AAD_APP/AAD_APP_CERT/AAD_CREDS/AAD_DEVICE",
109+
"auth_method": "AAD_APP/AAD_APP_CERT/AAD_CREDS/AAD_DEVICE/AZURE_TOKEN_CRED",
109110
"tenant": "Used with AAD_APP/AAD_APP_CERT/AAD_CREDS",
110111
"certificate": "Used with AAD_APP_CERT",
111112
"thumbprint": "Used with AAD_APP_CERT",
@@ -183,6 +184,11 @@ def get_required_param(name: str) -> str:
183184
)
184185
elif auth_method == "AAD_DEVICE":
185186
kcsb = KustoConnectionStringBuilder.with_aad_device_authentication(cluster)
187+
elif auth_method == "AZURE_TOKEN_CRED":
188+
kcsb = KustoConnectionStringBuilder.with_azure_token_credential(
189+
connection_string=cluster,
190+
credential=DefaultAzureCredential(),
191+
)
186192
else:
187193
raise AirflowException(f"Unknown authentication method: {auth_method}")
188194

tests/providers/microsoft/azure/hooks/test_adx.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,37 @@ def test_conn_method_aad_creds(self, mock_init):
110110
)
111111
)
112112

113+
@mock.patch("azure.identity._credentials.environment.ClientSecretCredential")
114+
def test_conn_method_token_creds(self, mock1):
115+
db.merge_conn(
116+
Connection(
117+
conn_id=ADX_TEST_CONN_ID,
118+
conn_type="azure_data_explorer",
119+
host="https://help.kusto.windows.net",
120+
extra=json.dumps(
121+
{
122+
"auth_method": "AZURE_TOKEN_CRED",
123+
}
124+
),
125+
)
126+
)
127+
with patch.dict(
128+
in_dict=os.environ,
129+
values={
130+
"AZURE_TENANT_ID": "tenant",
131+
"AZURE_CLIENT_ID": "client",
132+
"AZURE_CLIENT_SECRET": "secret",
133+
},
134+
):
135+
hook = AzureDataExplorerHook(azure_data_explorer_conn_id=ADX_TEST_CONN_ID)
136+
assert hook.connection._kcsb.data_source == "https://help.kusto.windows.net"
137+
mock1.assert_called_once_with(
138+
tenant_id="tenant",
139+
client_id="client",
140+
client_secret="secret",
141+
authority="https://login.microsoftonline.com",
142+
)
143+
113144
@mock.patch.object(KustoClient, "__init__")
114145
def test_conn_method_aad_app(self, mock_init):
115146
mock_init.return_value = None

0 commit comments

Comments
 (0)