Пример #1
0
    def create_file_upload_tasks(self, upload_pk, channel, is_mozilla_signed):
        """
        This method creates the validation chain used during the submission
        process, combining tasks in parallel (chord) with tasks chained
        together (where the output is used as input of the next task).
        """
        tasks_in_parallel = [tasks.forward_linter_results.s(upload_pk)]

        if waffle.switch_is_active('enable-yara'):
            tasks_in_parallel.append(run_yara.s(upload_pk))

        if waffle.switch_is_active('enable-customs'):
            tasks_in_parallel.append(run_customs.s(upload_pk))

        if waffle.switch_is_active('enable-wat'):
            tasks_in_parallel.append(run_wat.s(upload_pk))

        return [
            tasks.create_initial_validation_results.si(),
            repack_fileupload.s(upload_pk),
            tasks.validate_upload.s(upload_pk, channel),
            tasks.check_for_api_keys_in_file.s(upload_pk),
            chord(tasks_in_parallel, call_mad_api.s(upload_pk)),
            tasks.handle_upload_validation_result.s(upload_pk, channel,
                                                    is_mozilla_signed),
        ]
Пример #2
0
    def test_adds_all_scanners(self, mock_chain):
        self.create_switch('enable-customs', active=True)
        self.create_switch('enable-wat', active=True)
        self.create_switch('enable-yara', active=True)
        file_upload = self.get_upload('webextension.xpi', with_validation=False)
        channel = amo.RELEASE_CHANNEL_LISTED

        utils.Validator(file_upload, listed=True)

        mock_chain.assert_called_once_with(
            tasks.create_initial_validation_results.si(),
            repack_fileupload.s(file_upload.pk),
            tasks.validate_upload.s(file_upload.pk, channel),
            tasks.check_for_api_keys_in_file.s(file_upload.pk),
            chord(
                [
                    tasks.forward_linter_results.s(file_upload.pk),
                    run_yara.s(file_upload.pk),
                    run_customs.s(file_upload.pk),
                    run_wat.s(file_upload.pk),
                ],
                call_mad_api.s(file_upload.pk),
            ),
            tasks.handle_upload_validation_result.s(file_upload.pk, channel, False),
        )
Пример #3
0
    def test_does_not_add_run_wat_when_disabled(self, mock_chain):
        self.create_switch('enable-wat', active=False)
        file_upload = self.get_upload('webextension.xpi',
                                      with_validation=False)
        channel = amo.RELEASE_CHANNEL_LISTED

        utils.Validator(file_upload, listed=True)

        mock_chain.assert_called_once_with(
            tasks.create_initial_validation_results.si(),
            repack_fileupload.s(file_upload.pk),
            tasks.validate_upload.s(file_upload.pk, channel),
            chord([tasks.forward_linter_results.s(file_upload.pk)],
                  tasks.handle_upload_validation_result.s(
                      file_upload.pk, channel, False)),
        )
Пример #4
0
    def check_upload(self, file_upload, listed=True):
        """Check that the given new file upload is validated properly."""
        # Run validator.
        utils.Validator(file_upload, listed=listed)

        channel = (amo.RELEASE_CHANNEL_LISTED
                   if listed else amo.RELEASE_CHANNEL_UNLISTED)

        # Make sure we setup the correct validation task.
        self.mock_chain.assert_called_once_with(
            tasks.create_initial_validation_results.si(),
            repack_fileupload.s(file_upload.pk),
            tasks.validate_upload.s(file_upload.pk, channel),
            tasks.handle_upload_validation_result.s(file_upload.pk, channel,
                                                    False),
        )
Пример #5
0
    def test_appends_final_task_for_file_uploads(self, mock_chain):
        final_task = mock.Mock()
        file_upload = self.get_upload('webextension.xpi',
                                      with_validation=False)
        channel = amo.RELEASE_CHANNEL_LISTED

        utils.Validator(file_upload, listed=True, final_task=final_task)

        mock_chain.assert_called_once_with(
            tasks.create_initial_validation_results.si(),
            repack_fileupload.s(file_upload.pk),
            tasks.validate_upload.s(file_upload.pk, channel),
            tasks.handle_upload_validation_result.s(file_upload.pk, channel,
                                                    False),
            final_task,
        )
Пример #6
0
    def __init__(self, file_, addon=None, listed=None, final_task=None):
        self.addon = addon
        self.file = None
        self.prev_file = None

        if isinstance(file_, FileUpload):
            assert listed is not None
            channel = (amo.RELEASE_CHANNEL_LISTED
                       if listed else amo.RELEASE_CHANNEL_UNLISTED)
            is_mozilla_signed = False

            # We're dealing with a bare file upload. Try to extract the
            # metadata that we need to match it against a previous upload
            # from the file itself.
            try:
                addon_data = parse_addon(file_, minimal=True)
                is_mozilla_signed = addon_data.get(
                    'is_mozilla_signed_extension', False)
            except ValidationError as form_error:
                log.info('could not parse addon for upload {}: {}'.format(
                    file_.pk, form_error))
                addon_data = None
            else:
                file_.update(version=addon_data.get('version'))

            assert not file_.validation

            validation_tasks = [
                tasks.create_initial_validation_results.si(),
                repack_fileupload.s(file_.pk),
                tasks.validate_upload.s(file_.pk, channel),
                tasks.handle_upload_validation_result.s(
                    file_.pk, channel, is_mozilla_signed),
            ]
        elif isinstance(file_, File):
            # The listed flag for a File object should always come from
            # the status of its owner Addon. If the caller tries to override
            # this, something is wrong.
            assert listed is None

            channel = file_.version.channel
            is_mozilla_signed = file_.is_mozilla_signed_extension

            self.file = file_
            self.addon = self.file.version.addon
            addon_data = {
                'guid': self.addon.guid,
                'version': self.file.version.version
            }

            validation_tasks = [
                tasks.create_initial_validation_results.si(),
                tasks.validate_file.s(file_.pk),
                tasks.handle_file_validation_result.s(file_.pk)
            ]
        else:
            raise ValueError

        if final_task:
            validation_tasks.append(final_task)

        self.task = chain(*validation_tasks)

        # Create a cache key for the task, so multiple requests to validate the
        # same object do not result in duplicate tasks.
        opts = file_._meta
        self.cache_key = 'validation-task:{0}.{1}:{2}:{3}'.format(
            opts.app_label, opts.object_name, file_.pk, listed)