From 25efd49dee537942fcc0996453ea13b5222da930 Mon Sep 17 00:00:00 2001 From: Johannes Helmold Date: Fri, 7 May 2021 14:27:31 +0200 Subject: [PATCH 1/5] Added a new modification_time column to reports Added the new function "migrate_243_to_244 ()" in manage_migrators.c to add the new column "modification_time" to the "reports" table. Adjusted GVMD_DATABASE_VERSION in CMakelists.txt Adjusted the corresponding entries in the REPORT_ITERATOR_COLUMNS in manage_sql.c --- CMakeLists.txt | 2 +- src/manage_migrators.c | 32 ++++++++++++++++++++++++++++++++ src/manage_sql.c | 4 ++-- 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2e67f6ccc..3530a951b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -96,7 +96,7 @@ include (CPack) ## Variables -set (GVMD_DATABASE_VERSION 243) +set (GVMD_DATABASE_VERSION 244) set (GVMD_SCAP_DATABASE_VERSION 18) diff --git a/src/manage_migrators.c b/src/manage_migrators.c index 459169bb2..b984b9895 100644 --- a/src/manage_migrators.c +++ b/src/manage_migrators.c @@ -2682,6 +2682,37 @@ migrate_242_to_243 () return 0; } +/** + * @brief Migrate the database from version 243 to version 244. + * + * @return 0 success, -1 error. + */ +int +migrate_243_to_244 () +{ + sql_begin_immediate (); + + /* Ensure that the database is currently version 243. */ + + if (manage_db_version () != 243) + { + sql_rollback (); + return -1; + } + + /* Update the database. */ + + sql ("ALTER TABLE reports ADD COLUMN modification_time integer;"); + sql ("UPDATE reports SET modification_time = end_time;"); + + /* Set the database version to 244. */ + + set_db_version (244); + + sql_commit (); + + return 0; +} #undef UPDATE_DASHBOARD_SETTINGS @@ -2733,6 +2764,7 @@ static migrator_t database_migrators[] = { {241, migrate_240_to_241}, {242, migrate_241_to_242}, {243, migrate_242_to_243}, + {244, migrate_243_to_244}, /* End marker. */ {-1, NULL}}; diff --git a/src/manage_sql.c b/src/manage_sql.c index d18bfe206..258ebc0c9 100644 --- a/src/manage_sql.c +++ b/src/manage_sql.c @@ -20905,9 +20905,9 @@ report_add_result (report_t report, result_t result) { "iso_time (date)", "name", KEYWORD_TYPE_STRING }, \ { "''", NULL, KEYWORD_TYPE_STRING }, \ { "iso_time (date)", NULL, KEYWORD_TYPE_STRING }, \ - { "iso_time (end_time)", NULL, KEYWORD_TYPE_STRING }, \ + { "iso_time (modification_time)", NULL, KEYWORD_TYPE_STRING }, \ { "date", "created", KEYWORD_TYPE_INTEGER }, \ - { "end_time", "modified", KEYWORD_TYPE_INTEGER }, \ + { "modification_time", "modified", KEYWORD_TYPE_INTEGER }, \ { "(SELECT name FROM users WHERE users.id = reports.owner)", \ "_owner", \ KEYWORD_TYPE_STRING }, \ From bcc145d83d691d28d9118bc9366fbcaeda551933 Mon Sep 17 00:00:00 2001 From: Johannes Helmold Date: Fri, 7 May 2021 14:55:56 +0200 Subject: [PATCH 2/5] Added "Add a new modification_time column to reports" issue to CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8ac1b41f5..34be38e48 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ## [21.10] (unreleased) ### Added +- Add a new modification_time column to reports [#1513](https://github.com/greenbone/gvmd/pull/1513) ### Changed - Use pg-gvm extension for C PostgreSQL functions [#1400](https://github.com/greenbone/gvmd/pull/1400), [#1453](https://github.com/greenbone/gvmd/pull/1453) From df2e759fa0207b097fc5351398174102b01bd2b7 Mon Sep 17 00:00:00 2001 From: Johannes Helmold Date: Mon, 10 May 2021 11:01:45 +0200 Subject: [PATCH 3/5] Use new timestamps in generate_report_filename --- src/manage_sql.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/manage_sql.c b/src/manage_sql.c index 258ebc0c9..d157cab4f 100644 --- a/src/manage_sql.c +++ b/src/manage_sql.c @@ -12021,13 +12021,13 @@ generate_report_filename (report_t report, report_format_t report_format, report_id = report_uuid (report); creation_time - = sql_string ("SELECT iso_time (start_time)" + = sql_string ("SELECT iso_time (date)" " FROM reports" " WHERE id = %llu", report); modification_time - = sql_string ("SELECT iso_time (end_time)" + = sql_string ("SELECT iso_time (modification_time)" " FROM reports" " WHERE id = %llu", report); From 51a75e093e22a2ade13a288fa7518a0973ce2900 Mon Sep 17 00:00:00 2001 From: Johannes Helmold Date: Mon, 10 May 2021 13:48:17 +0200 Subject: [PATCH 4/5] Update report modification_time during scans Update modification_time when status is changed and when new results are received from scanner --- src/manage_sql.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/manage_sql.c b/src/manage_sql.c index d157cab4f..8b7e0b3f9 100644 --- a/src/manage_sql.c +++ b/src/manage_sql.c @@ -23626,7 +23626,8 @@ report_scan_run_status (report_t report, task_status_t* status) int set_report_scan_run_status (report_t report, task_status_t status) { - sql ("UPDATE reports SET scan_run_status = %u WHERE id = %llu;", + sql ("UPDATE reports SET scan_run_status = %u," + " modification_time = m_now() WHERE id = %llu;", status, report); if (setting_auto_cache_rebuild_int ()) @@ -28604,6 +28605,7 @@ parse_osp_report (task_t task, report_t report, const char *report_xml) const char *str; char *defs_file = NULL; time_t start_time, end_time; + gboolean has_results = FALSE; assert (task); assert (report); @@ -28641,6 +28643,9 @@ parse_osp_report (task_t task, report_t report, const char *report_xml) goto end_parse_osp_report; } results = child->entities; + if (results) + has_results = TRUE; + defs_file = task_definitions_file (task); while (results) { @@ -28748,6 +28753,10 @@ parse_osp_report (task_t task, report_t report, const char *report_xml) results = next_entities (results); } + if (has_results) + sql ("UPDATE reports SET modification_time = m_now() WHERE id = %llu;", + report); + end_parse_osp_report: sql_commit (); g_free (defs_file); From 062ef8424e455b0ffc1d320ee8f28932913d11a4 Mon Sep 17 00:00:00 2001 From: Johannes Helmold Date: Mon, 10 May 2021 14:52:47 +0200 Subject: [PATCH 5/5] Update modification_time for reports for CVE scans. Created new function "update_report_modification_time ()" and used it in "cve_scan_host ()" to update the modification_time during CVE scans. --- src/manage.c | 1 + src/manage_sql.c | 16 ++++++++++++++++ src/manage_sql.h | 2 ++ 3 files changed, 19 insertions(+) diff --git a/src/manage.c b/src/manage.c index ececa199f..42f60b55f 100644 --- a/src/manage.c +++ b/src/manage.c @@ -3113,6 +3113,7 @@ cve_scan_host (task_t task, report_t report, gvm_host_t *gvm_host) report_host_set_end_time (prognosis_report_host, time (NULL)); insert_report_host_detail (report, ip, "cve", "", "CVE Scanner", "CVE Scan", "1"); + update_report_modification_time (report); } } cleanup_iterator (&report_hosts); diff --git a/src/manage_sql.c b/src/manage_sql.c index 8b7e0b3f9..0657605c5 100644 --- a/src/manage_sql.c +++ b/src/manage_sql.c @@ -23635,6 +23635,22 @@ set_report_scan_run_status (report_t report, task_status_t status) return 0; } +/** + * @brief Update modification_time of a report to current time. + * + * @param[in] report Report. + * + * @return 0. + */ +int +update_report_modification_time (report_t report) +{ + sql("UPDATE reports SET modification_time = m_now() WHERE id = %llu;", + report); + + return 0; +} + /** * @brief Get the result severity counts for a report. * diff --git a/src/manage_sql.h b/src/manage_sql.h index a0688d0cc..4f1d66636 100644 --- a/src/manage_sql.h +++ b/src/manage_sql.h @@ -296,6 +296,8 @@ int delete_report_internal (report_t); int set_report_scan_run_status (report_t, task_status_t); +int update_report_modification_time (report_t); + int set_report_slave_progress (report_t, int); void init_task_file_iterator (iterator_t *, task_t, const char *);