Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Avoid DB check inserts in cleanup-sequences (Backport #2328) #2331

Merged
merged 2 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/manage.c
Original file line number Diff line number Diff line change
Expand Up @@ -975,7 +975,8 @@
manage_create_encryption_key (GSList *log_config,
const db_conn_info_t *database)
{
int ret = manage_option_setup (log_config, database);
int ret = manage_option_setup (log_config, database,

Check warning on line 978 in src/manage.c

View check run for this annotation

Codecov / codecov/patch

src/manage.c#L978

Added line #L978 was not covered by tests
0 /* avoid_db_check_inserts */);
if (ret)
{
printf ("Error setting up log config or database connection.");
Expand Down Expand Up @@ -1039,7 +1040,8 @@
const db_conn_info_t *database,
const char *uid)
{
int ret = manage_option_setup (log_config, database);
int ret = manage_option_setup (log_config, database,

Check warning on line 1043 in src/manage.c

View check run for this annotation

Codecov / codecov/patch

src/manage.c#L1043

Added line #L1043 was not covered by tests
0 /* avoid_db_check_inserts */);
if (ret)
{
printf ("Error setting up log config or database connection.\n");
Expand Down Expand Up @@ -5417,7 +5419,8 @@
return -1;
}

ret = manage_option_setup (log_config, database);
ret = manage_option_setup (log_config, database,

Check warning on line 5422 in src/manage.c

View check run for this annotation

Codecov / codecov/patch

src/manage.c#L5422

Added line #L5422 was not covered by tests
0 /* avoid_db_check_inserts */);
if (ret)
{
if (error_msg)
Expand Down
2 changes: 1 addition & 1 deletion src/manage.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ init_manage (GSList*, const db_conn_info_t *, int, int, int, int,
manage_connection_forker_t, int);

int
init_manage_helper (GSList *, const db_conn_info_t *, int);
init_manage_helper (GSList *, const db_conn_info_t *, int, int);

void
init_manage_process (const db_conn_info_t*);
Expand Down
113 changes: 75 additions & 38 deletions src/manage_sql.c
Original file line number Diff line number Diff line change
Expand Up @@ -933,13 +933,15 @@
*
* @param[in] log_config Log configuration.
* @param[in] database Database.
* @param[in] avoid_db_check_inserts Whether to avoid inserts in DB check.
*
* @return 0 success, -1 error, -2 database is too old,
* -3 database needs to be initialised from server,
* -5 database is too new.
*/
int
manage_option_setup (GSList *log_config, const db_conn_info_t *database)
manage_option_setup (GSList *log_config, const db_conn_info_t *database,

Check warning on line 943 in src/manage_sql.c

View check run for this annotation

Codecov / codecov/patch

src/manage_sql.c#L943

Added line #L943 was not covered by tests
int avoid_db_check_inserts)
{
int ret;

Expand All @@ -950,7 +952,8 @@
}

ret = init_manage_helper (log_config, database,
MANAGE_ABSOLUTE_MAX_IPS_PER_TARGET);
MANAGE_ABSOLUTE_MAX_IPS_PER_TARGET,
avoid_db_check_inserts);
assert (ret != -4);
switch (ret)
{
Expand Down Expand Up @@ -6167,10 +6170,9 @@
void
set_db_version (int version)
{
sql ("DELETE FROM %s.meta WHERE name = 'database_version';",
sql_schema ());
sql ("INSERT INTO %s.meta (name, value)"
" VALUES ('database_version', '%i');",
" VALUES ('database_version', '%i')"
" ON CONFLICT (name) DO UPDATE SET value = EXCLUDED.value;",
sql_schema (),
version);
}
Expand Down Expand Up @@ -6416,7 +6418,8 @@

g_info (" (Re-)encrypting all credentials.");

ret = manage_option_setup (log_config, database);
ret = manage_option_setup (log_config, database,

Check warning on line 6421 in src/manage_sql.c

View check run for this annotation

Codecov / codecov/patch

src/manage_sql.c#L6421

Added line #L6421 was not covered by tests
0 /* avoid_db_check_inserts */);
if (ret)
return ret;

Expand Down Expand Up @@ -6457,7 +6460,8 @@

g_info (" Decrypting all credentials.");

ret = manage_option_setup (log_config, database);
ret = manage_option_setup (log_config, database,

Check warning on line 6463 in src/manage_sql.c

View check run for this annotation

Codecov / codecov/patch

src/manage_sql.c#L6463

Added line #L6463 was not covered by tests
0 /* avoid_db_check_inserts */);
if (ret)
return ret;

Expand Down Expand Up @@ -6796,7 +6800,8 @@

g_info (" Checking alerts.");

ret = manage_option_setup (log_config, database);
ret = manage_option_setup (log_config, database,

Check warning on line 6803 in src/manage_sql.c

View check run for this annotation

Codecov / codecov/patch

src/manage_sql.c#L6803

Added line #L6803 was not covered by tests
0 /* avoid_db_check_inserts */);
if (ret)
return ret;

Expand Down Expand Up @@ -16775,11 +16780,11 @@
* Only called by init_manage_internal, and ultimately only by the main process.
*
* @param[in] check_encryption_key Whether to check encryption key.
*
* @param[in] avoid_db_check_inserts Whether to avoid inserts in DB check.
* @return 0 success, -1 error.
*/
static int
check_db (int check_encryption_key)
check_db (int check_encryption_key, int avoid_db_check_inserts)

Check warning on line 16787 in src/manage_sql.c

View check run for this annotation

Codecov / codecov/patch

src/manage_sql.c#L16787

Added line #L16787 was not covered by tests
{
/* The file locks managed at startup ensure that this is the only Manager
* process accessing the db. Nothing else should be accessing the db, access
Expand All @@ -16790,19 +16795,25 @@
create_tables ();
check_db_sequences ();
set_db_version (GVMD_DATABASE_VERSION);
check_db_roles ();
check_db_nvt_selectors ();
if (avoid_db_check_inserts == 0)
{
check_db_roles ();
check_db_nvt_selectors ();

Check warning on line 16801 in src/manage_sql.c

View check run for this annotation

Codecov / codecov/patch

src/manage_sql.c#L16800-L16801

Added lines #L16800 - L16801 were not covered by tests
}
check_db_nvts ();
check_db_port_lists ();
check_db_port_lists (avoid_db_check_inserts);

Check warning on line 16804 in src/manage_sql.c

View check run for this annotation

Codecov / codecov/patch

src/manage_sql.c#L16804

Added line #L16804 was not covered by tests
clean_auth_cache ();
if (check_db_scanners ())
if (avoid_db_check_inserts == 0 && check_db_scanners ())
goto fail;
if (check_db_report_formats ())
if (check_db_report_formats (avoid_db_check_inserts))
goto fail;
if (check_db_report_formats_trash ())
goto fail;
check_db_permissions ();
check_db_settings ();
if (avoid_db_check_inserts == 0)
{
check_db_permissions ();
check_db_settings ();

Check warning on line 16815 in src/manage_sql.c

View check run for this annotation

Codecov / codecov/patch

src/manage_sql.c#L16814-L16815

Added lines #L16814 - L16815 were not covered by tests
}
cleanup_schedule_times ();
if (check_encryption_key && check_db_encryption_key ())
goto fail;
Expand Down Expand Up @@ -16969,6 +16980,7 @@
* with GMP when an alert occurs.
* @param[in] skip_db_check Skip DB check.
* @param[in] check_encryption_key Check encryption key if doing DB check.
* @param[in] avoid_db_check_inserts Whether to avoid inserts in DB check.
*
* @return 0 success, -1 error, -2 database is too old,
* -4 max_ips_per_target out of range, -5 database is too new.
Expand All @@ -16983,7 +16995,8 @@
int stop_tasks,
manage_connection_forker_t fork_connection,
int skip_db_check,
int check_encryption_key)
int check_encryption_key,
int avoid_db_check_inserts)
{
int ret;

Expand Down Expand Up @@ -17069,16 +17082,18 @@
* 2 a helper processes (--create-user, --get-users, etc) when the
* main process is not running. */

ret = check_db (check_encryption_key);
ret = check_db (check_encryption_key, avoid_db_check_inserts);

Check warning on line 17085 in src/manage_sql.c

View check run for this annotation

Codecov / codecov/patch

src/manage_sql.c#L17085

Added line #L17085 was not covered by tests
if (ret)
return ret;

cleanup_tables ();

/* Set max_hosts in db, so database server side can access it. */

sql ("DELETE FROM meta WHERE name = 'max_hosts';");
sql ("INSERT INTO meta (name, value) VALUES ('max_hosts', %i);", max_hosts);
sql ("INSERT INTO meta (name, value)"

Check warning on line 17093 in src/manage_sql.c

View check run for this annotation

Codecov / codecov/patch

src/manage_sql.c#L17093

Added line #L17093 was not covered by tests
" VALUES ('max_hosts', %i)"
" ON CONFLICT (name) DO UPDATE SET value = EXCLUDED.value;",
max_hosts);
}

if (stop_tasks)
Expand All @@ -17092,7 +17107,7 @@

if (skip_db_check == 0)
/* Requires NVT cache. */
check_db_configs ();
check_db_configs (avoid_db_check_inserts);

Check warning on line 17110 in src/manage_sql.c

View check run for this annotation

Codecov / codecov/patch

src/manage_sql.c#L17110

Added line #L17110 was not covered by tests

sql_close ();
gvmd_db_conn_info.name = database->name ? g_strdup (database->name) : NULL;
Expand Down Expand Up @@ -17146,7 +17161,8 @@
1, /* Stop active tasks. */
fork_connection,
skip_db_check,
1); /* Check encryption key if checking db. */
1, /* Check encryption key if checking db. */
0 /* Do not avoid inserts if checking db. */);
}

/**
Expand All @@ -17158,15 +17174,16 @@
*
* @param[in] log_config Log configuration.
* @param[in] database Location of database.
* @param[in] max_ips_per_target Max number of IPs per target.
* @param[in] max_ips_per_target Max number of IPs per target.
* @param[in] avoid_db_check_inserts Whether to avoid inserts in DB check.
*
* @return 0 success, -1 error, -2 database is too old, -3 database needs
* to be initialised from server, -4 max_ips_per_target out of range,
* -5 database is too new.
*/
int
init_manage_helper (GSList *log_config, const db_conn_info_t *database,
int max_ips_per_target)
int max_ips_per_target, int avoid_db_check_inserts)
{
return init_manage_internal (log_config,
database,
Expand All @@ -17183,7 +17200,8 @@
lockfile_locked ("gvm-serving")
? 1 /* Skip DB check. */
: 0, /* Do DB check. */
0); /* Dummy. */
0, /* Dummy. */
avoid_db_check_inserts);
}

/**
Expand Down Expand Up @@ -41479,7 +41497,8 @@

g_info (" Creating scanner.");

ret = manage_option_setup (log_config, database);
ret = manage_option_setup (log_config, database,

Check warning on line 41500 in src/manage_sql.c

View check run for this annotation

Codecov / codecov/patch

src/manage_sql.c#L41500

Added line #L41500 was not covered by tests
0 /* avoid_db_check_inserts */);
if (ret)
return ret;

Expand Down Expand Up @@ -41668,7 +41687,8 @@
return 3;
}

ret = manage_option_setup (log_config, database);
ret = manage_option_setup (log_config, database,

Check warning on line 41690 in src/manage_sql.c

View check run for this annotation

Codecov / codecov/patch

src/manage_sql.c#L41690

Added line #L41690 was not covered by tests
0 /* avoid_db_check_inserts */);
if (ret)
return ret;

Expand Down Expand Up @@ -41736,7 +41756,8 @@

g_info (" Modifying scanner.");

ret = manage_option_setup (log_config, database);
ret = manage_option_setup (log_config, database,

Check warning on line 41759 in src/manage_sql.c

View check run for this annotation

Codecov / codecov/patch

src/manage_sql.c#L41759

Added line #L41759 was not covered by tests
0 /* avoid_db_check_inserts */);
if (ret)
return ret;

Expand Down Expand Up @@ -41955,7 +41976,8 @@

g_info (" Verifying scanner.");

ret = manage_option_setup (log_config, database);
ret = manage_option_setup (log_config, database,

Check warning on line 41979 in src/manage_sql.c

View check run for this annotation

Codecov / codecov/patch

src/manage_sql.c#L41979

Added line #L41979 was not covered by tests
0 /* avoid_db_check_inserts */);
if (ret)
return ret;

Expand Down Expand Up @@ -43440,7 +43462,8 @@

g_info (" Getting scanners.");

ret = manage_option_setup (log_config, database);
ret = manage_option_setup (log_config, database,

Check warning on line 43465 in src/manage_sql.c

View check run for this annotation

Codecov / codecov/patch

src/manage_sql.c#L43465

Added line #L43465 was not covered by tests
0 /* avoid_db_check_inserts */);
if (ret)
return ret;

Expand Down Expand Up @@ -47379,7 +47402,8 @@

g_info (" Getting roles.");

ret = manage_option_setup (log_config, database);
ret = manage_option_setup (log_config, database,

Check warning on line 47405 in src/manage_sql.c

View check run for this annotation

Codecov / codecov/patch

src/manage_sql.c#L47405

Added line #L47405 was not covered by tests
0 /* avoid_db_check_inserts */);
if (ret)
return ret;

Expand Down Expand Up @@ -54054,7 +54078,8 @@
return 3;
}

ret = manage_option_setup (log_config, database);
ret = manage_option_setup (log_config, database,

Check warning on line 54081 in src/manage_sql.c

View check run for this annotation

Codecov / codecov/patch

src/manage_sql.c#L54081

Added line #L54081 was not covered by tests
0 /* avoid_db_check_inserts */);
if (ret)
return ret;

Expand Down Expand Up @@ -54209,7 +54234,8 @@

g_info (" Creating user.");

ret = manage_option_setup (log_config, database);
ret = manage_option_setup (log_config, database,

Check warning on line 54237 in src/manage_sql.c

View check run for this annotation

Codecov / codecov/patch

src/manage_sql.c#L54237

Added line #L54237 was not covered by tests
0 /* avoid_db_check_inserts */);
if (ret)
return ret;

Expand Down Expand Up @@ -54299,7 +54325,8 @@

g_info (" Deleting user.");

ret = manage_option_setup (log_config, database);
ret = manage_option_setup (log_config, database,

Check warning on line 54328 in src/manage_sql.c

View check run for this annotation

Codecov / codecov/patch

src/manage_sql.c#L54328

Added line #L54328 was not covered by tests
0 /* avoid_db_check_inserts */);
if (ret)
return ret;

Expand Down Expand Up @@ -54364,7 +54391,8 @@

g_info (" Getting users.");

ret = manage_option_setup (log_config, database);
ret = manage_option_setup (log_config, database,

Check warning on line 54394 in src/manage_sql.c

View check run for this annotation

Codecov / codecov/patch

src/manage_sql.c#L54394

Added line #L54394 was not covered by tests
0 /* avoid_db_check_inserts */);
if (ret)
return ret;

Expand Down Expand Up @@ -54468,7 +54496,8 @@
return -1;
}

ret = manage_option_setup (log_config, database);
ret = manage_option_setup (log_config, database,

Check warning on line 54499 in src/manage_sql.c

View check run for this annotation

Codecov / codecov/patch

src/manage_sql.c#L54499

Added line #L54499 was not covered by tests
0 /* avoid_db_check_inserts */);
if (ret)
return ret;

Expand Down Expand Up @@ -59483,7 +59512,15 @@
return 1;
}

ret = manage_option_setup (log_config, database);
int avoid_db_check_inserts = 0;

Check warning on line 59515 in src/manage_sql.c

View check run for this annotation

Codecov / codecov/patch

src/manage_sql.c#L59515

Added line #L59515 was not covered by tests
/* The optimize=cleanup-sequences option may be used if a sequence has
* already reached its maximum value, so avoid any inserts that may cause
* a sequence maximum error. *
*/
if (strcasecmp (name, "cleanup-sequences") == 0)
avoid_db_check_inserts = 1;

Check warning on line 59521 in src/manage_sql.c

View check run for this annotation

Codecov / codecov/patch

src/manage_sql.c#L59521

Added line #L59521 was not covered by tests

ret = manage_option_setup (log_config, database, avoid_db_check_inserts);

Check warning on line 59523 in src/manage_sql.c

View check run for this annotation

Codecov / codecov/patch

src/manage_sql.c#L59523

Added line #L59523 was not covered by tests
if (ret)
return ret;

Expand Down
2 changes: 1 addition & 1 deletion src/manage_sql.h
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ void
check_alerts ();

int
manage_option_setup (GSList *, const db_conn_info_t *);
manage_option_setup (GSList *, const db_conn_info_t *, int);

void
manage_option_cleanup ();
Expand Down
7 changes: 6 additions & 1 deletion src/manage_sql_configs.c
Original file line number Diff line number Diff line change
Expand Up @@ -4532,12 +4532,17 @@

/**
* @brief Check configs, for startup.
*
* @param[in] avoid_db_check_inserts Whether to avoid inserts.
*/
void
check_db_configs ()
check_db_configs (int avoid_db_check_inserts)

Check warning on line 4539 in src/manage_sql_configs.c

View check run for this annotation

Codecov / codecov/patch

src/manage_sql_configs.c#L4539

Added line #L4539 was not covered by tests
{
migrate_predefined_configs ();

if (avoid_db_check_inserts)
return;

Check warning on line 4544 in src/manage_sql_configs.c

View check run for this annotation

Codecov / codecov/patch

src/manage_sql_configs.c#L4544

Added line #L4544 was not covered by tests

if (sync_configs_with_feed (FALSE) <= -1)
g_warning ("%s: Failed to sync configs with feed", __func__);

Expand Down
Loading
Loading