Beispiel #1
0
    def create(self, request, *args, **kwargs):
        """Create a source."""
        response = super().create(request, args, kwargs)

        # Modify json for response
        json_source = response.data
        format_source(json_source)

        # check to see if a connection scan was requested
        # through query parameter
        scan = request.query_params.get('scan', False)
        # If the scan was requested, create a connection scan
        if scan:
            if is_boolean(scan):
                if convert_to_boolean(scan):
                    # Grab the source id
                    source_id = response.data['id']
                    # Create the scan job
                    scan_job = ScanJob(scan_type=ScanTask.SCAN_TYPE_CONNECT)
                    scan_job.save()

                    # Add the source
                    scan_job.sources.add(source_id)
                    scan_job.save()

                    # Start the scan
                    start_scan.send(sender=self.__class__, instance=scan_job)
            else:
                error = {
                    'scan': [_(messages.SOURCE_CONNECTION_SCAN)]
                }
                raise ValidationError(error)
        return response
Beispiel #2
0
    def create(self, request, *args, **kwargs):
        """Create a scanjob."""
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        self.perform_create(serializer)
        headers = self.get_success_headers(serializer.data)
        scanjob_obj = ScanJob.objects.get(pk=serializer.data['id'])
        scanjob_obj.log_current_status()
        start_scan.send(sender=self.__class__, instance=scanjob_obj)

        return Response(serializer.data,
                        status=status.HTTP_201_CREATED,
                        headers=headers)
Beispiel #3
0
    def create(self, request, *args, **kwargs):
        """Create a scanjob."""
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        self.perform_create(serializer)
        headers = self.get_success_headers(serializer.data)
        scanjob_obj = ScanJob.objects.get(pk=serializer.data['id'])
        fact_endpoint = request.build_absolute_uri(reverse('facts-list'))
        start_scan.send(sender=self.__class__,
                        instance=scanjob_obj,
                        fact_endpoint=fact_endpoint)

        return Response(serializer.data,
                        status=status.HTTP_201_CREATED,
                        headers=headers)
Beispiel #4
0
    def create(self, request, *args, **kwargs):
        """Create a source."""
        response = super().create(request, args, kwargs)

        # Modify json for response
        json_source = response.data
        source_id = json_source.get('id')
        if not source_id or (source_id and not isinstance(source_id, int)):
            error = {'id': [_(messages.COMMON_ID_INV)]}
            raise ValidationError(error)

        get_object_or_404(self.queryset, pk=source_id)

        # Create expanded host cred JSON
        expand_credential(json_source)

        # check to see if a connection scan was requested
        # through query parameter
        scan = request.query_params.get('scan', False)
        # If the scan was requested, create a connection scan
        if scan:
            if is_boolean(scan):
                if convert_to_boolean(scan):
                    # Grab the source id
                    source_id = response.data['id']
                    # Define the scan options object
                    scan_options = ScanOptions()
                    scan_options.save()
                    # Create the scan job
                    scan_job = ScanJob(scan_type=ScanTask.SCAN_TYPE_CONNECT,
                                       options=scan_options)
                    scan_job.save()
                    # Add the source
                    scan_job.sources.add(source_id)
                    scan_job.save()
                    # Start the scan
                    start_scan.send(sender=self.__class__, instance=scan_job)
            else:
                error = {'scan': [_(messages.SOURCE_CONNECTION_SCAN)]}
                raise ValidationError(error)
        return response
Beispiel #5
0
def jobs(request, pk=None):
    """Get the jobs of a scan."""
    # pylint: disable=invalid-name
    if pk is not None:
        if not is_int(pk):
            return Response(status=status.HTTP_404_NOT_FOUND)
    result = []
    scan = get_object_or_404(Scan.objects.all(), pk=pk)
    if request.method == 'GET':
        job_queryset = get_job_queryset_query_set(scan,
                                                  request.query_params)
        paginator = StandardResultsSetPagination()
        page = paginator.paginate_queryset(job_queryset, request)

        if page is not None:
            serializer = ScanJobSerializer(page, many=True)
            for scan in serializer.data:
                json_scan = expand_scanjob(scan)
                result.append(json_scan)
            return paginator.get_paginated_response(serializer.data)

        for job in job_queryset:
            job_serializer = ScanJobSerializer(job)
            job_json = job_serializer.data
            job_json = expand_scanjob(job_serializer.data)
            result.append(job_json)
        return Response(result)
    else:
        job_data = {}
        job_data['scan'] = pk
        job_serializer = ScanJobSerializer(data=job_data)
        job_serializer.is_valid(raise_exception=True)
        job_serializer.save()
        scanjob_obj = ScanJob.objects.get(pk=job_serializer.data['id'])
        scanjob_obj.log_current_status()
        start_scan.send(sender=ScanViewSet.__class__, instance=scanjob_obj)

        return Response(job_serializer.data,
                        status=status.HTTP_201_CREATED)