-
Couldn't load subscription status.
- Fork 378
feat: replacing DefaultAzureCredential with ManagedIdentityCredential #350
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
Changes from all commits
Commits
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
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,41 @@ | ||
| import os | ||
| from azure.identity import ManagedIdentityCredential, DefaultAzureCredential | ||
| from azure.identity.aio import ManagedIdentityCredential as AioManagedIdentityCredential, DefaultAzureCredential as AioDefaultAzureCredential | ||
|
|
||
|
|
||
| async def get_azure_credential_async(client_id=None): | ||
| """ | ||
| Returns an Azure credential asynchronously based on the application environment. | ||
|
|
||
| If the environment is 'dev', it uses AioDefaultAzureCredential. | ||
| Otherwise, it uses AioManagedIdentityCredential. | ||
|
|
||
| Args: | ||
| client_id (str, optional): The client ID for the Managed Identity Credential. | ||
|
|
||
| Returns: | ||
| Credential object: Either AioDefaultAzureCredential or AioManagedIdentityCredential. | ||
| """ | ||
| if os.getenv("APP_ENV", "prod").lower() == 'dev': | ||
| return AioDefaultAzureCredential() # CodeQL [SM05139] Okay use of DefaultAzureCredential as it is only used in development | ||
| else: | ||
| return AioManagedIdentityCredential(client_id=client_id) | ||
|
|
||
|
|
||
| def get_azure_credential(client_id=None): | ||
| """ | ||
| Returns an Azure credential based on the application environment. | ||
|
|
||
| If the environment is 'dev', it uses DefaultAzureCredential. | ||
| Otherwise, it uses ManagedIdentityCredential. | ||
|
|
||
| Args: | ||
| client_id (str, optional): The client ID for the Managed Identity Credential. | ||
|
|
||
| Returns: | ||
| Credential object: Either DefaultAzureCredential or ManagedIdentityCredential. | ||
| """ | ||
| if os.getenv("APP_ENV", "prod").lower() == 'dev': | ||
| return DefaultAzureCredential() # CodeQL [SM05139] Okay use of DefaultAzureCredential as it is only used in development | ||
| else: | ||
| return ManagedIdentityCredential(client_id=client_id) |
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,78 @@ | ||
| import pytest | ||
| import sys | ||
| import os | ||
| from unittest.mock import patch, MagicMock | ||
|
|
||
| # Ensure src/backend is on the Python path for imports | ||
| sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..'))) | ||
|
|
||
| import helpers.azure_credential_utils as azure_credential_utils | ||
|
|
||
| # Synchronous tests | ||
|
|
||
| @patch("helpers.azure_credential_utils.os.getenv") | ||
| @patch("helpers.azure_credential_utils.DefaultAzureCredential") | ||
| @patch("helpers.azure_credential_utils.ManagedIdentityCredential") | ||
| def test_get_azure_credential_dev_env(mock_managed_identity_credential, mock_default_azure_credential, mock_getenv): | ||
| """Test get_azure_credential in dev environment.""" | ||
| mock_getenv.return_value = "dev" | ||
| mock_default_credential = MagicMock() | ||
| mock_default_azure_credential.return_value = mock_default_credential | ||
|
|
||
| credential = azure_credential_utils.get_azure_credential() | ||
|
|
||
| mock_getenv.assert_called_once_with("APP_ENV", "prod") | ||
| mock_default_azure_credential.assert_called_once() | ||
| mock_managed_identity_credential.assert_not_called() | ||
| assert credential == mock_default_credential | ||
|
|
||
| @patch("helpers.azure_credential_utils.os.getenv") | ||
| @patch("helpers.azure_credential_utils.DefaultAzureCredential") | ||
| @patch("helpers.azure_credential_utils.ManagedIdentityCredential") | ||
| def test_get_azure_credential_non_dev_env(mock_managed_identity_credential, mock_default_azure_credential, mock_getenv): | ||
| """Test get_azure_credential in non-dev environment.""" | ||
| mock_getenv.return_value = "prod" | ||
| mock_managed_credential = MagicMock() | ||
| mock_managed_identity_credential.return_value = mock_managed_credential | ||
| credential = azure_credential_utils.get_azure_credential(client_id="test-client-id") | ||
|
|
||
| mock_getenv.assert_called_once_with("APP_ENV", "prod") | ||
| mock_managed_identity_credential.assert_called_once_with(client_id="test-client-id") | ||
| mock_default_azure_credential.assert_not_called() | ||
| assert credential == mock_managed_credential | ||
|
|
||
| # Asynchronous tests | ||
|
|
||
| @pytest.mark.asyncio | ||
| @patch("helpers.azure_credential_utils.os.getenv") | ||
| @patch("helpers.azure_credential_utils.AioDefaultAzureCredential") | ||
| @patch("helpers.azure_credential_utils.AioManagedIdentityCredential") | ||
| async def test_get_azure_credential_async_dev_env(mock_aio_managed_identity_credential, mock_aio_default_azure_credential, mock_getenv): | ||
| """Test get_azure_credential_async in dev environment.""" | ||
| mock_getenv.return_value = "dev" | ||
| mock_aio_default_credential = MagicMock() | ||
| mock_aio_default_azure_credential.return_value = mock_aio_default_credential | ||
|
|
||
| credential = await azure_credential_utils.get_azure_credential_async() | ||
|
|
||
| mock_getenv.assert_called_once_with("APP_ENV", "prod") | ||
| mock_aio_default_azure_credential.assert_called_once() | ||
| mock_aio_managed_identity_credential.assert_not_called() | ||
| assert credential == mock_aio_default_credential | ||
|
|
||
| @pytest.mark.asyncio | ||
| @patch("helpers.azure_credential_utils.os.getenv") | ||
| @patch("helpers.azure_credential_utils.AioDefaultAzureCredential") | ||
| @patch("helpers.azure_credential_utils.AioManagedIdentityCredential") | ||
| async def test_get_azure_credential_async_non_dev_env(mock_aio_managed_identity_credential, mock_aio_default_azure_credential, mock_getenv): | ||
| """Test get_azure_credential_async in non-dev environment.""" | ||
| mock_getenv.return_value = "prod" | ||
| mock_aio_managed_credential = MagicMock() | ||
| mock_aio_managed_identity_credential.return_value = mock_aio_managed_credential | ||
|
|
||
| credential = await azure_credential_utils.get_azure_credential_async(client_id="test-client-id") | ||
|
|
||
| mock_getenv.assert_called_once_with("APP_ENV", "prod") | ||
| mock_aio_managed_identity_credential.assert_called_once_with(client_id="test-client-id") | ||
| mock_aio_default_azure_credential.assert_not_called() | ||
| assert credential == mock_aio_managed_credential |
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
Oops, something went wrong.
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.