From 4bfe5b58a1b4709e5df4d4c1b325ddddf6f4f2d9 Mon Sep 17 00:00:00 2001 From: wangyao Date: Mon, 17 Apr 2017 13:08:37 +0800 Subject: [PATCH] Fix user-list failed if host uses host_ip/netmask According bug exception trace this problem cause by host name validate . The origin code use netaddr.IPAddress to validate host name, it just fit ip address, so the ip/netmask style do not match this rule.To solve this problem, I changed this method to netaddr.IPNetwork, which support ip and ip/netmask. Also provide a test case to validate the method. Change-Id: I8dad9d1496d09372698821b832d1baa4f0c35a0d Closes-Bug: #1673874 --- trove/common/db/mysql/models.py | 4 ++-- trove/tests/unittests/guestagent/test_mysql_manager.py | 10 ++++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/trove/common/db/mysql/models.py b/trove/common/db/mysql/models.py index cf9d0fbf03..50700b103d 100644 --- a/trove/common/db/mysql/models.py +++ b/trove/common/db/mysql/models.py @@ -145,8 +145,8 @@ class MySQLUser(models.DatastoreUser): if CONF.hostname_require_valid_ip: try: # '%' works as a MySQL wildcard, but it is not a valid - # part of an IPAddress - netaddr.IPAddress(value.replace('%', '1')) + # part of an IPNetwork + netaddr.IPNetwork(value.replace('%', '1')) except (ValueError, netaddr.AddrFormatError): return False else: diff --git a/trove/tests/unittests/guestagent/test_mysql_manager.py b/trove/tests/unittests/guestagent/test_mysql_manager.py index 2f60dd05dd..1355420a61 100644 --- a/trove/tests/unittests/guestagent/test_mysql_manager.py +++ b/trove/tests/unittests/guestagent/test_mysql_manager.py @@ -20,6 +20,7 @@ from mock import patch from proboscis.asserts import assert_equal from testtools.matchers import Is, Equals, Not +from trove.common.db.mysql import models from trove.common.exception import InsufficientSpaceForReplica from trove.common.exception import ProcessExecutionError from trove.common import instance as rd_instance @@ -103,6 +104,15 @@ class GuestAgentManagerTest(DatastoreManagerTest): dbaas.MySqlAppStatus.get.assert_any_call() mock_status.update.assert_any_call() + def _empty_user(self): + return models.MySQLUser(deserializing=True) + + def test_valid_host_name(self): + test_host = "192.58.197.0/255.255.255.0" + user = self._empty_user() + user.host = test_host + self.assertEqual(test_host, user.host) + @patch.object(dbaas.MySqlAdmin, 'create_database') def test_create_database(self, create_db_mock): self.manager.create_database(self.context, ['db1'])