Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add redis ACL support #424

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion sonic_platform_base/sonic_eeprom/eeprom_tlvinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,30 @@
raise ImportError (str(e) + "- required module not found")

STATE_DB_INDEX = 6
USERNAME = 'admin'


def read_from_file(file_path, target_type=str):
"""
Read content from file and convert to target type
:param file_path: File path
:param target_type: target type
:return: content of the file according the target type.
"""
try:
with open(file_path, 'r') as f:
value = f.read()
if value is None:
# None return value is not allowed in any case, so we log error here for further debug.
syslog.syslog(syslog.LOG_ERR, 'Failed to read from {}, value is None, errno is {}'.format(file_path, ctypes.get_errno()))
# Raise ValueError for the except statement to handle this as a normal exception
raise ValueError('File content of {} is None'.format(file_path))
else:
value = target_type(value.strip())
except (ValueError, IOError) as e:
syslog.syslog(syslog.LOG_ERR, 'Failed to read from {}, errno is {}'.format(file_path, str(e)))

return value

#
# TlvInfo Format - This eeprom format was defined by Cumulus Networks
Expand Down Expand Up @@ -625,7 +649,9 @@ def redis_client(self):
A redis client instance
"""
if not self._redis_client:
self._redis_client = redis.Redis(db=STATE_DB_INDEX)
password = read_from_file('/etc/shadow_redis_dir/shadow_redis_admin')
redis_shadow_tls_ca="/etc/shadow_redis_dir/certs_redis/ca.crt"
self._redis_client = redis.Redis(port=6379, db=STATE_DB_INDEX, username=USERNAME, password=password, ssl=True, ssl_cert_reqs=None, ssl_ca_certs=redis_shadow_tls_ca)
return self._redis_client

def _redis_hget(self, key, field):
Expand Down
4 changes: 4 additions & 0 deletions tests/eeprom_base_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,17 @@ def test_eeprom_tlvinfo_set_eeprom(self):
assert exit_mock.called

def test_eeprom_tlvinfo_update_eeprom_db(self):
# mock read file of Redis ACL
eeprom_tlvinfo.read_from_file = mock.MagicMock(return_value = None)
# Test updating eeprom to DB by mocking redis hmset
eeprom_class = eeprom_tlvinfo.TlvInfoDecoder(EEPROM_SYMLINK_FULL_PATH, 0, '', True)
eeprom = eeprom_class.read_eeprom()
eeprom_class.redis_client.hmset = mock.MagicMock(return_value = True)
assert(0 == eeprom_class.update_eeprom_db(eeprom))

def test_eeprom_tlvinfo_read_eeprom_db(self):
# mock read file of Redis ACL
eeprom_tlvinfo.read_from_file = mock.MagicMock(return_value = None)
# Test reading from DB by mocking redis hget
eeprom_class = eeprom_tlvinfo.TlvInfoDecoder(EEPROM_SYMLINK_FULL_PATH, 0, '', True)
eeprom_class.redis_client.hget = mock.MagicMock(return_value = b'1')
Expand Down
Loading