1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
|
# ------------------------------------
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# ------------------------------------
import os
from unittest.mock import Mock, patch
from urllib.parse import urlparse
from azure.core.credentials import AccessToken
from azure.identity import CredentialUnavailableError
from azure.identity.aio import (
AzureCliCredential,
DefaultAzureCredential,
ManagedIdentityCredential,
SharedTokenCacheCredential,
VisualStudioCodeCredential,
)
from azure.identity._constants import EnvironmentVariables
import pytest
from helpers import mock_response, Request
from helpers_async import async_validating_transport, get_completed_future, wrap_in_future
from test_shared_cache_credential import build_aad_response, get_account_event, populated_cache
@pytest.mark.asyncio
async def test_iterates_only_once():
"""When a credential succeeds, DefaultAzureCredential should use that credential thereafter, ignoring the others"""
unavailable_credential = Mock(get_token=Mock(side_effect=CredentialUnavailableError(message="...")))
successful_credential = Mock(get_token=Mock(return_value=get_completed_future(AccessToken("***", 42))))
credential = DefaultAzureCredential()
credential.credentials = [
unavailable_credential,
successful_credential,
Mock(get_token=Mock(side_effect=Exception("iteration didn't stop after a credential provided a token"))),
]
for n in range(3):
await credential.get_token("scope")
assert unavailable_credential.get_token.call_count == 1
assert successful_credential.get_token.call_count == n + 1
@pytest.mark.parametrize("authority", ("localhost", "https://localhost"))
def test_authority(authority):
"""the credential should accept authority configuration by keyword argument or environment"""
parsed_authority = urlparse(authority)
expected_netloc = parsed_authority.netloc or authority # "localhost" parses to netloc "", path "localhost"
def test_initialization(mock_credential, expect_argument):
DefaultAzureCredential(authority=authority)
assert mock_credential.call_count == 1
# N.B. if os.environ has been patched somewhere in the stack, that patch is in place here
environment = dict(os.environ, **{EnvironmentVariables.AZURE_AUTHORITY_HOST: authority})
with patch.dict(DefaultAzureCredential.__module__ + ".os.environ", environment, clear=True):
DefaultAzureCredential()
assert mock_credential.call_count == 2
for _, kwargs in mock_credential.call_args_list:
if expect_argument:
actual = urlparse(kwargs["authority"])
assert actual.scheme == "https"
assert actual.netloc == expected_netloc
else:
assert "authority" not in kwargs
# authority should be passed to EnvironmentCredential as a keyword argument
environment = {var: "foo" for var in EnvironmentVariables.CLIENT_SECRET_VARS}
with patch(DefaultAzureCredential.__module__ + ".EnvironmentCredential") as mock_credential:
with patch.dict("os.environ", environment, clear=True):
test_initialization(mock_credential, expect_argument=True)
# authority should be passed to SharedTokenCacheCredential as a keyword argument
with patch(DefaultAzureCredential.__module__ + ".SharedTokenCacheCredential") as mock_credential:
mock_credential.supported = lambda: True
with patch.dict("os.environ", {}, clear=True):
test_initialization(mock_credential, expect_argument=True)
# authority should not be passed to ManagedIdentityCredential
with patch(DefaultAzureCredential.__module__ + ".ManagedIdentityCredential") as mock_credential:
with patch.dict("os.environ", {EnvironmentVariables.MSI_ENDPOINT: "_"}, clear=True):
test_initialization(mock_credential, expect_argument=False)
# authority should not be passed to AzureCliCredential
with patch(DefaultAzureCredential.__module__ + ".AzureCliCredential") as mock_credential:
with patch(DefaultAzureCredential.__module__ + ".SharedTokenCacheCredential") as shared_cache:
shared_cache.supported = lambda: False
with patch.dict("os.environ", {}, clear=True):
test_initialization(mock_credential, expect_argument=False)
def test_exclude_options():
def assert_credentials_not_present(chain, *credential_classes):
actual = {c.__class__ for c in chain.credentials}
assert len(actual)
# no unexpected credential is in the chain
excluded = set(credential_classes)
assert len(actual & excluded) == 0
# only excluded credentials have been excluded from the default
default = {c.__class__ for c in DefaultAzureCredential().credentials}
assert actual <= default # n.b. we know actual is non-empty
assert default - actual <= excluded
# when exclude_managed_identity_credential is set to True, check if ManagedIdentityCredential instance is not present
credential = DefaultAzureCredential(exclude_managed_identity_credential=True)
assert_credentials_not_present(credential, ManagedIdentityCredential)
if SharedTokenCacheCredential.supported():
credential = DefaultAzureCredential(exclude_shared_token_cache_credential=True)
assert_credentials_not_present(credential, SharedTokenCacheCredential)
credential = DefaultAzureCredential(exclude_cli_credential=True)
assert_credentials_not_present(credential, AzureCliCredential)
credential = DefaultAzureCredential(exclude_visual_studio_code_credential=True)
assert_credentials_not_present(credential, VisualStudioCodeCredential)
@pytest.mark.asyncio
async def test_shared_cache_tenant_id():
expected_access_token = "expected-access-token"
refresh_token_a = "refresh-token-a"
refresh_token_b = "refresh-token-b"
# The value of upn is arbitrary because this test verifies the credential's behavior given a specified
# tenant. During a complete live test run, $AZURE_USERNAME will have a value which DefaultAzureCredential
# should pass to SharedTokenCacheCredential. This test will fail if the mock accounts don't match that value.
upn = os.environ.get(EnvironmentVariables.AZURE_USERNAME, "spam@eggs")
tenant_a = "tenant-a"
tenant_b = "tenant-b"
# two cached accounts, same username, different tenants -> shared_cache_tenant_id should prevail
account_a = get_account_event(username=upn, uid="another-guid", utid=tenant_a, refresh_token=refresh_token_a)
account_b = get_account_event(username=upn, uid="more-guid", utid=tenant_b, refresh_token=refresh_token_b)
cache = populated_cache(account_a, account_b)
credential = get_credential_for_shared_cache_test(
refresh_token_b, expected_access_token, cache, shared_cache_tenant_id=tenant_b
)
token = await credential.get_token("scope")
assert token.token == expected_access_token
# redundantly specifying shared_cache_username makes no difference
credential = get_credential_for_shared_cache_test(
refresh_token_b, expected_access_token, cache, shared_cache_tenant_id=tenant_b, shared_cache_username=upn
)
token = await credential.get_token("scope")
assert token.token == expected_access_token
# shared_cache_tenant_id should prevail over AZURE_TENANT_ID
with patch("os.environ", {EnvironmentVariables.AZURE_TENANT_ID: tenant_a}):
credential = get_credential_for_shared_cache_test(
refresh_token_b, expected_access_token, cache, shared_cache_tenant_id=tenant_b
)
token = await credential.get_token("scope")
assert token.token == expected_access_token
# AZURE_TENANT_ID should be used when shared_cache_tenant_id isn't specified
with patch("os.environ", {EnvironmentVariables.AZURE_TENANT_ID: tenant_b}):
credential = get_credential_for_shared_cache_test(refresh_token_b, expected_access_token, cache)
token = await credential.get_token("scope")
assert token.token == expected_access_token
@pytest.mark.asyncio
async def test_shared_cache_username():
expected_access_token = "expected-access-token"
refresh_token_a = "refresh-token-a"
refresh_token_b = "refresh-token-b"
upn_a = "spam@eggs"
upn_b = "eggs@spam"
# The value of tenant_id is arbitrary because this test verifies the credential's behavior given a specified
# username. During a complete live test run, $AZURE_TENANT_ID will have a value which DefaultAzureCredential
# should pass to SharedTokenCacheCredential. This test will fail if the mock accounts don't match that value.
tenant_id = os.environ.get(EnvironmentVariables.AZURE_TENANT_ID, "the-tenant")
# two cached accounts, same tenant, different usernames -> shared_cache_username should prevail
account_a = get_account_event(username=upn_a, uid="another-guid", utid=tenant_id, refresh_token=refresh_token_a)
account_b = get_account_event(username=upn_b, uid="more-guid", utid=tenant_id, refresh_token=refresh_token_b)
cache = populated_cache(account_a, account_b)
credential = get_credential_for_shared_cache_test(
refresh_token_a, expected_access_token, cache, shared_cache_username=upn_a
)
token = await credential.get_token("scope")
assert token.token == expected_access_token
# shared_cache_username should prevail over AZURE_USERNAME
with patch("os.environ", {EnvironmentVariables.AZURE_USERNAME: upn_b}):
credential = get_credential_for_shared_cache_test(
refresh_token_a, expected_access_token, cache, shared_cache_username=upn_a
)
token = await credential.get_token("scope")
assert token.token == expected_access_token
# AZURE_USERNAME should be used when shared_cache_username isn't specified
with patch("os.environ", {EnvironmentVariables.AZURE_USERNAME: upn_b}):
credential = get_credential_for_shared_cache_test(refresh_token_b, expected_access_token, cache)
token = await credential.get_token("scope")
assert token.token == expected_access_token
def test_vscode_tenant_id():
"""the credential should allow configuring a tenant ID for VisualStudioCodeCredential by kwarg or environment"""
expected_args = {"tenant_id": "the-tenant"}
with patch(DefaultAzureCredential.__module__ + ".VisualStudioCodeCredential") as mock_credential:
DefaultAzureCredential(visual_studio_code_tenant_id=expected_args["tenant_id"])
mock_credential.assert_called_once_with(**expected_args)
# tenant id can also be specified in $AZURE_TENANT_ID
with patch.dict(os.environ, {EnvironmentVariables.AZURE_TENANT_ID: expected_args["tenant_id"]}, clear=True):
with patch(DefaultAzureCredential.__module__ + ".VisualStudioCodeCredential") as mock_credential:
DefaultAzureCredential()
mock_credential.assert_called_once_with(**expected_args)
# keyword argument should override environment variable
with patch.dict(
os.environ, {EnvironmentVariables.AZURE_TENANT_ID: "not-" + expected_args["tenant_id"]}, clear=True
):
with patch(DefaultAzureCredential.__module__ + ".VisualStudioCodeCredential") as mock_credential:
DefaultAzureCredential(visual_studio_code_tenant_id=expected_args["tenant_id"])
mock_credential.assert_called_once_with(**expected_args)
@pytest.mark.asyncio
async def test_default_credential_shared_cache_use():
with patch(DefaultAzureCredential.__module__ + ".SharedTokenCacheCredential") as mock_credential:
mock_credential.supported = Mock(return_value=False)
# unsupported platform -> default credential shouldn't use shared cache
credential = DefaultAzureCredential()
assert mock_credential.call_count == 0
assert mock_credential.supported.call_count == 1
mock_credential.supported.reset_mock()
mock_credential.supported = Mock(return_value=True)
# supported platform -> default credential should use shared cache
credential = DefaultAzureCredential()
assert mock_credential.call_count == 1
assert mock_credential.supported.call_count == 1
mock_credential.supported.reset_mock()
def test_managed_identity_client_id():
"""the credential should accept a user-assigned managed identity's client ID by kwarg or environment variable"""
expected_args = {"client_id": "the client"}
with patch(DefaultAzureCredential.__module__ + ".ManagedIdentityCredential") as mock_credential:
DefaultAzureCredential(managed_identity_client_id=expected_args["client_id"])
mock_credential.assert_called_once_with(**expected_args)
# client id can also be specified in $AZURE_CLIENT_ID
with patch.dict(os.environ, {EnvironmentVariables.AZURE_CLIENT_ID: expected_args["client_id"]}, clear=True):
with patch(DefaultAzureCredential.__module__ + ".ManagedIdentityCredential") as mock_credential:
DefaultAzureCredential()
mock_credential.assert_called_once_with(**expected_args)
# keyword argument should override environment variable
with patch.dict(
os.environ, {EnvironmentVariables.AZURE_CLIENT_ID: "not-" + expected_args["client_id"]}, clear=True
):
with patch(DefaultAzureCredential.__module__ + ".ManagedIdentityCredential") as mock_credential:
DefaultAzureCredential(managed_identity_client_id=expected_args["client_id"])
mock_credential.assert_called_once_with(**expected_args)
def get_credential_for_shared_cache_test(expected_refresh_token, expected_access_token, cache, **kwargs):
exclude_other_credentials = {
option: True for option in ("exclude_environment_credential", "exclude_managed_identity_credential")
}
# validating transport will raise if the shared cache credential isn't used, or selects the wrong refresh token
transport = async_validating_transport(
requests=[Request(required_data={"refresh_token": expected_refresh_token})],
responses=[mock_response(json_payload=build_aad_response(access_token=expected_access_token))],
)
# this credential uses a mock shared cache, so it works on all platforms
with patch.object(SharedTokenCacheCredential, "supported", lambda: True):
return DefaultAzureCredential(_cache=cache, transport=transport, **exclude_other_credentials, **kwargs)
|