Exemple #1
0
    def create(self, request, *args, **kwargs):
        """Create a details report."""
        # pylint: disable=unused-argument
        # Validate incoming request body
        has_errors, validation_result = validate_details_report_json(
            request.data)
        if has_errors:
            return Response(validation_result,
                            status=status.HTTP_400_BAD_REQUEST)

        # Create FC model and save data
        details_report = create_details_report(request.data)
        scan_job = ScanJob(scan_type=ScanTask.SCAN_TYPE_FINGERPRINT,
                           details_report=details_report)
        scan_job.save()
        scan_job.queue()
        runner = ScanJobRunner(scan_job)
        runner.run()

        if scan_job.status != ScanTask.COMPLETED:
            # pylint: disable=no-member
            error_json = {'error': scan_job.tasks.first().status_message}
            return Response(error_json, status=status.HTTP_400_BAD_REQUEST)

        scan_job = ScanJob.objects.get(pk=scan_job.id)
        details_report = DetailsReport.objects.get(pk=details_report.id)

        # Prepare REST response body
        serializer = self.get_serializer(details_report)
        result = serializer.data
        return Response(result, status=status.HTTP_201_CREATED)
Exemple #2
0
def sync_merge_reports(request):
    """Merge reports synchronously."""
    # pylint: disable=too-many-locals
    error = {'reports': []}

    details_report_json = _convert_ids_to_json(request.data)

    has_errors, validation_result = validate_details_report_json(
        details_report_json, True)
    if has_errors:
        message = _(messages.REPORT_MERGE_NO_RESULTS % validation_result)
        error.get('reports').append(message)
        raise ValidationError(error)

    # Create FC model and save data
    details_report_json = _reconcile_source_versions(details_report_json)
    report_version = details_report_json.get('report_version', None)
    details_report = create_details_report(report_version, details_report_json)
    merge_job = ScanJob(scan_type=ScanTask.SCAN_TYPE_FINGERPRINT,
                        details_report=details_report)
    merge_job.save()
    merge_job.queue()
    runner = ScanJobRunner(merge_job)
    runner.run()

    if merge_job.status != ScanTask.COMPLETED:
        raise Exception(merge_job.status_message)

    merge_job.refresh_from_db()
    details_report = DetailsReport.objects.get(pk=details_report.id)

    # Prepare REST response body
    serializer = DetailsReportSerializer(details_report)
    result = serializer.data
    return Response(result, status=status.HTTP_201_CREATED)
Exemple #3
0
    def create(self, request, *args, **kwargs):
        """Create a details report."""
        # pylint: disable=unused-argument
        # Validate incoming request body
        has_errors, validation_result = validate_details_report_json(
            request.data, True)
        if has_errors:
            return Response(validation_result,
                            status=status.HTTP_400_BAD_REQUEST)

        report_version = request.data.get('report_version', None)
        warn_deprecated = False
        if not report_version:
            warn_deprecated = True
            report_version = create_report_version()

        # Create FC model and save data
        details_report = create_details_report(create_report_version(),
                                               request.data)
        scan_job = ScanJob(scan_type=ScanTask.SCAN_TYPE_FINGERPRINT,
                           details_report=details_report)
        scan_job.save()
        if warn_deprecated:
            scan_job.log_message(_(messages.FC_MISSING_REPORT_VERSION),
                                 log_level=logging.WARNING)

        scan_job.queue()
        runner = ScanJobRunner(scan_job)
        runner.run()

        if scan_job.status != ScanTask.COMPLETED:
            # pylint: disable=no-member
            error_json = {'error': scan_job.tasks.first().status_message}
            return Response(error_json, status=status.HTTP_400_BAD_REQUEST)

        scan_job = ScanJob.objects.get(pk=scan_job.id)
        details_report = DetailsReport.objects.get(pk=details_report.id)

        # Prepare REST response body
        serializer = self.get_serializer(details_report)
        result = serializer.data
        return Response(result, status=status.HTTP_201_CREATED)
Exemple #4
0
def sync_merge_reports(request):
    """Merge reports synchronously."""
    error = {
        'reports': []
    }
    reports = validate_merge_report(request.data)
    sources = []
    for report in reports:
        sources = sources + report.get_sources()

    details_report_json = {'sources': sources}
    has_errors, validation_result = validate_details_report_json(
        details_report_json)
    if has_errors:
        message = _(messages.REPORT_MERGE_NO_RESULTS % validation_result)
        error.get('reports').append(message)
        raise ValidationError(error)

    # Create FC model and save data
    details_report = create_details_report(details_report_json)
    scan_job = ScanJob(scan_type=ScanTask.SCAN_TYPE_FINGERPRINT,
                       details_report=details_report)
    scan_job.save()
    scan_job.queue()
    runner = ScanJobRunner(scan_job)
    runner.run()

    if scan_job.status != ScanTask.COMPLETED:
        raise Exception(scan_job.status_message)

    scan_job = ScanJob.objects.get(pk=scan_job.id)
    details_report = DetailsReport.objects.get(pk=details_report.id)

    # Prepare REST response body
    serializer = DetailsReportSerializer(details_report)
    result = serializer.data
    return Response(result, status=status.HTTP_201_CREATED)