Skip to content

Commit 5b37ee6

Browse files
authored
Vnet_route_check TCP socket for DB connection. (sonic-net#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 <module> 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 <module> 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 ```
1 parent d64a90a commit 5b37ee6

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

scripts/vnet_route_check.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def print_message(lvl, *args):
7474
def check_vnet_cfg():
7575
''' Returns True if VNET is configured in APP_DB or False if no VNET configuration.
7676
'''
77-
db = swsscommon.DBConnector('APPL_DB', 0)
77+
db = swsscommon.DBConnector('APPL_DB', 0, True)
7878

7979
vnet_db_keys = swsscommon.Table(db, 'VNET_TABLE').getKeys()
8080

@@ -85,7 +85,7 @@ def get_vnet_intfs():
8585
''' Returns dictionary of VNETs and related VNET interfaces.
8686
Format: { <vnet_name>: [ <vnet_rif_name> ] }
8787
'''
88-
db = swsscommon.DBConnector('APPL_DB', 0)
88+
db = swsscommon.DBConnector('APPL_DB', 0, True)
8989

9090
intfs_table = swsscommon.Table(db, 'INTF_TABLE')
9191
intfs_keys = swsscommon.Table(db, 'INTF_TABLE').getKeys()
@@ -109,7 +109,7 @@ def get_all_rifs_oids():
109109
''' Returns dictionary of all router interfaces and their OIDs.
110110
Format: { <rif_name>: <rif_oid> }
111111
'''
112-
db = swsscommon.DBConnector('COUNTERS_DB', 0)
112+
db = swsscommon.DBConnector('COUNTERS_DB', 0, True)
113113
rif_table = swsscommon.Table(db, 'COUNTERS_RIF_NAME_MAP')
114114

115115
rif_name_oid_map = dict(rif_table.get('')[1])
@@ -140,7 +140,7 @@ def get_vrf_entries():
140140
''' Returns dictionary of VNET interfaces and corresponding VRF OIDs.
141141
Format: { <vnet_rif_name>: <vrf_oid> }
142142
'''
143-
db = swsscommon.DBConnector('ASIC_DB', 0)
143+
db = swsscommon.DBConnector('ASIC_DB', 0, True)
144144
rif_table = swsscommon.Table(db, 'ASIC_STATE')
145145

146146
vnet_rifs_oids = get_vnet_rifs_oids()
@@ -162,7 +162,7 @@ def filter_out_vnet_ip2me_routes(vnet_routes):
162162
''' Filters out IP2ME routes from the provided dictionary with VNET routes
163163
Format: { <vnet_name>: { 'routes': [ <pfx/pfx_len> ], 'vrf_oid': <oid> } }
164164
'''
165-
db = swsscommon.DBConnector('APPL_DB', 0)
165+
db = swsscommon.DBConnector('APPL_DB', 0, True)
166166

167167
all_rifs_db_keys = swsscommon.Table(db, 'INTF_TABLE').getKeys()
168168
vnet_intfs = get_vnet_intfs()
@@ -198,7 +198,7 @@ def get_vnet_routes_from_app_db():
198198
''' Returns dictionary of VNET routes configured per each VNET in APP_DB.
199199
Format: { <vnet_name>: { 'routes': [ <pfx/pfx_len> ], 'vrf_oid': <oid> } }
200200
'''
201-
db = swsscommon.DBConnector('APPL_DB', 0)
201+
db = swsscommon.DBConnector('APPL_DB', 0, True)
202202

203203
vnet_intfs = get_vnet_intfs()
204204
vnet_vrfs = get_vrf_entries()
@@ -245,7 +245,7 @@ def get_vnet_routes_from_asic_db():
245245
''' Returns dictionary of VNET routes configured per each VNET in ASIC_DB.
246246
Format: { <vnet_name>: { 'routes': [ <pfx/pfx_len> ], 'vrf_oid': <oid> } }
247247
'''
248-
db = swsscommon.DBConnector('ASIC_DB', 0)
248+
db = swsscommon.DBConnector('ASIC_DB', 0, True)
249249

250250
tbl = swsscommon.Table(db, 'ASIC_STATE')
251251

@@ -363,7 +363,7 @@ def main():
363363
# Don't run VNET routes consistancy logic if there is no VNET configuration
364364
if not check_vnet_cfg():
365365
return rc
366-
asic_db = swsscommon.DBConnector('ASIC_DB', 0)
366+
asic_db = swsscommon.DBConnector('ASIC_DB', 0, True)
367367
virtual_router = swsscommon.Table(asic_db, 'ASIC_STATE:SAI_OBJECT_TYPE_VIRTUAL_ROUTER')
368368
if virtual_router.getKeys() != []:
369369
global default_vrf_oid

tests/vnet_route_check_test.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,9 @@ def get(self, key):
341341

342342

343343
db_conns = {"APPL_DB": APPL_DB, "ASIC_DB": ASIC_DB, "COUNTERS_DB": CNTR_DB}
344-
def conn_side_effect(arg, _):
344+
345+
346+
def conn_side_effect(arg, _, __):
345347
return db_conns[arg]
346348

347349

0 commit comments

Comments
 (0)