Skip to content

Commit

Permalink
new(libsinsp): add return code to plugin set_config
Browse files Browse the repository at this point in the history
Signed-off-by: Gianmatteo Palmieri <mail@gian.im>
  • Loading branch information
mrgian committed Feb 6, 2024
1 parent 589affa commit f050d8d
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 7 deletions.
10 changes: 8 additions & 2 deletions userspace/libsinsp/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,7 @@ void sinsp_plugin::validate_config_json_schema(std::string& config, std::string
}
}

void sinsp_plugin::set_config(const std::string& config)
bool sinsp_plugin::set_config(const std::string& config)
{
if(!m_inited)
{
Expand All @@ -723,10 +723,16 @@ void sinsp_plugin::set_config(const std::string& config)
std::string conf = config;
validate_config(conf);

ss_plugin_rc rc = SS_PLUGIN_FAILURE;
ss_plugin_set_config_input input;
input.config = conf.c_str();

if(m_handle->api.set_config)
{
m_handle->api.set_config(m_state, conf.c_str());
rc = m_handle->api.set_config(m_state, &input);
}

return rc == SS_PLUGIN_SUCCESS;
}

/** Event Source CAP **/
Expand Down
2 changes: 1 addition & 1 deletion userspace/libsinsp/plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ class sinsp_plugin
void destroy();
std::string get_last_error() const;
std::string get_init_schema(ss_plugin_schema_type& schema_type) const;
void set_config(const std::string& config);
bool set_config(const std::string& config);

/** Event Sourcing **/
inline uint32_t id() const
Expand Down
2 changes: 1 addition & 1 deletion userspace/libsinsp/test/plugins.ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,7 @@ TEST(sinsp_plugin, plugin_set_config)
ASSERT_TRUE(std::equal(expected.rbegin(), expected.rend(), str.rbegin()));
});

p->set_config("some config");
ASSERT_TRUE(p->set_config("some config"));

libsinsp_logger()->remove_callback_log();
}
4 changes: 3 additions & 1 deletion userspace/libsinsp/test/plugins/plugin_extract.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,12 @@ static ss_plugin_rc plugin_extract_fields(ss_plugin_t *s, const ss_plugin_event_
return SS_PLUGIN_SUCCESS;
}

static void plugin_set_config(ss_plugin_t *s, const char* config)
static ss_plugin_rc plugin_set_config(ss_plugin_t *s, const ss_plugin_set_config_input* i)
{
plugin_state *ps = (plugin_state *) s;
ps->log(ps->owner, NULL, "new config!", SS_PLUGIN_LOG_SEV_INFO);

return SS_PLUGIN_SUCCESS;
}

void get_plugin_api_sample_plugin_extract(plugin_api& out)
Expand Down
15 changes: 13 additions & 2 deletions userspace/plugin/plugin_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,14 @@ typedef struct ss_plugin_event_parse_input
ss_plugin_table_writer_vtable_ext* table_writer_ext;
} ss_plugin_event_parse_input;

// Input passed to the plugin when setting a new configuration
typedef struct ss_plugin_set_config_input
{
//
// An opaque string representing the new configuration provided by the framework
const char* config;
} ss_plugin_set_config_input;

//
// Function handler used by plugin for sending asynchronous events to the
// Falcosecurity libs during a live event capture. The asynchronous events
Expand Down Expand Up @@ -944,9 +952,12 @@ typedef struct
// Required: no
// Arguments:
// - s: the plugin state, returned by init(). Can be NULL.
// - config: the new configuration, as an opaque string.
// - i: configuration input provided by the framework.
//
void (*set_config)(ss_plugin_t* s, const char* config);
// Return value: A ss_plugin_rc with value SS_PLUGIN_SUCCESS if the config is accepted
// or SS_PLUGIN_FAILURE if the config is rejected.
// If rejected the plugin should provide context in the string returned by get_last_error().
ss_plugin_rc (*set_config)(ss_plugin_t* s, const ss_plugin_set_config_input* i);
} plugin_api;

#ifdef __cplusplus
Expand Down

0 comments on commit f050d8d

Please sign in to comment.