Ejemplo n.º 1
0
def yoda_provider(plugin, plugin_options=None):
    config = Config(
        allow_includes=False,
        plugins=[plugin]
    )
    if plugin_options:
        config.set_for(plugin, plugin_options)
    return Gixy(config=config)
Ejemplo n.º 2
0
def main():
    parser = _get_cli_parser()
    args = parser.parse_args()
    _init_logger(args.debug)

    if len(args.nginx_files) == 1 and args.nginx_files[0] != '-':
        path = os.path.expanduser(args.nginx_files[0])
        if not os.path.exists(path):
            sys.stderr.write('File {path!r} was not found.\nPlease specify correct path to configuration.\n'.format(
                path=path))
            sys.exit(1)

    try:
        severity = gixy.severity.ALL[args.level]
    except IndexError:
        sys.stderr.write('Too high level filtering. Maximum level: -{0}\n'.format('l' * (len(gixy.severity.ALL) - 1)))
        sys.exit(1)

    if args.tests:
        tests = [x.strip() for x in args.tests.split(',')]
    else:
        tests = None

    if args.skips:
        skips = [x.strip() for x in args.skips.split(',')]
    else:
        skips = None

    config = Config(
        severity=severity,
        output_format=args.output_format,
        output_file=args.output_file,
        plugins=tests,
        skips=skips,
        allow_includes=not args.disable_includes
    )

    for plugin_cls in PluginsManager().plugins_classes:
        name = plugin_cls.__name__
        options = copy.deepcopy(plugin_cls.options)
        for opt_key, opt_val in options.items():
            option_name = '{name}:{key}'.format(name=name, key=opt_key)
            if option_name not in args:
                continue

            val = getattr(args, option_name)
            if val is None:
                continue

            if isinstance(opt_val, tuple):
                val = tuple([x.strip() for x in val.split(',')])
            elif isinstance(opt_val, set):
                val = set([x.strip() for x in val.split(',')])
            elif isinstance(opt_val, list):
                val = [x.strip() for x in val.split(',')]
            options[opt_key] = val
        config.set_for(name, options)

    formatter = formatters()[config.output_format]()
    failed = False
    for input_path in args.nginx_files:
        path = os.path.abspath(os.path.expanduser(input_path))
        if not os.path.exists(path):
            LOG.error('File %s was not found', path)
            continue

        with Gixy(config=config) as yoda:
            if path == '-':
                with os.fdopen(sys.stdin.fileno(), 'rb') as fdata:
                    yoda.audit('<stdin>', fdata, is_stdin=True)
            else:
                with open(path, mode='rb') as fdata:
                    yoda.audit(path, fdata, is_stdin=False)

            formatter.feed(path, yoda)
            failed = failed or sum(yoda.stats.values()) > 0

    if args.output_file:
        with open(config.output_file, 'w') as f:
            f.write(formatter.flush())
    else:
        print(formatter.flush())

    if failed:
        # If something found - exit code must be 1, otherwise 0
        sys.exit(1)
    sys.exit(0)
Ejemplo n.º 3
0
Archivo: main.py Proyecto: yut148/gixy
def main():
    parser = _get_cli_parser()
    args = parser.parse_args()
    _init_logger(args.debug)

    path = os.path.expanduser(args.nginx_file)
    if not os.path.isfile(path):
        sys.stderr.write('Please specify path to Nginx configuration.\n\n')
        parser.print_help()
        sys.exit(1)

    try:
        severity = gixy.severity.ALL[args.level]
    except IndexError:
        sys.stderr.write(
            'Too high level filtering. Maximum level: -{}\n'.format(
                'l' * (len(gixy.severity.ALL) - 1)))
        sys.exit(1)

    if args.tests:
        tests = [x.strip() for x in args.tests.split(',')]
    else:
        tests = None

    if args.skips:
        skips = [x.strip() for x in args.skips.split(',')]
    else:
        skips = None

    config = Config(severity=severity,
                    output_format=args.output_format,
                    output_file=args.output_file,
                    plugins=tests,
                    skips=skips,
                    allow_includes=not args.disable_includes)

    for plugin_cls in PluginsManager().plugins_classes:
        name = plugin_cls.__name__
        options = copy.deepcopy(plugin_cls.options)
        for opt_key, opt_val in options.items():
            option_name = '{}:{}'.format(name, opt_key)
            if option_name not in args:
                continue

            val = getattr(args, option_name)
            if val is None:
                continue

            if isinstance(opt_val, tuple):
                val = tuple([x.strip() for x in val.split(',')])
            elif isinstance(opt_val, set):
                val = set([x.strip() for x in val.split(',')])
            elif isinstance(opt_val, list):
                val = [x.strip() for x in val.split(',')]
            options[opt_key] = val
        config.set_for(name, options)

    with Gixy(config=config) as yoda:
        yoda.audit(path)
        formatted = formatters()[config.output_format]().format(yoda)
        if args.output_file:
            with open(config.output_file, 'w') as f:
                f.write(formatted)
        else:
            print(formatted)

        if sum(yoda.stats.values()) > 0:
            # If something found - exit code must be 1, otherwise 0
            sys.exit(1)
        sys.exit(0)