Skip to content

Commit

Permalink
multi-tenant: add reload-tenants command
Browse files Browse the repository at this point in the history
Command to reload all tenants. Their original yaml path will be
used.

Ticket: OISF#6267.
  • Loading branch information
victorjulien committed Aug 11, 2023
1 parent 6ba0956 commit de598f1
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 0 deletions.
1 change: 1 addition & 0 deletions python/suricata/sc/suricatasc.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ def __init__(self, sck_path, verbose=False):
"pcap-last-processed",
"pcap-interrupt",
"iface-list",
"reload-tenants",
]
self.fn_commands = [
"pcap-file",
Expand Down
47 changes: 47 additions & 0 deletions src/detect-engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -3986,6 +3986,39 @@ static int DetectLoaderFuncReloadTenant(void *vctx, int loader_id)
return 0;
}

static int DetectLoaderSetupReloadTenants(const int reload_cnt)
{
int ret = 0;
DetectEngineMasterCtx *master = &g_master_de_ctx;
SCMutexLock(&master->lock);

DetectEngineCtx *de_ctx = master->list;
while (de_ctx) {
if (de_ctx->type == DETECT_ENGINE_TYPE_TENANT) {
TenantLoaderCtx *t = SCCalloc(1, sizeof(*t));
if (t == NULL) {
ret = -1;
goto error;
}
t->tenant_id = de_ctx->tenant_id;
t->reload_cnt = reload_cnt;
int loader_id = de_ctx->loader_id;

int r = DetectLoaderQueueTask(
loader_id, DetectLoaderFuncReloadTenant, t, DetectLoaderFreeTenant);
if (r < 0) {
ret = -2;
goto error;
}
}

de_ctx = de_ctx->next;
}
error:
SCMutexUnlock(&master->lock);
return ret;
}

static int DetectLoaderSetupReloadTenant(uint32_t tenant_id, const char *yaml, int reload_cnt)
{
DetectEngineCtx *old_de_ctx = DetectEngineGetByTenantId(tenant_id);
Expand Down Expand Up @@ -4042,6 +4075,20 @@ int DetectEngineReloadTenantBlocking(uint32_t tenant_id, const char *yaml, int r
return 0;
}

/** \brief Reload all tenants and wait for loading to complete
*/
int DetectEngineReloadTenantsBlocking(const int reload_cnt)
{
int r = DetectLoaderSetupReloadTenants(reload_cnt);
if (r < 0)
return r;

if (DetectLoadersSync() != 0)
return -1;

return 0;
}

static int DetectEngineMultiTenantSetupLoadLivedevMappings(const ConfNode *mappings_root_node,
bool failure_fatal)
{
Expand Down
1 change: 1 addition & 0 deletions src/detect-engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ int DetectEngineReloadIsIdle(void);

int DetectEngineLoadTenantBlocking(uint32_t tenant_id, const char *yaml);
int DetectEngineReloadTenantBlocking(uint32_t tenant_id, const char *yaml, int reload_cnt);
int DetectEngineReloadTenantsBlocking(const int reload_cnt);

int DetectEngineTenantRegisterLivedev(uint32_t tenant_id, int device_id);
int DetectEngineTenantRegisterVlanId(uint32_t tenant_id, uint16_t vlan_id);
Expand Down
35 changes: 35 additions & 0 deletions src/runmode-unix-socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -1124,6 +1124,41 @@ TmEcode UnixSocketReloadTenant(json_t *cmd, json_t* answer, void *data)
return TM_ECODE_OK;
}

/**
* \brief Command to reload all tenants
*
* \param cmd the content of command Arguments as a json_t object
* \param answer the json_t object that has to be used to answer
* \param data pointer to data defining the context here a PcapCommand::
*/
TmEcode UnixSocketReloadTenants(json_t *cmd, json_t *answer, void *data)
{
if (!(DetectEngineMultiTenantEnabled())) {
SCLogInfo("error: multi-tenant support not enabled");
json_object_set_new(answer, "message", json_string("multi-tenant support not enabled"));
return TM_ECODE_FAILED;
}

if (DetectEngineReloadTenantsBlocking(reload_cnt) != 0) {
json_object_set_new(answer, "message", json_string("reload tenants failed"));
return TM_ECODE_FAILED;
}

reload_cnt++;

/* apply to the running system */
if (DetectEngineMTApply() < 0) {
json_object_set_new(answer, "message", json_string("couldn't apply settings"));
// TODO cleanup
return TM_ECODE_FAILED;
}

SCLogNotice("reload-tenants complete");

json_object_set_new(answer, "message", json_string("reloading tenants succeeded"));
return TM_ECODE_OK;
}

/**
* \brief Command to remove a tenant
*
Expand Down
1 change: 1 addition & 0 deletions src/runmode-unix-socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ TmEcode UnixSocketRegisterTenantHandler(json_t *cmd, json_t* answer, void *data)
TmEcode UnixSocketUnregisterTenantHandler(json_t *cmd, json_t* answer, void *data);
TmEcode UnixSocketRegisterTenant(json_t *cmd, json_t* answer, void *data);
TmEcode UnixSocketReloadTenant(json_t *cmd, json_t* answer, void *data);
TmEcode UnixSocketReloadTenants(json_t *cmd, json_t *answer, void *data);
TmEcode UnixSocketUnregisterTenant(json_t *cmd, json_t* answer, void *data);
TmEcode UnixSocketHostbitAdd(json_t *cmd, json_t* answer, void *data);
TmEcode UnixSocketHostbitRemove(json_t *cmd, json_t* answer, void *data);
Expand Down
1 change: 1 addition & 0 deletions src/unix-manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -1099,6 +1099,7 @@ int UnixManagerInit(void)
UnixManagerRegisterCommand("unregister-tenant-handler", UnixSocketUnregisterTenantHandler, &command, UNIX_CMD_TAKE_ARGS);
UnixManagerRegisterCommand("register-tenant", UnixSocketRegisterTenant, &command, UNIX_CMD_TAKE_ARGS);
UnixManagerRegisterCommand("reload-tenant", UnixSocketReloadTenant, &command, UNIX_CMD_TAKE_ARGS);
UnixManagerRegisterCommand("reload-tenants", UnixSocketReloadTenants, &command, 0);
UnixManagerRegisterCommand("unregister-tenant", UnixSocketUnregisterTenant, &command, UNIX_CMD_TAKE_ARGS);
UnixManagerRegisterCommand("add-hostbit", UnixSocketHostbitAdd, &command, UNIX_CMD_TAKE_ARGS);
UnixManagerRegisterCommand("remove-hostbit", UnixSocketHostbitRemove, &command, UNIX_CMD_TAKE_ARGS);
Expand Down

0 comments on commit de598f1

Please sign in to comment.