Blame SOURCES/bz1471182-crypt-1-new-ra.patch

bcdf71
From 019c3108feff48d8ad496cd0759349c46170dc2d Mon Sep 17 00:00:00 2001
bcdf71
From: Oyvind Albrigtsen <oalbrigt@redhat.com>
bcdf71
Date: Mon, 6 Apr 2020 10:23:51 +0200
bcdf71
Subject: [PATCH 1/2] crypt: new resource agent
bcdf71
bcdf71
---
bcdf71
 doc/man/Makefile.am   |   1 +
bcdf71
 heartbeat/Makefile.am |   1 +
bcdf71
 heartbeat/crypt       | 337 ++++++++++++++++++++++++++++++++++++++++++
bcdf71
 3 files changed, 339 insertions(+)
bcdf71
 create mode 100755 heartbeat/crypt
bcdf71
bcdf71
diff --git a/doc/man/Makefile.am b/doc/man/Makefile.am
bcdf71
index 478fbe4f8..53c9975ec 100644
bcdf71
--- a/doc/man/Makefile.am
bcdf71
+++ b/doc/man/Makefile.am
bcdf71
@@ -105,6 +105,7 @@ man_MANS                = ocf_heartbeat_AoEtarget.7 \
bcdf71
                           ocf_heartbeat_azure-lb.7 \
bcdf71
                           ocf_heartbeat_clvm.7 \
bcdf71
                           ocf_heartbeat_conntrackd.7 \
bcdf71
+                          ocf_heartbeat_crypt.7 \
bcdf71
                           ocf_heartbeat_db2.7 \
bcdf71
                           ocf_heartbeat_dhcpd.7 \
bcdf71
                           ocf_heartbeat_docker.7 \
bcdf71
diff --git a/heartbeat/Makefile.am b/heartbeat/Makefile.am
bcdf71
index 893115810..bbc9590ac 100644
bcdf71
--- a/heartbeat/Makefile.am
bcdf71
+++ b/heartbeat/Makefile.am
bcdf71
@@ -101,6 +101,7 @@ ocf_SCRIPTS	      = AoEtarget		\
bcdf71
 			azure-lb		\
bcdf71
 			clvm			\
bcdf71
 			conntrackd		\
bcdf71
+			crypt			\
bcdf71
 			db2			\
bcdf71
 			dhcpd			\
bcdf71
 			dnsupdate		\
bcdf71
diff --git a/heartbeat/crypt b/heartbeat/crypt
bcdf71
new file mode 100755
bcdf71
index 000000000..6bffdff89
bcdf71
--- /dev/null
bcdf71
+++ b/heartbeat/crypt
bcdf71
@@ -0,0 +1,337 @@
bcdf71
+#!/bin/sh
bcdf71
+#
bcdf71
+#	crypt/LUKS OCF RA. Manages cryptsetup devices.
bcdf71
+#
bcdf71
+# Copyright (c) 2020 Red Hat GmbH, Heinz Mauelshagen
bcdf71
+#                    All Rights Reserved.
bcdf71
+#
bcdf71
+# This program is free software; you can redistribute it and/or modify
bcdf71
+# it under the terms of version 2 of the GNU General Public License as
bcdf71
+# published by the Free Software Foundation.
bcdf71
+#
bcdf71
+# This program is distributed in the hope that it would be useful, but
bcdf71
+# WITHOUT ANY WARRANTY; without even the implied warranty of
bcdf71
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
bcdf71
+#
bcdf71
+# Further, this software is distributed without any warranty that it is
bcdf71
+# free of the rightful claim of any third person regarding infringement
bcdf71
+# or the like.  Any license provided herein, whether implied or
bcdf71
+# otherwise, applies only to this software file.  Patent licenses, if
bcdf71
+# any, provided herein do not apply to combinations of this program with
bcdf71
+# other software, or any other product whatsoever.
bcdf71
+#
bcdf71
+# You should have received a copy of the GNU General Public License
bcdf71
+# along with this program; if not, write the Free Software Foundation,
bcdf71
+# Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
bcdf71
+#
bcdf71
+
bcdf71
+#######################################################################
bcdf71
+# Initialization:
bcdf71
+
bcdf71
+: ${OCF_FUNCTIONS_DIR=${OCF_ROOT}/lib/heartbeat}
bcdf71
+. ${OCF_FUNCTIONS_DIR}/ocf-shellfuncs
bcdf71
+
bcdf71
+# Parameter defaults
bcdf71
+OCF_RESKEY_encrypted_dev_default=""
bcdf71
+OCF_RESKEY_crypt_dev_default=""
bcdf71
+OCF_RESKEY_key_file_default=""
bcdf71
+OCF_RESKEY_crypt_type_default=""
bcdf71
+OCF_RESKEY_force_stop_default="false"
bcdf71
+
bcdf71
+: ${OCF_RESKEY_encrypted_dev=${OCF_RESKEY_encrypted_dev_default}}
bcdf71
+: ${OCF_RESKEY_crypt_dev=${OCF_RESKEY_crypt_dev_default}}
bcdf71
+: ${OCF_RESKEY_key_file=${OCF_RESKEY_key_file_default}}
bcdf71
+: ${OCF_RESKEY_crypt_type=${OCF_RESKEY_crypt_type_default}}
bcdf71
+: ${OCF_RESKEY_force_stop=${OCF_RESKEY_force_stop_default}}
bcdf71
+
bcdf71
+#######################################################################
bcdf71
+
bcdf71
+meta_data() {
bcdf71
+	cat <
bcdf71
+
bcdf71
+
bcdf71
+<resource-agent name="crypt">
bcdf71
+<version>1.0</version>
bcdf71
+
bcdf71
+<longdesc lang="en">
bcdf71
+This is a LUKS/crypt Resource Agent managing encrypted devices via cryptsetup(8).
bcdf71
+The agent imposes limitations on device types supported: luks, luks[1..N].
bcdf71
+</longdesc>
bcdf71
+<shortdesc lang="en">LUKS/crypt resource agent</shortdesc>
bcdf71
+
bcdf71
+<parameters>
bcdf71
+
bcdf71
+<parameter name="encrypted_dev" unique="1" required="1">
bcdf71
+<longdesc lang="en">
bcdf71
+Encrypted backing device, which should be defined by UUID,
bcdf71
+36 characters including '-'s as reported by blkid(8).
bcdf71
+
bcdf71
+Although it can be defined as a block device path (e.g. /dev/sdh),
bcdf71
+the UUID should be preferred over the block device path to allow for the
bcdf71
+unique discovery of the crypt backing device given the volatile nature of
bcdf71
+/dev entries (e.g. /dev/sdh on one node may be /dev/sdg on another).
bcdf71
+
bcdf71
+Only define as block device path if you know what you are doing.
bcdf71
+</longdesc>
bcdf71
+<shortdesc lang="en">Encrypted device</shortdesc>
bcdf71
+<content type="string" default="${OCF_RESKEY_encrypted_dev_default}" />
bcdf71
+</parameter>
bcdf71
+
bcdf71
+<parameter name="crypt_dev" unique="1" required="1">
bcdf71
+<longdesc lang="en">
bcdf71
+Encrypted device name, no path.  I.e. the one given in "cryptsetup open name ...".
bcdf71
+The resulting block device path is /dev/mapper/name.
bcdf71
+</longdesc>
bcdf71
+<shortdesc lang="en">Encrypted device</shortdesc>
bcdf71
+<content type="string" default="${OCF_RESKEY_crypt_dev_default}" />
bcdf71
+</parameter>
bcdf71
+
bcdf71
+<parameter name="key_file" unique="1" required="1">
bcdf71
+<longdesc lang="en">
bcdf71
+Key file path containing the encryption passphrase
bcdf71
+(aka key; see cryptsetup(8)).  For LUKS, the passphrase as of the key_file
bcdf71
+parameter is used to decrypt a randomly selected key when the device was created.
bcdf71
+</longdesc>
bcdf71
+<shortdesc lang="en">Key file</shortdesc>
bcdf71
+<content type="string" default="${OCF_RESKEY_key_file_default}" />
bcdf71
+</parameter>
bcdf71
+
bcdf71
+<parameter name="crypt_type" unique="1" required="1">
bcdf71
+<longdesc lang="en">
bcdf71
+Encryption (device) type (e.g. "luks" or "luks2").
bcdf71
+
bcdf71
+This parameter affirms the encryption format as of the crypt metadata
bcdf71
+thus allowing for safety measures when starting the encrypted resource.
bcdf71
+</longdesc>
bcdf71
+<shortdesc lang="en">Encryption type</shortdesc>
bcdf71
+<content type="string" default="${OCF_RESKEY_crypt_type_default}" />
bcdf71
+</parameter>
bcdf71
+
bcdf71
+<parameter name="force_stop" unique="0" required="0">
bcdf71
+<longdesc lang="en">
bcdf71
+If processes or kernel threads are using the crypt device, it cannot
bcdf71
+be stopped. We will try to stop processes, first by sending TERM and
bcdf71
+then, if that doesn't help in $PROC_CLEANUP_TIME seconds, using KILL.
bcdf71
+The lsof(8) program is required to get the list of array users.
bcdf71
+Of course, the kernel threads cannot be stopped this way.
bcdf71
+If the processes are critical for data integrity, then set this
bcdf71
+parameter to false. Note that in that case the stop operation
bcdf71
+will fail and the node will be fenced.
bcdf71
+</longdesc>
bcdf71
+<shortdesc lang="en">force stop processes using the crpyt device</shortdesc>
bcdf71
+<content type="boolean" default="${OCF_RESKEY_force_stop_default}" />
bcdf71
+</parameter>
bcdf71
+
bcdf71
+</parameters>
bcdf71
+
bcdf71
+<actions>
bcdf71
+<action name="start"        timeout="20s" />
bcdf71
+<action name="stop"         timeout="20s" />
bcdf71
+<action name="monitor"      timeout="20s" interval="10s" depth="0" />
bcdf71
+<action name="meta-data"    timeout="5s" />
bcdf71
+<action name="validate-all" timeout="10s" />
bcdf71
+</actions>
bcdf71
+</resource-agent>
bcdf71
+END
bcdf71
+}
bcdf71
+
bcdf71
+# Disable cryptsetup auto-recovery if cloned.
bcdf71
+disable_locks=""
bcdf71
+ocf_is_clone && disable_locks="--disable-locks"
bcdf71
+
bcdf71
+crypt_usage() {
bcdf71
+	cat <
bcdf71
+usage: $0 {start|stop|monitor|usage|meta-data|validate-all}
bcdf71
+
bcdf71
+Expects to have a fully populated OCF RA-compliant environment set.
bcdf71
+END
bcdf71
+}
bcdf71
+
bcdf71
+encrypted_dev="${OCF_RESKEY_encrypted_dev}"
bcdf71
+crypt_dev="${OCF_RESKEY_crypt_dev}"
bcdf71
+crypt_dev_path="/dev/mapper/$crypt_dev"
bcdf71
+key_file="${OCF_RESKEY_key_file}"
bcdf71
+crypt_type="${OCF_RESKEY_crypt_type}"
bcdf71
+force_stop="${OCF_RESKEY_force_stop}"
bcdf71
+
bcdf71
+crypt_validate_all() {
bcdf71
+	if ! have_binary cryptsetup; then
bcdf71
+		ocf_exit_reason "Please install cryptsetup(8)"
bcdf71
+		return $OCF_ERR_INSTALLED
bcdf71
+	fi
bcdf71
+	if [ -z "$encrypted_dev" ]; then
bcdf71
+		ocf_exit_reason "Undefined OCF_RESKEY_encrypted_dev"
bcdf71
+		return $OCF_ERR_CONFIGURED
bcdf71
+	fi
bcdf71
+	if [ -n "$encrypted_dev" ]; then
bcdf71
+		case "$encrypted_dev" in
bcdf71
+		*-*-*-*) if [ `echo "$encrypted_dev" | wc -c` -ne 37 ]; then
bcdf71
+				ocf_exit_reason "Bogus encrypted device UUID \"$encrypted_dev\""
bcdf71
+				return $OCF_ERR_ARGS
bcdf71
+			 fi
bcdf71
+			 encrypted_dev=/dev/disk/by-uuid/"$encrypted_dev";;
bcdf71
+		*)	 case "$encrypted_dev" in
bcdf71
+			 /dev/*) ;;
bcdf71
+			 *)	ocf_exit_reason "Bogus encrypted device path"
bcdf71
+				return $OCF_ERR_ARGS;;
bcdf71
+			 esac
bcdf71
+		esac
bcdf71
+	fi
bcdf71
+	if [ ! -b "$encrypted_dev" ]; then
bcdf71
+		ocf_exit_reason "Encrypted device $encrypted_dev not accessible"
bcdf71
+		return $OCF_ERR_ARGS
bcdf71
+	fi
bcdf71
+	echo "$crypt_dev" | grep "/" >/dev/null
bcdf71
+	if [ $? -eq 0 ] &&  [ -z "$crypt_dev" ]; then
bcdf71
+		ocf_exit_reason "Crypt device \"$crypt_dev\" name has to at least 1 character long and without path"
bcdf71
+		return $OCF_ERR_ARGS
bcdf71
+	fi
bcdf71
+	if [ ! -r "$key_file" ]; then
bcdf71
+		ocf_exit_reason "Hash key file $key_file not accessible"
bcdf71
+		return $OCF_ERR_ARGS
bcdf71
+	fi
bcdf71
+	if ! ocf_is_true "$force_stop"  && "$force_stop" != "false" ]]; then
bcdf71
+		ocf_exit_reason "Bogus force_stop=\"$force_stop\" attribute"
bcdf71
+		return $OCF_ERR_CONFIGURED
bcdf71
+	fi
bcdf71
+	if "$force_stop" = "true" && ! have_binary lsof; then
bcdf71
+		ocf_exit_reason "Force stop requested, please install lsof(8)"
bcdf71
+		return $OCF_ERR_INSTALLED
bcdf71
+	fi
bcdf71
+	cryptsetup isLuks $encrypted_dev 2>/dev/null
bcdf71
+	if [ $? -ne 0 ]; then
bcdf71
+		ocf_exit_reason "$encrypted_dev is not a Luks formatted device"
bcdf71
+		return $OCF_ERR_CONFIGURED
bcdf71
+	fi
bcdf71
+
bcdf71
+	return $OCF_SUCCESS
bcdf71
+}
bcdf71
+
bcdf71
+get_users_pids() {
bcdf71
+	ocf_log debug "running lsof to list \"$crypt_dev\" users..."
bcdf71
+	ocf_run -warn 'lsof $crypt_dev_path | tail -n +2 | awk "{print $2}" | sort -u'
bcdf71
+}
bcdf71
+
bcdf71
+stop_crypt_users() {
bcdf71
+	local pids=`get_users_pids`
bcdf71
+
bcdf71
+	if [ -z "$pids" ]; then
bcdf71
+		ocf_log warn "lsof reported no users holding arrays"
bcdf71
+		return 2
bcdf71
+	fi
bcdf71
+
bcdf71
+	ocf_stop_processes TERM $PROC_CLEANUP_TIME $pids
bcdf71
+}
bcdf71
+
bcdf71
+show_users() {
bcdf71
+	local dm_dev
bcdf71
+
bcdf71
+	ocf_log info "running lsof to list \"$crypt_dev\" users..."
bcdf71
+	ocf_run -warn lsof $crypt_dev_path
bcdf71
+
bcdf71
+	dm_dev=$(basename $(realpath $crypt_dev_path))
bcdf71
+	if [ -d /sys/block/$dm_dev/holders ]; then
bcdf71
+		ocf_log debug "ls -l /sys/block/$dm_dev/holders"
bcdf71
+		ocf_run -warn ls -l /sys/block/$dm_dev/holders
bcdf71
+	fi
bcdf71
+}
bcdf71
+
bcdf71
+crypt_stop_one() {
bcdf71
+	cryptsetup close $crypt_dev $disable_locks
bcdf71
+}
bcdf71
+
bcdf71
+#######################################################################
bcdf71
+#
bcdf71
+# Action: START an encrypted resource
bcdf71
+#
bcdf71
+crypt_start() {
bcdf71
+	local rc
bcdf71
+
bcdf71
+	cryptsetup open $encrypted_dev $crypt_dev --type $crypt_type $disable_locks --key-file=$key_file
bcdf71
+	rc=$?
bcdf71
+	if [ $rc -eq 0 ];then
bcdf71
+		crypt_monitor
bcdf71
+		rc=$?
bcdf71
+	else
bcdf71
+		rc=$OCF_ERR_GERNERIC
bcdf71
+	fi
bcdf71
+	[ $rc -ne $OCF_SUCCESS ] ocf_exit_reason "Failed to start encrypted device \"$crypt_dev\""
bcdf71
+
bcdf71
+	return $rc
bcdf71
+}
bcdf71
+
bcdf71
+#
bcdf71
+# Action: STOP an encrypted resource
bcdf71
+#
bcdf71
+crypt_stop() {
bcdf71
+	local rc
bcdf71
+
bcdf71
+	crypt_monitor
bcdf71
+	rc=$?
bcdf71
+	if [ $rc -ne $OCF_NOT_RUNNING ]; then
bcdf71
+		crypt_stop_one
bcdf71
+		crypt_monitor
bcdf71
+		rc=$?
bcdf71
+	fi
bcdf71
+	if [ $rc -ne $OCF_NOT_RUNNING ] && ocf_is_true $FORCESTOP; then
bcdf71
+		stop_crypt_users
bcdf71
+		case $? in
bcdf71
+		2) rc=$OCF_SUCCESS;;
bcdf71
+		*) crypt_stop_one
bcdf71
+		   crypt_monitor
bcdf71
+		   rc=$?;;
bcdf71
+		esac
bcdf71
+	fi
bcdf71
+	if [ $rc -ne $OCF_NOT_RUNNING ]; then
bcdf71
+		ocf_log warn "Couldn't stop crypt device \"$crypt_dev\" (rc=$rc)"
bcdf71
+		show_users
bcdf71
+		ocf_exit_reason "Failed to stop crypt device \"$crypt_dev\"!"
bcdf71
+		return $OCF_ERR_GENERIC
bcdf71
+	fi
bcdf71
+
bcdf71
+	return $OCF_SUCCESS
bcdf71
+}
bcdf71
+
bcdf71
+#
bcdf71
+# Action: MONITOR an encrypted resource
bcdf71
+#
bcdf71
+crypt_monitor() {
bcdf71
+	cryptsetup status $crypt_dev $disable_locks &>/dev/null
bcdf71
+	if [ $? -eq 0 ]; then
bcdf71
+		[ -L $crypt_dev_path ] && return $OCF_SUCCESS
bcdf71
+		return $OCF_ERR_GENERIC
bcdf71
+	fi
bcdf71
+
bcdf71
+        [ "$__OCF_ACTION" = "monitor" ] && ! ocf_is_probe && ocf_exit_reason "Crypt resource not running"
bcdf71
+	return $OCF_NOT_RUNNING
bcdf71
+}
bcdf71
+
bcdf71
+# Check for stange argument count.
bcdf71
+if [ $# -ne 1 ]; then
bcdf71
+	usage
bcdf71
+	exit $OCF_ERR_ARGS
bcdf71
+fi
bcdf71
+
bcdf71
+case "$__OCF_ACTION" in
bcdf71
+meta-data)	meta_data
bcdf71
+		exit $OCF_SUCCESS;;
bcdf71
+usage|help)	crypt_usage
bcdf71
+		exit $OCF_SUCCESS;;
bcdf71
+esac
bcdf71
+
bcdf71
+# XME: remove once pacemaker is fixed and calls this action
bcdf71
+crypt_validate_all
bcdf71
+rc=$?
bcdf71
+[ $rc -ne $OCF_SUCCESS ] && exit $rc
bcdf71
+
bcdf71
+case "$__OCF_ACTION" in
bcdf71
+start)		crypt_start; rc=$?;;
bcdf71
+stop)		crypt_stop; rc=$?;;
bcdf71
+monitor)	crypt_monitor; rc=$?;;
bcdf71
+validate-all)	rc=$OCF_SUCCESS;; # crypt_validate_all would have errored out above already.
bcdf71
+*)		crypt_usage
bcdf71
+		exit $OCF_ERR_UNIMPLEMENTED;;
bcdf71
+esac
bcdf71
+
bcdf71
+ocf_log debug "${OCF_RESOURCE_INSTANCE} $__OCF_ACTION : $rc"
bcdf71
+exit $rc
bcdf71
bcdf71
From 5e0d35f8db967419ea9f1234ab621b88babcf3ea Mon Sep 17 00:00:00 2001
bcdf71
From: Oyvind Albrigtsen <oalbrigt@redhat.com>
bcdf71
Date: Tue, 7 Apr 2020 12:39:24 +0200
bcdf71
Subject: [PATCH 2/2] crypt: force_stop check fixes
bcdf71
bcdf71
---
bcdf71
 heartbeat/crypt | 8 ++------
bcdf71
 1 file changed, 2 insertions(+), 6 deletions(-)
bcdf71
bcdf71
diff --git a/heartbeat/crypt b/heartbeat/crypt
bcdf71
index 6bffdff89..8bfa1094d 100755
bcdf71
--- a/heartbeat/crypt
bcdf71
+++ b/heartbeat/crypt
bcdf71
@@ -190,11 +190,7 @@ crypt_validate_all() {
bcdf71
 		ocf_exit_reason "Hash key file $key_file not accessible"
bcdf71
 		return $OCF_ERR_ARGS
bcdf71
 	fi
bcdf71
-	if ! ocf_is_true "$force_stop"  && "$force_stop" != "false" ]]; then
bcdf71
-		ocf_exit_reason "Bogus force_stop=\"$force_stop\" attribute"
bcdf71
-		return $OCF_ERR_CONFIGURED
bcdf71
-	fi
bcdf71
-	if "$force_stop" = "true" && ! have_binary lsof; then
bcdf71
+	if ocf_is_true "$force_stop" && ! have_binary lsof; then
bcdf71
 		ocf_exit_reason "Force stop requested, please install lsof(8)"
bcdf71
 		return $OCF_ERR_INSTALLED
bcdf71
 	fi
bcdf71
@@ -273,7 +269,7 @@ crypt_stop() {
bcdf71
 		crypt_monitor
bcdf71
 		rc=$?
bcdf71
 	fi
bcdf71
-	if [ $rc -ne $OCF_NOT_RUNNING ] && ocf_is_true $FORCESTOP; then
bcdf71
+	if [ $rc -ne $OCF_NOT_RUNNING ] && ocf_is_true $force_stop; then
bcdf71
 		stop_crypt_users
bcdf71
 		case $? in
bcdf71
 		2) rc=$OCF_SUCCESS;;