Fix : Register vim if use_barbican is disabled

If use_barbican is disabled and if the tacker services are running
under Py3, the registration of vim fails with TypeError.

This patch fixes this issue by writing fernet key in binary mode.

Closes-Bug: #1873440

Change-Id: I08423a7c29c7fd5eeabe64276f6e0d208ccec72b
This commit is contained in:
Shubham 2020-04-21 14:43:58 +05:30
parent 8a4a035a48
commit 95bce292f4
2 changed files with 9 additions and 6 deletions

View File

@ -252,7 +252,7 @@ class OpenStack_Driver(abstract_vim_driver.VimAbstractDriver,
auth['key_type'] = 'fernet_key'
key_file = os.path.join(CONF.vim_keys.openstack, vim_id)
try:
with open(key_file, 'w') as f:
with open(key_file, 'wb') as f:
if six.PY2:
f.write(fernet_key.decode('utf-8'))
else:

View File

@ -13,6 +13,8 @@
# License for the specific language governing permissions and limitations
# under the License.
import os
from keystoneauth1 import exceptions
import mock
from oslo_config import cfg
@ -141,14 +143,15 @@ class TestOpenstack_Driver(base.TestCase):
self.keystone.initialize_client.return_value = mock_ks_client
fernet_attrs = {'encrypt.return_value': 'encrypted_password'}
mock_fernet_obj = mock.Mock(**fernet_attrs)
mock_fernet_key = 'test_fernet_key'
mock_fernet_key = b'test_fernet_key'
self.keystone.create_fernet_key.return_value = (mock_fernet_key,
mock_fernet_obj)
file_mock = mock.mock_open()
with mock.patch('six.moves.builtins.open', file_mock, create=True):
self.openstack_driver.register_vim(vim_obj)
self.openstack_driver.register_vim(vim_obj)
with open('/tmp/' + vim_obj['id'], 'r') as f:
# asserting that file has been written correctly.
self.assertEqual('test_fernet_key', f.read())
mock_fernet_obj.encrypt.assert_called_once_with(mock.ANY)
file_mock().write.assert_called_once_with('test_fernet_key')
os.remove('/tmp/' + vim_obj['id'])
@mock.patch('tacker.nfvo.drivers.vim.openstack_driver.os.remove')
@mock.patch('tacker.nfvo.drivers.vim.openstack_driver.os.path'