5
5
6
6
import datetime
7
7
import ipaddress
8
+ import json
8
9
import os
9
10
import re
10
11
import sys
115
116
PORT_INSTANCE_ERROR
116
117
}
117
118
119
+ SECRETS_PATH = "/etc/sonic/grpc_secrets.json"
120
+
118
121
def format_mapping_identifier (string ):
119
122
"""
120
123
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):
369
372
grpc_port_stubs [port ] = stub
370
373
return True
371
374
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
+
372
413
373
414
def get_grpc_credentials (type , kvp ):
374
415
375
416
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 ) :
377
418
root_cert = open (root_file , 'rb' ).read ()
378
419
else :
379
420
helper_logger .log_error ("grpc credential channel setup no root file in config_db" )
380
421
return None
381
422
382
423
if type == "mutual" :
383
424
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 ) :
385
426
cert_chain = open (cert_file , 'rb' ).read ()
386
427
else :
387
428
helper_logger .log_error ("grpc credential channel setup no cert file for mutual authentication in config_db" )
388
429
return None
389
430
390
431
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 ) :
392
433
key = open (key_file , 'rb' ).read ()
393
434
else :
394
435
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):
695
736
696
737
if read_side == - 1 :
697
738
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 )
698
741
699
742
helper_logger .log_debug ("Y_CABLE_DEBUG:while setting up grpc channels read side = {}" .format (read_side ))
700
743
@@ -1377,6 +1420,8 @@ def init_ports_status_for_y_cable(platform_sfp, platform_chassis, y_cable_presen
1377
1420
1378
1421
if read_side == - 1 :
1379
1422
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 )
1380
1425
1381
1426
# Init PORT_STATUS table if ports are on Y cable
1382
1427
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
1439
1484
1440
1485
if read_side == - 1 :
1441
1486
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 )
1442
1489
1443
1490
1444
1491
# 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():
1500
1547
state_db , config_db , port_tbl , y_cable_tbl = {}, {}, {}, {}
1501
1548
y_cable_tbl_keys = {}
1502
1549
static_tbl , mux_tbl = {}, {}
1550
+ grpc_config = {}
1503
1551
namespaces = multi_asic .get_front_end_namespaces ()
1504
1552
for namespace in namespaces :
1505
1553
asic_id = multi_asic .get_asic_index_from_namespace (namespace )
@@ -1513,6 +1561,14 @@ def delete_ports_status_for_y_cable():
1513
1561
mux_tbl [asic_id ] = swsscommon .Table (
1514
1562
state_db [asic_id ], MUX_CABLE_INFO_TABLE )
1515
1563
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" )
1516
1572
1517
1573
# delete PORTS on Y cable table if ports on Y cable
1518
1574
logical_port_list = y_cable_platform_sfputil .logical
0 commit comments