Skip to content

Commit 7339dc0

Browse files
committed
Add Redis ACL support
1 parent 1109e49 commit 7339dc0

File tree

4 files changed

+38
-2
lines changed

4 files changed

+38
-2
lines changed

Diff for: src/swsssdk/__init__.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010

1111
if ('unittest' not in sys.modules.keys() and
1212
'mockredis' not in sys.modules.keys() and
13-
'mock' not in sys.modules.keys()):
13+
'mock' not in sys.modules.keys() and
14+
'netq_agent' not in sys.modules.keys()):
15+
# netq_agent temporary fix until move to swsscommon lib.
1416
msg = "sonic-py-swsssdk been deprecated, please switch to sonic-swss-common."
1517
logger.exception(msg)
1618
raise ImportError("sonic-py-swsssdk been deprecated, please switch to sonic-swss-common.")

Diff for: src/swsssdk/interface.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import time
22
from functools import wraps
3+
from . import util
34

45
import redis
56
from redis import RedisError
@@ -143,6 +144,8 @@ class DBInterface(object):
143144
ACS Redis db mainly uses hash, therefore h is selected.
144145
"""
145146

147+
ACL_PW_PATH = '/etc/shadow_redis_dir/shadow_redis_admin'
148+
146149
def __init__(self, **kwargs):
147150

148151
super(DBInterface, self).__init__()
@@ -151,7 +154,13 @@ def __init__(self, **kwargs):
151154
self.redis_kwargs = kwargs
152155
if len(self.redis_kwargs) == 0:
153156
self.redis_kwargs['unix_socket_path'] = self.REDIS_UNIX_SOCKET_PATH
154-
157+
self.redis_kwargs['username'] = 'admin'
158+
if 'password' not in self.redis_kwargs:
159+
self.redis_kwargs['password'] = util.read_from_file(self.ACL_PW_PATH)
160+
redis_shadow_tls_ca="/etc/shadow_redis_dir/certs_redis/ca.crt"
161+
self.redis_kwargs['ssl'] = True
162+
self.redis_kwargs['ssl_cert_reqs'] = None
163+
self.redis_kwargs['ssl_ca_certs'] = redis_shadow_tls_ca
155164
# For thread safety as recommended by python-redis
156165
# Create a separate client for each database
157166
self.redis_clients = DBRegistry()

Diff for: src/swsssdk/sonic_db_dump_load.py

+1
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ def do_load(options, args):
9797
usage += "\nfrom standard input."
9898
parser = optparse.OptionParser(usage=usage)
9999
parser.add_option('-w', '--password', help='connect with PASSWORD')
100+
parser.add_option('-u', '--username', help='connect with USERNAME')
100101
if help == DUMP:
101102
parser.add_option('-n', '--dbname', help='dump DATABASE (APPL_DB/ASIC_DB...)')
102103
parser.add_option('-t', '--conntype', help='indicate redis connection type (tcp[default] or unix_socket)', default='tcp')

Diff for: src/swsssdk/util.py

+24
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,27 @@ def setup_logging(config_file_path, log_level=logging.INFO):
6565
logging.root.exception(
6666
"Could not load specified logging configuration '{}'. Verify the filepath exists and is compliant with: "
6767
"[https://docs.python.org/3/library/logging.config.html#object-connections]".format(config_file_path))
68+
69+
70+
def read_from_file(file_path, target_type=str):
71+
"""
72+
Read content from file and convert to target type
73+
:param file_path: File path
74+
:param target_type: target type
75+
:return: content of the file according the target type.
76+
"""
77+
value = None
78+
try:
79+
with open(file_path, 'r') as f:
80+
value = f.read()
81+
if value is None:
82+
# None return value is not allowed in any case, so we log error here for further debug.
83+
logging.error('Failed to read from {}, value is None, errno is {}'.format(file_path, ctypes.get_errno()))
84+
# Raise ValueError for the except statement to handle this as a normal exception
85+
raise ValueError('File content of {} is None'.format(file_path))
86+
else:
87+
value = target_type(value.strip())
88+
except (ValueError, IOError) as e:
89+
logging.error('Failed to read from {}, errno is {}'.format(file_path, str(e)))
90+
91+
return value

0 commit comments

Comments
 (0)