4747)
4848
4949
50+ def _get_authentication_method (parameters ):
51+ auth_method = parameters .get ("authentication" , parameters .get ("method" ))
52+ if auth_method is not None :
53+ auth_method = auth_method .upper ()
54+ if auth_method == "AZURE_DEFAULT" :
55+ auth_method = None
56+ return auth_method
57+
58+
5059def _get_credential (parameters ):
5160 """
5261 Returns the appropriate credential given the input supplied by the original
5362 connect string.
5463 """
5564
5665 tokens = []
57- auth = parameters .get ("authentication" )
58- if auth is not None :
59- auth = auth .upper ()
60- if auth == "AZURE_DEFAULT" :
61- auth = None
66+ auth_method = _get_authentication_method (parameters )
6267
63- if auth is None or auth == "AZURE_SERVICE_PRINCIPAL" :
68+ if auth_method is None or auth_method == "AZURE_SERVICE_PRINCIPAL" :
6469 if "azure_client_secret" in parameters :
6570 tokens .append (
6671 ClientSecretCredential (
@@ -69,7 +74,7 @@ def _get_credential(parameters):
6974 _get_required_parameter (parameters , "azure_client_secret" ),
7075 )
7176 )
72- if "azure_client_certificate_path" in parameters :
77+ elif "azure_client_certificate_path" in parameters :
7378 tokens .append (
7479 CertificateCredential (
7580 _get_required_parameter (parameters , "azure_tenant_id" ),
@@ -79,25 +84,79 @@ def _get_credential(parameters):
7984 ),
8085 )
8186 )
82- if auth is None or auth == "AZURE_MANAGED_IDENTITY" :
87+ if auth_method is None or auth_method == "AZURE_MANAGED_IDENTITY" :
8388 client_id = parameters .get ("azure_managed_identity_client_id" )
8489 if client_id is not None :
8590 tokens .append (ManagedIdentityCredential (client_id = client_id ))
8691
8792 if len (tokens ) == 0 :
88- message = "Authentication options not available in Connection String"
93+ message = (
94+ "Authentication options were not available in Connection String"
95+ )
8996 raise Exception (message )
9097 elif len (tokens ) == 1 :
9198 return tokens [0 ]
9299 tokens .append (EnvironmentCredential ())
93100 return ChainedTokenCredential (* tokens )
94101
95102
96- def _get_required_parameter (parameters , name ):
103+ def _get_password (pwd_string , parameters ):
104+ try :
105+ pwd = json .loads (pwd_string )
106+ except json .JSONDecodeError :
107+ message = (
108+ "Password is expected to be JSON"
109+ " containing Azure Vault details."
110+ )
111+ raise Exception (message )
112+
113+ pwd ["value" ] = pwd .pop ("uri" )
114+ pwd ["type" ] = "azurevault"
115+
116+ # make authentication section
117+ pwd ["authentication" ] = authentication = {}
118+
119+ authentication ["method" ] = auth_method = _get_authentication_method (
120+ parameters
121+ )
122+
123+ if auth_method is None or auth_method == "AZURE_SERVICE_PRINCIPAL" :
124+ if "azure_client_secret" in parameters :
125+ authentication ["azure_tenant_id" ] = _get_required_parameter (
126+ parameters , "azure_tenant_id"
127+ )
128+ authentication ["azure_client_id" ] = _get_required_parameter (
129+ parameters , "azure_client_id"
130+ )
131+ authentication ["azure_client_secret" ] = _get_required_parameter (
132+ parameters , "azure_client_secret"
133+ )
134+
135+ elif "azure_client_certificate_path" in parameters :
136+ authentication ["azure_tenant_id" ] = (
137+ _get_required_parameter (parameters , "azure_tenant_id" ),
138+ )
139+ authentication ["azure_client_id" ] = (
140+ _get_required_parameter (parameters , "azure_client_id" ),
141+ )
142+ authentication ["azure_client_certificate_path" ] = (
143+ _get_required_parameter (
144+ parameters , "azure_client_certificate_path"
145+ )
146+ )
147+
148+ if auth_method is None or auth_method == "AZURE_MANAGED_IDENTITY" :
149+ authentication ["azure_managed_identity_client_id" ] = parameters .get (
150+ "azure_managed_identity_client_id"
151+ )
152+ return pwd
153+
154+
155+ def _get_required_parameter (parameters , name , location = "connection string" ):
97156 try :
98157 return parameters [name ]
99158 except KeyError :
100- message = f'Parameter named "{ name } " missing from connection string '
159+ message = f'Parameter named "{ name } " is missing from { location } '
101160 raise Exception (message ) from None
102161
103162
@@ -134,7 +193,7 @@ def _parse_parameters(protocol_arg: str) -> dict:
134193
135194
136195def password_type_azure_vault_hook (args ):
137- uri = _get_required_parameter (args , "uri" )
196+ uri = _get_required_parameter (args , "value" , '"password" key section' )
138197 credential = args .get ("credential" )
139198
140199 if credential is None :
@@ -144,7 +203,7 @@ def password_type_azure_vault_hook(args):
144203 auth = args .get ("authentication" )
145204 if auth is None :
146205 raise Exception (
147- "Azure Vault authentication details are not provided."
206+ "Azure Vault authentication details were not provided."
148207 )
149208 credential = _get_credential (auth )
150209
@@ -182,17 +241,8 @@ def _process_config(parameters, connect_params):
182241 config ["user" ] = _get_setting (client , key , "user" , label , required = False )
183242 pwd = _get_setting (client , key , "password" , label , required = False )
184243 if pwd is not None :
185- try :
186- pwd = json .loads (pwd )
187- pwd ["type" ] = "azure-vault"
188- pwd ["credential" ] = credential
189- except json .JSONDecodeError :
190- message = (
191- "Password is expected to be JSON"
192- " containing Azure Vault details."
193- )
194- raise Exception (message )
195- config ["password" ] = pwd
244+ config ["password" ] = _get_password (pwd , parameters )
245+
196246 config ["config_time_to_live" ] = _get_setting (
197247 client , key , "config_time_to_live" , label , required = False
198248 )
@@ -217,5 +267,5 @@ def config_azure_hook(protocol, protocol_arg, connect_params):
217267 _process_config (parameters , connect_params )
218268
219269
220- oracledb .register_password_type ("azure-vault " , password_type_azure_vault_hook )
270+ oracledb .register_password_type ("azurevault " , password_type_azure_vault_hook )
221271oracledb .register_protocol ("config-azure" , config_azure_hook )
0 commit comments