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

Add: New config_id option for GMP get_reports #2153

Merged
merged 2 commits into from
Apr 4, 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
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 @@ -12256,6 +12256,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 @@ -12364,6 +12366,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 @@ -12382,12 +12386,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 @@ -12594,12 +12612,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 @@ -12616,6 +12638,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 @@ -30258,6 +30281,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 @@ -30274,6 +30298,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 @@ -30326,14 +30351,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 @@ -30426,6 +30451,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 @@ -30447,7 +30473,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 @@ -30507,6 +30535,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 @@ -30533,14 +30569,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
Loading