Exemplo n.º 1
0
def reports(request, pk=None):
    """Lookup and return reports."""
    reports_dict = dict()
    if pk is not None:
        if not is_int(pk):
            error = {
                'report_id': [_(messages.COMMON_ID_INV)]
            }
            raise ValidationError(error)
    reports_dict['report_id'] = pk
    # details
    details_data = get_object_or_404(DetailsReport.objects.all(), report_id=pk)
    serializer = DetailsReportSerializer(details_data)
    json_details = serializer.data
    json_details.pop('cached_csv', None)
    reports_dict['details_json'] = json_details
    # deployments
    deployments_data = get_object_or_404(DeploymentsReport.objects.all(),
                                         report_id=pk)
    if deployments_data.status != DeploymentsReport.STATUS_COMPLETE:
        deployments_id = deployments_data.details_report.id
        return Response({'detail':
                         'Deployment report %s could not be created.'
                         '  See server logs.' % deployments_id},
                        status=status.HTTP_424_FAILED_DEPENDENCY)
    reports_dict['deployments_json'] = \
        build_cached_json_report(deployments_data)
    return Response(reports_dict)
Exemplo n.º 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)
Exemplo n.º 3
0
def create_details_report(json_details_report):
    """Create details report.

    Fact collection consists of a DetailsReport record
    :param json_details_report: dict representing a details report
    :returns: The newly created DetailsReport
    """
    # Create new details report
    serializer = DetailsReportSerializer(data=json_details_report)
    if serializer.is_valid():
        details_report = serializer.save()
        logger.debug('Fact collection created: %s', details_report)
        return details_report

    logger.error('Fact collection could not be persisted.')
    logger.error('Invalid json_details_report: %s', json_details_report)
    logger.error('DetailsReport errors: %s', serializer.errors)

    return None
Exemplo n.º 4
0
def details(request, pk=None):
    """Lookup and return a details system report."""
    if pk is not None:
        if not is_int(pk):
            error = {'report_id': [_(messages.COMMON_ID_INV)]}
            raise ValidationError(error)
    detail_data = get_object_or_404(DetailsReport.objects.all(), report_id=pk)
    serializer = DetailsReportSerializer(detail_data)
    json_details = serializer.data
    http_accept = request.META.get('HTTP_ACCEPT')
    if http_accept and 'text/csv' not in http_accept:
        json_details.pop('cached_csv', None)
    return Response(json_details)
Exemplo n.º 5
0
def create_details_report(report_version, json_details_report):
    """Create details report.

    Fact collection consists of a DetailsReport record
    :param report_version: major.minor.patch version of report.
    :param json_details_report: dict representing a details report
    :returns: The newly created DetailsReport
    """
    # Create new details report
    serializer = DetailsReportSerializer(data=json_details_report)
    if serializer.is_valid():
        details_report = serializer.save()
        # removed by serializer since it is read-only.  Set again.
        details_report.report_version = report_version
        details_report.save()
        logger.debug('Fact collection created: %s', details_report)
        return details_report

    logger.error('Details report could not be persisted.')
    logger.error('Invalid json_details_report: %s', json_details_report)
    logger.error('DetailsReport errors: %s', serializer.errors)

    return None
Exemplo n.º 6
0
def reports(request, pk=None):
    """Lookup and return reports."""
    reports_dict = dict()
    mask_report = request.query_params.get('mask', False)
    if pk is not None:
        if not is_int(pk):
            error = {'report_id': [_(messages.COMMON_ID_INV)]}
            raise ValidationError(error)
    reports_dict['report_id'] = pk
    # details
    details_data = get_object_or_404(DetailsReport.objects.all(), report_id=pk)
    serializer = DetailsReportSerializer(details_data)
    json_details = serializer.data
    if validate_query_param_bool(mask_report):
        json_details = mask_details_facts(json_details)
    json_details.pop('cached_csv', None)
    reports_dict['details_json'] = json_details
    # deployments
    deployments_data = get_object_or_404(DeploymentsReport.objects.all(),
                                         report_id=pk)
    if deployments_data.status != DeploymentsReport.STATUS_COMPLETE:
        deployments_id = deployments_data.details_report.id
        return Response(
            {
                'detail':
                'Deployment report %s could not be created.'
                '  See server logs.' % deployments_id
            },
            status=status.HTTP_424_FAILED_DEPENDENCY)
    deployments_report = build_cached_json_report(deployments_data,
                                                  mask_report)
    if deployments_report:
        reports_dict['deployments_json'] = deployments_report
        return Response(reports_dict)
    error = {
        'detail':
        'Deployments report %s could not be masked. '
        'Rerun the scan to generate a masked deployments report.' % (pk)
    }
    return (Response(error, status=status.HTTP_428_PRECONDITION_REQUIRED))
Exemplo n.º 7
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)