示例#1
0
文件: config.py 项目: zhaoyun95/ava
def _check_proxy(proxy):
    """
    Checks the proxy is in ip:port format and the IP and port are valid. InvalidValueException is raised, if wrong
    format or IP/port are not valid.
    :param proxy: proxy string
    :return: proxy string
    """
    # extract parts
    url = proxy if proxy.startswith("http") else "http://" + proxy
    parsed = urlparse(url)

    # check ip
    try:
        ip = parsed.hostname
        socket.inet_aton(ip)
    except socket.error:
        raise InvalidValueException("Proxy IP must be valid")

    # check port
    try:
        port = parsed.port
    except ValueError:
        raise InvalidValueException("Proxy port must be an integer")

    # check format
    if not ip or not port:
        raise InvalidValueException("Proxy must be in the form 'ip:port'")

    return proxy
示例#2
0
文件: config.py 项目: zhaoyun95/ava
def _check_modules(package, modules):
    """
    Splits module string and checks each module exists in the given package. InvalidValueException is raised,
    if a module does not exist.
    :param package: package name
    :param modules: list of modules
    :return: list of modules
    """
    # convert package name
    package = "ava." + package

    # get package contents and class keys
    home = os.path.realpath(os.path.join(__file__, '..', '..', '..'))
    directory = os.path.join(home, package.replace('.', os.sep))
    contents = []
    keys = []
    for name in os.listdir(directory):
        if name.endswith(".py") and name != "__init__.py":
            contents.append(name)
            mod = package + '.' + name[:-3]
            imported = __import__(mod, fromlist=[package])
            for name, clazz in inspect.getmembers(imported, inspect.isclass):
                if clazz.__module__ == mod and not name.startswith('_'):
                    keys.append(clazz.key)

    # verify each module exists
    for mod in modules:
        if mod + ".py" not in contents and mod not in keys:
            raise InvalidValueException(
                "Module '{}' and check class '{}' not found".format(
                    package + '.' + mod, mod))

    return modules
示例#3
0
def _check_modules(package, modules):
    """
    Splits module string and checks each module exists in the given package. InvalidValueException is raised,
    if a module does not exist.
    :param package: package name
    :param modules: list of modules
    :return: list of modules
    """
    # get package contents
    home = os.path.realpath(os.path.join(__file__, '..', '..', '..'))
    contents = os.listdir(os.path.join(home, "ava", package))

    # filter by modules
    contents = [
        name for name in contents
        if name.endswith(".py") and name != "__init__.py"
    ]

    # verify each module exists
    for mod in modules:
        if mod + ".py" not in contents:
            raise InvalidValueException(
                "Module '{}' not found".format(package + '.' + mod))

    return modules
示例#4
0
文件: config.py 项目: zhaoyun95/ava
def _check_int(name, value):
    """
    Checks the value is greater than one. InvalidValueException is raised, if it is not.
    :param name: configuration name
    :param value: value as integer
    :return: integer value
    """
    # check value
    if value < 1:
        raise InvalidValueException(
            "Configuration '{}' must be greater than 0".format(name))

    return value
示例#5
0
文件: config.py 项目: zhaoyun95/ava
def _check_alternative_url(alternative):
    """
    Checks the url is in {http|https}://hostname[:port] format or hostname[:port]. InvalidValueException is raised, if it is not
    :param url: url string
    :return: url string
    """
    # extract parts
    url = alternative if alternative.startswith(
        "http") else "http://" + alternative
    parsed = urlparse(url)

    # check port
    try:
        parsed.port
    except ValueError:
        raise InvalidValueException("URL port must be an integer")

    # check hostname exists and path doesn't exists
    if not parsed.hostname or parsed.path:
        raise InvalidValueException(
            "URL must be in {http|https}://hostname[:port] format or hostname[:port]"
        )

    return alternative
示例#6
0
文件: config.py 项目: zhaoyun95/ava
def _check_keys(values):
    """
    Checks the keys are defined in check classes. InvalidValueException is raised, if a key is not defined.
    :param values: dictionary of payloads with keys
    :return: dictionary of payloads with keys
    """
    # get keys
    keys = []
    for clazz in utility.get_package_classes('actives'):
        keys.append(clazz.key)
    for clazz in utility.get_package_classes('blinds'):
        keys.append(clazz.key)

    # verify each key exists
    for key in values:
        if key not in keys:
            raise InvalidValueException("Check key '{}' not found".format(key))
    return values
示例#7
0
文件: config.py 项目: zhaoyun95/ava
def _check_url(url):
    """
    Checks the URL is in scheme://hostname/path format. InvalidValidException is raised, if scheme or
    network_location are missing. Default path of '/' is added, if path is missing.
    :param url: url string
    :return: url string
    """
    # extract parts
    parsed = urlparse(url)

    # check format
    if not parsed.scheme or not parsed.netloc:
        raise InvalidValueException(
            "URL must be in the form 'scheme://hostname/path'")

    # check path
    if not parsed.path:
        url += '/'

    return url
示例#8
0
def test_main_negative(mocker):
    # missing vector file
    args = []
    test = ava.scanner.main(args)
    assert test == 2

    # vector file not exists
    args = ["test.json"]
    mocker.patch("os.path.isfile", return_value=False)
    test = ava.scanner.main(args)
    assert test == 2

    # config reader missing component
    args = ["-c", "config.yml", "test.json"]
    mocker.patch("os.path.isfile", return_value=True)
    mocker.patch("ava.scanner._parse_yaml",
                 side_effect=MissingComponentException("Missing config file"))
    test = ava.scanner.main(args)
    assert test == 2

    # config reader invalid format
    args = ["-c", "config.yml", "test.json"]
    mocker.patch("os.path.isfile", return_value=True)
    mocker.patch(
        "ava.scanner._parse_yaml",
        side_effect=InvalidFormatException("Invalid config file format"))
    test = ava.scanner.main(args)
    assert test == 2

    # config reader unknown key
    args = ["-c", "config.yml", "test.json"]
    mocker.patch("os.path.isfile", return_value=True)
    mocker.patch("ava.scanner._parse_yaml",
                 side_effect=UnknownKeyException("Unknown config file key"))
    test = ava.scanner.main(args)
    assert test == 2

    # config generate invalid value
    args = ["test.json"]
    mocker.patch("os.path.isfile", return_value=True)
    mocker.patch("ava.common.config.generate",
                 side_effect=InvalidValueException("Config invalid value"))
    test = ava.scanner.main(args)
    assert test == 2

    # config generate unknown key
    args = ["test.json"]
    mocker.patch("os.path.isfile", return_value=True)
    mocker.patch("ava.common.config.generate",
                 side_effect=UnknownKeyException("Config unknown key"))
    test = ava.scanner.main(args)
    assert test == 2

    # run scanner missing component
    args = ["test.json"]
    mocker.patch("os.path.isfile", return_value=True)
    mocker.patch("ava.common.config.generate")
    mocker.patch(
        "ava.scanner._run_scanner",
        side_effect=MissingComponentException("Run missing component"))
    test = ava.scanner.main(args)
    assert test == 2

    # run scanner invalid format
    args = ["test.json"]
    mocker.patch("os.path.isfile", return_value=True)
    mocker.patch("ava.common.config.generate")
    mocker.patch("ava.scanner._run_scanner",
                 side_effect=InvalidFormatException("Invalid JSON format"))
    test = ava.scanner.main(args)
    assert test == 2