Пример #1
0
def validate_arguments(arguments):
    """
    Validate argument values:
        <IP>

    Arguments not validated:
        <CONTAINER>
        <INTERFACE>

    :param arguments: Docopt processed arguments
    """
    # Validate IP
    requested_ip = arguments.get("<IP>")
    if not (requested_ip is None or validate_ip(requested_ip, 4)
            or validate_ip(requested_ip, 6) or validate_cidr(requested_ip)
            or requested_ip.lower() in ('ipv4', 'ipv6')):
        print_paragraph("Invalid IP address specified.  Argument must be a "
                        "valid IP or CIDR.")
        sys.exit(1)

    # Validate POOL
    if requested_ip is not None and '/' in requested_ip:
        requested_pool = IPNetwork(requested_ip)

        try:
            client.get_ip_pool_config(requested_pool.version, requested_pool)
        except KeyError:
            print_paragraph("Invalid CIDR specified for desired pool. "
                            "No pool found for {0}.".format(requested_pool))
            sys.exit(1)

    # Validate PROFILE
    endpoint.validate_arguments(arguments)
Пример #2
0
def validate_arguments(arguments):
    """
    Validate argument values:
        <CIDRS>

    :param arguments: Docopt processed arguments
    """
    # Validate CIDR
    cidrs = arguments.get("<CIDRS>")
    start_ip = arguments.get("<START_IP>")
    end_ip = arguments.get("<END_IP>")
    if cidrs:
        for cidr in cidrs:
            if not validate_cidr(cidr):
                print "Invalid CIDR specified %s" % cidr
                sys.exit(1)
    elif start_ip or end_ip:
        if not (validate_ip(start_ip, 4) or validate_ip(start_ip, 6)):
            print "Invalid START_IP specified."
            sys.exit(1)
        elif not (validate_ip(end_ip, 4) or validate_ip(end_ip, 6)):
            print "Invalid END_IP specified."
            sys.exit(1)
        elif IPAddress(start_ip).version != IPAddress(end_ip).version:
            print "START_IP and END_IP must be the same ip version"
            sys.exit(1)
        elif not IPAddress(start_ip) < IPAddress(end_ip):
            print "START_IP must be a smaller ip address than END_IP"
            sys.exit(1)
Пример #3
0
def validate_arguments(arguments):
    """
    Validate argument values:
        <PEER_IP>
        <AS_NUM>

    Arguments not validated:

    :param arguments: Docopt processed arguments
    """
    # Validate IPs
    peer_ip_ok = arguments.get("<PEER_IP>") is None or \
                    validate_ip(arguments["<PEER_IP>"], 4) or \
                    validate_ip(arguments["<PEER_IP>"], 6)
    asnum_ok = True
    asnum = arguments.get("<AS_NUM>")
    if asnum:
        asnum_ok = validate_asn(asnum)

    # Print error messages
    if not peer_ip_ok:
        print "Invalid IP address specified."
    if not asnum_ok:
        print "Invalid AS Number specified."

    # Exit if not valid arguments
    if not (peer_ip_ok and asnum_ok):
        sys.exit(1)
Пример #4
0
def validate_arguments(arguments):
    """
    Validate argument values:
        <PEER_IP>
        <AS_NUM>

    Arguments not validated:

    :param arguments: Docopt processed arguments
    """
    # Validate IPs
    peer_ip_ok = arguments.get("<PEER_IP>") is None or \
                    validate_ip(arguments["<PEER_IP>"], 4) or \
                    validate_ip(arguments["<PEER_IP>"], 6)
    asnum_ok = True
    asnum = arguments.get("<AS_NUM>")
    if asnum:
        asnum_ok = validate_asn(asnum)

    # Print error messages
    if not peer_ip_ok:
        print "Invalid IP address specified."
    if not asnum_ok:
        print "Invalid AS Number specified."

    # Exit if not valid arguments
    if not (peer_ip_ok and asnum_ok):
        sys.exit(1)
Пример #5
0
def validate_arguments(arguments):
    """
    Validate argument values:
        <CIDRS>

    :param arguments: Docopt processed arguments
    """
    # Validate CIDR
    cidrs = arguments.get("<CIDRS>")
    start_ip = arguments.get("<START_IP>")
    end_ip = arguments.get("<END_IP>")
    if cidrs:
        for cidr in cidrs:
            if not validate_cidr(cidr):
                print "Invalid CIDR specified %s" % cidr
                sys.exit(1)
    elif start_ip or end_ip:
        if not (validate_ip(start_ip, 4) or validate_ip(start_ip, 6)):
            print "Invalid START_IP specified."
            sys.exit(1)
        elif not (validate_ip(end_ip, 4) or validate_ip(end_ip, 6)):
            print "Invalid END_IP specified."
            sys.exit(1)
        elif IPAddress(start_ip).version != IPAddress(end_ip).version:
            print "START_IP and END_IP must be the same ip version"
            sys.exit(1)
        elif not IPAddress(start_ip) < IPAddress(end_ip):
            print "START_IP must be a smaller ip address than END_IP"
            sys.exit(1)
Пример #6
0
    def test_validate_ip(self, ip_addr, version, expected_result):
        """
        Test validate_ip function in calico_ctl utils
        """
        if version not in (4, 6):
            with self.assertRaises(AssertionError):
                util.validate_ip(ip_addr, version)
        else:
            test_result = util.validate_ip(ip_addr, version)

            self.assertEqual(expected_result, test_result)
Пример #7
0
def validate_arguments(arguments):
    """
    Validate argument values:
        <IP>

    :param arguments: Docopt processed arguments
    """
    # Validate IP
    container_ip_ok = arguments.get("<IP>") is None or \
                      validate_ip(arguments["<IP>"], 4) or \
                      validate_ip(arguments["<IP>"], 6)

    # Print error message and exit if not valid argument
    if not container_ip_ok:
        print "Invalid IP address specified."
        sys.exit(1)
Пример #8
0
    def test_validate_ip(self, ip, version, expected_result):
        """
        Test validate_ip function in calico_ctl utils
        """
        # Call method under test
        test_result = validate_ip(ip, version)

        # Assert
        self.assertEqual(expected_result, test_result)
Пример #9
0
    def test_validate_ip(self, ip, version, expected_result):
        """
        Test validate_ip function in calico_ctl utils
        """
        # Call method under test
        test_result = validate_ip(ip, version)

        # Assert
        self.assertEqual(expected_result, test_result)
Пример #10
0
def validate_arguments(arguments):
    """
    Validate argument values:
        <IP>

    Arguments not validated:
        <CONTAINER>
        <INTERFACE>

    :param arguments: Docopt processed arguments
    """
    # Validate IP
    requested_ip = arguments.get("<IP>")
    if not (requested_ip is None or
            validate_ip(requested_ip, 4) or
            validate_ip(requested_ip, 6) or
            validate_cidr(requested_ip) or
            requested_ip.lower() in ('ipv4', 'ipv6')):
        print_paragraph("Invalid IP address specified.  Argument must be a "
                        "valid IP or CIDR.")
        sys.exit(1)

    # Validate POOL
    if requested_ip is not None and '/' in requested_ip:
        requested_pool = IPNetwork(requested_ip)

        try:
            client.get_ip_pool_config(requested_pool.version, requested_pool)
        except KeyError:
            print_paragraph("Invalid CIDR specified for desired pool. "
                            "No pool found for {0}.".format(requested_pool))
            sys.exit(1)


    # Validate PROFILE
    endpoint.validate_arguments(arguments)
Пример #11
0
def validate_arguments(arguments):
    """
    Validate argument values:
        <IP>
        <IP6>
        <PEER_IP>
        <AS_NUM>
        <DETACH>

    Arguments not validated:
        <DOCKER_IMAGE_NAME>
        <LOG_DIR>

    :param arguments: Docopt processed arguments
    """
    # Validate IPs
    ip_ok = arguments.get("--ip") is None or \
            arguments.get("--ip") is "" or \
            validate_ip(arguments.get("--ip"), 4)
    ip6_ok = arguments.get("--ip6") is None or \
             arguments.get("--ip6") is "" or \
             validate_ip(arguments.get("--ip6"), 6)
    container_ip_ok = arguments.get("<IP>") is None or \
                      validate_ip(arguments["<IP>"], 4) or \
                      validate_ip(arguments["<IP>"], 6)
    peer_ip_ok = arguments.get("<PEER_IP>") is None or \
                 validate_ip(arguments["<PEER_IP>"], 4) or \
                 validate_ip(arguments["<PEER_IP>"], 6)
    runtime_ok = arguments.get("--runtime") in [None, "none", "docker", "rkt"]

    asnum_ok = True
    asnum = arguments.get("<AS_NUM>") or arguments.get("--as")
    if asnum:
        asnum_ok = validate_asn(asnum)

    detach_ok = True
    if arguments.get("<DETACH>") or arguments.get("--detach"):
        detach_ok = arguments.get("--detach") in ["true", "false"]

    detach_libnetwork_ok = (arguments.get("--detach") == "true"
                            or not arguments.get("--libnetwork"))

    # Print error message
    if not ip_ok:
        print "Invalid IPv4 address specified with --ip argument."
    if not ip6_ok:
        print "Invalid IPv6 address specified with --ip6 argument."
    if not container_ip_ok or not peer_ip_ok:
        print "Invalid IP address specified."
    if not asnum_ok:
        print "Invalid AS Number specified."
    if not detach_ok:
        print "Valid values for --detach are 'true' and 'false'"
    if not detach_libnetwork_ok:
        print "The only valid value for --detach is 'true' when using libnetwork"
    if not runtime_ok:
        print "Runtime must be 'docker', 'rkt' or 'none'."

    # Exit if not valid argument
    if not (ip_ok and ip6_ok and container_ip_ok and peer_ip_ok and asnum_ok
            and detach_ok and detach_libnetwork_ok and runtime_ok):
        sys.exit(1)
Пример #12
0
def validate_arguments(arguments):
    """
    Validate argument values:
        <IP>
        <IP6>
        <PEER_IP>
        <AS_NUM>
        <DETACH>

    Arguments not validated:
        <DOCKER_IMAGE_NAME>
        <LOG_DIR>

    :param arguments: Docopt processed arguments
    """
    # Validate IPs
    ip_ok = arguments.get("--ip") is None or \
            arguments.get("--ip") is "" or \
            validate_ip(arguments.get("--ip"), 4)
    ip6_ok = arguments.get("--ip6") is None or \
             arguments.get("--ip6") is "" or \
             validate_ip(arguments.get("--ip6"), 6)
    container_ip_ok = arguments.get("<IP>") is None or \
                      validate_ip(arguments["<IP>"], 4) or \
                      validate_ip(arguments["<IP>"], 6)
    peer_ip_ok = arguments.get("<PEER_IP>") is None or \
                 validate_ip(arguments["<PEER_IP>"], 4) or \
                 validate_ip(arguments["<PEER_IP>"], 6)
    runtime_ok = arguments.get("--runtime") in [None, "none", "docker", "rkt"]

    asnum_ok = True
    asnum = arguments.get("<AS_NUM>") or arguments.get("--as")
    if asnum:
        asnum_ok = validate_asn(asnum)

    detach_ok = True
    if arguments.get("<DETACH>") or arguments.get("--detach"):
        detach_ok = arguments.get("--detach") in ["true", "false"]

    detach_libnetwork_ok = (arguments.get("--detach") == "true" or
                            not arguments.get("--libnetwork"))

    # Print error message
    if not ip_ok:
        print "Invalid IPv4 address specified with --ip argument."
    if not ip6_ok:
        print "Invalid IPv6 address specified with --ip6 argument."
    if not container_ip_ok or not peer_ip_ok:
        print "Invalid IP address specified."
    if not asnum_ok:
        print "Invalid AS Number specified."
    if not detach_ok:
        print "Valid values for --detach are 'true' and 'false'"
    if not detach_libnetwork_ok:
        print "The only valid value for --detach is 'true' when using libnetwork"
    if not runtime_ok:
        print "Runtime must be 'docker', 'rkt' or 'none'."

    # Exit if not valid argument
    if not (ip_ok and ip6_ok and container_ip_ok and peer_ip_ok and asnum_ok
            and detach_ok and detach_libnetwork_ok and runtime_ok):
        sys.exit(1)