47
47
)
48
48
49
49
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
+
50
59
def _get_credential (parameters ):
51
60
"""
52
61
Returns the appropriate credential given the input supplied by the original
53
62
connect string.
54
63
"""
55
64
56
65
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 )
62
67
63
- if auth is None or auth == "AZURE_SERVICE_PRINCIPAL" :
68
+ if auth_method is None or auth_method == "AZURE_SERVICE_PRINCIPAL" :
64
69
if "azure_client_secret" in parameters :
65
70
tokens .append (
66
71
ClientSecretCredential (
@@ -69,7 +74,7 @@ def _get_credential(parameters):
69
74
_get_required_parameter (parameters , "azure_client_secret" ),
70
75
)
71
76
)
72
- if "azure_client_certificate_path" in parameters :
77
+ elif "azure_client_certificate_path" in parameters :
73
78
tokens .append (
74
79
CertificateCredential (
75
80
_get_required_parameter (parameters , "azure_tenant_id" ),
@@ -79,25 +84,79 @@ def _get_credential(parameters):
79
84
),
80
85
)
81
86
)
82
- if auth is None or auth == "AZURE_MANAGED_IDENTITY" :
87
+ if auth_method is None or auth_method == "AZURE_MANAGED_IDENTITY" :
83
88
client_id = parameters .get ("azure_managed_identity_client_id" )
84
89
if client_id is not None :
85
90
tokens .append (ManagedIdentityCredential (client_id = client_id ))
86
91
87
92
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
+ )
89
96
raise Exception (message )
90
97
elif len (tokens ) == 1 :
91
98
return tokens [0 ]
92
99
tokens .append (EnvironmentCredential ())
93
100
return ChainedTokenCredential (* tokens )
94
101
95
102
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" ):
97
156
try :
98
157
return parameters [name ]
99
158
except KeyError :
100
- message = f'Parameter named "{ name } " missing from connection string '
159
+ message = f'Parameter named "{ name } " is missing from { location } '
101
160
raise Exception (message ) from None
102
161
103
162
@@ -134,7 +193,7 @@ def _parse_parameters(protocol_arg: str) -> dict:
134
193
135
194
136
195
def password_type_azure_vault_hook (args ):
137
- uri = _get_required_parameter (args , "uri" )
196
+ uri = _get_required_parameter (args , "value" , '"password" key section' )
138
197
credential = args .get ("credential" )
139
198
140
199
if credential is None :
@@ -144,7 +203,7 @@ def password_type_azure_vault_hook(args):
144
203
auth = args .get ("authentication" )
145
204
if auth is None :
146
205
raise Exception (
147
- "Azure Vault authentication details are not provided."
206
+ "Azure Vault authentication details were not provided."
148
207
)
149
208
credential = _get_credential (auth )
150
209
@@ -182,17 +241,8 @@ def _process_config(parameters, connect_params):
182
241
config ["user" ] = _get_setting (client , key , "user" , label , required = False )
183
242
pwd = _get_setting (client , key , "password" , label , required = False )
184
243
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
+
196
246
config ["config_time_to_live" ] = _get_setting (
197
247
client , key , "config_time_to_live" , label , required = False
198
248
)
@@ -217,5 +267,5 @@ def config_azure_hook(protocol, protocol_arg, connect_params):
217
267
_process_config (parameters , connect_params )
218
268
219
269
220
- oracledb .register_password_type ("azure-vault " , password_type_azure_vault_hook )
270
+ oracledb .register_password_type ("azurevault " , password_type_azure_vault_hook )
221
271
oracledb .register_protocol ("config-azure" , config_azure_hook )
0 commit comments