Merge "Ensure ssh key is sanitized when read from disk"

This commit is contained in:
Jenkins 2016-04-21 21:50:39 +00:00 committed by Gerrit Code Review
commit be4e3965e1
2 changed files with 14 additions and 1 deletions

View File

@ -433,7 +433,7 @@ def _ssh_key():
return ''
try:
with open(key) as out:
return out.read()
return out.read().strip()
except IOError:
LOG.warning(_LW('Could not load router ssh public key from %s'), key)
return ''

View File

@ -211,6 +211,19 @@ class TestNovaWrapper(base.RugTestBase):
result = nova._ssh_key()
self.assertEqual(result, 'fake-key')
@mock.patch.object(__builtins__, 'open', autospec=True)
def test_ssh_key_sanitize(self, fake_open):
mock_key_file = mock.MagicMock(spec=file)
mock_key_file.read.return_value = ('''
fake-key with some newlines
''')
mock_key_file.__enter__.return_value = mock_key_file
fake_open.return_value = mock_key_file
result = nova._ssh_key()
self.assertEqual(result, 'fake-key with some newlines')
@mock.patch.object(nova, 'LOG', autospec=True)
@mock.patch.object(__builtins__, 'open', autospec=True)
def test_ssh_key_not_found(self, fake_open, fake_log):