From 5b37ee6cd705198e34e5e2c6ca9a5c965cd4d86b Mon Sep 17 00:00:00 2001 From: siqbal1986 Date: Tue, 29 Oct 2024 13:01:04 -0700 Subject: [PATCH] Vnet_route_check TCP socket for DB connection. (#3578) Currently the Vnet_route_check fails if a user calls it witout sudo with the following error. ``` Traceback (most recent call last): File "/usr/local/bin/vnet_route_check.py", line 401, in sys.exit(main()) File "/usr/local/bin/vnet_route_check.py", line 364, in main if not check_vnet_cfg(): File "/usr/local/bin/vnet_route_check.py", line 77, in check_vnet_cfg db = swsscommon.DBConnector('APPL_DB', 0) File "/usr/lib/python3/dist-packages/swsscommon/swsscommon.py", line 1656, in __init__ _swsscommon.DBConnector_swiginit(self, _swsscommon.new_DBConnector(*args)) RuntimeError: Unable to connect to redis (unix-socket): Cannot assign requested address ``` #### What I did The **route_check** script accesses the same DB tables but is able to run without the sudo rights. To solve this problem I have changed the **Vnet_route_check** to use a TCP socket to connect to the DB as done in **route_check**. As a result the script doesn't fail with a run time error. #### How I did it #### How to verify it create a new user on a T1 device which has no docker or sudoers privilage. run vnet_route check. it should fail. #### Previous command output (if the output of a command-line utility has changed) ``` Traceback (most recent call last): File "/usr/local/bin/vnet_route_check.py", line 401, in sys.exit(main()) File "/usr/local/bin/vnet_route_check.py", line 364, in main if not check_vnet_cfg(): File "/usr/local/bin/vnet_route_check.py", line 77, in check_vnet_cfg db = swsscommon.DBConnector('APPL_DB', 0) File "/usr/lib/python3/dist-packages/swsscommon/swsscommon.py", line 1656, in __init__ _swsscommon.DBConnector_swiginit(self, _swsscommon.new_DBConnector(*args)) RuntimeError: Unable to connect to redis (unix-socket): Cannot assign requested address ``` --- scripts/vnet_route_check.py | 16 ++++++++-------- tests/vnet_route_check_test.py | 4 +++- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/scripts/vnet_route_check.py b/scripts/vnet_route_check.py index d925427d40..c747bf7efb 100755 --- a/scripts/vnet_route_check.py +++ b/scripts/vnet_route_check.py @@ -74,7 +74,7 @@ def print_message(lvl, *args): def check_vnet_cfg(): ''' Returns True if VNET is configured in APP_DB or False if no VNET configuration. ''' - db = swsscommon.DBConnector('APPL_DB', 0) + db = swsscommon.DBConnector('APPL_DB', 0, True) vnet_db_keys = swsscommon.Table(db, 'VNET_TABLE').getKeys() @@ -85,7 +85,7 @@ def get_vnet_intfs(): ''' Returns dictionary of VNETs and related VNET interfaces. Format: { : [ ] } ''' - db = swsscommon.DBConnector('APPL_DB', 0) + db = swsscommon.DBConnector('APPL_DB', 0, True) intfs_table = swsscommon.Table(db, 'INTF_TABLE') intfs_keys = swsscommon.Table(db, 'INTF_TABLE').getKeys() @@ -109,7 +109,7 @@ def get_all_rifs_oids(): ''' Returns dictionary of all router interfaces and their OIDs. Format: { : } ''' - db = swsscommon.DBConnector('COUNTERS_DB', 0) + db = swsscommon.DBConnector('COUNTERS_DB', 0, True) rif_table = swsscommon.Table(db, 'COUNTERS_RIF_NAME_MAP') rif_name_oid_map = dict(rif_table.get('')[1]) @@ -140,7 +140,7 @@ def get_vrf_entries(): ''' Returns dictionary of VNET interfaces and corresponding VRF OIDs. Format: { : } ''' - db = swsscommon.DBConnector('ASIC_DB', 0) + db = swsscommon.DBConnector('ASIC_DB', 0, True) rif_table = swsscommon.Table(db, 'ASIC_STATE') vnet_rifs_oids = get_vnet_rifs_oids() @@ -162,7 +162,7 @@ def filter_out_vnet_ip2me_routes(vnet_routes): ''' Filters out IP2ME routes from the provided dictionary with VNET routes Format: { : { 'routes': [ ], 'vrf_oid': } } ''' - db = swsscommon.DBConnector('APPL_DB', 0) + db = swsscommon.DBConnector('APPL_DB', 0, True) all_rifs_db_keys = swsscommon.Table(db, 'INTF_TABLE').getKeys() vnet_intfs = get_vnet_intfs() @@ -198,7 +198,7 @@ def get_vnet_routes_from_app_db(): ''' Returns dictionary of VNET routes configured per each VNET in APP_DB. Format: { : { 'routes': [ ], 'vrf_oid': } } ''' - db = swsscommon.DBConnector('APPL_DB', 0) + db = swsscommon.DBConnector('APPL_DB', 0, True) vnet_intfs = get_vnet_intfs() vnet_vrfs = get_vrf_entries() @@ -245,7 +245,7 @@ def get_vnet_routes_from_asic_db(): ''' Returns dictionary of VNET routes configured per each VNET in ASIC_DB. Format: { : { 'routes': [ ], 'vrf_oid': } } ''' - db = swsscommon.DBConnector('ASIC_DB', 0) + db = swsscommon.DBConnector('ASIC_DB', 0, True) tbl = swsscommon.Table(db, 'ASIC_STATE') @@ -363,7 +363,7 @@ def main(): # Don't run VNET routes consistancy logic if there is no VNET configuration if not check_vnet_cfg(): return rc - asic_db = swsscommon.DBConnector('ASIC_DB', 0) + asic_db = swsscommon.DBConnector('ASIC_DB', 0, True) virtual_router = swsscommon.Table(asic_db, 'ASIC_STATE:SAI_OBJECT_TYPE_VIRTUAL_ROUTER') if virtual_router.getKeys() != []: global default_vrf_oid diff --git a/tests/vnet_route_check_test.py b/tests/vnet_route_check_test.py index 092a89e2f9..10d97f21a3 100644 --- a/tests/vnet_route_check_test.py +++ b/tests/vnet_route_check_test.py @@ -341,7 +341,9 @@ def get(self, key): db_conns = {"APPL_DB": APPL_DB, "ASIC_DB": ASIC_DB, "COUNTERS_DB": CNTR_DB} -def conn_side_effect(arg, _): + + +def conn_side_effect(arg, _, __): return db_conns[arg]