From: Bob Callaway Date: Fri, 10 Jan 2014 16:15:07 +0000 (-0500) Subject: Ensure hostnames are converted to IP for comparison. X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=ed05bbe1e786e3eedfd94e7f6724ecb38f32f1e6;p=openstack-build%2Fcinder-build.git Ensure hostnames are converted to IP for comparison. Also switch to using getaddrinfo for IPv6 compliance vs gethostbyname. Change-Id: I1a194eae514a65bff56488571c05457bfa660c02 Closes-Bug: 1267667 --- diff --git a/cinder/tests/test_netapp_nfs.py b/cinder/tests/test_netapp_nfs.py index e909b5d09..471ecdeaa 100644 --- a/cinder/tests/test_netapp_nfs.py +++ b/cinder/tests/test_netapp_nfs.py @@ -20,7 +20,6 @@ import mox from mox import IgnoreArg from mox import IsA import os -import socket from cinder import context from cinder import exception @@ -752,8 +751,8 @@ class NetappDirectCmodeNfsDriverTestCase(test.TestCase): def test_check_share_in_use_incorrect_host(self): drv = self._driver mox = self.mox - mox.StubOutWithMock(socket, 'gethostbyname') - socket.gethostbyname(IgnoreArg()).AndRaise(Exception()) + mox.StubOutWithMock(drv, '_resolve_hostname') + drv._resolve_hostname(IgnoreArg()).AndRaise(Exception()) mox.ReplayAll() share = drv._check_share_in_use('incorrect:8989', '/dir') mox.VerifyAll() @@ -764,9 +763,9 @@ class NetappDirectCmodeNfsDriverTestCase(test.TestCase): drv = self._driver mox = self.mox drv._mounted_shares = ['127.0.0.1:/dir/share'] - mox.StubOutWithMock(socket, 'gethostbyname') + mox.StubOutWithMock(drv, '_resolve_hostname') mox.StubOutWithMock(drv, '_share_match_for_ip') - socket.gethostbyname(IgnoreArg()).AndReturn('10.22.33.44') + drv._resolve_hostname(IgnoreArg()).AndReturn('10.22.33.44') drv._share_match_for_ip( '10.22.33.44', ['127.0.0.1:/dir/share']).AndReturn('share') mox.ReplayAll() diff --git a/cinder/volume/drivers/netapp/nfs.py b/cinder/volume/drivers/netapp/nfs.py index 79bb1d992..f43a8b07c 100644 --- a/cinder/volume/drivers/netapp/nfs.py +++ b/cinder/volume/drivers/netapp/nfs.py @@ -547,7 +547,7 @@ class NetAppNFSDriver(nfs.NfsDriver): try: if conn: host = conn.split(':')[0] - ipv4 = socket.gethostbyname(host) + ip = self._resolve_hostname(host) share_candidates = [] for sh in self._mounted_shares: sh_exp = sh.split(':')[1] @@ -556,7 +556,7 @@ class NetAppNFSDriver(nfs.NfsDriver): if share_candidates: LOG.debug(_('Found possible share matches %s'), share_candidates) - return self._share_match_for_ip(ipv4, share_candidates) + return self._share_match_for_ip(ip, share_candidates) except Exception: LOG.warn(_("Unexpected exception while short listing used share.")) return None @@ -604,6 +604,12 @@ class NetAppNFSDriver(nfs.NfsDriver): """Checks if share is compatible with volume to host it.""" raise NotImplementedError() + def _resolve_hostname(self, hostname): + """Resolves hostname to IP address.""" + res = socket.getaddrinfo(hostname, None)[0] + family, socktype, proto, canonname, sockaddr = res + return sockaddr[0] + class NetAppDirectNfsDriver (NetAppNFSDriver): """Executes commands related to volumes on NetApp filer.""" @@ -798,7 +804,8 @@ class NetAppDirectCmodeNfsDriver (NetAppDirectNfsDriver): net_if_iter.add_new_child('max-records', '10') query = NaElement('query') net_if_iter.add_child_elem(query) - query.add_node_with_children('net-interface-info', **{'address': ip}) + query.add_node_with_children('net-interface-info', + **{'address': self._resolve_hostname(ip)}) result = self._invoke_successfully(net_if_iter) if result.get_child_content('num-records') and\ int(result.get_child_content('num-records')) >= 1: @@ -808,7 +815,7 @@ class NetAppDirectCmodeNfsDriver (NetAppDirectNfsDriver): _('No interface found on cluster for ip %s') % (ip)) - def _get_verver_ips(self, vserver): + def _get_vserver_ips(self, vserver): """Get ips for the vserver.""" result = na_utils.invoke_api( self._client, api_name='net-interface-get-iter', @@ -934,13 +941,13 @@ class NetAppDirectCmodeNfsDriver (NetAppDirectNfsDriver): LOG.warn(_("No shares found hence skipping ssc refresh.")) return mnt_share_vols = set() - vs_ifs = self._get_verver_ips(self.vserver) + vs_ifs = self._get_vserver_ips(self.vserver) for vol in vols['all']: for sh in self._mounted_shares: host = sh.split(':')[0] junction = sh.split(':')[1] - ipv4 = socket.gethostbyname(host) - if (self._ip_in_ifs(ipv4, vs_ifs) and + ip = self._resolve_hostname(host) + if (self._ip_in_ifs(ip, vs_ifs) and junction == vol.id['junction_path']): mnt_share_vols.add(vol) vol.export['path'] = sh