Skip to content

Commit

Permalink
Add: New config_id option for GMP get_reports
Browse files Browse the repository at this point in the history
It is now possible to specify a report config in the GMP command
get_reports to override the default report format parameters
with the ones from the config.

This is the first use case for the new report config type.
  • Loading branch information
timopollmeier committed Mar 13, 2024
1 parent ccb000c commit 5974519
Show file tree
Hide file tree
Showing 8 changed files with 209 additions and 25 deletions.
74 changes: 70 additions & 4 deletions src/gmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -1974,6 +1974,7 @@ typedef struct
{
get_data_t get; ///< Get args with result filtering.
get_data_t report_get; ///< Get args with report filtering.
char *config_id; ///< ID of report config.
char *delta_report_id; ///< ID of report to compare single report to.
char *format_id; ///< ID of report format.
char *alert_id; ///< ID of alert.
Expand All @@ -1995,6 +1996,7 @@ get_reports_data_reset (get_reports_data_t *data)
{
get_data_reset (&data->get);
get_data_reset (&data->report_get);
free (data->config_id);
free (data->delta_report_id);
free (data->format_id);
free (data->alert_id);
Expand Down Expand Up @@ -5526,6 +5528,9 @@ gmp_xml_handle_start_element (/* unused */ GMarkupParseContext* context,
append_attribute (attribute_names, attribute_values, "alert_id",
&get_reports_data->alert_id);

append_attribute (attribute_names, attribute_values, "config_id",
&get_reports_data->config_id);

append_attribute (attribute_names, attribute_values, "format_id",
&get_reports_data->format_id);

Expand Down Expand Up @@ -14450,8 +14455,9 @@ static void
handle_get_reports (gmp_parser_t *gmp_parser, GError **error)
{
report_t request_report = 0, delta_report = 0, report;
char no_report_format;
report_format_t report_format;
char no_report_config, no_report_format;
report_config_t report_config = 0;
report_format_t report_format = 0;
iterator_t reports;
int count, filtered, ret, first;

Expand Down Expand Up @@ -14515,6 +14521,8 @@ handle_get_reports (gmp_parser_t *gmp_parser, GError **error)
return;
}

no_report_config = (get_reports_data->config_id == NULL)
|| (strcmp(get_reports_data->config_id, "") == 0);
no_report_format = (get_reports_data->format_id == NULL)
|| (strcmp(get_reports_data->format_id, "") == 0);

Expand Down Expand Up @@ -14543,6 +14551,44 @@ handle_get_reports (gmp_parser_t *gmp_parser, GError **error)
return;
}

if (!no_report_config)
{
if (find_report_config_with_permission (get_reports_data->config_id,
&report_config,
"get_report_configs"))
{
get_reports_data_reset (get_reports_data);
SEND_TO_CLIENT_OR_FAIL (XML_INTERNAL_ERROR ("get_reports"));
set_client_state (CLIENT_AUTHENTIC);
return;
}

if (report_config == 0)
{
if (send_find_error_to_client ("get_reports", "report config",
get_reports_data->config_id,
gmp_parser))
{
error_send_to_client (error);
return;
}
get_reports_data_reset (get_reports_data);
set_client_state (CLIENT_AUTHENTIC);
return;
}

if (report_config_report_format (report_config) != report_format)
{
get_reports_data_reset (get_reports_data);
SEND_TO_CLIENT_OR_FAIL
(XML_ERROR_SYNTAX ("get_reports",
"Report config is not compatible with"
" selected report format"));
set_client_state (CLIENT_AUTHENTIC);
return;
}
}

if (get_reports_data->get.filt_id
&& strcmp (get_reports_data->get.filt_id, FILT_ID_NONE)
&& strcmp (get_reports_data->get.filt_id, FILT_ID_USER_SETTING))
Expand Down Expand Up @@ -14748,12 +14794,16 @@ handle_get_reports (gmp_parser_t *gmp_parser, GError **error)
"<report"
" id=\"%s\""
" format_id=\"%s\""
" config_id=\"%s\""
" extension=\"%s\""
" content_type=\"%s\">",
report_iterator_uuid (&reports),
no_report_format
? ""
: get_reports_data->format_id,
no_report_config
? ""
: get_reports_data->config_id,
extension,
content_type);

Expand Down Expand Up @@ -14822,7 +14872,7 @@ handle_get_reports (gmp_parser_t *gmp_parser, GError **error)
g_free (report_task_uuid);
}

if (get_reports_data->format_id)
if (!no_report_format)
{
gchar *format_name = NULL;
format_name = report_format_name (report_format);
Expand All @@ -14834,7 +14884,22 @@ handle_get_reports (gmp_parser_t *gmp_parser, GError **error)
"</report_format>",
get_reports_data->format_id,
format_name ? format_name : "");
// g_free (report_format_name);
free (format_name);
}

if (!no_report_config)
{
gchar *config_name = NULL;
config_name = report_config_name (report_config);

buffer_xml_append_printf
(prefix,
"<report_config id=\"%s\">"
"<name>%s</name>"
"</report_config>",
get_reports_data->config_id,
config_name ? config_name : "");
free (config_name);
}

}
Expand All @@ -14850,6 +14915,7 @@ handle_get_reports (gmp_parser_t *gmp_parser, GError **error)
ret = manage_send_report (report,
delta_report,
no_report_format ? -1 : report_format,
report_config,
&get_reports_data->get,
get_reports_data->notes_details,
get_reports_data->overrides_details,
Expand Down
6 changes: 4 additions & 2 deletions src/manage.h
Original file line number Diff line number Diff line change
Expand Up @@ -1634,12 +1634,14 @@ int
report_progress (report_t);

gchar *
manage_report (report_t, report_t, const get_data_t *, report_format_t,
manage_report (report_t, report_t, const get_data_t *,
report_format_t, report_config_t,
int, int, gsize *, gchar **, gchar **, gchar **, gchar **,
gchar **);

int
manage_send_report (report_t, report_t, report_format_t, const get_data_t *,
manage_send_report (report_t, report_t, report_format_t, report_config_t,
const get_data_t *,
int, int, int, int, int, int,
gboolean (*) (const char *,
int (*) (const char*, void*),
Expand Down
3 changes: 3 additions & 0 deletions src/manage_report_configs.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ modify_report_config (const char *, const char *, const char *, array_t *,
int
delete_report_config (const char *, int);

char *
report_config_name (report_format_t);

char *
report_config_uuid (report_config_t);

Expand Down
46 changes: 41 additions & 5 deletions src/manage_sql.c
Original file line number Diff line number Diff line change
Expand Up @@ -12254,6 +12254,8 @@ report_content_for_alert (alert_t alert, report_t report, task_t task,
{
int ret;
report_format_t report_format;
gboolean report_format_is_fallback = FALSE;
report_config_t report_config;
get_data_t *alert_filter_get;
gchar *report_content;
filter_t filter;
Expand Down Expand Up @@ -12362,6 +12364,8 @@ report_content_for_alert (alert_t alert, report_t report, task_t task,
return -1;
}

report_format_is_fallback = TRUE;

if (find_report_format_with_permission
(fallback_format_id,
&report_format,
Expand All @@ -12380,12 +12384,26 @@ report_content_for_alert (alert_t alert, report_t report, task_t task,
}
}

// Get report config

if (report_format_is_fallback)
{
// Config would only be valid for the original report format
report_config = 0;
}
else
{
// TODO: Get report config from alert
report_config = 0;
}

// Generate report content

report_content = manage_report (report,
get_delta_report (alert, task, report),
alert_filter_get ? alert_filter_get : get,
report_format,
report_config,
notes_details,
overrides_details,
content_length,
Expand Down Expand Up @@ -12592,12 +12610,16 @@ escalate_to_vfire (alert_t alert, task_t task, report_t report, event_t event,
{
gchar *report_format_id;
report_format_t report_format;
report_config_t report_config;

report_format_id = g_strstrip (*point);
find_report_format_with_permission (report_format_id,
&report_format,
"get_report_formats");

// TODO: Add option to set report configs
report_config = 0;

if (report_format)
{
alert_report_data_t *alert_report_item;
Expand All @@ -12614,6 +12636,7 @@ escalate_to_vfire (alert_t alert, task_t task, report_t report, event_t event,
? alert_filter_get
: get,
report_format,
report_config,
notes_details,
overrides_details,
&content_length,
Expand Down Expand Up @@ -30206,6 +30229,7 @@ print_report_xml_start (report_t report, report_t delta, task_t task,
* @param[in] delta_report Report to compare with.
* @param[in] get GET data for report.
* @param[in] report_format Report format.
* @param[in] report_config Report config.
* @param[in] notes_details If notes, Whether to include details.
* @param[in] overrides_details If overrides, Whether to include details.
* @param[out] output_length NULL or location for length of return.
Expand All @@ -30222,6 +30246,7 @@ print_report_xml_start (report_t report, report_t delta, task_t task,
gchar *
manage_report (report_t report, report_t delta_report, const get_data_t *get,
const report_format_t report_format,
const report_config_t report_config,
int notes_details, int overrides_details,
gsize *output_length, gchar **extension, gchar **content_type,
gchar **filter_term_return, gchar **zone_return,
Expand Down Expand Up @@ -30274,14 +30299,14 @@ manage_report (report_t report, report_t delta_report, const get_data_t *get,
/* Apply report format(s) */
gchar* report_format_id = report_format_uuid (report_format);

output_file = apply_report_format (report_format_id,
output_file = apply_report_format (report_format_id, report_config,
xml_start, xml_file, xml_dir,
&used_rfps);
g_free (report_format_id);
}
else
{
print_report_xml_end(xml_start, xml_file, -1);
print_report_xml_end (xml_start, xml_file, -1, 0);
output_file = g_strdup(xml_file);
}

Expand Down Expand Up @@ -30374,6 +30399,7 @@ manage_report (report_t report, report_t delta_report, const get_data_t *get,
* @param[in] report Report.
* @param[in] delta_report Report to compare with.
* @param[in] report_format Report format.
* @param[in] report_config Report config.
* @param[in] get GET command data.
* @param[in] notes_details If notes, Whether to include details.
* @param[in] overrides_details If overrides, Whether to include details.
Expand All @@ -30395,7 +30421,9 @@ manage_report (report_t report, report_t delta_report, const get_data_t *get,
*/
int
manage_send_report (report_t report, report_t delta_report,
report_format_t report_format, const get_data_t *get,
report_format_t report_format,
report_config_t report_config,
const get_data_t *get,
int notes_details, int overrides_details, int result_tags,
int ignore_pagination, int lean, int base64,
gboolean (*send) (const char *,
Expand Down Expand Up @@ -30455,6 +30483,14 @@ manage_send_report (report_t report, report_t delta_report,
&& (report_format_trust (report_format) != TRUST_YES))
return -1;

if (report_config
&& report_config_report_format (report_config) != report_format)
{
g_warning ("%s: report config is not compatible with report_format",
__func__);
return -1;
}

if (mkdtemp (xml_dir) == NULL)
{
g_warning ("%s: mkdtemp failed", __func__);
Expand All @@ -30481,14 +30517,14 @@ manage_send_report (report_t report, report_t delta_report,
/* Apply report format(s). */
gchar* report_format_id = report_format_uuid (report_format);

output_file = apply_report_format (report_format_id,
output_file = apply_report_format (report_format_id, report_config,
xml_start, xml_file, xml_dir,
&used_rfps);
g_free (report_format_id);
}
else
{
print_report_xml_end(xml_start, xml_file, -1);
print_report_xml_end (xml_start, xml_file, -1, 0);
output_file = g_strdup(xml_file);
}

Expand Down
14 changes: 14 additions & 0 deletions src/manage_sql_report_configs.c
Original file line number Diff line number Diff line change
Expand Up @@ -1032,6 +1032,20 @@ report_config_param_iterator_using_default (iterator_t *iterator)

/* Misc. functions */

/**
* @brief Return the name of a config config.
*
* @param[in] report_config Report config.
*
* @return Newly allocated UUID.
*/
char *
report_config_name (report_config_t report_config)
{
return sql_string ("SELECT name FROM report_configs WHERE id = %llu;",
report_config);
}

/**
* @brief Return the UUID of a config config.
*
Expand Down
Loading

0 comments on commit 5974519

Please sign in to comment.