Blame SOURCES/0009-subman-Don-t-treat-failure-to-attach-as-fatal.patch

e296c6
From 1255b2b83284d262f6b8c3ceb23d499ddbf77d48 Mon Sep 17 00:00:00 2001
e296c6
From: Ray Strode <rstrode@redhat.com>
e296c6
Date: Thu, 21 Jan 2021 09:52:19 -0500
e296c6
Subject: [PATCH 09/15] subman: Don't treat failure to attach as fatal
e296c6
e296c6
Many organizations don't require specific subscriptions to get
e296c6
updates (called "simple content access").  At the moment,
e296c6
those systems get an error when registering.
e296c6
e296c6
This commit quiets the error.
e296c6
---
e296c6
 plugins/subman/gsd-subman-helper.c | 46 ++++++++++++++++++++++++------
e296c6
 1 file changed, 37 insertions(+), 9 deletions(-)
e296c6
e296c6
diff --git a/plugins/subman/gsd-subman-helper.c b/plugins/subman/gsd-subman-helper.c
e296c6
index edf1e41f..53a4d56b 100644
e296c6
--- a/plugins/subman/gsd-subman-helper.c
e296c6
+++ b/plugins/subman/gsd-subman-helper.c
e296c6
@@ -25,145 +25,169 @@
e296c6
 #include <sys/types.h>
e296c6
 #include <unistd.h>
e296c6
 #include <stdlib.h>
e296c6
 #include <locale.h>
e296c6
 
e296c6
 #include <gio/gio.h>
e296c6
 #include <gio/gunixinputstream.h>
e296c6
 #include <json-glib/json-glib.h>
e296c6
 
e296c6
 #define DBUS_TIMEOUT 300000 /* 5 minutes */
e296c6
 static const char *locale;
e296c6
 
e296c6
 static void
e296c6
 _helper_convert_error (const gchar *json_txt, GError **error)
e296c6
 {
e296c6
 	JsonNode *json_root;
e296c6
 	JsonObject *json_obj;
e296c6
 	const gchar *message;
e296c6
 	g_autoptr(JsonParser) json_parser = json_parser_new ();
e296c6
 
e296c6
 	/* this may be plain text or JSON :| */
e296c6
 	if (!json_parser_load_from_data (json_parser, json_txt, -1, NULL)) {
e296c6
 		g_set_error_literal (error,
e296c6
 				     G_IO_ERROR,
e296c6
 				     G_IO_ERROR_NOT_SUPPORTED,
e296c6
 				     json_txt);
e296c6
 		return;
e296c6
 	}
e296c6
 	json_root = json_parser_get_root (json_parser);
e296c6
 	json_obj = json_node_get_object (json_root);
e296c6
+	if (json_object_has_member (json_obj, "severity")) {
e296c6
+		const gchar *severity;
e296c6
+
e296c6
+		/* warnings are non-fatal so we ignore them
e296c6
+		 */
e296c6
+		severity = json_object_get_string_member (json_obj, "severity");
e296c6
+		if (g_strstr_len (severity, -1, "warning") != NULL) {
e296c6
+			return;
e296c6
+		}
e296c6
+	}
e296c6
+
e296c6
 	if (!json_object_has_member (json_obj, "message")) {
e296c6
 		g_set_error (error,
e296c6
 			     G_IO_ERROR,
e296c6
 			     G_IO_ERROR_INVALID_DATA,
e296c6
 			     "no message' in %s", json_txt);
e296c6
 		return;
e296c6
 	}
e296c6
 	message = json_object_get_string_member (json_obj, "message");
e296c6
 	if (g_strstr_len (message, -1, "Invalid user credentials") != NULL) {
e296c6
 		g_set_error_literal (error,
e296c6
 				     G_IO_ERROR,
e296c6
 				     G_IO_ERROR_PERMISSION_DENIED,
e296c6
 				     message);
e296c6
 		return;
e296c6
 	}
e296c6
 	g_set_error_literal (error,
e296c6
 			     G_IO_ERROR,
e296c6
 			     G_IO_ERROR_NOT_SUPPORTED,
e296c6
 			     message);
e296c6
 }
e296c6
 
e296c6
 static gboolean
e296c6
 _helper_unregister (GError **error)
e296c6
 {
e296c6
 	g_autoptr(GDBusProxy) proxy = NULL;
e296c6
 	g_autoptr(GVariantBuilder) proxy_options = NULL;
e296c6
 	g_autoptr(GVariant) res = NULL;
e296c6
 
e296c6
 	proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
e296c6
 					       G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
e296c6
 					       G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
e296c6
 					       NULL,
e296c6
 					       "com.redhat.RHSM1",
e296c6
 					       "/com/redhat/RHSM1/Unregister",
e296c6
 					       "com.redhat.RHSM1.Unregister",
e296c6
 					       NULL, error);
e296c6
 	if (proxy == NULL) {
e296c6
 		g_prefix_error (error, "Failed to get proxy: ");
e296c6
 		return FALSE;
e296c6
 	}
e296c6
 	proxy_options = g_variant_builder_new (G_VARIANT_TYPE_VARDICT);
e296c6
 	res = g_dbus_proxy_call_sync (proxy,
e296c6
 				      "Unregister",
e296c6
 				      g_variant_new ("(a{sv}s)",
e296c6
 						     proxy_options,
e296c6
 						     locale),
e296c6
 				      G_DBUS_CALL_FLAGS_NONE,
e296c6
 				      DBUS_TIMEOUT,
e296c6
 				      NULL, error);
e296c6
 	return res != NULL;
e296c6
 }
e296c6
 
e296c6
 static gboolean
e296c6
 _helper_auto_attach (GError **error)
e296c6
 {
e296c6
 	const gchar *str = NULL;
e296c6
+	g_autoptr(GError) error_local = NULL;
e296c6
 	g_autoptr(GDBusProxy) proxy = NULL;
e296c6
 	g_autoptr(GVariantBuilder) proxy_options = NULL;
e296c6
 	g_autoptr(GVariant) res = NULL;
e296c6
 
e296c6
 	g_debug ("auto-attaching subscriptions");
e296c6
 	proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
e296c6
 					       G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
e296c6
 					       G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
e296c6
 					       NULL,
e296c6
 					       "com.redhat.RHSM1",
e296c6
 					       "/com/redhat/RHSM1/Attach",
e296c6
 					       "com.redhat.RHSM1.Attach",
e296c6
-					       NULL, error);
e296c6
+					       NULL, &error_local);
e296c6
 	if (proxy == NULL) {
e296c6
-		g_prefix_error (error, "Failed to get proxy: ");
e296c6
+		g_dbus_error_strip_remote_error (error_local);
e296c6
+		g_propagate_prefixed_error (error,
e296c6
+					    g_steal_pointer (&error_local),
e296c6
+					    "Failed to get proxy: ");
e296c6
 		return FALSE;
e296c6
 	}
e296c6
 	proxy_options = g_variant_builder_new (G_VARIANT_TYPE_VARDICT);
e296c6
 	res = g_dbus_proxy_call_sync (proxy,
e296c6
 				      "AutoAttach",
e296c6
 				      g_variant_new ("(sa{sv}s)",
e296c6
 						     "", /* now? */
e296c6
 						     proxy_options,
e296c6
 						     locale),
e296c6
 				      G_DBUS_CALL_FLAGS_NONE,
e296c6
 				      DBUS_TIMEOUT,
e296c6
-				      NULL, error);
e296c6
-	if (res == NULL)
e296c6
-		return FALSE;
e296c6
+				      NULL, &error_local);
e296c6
+	if (res == NULL) {
e296c6
+		g_dbus_error_strip_remote_error (error_local);
e296c6
+		_helper_convert_error (error_local->message, error);
e296c6
+
e296c6
+		if (*error != NULL) {
e296c6
+			g_prefix_error (error, "Failed to get proxy: ");
e296c6
+			return FALSE;
e296c6
+		}
e296c6
+
e296c6
+		return TRUE;
e296c6
+	}
e296c6
 	g_variant_get (res, "(&s)", &str);
e296c6
 	g_debug ("Attach.AutoAttach: %s", str);
e296c6
 	return TRUE;
e296c6
 }
e296c6
 
e296c6
 static gboolean
e296c6
 _helper_save_config (const gchar *key, const gchar *value, GError **error)
e296c6
 {
e296c6
 	g_autoptr(GDBusProxy) proxy = NULL;
e296c6
 	g_autoptr(GVariant) res = NULL;
e296c6
 	proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
e296c6
 					       G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
e296c6
 					       G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
e296c6
 					       NULL,
e296c6
 					       "com.redhat.RHSM1",
e296c6
 					       "/com/redhat/RHSM1/Config",
e296c6
 					       "com.redhat.RHSM1.Config",
e296c6
 					       NULL, error);
e296c6
 	if (proxy == NULL) {
e296c6
 		g_prefix_error (error, "Failed to get proxy: ");
e296c6
 		return FALSE;
e296c6
 	}
e296c6
 	res = g_dbus_proxy_call_sync (proxy, "Set",
e296c6
 				      g_variant_new ("(svs)",
e296c6
 						     key,
e296c6
 						     g_variant_new_string (value),
e296c6
 						     locale),
e296c6
 				      G_DBUS_CALL_FLAGS_NONE,
e296c6
 				      DBUS_TIMEOUT,
e296c6
 				      NULL, error);
e296c6
@@ -298,105 +322,109 @@ main (int argc, char *argv[])
e296c6
 			g_printerr ("Required --organisation\n");
e296c6
 			return G_IO_ERROR_INVALID_DATA;
e296c6
 		}
e296c6
 
e296c6
 		g_input_stream_read (standard_input_stream, activation_key, sizeof (activation_key) - 1, NULL, &error_local);
e296c6
 
e296c6
 		if (error_local != NULL) {
e296c6
 			g_printerr ("Could not read activation key: %s\n", error_local->message);
e296c6
 			return G_IO_ERROR_INVALID_DATA;
e296c6
 		}
e296c6
 
e296c6
 		g_debug ("trying to unregister in case machine is already registered");
e296c6
 		_helper_unregister (NULL);
e296c6
 
e296c6
 		g_debug ("registering using activation key");
e296c6
 		activation_keys = g_strsplit (activation_key, ",", -1);
e296c6
 		res = g_dbus_proxy_call_sync (proxy,
e296c6
 					      "RegisterWithActivationKeys",
e296c6
 					      g_variant_new ("(s^asa{ss}a{ss}s)",
e296c6
 							     organisation,
e296c6
 							     activation_keys,
e296c6
 							     subman_options,
e296c6
 							     subman_conopts,
e296c6
 							     locale),
e296c6
 					      G_DBUS_CALL_FLAGS_NO_AUTO_START,
e296c6
 					      DBUS_TIMEOUT,
e296c6
 					      NULL, &error_local);
e296c6
 		if (res == NULL) {
e296c6
 			g_dbus_error_strip_remote_error (error_local);
e296c6
 			_helper_convert_error (error_local->message, &error);
e296c6
-			g_printerr ("Failed to RegisterWithActivationKeys: %s\n", error->message);
e296c6
-			return error->code;
e296c6
+			if (error != NULL) {
e296c6
+				g_printerr ("Failed to RegisterWithActivationKeys: %s\n", error->message);
e296c6
+				return error->code;
e296c6
+			}
e296c6
 		}
e296c6
 	} else if (g_strcmp0 (kind, "register-with-username") == 0) {
e296c6
 		g_autoptr(GError) error_local = NULL;
e296c6
 		g_autoptr(GVariant) res = NULL;
e296c6
 		gchar password[PIPE_BUF + 1] = "";
e296c6
 
e296c6
 		if (username == NULL) {
e296c6
 			g_printerr ("Required --username\n");
e296c6
 			return G_IO_ERROR_INVALID_DATA;
e296c6
 		}
e296c6
 		if (organisation == NULL) {
e296c6
 			g_printerr ("Required --organisation\n");
e296c6
 			return G_IO_ERROR_INVALID_DATA;
e296c6
 		}
e296c6
 
e296c6
 		g_input_stream_read (standard_input_stream, password, sizeof (password) - 1, NULL, &error_local);
e296c6
 
e296c6
 		if (error_local != NULL) {
e296c6
 			g_printerr ("Could not read password: %s\n", error_local->message);
e296c6
 			return G_IO_ERROR_INVALID_DATA;
e296c6
 		}
e296c6
 
e296c6
 		g_debug ("trying to unregister in case machine is already registered");
e296c6
 		_helper_unregister (NULL);
e296c6
 
e296c6
 		g_debug ("registering using username and password");
e296c6
 		res = g_dbus_proxy_call_sync (proxy,
e296c6
 					      "Register",
e296c6
 					      g_variant_new ("(sssa{ss}a{ss}s)",
e296c6
 							     organisation,
e296c6
 							     username,
e296c6
 							     password,
e296c6
 							     subman_options,
e296c6
 							     subman_conopts,
e296c6
 							     locale),
e296c6
 					      G_DBUS_CALL_FLAGS_NO_AUTO_START,
e296c6
 					      DBUS_TIMEOUT,
e296c6
 					      NULL, &error_local);
e296c6
 		if (res == NULL) {
e296c6
 			g_dbus_error_strip_remote_error (error_local);
e296c6
 			_helper_convert_error (error_local->message, &error);
e296c6
-			g_printerr ("Failed to Register: %s\n", error->message);
e296c6
-			return error->code;
e296c6
+			if (error != NULL) {
e296c6
+				g_printerr ("Failed to Register: %s\n", error->message);
e296c6
+				return error->code;
e296c6
+			}
e296c6
 		}
e296c6
 	} else {
e296c6
 		g_printerr ("Invalid --kind specified: %s\n", kind);
e296c6
 		return G_IO_ERROR_INVALID_DATA;
e296c6
 	}
e296c6
 
e296c6
 	/* set the new hostname */
e296c6
 	if (!_helper_save_config ("server.hostname", hostname, &error)) {
e296c6
 		g_printerr ("Failed to save hostname: %s\n", error->message);
e296c6
 		return G_IO_ERROR_NOT_INITIALIZED;
e296c6
 	}
e296c6
 	if (!_helper_save_config ("server.prefix", prefix, &error)) {
e296c6
 		g_printerr ("Failed to save prefix: %s\n", error->message);
e296c6
 		return G_IO_ERROR_NOT_INITIALIZED;
e296c6
 	}
e296c6
 	if (!_helper_save_config ("server.port", port, &error)) {
e296c6
 		g_printerr ("Failed to save port: %s\n", error->message);
e296c6
 		return G_IO_ERROR_NOT_INITIALIZED;
e296c6
 	}
e296c6
 
e296c6
 	/* wait for rhsmd to notice the new config */
e296c6
 	g_usleep (G_USEC_PER_SEC * 5);
e296c6
 
e296c6
 	/* auto-attach */
e296c6
 	if (!_helper_auto_attach (&error)) {
e296c6
 		g_printerr ("Failed to AutoAttach: %s\n", error->message);
e296c6
 		return G_IO_ERROR_NOT_INITIALIZED;
e296c6
 	}
e296c6
 
e296c6
 	return EXIT_SUCCESS;
e296c6
-- 
e296c6
2.30.0
e296c6