Fix huawei driver username/password encoding bug

While huawei driver reads username/password configuration from
configuration file, if the value is plain text, huawei driver'll
encode this text and write back to configuration file.

huawei driver leverages base64.b64encode to implement the encoding.
The encoded result is a byte string, and then is converted to
unicode string.

In py2, this works fine. However, in py3, the unicode string
converted from byte string is like "b'***'", *** is the orginal
string supposed to be.

So the encoded string is broken, and exception occurs while decoding
from it.

This patch fixes the incorrect unicode string converting and make
the encoded result string right.

Change-Id: I2483ae4999484b3a67c7d443f9b3cc214a3473b2
Closes-Bug: #1613242
This commit is contained in:
zengyingzhe 2016-09-08 14:23:31 +08:00 committed by Yingzhe Zeng
parent 8b277fd7d8
commit d14e906ac0
2 changed files with 14 additions and 3 deletions
manila
share/drivers/huawei/v3
tests/share/drivers/huawei

@ -191,11 +191,12 @@ class RestHelper(object):
for key in ['UserName', 'UserPassword']:
node = root.find('Storage/%s' % key)
if node.text.find(prefix_name) > -1:
logininfo[key] = base64.b64decode(six.b(node.text[4:]))
logininfo[key] = base64.b64decode(
six.b(node.text[4:])).decode()
else:
logininfo[key] = node.text
node.text = prefix_name + six.text_type(
base64.b64encode(six.b(node.text)))
node.text = prefix_name + base64.b64encode(
six.b(node.text)).decode()
need_encode = True
if need_encode:
self._change_file_mode(filename)

@ -4482,3 +4482,13 @@ class HuaweiShareDriverTestCase(test.TestCase):
activate_deactivate_qos_mock.assert_called_once_with('11', False)
delete_qos_mock.assert_called_once_with('11')
def test_username_password_encode_decode(self):
for i in (1, 2):
# First loop will encode the username/password and
# write back to configuration.
# Second loop will get the encoded username/password and
# decode them.
logininfo = self.driver.plugin.helper._get_login_info()
self.assertEqual('admin', logininfo['UserName'])
self.assertEqual('Admin@storage', logininfo['UserPassword'])