-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Identity token cred env #41060
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Identity token cred env #41060
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
e687121
Added `AZURE_TOKEN_CREDENTIALS` environment variable support
xiangyan99 eb3c461
add trimming
xiangyan99 b3bc48f
update
xiangyan99 d592b06
updates
xiangyan99 7e669a4
udpate tests
xiangyan99 81aeaf3
fix typo
xiangyan99 5beaf8b
Update sdk/identity/azure-identity/tests/test_token_credentials_env_a…
xiangyan99 1399b98
Update sdk/identity/azure-identity/tests/test_token_credentials_env.py
xiangyan99 b036859
Update sdk/identity/azure-identity/tests/test_token_credentials_env.py
xiangyan99 14b0d6b
Update sdk/identity/azure-identity/tests/test_token_credentials_env_a…
xiangyan99 eaf602b
Update sdk/identity/azure-identity/CHANGELOG.md
xiangyan99 e7b8245
add tests
xiangyan99 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
sdk/identity/azure-identity/tests/test_token_credentials_env.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| # ------------------------------------ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT License. | ||
| # ------------------------------------ | ||
| import os | ||
| from unittest.mock import patch | ||
|
|
||
| import pytest | ||
|
|
||
| from azure.identity import ( | ||
| AzureCliCredential, | ||
| AzureDeveloperCliCredential, | ||
| AzurePowerShellCredential, | ||
| DefaultAzureCredential, | ||
| EnvironmentCredential, | ||
| ManagedIdentityCredential, | ||
| SharedTokenCacheCredential, | ||
| WorkloadIdentityCredential, | ||
| ) | ||
| from azure.identity._constants import EnvironmentVariables | ||
|
|
||
|
|
||
| def test_token_credentials_env_dev(): | ||
| """With AZURE_TOKEN_CREDENTIALS=dev, DefaultAzureCredential should use only developer credentials""" | ||
|
|
||
| prod_credentials = {EnvironmentCredential, WorkloadIdentityCredential, ManagedIdentityCredential} | ||
|
|
||
| with patch.dict("os.environ", {EnvironmentVariables.AZURE_TOKEN_CREDENTIALS: "dev"}, clear=False): | ||
| credential = DefaultAzureCredential() | ||
|
|
||
| # Get the actual credential classes in the chain | ||
| actual_classes = {c.__class__ for c in credential.credentials} | ||
|
|
||
| # All dev credentials should be present (if supported) | ||
| if SharedTokenCacheCredential.supported(): | ||
| assert SharedTokenCacheCredential in actual_classes | ||
|
|
||
| # Other developer credentials should be present | ||
| assert AzureCliCredential in actual_classes | ||
| assert AzureDeveloperCliCredential in actual_classes | ||
| assert AzurePowerShellCredential in actual_classes | ||
|
|
||
| # Production credentials should NOT be present | ||
| for cred_class in prod_credentials: | ||
| if cred_class == WorkloadIdentityCredential: | ||
| # Skip this check unless env vars are set | ||
| if not all(os.environ.get(var) for var in EnvironmentVariables.WORKLOAD_IDENTITY_VARS): | ||
| continue | ||
| assert cred_class not in actual_classes | ||
|
|
||
|
|
||
| def test_token_credentials_env_prod(): | ||
| """With AZURE_TOKEN_CREDENTIALS=prod, DefaultAzureCredential should use only production credentials""" | ||
|
|
||
| dev_credentials = { | ||
| SharedTokenCacheCredential, | ||
| AzureCliCredential, | ||
| AzureDeveloperCliCredential, | ||
| AzurePowerShellCredential, | ||
| } | ||
|
|
||
| with patch.dict("os.environ", {EnvironmentVariables.AZURE_TOKEN_CREDENTIALS: "prod"}, clear=False): | ||
| # Print to verify the environment variable is set in the test | ||
| print(f"AZURE_TOKEN_CREDENTIALS={os.environ.get(EnvironmentVariables.AZURE_TOKEN_CREDENTIALS)}") | ||
|
|
||
| credential = DefaultAzureCredential() | ||
|
|
||
| # Get the actual credential classes in the chain | ||
| actual_classes = {c.__class__ for c in credential.credentials} | ||
|
|
||
| # Print which credentials are actually in the chain | ||
| print("Credentials in chain:") | ||
| for cls in actual_classes: | ||
| print(f" - {cls.__name__}") | ||
|
|
||
| # Production credentials should be present | ||
| assert EnvironmentCredential in actual_classes | ||
| assert ManagedIdentityCredential in actual_classes | ||
|
|
||
| # Check WorkloadIdentityCredential only if env vars are set | ||
| if all(os.environ.get(var) for var in EnvironmentVariables.WORKLOAD_IDENTITY_VARS): | ||
| assert WorkloadIdentityCredential in actual_classes | ||
|
|
||
| # Developer credentials should NOT be present | ||
| for cred_class in dev_credentials: | ||
| assert cred_class not in actual_classes | ||
|
|
||
|
xiangyan99 marked this conversation as resolved.
|
||
|
|
||
| def test_token_credentials_env_case_insensitive(): | ||
| """AZURE_TOKEN_CREDENTIALS should be case insensitive""" | ||
|
|
||
| with patch.dict("os.environ", {EnvironmentVariables.AZURE_TOKEN_CREDENTIALS: "DeV"}, clear=False): | ||
| credential = DefaultAzureCredential() | ||
|
|
||
| # Get the actual credential classes in the chain | ||
| actual_classes = {c.__class__ for c in credential.credentials} | ||
|
|
||
| # EnvironmentCredential (prod) should not be present | ||
| assert EnvironmentCredential not in actual_classes | ||
|
|
||
| # AzureCliCredential (dev) should be present | ||
| assert AzureCliCredential in actual_classes | ||
|
|
||
|
|
||
| def test_token_credentials_env_invalid(): | ||
| """Invalid AZURE_TOKEN_CREDENTIALS value should raise an error""" | ||
|
|
||
| with patch.dict("os.environ", {EnvironmentVariables.AZURE_TOKEN_CREDENTIALS: "invalid"}, clear=False): | ||
| with pytest.raises(ValueError): | ||
| credential = DefaultAzureCredential() | ||
|
|
||
|
|
||
| def test_token_credentials_env_with_exclude(): | ||
| with patch.dict("os.environ", {EnvironmentVariables.AZURE_TOKEN_CREDENTIALS: "prod"}, clear=False): | ||
| credential = DefaultAzureCredential(exclude_environment_credential=True) | ||
| actual_classes = {c.__class__ for c in credential.credentials} | ||
|
|
||
| assert EnvironmentCredential not in actual_classes | ||
118 changes: 118 additions & 0 deletions
118
sdk/identity/azure-identity/tests/test_token_credentials_env_async.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| # ------------------------------------ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT License. | ||
| # ------------------------------------ | ||
| import os | ||
| from unittest.mock import patch | ||
|
|
||
| import pytest | ||
|
|
||
| from azure.identity.aio import ( | ||
| AzureCliCredential, | ||
| AzureDeveloperCliCredential, | ||
| AzurePowerShellCredential, | ||
| DefaultAzureCredential, | ||
| EnvironmentCredential, | ||
| ManagedIdentityCredential, | ||
| SharedTokenCacheCredential, | ||
| WorkloadIdentityCredential, | ||
| ) | ||
| from azure.identity._constants import EnvironmentVariables | ||
|
|
||
|
|
||
| def test_token_credentials_env_dev(): | ||
| """With AZURE_TOKEN_CREDENTIALS=dev, DefaultAzureCredential should use only developer credentials""" | ||
|
|
||
| prod_credentials = {EnvironmentCredential, WorkloadIdentityCredential, ManagedIdentityCredential} | ||
|
|
||
| with patch.dict("os.environ", {EnvironmentVariables.AZURE_TOKEN_CREDENTIALS: "dev"}, clear=False): | ||
| credential = DefaultAzureCredential() | ||
|
|
||
| # Get the actual credential classes in the chain | ||
| actual_classes = {c.__class__ for c in credential.credentials} | ||
|
|
||
| # All dev credentials should be present (if supported) | ||
| if SharedTokenCacheCredential.supported(): | ||
| assert SharedTokenCacheCredential in actual_classes | ||
|
|
||
| # Other developer credentials should be present | ||
| assert AzureCliCredential in actual_classes | ||
| assert AzureDeveloperCliCredential in actual_classes | ||
| assert AzurePowerShellCredential in actual_classes | ||
|
|
||
| # Production credentials should NOT be present | ||
| for cred_class in prod_credentials: | ||
| if cred_class == WorkloadIdentityCredential: | ||
| # Skip this check unless env vars are set | ||
| if not all(os.environ.get(var) for var in EnvironmentVariables.WORKLOAD_IDENTITY_VARS): | ||
| continue | ||
| assert cred_class not in actual_classes | ||
|
|
||
|
|
||
| def test_token_credentials_env_prod(): | ||
| """With AZURE_TOKEN_CREDENTIALS=prod, DefaultAzureCredential should use only production credentials""" | ||
|
|
||
| dev_credentials = { | ||
| SharedTokenCacheCredential, | ||
| AzureCliCredential, | ||
| AzureDeveloperCliCredential, | ||
| AzurePowerShellCredential, | ||
| } | ||
|
|
||
| with patch.dict("os.environ", {EnvironmentVariables.AZURE_TOKEN_CREDENTIALS: "prod"}, clear=False): | ||
| # Print to verify the environment variable is set in the test | ||
| print(f"AZURE_TOKEN_CREDENTIALS={os.environ.get(EnvironmentVariables.AZURE_TOKEN_CREDENTIALS)}") | ||
|
|
||
| credential = DefaultAzureCredential() | ||
|
|
||
| # Get the actual credential classes in the chain | ||
| actual_classes = {c.__class__ for c in credential.credentials} | ||
|
|
||
| # Print which credentials are actually in the chain | ||
| print("Credentials in chain:") | ||
| for cls in actual_classes: | ||
| print(f" - {cls.__name__}") | ||
|
|
||
| # Production credentials should be present | ||
| assert EnvironmentCredential in actual_classes | ||
| assert ManagedIdentityCredential in actual_classes | ||
|
|
||
| # Check WorkloadIdentityCredential only if env vars are set | ||
| if all(os.environ.get(var) for var in EnvironmentVariables.WORKLOAD_IDENTITY_VARS): | ||
| assert WorkloadIdentityCredential in actual_classes | ||
|
|
||
| # Developer credentials should NOT be present | ||
| for cred_class in dev_credentials: | ||
| assert cred_class not in actual_classes | ||
|
|
||
|
|
||
| def test_token_credentials_env_case_insensitive(): | ||
| """AZURE_TOKEN_CREDENTIALS should be case insensitive""" | ||
|
|
||
| with patch.dict("os.environ", {EnvironmentVariables.AZURE_TOKEN_CREDENTIALS: "DeV"}, clear=False): | ||
| credential = DefaultAzureCredential() | ||
|
|
||
| # Get the actual credential classes in the chain | ||
| actual_classes = {c.__class__ for c in credential.credentials} | ||
|
|
||
| # EnvironmentCredential (prod) should not be present | ||
| assert EnvironmentCredential not in actual_classes | ||
|
|
||
| # AzureCliCredential (dev) should be present | ||
| assert AzureCliCredential in actual_classes | ||
|
|
||
|
|
||
| def test_token_credentials_env_invalid(): | ||
| """Invalid AZURE_TOKEN_CREDENTIALS value should raise an error""" | ||
|
|
||
| with patch.dict("os.environ", {EnvironmentVariables.AZURE_TOKEN_CREDENTIALS: "invalid"}, clear=False): | ||
| with pytest.raises(ValueError): | ||
| credential = DefaultAzureCredential() | ||
|
|
||
|
|
||
| def test_token_credentials_env_with_exclude(): | ||
| with patch.dict("os.environ", {EnvironmentVariables.AZURE_TOKEN_CREDENTIALS: "prod"}, clear=False): | ||
| credential = DefaultAzureCredential(exclude_environment_credential=True) | ||
| actual_classes = {c.__class__ for c in credential.credentials} | ||
|
|
||
| assert EnvironmentCredential not in actual_classes |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.