Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions azure-kusto-data/tests/aio/test_kusto_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
from unittest.mock import patch

import pytest
from aiohttp import ClientResponse

from azure.kusto.data._cloud_settings import CloudSettings
from azure.kusto.data import KustoConnectionStringBuilder
from azure.kusto.data._decorators import aio_documented_by
from azure.kusto.data.client_request_properties import ClientRequestProperties
from azure.kusto.data.exceptions import KustoClosedError, KustoMultiApiError, KustoNetworkError
from azure.kusto.data.exceptions import KustoClosedError, KustoMultiApiError, KustoNetworkError, KustoServiceError
from azure.kusto.data.helpers import dataframe_from_result_table
from ..kusto_client_common import KustoClientTestsMixin, mocked_requests_post
from ..test_kusto_client import TestKustoClient as KustoClientTestsSync
Expand All @@ -18,6 +19,18 @@

@aio_documented_by(KustoClientTestsSync)
class TestKustoClient(KustoClientTestsMixin):
@aio_documented_by(KustoClientTestsSync.test_no_redirects)
@pytest.mark.asyncio
async def test_no_redirects(self, redirect_server: tuple[str, int, list[str]]) -> None:
cluster, code, requests_seen = redirect_server
kcsb = KustoConnectionStringBuilder.with_aad_user_token_authentication(cluster, "test-token")
async with KustoClient(kcsb) as client:
with pytest.raises(KustoServiceError) as error:
_ = await client.execute("db", "table")
assert isinstance(error.value.http_response, ClientResponse)
assert error.value.http_response.status == code
assert requests_seen == ["/v2/rest/query"]

@staticmethod
def _mock_callback(url, **kwargs):
body = json.dumps(mocked_requests_post(str(url), **kwargs).json())
Expand Down Expand Up @@ -161,7 +174,7 @@ async def test_request_id(self):

@aio_documented_by(KustoClientTestsSync.test_proxy_token_providers)
@pytest.mark.asyncio
async def test_proxy_token_providers(self, proxy_kcsb):
async def test_proxy_token_providers(self, proxy_kcsb, isolated_cloud_cache):
"""Test query V2."""
proxy = "https://my_proxy.sample"
kcsb, auth_supports_proxy = proxy_kcsb
Expand All @@ -176,7 +189,7 @@ async def test_proxy_token_providers(self, proxy_kcsb):

assert client._aad_helper.token_provider._proxy_dict == expected_dict

CloudSettings._cloud_cache.clear()
isolated_cloud_cache.clear()
with patch("requests.get", side_effect=mocked_requests_post) as mock_get:
client._aad_helper.token_provider._init_resources()

Expand Down
45 changes: 45 additions & 0 deletions azure-kusto-data/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,57 @@
import asyncio
from collections.abc import Iterator
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
from threading import Thread
from typing import Tuple

import pytest
from azure.kusto.data import KustoConnectionStringBuilder
from azure.kusto.data._cloud_settings import CloudInfo, CloudSettings

from .kusto_client_common import KustoClientTestsMixin


@pytest.fixture
def isolated_cloud_cache(monkeypatch: pytest.MonkeyPatch) -> dict[str, CloudInfo]:
cache: dict[str, CloudInfo] = {}
monkeypatch.setattr(CloudSettings, "_cloud_cache", cache)
return cache


@pytest.fixture(params=[301, 302, 307, 308])
def redirect_server(request: pytest.FixtureRequest, monkeypatch: pytest.MonkeyPatch) -> Iterator[tuple[str, int, list[str]]]:
code: int = request.param
requests_seen: list[str] = []

class RedirectHandler(BaseHTTPRequestHandler):
def do_GET(self) -> None:
self._respond()

def do_POST(self) -> None:
self._respond()

def _respond(self) -> None:
requests_seen.append(self.path)
self.send_response(200 if self.path == "/redirected" else code)
if self.path != "/redirected":
self.send_header("Location", "/redirected")
self.send_header("Content-Length", "0")
self.end_headers()

with ThreadingHTTPServer(("127.0.0.1", 0), RedirectHandler) as server:
cluster = f"http://127.0.0.1:{server.server_port}"
monkeypatch.setattr(CloudSettings, "_cloud_cache", {cluster: CloudSettings.DEFAULT_CLOUD})
monkeypatch.setenv("NO_PROXY", "127.0.0.1")
monkeypatch.setenv("no_proxy", "127.0.0.1")
thread = Thread(target=server.serve_forever, kwargs={"poll_interval": 0.05}, daemon=True)
thread.start()
try:
yield cluster, code, requests_seen
finally:
server.shutdown()
thread.join()


@pytest.fixture(
params=[
"user_password",
Expand Down
17 changes: 2 additions & 15 deletions azure-kusto-data/tests/test_cloud_settings.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,8 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License
import pytest

from azure.kusto.data._cloud_settings import CloudSettings, CloudInfo


@pytest.fixture
def clear_cache():
"""Fixture to clear the CloudSettings cache before each test"""
with CloudSettings._cloud_cache_lock:
CloudSettings._cloud_cache.clear()
yield
# Clean up after test if needed
with CloudSettings._cloud_cache_lock:
CloudSettings._cloud_cache.clear()


def test_normalize_uri_extracts_authority():
"""Test that _normalize_uri extracts only the authority part (schema, host, port) from a URI."""
# Test with various URI formats
Expand All @@ -32,7 +19,7 @@ def test_normalize_uri_extracts_authority():
assert CloudSettings._normalize_uri(input_uri) == expected_authority


def test_cloud_info_cached_by_authority(clear_cache):
def test_cloud_info_cached_by_authority(isolated_cloud_cache):
"""Test that CloudInfo is cached by authority part of the URI (schema, host, port)."""
# Create a test CloudInfo object
test_cloud_info = CloudInfo(
Expand Down Expand Up @@ -67,7 +54,7 @@ def test_cloud_info_cached_by_authority(clear_cache):
assert retrieved_info is test_cloud_info


def test_cloud_info_cached_with_port(clear_cache):
def test_cloud_info_cached_with_port(isolated_cloud_cache):
"""Test that URIs with ports are cached separately from those without."""
# Create two different CloudInfo objects
cloud_info_default = CloudInfo(
Expand Down
54 changes: 1 addition & 53 deletions azure-kusto-data/tests/test_e2e_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import asyncio
import csv
import dataclasses
import json
import os
import platform
Expand All @@ -13,15 +12,13 @@
from urllib.parse import urljoin

import pytest
from requests import Response
from azure.identity import DefaultAzureCredential
from azure.kusto.data import KustoClient, KustoConnectionStringBuilder
from azure.kusto.data._cloud_settings import CloudSettings, DEFAULT_DEV_KUSTO_SERVICE_RESOURCE_ID
from azure.kusto.data._cloud_settings import CloudSettings
from azure.kusto.data._models import WellKnownDataSet
from azure.kusto.data.aio import KustoClient as AsyncKustoClient
from azure.kusto.data.env_utils import get_env, set_env, prepare_app_key_auth
from azure.kusto.data.exceptions import KustoServiceError
from azure.kusto.data.kusto_trusted_endpoints import MatchRule, well_known_kusto_endpoints
from azure.kusto.data.streaming_response import FrameType


Expand Down Expand Up @@ -337,55 +334,6 @@ def test_cloud_info(self):
assert cloud_info is CloudSettings.get_cloud_info_for_cluster(self.engine_cs)
assert cloud_info == CloudSettings.get_cloud_info_for_cluster(urljoin(self.engine_cs, "/test1/test2/test3"))

def test_cloud_info_404(self):
pytest.skip("This test is currently wrong - until all cluster are updated to the redirect uri, this test will fail")
cloud_info = CloudSettings.get_cloud_info_for_cluster("https://statusreturner.azurewebsites.net/404/test")
default_dev_cloud = dataclasses.replace(CloudSettings.DEFAULT_CLOUD, kusto_service_resource_id=DEFAULT_DEV_KUSTO_SERVICE_RESOURCE_ID)
assert cloud_info == CloudSettings.DEFAULT_CLOUD or cloud_info == default_dev_cloud

@pytest.mark.parametrize("code", [301, 302, 307, 308])
def test_no_redirects_fail_in_cloud(self, code):
pytest.skip("This test is currently not supported as it relies on the URI path witch we now ignore")
with KustoClient(
KustoConnectionStringBuilder.with_azure_token_credential(f"https://statusreturner.azurewebsites.net/{code}/nocloud", self.__class__.cred())
) as client:
with pytest.raises(KustoServiceError) as ex:
client.execute("db", "table")
assert ex.value.http_response.status_code == code

@pytest.mark.parametrize("code", [301, 302, 307, 308])
def test_no_redirects_fail_in_client(self, code):
well_known_kusto_endpoints.add_trusted_hosts([MatchRule("statusreturner.azurewebsites.net", False)], False)
with KustoClient(
KustoConnectionStringBuilder.with_azure_token_credential(f"https://statusreturner.azurewebsites.net/{code}/segment", self.__class__.cred())
) as client:
with pytest.raises(KustoServiceError) as ex:
client.execute("db", "table")
assert isinstance(ex.value.http_response, Response)
assert ex.value.http_response.status_code == code

@pytest.mark.asyncio
@pytest.mark.parametrize("code", [301, 302, 307, 308])
async def test_no_redirects_fail_in_cloud_async(self, code):
pytest.skip("This test is currently not supported as it relies on the URI path witch we now ignore")
async with AsyncKustoClient(
KustoConnectionStringBuilder.with_azure_token_credential(f"https://statusreturner.azurewebsites.net/{code}/nocloud", self.__class__.async_cred())
) as client:
with pytest.raises(KustoServiceError) as ex:
await client.execute("db", "table")
assert ex.value.http_response.status_code == code

@pytest.mark.asyncio
@pytest.mark.parametrize("code", [301, 302, 307, 308])
async def test_no_redirects_fail_in_client_async(self, code):
well_known_kusto_endpoints.add_trusted_hosts([MatchRule("statusreturner.azurewebsites.net", False)], False)
async with AsyncKustoClient(
KustoConnectionStringBuilder.with_azure_token_credential(f"https://statusreturner.azurewebsites.net/{code}/segment", self.__class__.async_cred())
) as client:
with pytest.raises(KustoServiceError) as ex:
await client.execute("db", "table")
assert ex.value.http_response.status == code

def test_semantic_error(self):
with self.get_client() as client:
with pytest.raises(KustoServiceError) as ex:
Expand Down
16 changes: 13 additions & 3 deletions azure-kusto-data/tests/test_kusto_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

import pandas
import pytest
from requests import Response

from azure.kusto.data import ClientRequestProperties, KustoClient, KustoConnectionStringBuilder
from azure.kusto.data._cloud_settings import CloudSettings
from azure.kusto.data.exceptions import KustoClosedError, KustoMultiApiError, KustoNetworkError, KustoServiceError
from azure.kusto.data.helpers import dataframe_from_result_table
from azure.kusto.data.response import KustoStreamingResponseDataSet
Expand All @@ -22,6 +22,16 @@ def method(request):
class TestKustoClient(KustoClientTestsMixin):
"""Tests class for KustoClient API"""

def test_no_redirects(self, redirect_server: tuple[str, int, list[str]]) -> None:
cluster, code, requests_seen = redirect_server
kcsb = KustoConnectionStringBuilder.with_aad_user_token_authentication(cluster, "test-token")
with KustoClient(kcsb) as client:
with pytest.raises(KustoServiceError) as error:
_ = client.execute("db", "table")
assert isinstance(error.value.http_response, Response)
assert error.value.http_response.status_code == code
assert requests_seen == ["/v2/rest/query"]

def test_throws_on_close(self):
"""Test query V2."""
with KustoClient(self.HOST) as client:
Expand Down Expand Up @@ -182,7 +192,7 @@ def test_custom_request_id(self, mock_post, method):
self._assert_client_request_id(mock_post.call_args[-1], value=request_id)

@patch("requests.Session.get", side_effect=mocked_requests_post)
def test_proxy_token_providers(self, mock_get, proxy_kcsb):
def test_proxy_token_providers(self, mock_get, proxy_kcsb, isolated_cloud_cache):
"""Test query V2."""
proxy = "https://my_proxy.sample"
kcsb, auth_supports_proxy = proxy_kcsb
Expand All @@ -198,7 +208,7 @@ def test_proxy_token_providers(self, mock_get, proxy_kcsb):
assert client._aad_helper.token_provider._proxy_dict == expected_dict
assert client._session.proxies == expected_dict

CloudSettings._cloud_cache.clear()
isolated_cloud_cache.clear()

client._aad_helper.token_provider._init_resources()

Expand Down
83 changes: 50 additions & 33 deletions azure-kusto-data/tests/test_security.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License
from unittest.mock import patch

import pytest

from azure.kusto.data._cloud_settings import CloudSettings
from azure.kusto.data._cloud_settings import CloudInfo, CloudSettings
from azure.kusto.data._token_providers import CallbackTokenProvider, MsiTokenProvider, UserPassTokenProvider
from azure.kusto.data import KustoConnectionStringBuilder
from azure.kusto.data.exceptions import KustoAuthenticationError
Expand All @@ -11,26 +13,44 @@
KUSTO_TEST_URI = "https://thisclusterdoesnotexist.kusto.windows.net"
TEST_INTERACTIVE_AUTH = False # User interaction required, enable this when running test manually

CloudSettings.add_to_cache(KUSTO_TEST_URI, CloudSettings.DEFAULT_CLOUD)
CloudSettings.add_to_cache("https://somecluster.kusto.windows.net", CloudSettings.DEFAULT_CLOUD)

@pytest.fixture(autouse=True)
def cached_cloud_settings(isolated_cloud_cache: dict[str, CloudInfo]) -> None:
isolated_cloud_cache[KUSTO_TEST_URI] = CloudSettings.DEFAULT_CLOUD
isolated_cloud_cache["https://somecluster.kusto.windows.net"] = CloudSettings.DEFAULT_CLOUD


def test_unauthorized_exception():
"""Test the exception thrown when authorization fails."""
cluster = "https://somecluster.kusto.windows.net"
username = "username@microsoft.com"
kcsb = KustoConnectionStringBuilder.with_aad_user_password_authentication(cluster, username, "StrongestPasswordEver", "authorityName")
aad_helper = _AadHelper(kcsb, False)
aad_helper.token_provider._init_resources()

try:
aad_helper.acquire_authorization_header()
except KustoAuthenticationError as error:
assert error.authentication_method == UserPassTokenProvider.name()
assert error.authority == "https://login.microsoftonline.com/authorityName"
assert error.kusto_cluster == cluster
assert error.kwargs["username"] == username
assert error.kwargs["client_id"] == CloudSettings.DEFAULT_CLOUD.kusto_client_app_id
password = "StrongestPasswordEver"
auth_error = {"error": "invalid_grant", "error_description": "Invalid test credentials"}
with (
patch("azure.kusto.data._token_providers.PublicClientApplication") as public_client,
patch("requests.sessions.Session.request", side_effect=AssertionError("Unexpected HTTP request")) as http_request,
):
msal_client = public_client.return_value
msal_client.get_accounts.return_value = []
msal_client.acquire_token_silent.return_value = None
msal_client.acquire_token_by_username_password.return_value = auth_error
kcsb = KustoConnectionStringBuilder.with_aad_user_password_authentication(cluster, username, password, "authorityName")
aad_helper = _AadHelper(kcsb, False)
aad_helper.token_provider._init_resources()

with pytest.raises(KustoAuthenticationError) as error:
aad_helper.acquire_authorization_header()

assert error.value.authentication_method == UserPassTokenProvider.name()
assert error.value.authority == "https://login.microsoftonline.com/authorityName"
assert error.value.kusto_cluster == cluster
assert error.value.kwargs["username"] == username
assert error.value.kwargs["client_id"] == CloudSettings.DEFAULT_CLOUD.kusto_client_app_id
assert auth_error["error_description"] in str(error.value.exception)
msal_client.acquire_token_by_username_password.assert_called_once_with(
username=username, password=password, scopes=[CloudSettings.DEFAULT_CLOUD.kusto_service_resource_id + "/.default"]
)
http_request.assert_not_called()


# TODO: remove this once we can control the timeout
Expand Down Expand Up @@ -60,23 +80,21 @@ def test_msi_auth():
for h in helpers:
h.token_provider._init_resources()

try:
with pytest.raises(KustoAuthenticationError) as error:
helpers[0].acquire_authorization_header()
except KustoAuthenticationError as e:
assert e.authentication_method == MsiTokenProvider.name()
assert "client_id" not in e.kwargs
assert "object_id" not in e.kwargs
assert "msi_res_id" not in e.kwargs
assert error.value.authentication_method == MsiTokenProvider.name()
assert "client_id" not in error.value.kwargs
assert "object_id" not in error.value.kwargs
assert "msi_res_id" not in error.value.kwargs

try:
with pytest.raises(KustoAuthenticationError) as error:
helpers[1].acquire_authorization_header()
except KustoAuthenticationError as e:
assert e.authentication_method == MsiTokenProvider.name()
assert e.kwargs["client_id"] == client_guid
assert "object_id" not in e.kwargs
assert "msi_res_id" not in e.kwargs
assert str(e.exception).index("client_id") > -1
assert str(e.exception).index(client_guid) > -1
assert error.value.authentication_method == MsiTokenProvider.name()
assert error.value.kwargs["client_id"] == client_guid
assert "object_id" not in error.value.kwargs
assert "msi_res_id" not in error.value.kwargs
assert str(error.value.exception).index("client_id") > -1
assert str(error.value.exception).index(client_guid) > -1


def test_token_provider_auth():
Expand All @@ -94,11 +112,10 @@ def test_token_provider_auth():
auth_header = valid_helper.acquire_authorization_header()
assert auth_header.index(valid_token_provider()) > -1

try:
with pytest.raises(KustoAuthenticationError) as error:
invalid_helper.acquire_authorization_header()
except KustoAuthenticationError as e:
assert e.authentication_method == CallbackTokenProvider.name()
assert str(e.exception).index(str(type(invalid_token_provider()))) > -1
assert error.value.authentication_method == CallbackTokenProvider.name()
assert str(error.value.exception).index(str(type(invalid_token_provider()))) > -1


def test_user_app_token_auth():
Expand Down
Loading