Mask passwords in utils.trace for func params

The utils.trace helper is logging the args list to
the decorated function but is not masking passwords
in those args. This change adds a call to mask passwords
in the function args list.

Synchronize from os_brick.utils.trace.

Change-Id: I1c0b5d22258416b7cd2d84fafc339a1308ff34ab
This commit is contained in:
yenai 2019-01-02 17:22:07 +08:00
parent 84d268d9a0
commit 52d0f8a660
2 changed files with 26 additions and 1 deletions

View File

@ -1359,6 +1359,29 @@ class LogTracingTestCase(test.TestCase):
self.assertIn("'adminPass': '***'",
str(mock_log.debug.call_args_list[1]))
def test_utils_trace_method_with_password_in_formal_params(self):
mock_logging = self.mock_object(utils, 'logging')
mock_log = mock.Mock()
mock_log.isEnabledFor = lambda x: True
mock_logging.getLogger = mock.Mock(return_value=mock_log)
@utils.trace
def _trace_test_method(*args, **kwargs):
self.assertEqual('verybadpass',
kwargs['test_args']['data']['password'])
pass
test_args = {
'data': {
'password': 'verybadpass'
}
}
_trace_test_method(self, test_args=test_args)
self.assertEqual(2, mock_log.debug.call_count)
self.assertIn("'password': '***'",
str(mock_log.debug.call_args_list[0]))
@ddt.data(
{'total': 30.01, 'free': 28.01, 'provisioned': 2.0, 'max_ratio': 1.0,
'thin_support': False, 'thick_support': True,

View File

@ -911,7 +911,9 @@ def trace(*dec_args, **dec_kwargs):
if pass_filter:
logger.debug('==> %(func)s: call %(all_args)r',
{'func': func_name, 'all_args': all_args})
{'func': func_name,
'all_args': strutils.mask_password(
six.text_type(all_args))})
start_time = time.time() * 1000
try: