From 3f2800ede54692510c4c652c34176aa6c10c7f97 Mon Sep 17 00:00:00 2001
From: zengyingzhe <zengyingzhe@huawei.com>
Date: Wed, 19 Oct 2016 14:58:40 +0800
Subject: [PATCH] Compare the encoded tag more accurately for huawei driver

huawei driver uses a tag '!$$$' to indicate if username/password
is encoded or not in huawei configuration file.

Currently 'find' method is used to check if this tag is included
in the configuration string, but it'll misread if the tag is
included not in front of the string.

So change the comparing statement to a more accurate way, compare
the front slice of the string directly to the tag.

Change-Id: Ic343970578477362460340b01e5766e03c7d63a4
Closes-Bug: 1635073
---
 manila/share/drivers/huawei/v3/helper.py      |  2 +-
 .../share/drivers/huawei/test_huawei_nas.py   | 73 +++++++++++++++++++
 2 files changed, 74 insertions(+), 1 deletion(-)

diff --git a/manila/share/drivers/huawei/v3/helper.py b/manila/share/drivers/huawei/v3/helper.py
index e2df939838..371c897944 100644
--- a/manila/share/drivers/huawei/v3/helper.py
+++ b/manila/share/drivers/huawei/v3/helper.py
@@ -190,7 +190,7 @@ class RestHelper(object):
         need_encode = False
         for key in ['UserName', 'UserPassword']:
             node = root.find('Storage/%s' % key)
-            if node.text.find(prefix_name) > -1:
+            if node.text.startswith(prefix_name):
                 logininfo[key] = base64.b64decode(
                     six.b(node.text[4:])).decode()
             else:
diff --git a/manila/tests/share/drivers/huawei/test_huawei_nas.py b/manila/tests/share/drivers/huawei/test_huawei_nas.py
index 2d3d1f3877..805af85116 100644
--- a/manila/tests/share/drivers/huawei/test_huawei_nas.py
+++ b/manila/tests/share/drivers/huawei/test_huawei_nas.py
@@ -26,6 +26,7 @@ import xml.dom.minidom
 import ddt
 import mock
 from oslo_serialization import jsonutils
+from xml.etree import ElementTree as ET
 
 from manila.common import constants as common_constants
 from manila import context
@@ -823,6 +824,45 @@ class FakeHuaweiNasDriver(huawei_nas.HuaweiNasDriver):
         self.plugin.private_storage = FakePrivateStorage()
 
 
+class FakeConfigParseTree(object):
+    class FakeNode(object):
+        def __init__(self, text):
+            self._text = text
+
+        @property
+        def text(self):
+            return self._text
+
+        @text.setter
+        def text(self, text):
+            self._text = text
+
+    class FakeRoot(object):
+        def __init__(self):
+            self._node_map = {}
+
+        def findtext(self, path, default=None):
+            if path in self._node_map:
+                return self._node_map[path].text
+            return default
+
+        def find(self, path):
+            if path in self._node_map:
+                return self._node_map[path]
+            return None
+
+    def __init__(self, path_value):
+        self.root = self.FakeRoot()
+        for k in path_value:
+            self.root._node_map[k] = self.FakeNode(path_value[k])
+
+    def getroot(self):
+        return self.root
+
+    def write(self, filename, format):
+        pass
+
+
 @ddt.ddt
 class HuaweiShareDriverTestCase(test.TestCase):
     """Tests GenericShareDriver."""
@@ -4492,3 +4532,36 @@ class HuaweiShareDriverTestCase(test.TestCase):
             logininfo = self.driver.plugin.helper._get_login_info()
             self.assertEqual('admin', logininfo['UserName'])
             self.assertEqual('Admin@storage', logininfo['UserPassword'])
+
+    @ddt.data({
+        'username': 'abc',
+        'password': '123456',
+        'expect_username': 'abc',
+        'expect_password': '123456',
+    }, {
+        'username': '!$$$YWJj',
+        'password': '!$$$MTIzNDU2',
+        'expect_username': 'abc',
+        'expect_password': '123456',
+    }, {
+        'username': 'ab!$$$c',
+        'password': '123!$$$456',
+        'expect_username': 'ab!$$$c',
+        'expect_password': '123!$$$456',
+    })
+    @ddt.unpack
+    def test__get_login_info(self, username, password, expect_username,
+                             expect_password):
+        configs = {
+            'Storage/RestURL': 'https://123456',
+            'Storage/UserName': username,
+            'Storage/UserPassword': password,
+        }
+        self.mock_object(
+            ET, 'parse',
+            mock.Mock(return_value=FakeConfigParseTree(configs)))
+
+        result = self.driver.plugin.helper._get_login_info()
+        self.assertEqual(expect_username, result['UserName'])
+        self.assertEqual(expect_password, result['UserPassword'])
+        ET.parse.assert_called_once_with(self.fake_conf_file)