Example #1
0
    def __init__(self, configuration, do_validate=False):
        """Creates a Scan configuration object from the given dictionary. The general format is checked for
        correctness, but the specified workspace(s) are not checked.

        :param configuration: The Scan configuration
        :type configuration: dict
        :raises InvalidScanConfiguration: If the given configuration is invalid
        """

        self._configuration = configuration

        # Convert old versions
        if 'version' in self._configuration and self._configuration[
                'version'] == '1.0':
            self._configuration['version'] = SCHEMA_VERSION
        if 'version' not in self._configuration:
            self._configuration['version'] = SCHEMA_VERSION

        try:
            if do_validate:
                validate(self._configuration, SCAN_CONFIGURATION_SCHEMA)
        except ValidationError as ex:
            raise InvalidScanConfiguration('Invalid Scan configuration: %s' %
                                           unicode(ex))

        self._populate_default_values()
        if self._configuration['version'] not in SCHEMA_VERSIONS:
            msg = 'Invalid Scan configuration: %s is an unsupported version number'
            raise InvalidScanConfiguration(msg %
                                           self._configuration['version'])

        self._file_handler = FileHandler()
        for file_dict in self._configuration['files_to_ingest']:
            try:
                regex_pattern = re.compile(file_dict['filename_regex'])
            except re.error:
                raise InvalidScanConfiguration('Invalid file name regex: %s' %
                                               file_dict['filename_regex'])
            new_workspace = None
            if 'new_workspace' in file_dict:
                new_workspace = file_dict['new_workspace']
            new_file_path = None
            if 'new_file_path' in file_dict:
                if os.path.isabs(file_dict['new_file_path']):
                    msg = 'Invalid Scan configuration: new_file_path may not be an absolute path'
                    raise InvalidScanConfiguration(msg)
                file_dict['new_file_path'] = os.path.normpath(
                    file_dict['new_file_path'])
                new_file_path = file_dict['new_file_path']
            rule = FileRule(regex_pattern, file_dict['data_types'],
                            new_workspace, new_file_path)
            self._file_handler.add_rule(rule)
Example #2
0
    def validate(self):
        """Validates the Scan configuration

        :returns: A list of warnings discovered during validation
        :rtype: list[:class:`ingest.scan.configuration.scan_configuration.ValidationWarning`]

        :raises :class:`ingest.scan.configuration.exceptions.InvalidScanConfiguration`: If the configuration is
            invalid.
        """

        warnings = []

        scanner_type = self._configuration['scanner']['type']
        if scanner_type not in factory.get_scanner_types():
            raise InvalidScanConfiguration('\'%s\' is an invalid scanner' %
                                           scanner_type)

        scanned_workspace_name = self._configuration['workspace']
        workspace_names = {scanned_workspace_name}
        for rule in self._file_handler.rules:
            if rule.new_workspace:
                workspace_names.add(rule.new_workspace)

        for workspace in Workspace.objects.filter(name__in=workspace_names):
            if workspace.name == scanned_workspace_name:
                broker_type = workspace.get_broker().broker_type
                scanner = factory.get_scanner(scanner_type)
                if broker_type not in scanner.supported_broker_types:
                    msg = 'Scanner type %s does not support broker type %s'
                    raise InvalidScanConfiguration(msg %
                                                   (scanner_type, broker_type))
            if not workspace.is_active:
                raise InvalidScanConfiguration('Workspace is not active: %s' %
                                               workspace.name)
            workspace_names.remove(workspace.name)

        if workspace_names:
            raise InvalidScanConfiguration('Unknown workspace name: %s' %
                                           workspace_names.pop())

        return warnings
Example #3
0
    def validate(self):
        """Validates the Scan configuration

        :returns: A list of warnings discovered during validation
        :rtype: list[:class:`ingest.scan.configuration.scan_configuration.ValidationWarning`]

        :raises :class:`ingest.scan.configuration.exceptions.InvalidScanConfiguration`: If the configuration is
            invalid.
        """

        warnings = []

        scanner_type = self.scanner_type
        if scanner_type not in factory.get_scanner_types():
            raise InvalidScanConfiguration('\'%s\' is an invalid scanner' %
                                           scanner_type)

        if 'recipe' in self.config_dict:
            recipe_name = self.config_dict['recipe'][
                'name'] if 'name' in self.config_dict['recipe'] else None
            revision_num = self.config_dict['recipe'][
                'revision_num'] if 'revision_num' in self.config_dict[
                    'recipe'] else None

            if not recipe_name:
                msg = 'Recipe Type name is not defined'
                raise InvalidScanConfiguration(msg)

            if RecipeType.objects.filter(name=recipe_name).count() == 0:
                msg = 'Recipe Type %s does not exist'
                raise InvalidScanConfiguration(msg % recipe_name)

            if revision_num:
                rt = RecipeType.objects.get(name=recipe_name)
                if RecipeTypeRevision.objects.filter(
                        recipe_type=rt,
                        revision_num=revision_num).count() == 0:
                    msg = 'Recipe Type revision number %s does not exist for recipe type %s'
                    raise InvalidScanConfiguration(msg %
                                                   (revision_num, recipe_name))

        scanned_workspace_name = self.workspace
        workspace_names = {scanned_workspace_name}
        for rule in self.file_handler.rules:
            if rule.new_workspace:
                workspace_names.add(rule.new_workspace)

        for workspace in Workspace.objects.filter(name__in=workspace_names):
            if workspace.name == scanned_workspace_name:
                broker_type = workspace.get_broker().broker_type
                scanner = factory.get_scanner(scanner_type)
                if broker_type not in scanner.supported_broker_types:
                    msg = 'Scanner type %s does not support broker type %s'
                    raise InvalidScanConfiguration(msg %
                                                   (scanner_type, broker_type))
            if not workspace.is_active:
                raise InvalidScanConfiguration('Workspace is not active: %s' %
                                               workspace.name)
            workspace_names.remove(workspace.name)

        if workspace_names:
            raise InvalidScanConfiguration('Unknown workspace name: %s' %
                                           workspace_names.pop())

        return warnings