Skip to content

Commit 5cb4306

Browse files
committed
Remove delete_agent_installer GMP command
The agent installers can no longer be deleted by users via GMP and the corresponding trash tables are no longer used. Instead the feed sync will also remove any installers not in the metadata file from the database. This simplifies the file handling for agent installers and resolves the issue that removing files from the feed would make installer entries in the database unusable. Now this has to be managed only by keeping the installer and metadata files consistent with each other.
1 parent eed7640 commit 5cb4306

10 files changed

Lines changed: 74 additions & 397 deletions

src/gmp.c

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4375,7 +4375,6 @@ typedef enum
43754375
CLIENT_CREATE_USER_SOURCES_SOURCE,
43764376
#if ENABLE_AGENTS
43774377
CLIENT_DELETE_AGENT_GROUP,
4378-
CLIENT_DELETE_AGENT_INSTALLER,
43794378
CLIENT_DELETE_AGENTS,
43804379
#endif /* ENABLE_AGENTS */
43814380
CLIENT_DELETE_ALERT,
@@ -4963,12 +4962,6 @@ gmp_xml_handle_start_element (/* unused */ GMarkupParseContext* context,
49634962
attribute_names, attribute_values);
49644963
set_client_state (CLIENT_DELETE_AGENT_GROUP);
49654964
}
4966-
else if (strcasecmp ("DELETE_AGENT_INSTALLER", element_name) == 0)
4967-
{
4968-
delete_start ("agent_installer", "Agent Installer",
4969-
attribute_names, attribute_values);
4970-
set_client_state (CLIENT_DELETE_AGENT_INSTALLER);
4971-
}
49724965
else if (strcasecmp ("DELETE_AGENTS", element_name) == 0)
49734966
{
49744967
delete_agents_start (gmp_parser, attribute_names, attribute_values);
@@ -20559,11 +20552,6 @@ gmp_xml_handle_end_element (/* unused */ GMarkupParseContext* context,
2055920552
break;
2056020553

2056120554
#if ENABLE_AGENTS
20562-
case CLIENT_DELETE_AGENT_INSTALLER:
20563-
delete_run (gmp_parser, error);
20564-
set_client_state (CLIENT_AUTHENTIC);
20565-
break;
20566-
2056720555
case CLIENT_DELETE_AGENT_GROUP:
2056820556
delete_run (gmp_parser, error);
2056920557
set_client_state (CLIENT_AUTHENTIC);

src/manage.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7040,10 +7040,6 @@ manage_run_wizard (const gchar *wizard_name,
70407040
int
70417041
delete_resource (const char *type, const char *resource_id, int ultimate)
70427042
{
7043-
#if ENABLE_AGENTS
7044-
if (strcasecmp (type, "agent_installer") == 0)
7045-
return delete_agent_installer (resource_id, ultimate);
7046-
#endif /* ENABLE_AGENTS */
70477043
#if ENABLE_CONTAINER_SCANNING
70487044
if (strcasecmp (type, "oci_image_target") == 0)
70497045
return delete_oci_image_target (resource_id, ultimate);

src/manage_agent_installers.c

Lines changed: 58 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* General management of agent installers.
1111
*/
1212

13+
#include "gmp_base.h"
1314
#include "manage_agent_installers.h"
1415
#include "manage_sql_agent_installers.h"
1516
#include <gvm/util/jsonpull.h>
@@ -487,17 +488,21 @@ get_agent_installer_data_from_json (cJSON *json,
487488
*
488489
* @param[in] entry The JSON object to process.
489490
* @param[in] rebuild Whether to also update installers with old timestamps.
491+
* @param[in,out] installers_list_sql String buffer for a list of UUIDs
492+
* of installers in the feed in SQL-quoted
493+
* form.
490494
*
491495
* @return 0 success, -1 error.
492496
*/
493497
static int
494-
agent_installers_json_handle_entry (cJSON *entry, gboolean rebuild)
498+
agent_installers_json_handle_entry (cJSON *entry, gboolean rebuild,
499+
GString *installers_list_sql)
495500
{
496501
agent_installer_data_t data;
497502
agent_installer_t agent_installer;
498-
int trash = 0;
499503
time_t db_modification_time = 0;
500504
int ret = 0;
505+
gchar *quoted_uuid;
501506

502507
memset (&data, 0, sizeof (data));
503508
if (get_agent_installer_data_from_json (entry, &data))
@@ -508,30 +513,27 @@ agent_installers_json_handle_entry (cJSON *entry, gboolean rebuild)
508513
return -1;
509514
}
510515

511-
agent_installer = agent_installer_by_uuid (data.uuid, FALSE);
516+
agent_installer = agent_installer_by_uuid (data.uuid);
512517
if (agent_installer)
513518
{
514519
db_modification_time
515-
= agent_installer_modification_time (agent_installer, FALSE);
516-
}
517-
else
518-
{
519-
agent_installer = agent_installer_by_uuid (data.uuid, TRUE);
520-
if (agent_installer)
521-
{
522-
trash = 1;
523-
db_modification_time
524-
= agent_installer_modification_time (agent_installer, TRUE);
525-
}
520+
= agent_installer_modification_time (agent_installer);
526521
}
527522

528523
if (agent_installer == 0)
529524
ret = create_agent_installer_from_data (&data);
530525
else if (rebuild || db_modification_time < data.modification_time)
531-
ret = update_agent_installer_from_data (agent_installer, trash, &data);
526+
ret = update_agent_installer_from_data (agent_installer, &data);
532527
else
533528
g_debug ("%s: skipping agent installer %s", __func__, data.uuid);
534529

530+
quoted_uuid = sql_quote (data.uuid);
531+
g_string_append_printf (installers_list_sql,
532+
"%s'%s'",
533+
installers_list_sql->len ? ", " : "",
534+
quoted_uuid);
535+
g_free (quoted_uuid);
536+
535537
g_ptr_array_free (data.cpes, TRUE);
536538
return ret ? -1 : 0;
537539
}
@@ -544,13 +546,17 @@ agent_installers_json_handle_entry (cJSON *entry, gboolean rebuild)
544546
* @param[in] parser The JSON parser.
545547
* @param[in] parser The JSON parser event data structure.
546548
* @param[in] rebuild Whether to also update installers with old timestamps.
549+
* @param[in,out] installers_list_sql String buffer for a list of UUIDs
550+
* of installers in the feed in SQL-quoted
551+
* form.
547552
*
548553
* @return 0 success, -1 error.
549554
*/
550555
static int
551556
agent_installers_json_handle_list_items (gvm_json_pull_parser_t *parser,
552557
gvm_json_pull_event_t *event,
553-
gboolean rebuild)
558+
gboolean rebuild,
559+
GString *installers_list_sql)
554560
{
555561
gvm_json_pull_parser_next (parser, event);
556562
while (event->type != GVM_JSON_PULL_EVENT_ARRAY_END)
@@ -569,7 +575,9 @@ agent_installers_json_handle_list_items (gvm_json_pull_parser_t *parser,
569575
cJSON_Delete (entry);
570576
return -1;
571577
}
572-
else if (agent_installers_json_handle_entry (entry, rebuild)) {
578+
else if (agent_installers_json_handle_entry (entry,
579+
rebuild,
580+
installers_list_sql)) {
573581
cJSON_Delete (entry);
574582
return -1;
575583
}
@@ -606,6 +614,7 @@ sync_agent_installers_with_feed (gboolean rebuild)
606614
gchar *feed_owner_uuid, *feed_owner_name;
607615
gvm_json_pull_parser_t parser;
608616
gvm_json_pull_event_t event;
617+
GString *installers_list_sql;
609618

610619
g_info ("Updating agent installers%s", rebuild ? " (rebuild)" : "");
611620
update_meta_agent_installers_last_update ();
@@ -656,7 +665,10 @@ sync_agent_installers_with_feed (gboolean rebuild)
656665
return -1;
657666
}
658667

659-
if (agent_installers_json_handle_list_items (&parser, &event, rebuild))
668+
installers_list_sql = g_string_new ("");
669+
670+
if (agent_installers_json_handle_list_items (&parser, &event, rebuild,
671+
installers_list_sql))
660672
{
661673
gvm_json_pull_parser_cleanup (&parser);
662674
gvm_json_pull_event_cleanup (&event);
@@ -667,6 +679,34 @@ sync_agent_installers_with_feed (gboolean rebuild)
667679
gvm_json_pull_parser_cleanup (&parser);
668680
gvm_json_pull_event_cleanup (&event);
669681
fclose (stream);
682+
683+
if (installers_list_sql->len)
684+
{
685+
iterator_t deleted_installers;
686+
687+
sql_begin_immediate ();
688+
689+
sql ("DELETE FROM agent_installer_cpes WHERE agent_installer NOT IN"
690+
" (SELECT id FROM agent_installers WHERE uuid IN (%s));",
691+
installers_list_sql->str);
692+
693+
init_iterator (&deleted_installers,
694+
"DELETE FROM agent_installers WHERE uuid NOT IN (%s)"
695+
" RETURNING uuid;",
696+
installers_list_sql->str);
697+
while (next (&deleted_installers))
698+
{
699+
log_event ("agent_installer", "Agent Installer",
700+
iterator_string (&deleted_installers, 0),
701+
"deleted");
702+
}
703+
cleanup_iterator (&deleted_installers);
704+
705+
sql_commit ();
706+
}
707+
else
708+
g_warning ("%s: No agent installers found in metadata file", __func__);
709+
670710
g_info ("Finished updating agent installers");
671711
return 0;
672712
}

src/manage_agent_installers.h

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,6 @@ typedef struct {
6060
void
6161
agent_installer_cpe_data_free (agent_installer_cpe_data_t *);
6262

63-
int
64-
delete_agent_installer (const char*, int);
65-
6663
FILE *
6764
open_agent_installer_file (const char *, gchar **);
6865

@@ -92,21 +89,20 @@ void
9289
manage_sync_agent_installers ();
9390

9491
int
95-
create_agent_installer_from_data (agent_installer_data_t *agent_installer_data);
92+
create_agent_installer_from_data (agent_installer_data_t *);
9693

9794
int
9895
update_agent_installer_from_data (agent_installer_t,
99-
gboolean,
100-
agent_installer_data_t *agent_installer_data);
96+
agent_installer_data_t *);
10197

10298
int
10399
agent_installer_count (const get_data_t *get);
104100

105101
agent_installer_t
106-
agent_installer_by_uuid (const char *, int);
102+
agent_installer_by_uuid (const char *);
107103

108104
time_t
109-
agent_installer_modification_time (agent_installer_t, int);
105+
agent_installer_modification_time (agent_installer_t);
110106

111107
int
112108
init_agent_installer_iterator (iterator_t*, get_data_t*);

src/manage_commands.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ command_t gmp_commands[]
5353
#if ENABLE_AGENTS
5454
{"DELETE_AGENT_GROUP", "Delete an agent group."},
5555
{"DELETE_AGENTS", "Delete one or more agents."},
56-
{"DELETE_AGENT_INSTALLER", "Delete an agent installer."},
5756
#endif /* ENABLE_AGENTS */
5857
{"DELETE_ALERT", "Delete an alert."},
5958
{"DELETE_ASSET", "Delete an asset."},

src/manage_pg.c

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2275,33 +2275,6 @@ create_tables ()
22752275
" version_end_incl text,"
22762276
" version_end_excl text);");
22772277

2278-
sql ("CREATE TABLE IF NOT EXISTS agent_installers_trash"
2279-
" (id SERIAL PRIMARY KEY,"
2280-
" uuid text UNIQUE NOT NULL,"
2281-
" owner integer REFERENCES users (id) ON DELETE RESTRICT,"
2282-
" name text NOT NULL,"
2283-
" comment text,"
2284-
" creation_time integer,"
2285-
" modification_time integer,"
2286-
" description text,"
2287-
" content_type text,"
2288-
" file_extension text,"
2289-
" installer_path text,"
2290-
" version text,"
2291-
" checksum text,"
2292-
" file_size integer,"
2293-
" last_update integer);");
2294-
2295-
sql ("CREATE TABLE IF NOT EXISTS agent_installer_cpes_trash"
2296-
" (id SERIAL PRIMARY KEY,"
2297-
" agent_installer integer"
2298-
" REFERENCES agent_installers_trash (id) ON DELETE RESTRICT,"
2299-
" criteria text,"
2300-
" version_start_incl text,"
2301-
" version_start_excl text,"
2302-
" version_end_incl text,"
2303-
" version_end_excl text);");
2304-
23052278
sql ("CREATE TABLE IF NOT EXISTS agent_groups"
23062279
" (id SERIAL PRIMARY KEY,"
23072280
" uuid TEXT NOT NULL UNIQUE,"

src/manage_resources.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,9 @@ type_has_comment (const char *type)
282282
int
283283
type_has_trash (const char *type)
284284
{
285-
return strcasecmp (type, "report")
285+
return strcasecmp (type, "agent")
286+
&& strcasecmp (type, "agent_installer")
287+
&& strcasecmp (type, "report")
286288
&& strcasecmp (type, "result")
287289
&& strcasecmp (type, "info")
288290
&& type_is_info_subtype (type) == 0

src/manage_sql.c

Lines changed: 0 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -39613,65 +39613,6 @@ manage_restore (const char *id)
3961339613
return 0;
3961439614
}
3961539615

39616-
#if ENABLE_AGENTS
39617-
/* Agent Installer. */
39618-
39619-
if (find_trash ("agent_installer", id, &resource))
39620-
{
39621-
sql_rollback ();
39622-
return -1;
39623-
}
39624-
39625-
if (resource)
39626-
{
39627-
agent_installer_t agent_installer;
39628-
39629-
agent_installer
39630-
= sql_int64_0 ("INSERT INTO agent_installers"
39631-
" (uuid, owner, name, comment,"
39632-
" creation_time, modification_time,"
39633-
" description, content_type, file_extension,"
39634-
" installer_path, version, checksum,"
39635-
" file_size, last_update)"
39636-
" SELECT uuid, owner, name, comment,"
39637-
" creation_time, modification_time,"
39638-
" description, content_type, file_extension,"
39639-
" installer_path, version, checksum,"
39640-
" file_size, last_update"
39641-
" FROM agent_installers_trash WHERE id = %llu"
39642-
" RETURNING id;",
39643-
resource);
39644-
39645-
sql ("INSERT INTO agent_installer_cpes"
39646-
" (agent_installer, criteria,"
39647-
" version_start_incl, version_start_excl,"
39648-
" version_end_incl, version_end_excl)"
39649-
" SELECT %llu, criteria,"
39650-
" version_start_incl, version_start_excl,"
39651-
" version_end_incl, version_end_excl"
39652-
" FROM agent_installer_cpes_trash WHERE agent_installer = %llu;",
39653-
agent_installer,
39654-
resource);
39655-
39656-
permissions_set_locations ("agent_installer",
39657-
resource,
39658-
agent_installer,
39659-
LOCATION_TABLE);
39660-
tags_set_locations ("agent_installer",
39661-
resource,
39662-
agent_installer,
39663-
LOCATION_TABLE);
39664-
39665-
sql ("DELETE FROM agent_installer_cpes_trash"
39666-
" WHERE agent_installer = %llu;",
39667-
resource);
39668-
sql ("DELETE FROM agent_installers_trash WHERE id = %llu;", resource);
39669-
39670-
sql_commit ();
39671-
return 0;
39672-
}
39673-
#endif /* ENABLE_AGENTS */
39674-
3967539616
/* Alert. */
3967639617

3967739618
if (find_trash ("alert", id, &resource))
@@ -40579,14 +40520,6 @@ manage_empty_trashcan ()
4057940520
" WHERE uuid = '%s'));",
4058040521
current_credentials.uuid);
4058140522
sql ("DELETE FROM groups_trash" WHERE_OWNER);
40582-
#if ENABLE_AGENTS
40583-
sql ("DELETE FROM agent_installer_cpes_trash"
40584-
" WHERE agent_installer IN (SELECT id from agent_installers_trash"
40585-
" WHERE owner = (SELECT id FROM users"
40586-
" WHERE uuid = '%s'));",
40587-
current_credentials.uuid);
40588-
sql ("DELETE FROM agent_installers_trash" WHERE_OWNER);
40589-
#endif /* ENABLE_AGENTS */
4059040523
sql ("DELETE FROM alert_condition_data_trash"
4059140524
" WHERE alert IN (SELECT id from alerts_trash"
4059240525
" WHERE owner = (SELECT id FROM users"

0 commit comments

Comments
 (0)