diff --git a/sdk/keyvault/azure-keyvault-keys/azure/keyvault/keys/_shared/__init__.py b/sdk/keyvault/azure-keyvault-keys/azure/keyvault/keys/_shared/__init__.py index 72fb27441900..f4b6c6c3563a 100644 --- a/sdk/keyvault/azure-keyvault-keys/azure/keyvault/keys/_shared/__init__.py +++ b/sdk/keyvault/azure-keyvault-keys/azure/keyvault/keys/_shared/__init__.py @@ -3,6 +3,8 @@ # Licensed under the MIT License. # ------------------------------------ from collections import namedtuple +import platform +from .._version import VERSION try: import urllib.parse as parse @@ -10,6 +12,10 @@ # pylint:disable=import-error import urlparse as parse # type: ignore +USER_AGENT = "azsdk-python-keyvault-keys/{} Python/{} ({})".format( + VERSION, platform.python_version(), platform.platform() +) + from .challenge_auth_policy import ChallengeAuthPolicy, ChallengeAuthPolicyBase from .client_base import KeyVaultClientBase from .http_challenge import HttpChallenge diff --git a/sdk/keyvault/azure-keyvault-keys/azure/keyvault/keys/_shared/async_client_base.py b/sdk/keyvault/azure-keyvault-keys/azure/keyvault/keys/_shared/async_client_base.py index 193e6e324c7e..8018b3110248 100644 --- a/sdk/keyvault/azure-keyvault-keys/azure/keyvault/keys/_shared/async_client_base.py +++ b/sdk/keyvault/azure-keyvault-keys/azure/keyvault/keys/_shared/async_client_base.py @@ -3,14 +3,16 @@ # Licensed under the MIT License. # ------------------------------------ from typing import Any, Callable, Mapping, AsyncIterator, TYPE_CHECKING + from azure.core.configuration import Configuration from azure.core.pipeline import AsyncPipeline +from azure.core.pipeline.policies import UserAgentPolicy from azure.core.pipeline.policies.distributed_tracing import DistributedTracingPolicy from azure.core.pipeline.transport import AsyncHttpTransport from msrest.serialization import Model from ._generated import KeyVaultClient -from . import AsyncChallengeAuthPolicy +from . import AsyncChallengeAuthPolicy, USER_AGENT if TYPE_CHECKING: @@ -22,14 +24,7 @@ class AsyncKeyVaultClientBase: - """ - :param credential: A credential or credential provider which can be used to authenticate to the vault, - a ValueError will be raised if the entity is not provided - :type credential: azure.authentication.Credential or azure.authentication.CredentialProvider - :param str vault_url: The url of the vault to which the client will connect, - a ValueError will be raised if the entity is not provided - :param ~azure.core.configuration.Configuration config: The configuration for the SecretClient - """ + """Base class for async Key Vault clients""" @staticmethod def _create_config( @@ -39,6 +34,11 @@ def _create_config( api_version = KeyVaultClient.DEFAULT_API_VERSION config = KeyVaultClient.get_configuration_class(api_version, aio=True)(credential, **kwargs) config.authentication_policy = AsyncChallengeAuthPolicy(credential) + + # replace the autorest-generated UserAgentPolicy and its hard-coded user agent + # https://github.com/Azure/azure-sdk-for-python/issues/6637 + config.user_agent_policy = UserAgentPolicy(base_user_agent=USER_AGENT, **kwargs) + return config def __init__( @@ -86,6 +86,7 @@ def _build_pipeline(config: Configuration, transport: AsyncHttpTransport, **kwar if transport is None: from azure.core.pipeline.transport import AioHttpTransport + transport = AioHttpTransport(**kwargs) return AsyncPipeline(transport, policies=policies) diff --git a/sdk/keyvault/azure-keyvault-keys/azure/keyvault/keys/_shared/client_base.py b/sdk/keyvault/azure-keyvault-keys/azure/keyvault/keys/_shared/client_base.py index 6b57476d8d3b..a521c09a1585 100644 --- a/sdk/keyvault/azure-keyvault-keys/azure/keyvault/keys/_shared/client_base.py +++ b/sdk/keyvault/azure-keyvault-keys/azure/keyvault/keys/_shared/client_base.py @@ -3,8 +3,10 @@ # Licensed under the MIT License. # ------------------------------------ from typing import TYPE_CHECKING + from azure.core import Configuration from azure.core.pipeline import Pipeline +from azure.core.pipeline.policies import UserAgentPolicy from azure.core.pipeline.transport import RequestsTransport from azure.core.pipeline.policies.distributed_tracing import DistributedTracingPolicy from ._generated import KeyVaultClient @@ -16,20 +18,14 @@ from azure.core.pipeline.transport import HttpTransport from .challenge_auth_policy import ChallengeAuthPolicy +from . import USER_AGENT KEY_VAULT_SCOPE = "https://vault.azure.net/.default" class KeyVaultClientBase(object): - """ - :param credential: A credential or credential provider which can be used to authenticate to the vault, - a ValueError will be raised if the entity is not provided - :type credential: azure.core.credentials.TokenCredential - :param str vault_url: The url of the vault to which the client will connect, - a ValueError will be raised if the entity is not provided - :param ~azure.core.configuration.Configuration config: The configuration for the KeyClient - """ + """Base class for Key Vault clients""" @staticmethod def _create_config(credential, api_version=None, **kwargs): @@ -38,6 +34,11 @@ def _create_config(credential, api_version=None, **kwargs): api_version = KeyVaultClient.DEFAULT_API_VERSION config = KeyVaultClient.get_configuration_class(api_version, aio=False)(credential, **kwargs) config.authentication_policy = ChallengeAuthPolicy(credential) + + # replace the autorest-generated UserAgentPolicy and its hard-coded user agent + # https://github.com/Azure/azure-sdk-for-python/issues/6637 + config.user_agent_policy = UserAgentPolicy(base_user_agent=USER_AGENT, **kwargs) + return config def __init__(self, vault_url, credential, transport=None, api_version=None, **kwargs): diff --git a/sdk/keyvault/azure-keyvault-keys/azure/keyvault/keys/aio/client.py b/sdk/keyvault/azure-keyvault-keys/azure/keyvault/keys/aio/client.py index 390bd77a6602..7f345a4143ce 100644 --- a/sdk/keyvault/azure-keyvault-keys/azure/keyvault/keys/aio/client.py +++ b/sdk/keyvault/azure-keyvault-keys/azure/keyvault/keys/aio/client.py @@ -13,7 +13,11 @@ class KeyClient(AsyncKeyVaultClientBase): - """A high-level interface for managing a vault's keys. + """A high-level asynchronous interface for managing a vault's keys. + + :param credential: An object which can provide an access token for the vault, such as a credential from + :mod:`azure.identity.aio` + :param str vault_url: URL of the vault the client will access Example: .. literalinclude:: ../tests/test_samples_keys_async.py diff --git a/sdk/keyvault/azure-keyvault-keys/azure/keyvault/keys/client.py b/sdk/keyvault/azure-keyvault-keys/azure/keyvault/keys/client.py index dd5854d00533..57275b9ab954 100644 --- a/sdk/keyvault/azure-keyvault-keys/azure/keyvault/keys/client.py +++ b/sdk/keyvault/azure-keyvault-keys/azure/keyvault/keys/client.py @@ -25,6 +25,10 @@ class KeyClient(KeyVaultClientBase): """A high-level interface for managing a vault's keys. + :param credential: An object which can provide an access token for the vault, such as a credential from + :mod:`azure.identity` + :param str vault_url: URL of the vault the client will access + Example: .. literalinclude:: ../tests/test_samples_keys.py :start-after: [START create_key_client] diff --git a/sdk/keyvault/azure-keyvault-secrets/azure/keyvault/secrets/aio/client.py b/sdk/keyvault/azure-keyvault-secrets/azure/keyvault/secrets/aio/client.py index 2dbc6944adcc..7dbee4a3d421 100644 --- a/sdk/keyvault/azure-keyvault-secrets/azure/keyvault/secrets/aio/client.py +++ b/sdk/keyvault/azure-keyvault-secrets/azure/keyvault/secrets/aio/client.py @@ -14,7 +14,11 @@ class SecretClient(AsyncKeyVaultClientBase): - """A high-level interface for managing a vault's secrets. + """A high-level asynchronous interface for managing a vault's secrets. + + :param credential: An object which can provide an access token for the vault, such as a credential from + :mod:`azure.identity.aio` + :param str vault_url: URL of the vault the client will access Example: .. literalinclude:: ../tests/test_samples_secrets_async.py diff --git a/sdk/keyvault/azure-keyvault-secrets/azure/keyvault/secrets/client.py b/sdk/keyvault/azure-keyvault-secrets/azure/keyvault/secrets/client.py index 1246e49769cb..4b6ed8a78356 100644 --- a/sdk/keyvault/azure-keyvault-secrets/azure/keyvault/secrets/client.py +++ b/sdk/keyvault/azure-keyvault-secrets/azure/keyvault/secrets/client.py @@ -24,6 +24,10 @@ class SecretClient(KeyVaultClientBase): """A high-level interface for managing a vault's secrets. + :param credential: An object which can provide an access token for the vault, such as a credential from + :mod:`azure.identity` + :param str vault_url: URL of the vault the client will access + Example: .. literalinclude:: ../tests/test_samples_secrets.py :start-after: [START create_secret_client]