@@ -158,11 +158,25 @@ def set_secure(
158
158
-------
159
159
self
160
160
"""
161
+ self .__secure = True
161
162
self .__root_certificates = root_certificates
162
163
self .__private_key = private_key
163
164
self .__certificate_chain = certificate_chain
164
165
return self
165
166
167
+ def _set_insecure (self ):
168
+ """Sets the flag to use an insecure channel.
169
+ THIS IS AGAINST SPECIFICATION and should not
170
+ be used unless necessary and secure transport
171
+ is already well understood.
172
+
173
+ Returns
174
+ -------
175
+ self
176
+ """
177
+ self .__secure = False
178
+ return self
179
+
166
180
def set_secure_from_file (
167
181
self , root_certificates = None , private_key = None , certificate_chain = None
168
182
):
@@ -267,7 +281,7 @@ def set_channel_option(self, name, value):
267
281
self .__channel_options .append (new_option )
268
282
return self
269
283
270
- def construct (self ):
284
+ def construct (self , return_channel = False ):
271
285
"""Constructs and returns the desired Client object.
272
286
The instance of this class will reset to default values for further building.
273
287
@@ -276,36 +290,36 @@ def construct(self):
276
290
Client or NXClient or XEClient or XRClient
277
291
"""
278
292
channel = None
279
- channel_ssl_creds = None
280
- channel_metadata_creds = None
281
- channel_creds = None
282
- channel_ssl_creds = None
283
- if any ((self .__root_certificates , self .__private_key , self .__certificate_chain )):
293
+ if self .__secure :
294
+ LOGGER .debug ("Using secure channel." )
295
+ channel_metadata_creds = None
296
+ if self .__username and self .__password :
297
+ LOGGER .debug ("Using username/password call authentication." )
298
+ channel_metadata_creds = grpc .metadata_call_credentials (
299
+ CiscoAuthPlugin (self .__username , self .__password )
300
+ )
284
301
channel_ssl_creds = grpc .ssl_channel_credentials (
285
302
self .__root_certificates , self .__private_key , self .__certificate_chain
286
303
)
287
- if self .__username and self .__password :
288
- channel_metadata_creds = grpc .metadata_call_credentials (
289
- CiscoAuthPlugin (self .__username , self .__password )
290
- )
291
- logging .debug ("Using username/password call authentication." )
292
- if channel_ssl_creds and channel_metadata_creds :
293
- channel_creds = grpc .composite_channel_credentials (
294
- channel_ssl_creds , channel_metadata_creds
295
- )
296
- logging .debug ("Using SSL/metadata authentication composite credentials." )
297
- elif channel_ssl_creds :
298
- channel_creds = channel_ssl_creds
299
- logging .debug ("Using SSL credentials, no metadata authentication." )
300
- if channel_creds :
304
+ channel_creds = None
305
+ if channel_ssl_creds and channel_metadata_creds :
306
+ LOGGER .debug ("Using SSL/metadata authentication composite credentials." )
307
+ channel_creds = grpc .composite_channel_credentials (
308
+ channel_ssl_creds , channel_metadata_creds
309
+ )
310
+ else :
311
+ LOGGER .debug (
312
+ "Using SSL credentials, no channel metadata authentication."
313
+ )
314
+ channel_creds = channel_ssl_creds
301
315
if self .__ssl_target_name_override is not False :
302
316
if self .__ssl_target_name_override is None :
303
317
if not self .__root_certificates :
304
318
raise Exception ("Deriving override requires root certificate!" )
305
319
self .__ssl_target_name_override = get_cn_from_cert (
306
320
self .__root_certificates
307
321
)
308
- logging .warning (
322
+ LOGGER .warning (
309
323
"Overriding SSL option from certificate could increase MITM susceptibility!"
310
324
)
311
325
self .set_channel_option (
@@ -315,62 +329,28 @@ def construct(self):
315
329
self .__target_netloc .netloc , channel_creds , self .__channel_options
316
330
)
317
331
else :
332
+ LOGGER .warning (
333
+ "Insecure gRPC channel is against gNMI specification, personal data may be compromised."
334
+ )
318
335
channel = grpc .insecure_channel (self .__target_netloc .netloc )
319
336
if self .__client_class is None :
320
337
self .set_os ()
321
- client = self .__client_class (channel )
322
- self ._reset ()
323
- return client
324
-
325
- def save_construct (self ):
326
- """Constructs and returns the desired Client object.
327
- The instance of this class will reset to default values for further building.
328
-
329
- Returns
330
- -------
331
- Client or NXClient or XEClient or XRClient
332
- """
333
- channel = None
334
- channel_ssl_creds = None
335
- channel_metadata_creds = None
336
- channel_creds = None
337
- channel_ssl_creds = grpc .ssl_channel_credentials (
338
- self .__root_certificates , self .__private_key , self .__certificate_chain
339
- )
340
- if self .__username and self .__password :
341
- LOGGER .debug ("Using username/password call authentication." )
342
- channel_metadata_creds = grpc .metadata_call_credentials (
343
- CiscoAuthPlugin (self .__username , self .__password )
344
- )
345
- if channel_ssl_creds and channel_metadata_creds :
346
- LOGGER .debug ("Using SSL/metadata authentication composite credentials." )
347
- channel_creds = grpc .composite_channel_credentials (
348
- channel_ssl_creds , channel_metadata_creds
349
- )
338
+ client = None
339
+ if self .__secure :
340
+ client = self .__client_class (channel )
350
341
else :
351
- LOGGER .debug ("Using SSL credentials, no metadata authentication." )
352
- channel_creds = channel_ssl_creds
353
- if self .__ssl_target_name_override is not False :
354
- if self .__ssl_target_name_override is None :
355
- if not self .__root_certificates :
356
- raise Exception ("Deriving override requires root certificate!" )
357
- self .__ssl_target_name_override = get_cn_from_cert (
358
- self .__root_certificates
359
- )
360
- LOGGER .warning (
361
- "Overriding SSL option from certificate could increase MITM susceptibility!"
362
- )
363
- self .set_channel_option (
364
- "grpc.ssl_target_name_override" , self .__ssl_target_name_override
342
+ client = self .__client_class (
343
+ channel ,
344
+ default_call_metadata = [
345
+ ("username" , self .__username ),
346
+ ("password" , self .__password ),
347
+ ],
365
348
)
366
- channel = grpc .secure_channel (
367
- self .__target_netloc .netloc , channel_creds , self .__channel_options
368
- )
369
- if self .__client_class is None :
370
- self .set_os ()
371
- client = self .__client_class (channel )
372
349
self ._reset ()
373
- return client
350
+ if return_channel :
351
+ return client , channel
352
+ else :
353
+ return client
374
354
375
355
def _reset (self ):
376
356
"""Resets the builder.
@@ -388,4 +368,5 @@ def _reset(self):
388
368
self .__password = None
389
369
self .__channel_options = None
390
370
self .__ssl_target_name_override = False
371
+ self .__secure = True
391
372
return self
0 commit comments