Example #1
0
    def create(self, validated_data):
        """Create a scan."""
        name = validated_data.get('name')
        check_for_existing_name(Scan.objects, name,
                                _(messages.SCAN_NAME_ALREADY_EXISTS % name))

        options = validated_data.pop('options', None)
        scan = super().create(validated_data)

        if options:
            optional_products = options.pop('disabled_optional_products', None)
            extended_search = options.pop('enabled_extended_product_search',
                                          None)
            options = ScanOptions.objects.create(**options)
            if optional_products:
                optional_products = \
                    DisabledOptionalProductsOptions.objects.create(
                        **optional_products)
                optional_products.save()
                options.disabled_optional_products = optional_products

            if extended_search:
                extended_search = \
                    ExtendedProductSearchOptions.objects.create(
                        **extended_search)
                extended_search.save()
                options.enabled_extended_product_search = extended_search
            options.save()
            scan.options = options
            scan.save()

        return scan
Example #2
0
    def create(self, validated_data):
        """Create host credential."""
        name = validated_data.get('name')
        check_for_existing_name(Credential.objects, name,
                                _(messages.HC_NAME_ALREADY_EXISTS % name))

        if 'cred_type' not in validated_data:
            error = {'cred_type': [_(messages.CRED_TYPE_REQUIRED_CREATED)]}
            raise ValidationError(error)

        cred_type = validated_data.get('cred_type')
        become_method = validated_data.get('become_method')
        become_user = validated_data.get('become_user')

        if cred_type == Credential.NETWORK_CRED_TYPE and not become_method:
            # Set the default become_method to be sudo if not specified
            validated_data['become_method'] = Credential.BECOME_SUDO
        if cred_type == Credential.NETWORK_CRED_TYPE and not become_user:
            # Set the default become_user to root if not specified
            validated_data['become_user'] = Credential.BECOME_USER_DEFAULT

        if cred_type == Credential.VCENTER_CRED_TYPE:
            validated_data = self.validate_vcenter_cred(validated_data)
        elif cred_type == Credential.SATELLITE_CRED_TYPE:
            validated_data = self.validate_satellite_cred(validated_data)
        else:
            validated_data = self.validate_host_cred(validated_data)

        return super().create(validated_data)
Example #3
0
    def update(self, instance, validated_data):
        """Update a host credential."""
        name = validated_data.get('name')
        check_for_existing_name(Credential.objects,
                                name,
                                _(messages.HC_NAME_ALREADY_EXISTS % name),
                                search_id=instance.id)

        if 'cred_type' in validated_data:
            error = {'cred_type': [_(messages.CRED_TYPE_NOT_ALLOWED_UPDATE)]}
            raise ValidationError(error)

        return super().update(instance, validated_data)
Example #4
0
    def update(self, instance, validated_data):
        """Update a scan."""
        # If we ever add optional fields to Scan, we need to
        # add logic here to clear them on full update even if they are
        # not supplied.
        name = validated_data.get('name')
        check_for_existing_name(Scan.objects,
                                name,
                                _(messages.HC_NAME_ALREADY_EXISTS % name),
                                search_id=instance.id)

        if not self.partial:
            return self._do_full_update(instance, validated_data)
        return self._do_partial_update(instance, validated_data)
Example #5
0
    def update(self, instance, validated_data):
        """Update a host credential."""
        name = validated_data.get('name')
        check_for_existing_name(Credential.objects,
                                name,
                                _(messages.HC_NAME_ALREADY_EXISTS % name),
                                search_id=instance.id)

        if 'cred_type' in validated_data:
            error = {'cred_type': [_(messages.CRED_TYPE_NOT_ALLOWED_UPDATE)]}
            raise ValidationError(error)

        cred_type = instance.cred_type
        if cred_type == Credential.VCENTER_CRED_TYPE:
            validated_data = self.validate_vcenter_cred(validated_data)
        elif cred_type == Credential.SATELLITE_CRED_TYPE:
            validated_data = self.validate_satellite_cred(validated_data)
        else:
            validated_data = self.validate_host_cred(validated_data)

        return super().update(instance, validated_data)
Example #6
0
    def update(self, instance, validated_data):
        """Update a scan."""
        # If we ever add optional fields to Scan, we need to
        # add logic here to clear them on full update even if they are
        # not supplied.
        # pylint: disable=too-many-statements,too-many-branches
        name = validated_data.get('name')
        check_for_existing_name(Scan.objects,
                                name,
                                _(messages.HC_NAME_ALREADY_EXISTS % name),
                                search_id=instance.id)

        name = validated_data.pop('name', None)
        scan_type = validated_data.pop('scan_type', None)
        sources = validated_data.pop('sources', None)
        old_options = instance.options
        options = validated_data.pop('options', None)
        if not self.partial:
            instance.name = name
            instance.scan_type = scan_type
            instance.sources = sources

            if options:
                optional_products = options.pop('disabled_optional_products',
                                                None)
                extended_search = options.pop(
                    'enabled_extended_product_search', None)
                options = ScanOptions.objects.create(**options)
                if optional_products:
                    optional_products = \
                        DisabledOptionalProductsOptions.objects.create(
                            **optional_products)
                    optional_products.save()
                    options.disabled_optional_products = optional_products

                if extended_search:
                    extended_search = \
                        ExtendedProductSearchOptions.objects.create(
                            **extended_search)
                    extended_search.save()
                    options.enabled_extended_product_search = extended_search
                options.save()
                instance.options = options
        else:
            if name is not None:
                instance.name = name
            if scan_type is not None:
                instance.scan_type = scan_type
            if sources is not None:
                instance.sources = sources
            if options is not None:
                # grab the old options
                old_optional_products = old_options.disabled_optional_products
                old_extended_search = \
                    old_options.enabled_extended_product_search
                # set the defaults
                real_extended_search = \
                    {ScanOptions.JBOSS_EAP:
                     ExtendedProductSearchOptions.EXT_JBOSS_EAP,
                     ScanOptions.JBOSS_BRMS:
                     ExtendedProductSearchOptions.EXT_JBOSS_BRMS,
                     ScanOptions.JBOSS_FUSE:
                     ExtendedProductSearchOptions.EXT_JBOSS_FUSE,
                     ScanOptions.JBOSS_WS:
                     ExtendedProductSearchOptions.EXT_JBOSS_WS}
                real_optional_products = \
                    {ScanOptions.JBOSS_EAP:
                     DisabledOptionalProductsOptions.MODEL_OPT_JBOSS_EAP,
                     ScanOptions.JBOSS_BRMS:
                     DisabledOptionalProductsOptions.MODEL_OPT_JBOSS_BRMS,
                     ScanOptions.JBOSS_FUSE:
                     DisabledOptionalProductsOptions.MODEL_OPT_JBOSS_FUSE,
                     ScanOptions.JBOSS_WS:
                     DisabledOptionalProductsOptions.MODEL_OPT_JBOSS_WS}
                # update defaults with old options if they exist
                if old_extended_search:
                    real_extended_search[ScanOptions.JBOSS_EAP] = \
                        old_extended_search.jboss_eap
                    real_extended_search[ScanOptions.JBOSS_BRMS] = \
                        old_extended_search.jboss_brms
                    real_extended_search[ScanOptions.JBOSS_FUSE] = \
                        old_extended_search.jboss_fuse
                    real_extended_search[ScanOptions.JBOSS_WS] = \
                        old_extended_search.jboss_ws
                    if old_extended_search.search_directories:
                        real_extended_search['search_directories'] = \
                            old_extended_search.search_directories
                if old_optional_products:
                    real_optional_products[ScanOptions.JBOSS_EAP] = \
                        old_optional_products.jboss_eap
                    real_optional_products[ScanOptions.JBOSS_BRMS] = \
                        old_optional_products.jboss_brms
                    real_optional_products[ScanOptions.JBOSS_FUSE] = \
                        old_optional_products.jboss_fuse
                    real_optional_products[ScanOptions.JBOSS_WS] = \
                        old_optional_products.jboss_ws
                # grab the new options
                optional_products = options.pop('disabled_optional_products',
                                                None)
                extended_search = options.pop(
                    'enabled_extended_product_search', None)
                if extended_search:
                    # Grab the new extended search options
                    jboss_eap_ext = \
                        extended_search.pop(ScanOptions.JBOSS_EAP, None)
                    jboss_fuse_ext = \
                        extended_search.pop(ScanOptions.JBOSS_FUSE, None)
                    jboss_brms_ext = \
                        extended_search.pop(ScanOptions.JBOSS_BRMS, None)
                    jboss_ws_ext = \
                        extended_search.pop(ScanOptions.JBOSS_WS, None)
                    search_directories = extended_search.pop(
                        'search_directories', None)

                    # for each extended search option, set if provided
                    # else retain the old option
                    if jboss_eap_ext is not None:
                        real_extended_search[ScanOptions.JBOSS_EAP] = \
                            jboss_eap_ext
                    if jboss_brms_ext is not None:
                        real_extended_search[ScanOptions.JBOSS_BRMS] = \
                            jboss_brms_ext
                    if jboss_fuse_ext is not None:
                        real_extended_search[ScanOptions.JBOSS_FUSE] = \
                            jboss_fuse_ext
                    if jboss_ws_ext is not None:
                        real_extended_search[ScanOptions.JBOSS_WS] = \
                            jboss_ws_ext
                    if search_directories is not None:
                        real_extended_search['search_directories'] = \
                            search_directories
                    extended_search = \
                        ExtendedProductSearchOptions.objects.create(
                            **real_extended_search)
                    extended_search.save()

                else:
                    extended_search = old_extended_search

                if optional_products:
                    jboss_eap = \
                        optional_products.pop(ScanOptions.JBOSS_EAP, None)
                    jboss_fuse = \
                        optional_products.pop(ScanOptions.JBOSS_FUSE, None)
                    jboss_brms = \
                        optional_products.pop(ScanOptions.JBOSS_BRMS, None)
                    jboss_ws = \
                        optional_products.pop(ScanOptions.JBOSS_WS, None)

                    if jboss_eap is not None:
                        real_optional_products[ScanOptions.JBOSS_EAP] = \
                            jboss_eap
                    if jboss_brms is not None:
                        real_optional_products[ScanOptions.JBOSS_BRMS] = \
                            jboss_brms
                    if jboss_fuse is not None:
                        real_optional_products[ScanOptions.JBOSS_FUSE] = \
                            jboss_fuse
                    if jboss_ws is not None:
                        real_optional_products[ScanOptions.JBOSS_WS] = \
                            jboss_ws
                    optional_products = \
                        DisabledOptionalProductsOptions.objects.create(
                            **real_optional_products)
                    optional_products.save()
                else:
                    optional_products = old_optional_products
                # create Scan Options for the instance
                instance.options = ScanOptions.objects.create(**options)
                # set the disabled products
                instance.options.disabled_optional_products = \
                    optional_products
                # set the enabled products
                instance.options.enabled_extended_product_search = \
                    extended_search
                instance.options.save()

        instance.save()
        return instance
Example #7
0
    def update(self, instance, validated_data):
        """Update a source."""
        # If we ever add optional fields to Source, we need to
        # add logic here to clear them on full update even if they are
        # not supplied.
        name = validated_data.get('name')
        check_for_existing_name(Source.objects,
                                name,
                                _(messages.SOURCE_NAME_ALREADY_EXISTS % name),
                                search_id=instance.id)

        if 'source_type' in validated_data:
            error = {'source_type': [_(messages.SOURCE_TYPE_INV)]}
            raise ValidationError(error)
        source_type = instance.source_type
        credentials = validated_data.pop('credentials', None)
        hosts_list = validated_data.pop('hosts', None)
        exclude_hosts_list = validated_data.pop('exclude_hosts', None)
        options = validated_data.pop('options', None)

        if source_type == Source.NETWORK_SOURCE_TYPE:
            if credentials:
                for cred in credentials:
                    SourceSerializer.check_credential_type(source_type, cred)
        elif source_type == Source.VCENTER_SOURCE_TYPE:
            if hosts_list and len(hosts_list) != 1:
                error = {'hosts': [_(messages.VC_ONE_HOST)]}
                raise ValidationError(error)
            if hosts_list and '[' in hosts_list[0]:
                error = {'hosts': [_(messages.VC_ONE_HOST)]}
                raise ValidationError(error)
            if exclude_hosts_list is not None:
                error = {
                    'exclude_hosts': [_(messages.VC_EXCLUDE_HOSTS_INCLUDED)]
                }
                raise ValidationError(error)
            if credentials and len(credentials) > 1:
                error = {'credentials': [_(messages.VC_ONE_CRED)]}
                raise ValidationError(error)
            if credentials and len(credentials) == 1:
                SourceSerializer.check_credential_type(source_type,
                                                       credentials[0])
        elif source_type == Source.SATELLITE_SOURCE_TYPE:
            if hosts_list and len(hosts_list) != 1:
                error = {'hosts': [_(messages.SAT_ONE_HOST)]}
                raise ValidationError(error)
            if hosts_list and '[' in hosts_list[0]:
                error = {'hosts': [_(messages.VC_ONE_HOST)]}
                raise ValidationError(error)
            if exclude_hosts_list is not None:
                error = {
                    'exclude_hosts': [_(messages.SAT_EXCLUDE_HOSTS_INCLUDED)]
                }
                raise ValidationError(error)
            if credentials and len(credentials) > 1:
                error = {'credentials': [_(messages.SAT_ONE_CRED)]}
                raise ValidationError(error)
            if credentials and len(credentials) == 1:
                SourceSerializer.check_credential_type(source_type,
                                                       credentials[0])

        for name, value in validated_data.items():
            setattr(instance, name, value)
        instance.save()

        # If hosts_list was not supplied and this is a full update,
        # then we should already have raised a ValidationError before
        # this point, so it's safe to use hosts_list as an indicator
        # of whether to replace the hosts.
        if hosts_list:
            instance.hosts = json.dumps(hosts_list)

        if exclude_hosts_list:
            instance.exclude_hosts = json.dumps(exclude_hosts_list)

        # credentials is safe to use as a flag for the same reason as
        # hosts_data above.
        if credentials:
            instance.credentials.set(credentials)

        if options:
            SourceSerializer.validate_opts(options, source_type)
            if instance.options is None:
                options = SourceOptions.objects.create(**options)
                options.save()
                instance.options = options
            else:
                self.update_options(options, instance.options)

        instance.save()
        return instance
Example #8
0
    def create(self, validated_data):
        """Create a source."""
        name = validated_data.get('name')
        check_for_existing_name(Source.objects, name,
                                _(messages.SOURCE_NAME_ALREADY_EXISTS % name))

        if 'source_type' not in validated_data:
            error = {'source_type': [_(messages.SOURCE_TYPE_REQ)]}
            raise ValidationError(error)
        source_type = validated_data.get('source_type')
        credentials = validated_data.pop('credentials')
        hosts_list = validated_data.pop('hosts', None)
        exclude_hosts_list = validated_data.pop('exclude_hosts', None)
        port = None
        if 'port' in validated_data:
            port = validated_data['port']

        options = validated_data.pop('options', None)

        if source_type == Source.NETWORK_SOURCE_TYPE:
            if credentials:
                for cred in credentials:
                    SourceSerializer.check_credential_type(source_type, cred)
            if port is None:
                validated_data['port'] = 22

        elif source_type == Source.VCENTER_SOURCE_TYPE:
            if port is None:
                validated_data['port'] = 443
            if hosts_list and len(hosts_list) != 1:
                error = {'hosts': [_(messages.VC_ONE_HOST)]}
                raise ValidationError(error)
            if hosts_list and '[' in hosts_list[0]:
                error = {'hosts': [_(messages.VC_ONE_HOST)]}
                raise ValidationError(error)
            if exclude_hosts_list is not None:
                error = {
                    'exclude_hosts': [_(messages.VC_EXCLUDE_HOSTS_INCLUDED)]
                }
                raise ValidationError(error)
            if credentials and len(credentials) > 1:
                error = {'credentials': [_(messages.VC_ONE_CRED)]}
                raise ValidationError(error)
            if credentials and len(credentials) == 1:
                SourceSerializer.check_credential_type(source_type,
                                                       credentials[0])
        elif source_type == Source.SATELLITE_SOURCE_TYPE:
            if port is None:
                validated_data['port'] = 443
            if hosts_list and len(hosts_list) != 1:
                error = {'hosts': [_(messages.SAT_ONE_HOST)]}
                raise ValidationError(error)
            if hosts_list and '[' in hosts_list[0]:
                error = {'hosts': [_(messages.VC_ONE_HOST)]}
                raise ValidationError(error)
            if exclude_hosts_list is not None:
                error = {
                    'exclude_hosts': [_(messages.SAT_EXCLUDE_HOSTS_INCLUDED)]
                }
                raise ValidationError(error)
            if credentials and len(credentials) > 1:
                error = {'credentials': [_(messages.SAT_ONE_CRED)]}
                raise ValidationError(error)
            if credentials and len(credentials) == 1:
                SourceSerializer.check_credential_type(source_type,
                                                       credentials[0])

        source = Source.objects.create(**validated_data)

        if options:
            SourceSerializer.validate_opts(options, source_type)
            options = SourceOptions.objects.create(**options)
            options.save()
            source.options = options
        elif not options and source_type == Source.SATELLITE_SOURCE_TYPE:
            options = SourceOptions()
            options.ssl_cert_verify = True
            options.save()
            source.options = options
        elif not options and source_type == Source.VCENTER_SOURCE_TYPE:
            options = SourceOptions()
            options.ssl_cert_verify = True
            options.save()
            source.options = options

        source.hosts = json.dumps(hosts_list)
        if exclude_hosts_list:
            source.exclude_hosts = json.dumps(exclude_hosts_list)

        for credential in credentials:
            source.credentials.add(credential)

        source.save()
        return source
Example #9
0
    def create(self, validated_data):
        """Create a source."""
        name = validated_data.get('name')
        check_for_existing_name(Source.objects, name,
                                _(messages.SOURCE_NAME_ALREADY_EXISTS % name))

        if 'source_type' not in validated_data:
            error = {'source_type': [_(messages.SOURCE_TYPE_REQ)]}
            raise ValidationError(error)
        source_type = validated_data.get('source_type')
        credentials = validated_data.pop('credentials')
        hosts_list = validated_data.pop('hosts', None)
        port = None
        if 'port' in validated_data:
            port = validated_data['port']

        options = validated_data.pop('options', None)

        if source_type == Source.NETWORK_SOURCE_TYPE:
            if credentials:
                for cred in credentials:
                    SourceSerializer.check_credential_type(source_type, cred)
            if port is None:
                validated_data['port'] = 22
        elif source_type == Source.VCENTER_SOURCE_TYPE:
            if port is None:
                validated_data['port'] = 443
            if hosts_list and len(hosts_list) != 1:
                error = {'hosts': [_(messages.VC_ONE_HOST)]}
                raise ValidationError(error)
            elif hosts_list and '[' in hosts_list[0]:
                error = {'hosts': [_(messages.VC_ONE_HOST)]}
                raise ValidationError(error)
            if credentials and len(credentials) > 1:
                error = {'credentials': [_(messages.VC_ONE_CRED)]}
                raise ValidationError(error)
            elif credentials and len(credentials) == 1:
                SourceSerializer.check_credential_type(source_type,
                                                       credentials[0])
        elif source_type == Source.SATELLITE_SOURCE_TYPE:
            if port is None:
                validated_data['port'] = 443
            if hosts_list and len(hosts_list) != 1:
                error = {'hosts': [_(messages.SAT_ONE_HOST)]}
                raise ValidationError(error)
            elif hosts_list and '[' in hosts_list[0]:
                error = {'hosts': [_(messages.VC_ONE_HOST)]}
                raise ValidationError(error)
            if credentials and len(credentials) > 1:
                error = {'credentials': [_(messages.SAT_ONE_CRED)]}
                raise ValidationError(error)
            elif credentials and len(credentials) == 1:
                SourceSerializer.check_credential_type(source_type,
                                                       credentials[0])

        source = Source.objects.create(**validated_data)

        if options:
            if source_type == Source.SATELLITE_SOURCE_TYPE:
                if options.get('ssl_cert_verify') is None:
                    options['ssl_cert_verify'] = True
            if (source_type == Source.VCENTER_SOURCE_TYPE
                    and options.get('ssl_cert_verify') is None):
                options['ssl_cert_verify'] = True
            if source_type == Source.NETWORK_SOURCE_TYPE and \
                    bool(options):
                invalid_options = ', '.join([key for key in options.keys()])
                error = {
                    'options': [
                        _(messages.NET_SSL_OPTIONS_NOT_ALLOWED %
                          invalid_options)
                    ]
                }
                raise ValidationError(error)

            options = SourceOptions.objects.create(**options)
            options.save()
            source.options = options
        elif not options and source_type == Source.SATELLITE_SOURCE_TYPE:
            options = SourceOptions()
            options.ssl_cert_verify = True
            options.save()
            source.options = options
        elif not options and source_type == Source.VCENTER_SOURCE_TYPE:
            options = SourceOptions()
            options.ssl_cert_verify = True
            options.save()
            source.options = options

        source.hosts = json.dumps(hosts_list)

        for credential in credentials:
            source.credentials.add(credential)

        source.save()
        return source