@@ -158,11 +158,25 @@ def set_secure(
158158 -------
159159 self
160160 """
161+ self .__secure = True
161162 self .__root_certificates = root_certificates
162163 self .__private_key = private_key
163164 self .__certificate_chain = certificate_chain
164165 return self
165166
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+
166180 def set_secure_from_file (
167181 self , root_certificates = None , private_key = None , certificate_chain = None
168182 ):
@@ -276,44 +290,62 @@ def construct(self):
276290 Client or NXClient or XEClient or XRClient
277291 """
278292 channel = None
279- channel_ssl_creds = None
280- channel_metadata_creds = None
281- channel_creds = None
282- channel_ssl_creds = grpc .ssl_channel_credentials (
283- self .__root_certificates , self .__private_key , self .__certificate_chain
284- )
285- if self .__username and self .__password :
286- LOGGER .debug ("Using username/password call authentication." )
287- channel_metadata_creds = grpc .metadata_call_credentials (
288- CiscoAuthPlugin (self .__username , self .__password )
289- )
290- if channel_ssl_creds and channel_metadata_creds :
291- LOGGER .debug ("Using SSL/metadata authentication composite credentials." )
292- channel_creds = grpc .composite_channel_credentials (
293- channel_ssl_creds , channel_metadata_creds
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+ )
301+ channel_ssl_creds = grpc .ssl_channel_credentials (
302+ self .__root_certificates , self .__private_key , self .__certificate_chain
294303 )
295- else :
296- LOGGER . debug ( "Using SSL credentials, no metadata authentication." )
297- channel_creds = channel_ssl_creds
298- if self . __ssl_target_name_override is not False :
299- if self . __ssl_target_name_override is None :
300- if not self . __root_certificates :
301- raise Exception ( "Deriving override requires root certificate!" )
302- self . __ssl_target_name_override = get_cn_from_cert (
303- self . __root_certificates
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."
304313 )
305- LOGGER .warning (
306- "Overriding SSL option from certificate could increase MITM susceptibility!"
314+ channel_creds = channel_ssl_creds
315+ if self .__ssl_target_name_override is not False :
316+ if self .__ssl_target_name_override is None :
317+ if not self .__root_certificates :
318+ raise Exception ("Deriving override requires root certificate!" )
319+ self .__ssl_target_name_override = get_cn_from_cert (
320+ self .__root_certificates
321+ )
322+ LOGGER .warning (
323+ "Overriding SSL option from certificate could increase MITM susceptibility!"
324+ )
325+ self .set_channel_option (
326+ "grpc.ssl_target_name_override" , self .__ssl_target_name_override
307327 )
308- self . set_channel_option (
309- "grpc.ssl_target_name_override" , self .__ssl_target_name_override
328+ channel = grpc . secure_channel (
329+ self . __target_netloc . netloc , channel_creds , self .__channel_options
310330 )
311- channel = grpc .secure_channel (
312- self .__target_netloc .netloc , channel_creds , self .__channel_options
313- )
331+ else :
332+ LOGGER .warning (
333+ "Insecure gRPC channel is against gNMI specification, personal data may be compromised."
334+ )
335+ channel = grpc .insecure_channel (self .__target_netloc .netloc )
314336 if self .__client_class is None :
315337 self .set_os ()
316- client = self .__client_class (channel )
338+ client = None
339+ if self .__secure :
340+ client = self .__client_class (channel )
341+ else :
342+ client = self .__client_class (
343+ channel ,
344+ default_call_metadata = [
345+ ("username" , self .__username ),
346+ ("password" , self .__password ),
347+ ],
348+ )
317349 self ._reset ()
318350 return client
319351
@@ -333,4 +365,5 @@ def _reset(self):
333365 self .__password = None
334366 self .__channel_options = None
335367 self .__ssl_target_name_override = False
368+ self .__secure = True
336369 return self
0 commit comments