Blame SOURCES/0083-rhtsupport-submit-ureport-and-attach-case-ID-to-urep.patch

562801
From c25092c8a9b5fa42cd5388313b1db4d8ad81034f Mon Sep 17 00:00:00 2001
562801
From: Jakub Filak <jfilak@redhat.com>
562801
Date: Tue, 16 Sep 2014 13:34:28 +0200
562801
Subject: [LIBREPORT PATCH 83/93] rhtsupport: submit ureport and attach case ID
562801
 to ureport
562801
562801
ABRT Server for RHEL provides an authenticated way of uReport
562801
submission. The authentication can be done via client certificates or
562801
HTTP Basic auth. ABRT client uses the client certificates for
562801
Auto-reporting, but when user wants to report a problem manually he
562801
needs to have special permissions to work with the certificates or he
562801
cannot use this way of authentication. Fortunately, RHEL ABRT Server can
562801
authenticate user via RHTSupport user name an password, but we don't
562801
want to ask users to provide/configure the Customer Portal credentials
562801
twice, hence we will attach a case ID to a bthash from
562801
reporter-rhtsupport.
562801
562801
This commit also addresses complaints about no evidences of sending
562801
uReport to ABRT Server. This commit adds a log message
562801
"Sending ABRT crash statistics data".
562801
562801
Relate to rhbz#1139987, rhbz#1084028
562801
562801
Signed-off-by: Jakub Filak <jfilak@redhat.com>
562801
---
562801
 doc/reporter-rhtsupport.txt       |  18 +++++-
562801
 src/include/ureport.h             |  13 +++-
562801
 src/lib/ureport.c                 |  13 +++-
562801
 src/plugins/reporter-rhtsupport.c | 121 +++++++++++++++++++++++++++++++++++++-
562801
 src/plugins/reporter-ureport.c    |   2 +-
562801
 5 files changed, 162 insertions(+), 5 deletions(-)
562801
562801
diff --git a/doc/reporter-rhtsupport.txt b/doc/reporter-rhtsupport.txt
562801
index b018906..c4aa459 100644
562801
--- a/doc/reporter-rhtsupport.txt
562801
+++ b/doc/reporter-rhtsupport.txt
562801
@@ -7,7 +7,7 @@ reporter-rhtsupport - Reports problem to RHTSupport.
562801
 
562801
 SYNOPSIS
562801
 --------
562801
-'reporter-rhtsupport' [-v] [-c CONFFILE] -d DIR
562801
+'reporter-rhtsupport' [-v] [-c CONFFILE] [-u -C UR_CONFFILE] -d DIR
562801
 
562801
 Or:
562801
 
562801
@@ -18,6 +18,9 @@ DESCRIPTION
562801
 The tool reads problem directory DIR. Then it logs in to RHTSupport
562801
 and creates a new case.
562801
 
562801
+The tool can be configured to submit an uReport to RHTSupport together with
562801
+creating a new case.
562801
+
562801
 The URL to new case is printed to stdout and recorded in 'reported_to'
562801
 element in DIR.
562801
 
562801
@@ -30,6 +33,9 @@ If problem data in DIR was never reported to RHTSupport, upload will fail.
562801
 Option -tCASE uploads FILEs to the case CASE on RHTSupport site.
562801
 -d DIR is ignored.
562801
 
562801
+Option -u uploads uReport along with creating a new case. uReport configuration
562801
+is loaded from UR_CONFFILE which defaults to
562801
+/etc/libreport/plugins/ureport.conf.
562801
 
562801
 Configuration file
562801
 ~~~~~~~~~~~~~~~~~~
562801
@@ -47,6 +53,10 @@ Configuration file lines should have 'PARAM = VALUE' format. The parameters are:
562801
 'SSLVerify'::
562801
 	Use yes/true/on/1 to verify server's SSL certificate. (default: yes)
562801
 
562801
+'SubmitUReport'::
562801
+	Use yes/true/on/1 to enable submitting uReport together wit creating a new
562801
+	case. (default: no)
562801
+
562801
 Parameters can be overridden via $RHTSupport_PARAM environment variables.
562801
 
562801
 Integration with ABRT events
562801
@@ -71,6 +81,12 @@ OPTIONS
562801
 -t[ID]::
562801
    Upload FILEs to the already created case on RHTSupport site.
562801
 
562801
+-u::
562801
+   Submit uReport together with creating a new case.
562801
+
562801
+-C UR_CONFFILE::
562801
+   Configuration file for submitting uReports.
562801
+
562801
 FILES
562801
 -----
562801
 /usr/share/libreport/conf.d/plugins/rhtsupport.conf::
562801
diff --git a/src/include/ureport.h b/src/include/ureport.h
562801
index 3ee12cd..8bb1f6c 100644
562801
--- a/src/include/ureport.h
562801
+++ b/src/include/ureport.h
562801
@@ -47,7 +47,7 @@ struct ureport_preferences
562801
  */
562801
 struct ureport_server_config
562801
 {
562801
-    const char *ur_url;   ///< Web service URL
562801
+    char *ur_url;         ///< Web service URL
562801
     bool ur_ssl_verify;   ///< Verify HOST and PEER certificates
562801
     char *ur_client_cert; ///< Path to certificate used for client
562801
                           ///< authentication (or NULL)
562801
@@ -91,6 +91,17 @@ ureport_server_config_load(struct ureport_server_config *config,
562801
                            map_string_t *settings);
562801
 
562801
 /*
562801
+ * Configure HTTP(S) URL to server's index page
562801
+ *
562801
+ * @param config Where the url is stored
562801
+ * @param server_url Index URL
562801
+ */
562801
+#define ureport_server_config_set_url libreport_ureport_server_config_set_url
562801
+void
562801
+ureport_server_config_set_url(struct ureport_server_config *config,
562801
+                              char *server_url);
562801
+
562801
+/*
562801
  * Configure client certificate paths
562801
  *
562801
  * @param config Where the paths are stored
562801
diff --git a/src/lib/ureport.c b/src/lib/ureport.c
562801
index 5453a37..26f3562 100644
562801
--- a/src/lib/ureport.c
562801
+++ b/src/lib/ureport.c
562801
@@ -62,6 +62,14 @@ error:
562801
 }
562801
 
562801
 void
562801
+ureport_server_config_set_url(struct ureport_server_config *config,
562801
+                              char *server_url)
562801
+{
562801
+    free(config->ur_url);
562801
+    config->ur_url = server_url;
562801
+}
562801
+
562801
+void
562801
 ureport_server_config_set_client_auth(struct ureport_server_config *config,
562801
                                       const char *client_auth)
562801
 {
562801
@@ -211,7 +219,7 @@ void
562801
 ureport_server_config_load(struct ureport_server_config *config,
562801
                            map_string_t *settings)
562801
 {
562801
-    UREPORT_OPTION_VALUE_FROM_CONF(settings, "URL", config->ur_url, (const char *));
562801
+    UREPORT_OPTION_VALUE_FROM_CONF(settings, "URL", config->ur_url, xstrdup);
562801
     UREPORT_OPTION_VALUE_FROM_CONF(settings, "SSLVerify", config->ur_ssl_verify, string_to_bool);
562801
 
562801
     bool include_auth = false;
562801
@@ -248,6 +256,9 @@ ureport_server_config_init(struct ureport_server_config *config)
562801
 void
562801
 ureport_server_config_destroy(struct ureport_server_config *config)
562801
 {
562801
+    free(config->ur_url);
562801
+    config->ur_url = DESTROYED_POINTER;
562801
+
562801
     free(config->ur_client_cert);
562801
     config->ur_client_cert = DESTROYED_POINTER;
562801
 
562801
diff --git a/src/plugins/reporter-rhtsupport.c b/src/plugins/reporter-rhtsupport.c
562801
index 55bcda6..cd72c87 100644
562801
--- a/src/plugins/reporter-rhtsupport.c
562801
+++ b/src/plugins/reporter-rhtsupport.c
562801
@@ -17,6 +17,7 @@
562801
     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
562801
 */
562801
 #include <libtar.h>
562801
+#include "ureport.h"
562801
 #include "internal_libreport.h"
562801
 #include "client.h"
562801
 #include "libreport_curl.h"
562801
@@ -169,6 +170,51 @@ ret_clean:
562801
 }
562801
 
562801
 static
562801
+char *submit_ureport(const char *dump_dir_name, struct ureport_server_config *conf)
562801
+{
562801
+    struct dump_dir *dd = dd_opendir(dump_dir_name, DD_OPEN_READONLY);
562801
+    if (dd == NULL)
562801
+        return NULL;
562801
+
562801
+    report_result_t *rr_bthash = find_in_reported_to(dd, "uReport");
562801
+    dd_close(dd);
562801
+
562801
+    if (rr_bthash != NULL)
562801
+    {
562801
+        log_notice("uReport has already been submitted.");
562801
+        char *ret = xstrdup(rr_bthash->bthash);
562801
+        free_report_result(rr_bthash);
562801
+        return ret;
562801
+    }
562801
+
562801
+    char *json = ureport_from_dump_dir(dump_dir_name);
562801
+    if (json == NULL)
562801
+        return NULL;
562801
+
562801
+    struct ureport_server_response *resp = ureport_submit(json, conf);
562801
+    free(json);
562801
+    if (resp == NULL)
562801
+        return NULL;
562801
+
562801
+    char *bthash = NULL;
562801
+    if (!resp->urr_is_error)
562801
+    {
562801
+        if (resp->urr_bthash != NULL)
562801
+            bthash = xstrdup(resp->urr_bthash);
562801
+
562801
+        ureport_server_response_save_in_dump_dir(resp, dump_dir_name, conf);
562801
+
562801
+        if (resp->urr_message)
562801
+            log(resp->urr_message);
562801
+    }
562801
+    else if (g_verbose > 2)
562801
+        error_msg(_("Server responded with an error: '%s'"), resp->urr_value);
562801
+
562801
+    ureport_server_response_free(resp);
562801
+    return bthash;
562801
+}
562801
+
562801
+static
562801
 char *ask_rh_login(const char *message)
562801
 {
562801
     char *login = ask(message);
562801
@@ -203,6 +249,40 @@ char *get_param_string(const char *name, map_string_t *settings, const char *dfl
562801
     return xstrdup(envvar ? envvar : (get_map_string_item_or_NULL(settings, name) ? : dflt));
562801
 }
562801
 
562801
+static
562801
+void prepare_ureport_configuration(const char *urcfile,
562801
+        map_string_t *settings, struct ureport_server_config *urconf,
562801
+        const char *portal_url, const char *login, const char *password, bool ssl_verify)
562801
+{
562801
+    load_conf_file(urcfile, settings, false);
562801
+    ureport_server_config_init(urconf);
562801
+
562801
+    char *url = NULL;
562801
+    UREPORT_OPTION_VALUE_FROM_CONF(settings, "URL", url, xstrdup);
562801
+    if (url == NULL)
562801
+    {
562801
+        ureport_server_config_set_url(urconf, concat_path_file(portal_url, "/telemetry/abrt"));
562801
+        urconf->ur_ssl_verify = ssl_verify;
562801
+    }
562801
+    else
562801
+    {
562801
+        UREPORT_OPTION_VALUE_FROM_CONF(settings, "SSLVerify", urconf->ur_ssl_verify, string_to_bool);
562801
+        ureport_server_config_set_url(urconf, url);
562801
+    }
562801
+
562801
+    ureport_server_config_set_basic_auth(urconf, login, password);
562801
+
562801
+    bool include_auth = true;
562801
+    UREPORT_OPTION_VALUE_FROM_CONF(settings, "IncludeAuthData", include_auth, string_to_bool);
562801
+
562801
+    if (include_auth)
562801
+    {
562801
+        const char *auth_items = NULL;
562801
+        UREPORT_OPTION_VALUE_FROM_CONF(settings, "AuthDataItems", auth_items, (const char *));
562801
+        urconf->ur_prefs.urp_auth_items = parse_list(auth_items);
562801
+    }
562801
+}
562801
+
562801
 int main(int argc, char **argv)
562801
 {
562801
     abrt_init(argv);
562801
@@ -217,13 +297,14 @@ int main(int argc, char **argv)
562801
     const char *dump_dir_name = ".";
562801
     const char *case_no = NULL;
562801
     GList *conf_file = NULL;
562801
+    const char *urconf_file = UREPORT_CONF_FILE_PATH;
562801
 
562801
     /* Can't keep these strings/structs static: _() doesn't support that */
562801
     const char *program_usage_string = _(
562801
         "\n"
562801
         "& [-v] [-c CONFFILE] -d DIR\n"
562801
         "or:\n"
562801
-        "& [-v] [-c CONFFILE] [-d DIR] -t[ID] FILE...\n"
562801
+        "& [-v] [-c CONFFILE] [-d DIR] -t[ID] [-u -C UR_CONFFILE] FILE...\n"
562801
         "\n"
562801
         "Reports a problem to RHTSupport.\n"
562801
         "\n"
562801
@@ -240,6 +321,10 @@ int main(int argc, char **argv)
562801
         "\n"
562801
         "Option -tCASE uploads FILEs to the case CASE on RHTSupport site.\n"
562801
         "-d DIR is ignored."
562801
+        "\n"
562801
+        "Option -u sends ABRT crash statistics data (uReport) before creating a new case.\n"
562801
+        "uReport configuration is loaded from UR_CONFFILE which defaults to\n"
562801
+        UREPORT_CONF_FILE_PATH".\n"
562801
     );
562801
     enum {
562801
         OPT_v = 1 << 0,
562801
@@ -247,6 +332,7 @@ int main(int argc, char **argv)
562801
         OPT_c = 1 << 2,
562801
         OPT_t = 1 << 3,
562801
         OPT_f = 1 << 4,
562801
+        OPT_u = 1 << 5,
562801
     };
562801
     /* Keep enum above and order of options below in sync! */
562801
     struct options program_options[] = {
562801
@@ -255,6 +341,8 @@ int main(int argc, char **argv)
562801
         OPT_LIST(     'c', NULL, &conf_file    , "FILE", _("Configuration file (may be given many times)")),
562801
         OPT_OPTSTRING('t', NULL, &case_no      , "ID"  , _("Upload FILEs [to case with this ID]")),
562801
         OPT_BOOL(     'f', NULL, NULL          ,         _("Force reporting even if this problem is already reported")),
562801
+        OPT_BOOL(     'u', NULL, NULL          ,         _("Submit uReport before creating a new case")),
562801
+        OPT_STRING(   'C', NULL, &urconf_file  , "FILE", _("Configuration file for uReport")),
562801
         OPT_END()
562801
     };
562801
     unsigned opts = parse_opts(argc, argv, program_options, program_usage_string);
562801
@@ -303,6 +391,12 @@ int main(int argc, char **argv)
562801
                 /* RH has a 250m limit for web attachments (as of 2013) */
562801
                 envvar ? envvar : (get_map_string_item_or_NULL(settings, "BigSizeMB") ? : "200")
562801
     );
562801
+    envvar = getenv("RHTSupport_SubmitUReport");
562801
+    bool submit_ur = string_to_bool(
562801
+                envvar ? envvar :
562801
+                    (get_map_string_item_or_NULL(settings, "SubmitUReport") ? :
562801
+                        ((opts & OPT_u) ? "1" : "0"))
562801
+    );
562801
     free_map_string(settings);
562801
 
562801
     char *base_api_url = xstrdup(url);
562801
@@ -484,6 +578,21 @@ int main(int argc, char **argv)
562801
 
562801
     if (!(opts & OPT_t))
562801
     {
562801
+        char *bthash = NULL;
562801
+
562801
+        map_string_t *ursettings = new_map_string();
562801
+        struct ureport_server_config urconf;
562801
+
562801
+        prepare_ureport_configuration(urconf_file, ursettings, &urconf,
562801
+                url, login, password, ssl_verify);
562801
+
562801
+        if (submit_ur)
562801
+        {
562801
+            log(_("Sending ABRT crash statistics data"));
562801
+
562801
+            bthash = submit_ureport(dump_dir_name, &urconf);
562801
+        }
562801
+
562801
         log(_("Creating a new case"));
562801
 
562801
         char *product = NULL;
562801
@@ -555,10 +664,20 @@ int main(int argc, char **argv)
562801
         }
562801
         /* else: error msg was already emitted by dd_opendir */
562801
 
562801
+        if (bthash)
562801
+        {
562801
+            log(_("Linking ABRT crash statistics record with the case"));
562801
+            ureport_attach_string(bthash, "RHCID", result->url, &urconf);
562801
+        }
562801
+
562801
         url = result->url;
562801
         result->url = NULL;
562801
         free_rhts_result(result);
562801
         result = NULL;
562801
+
562801
+        ureport_server_config_destroy(&urconf);
562801
+        free_map_string(ursettings);
562801
+        free(bthash);
562801
     }
562801
 
562801
     char *remote_filename = NULL;
562801
diff --git a/src/plugins/reporter-ureport.c b/src/plugins/reporter-ureport.c
562801
index 7bd3fb3..06b5341 100644
562801
--- a/src/plugins/reporter-ureport.c
562801
+++ b/src/plugins/reporter-ureport.c
562801
@@ -100,7 +100,7 @@ int main(int argc, char **argv)
562801
     ureport_server_config_load(&config, settings);
562801
 
562801
     if (opts & OPT_u)
562801
-        config.ur_url = arg_server_url;
562801
+        ureport_server_config_set_url(&config, xstrdup(arg_server_url));
562801
     if (opts & OPT_k)
562801
         config.ur_ssl_verify = !insecure;
562801
     if (opts & OPT_t)
562801
-- 
562801
1.8.3.1
562801