Skip to content

Commit

Permalink
Adjust tag iterator and resources count to return correct subtypes
Browse files Browse the repository at this point in the history
  • Loading branch information
a-h-abdelsalam committed Feb 3, 2025
1 parent d02e5e2 commit 23652e6
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 5 deletions.
55 changes: 55 additions & 0 deletions src/manage_pg.c
Original file line number Diff line number Diff line change
Expand Up @@ -970,6 +970,61 @@ manage_create_sql_functions ()
" $$ LANGUAGE plpgsql"
" IMMUTABLE;");

sql ("CREATE OR REPLACE FUNCTION tag_resources_count ("
" tag_id integer,"
" tag_type text)"
" RETURNS integer AS $$"
" DECLARE table_name text;"
" DECLARE count integer;"
" DECLARE usage_types text[];"
" BEGIN"
" IF $2 = 'audit' OR $2 = 'audit_report' OR $2 = 'policy'"
" THEN"
" usage_types := ARRAY['audit', 'policy'];"
" ELSIF $2 = 'task' OR $2 = 'report' OR $2 = 'config' "
" THEN"
" usage_types := ARRAY['scan'];"
" END IF;"
" CASE"
" WHEN $2 = 'audit' OR $2 = 'policy' OR $2 = 'task' OR $2 = 'config'"
" THEN"
" SELECT resource_type INTO table_name FROM tag_resources WHERE tag = $1;"
" table_name := table_name || 's';"
" EXECUTE"
" 'SELECT count(*)"
" FROM tag_resources"
" JOIN ' || table_name || '"
" ON ' || table_name || '.id = resource"
" WHERE tag= $1 AND ' || table_name || '.usage_type = ANY($2)"
" AND resource_location = " G_STRINGIFY (LOCATION_TABLE) ";'"
" INTO count"
" USING $1, usage_types;"
" WHEN $2 = 'audit_report' OR $2 = 'report'"
" THEN"
" EXECUTE"
" 'SELECT count(*)"
" FROM tag_resources"
" JOIN tasks"
" ON tasks.id = (SELECT task FROM reports"
" WHERE reports.id = resource)"
" WHERE tag= $1 AND tasks.usage_type = ANY($2)"
" AND resource_location = " G_STRINGIFY (LOCATION_TABLE) ";'"
" INTO count"
" USING $1, usage_types; "
" ELSE"
" EXECUTE"
" 'SELECT count(*)"
" FROM tag_resources"
" WHERE tag = $1"
" AND resource_location = " G_STRINGIFY (LOCATION_TABLE) ";'"
" INTO count"
" USING $1;"
" END CASE;"
" RETURN count;"
" END;"
" $$ LANGUAGE plpgsql"
" IMMUTABLE;");

/* Functions in SQL. */

if (sql_int ("SELECT (EXISTS (SELECT * FROM information_schema.tables"
Expand Down
20 changes: 15 additions & 5 deletions src/manage_sql.c
Original file line number Diff line number Diff line change
Expand Up @@ -29703,7 +29703,7 @@ print_report_xml_start (report_t report, report_t delta, task_t task,
{
iterator_t tags;

init_resource_tag_iterator (&tags, "report", report, 1, NULL, 1);
init_resource_tag_iterator (&tags, report_type, report, 1, NULL, 1);

while (next (&tags))
{
Expand Down Expand Up @@ -57956,9 +57956,7 @@ modify_tag (const char *tag_id, const char *name, const char *comment,
{ "resource_type", NULL, KEYWORD_TYPE_STRING }, \
{ "active", NULL, KEYWORD_TYPE_INTEGER }, \
{ "value", NULL, KEYWORD_TYPE_STRING }, \
{ "(SELECT count(*) FROM tag_resources" \
" WHERE tag = tags.id" \
" AND resource_location = " G_STRINGIFY (LOCATION_TABLE) ")", \
{ "tag_resources_count (id, resource_type)", \
"resources", KEYWORD_TYPE_INTEGER }, \
{ NULL, NULL, KEYWORD_TYPE_UNKNOWN } \
}
Expand Down Expand Up @@ -58144,6 +58142,7 @@ init_resource_tag_iterator (iterator_t* iterator, const char* type,
{
get_data_t get;
gchar *owned_clause, *with_clause;
const char *parent_type;

assert (type);
assert (resource);
Expand All @@ -58153,11 +58152,21 @@ init_resource_tag_iterator (iterator_t* iterator, const char* type,
owned_clause = acl_where_owned ("tag", &get, 1, "any", 0, NULL, 0,
&with_clause);

if (type_is_report_subtype (type))
parent_type = "report";
else if (type_is_task_subtype (type))
parent_type = "task";
else if (type_is_config_subtype (type))
parent_type = "config";
else
parent_type = type;

init_iterator (iterator,
"%s"
" SELECT id, uuid, name, value, comment"
" FROM tags"
" WHERE EXISTS"
" WHERE resource_type = '%s'"
" AND EXISTS"
" (SELECT * FROM tag_resources"
" WHERE resource_type = '%s'"
" AND resource = %llu"
Expand All @@ -58168,6 +58177,7 @@ init_resource_tag_iterator (iterator_t* iterator, const char* type,
" ORDER BY %s %s;",
with_clause ? with_clause : "",
type,
parent_type,
resource,
LOCATION_TABLE,
active_only ? " AND active=1" : "",
Expand Down

0 comments on commit 23652e6

Please sign in to comment.