コード例 #1
0
    def scan_file(self, filename, filename_key=None):
        """Scans a specified file, and adds information to self.data

        :type filename: str
        :param filename: full path to file to scan.

        :type filename_key: str
        :param filename_key: key to store in self.data

        :returns: boolean; though this value is only used for testing
        """

        if not filename_key:
            filename_key = filename

        if os.path.islink(filename):
            return False
        if os.path.splitext(filename)[1] in IGNORED_FILE_EXTENSIONS:
            return False
        try:
            with codecs.open(filename, encoding='utf-8') as f:
                self._extract_secrets_from_file(f, filename_key)

            return True
        except IOError:
            log.warning('Unable to open file: %s', filename)
            return False
コード例 #2
0
def from_plugin_classname(plugin_classname,
                          exclude_lines_regex=None,
                          should_verify_secrets=False,
                          **kwargs):
    """Initializes a plugin class, given a classname and kwargs.

    :type plugin_classname: str
    :param plugin_classname: subclass of BasePlugin.

    :type exclude_lines_regex: str|None
    :param exclude_lines_regex: optional regex for ignored lines.
    """
    klass = globals()[plugin_classname]

    # Make sure the instance is a BasePlugin type, before creating it.
    if not issubclass(klass, BasePlugin):  # pragma: no cover
        raise TypeError

    try:
        instance = klass(exclude_lines_regex=exclude_lines_regex,
                         should_verify=should_verify_secrets,
                         **kwargs)
    except TypeError:
        log.warning('Unable to initialize plugin!', )
        raise

    return instance
コード例 #3
0
    def scan_file(self, filename, filename_key=None):
        """Scans a specified file, and adds information to self.data

        :type filename: str
        :param filename: full path to file to scan.

        :type filename_key: str
        :param filename_key: key to store in self.data

        :returns: boolean; though this value is only used for testing
        """

        if not filename_key:
            filename_key = filename

        if os.path.islink(filename):
            return False

        try:
            with codecs.open(filename, encoding='utf-8') as f:
                self._extract_secrets_from_file(f, filename_key)

            return True
        except IOError:
            log.warning("Unable to open file: %s", filename)
            return False
コード例 #4
0
ファイル: initialize.py プロジェクト: linqining/secrets-test
def from_plugin_classname(plugin_classname,
                          exclude_lines_regex=None,
                          automaton=None,
                          should_verify_secrets=False,
                          **kwargs):
    """Initializes a plugin class, given a classname and kwargs.

    :type plugin_classname: str
    :param plugin_classname: subclass of BasePlugin.

    :type exclude_lines_regex: str|None
    :param exclude_lines_regex: optional regex for ignored lines.

    :type automaton: ahocorasick.Automaton|None
    :param automaton: optional automaton for ignoring English-words.

    :type should_verify_secrets: bool
    """
    try:
        klass = import_plugins()[plugin_classname]
    except KeyError:
        log.warning('No such plugin to initialize.')
        raise TypeError

    try:
        instance = klass(exclude_lines_regex=exclude_lines_regex,
                         automaton=automaton,
                         should_verify=should_verify_secrets,
                         **kwargs)
    except TypeError:
        log.warning('Unable to initialize plugin!')
        raise

    return instance
コード例 #5
0
def from_plugin_classname(
    plugin_classname,
    exclude_lines_regex=None,
    automaton=None,
    should_verify_secrets=False,
    plugin_filenames=None,
    **kwargs,
):
    """Initializes a plugin class, given a classname and kwargs.

    :type plugin_classname: str
    :param plugin_classname: subclass of BasePlugin.

    :type exclude_lines_regex: str|None
    :param exclude_lines_regex: optional regex for ignored lines.

    :type automaton: ahocorasick.Automaton|None
    :param automaton: optional automaton for ignoring English-words.

    :type should_verify_secrets: bool

    :type plugin_filenames: tuple
    :param plugin_filenames: the plugin filenames.

    :type plugin_filenames: tuple
    """
    try:
        klass = import_plugins(plugin_filenames)[plugin_classname]
    except KeyError:
        yellow = '\033[93m'
        end_yellow = '\033[0m'
        print(
            yellow,
            'Warning: No such %s plugin to initialize.\n' % plugin_classname,
            'Chances are you\'ve disabled it with command line options,',
            'or need to run `pre-commit autoupdate`.\n',
            'This error occurs when using a baseline file that',
            'references a plugin which is disabled or not installed.',
            end_yellow,
            file=sys.stderr,
            flush=True,
        )
        return None

    try:
        instance = klass(
            exclude_lines_regex=exclude_lines_regex,
            automaton=automaton,
            should_verify=should_verify_secrets,
            **kwargs
        )
    except TypeError:
        log.warning('Unable to initialize plugin!')
        raise

    return instance
コード例 #6
0
    def _extract_secrets_from_file(self, f, filename):
        """Extract secrets from a given file object.

        :type f:        File object
        :type filename: string
        """
        try:
            log.info('Checking file: %s', filename)

            for results, plugin in self._results_accumulator(filename):
                results.update(plugin.analyze(f, filename))
                f.seek(0)

        except UnicodeDecodeError:
            log.warning('%s failed to load.', filename)
コード例 #7
0
    def _extract_secrets_from_file(self, f, filename):
        """Extract secrets from a given file object.

        :type f:        File object
        :type filename: string
        """
        try:
            log.info("Checking file: %s", filename)

            for results, plugin in self._results_accumulator(filename):
                results.update(plugin.analyze(f, filename))
                f.seek(0)

        except UnicodeDecodeError:
            log.warning("%s failed to load.", filename)
コード例 #8
0
ファイル: initialize.py プロジェクト: miss-bug/detect-secrets
def from_plugin_classname(plugin_classname, **kwargs):
    """Initializes a plugin class, given a classname and kwargs.

    :type plugin_classname: str
    :param plugin_classname: subclass of BasePlugin
    """
    klass = globals()[plugin_classname]

    # Make sure the instance is a BasePlugin type, before creating it.
    if not issubclass(klass, BasePlugin):
        raise TypeError

    try:
        instance = klass(**kwargs)
    except TypeError:
        log.warning('Unable to initialize plugin!', )
        raise

    return instance
コード例 #9
0
ファイル: initialize.py プロジェクト: soopsio/detect-secrets
def from_plugin_classname(plugin_classname, **kwargs):
    """Initializes a plugin class, given a classname and kwargs.

    :type plugin_classname: str
    :param plugin_classname: subclass of BasePlugin
    """
    klass = globals()[plugin_classname]

    # Make sure the instance is a BasePlugin type, before creating it.
    if not issubclass(klass, BasePlugin):
        raise TypeError

    try:
        instance = klass(**kwargs)
    except TypeError:
        log.warning(
            'Unable to initialize plugin!',
        )
        raise

    return instance
コード例 #10
0
def from_plugin_classname(plugin_classname,
                          exclude_lines_regex=None,
                          automaton=None,
                          should_verify_secrets=False,
                          **kwargs):
    """Initializes a plugin class, given a classname and kwargs.

    :type plugin_classname: str
    :param plugin_classname: subclass of BasePlugin.

    :type exclude_lines_regex: str|None
    :param exclude_lines_regex: optional regex for ignored lines.

    :type automaton: ahocorasick.Automaton|None
    :param automaton: optional automaton for ignoring English-words.

    :type should_verify_secrets: bool
    """
    try:
        klass = import_plugins()[plugin_classname]
    except KeyError:
        log.error('Error: No such `{}` plugin to initialize.'.format(
            plugin_classname))
        log.error('Chances are you should run `pre-commit autoupdate`.')
        log.error(
            'This error occurs when using a baseline that was made by '
            'a newer detect-secrets version than the one running.', )
        raise TypeError

    try:
        instance = klass(exclude_lines_regex=exclude_lines_regex,
                         automaton=automaton,
                         should_verify=should_verify_secrets,
                         **kwargs)
    except TypeError:
        log.warning('Unable to initialize plugin!')
        raise

    return instance
コード例 #11
0
def merge_plugin_from_baseline(baseline_plugins, args):
    """
    :type baseline_plugins: tuple of BasePlugin
    :param baseline_plugins: BasePlugin instances from baseline file

    :type args: dict
    :param args: dictionary of arguments parsed from usage

    param priority: input param > baseline param > default

    :returns: tuple of initialized plugins
    """
    def _remove_key(d, key):
        r = dict(d)
        r.pop(key)
        return r

    baseline_plugins_dict = {
        vars(plugin)['name']: _remove_key(vars(plugin), 'name')
        for plugin in baseline_plugins
    }

    # Use input plugin as starting point
    if args.use_all_plugins:
        # input param and default param are used
        plugins_dict = dict(args.plugins)

        # baseline param priority > default
        for plugin_name, param_name, param_value in _get_prioritized_parameters(
                baseline_plugins_dict,
                args.is_using_default_value,
                prefer_default=True,
        ):
            try:
                plugins_dict[plugin_name][param_name] = param_value
            except KeyError:
                log.warning(
                    'Baseline contain plugin {} which is not in all plugins! Ignoring...',
                    plugin_name,
                )

        return from_parser_builder(
            plugins_dict,
            exclude_lines_regex=args.exclude_lines,
            should_verify_secrets=not args.no_verify,
        )

    # Use baseline plugin as starting point
    disabled_plugins = PluginOptions.get_disabled_plugins(args)
    plugins_dict = {
        plugin_name: plugin_params
        for plugin_name, plugin_params in baseline_plugins_dict.items()
        if plugin_name not in disabled_plugins
    }

    # input param priority > baseline
    input_plugins_dict = dict(args.plugins)
    for plugin_name, param_name, param_value in _get_prioritized_parameters(
            input_plugins_dict,
            args.is_using_default_value,
            prefer_default=False,
    ):
        try:
            plugins_dict[plugin_name][param_name] = param_value
        except KeyError:
            log.warning(
                '{} specified, but {} not configured! Ignoring...',
                ''.join(['--', param_name.replace('_', '-')]),
                plugin_name,
            )

    return from_parser_builder(
        plugins_dict,
        exclude_lines_regex=args.exclude_lines,
    )