Blame SOURCES/0003-subman-Increase-RHSM-dbus-call-timeouts.patch

06f198
From 0d88b2133b20957e00b0eeb0c0f48932485cc73d Mon Sep 17 00:00:00 2001
06f198
From: Kalev Lember <klember@redhat.com>
06f198
Date: Fri, 28 Jun 2019 18:10:36 +0200
06f198
Subject: [PATCH 03/15] subman: Increase RHSM dbus call timeouts
06f198
06f198
Increase the dbus timeouts to 5 minutes as the register/unregister calls
06f198
seem to routinely take more than a minute.
06f198
---
06f198
 plugins/subman/gsd-subman-helper.c | 17 ++++++++++++-----
06f198
 1 file changed, 12 insertions(+), 5 deletions(-)
06f198
06f198
diff --git a/plugins/subman/gsd-subman-helper.c b/plugins/subman/gsd-subman-helper.c
06f198
index 182f7190..af7a82e9 100644
06f198
--- a/plugins/subman/gsd-subman-helper.c
06f198
+++ b/plugins/subman/gsd-subman-helper.c
06f198
@@ -1,60 +1,62 @@
06f198
 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
06f198
  *
06f198
  * Copyright (C) 2019 Richard Hughes <rhughes@redhat.com>
06f198
  *
06f198
  * Licensed under the GNU General Public License Version 2
06f198
  *
06f198
  * This program is free software; you can redistribute it and/or modify
06f198
  * it under the terms of the GNU General Public License as published by
06f198
  * the Free Software Foundation; either version 2 of the License, or
06f198
  * (at your option) any later version.
06f198
  *
06f198
  * This program is distributed in the hope that it will be useful,
06f198
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
06f198
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
06f198
  * GNU General Public License for more details.
06f198
  *
06f198
  * You should have received a copy of the GNU General Public License
06f198
  * along with this program; if not, write to the Free Software
06f198
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
06f198
  */
06f198
 
06f198
 #include "config.h"
06f198
 
06f198
 #include <sys/types.h>
06f198
 #include <unistd.h>
06f198
 #include <stdlib.h>
06f198
 
06f198
 #include <gio/gio.h>
06f198
 #include <json-glib/json-glib.h>
06f198
 
06f198
+#define DBUS_TIMEOUT 300000 /* 5 minutes */
06f198
+
06f198
 static void
06f198
 _helper_convert_error (const gchar *json_txt, GError **error)
06f198
 {
06f198
 	JsonNode *json_root;
06f198
 	JsonObject *json_obj;
06f198
 	const gchar *message;
06f198
 	g_autoptr(JsonParser) json_parser = json_parser_new ();
06f198
 
06f198
 	/* this may be plain text or JSON :| */
06f198
 	if (!json_parser_load_from_data (json_parser, json_txt, -1, NULL)) {
06f198
 		g_set_error_literal (error,
06f198
 				     G_IO_ERROR,
06f198
 				     G_IO_ERROR_NOT_SUPPORTED,
06f198
 				     json_txt);
06f198
 		return;
06f198
 	}
06f198
 	json_root = json_parser_get_root (json_parser);
06f198
 	json_obj = json_node_get_object (json_root);
06f198
 	if (!json_object_has_member (json_obj, "message")) {
06f198
 		g_set_error (error,
06f198
 			     G_IO_ERROR,
06f198
 			     G_IO_ERROR_INVALID_DATA,
06f198
 			     "no message' in %s", json_txt);
06f198
 		return;
06f198
 	}
06f198
 	message = json_object_get_string_member (json_obj, "message");
06f198
 	if (g_strstr_len (message, -1, "Invalid user credentials") != NULL) {
06f198
 		g_set_error_literal (error,
06f198
 				     G_IO_ERROR,
06f198
 				     G_IO_ERROR_PERMISSION_DENIED,
06f198
@@ -67,125 +69,128 @@ _helper_convert_error (const gchar *json_txt, GError **error)
06f198
 			     message);
06f198
 }
06f198
 
06f198
 static gboolean
06f198
 _helper_unregister (GError **error)
06f198
 {
06f198
 	g_autoptr(GDBusProxy) proxy = NULL;
06f198
 	g_autoptr(GVariantBuilder) proxy_options = NULL;
06f198
 	g_autoptr(GVariant) res = NULL;
06f198
 
06f198
 	g_debug ("unregistering");
06f198
 	proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
06f198
 					       G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
06f198
 					       G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
06f198
 					       NULL,
06f198
 					       "com.redhat.RHSM1",
06f198
 					       "/com/redhat/RHSM1/Unregister",
06f198
 					       "com.redhat.RHSM1.Unregister",
06f198
 					       NULL, error);
06f198
 	if (proxy == NULL) {
06f198
 		g_prefix_error (error, "Failed to get proxy: ");
06f198
 		return FALSE;
06f198
 	}
06f198
 	proxy_options = g_variant_builder_new (G_VARIANT_TYPE_VARDICT);
06f198
 	res = g_dbus_proxy_call_sync (proxy,
06f198
 				      "Unregister",
06f198
 				      g_variant_new ("(a{sv}s)",
06f198
 						     proxy_options,
06f198
 						     ""), /* lang */
06f198
 				      G_DBUS_CALL_FLAGS_NONE,
06f198
-				      -1, NULL, error);
06f198
+				      DBUS_TIMEOUT,
06f198
+				      NULL, error);
06f198
 	return res != NULL;
06f198
 }
06f198
 
06f198
 static gboolean
06f198
 _helper_auto_attach (GError **error)
06f198
 {
06f198
 	const gchar *str = NULL;
06f198
 	g_autoptr(GDBusProxy) proxy = NULL;
06f198
 	g_autoptr(GVariantBuilder) proxy_options = NULL;
06f198
 	g_autoptr(GVariant) res = NULL;
06f198
 
06f198
 	g_debug ("auto-attaching subscriptions");
06f198
 	proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
06f198
 					       G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
06f198
 					       G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
06f198
 					       NULL,
06f198
 					       "com.redhat.RHSM1",
06f198
 					       "/com/redhat/RHSM1/Attach",
06f198
 					       "com.redhat.RHSM1.Attach",
06f198
 					       NULL, error);
06f198
 	if (proxy == NULL) {
06f198
 		g_prefix_error (error, "Failed to get proxy: ");
06f198
 		return FALSE;
06f198
 	}
06f198
 	proxy_options = g_variant_builder_new (G_VARIANT_TYPE_VARDICT);
06f198
 	res = g_dbus_proxy_call_sync (proxy,
06f198
 				      "AutoAttach",
06f198
 				      g_variant_new ("(sa{sv}s)",
06f198
 						     "", /* now? */
06f198
 						     proxy_options,
06f198
 						     ""), /* lang */
06f198
 				      G_DBUS_CALL_FLAGS_NONE,
06f198
-				      -1, NULL, error);
06f198
+				      DBUS_TIMEOUT,
06f198
+				      NULL, error);
06f198
 	if (res == NULL)
06f198
 		return FALSE;
06f198
 	g_variant_get (res, "(&s)", &str);
06f198
 	g_debug ("Attach.AutoAttach: %s", str);
06f198
 	return TRUE;
06f198
 }
06f198
 
06f198
 static gboolean
06f198
 _helper_save_config (const gchar *key, const gchar *value, GError **error)
06f198
 {
06f198
 	g_autoptr(GDBusProxy) proxy = NULL;
06f198
 	g_autoptr(GVariant) res = NULL;
06f198
 	proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
06f198
 					       G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
06f198
 					       G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
06f198
 					       NULL,
06f198
 					       "com.redhat.RHSM1",
06f198
 					       "/com/redhat/RHSM1/Config",
06f198
 					       "com.redhat.RHSM1.Config",
06f198
 					       NULL, error);
06f198
 	if (proxy == NULL) {
06f198
 		g_prefix_error (error, "Failed to get proxy: ");
06f198
 		return FALSE;
06f198
 	}
06f198
 	res = g_dbus_proxy_call_sync (proxy, "Set",
06f198
 				      g_variant_new ("(svs)",
06f198
 						     key,
06f198
 						     g_variant_new_string (value),
06f198
 						     ""), /* lang */
06f198
 				      G_DBUS_CALL_FLAGS_NONE,
06f198
-				      -1, NULL, error);
06f198
+				      DBUS_TIMEOUT,
06f198
+				      NULL, error);
06f198
 	return res != NULL;
06f198
 }
06f198
 
06f198
 int
06f198
 main (int argc, char *argv[])
06f198
 {
06f198
 	const gchar *userlang = ""; /* as root, so no translations */
06f198
 	g_autofree gchar *activation_key = NULL;
06f198
 	g_autofree gchar *address = NULL;
06f198
 	g_autofree gchar *hostname = NULL;
06f198
 	g_autofree gchar *kind = NULL;
06f198
 	g_autofree gchar *organisation = NULL;
06f198
 	g_autofree gchar *password = NULL;
06f198
 	g_autofree gchar *port = NULL;
06f198
 	g_autofree gchar *prefix = NULL;
06f198
 	g_autofree gchar *proxy_server = NULL;
06f198
 	g_autofree gchar *username = NULL;
06f198
 	g_autoptr(GDBusConnection) conn_private = NULL;
06f198
 	g_autoptr(GDBusProxy) proxy = NULL;
06f198
 	g_autoptr(GError) error = NULL;
06f198
 	g_autoptr(GOptionContext) context = g_option_context_new (NULL);
06f198
 	g_autoptr(GVariantBuilder) proxy_options = NULL;
06f198
 	g_autoptr(GVariantBuilder) subman_conopts = NULL;
06f198
 	g_autoptr(GVariantBuilder) subman_options = NULL;
06f198
 
06f198
 	const GOptionEntry options[] = {
06f198
 		{ "kind", '\0', G_OPTION_FLAG_NONE, G_OPTION_ARG_STRING,
06f198
 			&kind, "Kind, e.g. 'username' or 'key'", NULL },
06f198
 		{ "address", '\0', G_OPTION_FLAG_NONE, G_OPTION_ARG_STRING,
06f198
 			&address, "UNIX address", NULL },
06f198
@@ -278,95 +283,97 @@ main (int argc, char *argv[])
06f198
 	g_variant_builder_add (subman_conopts, "{ss}", "host", hostname);
06f198
 	g_variant_builder_add (subman_conopts, "{ss}", "handler", prefix);
06f198
 	g_variant_builder_add (subman_conopts, "{ss}", "port", port);
06f198
 
06f198
 	/* call into RHSM */
06f198
 	if (g_strcmp0 (kind, "register-with-key") == 0) {
06f198
 		g_auto(GStrv) activation_keys = NULL;
06f198
 		g_autoptr(GError) error_local = NULL;
06f198
 		g_autoptr(GVariant) res = NULL;
06f198
 
06f198
 		if (activation_key == NULL) {
06f198
 			g_printerr ("Required --activation-key\n");
06f198
 			return G_IO_ERROR_INVALID_DATA;
06f198
 		}
06f198
 		if (organisation == NULL) {
06f198
 			g_printerr ("Required --organisation\n");
06f198
 			return G_IO_ERROR_INVALID_DATA;
06f198
 		}
06f198
 
06f198
 		g_debug ("registering using activation key");
06f198
 		activation_keys = g_strsplit (activation_key, ",", -1);
06f198
 		res = g_dbus_proxy_call_sync (proxy,
06f198
 					      "RegisterWithActivationKeys",
06f198
 					      g_variant_new ("(s^asa{ss}a{ss}s)",
06f198
 							     organisation,
06f198
 							     activation_keys,
06f198
 							     subman_options,
06f198
 							     subman_conopts,
06f198
 							     userlang),
06f198
 					      G_DBUS_CALL_FLAGS_NO_AUTO_START,
06f198
-					      -1, NULL, &error_local);
06f198
+					      DBUS_TIMEOUT,
06f198
+					      NULL, &error_local);
06f198
 		if (res == NULL) {
06f198
 			g_dbus_error_strip_remote_error (error_local);
06f198
 			_helper_convert_error (error_local->message, &error);
06f198
 			g_printerr ("Failed to RegisterWithActivationKeys: %s\n", error->message);
06f198
 			return error->code;
06f198
 		}
06f198
 	} else if (g_strcmp0 (kind, "register-with-username") == 0) {
06f198
 		g_autoptr(GError) error_local = NULL;
06f198
 		g_autoptr(GVariant) res = NULL;
06f198
 
06f198
 		g_debug ("registering using username and password");
06f198
 		if (username == NULL) {
06f198
 			g_printerr ("Required --username\n");
06f198
 			return G_IO_ERROR_INVALID_DATA;
06f198
 		}
06f198
 		if (password == NULL) {
06f198
 			g_printerr ("Required --password\n");
06f198
 			return G_IO_ERROR_INVALID_DATA;
06f198
 		}
06f198
 		if (organisation == NULL) {
06f198
 			g_printerr ("Required --organisation\n");
06f198
 			return G_IO_ERROR_INVALID_DATA;
06f198
 		}
06f198
 		res = g_dbus_proxy_call_sync (proxy,
06f198
 					      "Register",
06f198
 					      g_variant_new ("(sssa{ss}a{ss}s)",
06f198
 							     organisation,
06f198
 							     username,
06f198
 							     password,
06f198
 							     subman_options,
06f198
 							     subman_conopts,
06f198
 							     userlang),
06f198
 					      G_DBUS_CALL_FLAGS_NO_AUTO_START,
06f198
-					      -1, NULL, &error_local);
06f198
+					      DBUS_TIMEOUT,
06f198
+					      NULL, &error_local);
06f198
 		if (res == NULL) {
06f198
 			g_dbus_error_strip_remote_error (error_local);
06f198
 			_helper_convert_error (error_local->message, &error);
06f198
 			g_printerr ("Failed to Register: %s\n", error->message);
06f198
 			return error->code;
06f198
 		}
06f198
 	} else {
06f198
 		g_printerr ("Invalid --kind specified: %s\n", kind);
06f198
 		return G_IO_ERROR_INVALID_DATA;
06f198
 	}
06f198
 
06f198
 	/* set the new hostname */
06f198
 	if (!_helper_save_config ("server.hostname", hostname, &error)) {
06f198
 		g_printerr ("Failed to save hostname: %s\n", error->message);
06f198
 		return G_IO_ERROR_NOT_INITIALIZED;
06f198
 	}
06f198
 	if (!_helper_save_config ("server.prefix", prefix, &error)) {
06f198
 		g_printerr ("Failed to save prefix: %s\n", error->message);
06f198
 		return G_IO_ERROR_NOT_INITIALIZED;
06f198
 	}
06f198
 	if (!_helper_save_config ("server.port", port, &error)) {
06f198
 		g_printerr ("Failed to save port: %s\n", error->message);
06f198
 		return G_IO_ERROR_NOT_INITIALIZED;
06f198
 	}
06f198
 
06f198
 	/* wait for rhsmd to notice the new config */
06f198
 	g_usleep (G_USEC_PER_SEC * 5);
06f198
 
06f198
 	/* auto-attach */
06f198
 	if (!_helper_auto_attach (&error)) {
06f198
-- 
06f198
2.30.0
06f198