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

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