예제 #1
0
    def handle_general_issue(self, item: IntelMQBotInstallIssue, bots: BOTS,
                             runtime: Runtime, pipeline: Optional[Pipeline],
                             auto: bool) -> None:
        self.logger.info('Handling Install Issues')
        for issue in item.issues:
            if isinstance(issue, AvailableExecutableIssue):
                self.output_handler.print_issue(issue)
                if auto:
                    do_fix = True
                else:
                    do_fix = query_yes_no(
                        'Do you want to remove the executable "{}" from {}'.
                        format(issue.file_name, issue.path))
                if do_fix:
                    print(colorize_text('Fixed', 'Green'))
                    remove(join(issue.path, issue.file_name))
            elif isinstance(issue, ReferenceIssue):
                self.output_handler.print_issue(issue)
                do_fix = query_yes_no(
                    'Do you want to remove the reference "{}" from {}'.format(
                        issue.reference, issue.location),
                    default='no')
                if do_fix:
                    result = False
                    if issue.location == InstallIssueLocations.RUNTIME:
                        result = self.intelmq_handler.remove_runtime_item_by_bot_id(
                            issue.reference, runtime, pipeline,
                            self.config.bin_folder)
                    elif issue.location == InstallIssueLocations.BOTS:
                        # this one is only executed either by force or if it's intelmq 2.x
                        result = self.intelmq_handler.remove_bots_item(
                            issue.reference, issue.module, issue.name, bots,
                            runtime, self.config.bin_folder)
                    else:
                        raise IntelMQToolException('Unknown location')

                    if result:
                        print(colorize_text('Fixed', 'Green'))

            elif isinstance(issue, NotInstalledIssue):
                self.output_handler.print_issue(issue)
                do_fix = query_yes_no(
                    'Do you want to install Bot "{}" ({})'.format(
                        issue.bot.name, issue.bot.module),
                    default='no')
                if do_fix:
                    print(colorize_text('Fixed', 'Green'))
                    self.output_handler.install_bot(issue.bot,
                                                    self.config.bin_folder,
                                                    self.config.bot_folder,
                                                    bots)

            else:
                raise IntelMQToolException('Issue is not known. Stopping')
예제 #2
0
    def handle_bot_issues(self,
                          issues: List[Union[MissingIssue, MismatchIssue,
                                             AdditionalIssue,
                                             MissingExecutable,
                                             MissingDefaultConfigurationIssue,
                                             MissingDescriptionIssue]],
                          bots: BOTS, runtime: Runtime, auto: bool):
        self.logger.info('Handling bot issues')
        for issue in issues:
            if isinstance(issue, MismatchIssue):
                raise NotImplementedError()
            elif isinstance(issue, MissingIssue):
                raise NotImplementedError()
            elif isinstance(issue, AdditionalIssue):
                raise NotImplementedError()
            elif isinstance(issue, MismatchInstallIssue):
                self.output_handler.print_issue(issue)
                if auto:
                    do_fix = True
                else:
                    do_fix = query_yes_no(
                        'Do you want to sync install of bot "{}" ({})'.format(
                            issue.bot.class_name, issue.bot.module),
                        default='yes')
                if do_fix:
                    print(colorize_text('Fixed', 'Green'))
                    self.output_handler.sync_folders(issue.source,
                                                     issue.destination)

            elif isinstance(issue, MissingExecutable):
                self.output_handler.print_issue(issue)
                if auto:
                    do_fix = True
                else:
                    do_fix = query_yes_no(
                        'Do you want to create executable {}'.format(
                            join(issue.path, issue.file_name)),
                        default='yes')
                if do_fix:
                    print(colorize_text('Fixed', 'Green'))
                    self.output_handler.create_executable(
                        issue.bot, self.config.bin_folder)
            elif isinstance(issue, MissingDefaultConfigurationIssue):
                message = '{}: Cannot fix {} -> Skipping as manual action required.'.format(
                    colorize_text('Major Issue', 'Red'), issue.description)
                print(message)
            elif isinstance(issue, MissingDescriptionIssue):
                message = '{}: Cannot fix {} -> Skipping as manual action required.'.format(
                    colorize_text('Major Issue', 'Red'), issue.description)
                print(message)
            else:
                raise IntelMQToolException('Issue is not known. Stopping')
예제 #3
0
    def convert_to_new_runtime(self, pipeline: Pipeline,
                               runtime: Runtime) -> Runtime:
        self.logger.info(
            'Converting pipeline.conf and runtime.conf to new setup')
        # modify runtime to fit the new pattern
        runtime_items_to_remove = list()
        for runtime_item in runtime.get_items():
            pipeline_item = pipeline.get_item_for(runtime_item.bot_id)
            if pipeline_item:
                parameter = Parameters()
                parameter.add_values({
                    'destination_queues': {
                        '_default': pipeline_item.destinations
                    }
                })
                runtime_item.parameters.merge_parameters(parameter)
            else:
                if runtime_item.group in ['Collector']:
                    message = 'No Pipeline Found fir bot "{}"'.format(
                        runtime_item.bot_id)
                    self.logger.error(message)
                    result = query_yes_no('Do you want to remove this item?')
                    if result:
                        runtime_items_to_remove.append(runtime_item)
                    else:
                        raise IntelMQParsingException(
                            'Manual Action required to solve the issue.')
                elif runtime_item.group in ['Output']:
                    self.logger.debug(
                        'Output Bot "{}" found. Taking no action'.format(
                            runtime_item.bot_id))
                else:
                    self.logger.critical(
                        'Non Collector bot found with no Pipeline. Removing "{}"'
                        .format(runtime_item.bot_id))
                    runtime_items_to_remove.append(runtime_item)

        for item in runtime_items_to_remove:
            runtime.remove_by_bot_id(item.bot_id)

        return runtime
예제 #4
0
 def handle_bots_issues(self, issues: IntelMQBotsIssue, bots: BOTS,
                        auto: bool):
     self.logger.info('Handling issues in BOTS')
     bots_item = bots.get_bot_item_by_bot(issues.bot)
     for issue in issues.issues:
         if isinstance(issue, MismatchIssue):
             self.output_handler.print_issue(issue)
             do_fix = query_yes_no(
                 'Do you want to set the value "{}" to key "{}"'.format(
                     issue.should_value, issue.key))
             if do_fix:
                 print(colorize_text('Fixed', 'Green'))
                 setattr(bots_item, issue.key, issue.should_value)
         elif isinstance(issue, MissingIssue):
             raise NotImplementedError()
         elif isinstance(issue, AdditionalIssue):
             raise NotImplementedError()
         else:
             raise IntelMQToolException('Issue is not known. Stopping')
     for issue in issues.parameter_issues:
         if isinstance(issue, MismatchIssue):
             self.output_handler.print_issue(issue)
             do_fix = query_yes_no(
                 'Do you want to set the value "{}" to key "{}"'.format(
                     issue.should_value, issue.key))
             if do_fix:
                 print(colorize_text('Fixed', 'Green'))
                 bots_item.parameters.set_value(issue.key,
                                                issue.should_value)
         elif isinstance(issue, MissingIssue):
             self.output_handler.print_issue(issue)
             if auto:
                 do_fix = True
             else:
                 do_fix = query_yes_no(
                     'Do you want to add the key "{}" with value "{}"'.
                     format(issue.key, issue.default_value))
             if do_fix:
                 print(colorize_text('Fixed', 'Green'))
                 bots_item.parameters.add_value(issue.key,
                                                issue.default_value)
         elif isinstance(issue, AdditionalIssue):
             self.output_handler.print_issue(issue)
             if auto:
                 do_fix = True
             else:
                 do_fix = query_yes_no(
                     'Do you want to add the key "{}" with value "{}"'.
                     format(issue.key, issue.value))
             if do_fix:
                 print(colorize_text('Fixed', 'Green'))
                 bots_item.parameters.add_value(issue.key, issue.value)
         elif isinstance(issue, AbsentIssue):
             self.output_handler.print_issue(issue)
             if auto:
                 do_fix = True
             else:
                 do_fix = query_yes_no(
                     'Do you want to remove the key "{}" with value "{}"'.
                     format(issue.key, issue.default_value))
             if do_fix:
                 print(colorize_text('Fixed', 'Green'))
                 bots_item.parameters.remove_key(issue.key)
         else:
             raise IntelMQToolException('Issue is not known. Stopping')
예제 #5
0
 def handle_runtime_issues(self, issues: List[IntelMQRuntimeIssue],
                           runtime: Runtime, auto: bool) -> None:
     self.logger.info('Handling Runtime Issues')
     for item in issues:
         runtime_item = runtime.get_item_by_id(item.bot_id)
         for issue in item.issues:
             if isinstance(issue, MismatchIssue):
                 self.output_handler.print_issue(issue)
                 do_fix = query_yes_no(
                     'Do you want to set the value "{}" to key "{}"'.format(
                         issue.should_value, issue.key))
                 if do_fix:
                     print(colorize_text('Fixed', 'Green'))
                     setattr(runtime_item, issue.key, issue.should_value)
             elif isinstance(issue, MissingIssue):
                 raise NotImplementedError()
             elif isinstance(issue, AdditionalIssue):
                 raise NotImplementedError()
             else:
                 raise IntelMQToolException('Issue is not known. Stopping')
         for issue in item.parameter_issues:
             if isinstance(issue, MismatchIssue):
                 self.output_handler.print_issue(issue)
                 do_fix = query_yes_no(
                     'Do you want to set the value "{}" to key "{}"'.format(
                         issue.should_value, issue.key))
                 if do_fix:
                     print(colorize_text('Fixed', 'Green'))
                     runtime_item.parameters.set_value(
                         issue.key, issue.should_value)
             elif isinstance(issue, MissingIssue):
                 self.output_handler.print_issue(issue)
                 if auto:
                     do_fix = True
                 else:
                     do_fix = query_yes_no(
                         'Do you want to add the key "{}" with value "{}"'.
                         format(issue.key, issue.default_value))
                 if do_fix:
                     print(colorize_text('Fixed', 'Green'))
                     runtime_item.parameters.add_value(
                         issue.key, issue.default_value)
             elif isinstance(issue, AdditionalIssue):
                 self.output_handler.print_issue(issue)
                 if auto:
                     do_fix = True
                 else:
                     do_fix = query_yes_no(
                         'Do you want to add the key "{}" with value "{}"'.
                         format(issue.key, issue.value))
                 if do_fix:
                     print(colorize_text('Fixed', 'Green'))
                     runtime_item.parameters.add_value(
                         issue.key, issue.value)
             elif isinstance(issue, AbsentIssue):
                 self.output_handler.print_issue(issue)
                 if auto:
                     do_fix = True
                 else:
                     do_fix = query_yes_no(
                         'Do you want to remove the key "{}" with value "{}"'
                         .format(issue.key, issue.default_value))
                 if do_fix:
                     print(colorize_text('Fixed', 'Green'))
                     runtime_item.parameters.remove_key(issue.key)
             else:
                 raise IntelMQToolException('Issue is not known. Stopping')