Mask passwords when logging messages

When logging a message, any secrets and passwords should be masked. This
uses oslo_utils.strutils to mask any passwords that are to be logged.

Change-Id: I263d44c0f2e900c5f6e210cbd7ec56e48d0d5bb2
Closes-Bug: #1487038
This commit is contained in:
Ryan Rossiter 2015-08-20 20:47:42 +00:00
parent 67d8bcd5dc
commit c990ee02fa
2 changed files with 19 additions and 1 deletions

View File

@ -18,6 +18,7 @@
import logging
from oslo_serialization import jsonutils
from oslo_utils import strutils
from oslo_messaging.notify import notifier
@ -38,4 +39,4 @@ class LogDriver(notifier._Driver):
message['event_type']))
method = getattr(logger, priority.lower(), None)
if method:
method(jsonutils.dumps(message))
method(strutils.mask_password(jsonutils.dumps(message)))

View File

@ -20,6 +20,7 @@ import uuid
import fixtures
from oslo_serialization import jsonutils
from oslo_utils import strutils
from oslo_utils import timeutils
from stevedore import dispatch
from stevedore import extension
@ -317,6 +318,22 @@ class TestLogNotifier(test_utils.BaseTestCase):
msg = {'event_type': 'foo'}
driver.notify(None, msg, "sample", None)
def test_mask_passwords(self):
# Ensure that passwords are masked with notifications
driver = _impl_log.LogDriver(None, None, None)
logger = mock.MagicMock()
logger.info = mock.MagicMock()
message = {'password': 'passw0rd', 'event_type': 'foo'}
json_str = jsonutils.dumps(message)
mask_str = strutils.mask_password(json_str)
with mock.patch.object(logging, 'getLogger') as gl:
gl.return_value = logger
driver.notify(None, message, 'info', 0)
logger.info.assert_called_once_with(mask_str)
class TestRoutingNotifier(test_utils.BaseTestCase):
def setUp(self):