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
This commit is contained in:
wangyao 2017-04-17 13:08:37 +08:00
parent 00fde782be
commit 4bfe5b58a1
2 changed files with 12 additions and 2 deletions

View File

@ -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:

View File

@ -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'])