55
66import datetime
77import ipaddress
8+ import json
89import os
910import re
1011import sys
115116 PORT_INSTANCE_ERROR
116117}
117118
119+ SECRETS_PATH = "/etc/sonic/grpc_secrets.json"
120+
118121def format_mapping_identifier (string ):
119122 """
120123 Takes an arbitrary string and creates a valid entity for port mapping file.
@@ -369,26 +372,64 @@ def retry_setup_grpc_channel_for_port(port, asic_index):
369372 grpc_port_stubs [port ] = stub
370373 return True
371374
375+ def apply_grpc_secrets_configuration (SECRETS_PATH ):
376+
377+
378+ f = open (SECRETS_PATH , 'rb' )
379+ parsed_data = json .load (f )
380+
381+ config_db , grpc_config = {}, {}
382+ namespaces = multi_asic .get_front_end_namespaces ()
383+ for namespace in namespaces :
384+ asic_id = multi_asic .get_asic_index_from_namespace (namespace )
385+ config_db [asic_id ] = daemon_base .db_connect ("CONFIG_DB" , namespace )
386+ grpc_config [asic_id ] = swsscommon .Table (config_db [asic_id ], "GRPCCLIENT" )
387+
388+
389+ asic_index = multi_asic .get_asic_index_from_namespace (DEFAULT_NAMESPACE )
390+ grpc_client_config = parsed_data .get ("GRPCCLIENT" , None )
391+ if grpc_client_config is not None :
392+ config = grpc_client_config .get ("config" , None )
393+ if config is not None :
394+ type = config .get ("type" ,None )
395+ auth_level = config .get ("auth_level" ,None )
396+ log_level = config .get ("log_level" , None )
397+ fvs_updated = swsscommon .FieldValuePairs ([('type' , type ),
398+ ('auth_level' ,auth_level ),
399+ ('log_level' ,log_level )])
400+ grpc_config [asic_index ].set ('config' , fvs_updated )
401+ certs = grpc_client_config .get ("certs" , None )
402+ if certs is not None :
403+ client_crt = certs .get ("client_crt" , None )
404+ client_key = certs .get ("client_key" , None )
405+ ca_crt = certs .get ("ca_crt" , None )
406+ grpc_ssl_credential = certs .get ("grpc_ssl_credential" ,None )
407+ fvs_updated = swsscommon .FieldValuePairs ([('client_crt' , client_crt ),
408+ ('client_key' , client_key ),
409+ ('grpc_ssl_credential' , grpc_ssl_credential ),
410+ ('ca_crt' ,ca_crt )])
411+ grpc_config [asic_index ].set ('certs' , fvs_updated )
412+
372413
373414def get_grpc_credentials (type , kvp ):
374415
375416 root_file = kvp .get ("ca_crt" , None )
376- if root_file is not None :
417+ if root_file is not None and os . path . isfile ( root_file ) :
377418 root_cert = open (root_file , 'rb' ).read ()
378419 else :
379420 helper_logger .log_error ("grpc credential channel setup no root file in config_db" )
380421 return None
381422
382423 if type == "mutual" :
383424 cert_file = kvp .get ("client_crt" , None )
384- if cert_file is not None :
425+ if cert_file is not None and os . path . isfile ( cert_file ) :
385426 cert_chain = open (cert_file , 'rb' ).read ()
386427 else :
387428 helper_logger .log_error ("grpc credential channel setup no cert file for mutual authentication in config_db" )
388429 return None
389430
390431 key_file = kvp .get ("client_key" , None )
391- if key_file is not None :
432+ if key_file is not None and os . path . isfile ( key_file ) :
392433 key = open (key_file , 'rb' ).read ()
393434 else :
394435 helper_logger .log_error ("grpc credential channel setup no key file for mutual authentication in config_db" )
@@ -695,6 +736,8 @@ def setup_grpc_channels(stop_event):
695736
696737 if read_side == - 1 :
697738 read_side = process_loopback_interface_and_get_read_side (loopback_keys )
739+ if os .path .isfile (SECRETS_PATH ):
740+ apply_grpc_secrets_configuration (SECRETS_PATH )
698741
699742 helper_logger .log_debug ("Y_CABLE_DEBUG:while setting up grpc channels read side = {}" .format (read_side ))
700743
@@ -1377,6 +1420,8 @@ def init_ports_status_for_y_cable(platform_sfp, platform_chassis, y_cable_presen
13771420
13781421 if read_side == - 1 :
13791422 read_side = process_loopback_interface_and_get_read_side (loopback_keys )
1423+ if os .path .isfile (SECRETS_PATH ):
1424+ apply_grpc_secrets_configuration (SECRETS_PATH )
13801425
13811426 # Init PORT_STATUS table if ports are on Y cable
13821427 logical_port_list = y_cable_platform_sfputil .logical
@@ -1439,6 +1484,8 @@ def change_ports_status_for_y_cable_change_event(port_dict, y_cable_presence, st
14391484
14401485 if read_side == - 1 :
14411486 read_side = process_loopback_interface_and_get_read_side (loopback_keys )
1487+ if os .path .isfile (SECRETS_PATH ):
1488+ apply_grpc_secrets_configuration (SECRETS_PATH )
14421489
14431490
14441491 # Init PORT_STATUS table if ports are on Y cable and an event is received
@@ -1500,6 +1547,7 @@ def delete_ports_status_for_y_cable():
15001547 state_db , config_db , port_tbl , y_cable_tbl = {}, {}, {}, {}
15011548 y_cable_tbl_keys = {}
15021549 static_tbl , mux_tbl = {}, {}
1550+ grpc_config = {}
15031551 namespaces = multi_asic .get_front_end_namespaces ()
15041552 for namespace in namespaces :
15051553 asic_id = multi_asic .get_asic_index_from_namespace (namespace )
@@ -1513,6 +1561,14 @@ def delete_ports_status_for_y_cable():
15131561 mux_tbl [asic_id ] = swsscommon .Table (
15141562 state_db [asic_id ], MUX_CABLE_INFO_TABLE )
15151563 port_tbl [asic_id ] = swsscommon .Table (config_db [asic_id ], "MUX_CABLE" )
1564+ grpc_config [asic_id ] = swsscommon .Table (config_db [asic_id ], "GRPCCLIENT" )
1565+
1566+
1567+ if read_side != - 1 :
1568+ asic_index = multi_asic .get_asic_index_from_namespace (DEFAULT_NAMESPACE )
1569+ if os .path .isfile (SECRETS_PATH ):
1570+ grpc_config [asic_index ]._del ("config" )
1571+ grpc_config [asic_index ]._del ("certs" )
15161572
15171573 # delete PORTS on Y cable table if ports on Y cable
15181574 logical_port_list = y_cable_platform_sfputil .logical
0 commit comments