def test_is_valid_ipv6(self): self.assertTrue(netutils.is_valid_ipv6('::1')) self.assertFalse(netutils.is_valid_ipv6( '1fff::a88:85a3::172.31.128.1')) self.assertFalse(netutils.is_valid_ipv6(''))
def bind_udp(host, port): """Bind to an UDP port and listen. Use reuseaddr, reuseport if available :param host: IPv4/v6 address or "". "" binds to every IPv4 interface. :type host: str :param port: UDP port :type port: int :returns: socket """ LOG.info('Opening UDP Listening Socket on %(host)s:%(port)d', {'host': host, 'port': port}) family = socket.AF_INET6 if is_valid_ipv6(host) else socket.AF_INET sock_udp = socket.socket(family, socket.SOCK_DGRAM) sock_udp.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # NOTE: Linux supports socket.SO_REUSEPORT only in 3.9 and later releases. try: sock_udp.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1) except Exception: LOG.info('SO_REUSEPORT not available, ignoring.') sock_udp.setblocking(True) sock_udp.bind((host, port)) if port == 0: newport = sock_udp.getsockname()[1] LOG.info('Listening on UDP port %(port)d', {'port': newport}) return sock_udp
def generate_glance_url(): """Generate the URL to glance.""" glance_host = CONF.glance.host if netutils.is_valid_ipv6(glance_host): glance_host = '[%s]' % glance_host return "%s://%s:%d" % (CONF.glance.protocol, glance_host, CONF.glance.port)
def request(self, url, method='GET', body=None, headers=None, ssl_verify=True, stream=False): _headers = {'Content-Type': 'application/json'} _headers.update(headers or {}) parsed_url = urlparse.urlparse(url) port = parsed_url.port hostname = parsed_url.hostname scheme = parsed_url.scheme if netutils.is_valid_ipv6(hostname): hostname = "[%s]" % hostname relative_url = parsed_url.path if parsed_url.query: relative_url = relative_url + "?" + parsed_url.query LOG.info(_LI("Doing %(method)s on %(relative_url)s"), {'method': method, 'relative_url': relative_url}) if body: LOG.info(_LI("Body: %s") % body) if port: _url = "%s://%s:%d%s" % (scheme, hostname, int(port), relative_url) else: _url = "%s://%s%s" % (scheme, hostname, relative_url) response = requests.request(method, _url, data=body, headers=_headers, verify=ssl_verify, stream=stream) return response
def _build_nics(self, networks, security_groups=None): if not networks: return None nics = [] for idx, net in enumerate(networks): self._validate_belonging_subnet_to_net(net) nic_info = {'net-id': self._get_network_id(net)} if net.get(self.NETWORK_PORT): nic_info['port-id'] = net[self.NETWORK_PORT] elif self.is_using_neutron() and net.get(self.NETWORK_SUBNET): nic_info['port-id'] = self._create_internal_port( net, idx, security_groups) # if nic_info including 'port-id', do not set ip for nic if not nic_info.get('port-id'): if net.get(self.NETWORK_FIXED_IP): ip = net[self.NETWORK_FIXED_IP] if netutils.is_valid_ipv6(ip): nic_info['v6-fixed-ip'] = ip else: nic_info['v4-fixed-ip'] = ip if net.get(self.NETWORK_FLOATING_IP) and nic_info.get('port-id'): floating_ip_data = {'port_id': nic_info['port-id']} if net.get(self.NETWORK_FIXED_IP): floating_ip_data.update( {'fixed_ip_address': net.get(self.NETWORK_FIXED_IP)}) self._floating_ip_neutron_associate( net.get(self.NETWORK_FLOATING_IP), floating_ip_data) nics.append(nic_info) return nics
def start_udp(self): address_family = socket.AF_INET if netutils.is_valid_ipv6(self.udp_address): address_family = socket.AF_INET6 udp = socket.socket(address_family, socket.SOCK_DGRAM) udp.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) udp.bind((self.udp_address, self.udp_port)) self.setup_rabbit_mq_channel() self.udp_run = True while self.udp_run: # NOTE(jd) Arbitrary limit of 64K because that ought to be # enough for anybody. data, source = udp.recvfrom(64 * units.Ki) try: sample = msgpack.loads(data, encoding='utf-8') except Exception: logging.warning("UDP: Cannot decode data sent by %s", source) else: try: if sample.has_key("event_type"): #logging.debug("recevied event :%s",sample) logging.debug("recevied event :%s",sample['event_type']) #self.producer.publish(sample) self.publish(sample) else: #logging.debug("recevied Sample :%s",sample) logging.debug("recevied Sample :%s",sample['counter_name']) msg = self.convert_sample_to_event_data(sample) #self.producer.publish(msg) self.publish(msg) except Exception: logging.exception("UDP: Unable to publish msg")
def _construct_url(self, action, params=None): """ Create a URL object we can use to pass to _do_request(). """ action = urlparse.quote(action) path = '/'.join([self.doc_root or '', action.lstrip('/')]) scheme = "https" if self.use_ssl else "http" if netutils.is_valid_ipv6(self.host): netloc = "[%s]:%d" % (self.host, self.port) else: netloc = "%s:%d" % (self.host, self.port) if isinstance(params, dict): for (key, value) in list(params.items()): if value is None: del params[key] continue if not isinstance(value, six.string_types): value = str(value) params[key] = encodeutils.safe_encode(value) query = urlparse.urlencode(params) else: query = None url = urlparse.ParseResult(scheme, netloc, path, '', query, '') log_msg = _("Constructed URL: %s") LOG.debug(log_msg, url.geturl()) return url
def _create_glance_client(context, host, port, use_ssl, version=1): """Instantiate a new glanceclient.Client object.""" params = {} if use_ssl: scheme = "https" # https specific params params["insecure"] = CONF.glance.api_insecure params["ssl_compression"] = False if CONF.ssl.cert_file: params["cert_file"] = CONF.ssl.cert_file if CONF.ssl.key_file: params["key_file"] = CONF.ssl.key_file if CONF.ssl.ca_file: params["cacert"] = CONF.ssl.ca_file else: scheme = "http" if CONF.auth_strategy == "keystone": # NOTE(isethi): Glanceclient <= 0.9.0.49 accepts only # keyword 'token', but later versions accept both the # header 'X-Auth-Token' and 'token' params["token"] = context.auth_token params["identity_headers"] = generate_identity_headers(context) if netutils.is_valid_ipv6(host): # if so, it is ipv6 address, need to wrap it with '[]' host = "[%s]" % host endpoint = "%s://%s:%s" % (scheme, host, port) return glanceclient.Client(str(version), endpoint, **params)
def get_iscsi_connection_properties(lun_id, volume, iqns, addresses, ports): # literal ipv6 address addresses = [netutils.escape_ipv6(a) if netutils.is_valid_ipv6(a) else a for a in addresses] lun_id = int(lun_id) if isinstance(iqns, six.string_types): iqns = [iqns] * len(addresses) target_portals = ['%s:%s' % (a, p) for a, p in zip(addresses, ports)] properties = {} properties['target_discovered'] = False properties['target_portal'] = target_portals[0] properties['target_iqn'] = iqns[0] properties['target_lun'] = lun_id properties['volume_id'] = volume['id'] if len(addresses) > 1: properties['target_portals'] = target_portals properties['target_iqns'] = iqns properties['target_luns'] = [lun_id] * len(addresses) auth = volume['provider_auth'] if auth: (auth_method, auth_username, auth_secret) = auth.split() properties['auth_method'] = auth_method properties['auth_username'] = auth_username properties['auth_password'] = auth_secret return { 'driver_volume_type': 'iscsi', 'data': properties, }
def _build_nics(self, networks): if not networks: return None nics = [] for net_data in networks: nic_info = {} net_identifier = (net_data.get(self.NETWORK_UUID) or net_data.get(self.NETWORK_ID)) if net_identifier: if self.is_using_neutron(): net_id = (self.client_plugin( 'neutron').resolve_network( net_data, self.NETWORK_ID, self.NETWORK_UUID)) else: net_id = (self.client_plugin( 'nova').get_nova_network_id(net_identifier)) nic_info['net-id'] = net_id if net_data.get(self.NETWORK_FIXED_IP): ip = net_data[self.NETWORK_FIXED_IP] if netutils.is_valid_ipv6(ip): nic_info['v6-fixed-ip'] = ip else: nic_info['v4-fixed-ip'] = ip if net_data.get(self.NETWORK_PORT): nic_info['port-id'] = net_data[self.NETWORK_PORT] nics.append(nic_info) return nics
def start_udp(self): address_family = socket.AF_INET if netutils.is_valid_ipv6(cfg.CONF.collector.udp_address): address_family = socket.AF_INET6 udp = socket.socket(address_family, socket.SOCK_DGRAM) udp.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) udp.bind((cfg.CONF.collector.udp_address, cfg.CONF.collector.udp_port)) self.udp_run = True while self.udp_run: # NOTE(jd) Arbitrary limit of 64K because that ought to be # enough for anybody. data, source = udp.recvfrom(64 * units.Ki) try: sample = msgpack.loads(data, encoding='utf-8') except Exception: LOG.warn(_("UDP: Cannot decode data sent by %s"), source) else: try: LOG.debug(_("UDP: Storing %s"), sample) self.dispatcher_manager.map_method('record_metering_data', sample) except Exception: LOG.exception(_("UDP: Unable to store meter"))
def _vrts_get_iscsi_properties(self, volume, target_name): """Get target and LUN details.""" lun_name = self._get_va_lun_name(volume.id) data = {} path = self._lun_getid_str provider = '%s:%s' % (self._va_ip, self._port) lun_id_list = self._access_api(self.session, provider, path, json.dumps(data), 'GET') for lun in eval(lun_id_list['output']): vrts_lun_name = lun['storage_object'].split('/')[3] if vrts_lun_name == lun_name: lun_id = int(lun['index']) target_list = self._vrts_parse_xml_file(self.target_info_file) authentication = False portal_ip = "" for target in target_list: if target_name == target['name']: portal_ip = target['portal_ip'] if target['auth'] == '1': auth_user = target['auth_user'] auth_password = target['auth_password'] authentication = True break if portal_ip == "": message = (_('ACCESSIscsiDriver initialize_connection ' 'failed for %s as no portal ip was found') % volume.id) LOG.error(message) raise exception.VolumeBackendAPIException(message=message) portal_list = portal_ip.split(',') target_portal_list = [] for ip in portal_list: if netutils.is_valid_ipv6(ip): target_portal_list.append('[%s]:%s' % (ip, str(self.iscsi_port))) else: target_portal_list.append('%s:%s' % (ip, str(self.iscsi_port))) iscsi_properties = {} iscsi_properties['target_discovered'] = True iscsi_properties['target_iqn'] = target_name iscsi_properties['target_portal'] = target_portal_list[0] if len(target_portal_list) > 1: iscsi_properties['target_portals'] = target_portal_list iscsi_properties['target_lun'] = lun_id iscsi_properties['volume_id'] = volume.id if authentication: iscsi_properties['auth_username'] = auth_user iscsi_properties['auth_password'] = auth_password iscsi_properties['auth_method'] = 'CHAP' return iscsi_properties
def _get_shellinabox_console(self, scheme): generated_url = ( console_utils.get_shellinabox_console_url(self.info['port'])) console_host = CONF.my_ip if netutils.is_valid_ipv6(console_host): console_host = '[%s]' % console_host http_url = "%s://%s:%s" % (scheme, console_host, self.info['port']) self.assertEqual(http_url, generated_url)
def is_valid_ip_address(ip_address, ip_version): if int(ip_version) == 4: return netutils.is_valid_ipv4(ip_address) elif int(ip_version) == 6: return netutils.is_valid_ipv6(ip_address) else: raise exception.ManilaException( _("Provided improper IP version '%s'.") % ip_version)
def test_get_shellinabox_console_url(self): generated_url = console_utils.get_shellinabox_console_url( self.info['port']) console_host = CONF.my_ip if netutils.is_valid_ipv6(console_host): console_host = '[%s]' % console_host http_url = "http://%s:%s" % (console_host, self.info['port']) self.assertEqual(generated_url, http_url)
def _get_base_url(self, scheme, host, port, file_path): if netutils.is_valid_ipv6(host): base_url = "{0!s}://[{1!s}]:{2!s}/folder/{3!s}".format(scheme, host, port, urllib.pathname2url(file_path)) else: base_url = "{0!s}://{1!s}:{2!s}/folder/{3!s}".format(scheme, host, port, urllib.pathname2url(file_path)) return base_url
def _get_url(self): host = self._host if netutils.is_valid_ipv6(host): host = netutils.escape_ipv6(host) return '%s://%s:%s/%s' % (self._protocol, host, self._port, self._url)
def _get_base_url(self, scheme, host, port, file_path): if netutils.is_valid_ipv6(host): base_url = "%s://[%s]:%s/folder/%s" % (scheme, host, port, urllib.pathname2url(file_path)) else: base_url = "%s://%s:%s/folder/%s" % (scheme, host, port, urllib.pathname2url(file_path)) return base_url
def test_generate_glance_http_url(self): generated_url = glance.generate_glance_url() glance_host = CONF.glance.host # ipv6 address, need to wrap it with '[]' if netutils.is_valid_ipv6(glance_host): glance_host = '[%s]' % glance_host http_url = "http://%s:%d" % (glance_host, CONF.glance.port) self.assertEqual(generated_url, http_url)
def get_uri(self): if netutils.is_valid_ipv6(self.server_host): base_url = '%s://[%s]%s' % (self.scheme, self.server_host, self.path) else: base_url = '%s://%s%s' % (self.scheme, self.server_host, self.path) return '%s?%s' % (base_url, self.query)
def __init__(self, **kwargs): self.url = kwargs.get('url', None) if self.url is None: host = kwargs['host'] self.url = '%s://%s:%s' % ( 'https' if kwargs.get('use_ssl', False) else 'http', '[' + host + ']' if netutils.is_valid_ipv6(host) else host, kwargs['port'])
def test_generate_glance_https_url(self): self.flags(protocol="https", group="glance") generated_url = glance.generate_glance_url() glance_host = CONF.glance.host # ipv6 address, need to wrap it with '[]' if netutils.is_valid_ipv6(glance_host): glance_host = "[%s]" % glance_host https_url = "https://%s:%d" % (glance_host, CONF.glance.port) self.assertEqual(generated_url, https_url)
def process_bind_param(self, value, dialect): """Process/Formats the value before insert it into the db.""" if dialect.name == 'postgresql': return value # NOTE(maurosr): The purpose here is to convert ipv6 to the shortened # form, not validate it. elif netutils.is_valid_ipv6(value): return utils.get_shortened_ipv6(value) return value
def _get_server_cls(self, host): """Return an appropriate WSGI server class base on provided host :param host: The listen host for the zaqar API server. """ server_cls = simple_server.WSGIServer if netutils.is_valid_ipv6(host): if getattr(server_cls, 'address_family') == socket.AF_INET: class server_cls(server_cls): address_family = socket.AF_INET6 return server_cls
def _get_volume_location(self, volume_id): """Returns NFS mount address as <nfs_ip_address>:<nfs_mount_dir>.""" provider_location = self._get_provider_location(volume_id) nfs_server_ip, export_path = na_utils.get_export_host_junction_path( provider_location) if netutils.is_valid_ipv6(nfs_server_ip): nfs_server_ip = netutils.escape_ipv6(nfs_server_ip) return nfs_server_ip + ':' + export_path
def __init__(self, parsed_url): self.host, self.port = netutils.parse_host_port( parsed_url.netloc, default_port=cfg.CONF.collector.udp_port) if netutils.is_valid_ipv6(self.host): addr_family = socket.AF_INET6 else: addr_family = socket.AF_INET self.socket = socket.socket(addr_family, socket.SOCK_DGRAM)
def get_socat_console_url(port): """Get a URL to access the console via socat. :param port: the terminal port (integer) for the node :return: an access URL to the socat console of the node """ console_host = CONF.my_ip if netutils.is_valid_ipv6(console_host): console_host = "[%s]" % console_host return "tcp://%(host)s:%(port)s" % {"host": console_host, "port": port}
def get_soap_url(protocol, host, path='sdk'): """Return URL to SOAP services for ESX/VC server. :param protocol: https or http :param host: ESX/VC server host IP :param path: path part of the SOAP URL :return: URL to SOAP services for ESX/VC server """ if netutils.is_valid_ipv6(host): return '%s://[%s]/%s' % (protocol, host, path) return '%s://%s/%s' % (protocol, host, path)
def get_shellinabox_console_url(port): """Get a url to access the console via shellinaboxd. :param port: the terminal port for the node. """ console_host = CONF.my_ip if netutils.is_valid_ipv6(console_host): console_host = '[%s]' % console_host console_url = "http://%s:%s" % (console_host, port) return console_url
def get_socat_console_url(port): """Get a URL to access the console via socat. :param port: the terminal port (integer) for the node :return: an access URL to the socat console of the node """ console_host = CONF.console.socat_address if netutils.is_valid_ipv6(console_host): console_host = '[%s]' % console_host return 'tcp://%(host)s:%(port)s' % {'host': console_host, 'port': port}
def get_shellinabox_console_url(port): """Get a url to access the console via shellinaboxd. :param port: the terminal port for the node. """ console_host = CONF.my_ip if netutils.is_valid_ipv6(console_host): console_host = "[%s]" % console_host scheme = "https" if CONF.console.terminal_cert_dir else "http" return "%(scheme)s://%(host)s:%(port)s" % {"scheme": scheme, "host": console_host, "port": port}
def build_network_address(host, port): """Combines the specified host name or IP address with the specified port. :param host: Host name or IP address in presentation (string) format :param port: Port number :return: The host name or IP address and port combination; IPv6 addresses are enclosed in the square brackets """ if netutils.is_valid_ipv6(host): return '[%s]:%s' % (host, port) else: return '%s:%s' % (host, port)
def get_default_dns(self, ip_version=4): dns_list = self._settings.default_dns valid_dns = [] for ip in dns_list: if ip_version == 6 and netutils.is_valid_ipv6(ip): valid_dns.append(ip) elif ip_version == 4 and netutils.is_valid_ipv4(ip): valid_dns.append(ip) else: LOG.warning('{0} is not a vaild IPV{1} address, ' 'ingore...'.format(ip, ip_version)) return valid_dns
def get_shellinabox_console_url(port): """Get a url to access the console via shellinaboxd. :param port: the terminal port for the node. """ console_host = CONF.my_ip if netutils.is_valid_ipv6(console_host): console_host = '[%s]' % console_host scheme = 'https' if CONF.console.terminal_cert_dir else 'http' return '%(scheme)s://%(host)s:%(port)s' % {'scheme': scheme, 'host': console_host, 'port': port}
def remove_macs_from_lsp_addresses(addresses): """Remove the mac addreses from the Logical_Switch_Port addresses column. :param addresses: The list of addresses from the Logical_Switch_Port. Example: ["80:fa:5b:06:72:b7 158.36.44.22", "ff:ff:ff:ff:ff:ff 10.0.0.2"] :returns: A list of IP addesses (v4 and v6) """ ip_list = [] for addr in addresses: ip_list.extend([x for x in addr.split() if (netutils.is_valid_ipv4(x) or netutils.is_valid_ipv6(x))]) return ip_list
def initialize_connection(self, volume, connector): """Export a volume to a host.""" # create client initiator_iqn = connector['initiator'] self.create_client(initiator_iqn) auth = self._get_auth_for_client(initiator_iqn) username = initiator_iqn if not auth['password']: password = volume_utils.generate_password(length=self.CHAP_LENGTH) self._set_chap_for_client(initiator_iqn, username, password) else: LOG.debug("using existing CHAP password") password = auth['password'] # add disk for export iscsi_config = self._get_config() # First have to ensure that the disk is registered with # the gateways. self.create_disk(volume.name) self.register_disk(self.target_iqn, volume.name) iscsi_config = self._get_config() # Now export the disk to the initiator lun = self.export_disk(initiator_iqn, volume.name, iscsi_config) # fetch the updated config so we can get the lun id iscsi_config = self._get_config() target_info = iscsi_config['targets'][self.target_iqn] ips = target_info['ip_list'] target_portal = ips[0] if netutils.is_valid_ipv6(target_portal): target_portal = "[%s]:3260" % target_portal else: target_portal = "%s:3260" % target_portal data = { 'driver_volume_type': 'iscsi', 'data': { 'target_iqn': self.target_iqn, 'target_portal': target_portal, 'target_lun': lun['id'], 'auth_method': 'CHAP', 'auth_username': username, 'auth_password': password, } } return data
def test__get_url(self, host): port = '80' root = netapp_api.NaServer(host, port=port) protocol = root.TRANSPORT_TYPE_HTTP url = root.URL_FILER if netutils.is_valid_ipv6(host): host = netutils.escape_ipv6(host) result = '%s://%s:%s/%s' % (protocol, host, port, url) url = root._get_url() self.assertEqual(result, url)
def is_valid_ip_address(ip_address, ip_version): ip_version = ([int(ip_version)] if not isinstance(ip_version, list) else ip_version) if not set(ip_version).issubset(set([4, 6])): raise exception.ImproperIPVersion(ip_version) if 4 in ip_version: if netutils.is_valid_ipv4(ip_address): return True if 6 in ip_version: if netutils.is_valid_ipv6(ip_address): return True return False
def get_ics_console_url(port): """Get a url to access the console (ironic console server). :param port: the terminal port for the node. """ console_host = CONF.my_ip schema = 'tcp' if netutils.is_valid_ipv6(console_host): console_host = '[%s]' % console_host schema = 'tcp6' return '%(schema)s://%(host)s:%(port)s' % { 'schema': schema, 'host': console_host, 'port': port }
def is_valid_ip_address(ip_address, ip_version): ip_version = ([int(ip_version)] if not isinstance(ip_version, list) else ip_version) if not set(ip_version).issubset(set([4, 6])): raise exception.ManilaException( _("Provided improper IP version '%s'.") % ip_version) if 4 in ip_version: if netutils.is_valid_ipv4(ip_address): return True if 6 in ip_version: if netutils.is_valid_ipv6(ip_address): return True return False
def get_system_dns_resolvers(resolver_file=DNS_RESOLVER_FILE): resolvers = [] if not os.path.exists(resolver_file): return resolvers with open(resolver_file, 'r') as rconf: for line in rconf.readlines(): if not line.startswith('nameserver'): continue line = line.split('nameserver')[1].strip() valid_ip = (netutils.is_valid_ipv4(line, strict=True) or netutils.is_valid_ipv6(line)) if valid_ip: resolvers.append(line) return resolvers
def _fix_esx_url(self, url, host, port): """Fix netloc in the case of an ESX host. In the case of an ESX host, the netloc is set to '*' in the URL returned in HttpNfcLeaseInfo. It should be replaced with host name or IP address. """ urlp = urlparse.urlparse(url) if urlp.netloc == '*': scheme, netloc, path, params, query, fragment = urlp if netutils.is_valid_ipv6(host): netloc = '[%s]:%d' % (host, port) else: netloc = "%s:%d" % (host, port) url = urlparse.urlunparse( (scheme, netloc, path, params, query, fragment)) return url
def _build_nics(self, networks, security_groups=None): if not networks: return None str_network = self._str_network(networks) if str_network: return str_network nics = [] for idx, net in enumerate(networks): self._validate_belonging_subnet_to_net(net) nic_info = {'net-id': self._get_network_id(net)} if net.get(self.NETWORK_PORT): nic_info['port-id'] = net[self.NETWORK_PORT] elif net.get(self.NETWORK_SUBNET): nic_info['port-id'] = self._create_internal_port( net, idx, security_groups) # if nic_info including 'port-id', do not set ip for nic if not nic_info.get('port-id'): if net.get(self.NETWORK_FIXED_IP): ip = net[self.NETWORK_FIXED_IP] if netutils.is_valid_ipv6(ip): nic_info['v6-fixed-ip'] = ip else: nic_info['v4-fixed-ip'] = ip if net.get(self.NETWORK_FLOATING_IP) and nic_info.get('port-id'): floating_ip_data = {'port_id': nic_info['port-id']} if net.get(self.NETWORK_FIXED_IP): floating_ip_data.update( {'fixed_ip_address': net.get(self.NETWORK_FIXED_IP)}) self._floating_ip_neutron_associate( net.get(self.NETWORK_FLOATING_IP), floating_ip_data) if net.get(self.NETWORK_VIF_MODEL): nic_info['vif-model'] = net[self.NETWORK_VIF_MODEL] if net.get(self.NETWORK_VIF_PCI_ADDRESS): nic_info['vif-pci-address'] = net[self.NETWORK_VIF_PCI_ADDRESS] if net.get(self.NIC_TAG): nic_info[self.NIC_TAG] = net.get(self.NIC_TAG) nics.append(nic_info) return nics
def check_params(self, values): if 'network_id' not in values: raise manager_ex.MissingParameter(param='network_id') if 'amount' not in values: raise manager_ex.MissingParameter(param='amount') if not strutils.is_int_like(values['amount']): raise manager_ex.MalformedParameter(param='amount') # required_floatingips param is an optional parameter fips = values.get('required_floatingips', []) if not isinstance(fips, list): manager_ex.MalformedParameter(param='required_floatingips') for ip in fips: if not (netutils.is_valid_ipv4(ip) or netutils.is_valid_ipv6(ip)): raise manager_ex.InvalidIPFormat(ip=ip)
def bind_tcp(host, port, tcp_backlog, tcp_keepidle=None): """Bind to a TCP port and listen. Use reuseaddr, reuseport if available, keepalive if specified :param host: IPv4/v6 address or "". "" binds to every IPv4 interface. :type host: str :param port: TCP port :type port: int :param tcp_backlog: TCP listen backlog :type tcp_backlog: int :param tcp_keepidle: TCP keepalive interval :type tcp_keepidle: int :returns: socket """ LOG.info('Opening TCP Listening Socket on %(host)s:%(port)d', { 'host': host, 'port': port }) family = socket.AF_INET6 if is_valid_ipv6(host) else socket.AF_INET sock_tcp = socket.socket(family, socket.SOCK_STREAM) sock_tcp.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) sock_tcp.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1) # NOTE: Linux supports socket.SO_REUSEPORT only in 3.9 and later releases. try: sock_tcp.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1) except Exception: LOG.info('SO_REUSEPORT not available, ignoring.') # This option isn't available in the OS X version of eventlet if tcp_keepidle and hasattr(socket, 'TCP_KEEPIDLE'): sock_tcp.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPIDLE, tcp_keepidle) sock_tcp.setblocking(True) sock_tcp.bind((host, port)) if port == 0: newport = sock_tcp.getsockname()[1] LOG.info('Listening on TCP port %(port)d', {'port': newport}) sock_tcp.listen(tcp_backlog) return sock_tcp
def start_udp(self): address_family = socket.AF_INET if netutils.is_valid_ipv6(self.conf.collector.udp_address): address_family = socket.AF_INET6 udp = socket.socket(address_family, socket.SOCK_DGRAM) udp.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) try: # NOTE(zhengwei): linux kernel >= 3.9 udp.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1) except Exception: LOG.warning( _LW("System does not support socket.SO_REUSEPORT " "option. Only one worker will be able to process " "incoming data.")) udp.bind( (self.conf.collector.udp_address, self.conf.collector.udp_port)) self.udp_run = True while self.udp_run: # NOTE(sileht): return every 10 seconds to allow # clear shutdown if not select.select([udp], [], [], 10.0)[0]: continue # NOTE(jd) Arbitrary limit of 64K because that ought to be # enough for anybody. data, source = udp.recvfrom(64 * units.Ki) try: sample = msgpack.loads(data, encoding='utf-8') except Exception: LOG.warning(_("UDP: Cannot decode data sent by %s"), source) else: if publisher_utils.verify_signature( sample, self.conf.publisher.telemetry_secret): try: LOG.debug("UDP: Storing %s", sample) self.meter_manager.map_method('record_metering_data', sample) except Exception: LOG.exception(_("UDP: Unable to store meter")) else: LOG.warning( _LW('sample signature invalid, ' 'discarding: %s'), sample)
def _build_nics(self, networks): if not networks: return None nics = [] for net in networks: nic_info = {} nic_info['net-id'] = self._get_network_id(net) if net.get(self.NETWORK_FIXED_IP): ip = net[self.NETWORK_FIXED_IP] if netutils.is_valid_ipv6(ip): nic_info['v6-fixed-ip'] = ip else: nic_info['v4-fixed-ip'] = ip if net.get(self.NETWORK_PORT): nic_info['port-id'] = net[self.NETWORK_PORT] nics.append(nic_info) return nics
def _dhcp_option_file_or_url(task, urlboot=False): """Returns the appropriate file or URL. :param task: A TaskManager object. :param url_boot: Boolean value default False to indicate if a URL should be returned to the file as opposed to a file. """ boot_file = deploy_utils.get_pxe_boot_file(task.node) # NOTE(TheJulia): There are additional cases as we add new # features, so the logic below is in the form of if/elif/elif if not urlboot: return boot_file elif urlboot: if netutils.is_valid_ipv6(CONF.pxe.tftp_server): host = "[%s]" % CONF.pxe.tftp_server else: host = CONF.pxe.tftp_server return "tftp://{host}/{boot_file}".format(host=host, boot_file=boot_file)
def request(self, url, method='GET', body=None, headers=None, ssl_verify=True, stream=False): _headers = {'Content-Type': 'application/json'} _headers.update(headers or {}) parsed_url = urlparse.urlparse(url) port = parsed_url.port hostname = parsed_url.hostname scheme = parsed_url.scheme if netutils.is_valid_ipv6(hostname): hostname = "[%s]" % hostname relative_url = parsed_url.path if parsed_url.query: relative_url = relative_url + "?" + parsed_url.query LOG.info(_LI("Doing %(method)s on %(relative_url)s"), { 'method': method, 'relative_url': relative_url }) if body: LOG.info(_LI("Body: %s") % body) if port: _url = "%s://%s:%d%s" % (scheme, hostname, int(port), relative_url) else: _url = "%s://%s%s" % (scheme, hostname, relative_url) response = requests.request(method, _url, data=body, headers=_headers, verify=ssl_verify, stream=stream) return response
def _build_nics(self, networks): if not networks: return None nics = [] for idx, net in enumerate(networks): self._validate_belonging_subnet_to_net(net) nic_info = {'net-id': self._get_network_id(net)} if net.get(self.NETWORK_FIXED_IP): ip = net[self.NETWORK_FIXED_IP] if netutils.is_valid_ipv6(ip): nic_info['v6-fixed-ip'] = ip else: nic_info['v4-fixed-ip'] = ip if net.get(self.NETWORK_PORT): nic_info['port-id'] = net[self.NETWORK_PORT] elif self.is_using_neutron() and net.get(self.NETWORK_SUBNET): nic_info['port-id'] = self._create_internal_port(net, idx) nics.append(nic_info) return nics
def get_iscsi_connection_properties(lun_id, volume, iqn, address, port): # literal ipv6 address if netutils.is_valid_ipv6(address): address = netutils.escape_ipv6(address) properties = {} properties['target_discovered'] = False properties['target_portal'] = '%s:%s' % (address, port) properties['target_iqn'] = iqn properties['target_lun'] = int(lun_id) properties['volume_id'] = volume['id'] auth = volume['provider_auth'] if auth: (auth_method, auth_username, auth_secret) = auth.split() properties['auth_method'] = auth_method properties['auth_username'] = auth_username properties['auth_password'] = auth_secret return { 'driver_volume_type': 'iscsi', 'data': properties, }
def export_unc_path(ip_addr): """Convert IPv6 address to valid UNC path. In Microsoft Windows OS, UNC (Uniform Naming Convention) specifies a common syntax to describe the location of a network resource. The colon which used by IPv6 is an illegal character in a UNC path name. So the IPv6 address need to be converted to valid UNC path. References: - https://en.wikipedia.org/wiki/IPv6_address #Literal_IPv6_addresses_in_UNC_path_names - https://en.wikipedia.org/wiki/Path_(computing)#Uniform_Naming_Convention :param ip_addr: IPv6 address. :return: UNC path. """ unc_suffix = '.ipv6-literal.net' if netutils.is_valid_ipv6(ip_addr): ip_addr = ip_addr.replace(':', '-') + unc_suffix return ip_addr
def parse_nets(ns): err_msg = ("Invalid nets argument '%s'. nets arguments must be of " "the form --nets <network=network, v4-fixed-ip=ip-addr," "v6-fixed-ip=ip-addr, port=port-uuid>, " "with only one of network, or port specified.") nets = [] for net_str in ns: net_info = { "network": "", "v4-fixed-ip": "", "v6-fixed-ip": "", "port": "" } for kv_str in net_str.split(","): try: k, v = kv_str.split("=", 1) k = k.strip() v = v.strip() except ValueError: raise apiexec.CommandError(err_msg % net_str) if k in net_info: if net_info[k]: raise apiexec.CommandError(err_msg % net_str) net_info[k] = v else: raise apiexec.CommandError(err_msg % net_str) if net_info['v4-fixed-ip'] and not netutils.is_valid_ipv4( net_info['v4-fixed-ip']): raise apiexec.CommandError("Invalid ipv4 address.") if net_info['v6-fixed-ip'] and not netutils.is_valid_ipv6( net_info['v6-fixed-ip']): raise apiexec.CommandError("Invalid ipv6 address.") if bool(net_info['network']) == bool(net_info['port']): raise apiexec.CommandError(err_msg % net_str) nets.append(net_info) return nets
def parse_valid_host_port(host_port): """ Given a "host:port" string, attempts to parse it as intelligently as possible to determine if it is valid. This includes IPv6 [host]:port form, IPv4 ip:port form, and hostname:port or fqdn:port form. Invalid inputs will raise a ValueError, while valid inputs will return a (host, port) tuple where the port will always be of type int. """ try: try: host, port = netutils.parse_host_port(host_port) except Exception: raise ValueError(_('Host and port "%s" is not valid.') % host_port) if not netutils.is_valid_port(port): raise ValueError(_('Port "%s" is not valid.') % port) # First check for valid IPv6 and IPv4 addresses, then a generic # hostname. Failing those, if the host includes a period, then this # should pass a very generic FQDN check. The FQDN check for letters at # the tail end will weed out any hilariously absurd IPv4 addresses. if not (netutils.is_valid_ipv6(host) or netutils.is_valid_ipv4(host) or is_valid_hostname(host) or is_valid_fqdn(host)): raise ValueError(_('Host "%s" is not valid.') % host) except Exception as ex: raise ValueError( _('%s ' 'Please specify a host:port pair, where host is an ' 'IPv4 address, IPv6 address, hostname, or FQDN. If ' 'using an IPv6 address, enclose it in brackets ' 'separately from the port (i.e., ' '"[fe80::a:b:c]:9876").') % ex) return (host, int(port))
def start_udp(self): address_family = socket.AF_INET if netutils.is_valid_ipv6(cfg.CONF.collector.udp_address): address_family = socket.AF_INET6 udp = socket.socket(address_family, socket.SOCK_DGRAM) udp.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) udp.bind((cfg.CONF.collector.udp_address, cfg.CONF.collector.udp_port)) self.udp_run = True while self.udp_run: # NOTE(jd) Arbitrary limit of 64K because that ought to be # enough for anybody. data, source = udp.recvfrom(64 * units.Ki) try: sample = msgpack.loads(data, encoding='utf-8') except Exception: LOG.warn(_("UDP: Cannot decode data sent by %s"), source) else: try: LOG.debug("UDP: Storing %s", sample) self.meter_manager.map_method('record_metering_data', sample) except Exception: LOG.exception(_("UDP: Unable to store meter"))
def __init__(self, **kwargs): """Initialize a new client for the plugin.""" self.format = 'json' # Extract configuration parameters from the configuration file. self.username = cfg.CONF.ml2_cisco_n1kv.username self.password = cfg.CONF.ml2_cisco_n1kv.password self.vsm_ips = config.get_vsm_hosts() self.action_prefix = 'http://%s/api/n1k' self.timeout = cfg.CONF.ml2_cisco_n1kv.http_timeout self.max_vsm_retries = cfg.CONF.ml2_cisco_n1kv.max_vsm_retries required_opts = ('vsm_ips', 'username', 'password') # Validate whether required options are configured for opt in required_opts: if not getattr(self, opt): raise cfg.RequiredOptError(opt, 'ml2_cisco_n1kv') # Validate the configured VSM IP addresses # Note: Currently only support IPv4 for vsm_ip in self.vsm_ips: if not (netutils.is_valid_ipv4(vsm_ip) or netutils.is_valid_ipv6(vsm_ip)): raise cfg.Error( _("Cisco Nexus1000V ML2 driver config: " "Invalid format for VSM IP address: %s") % vsm_ip)