Skip to content
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

Support user-based authentication in live tests #33994

Merged
merged 3 commits into from
Jan 25, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
Re-organize credential choice logic
  • Loading branch information
mccoyp committed Jan 24, 2024
commit 79e01849033f6d7b5c1c0f5a1fd8948d1e39f47a
Original file line number Diff line number Diff line change
Expand Up @@ -95,45 +95,49 @@ def get_credential(self, client_class, **kwargs):
use_cli = os.environ.get("AZURE_TEST_USE_CLI_AUTH", "false")
is_async = kwargs.pop("is_async", False)

# User-based authentication through Azure PowerShell, if requested
if use_pwsh.lower() == "true" and self.is_live:
_LOGGER.info(
"Environment variable AZURE_TEST_USE_PWSH_AUTH set to 'true'. Using AzurePowerShellCredential."
)
from azure.identity import AzurePowerShellCredential

if is_async:
from azure.identity.aio import AzurePowerShellCredential
return AzurePowerShellCredential()
# User-based authentication through Azure CLI, if requested
if use_cli.lower() == "true" and self.is_live:
_LOGGER.info("Environment variable AZURE_TEST_USE_CLI_AUTH set to 'true'. Using AzureCliCredential.")
from azure.identity import AzureCliCredential

if is_async:
from azure.identity.aio import AzureCliCredential
return AzureCliCredential()

# Service principal authentication
if tenant_id and client_id and secret and self.is_live:
if _is_autorest_v3(client_class):
# Return live credentials only in live mode
if self.is_live:
# User-based authentication through Azure PowerShell, if requested
if use_pwsh.lower() == "true":
_LOGGER.info(
"Service principal client ID, secret, and tenant ID detected. Using ClientSecretCredential.\n"
"For user-based authentication, set AZURE_TEST_USE_PWSH_AUTH or AZURE_TEST_USE_CLI_AUTH to 'true'."
"Environment variable AZURE_TEST_USE_PWSH_AUTH set to 'true'. Using AzurePowerShellCredential."
)
# Create azure-identity class
from azure.identity import ClientSecretCredential
from azure.identity import AzurePowerShellCredential

if is_async:
from azure.identity.aio import ClientSecretCredential
return ClientSecretCredential(tenant_id=tenant_id, client_id=client_id, client_secret=secret)
else:
# Create msrestazure class
from msrestazure.azure_active_directory import (
ServicePrincipalCredentials,
)
from azure.identity.aio import AzurePowerShellCredential
return AzurePowerShellCredential()
# User-based authentication through Azure CLI, if requested
if use_cli.lower() == "true":
_LOGGER.info("Environment variable AZURE_TEST_USE_CLI_AUTH set to 'true'. Using AzureCliCredential.")
from azure.identity import AzureCliCredential

if is_async:
from azure.identity.aio import AzureCliCredential
return AzureCliCredential()

# Service principal authentication
if tenant_id and client_id and secret:
# Check for track 2 client
if _is_autorest_v3(client_class):
_LOGGER.info(
"Service principal client ID, secret, and tenant ID detected. Using ClientSecretCredential.\n"
"For user-based auth, set AZURE_TEST_USE_PWSH_AUTH or AZURE_TEST_USE_CLI_AUTH to 'true'."
)
from azure.identity import ClientSecretCredential

if is_async:
from azure.identity.aio import ClientSecretCredential
return ClientSecretCredential(tenant_id=tenant_id, client_id=client_id, client_secret=secret)
else:
# Create msrestazure class
from msrestazure.azure_active_directory import (
ServicePrincipalCredentials,
)

return ServicePrincipalCredentials(tenant=tenant_id, client_id=client_id, secret=secret)

return ServicePrincipalCredentials(tenant=tenant_id, client_id=client_id, secret=secret)
# For playback tests, return credentials that will accept playback `get_token` calls
else:
if _is_autorest_v3(client_class):
if is_async:
Expand Down