示例#1
0
def parse_cidr(cidr):
    if not cidr:
        return '0.0.0.0/0'

    try:
        cidr = encodeutils.safe_decode(urllib.parse.unquote(cidr))
    except Exception:
        raise exception.InvalidCidr(cidr=cidr)

    if not netutils.is_valid_cidr(cidr):
        raise exception.InvalidCidr(cidr=cidr)

    return cidr
示例#2
0
    def create(self, req, body):
        context = req.environ['nova.context']
        authorize(context)

        def bad(e):
            return exc.HTTPBadRequest(explanation=e)

        if not (body and body.get("network")):
            raise bad(_("Missing network in body"))

        params = body["network"]
        if not params.get("label"):
            raise bad(_("Network label is required"))

        cidr = params.get("cidr") or params.get("cidr_v6")
        if not cidr:
            raise bad(_("Network cidr or cidr_v6 is required"))

        if params.get("project_id") == "":
            params["project_id"] = None

        LOG.debug("Creating network with label %s", params["label"])

        try:
            params["num_networks"] = 1
            try:
                params["network_size"] = netaddr.IPNetwork(cidr).size
            except netaddr.AddrFormatError:
                raise exception.InvalidCidr(cidr=cidr)
            if not self.extended:
                create_params = ('allowed_start', 'allowed_end')
                for field in extended_fields + create_params:
                    if field in params:
                        del params[field]

            network = self.network_api.create(context, **params)[0]
        except exception.NovaException as ex:
            if ex.code == 400:
                raise bad(ex.format_message())
            elif ex.code == 409:
                raise exc.HTTPConflict(explanation=ex.format_message())
            raise
        return {"network": network_dict(context, network, self.extended)}
示例#3
0
    def create(self, req, body):
        context = req.environ['nova.context']
        authorize(context)

        def bad(e):
            return exc.HTTPBadRequest(explanation=e)

        if not (body and body.get("network")):
            raise bad(_("Missing network in body"))

        params = body["network"]
        if not params.get("label"):
            raise bad(_("Network label is required"))

        cidr = params.get("cidr") or params.get("cidr_v6")
        if not cidr:
            raise bad(_("Network cidr or cidr_v6 is required"))

        if params.get("project_id") == "":
            params["project_id"] = None

        try:
            params["num_networks"] = 1
            try:
                params["network_size"] = netaddr.IPNetwork(cidr).size
            except netaddr.AddrFormatError:
                raise exception.InvalidCidr(cidr=cidr)

            network = self.network_api.create(context, **params)[0]
        except exception.NovaException as ex:
            if ex.code == 400:
                raise bad(ex.format_message())
            elif ex.code == 409:
                raise exc.HTTPConflict(explanation=ex.format_message())
            raise
        return {"network": network_dict(context, network)}
示例#4
0
 def raise_invalid_cidr(cidr, decoding_exception=None):
     raise exception.InvalidCidr(cidr=cidr)
示例#5
0
    def _rule_args_to_dict(self, context, to_port=None, from_port=None,
                                  parent_group_id=None, ip_protocol=None,
                                  cidr=None, group_id=None):
        values = {}

        if group_id is not None:
            try:
                parent_group_id = int(parent_group_id)
                group_id = int(group_id)
            except ValueError:
                msg = _("Parent or group id is not integer")
                raise exception.InvalidInput(reason=msg)

            values['group_id'] = group_id
            #check if groupId exists
            db.security_group_get(context, group_id)
        elif cidr:
            # If this fails, it throws an exception. This is what we want.
            try:
                cidr = urllib.unquote(cidr).decode()
            except Exception:
                raise exception.InvalidCidr(cidr=cidr)

            if not utils.is_valid_cidr(cidr):
                # Raise exception for non-valid address
                raise exception.InvalidCidr(cidr=cidr)

            values['cidr'] = cidr
        else:
            values['cidr'] = '0.0.0.0/0'

        if group_id:
            # Open everything if an explicit port range or type/code are not
            # specified, but only if a source group was specified.
            ip_proto_upper = ip_protocol.upper() if ip_protocol else ''
            if (ip_proto_upper == 'ICMP' and
                from_port is None and to_port is None):
                from_port = -1
                to_port = -1
            elif (ip_proto_upper in ['TCP', 'UDP'] and from_port is None
                  and to_port is None):
                from_port = 1
                to_port = 65535

        if ip_protocol and from_port is not None and to_port is not None:

            ip_protocol = str(ip_protocol)
            try:
                from_port = int(from_port)
                to_port = int(to_port)
            except ValueError:
                if ip_protocol.upper() == 'ICMP':
                    raise exception.InvalidInput(reason="Type and"
                         " Code must be integers for ICMP protocol type")
                else:
                    raise exception.InvalidInput(reason="To and From ports "
                          "must be integers")

            if ip_protocol.upper() not in ['TCP', 'UDP', 'ICMP']:
                raise exception.InvalidIpProtocol(protocol=ip_protocol)

            # Verify that from_port must always be less than
            # or equal to to_port
            if (ip_protocol.upper() in ['TCP', 'UDP'] and
                from_port > to_port):
                raise exception.InvalidPortRange(from_port=from_port,
                      to_port=to_port, msg="Former value cannot"
                                            " be greater than the later")

            # Verify valid TCP, UDP port ranges
            if (ip_protocol.upper() in ['TCP', 'UDP'] and
                (from_port < 1 or to_port > 65535)):
                raise exception.InvalidPortRange(from_port=from_port,
                      to_port=to_port, msg="Valid TCP ports should"
                                           " be between 1-65535")

            # Verify ICMP type and code
            if (ip_protocol.upper() == "ICMP" and
                (from_port < -1 or from_port > 255 or
                to_port < -1 or to_port > 255)):
                raise exception.InvalidPortRange(from_port=from_port,
                      to_port=to_port, msg="For ICMP, the"
                                           " type:code must be valid")

            values['protocol'] = ip_protocol
            values['from_port'] = from_port
            values['to_port'] = to_port
        else:
            # If cidr based filtering, protocol and ports are mandatory
            if 'cidr' in values:
                return None

        return values
示例#6
0
    def _rule_args_to_dict(self,
                           context,
                           to_port=None,
                           from_port=None,
                           parent_group_id=None,
                           ip_protocol=None,
                           cidr=None,
                           group_id=None):
        values = {}

        if group_id:
            try:
                parent_group_id = int(parent_group_id)
                group_id = int(group_id)
            except ValueError:
                msg = _("Parent or group id is not integer")
                raise exception.InvalidInput(reason=msg)

            if parent_group_id == group_id:
                msg = _("Parent group id and group id cannot be same")
                raise exception.InvalidInput(reason=msg)

            values['group_id'] = group_id
            #check if groupId exists
            db.security_group_get(context, group_id)
        elif cidr:
            # If this fails, it throws an exception. This is what we want.
            try:
                cidr = urllib.unquote(cidr).decode()
                netaddr.IPNetwork(cidr)
            except Exception:
                raise exception.InvalidCidr(cidr=cidr)
            values['cidr'] = cidr
        else:
            values['cidr'] = '0.0.0.0/0'

        if ip_protocol and from_port and to_port:

            try:
                from_port = int(from_port)
                to_port = int(to_port)
            except ValueError:
                raise exception.InvalidPortRange(from_port=from_port,
                                                 to_port=to_port)
            ip_protocol = str(ip_protocol)
            if ip_protocol.upper() not in ['TCP', 'UDP', 'ICMP']:
                raise exception.InvalidIpProtocol(protocol=ip_protocol)
            if ((min(from_port, to_port) < -1)
                    or (max(from_port, to_port) > 65535)):
                raise exception.InvalidPortRange(from_port=from_port,
                                                 to_port=to_port)

            values['protocol'] = ip_protocol
            values['from_port'] = from_port
            values['to_port'] = to_port
        else:
            # If cidr based filtering, protocol and ports are mandatory
            if 'cidr' in values:
                return None

        return values