Beispiel #1
0
def test_plugin_configuration(plugin_ref) -> Response:
    """
    Endpoint for testing a plugin configuration's valid connection
    """
    project = Project.find()
    payload = request.get_json()
    config_service = ConfigService(project)
    plugin = config_service.get_plugin(plugin_ref)

    # load the correct profile
    plugin.use_profile(plugin.get_profile(payload.get("profile")))

    settings = PluginSettingsService(project, show_hidden=False)

    config = payload.get("config", {})
    valid_config = {
        name: value
        for name, value in config.items()
        if validate_plugin_config(plugin, name, value, project, settings)
    }

    async def test_stream(tap_stream) -> bool:
        while not tap_stream.at_eof():
            message = await tap_stream.readline()
            json_dict = json.loads(message)
            if json_dict["type"] == "RECORD":
                return True

        return False

    async def test_extractor(config={}):
        try:
            settings_service = settings.with_config_override(
                PluginSettingsService.unredact(config))
            invoker = invoker_factory(
                project,
                plugin,
                prepare_with_session=db.session,
                plugin_settings_service=settings_service,
            )
            process = await invoker.invoke_async(stdout=asyncio.subprocess.PIPE
                                                 )
            return await test_stream(process.stdout)
        except Exception as err:
            # if anything happens, this is not successful
            return False
        finally:
            try:
                if process:
                    psutil.Process(process.pid).terminate()
            except Exception as err:
                logging.debug(err)

    loop = asyncio.get_event_loop()
    success = loop.run_until_complete(test_extractor(valid_config))

    return jsonify({"is_success": success}), 200
Beispiel #2
0
def add_plugin_configuration_profile(plugin_ref) -> Response:
    """
    Endpoint for adding a configuration profile to a plugin
    """
    payload = request.get_json()
    project = Project.find()
    config = ConfigService(project)
    plugin = config.get_plugin(plugin_ref)
    settings = PluginSettingsService(project)

    # create the new profile for this plugin
    name = payload["name"]
    profile = plugin.add_profile(slugify(name), label=name)

    config.update_plugin(plugin)

    profile_config = settings.profile_with_config(db.session,
                                                  plugin,
                                                  profile,
                                                  redacted=True)
    freeze_profile_config_keys(profile_config)

    return jsonify(profile_config)