3
3
# Licensed under the MIT License. See LICENSE.txt in the project root for
4
4
# license information.
5
5
# --------------------------------------------------------------------------
6
- from azure .core .pipeline .policies import HTTPPolicy
6
+ from collections import namedtuple
7
+ from typing import TYPE_CHECKING
8
+
9
+ if TYPE_CHECKING :
10
+ # pylint:disable=unused-import
11
+ from typing import Any , Mapping , Optional
12
+ from azure .core .credentials import TokenCredential
13
+ from azure .core .pipeline .transport import HttpTransport
7
14
8
15
try :
9
16
import urllib .parse as parse
10
17
except ImportError :
11
18
import urlparse as parse # pylint: disable=import-error
12
19
13
- from collections import namedtuple
20
+ from azure .core import Configuration
21
+ from azure .core .pipeline import Pipeline
22
+ from azure .core .pipeline .policies import BearerTokenCredentialPolicy , HTTPPolicy
23
+ from azure .core .pipeline .transport import RequestsTransport
24
+
25
+ from ._generated import KeyVaultClient
14
26
15
27
16
28
_VaultId = namedtuple ("VaultId" , ["vault_url" , "collection" , "name" , "version" ])
17
29
18
30
31
+ KEY_VAULT_SCOPES = ("https://vault.azure.net/.default" ,)
32
+
33
+
19
34
def _parse_vault_id (url ):
20
35
try :
21
36
parsed_uri = parse .urlparse (url )
@@ -37,13 +52,67 @@ def _parse_vault_id(url):
37
52
)
38
53
39
54
40
- # TODO: integrate with azure.core
41
- class _BearerTokenCredentialPolicy (HTTPPolicy ):
42
- def __init__ (self , credentials ):
43
- self ._credentials = credentials
55
+ class _KeyVaultClientBase (object ):
56
+ """
57
+ :param credential: A credential or credential provider which can be used to authenticate to the vault,
58
+ a ValueError will be raised if the entity is not provided
59
+ :type credential: azure.core.credentials.TokenCredential
60
+ :param str vault_url: The url of the vault to which the client will connect,
61
+ a ValueError will be raised if the entity is not provided
62
+ :param ~azure.core.configuration.Configuration config: The configuration for the KeyClient
63
+ """
64
+
65
+ @staticmethod
66
+ def create_config (credential , api_version = None , ** kwargs ):
67
+ # type: (TokenCredential, Optional[str], Mapping[str, Any]) -> Configuration
68
+ if api_version is None :
69
+ api_version = KeyVaultClient .DEFAULT_API_VERSION
70
+ config = KeyVaultClient .get_configuration_class (api_version , aio = False )(credential , ** kwargs )
71
+ config .authentication_policy = BearerTokenCredentialPolicy (credential , scopes = KEY_VAULT_SCOPES )
72
+ return config
73
+
74
+ def __init__ (self , vault_url , credential , config = None , transport = None , api_version = None , ** kwargs ):
75
+ # type: (str, TokenCredential, Configuration, Optional[HttpTransport], Optional[str], **Any) -> None
76
+ if not credential :
77
+ raise ValueError (
78
+ "credential should be an object supporting the TokenCredential protocol, such as a credential from azure-identity"
79
+ )
80
+ if not vault_url :
81
+ raise ValueError ("vault_url must be the URL of an Azure Key Vault" )
82
+
83
+ self ._vault_url = vault_url .strip (" /" )
84
+
85
+ client = kwargs .pop ("generated_client" , None )
86
+ if client :
87
+ # caller provided a configured client -> nothing left to initialize
88
+ self ._client = client
89
+ return
90
+
91
+ if api_version is None :
92
+ api_version = KeyVaultClient .DEFAULT_API_VERSION
93
+
94
+ config = config or self .create_config (credential , api_version = api_version , ** kwargs )
95
+ pipeline = kwargs .pop ("pipeline" , None ) or self ._build_pipeline (config , transport )
96
+ self ._client = KeyVaultClient (credential , api_version = api_version , pipeline = pipeline , aio = False , ** kwargs )
97
+
98
+ def _build_pipeline (self , config , transport ):
99
+ # type: (Configuration, HttpTransport) -> Pipeline
100
+ policies = [
101
+ config .headers_policy ,
102
+ config .user_agent_policy ,
103
+ config .proxy_policy ,
104
+ config .redirect_policy ,
105
+ config .retry_policy ,
106
+ config .authentication_policy ,
107
+ config .logging_policy ,
108
+ ]
109
+
110
+ if transport is None :
111
+ transport = RequestsTransport (config )
44
112
45
- def send (self , request , ** kwargs ):
46
- auth_header = "Bearer " + self ._credentials .token ["access_token" ]
47
- request .http_request .headers ["Authorization" ] = auth_header
113
+ return Pipeline (transport , policies = policies )
48
114
49
- return self .next .send (request , ** kwargs )
115
+ @property
116
+ def vault_url (self ):
117
+ # type: () -> str
118
+ return self ._vault_url
0 commit comments