Skip to content
Merged
Show file tree
Hide file tree
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
Next Next commit
update shared code in azure-keyvault-secrets
  • Loading branch information
chlowell committed Aug 2, 2019
commit 0dd5e368884355106c10e5e0db9d9311e1133e50
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
# 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 .._user_agent import USER_AGENT


if TYPE_CHECKING:
Expand All @@ -22,14 +25,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(
Expand All @@ -39,6 +35,27 @@ 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)

# Override config policies if found in kwargs
if "user_agent_policy" in kwargs:
config.user_agent_policy = kwargs["user_agent_policy"]
if "headers_policy" in kwargs:
config.headers_policy = kwargs["headers_policy"]
if "proxy_policy" in kwargs:
config.proxy_policy = kwargs["proxy_policy"]
if "logging_policy" in kwargs:
config.logging_policy = kwargs["logging_policy"]
if "retry_policy" in kwargs:
config.retry_policy = kwargs["retry_policy"]
if "custom_hook_policy" in kwargs:
config.custom_hook_policy = kwargs["custom_hook_policy"]
if "redirect_policy" in kwargs:
config.redirect_policy = kwargs["redirect_policy"]

return config

def __init__(
Expand Down Expand Up @@ -86,6 +103,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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
# 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

if TYPE_CHECKING:
Expand All @@ -15,21 +18,14 @@
from azure.core.pipeline.transport import HttpTransport

from .challenge_auth_policy import ChallengeAuthPolicy
from azure.core.pipeline.policies.distributed_tracing import DistributedTracingPolicy
from .._user_agent 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):
Expand All @@ -38,6 +34,27 @@ 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)

# Override config policies if found in kwargs
if "user_agent_policy" in kwargs:
config.user_agent_policy = kwargs["user_agent_policy"]
if "headers_policy" in kwargs:
config.headers_policy = kwargs["headers_policy"]
if "proxy_policy" in kwargs:
config.proxy_policy = kwargs["proxy_policy"]
if "logging_policy" in kwargs:
config.logging_policy = kwargs["logging_policy"]
if "retry_policy" in kwargs:
config.retry_policy = kwargs["retry_policy"]
if "custom_hook_policy" in kwargs:
config.custom_hook_policy = kwargs["custom_hook_policy"]
if "redirect_policy" in kwargs:
config.redirect_policy = kwargs["redirect_policy"]

return config

def __init__(self, vault_url, credential, transport=None, api_version=None, **kwargs):
Expand Down