Example #1
0
 def test_valid_port_fail(self):
     invalid_inputs = [
         '-32768', '65536', 528491, '528491', '528.491', 'thirty-seven',
         None
     ]
     for input_str in invalid_inputs:
         self.assertFalse(netutils.is_valid_port(input_str))
Example #2
0
def validate_port_range_or_none(data, valid_values=None):
    """Validate data is a range of TCP/UDP port numbers

    :param data: The data to validate
    :param valid_values: Not used!
    :returns: None if data is an int between 0 and 65535, or two ints between 0
        and 65535 with a colon between them, otherwise a human readable message
        as to why data is invalid.
    """
    if data is None:
        return
    data = str(data)
    ports = data.split(':')
    if len(ports) > 2:
        msg = _("Port range must be two integers separated by a colon")
        LOG.debug(msg)
        return msg
    for p in ports:
        if len(p) == 0:
            msg = _("Port range must be two integers separated by a colon")
            LOG.debug(msg)
            return msg
        if not netutils.is_valid_port(p):
            msg = _("Invalid port: %s") % p
            LOG.debug(msg)
            return msg
    if len(ports) > 1 and int(ports[0]) > int(ports[1]):
        msg = _("First port in a port range must be lower than the second "
                "port")
        LOG.debug(msg)
        return msg
Example #3
0
def convert_validate_port_value(port):
    if port is None:
        return port

    if netutils.is_valid_port(port):
        return int(port)
    else:
        raise SecurityGroupInvalidPortValue(port=port)
Example #4
0
def convert_validate_port_value(port):
    if port is None:
        return port

    if netutils.is_valid_port(port):
        return int(port)
    else:
        raise SecurityGroupInvalidPortValue(port=port)
Example #5
0
def convert_validate_port_value(port):
    if port is None:
        return port

    if netutils.is_valid_port(port):
        return int(port)
    else:
        raise HostingDeviceInvalidPortValue(port=port)
def convert_validate_port_value(port):
    if port is None:
        return port

    if netutils.is_valid_port(port):
        return int(port)
    else:
        raise HostingDeviceInvalidPortValue(port=port)
Example #7
0
def validate_network_port(port, port_name="Port"):
    """Validates the given port.

    :param port: TCP/UDP port.
    :param port_name: Name of the port.
    :returns: An integer port number.
    :raises: InvalidParameterValue, if the port is invalid.
    """

    if netutils.is_valid_port(port):
        return int(port)

    raise exception.InvalidParameterValue(_(
        '%(port_name)s "%(port)s" is not a valid port.') %
        {'port_name': port_name, 'port': port})
Example #8
0
def validate_network_port(port, port_name="Port"):
    """Validates the given port.

    :param port: TCP/UDP port.
    :param port_name: Name of the port.
    :returns: An integer port number.
    :raises: InvalidParameterValue, if the port is invalid.
    """

    if netutils.is_valid_port(port):
        return int(port)

    raise exception.InvalidParameterValue(_(
        '%(port_name)s "%(port)s" is not a valid port.') %
        {'port_name': port_name, 'port': port})
Example #9
0
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))
Example #10
0
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))
Example #11
0
def validate_port_range(port):
    if not netutils.is_valid_port(port):
        raise ValidationError(_("Not a valid port number"))
    def __init__(self, parsed_url):
        self.driver_endpoint_from_pipeline = parsed_url
        # database connection parameters
        self.dbaddress = cfg.CONF.influxdb.influxdb_addr
        self.dbport = cfg.CONF.influxdb.influxdb_port
        self.dbname = cfg.CONF.influxdb.influxdb_instance
        self.dbuser = cfg.CONF.influxdb.influxdb_user
        self.dbpass = cfg.CONF.influxdb.influxdb_pass
        self.retention_policy = cfg.CONF.influxdb.retention_policy
        self.verboselog = cfg.CONF.influxdb.verboselog
        self.mappings = cfg.CONF.influxdb.mappings
        self.mapping_data = {}

        # open mapping file
        with open(self.mappings, "r") as mapping_descriptor:
            self.mappingfile = json.loads(mapping_descriptor.read())
            LOG.info(
                "[*] InfluxDB Publisher: Loaded Meters and Tag Mappings from config file [%s]."
                % self.mappings)

        # parse json...
        for entry in self.mappingfile:
            self.mapping_data[entry["name"]] = entry["values"]

        # this host
        self.hostname = gethostname()

        # compile additional tags
        self.additional_tags = {
            'hypervisor_hostname': self.hostname,
            'retention_policy': self.retention_policy
        }

        # set meter prefix
        if cfg.CONF.influxdb.append_hypervisor:
            self.meter_prefix = cfg.CONF.influxdb.metering_prefix + self.hostname
        else:
            self.meter_prefix = cfg.CONF.influxdb.metering_prefix

        # get keystone client instance
        self.identity = get_client()

        # get initial tenant list
        self.tenants = self.identity.projects.list()

        # at startup, register available tenants in in-memory database
        # subsequent queries either hit the in memory cache or need a new query to keystone
        for t in self.tenants:
            InfluxDBPublisherUtils.pushTenant(t.id, t.name)

        # create DB connection
        # sanity check on database parameters
        if not (network_utils.is_valid_ipv4(self.dbaddress)
                and network_utils.is_valid_port(self.dbport)):
            raise Exception("dbaddr:dbport validation error %s:%s" %
                            (self.dbaddress, self.dbport))

        try:
            self.dbconn = dbclient(self.dbaddress, self.dbport, self.dbuser,
                                   self.dbpass, self.dbname)
        except Exception as e:
            LOG.info(e)

        # OK init done
        LOG.info("[+] InfluxDB Publisher [%s] registered to [%s]" %
                 (self.driver_endpoint_from_pipeline, self.dbaddress))
Example #13
0
 def test_valid_port_fail(self):
     invalid_inputs = ['-32768', '0', 0, '65536', 528491, '528491',
                       '528.491', 'thirty-seven', None]
     for input_str in invalid_inputs:
         self.assertFalse(netutils.is_valid_port(input_str))
Example #14
0
 def test_valid_port(self):
     valid_inputs = [1, '1', 2, '3', '5', 8, 13, 21,
                     '80', '3246', '65535']
     for input_str in valid_inputs:
         self.assertTrue(netutils.is_valid_port(input_str))
Example #15
0
 def test_valid_port(self):
     valid_inputs = [
         0, '0', 1, '1', 2, '3', '5', 8, 13, 21, '80', '3246', '65535'
     ]
     for input_str in valid_inputs:
         self.assertTrue(netutils.is_valid_port(input_str))