Blame SOURCES/bz1343905-1-rabbitmq-cluster-dump-restore-users-3.6.x.patch

581d9d
From f00a952bd5e133cad30689d9edcc98f5d33a71a9 Mon Sep 17 00:00:00 2001
581d9d
From: Peter Lemenkov <lemenkov@redhat.com>
581d9d
Date: Thu, 16 Jun 2016 16:44:48 +0200
581d9d
Subject: [PATCH] Enable dump/restore users from RabbitMQ ver. 3.6.x
581d9d
581d9d
RabbitMQ changed internal_users scheme since ver. 3.6.0. See the
581d9d
following links for further details:
581d9d
581d9d
* rabbitmq/rabbitmq-server#270
581d9d
* rabbitmq/rabbitmq-server#310
581d9d
* rabbitmq/rabbitmq-common@9c86a7401cf464dc20527890192c5dc0fe43b6c8
581d9d
* rabbitmq/rabbitmq-server@93b5a3a8092f52063cbca3ab661c7c6bae43c512
581d9d
581d9d
CC @oalbrigt
581d9d
581d9d
Signed-off-by: Peter Lemenkov <lemenkov@redhat.com>
581d9d
---
581d9d
 heartbeat/rabbitmq-cluster | 64 ++++++++++++++++++++++++++++++++++++----------
581d9d
 1 file changed, 50 insertions(+), 14 deletions(-)
581d9d
581d9d
diff --git a/heartbeat/rabbitmq-cluster b/heartbeat/rabbitmq-cluster
581d9d
index 0724901..facca35 100755
581d9d
--- a/heartbeat/rabbitmq-cluster
581d9d
+++ b/heartbeat/rabbitmq-cluster
581d9d
@@ -342,14 +342,40 @@ rmq_start() {
581d9d
 		rmq_join_existing "$join_list"
581d9d
 		rc=$?
581d9d
 
581d9d
-                # Restore users (if any)
581d9d
-                BaseDataDir=`dirname $RMQ_DATA_DIR`
581d9d
-                if [ -f $BaseDataDir/users.erl ] ; then
581d9d
-                        rabbitmqctl eval "
581d9d
-                                {ok, [Users]} = file:consult(\"$BaseDataDir/users.erl\"),
581d9d
-                                lists:foreach(fun(X) -> mnesia:dirty_write(rabbit_user, X) end, Users).
581d9d
-                        "
581d9d
-                        rm -f $BaseDataDir/users.erl
581d9d
+		# Restore users (if any)
581d9d
+		BaseDataDir=`dirname $RMQ_DATA_DIR`
581d9d
+		if [ -f $BaseDataDir/users.erl ] ; then
581d9d
+			rabbitmqctl eval "
581d9d
+
581d9d
+				[WildPattern] = ets:select(mnesia_gvar, [ { {{rabbit_user, wild_pattern}, '\\\$1'}, [], ['\\\$1'] } ]),
581d9d
+
581d9d
+				%% Read users first
581d9d
+				{ok, [Users]} = file:consult(\"$BaseDataDir/users.erl\"),
581d9d
+
581d9d
+				Upgrade = fun
581d9d
+					({internal_user, A, B, C}) -> {internal_user, A, B, C, rabbit_password_hashing_md5};
581d9d
+					({internal_user, A, B, C, D}) -> {internal_user, A, B, C, D}
581d9d
+				end,
581d9d
+
581d9d
+				Downgrade = fun
581d9d
+					({internal_user, A, B, C}) -> {internal_user, A, B, C};
581d9d
+					({internal_user, A, B, C, rabbit_password_hashing_md5}) -> {internal_user, A, B, C};
581d9d
+					%% Incompatible scheme, so we will loose user's password ('B' value) during conversion.
581d9d
+					%% Unfortunately, this case will require manual intervention - user have to run:
581d9d
+					%%    rabbitmqctl change_password  <somenewpassword>
581d9d
+					({internal_user, A, B, C, _}) -> {internal_user, A, B, C}
581d9d
+				end,
581d9d
+
581d9d
+				case WildPattern of
581d9d
+					%% Version < 3.6.0
581d9d
+					{internal_user,'_','_','_'} ->
581d9d
+						lists:foreach(fun(X) -> mnesia:dirty_write(rabbit_user, Downgrade(X)) end, Users);
581d9d
+					%% Version >= 3.6.0
581d9d
+					{internal_user,'_','_','_','_'} ->
581d9d
+						lists:foreach(fun(X) -> mnesia:dirty_write(rabbit_user, Upgrade(X)) end, Users)
581d9d
+				end.
581d9d
+			"
581d9d
+			rm -f $BaseDataDir/users.erl
581d9d
                 fi
581d9d
 
581d9d
 		if [ $rc -ne 0 ]; then
581d9d
@@ -362,12 +388,22 @@ rmq_start() {
581d9d
 }
581d9d
 
581d9d
 rmq_stop() {
581d9d
-        # Backup users
581d9d
-        BaseDataDir=`dirname $RMQ_DATA_DIR`
581d9d
-        rabbitmqctl eval "
581d9d
-                Users = mnesia:dirty_select(rabbit_user, [{ {internal_user, '\\\$1', '_', '_'}, [{'/=', '\\\$1', <<\"guest\">>}], ['\\\$_'] } ]),
581d9d
-                file:write_file(\"$BaseDataDir/users.erl\", io_lib:fwrite(\"~p.~n\", [Users])).
581d9d
-        "
581d9d
+	# Backup users
581d9d
+	BaseDataDir=`dirname $RMQ_DATA_DIR`
581d9d
+	rabbitmqctl eval "
581d9d
+		[WildPattern] = ets:select(mnesia_gvar, [ { {{rabbit_user, wild_pattern}, '\\\$1'}, [], ['\\\$1'] } ]),
581d9d
+
581d9d
+		Users = case WildPattern of
581d9d
+			%% Version < 3.6.0
581d9d
+			{internal_user,'_','_','_'} ->
581d9d
+				mnesia:dirty_select(rabbit_user, [{ {internal_user, '\\\$1', '_', '_'}, [{'/=', '\\\$1', <<\"guest\">>}], ['\\\$_'] } ]);
581d9d
+			%% Version >= 3.6.0
581d9d
+			{internal_user,'_','_','_','_'} ->
581d9d
+				mnesia:dirty_select(rabbit_user, [{ {internal_user, '\\\$1', '_', '_', '_'}, [{'/=', '\\\$1', <<\"guest\">>}], ['\\\$_'] } ])
581d9d
+		end,
581d9d
+
581d9d
+		file:write_file(\"$BaseDataDir/users.erl\", io_lib:fwrite(\"~p.~n\", [Users])).
581d9d
+	"
581d9d
 
581d9d
 	rmq_monitor
581d9d
 	if [ $? -eq $OCF_NOT_RUNNING ]; then