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

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