1df6c8
From dcf3f74fa7e812dfe89667bd6219f70a8457f755 Mon Sep 17 00:00:00 2001
1df6c8
From: Anoop C S <anoopcs@redhat.com>
1df6c8
Date: Thu, 6 Jun 2019 18:33:19 +0530
1df6c8
Subject: [PATCH 324/335] extras/hooks: Add SELinux label on new bricks during
1df6c8
 add-brick
1df6c8
1df6c8
Backport of https://review.gluster.org/c/glusterfs/+/22834
1df6c8
1df6c8
Change-Id: Ifd8ae5eeb91b968cc1a9a9b5d15844c5233d56db
1df6c8
BUG: 1686800
1df6c8
Signed-off-by: Anoop C S <anoopcs@redhat.com>
1df6c8
Reviewed-on: https://code.engineering.redhat.com/gerrit/185855
1df6c8
Tested-by: RHGS Build Bot <nigelb@redhat.com>
1df6c8
Reviewed-by: Sunil Kumar Heggodu Gopala Acharya <sheggodu@redhat.com>
1df6c8
---
1df6c8
 .../add-brick/post/S10selinux-label-brick.sh       | 100 +++++++++++++++++++++
1df6c8
 1 file changed, 100 insertions(+)
1df6c8
 create mode 100755 extras/hook-scripts/add-brick/post/S10selinux-label-brick.sh
1df6c8
1df6c8
diff --git a/extras/hook-scripts/add-brick/post/S10selinux-label-brick.sh b/extras/hook-scripts/add-brick/post/S10selinux-label-brick.sh
1df6c8
new file mode 100755
1df6c8
index 0000000..4a17c99
1df6c8
--- /dev/null
1df6c8
+++ b/extras/hook-scripts/add-brick/post/S10selinux-label-brick.sh
1df6c8
@@ -0,0 +1,100 @@
1df6c8
+#!/bin/bash
1df6c8
+#
1df6c8
+# Install to hooks/<HOOKS_VER>/add-brick/post
1df6c8
+#
1df6c8
+# Add an SELinux file context for each brick using the glusterd_brick_t type.
1df6c8
+# This ensures that the brick is relabeled correctly on an SELinux restart or
1df6c8
+# restore. Subsequently, run a restore on the brick path to set the selinux
1df6c8
+# labels.
1df6c8
+#
1df6c8
+###
1df6c8
+
1df6c8
+PROGNAME="Sselinux"
1df6c8
+OPTSPEC="volname:,version:,gd-workdir:,volume-op:"
1df6c8
+VOL=
1df6c8
+
1df6c8
+parse_args () {
1df6c8
+  ARGS=$(getopt -o '' -l ${OPTSPEC} -n ${PROGNAME} -- "$@")
1df6c8
+  eval set -- "${ARGS}"
1df6c8
+
1df6c8
+  while true; do
1df6c8
+    case ${1} in
1df6c8
+      --volname)
1df6c8
+        shift
1df6c8
+        VOL=${1}
1df6c8
+        ;;
1df6c8
+      --gd-workdir)
1df6c8
+          shift
1df6c8
+          GLUSTERD_WORKDIR=$1
1df6c8
+          ;;
1df6c8
+      --version)
1df6c8
+          shift
1df6c8
+          ;;
1df6c8
+      --volume-op)
1df6c8
+          shift
1df6c8
+          ;;
1df6c8
+      *)
1df6c8
+          shift
1df6c8
+          break
1df6c8
+          ;;
1df6c8
+    esac
1df6c8
+    shift
1df6c8
+  done
1df6c8
+}
1df6c8
+
1df6c8
+set_brick_labels()
1df6c8
+{
1df6c8
+  local volname="${1}"
1df6c8
+  local fctx
1df6c8
+  local list=()
1df6c8
+
1df6c8
+  fctx="$(semanage fcontext --list -C)"
1df6c8
+
1df6c8
+  # wait for new brick path to be updated under
1df6c8
+  # ${GLUSTERD_WORKDIR}/vols/${volname}/bricks/
1df6c8
+  sleep 5
1df6c8
+
1df6c8
+  # grab the path for each local brick
1df6c8
+  brickpath="${GLUSTERD_WORKDIR}/vols/${volname}/bricks/"
1df6c8
+  brickdirs=$(
1df6c8
+    find "${brickpath}" -type f -exec grep '^path=' {} \; | \
1df6c8
+    cut -d= -f 2 | \
1df6c8
+    sort -u
1df6c8
+  )
1df6c8
+
1df6c8
+  # create a list of bricks for which custom SELinux
1df6c8
+  # label doesn't exist
1df6c8
+  for b in ${brickdirs}; do
1df6c8
+    pattern="${b}(/.*)?"
1df6c8
+    echo "${fctx}" | grep "^${pattern}\s" >/dev/null
1df6c8
+    if [[ $? -ne 0 ]]; then
1df6c8
+      list+=("${pattern}")
1df6c8
+    fi
1df6c8
+  done
1df6c8
+
1df6c8
+  # Add a file context for each brick path in the list and associate with the
1df6c8
+  # glusterd_brick_t SELinux type.
1df6c8
+  for p in ${list[@]}
1df6c8
+  do
1df6c8
+    semanage fcontext --add -t glusterd_brick_t -r s0 "${p}"
1df6c8
+  done
1df6c8
+
1df6c8
+  # Set the labels for which SELinux label was added above
1df6c8
+  for b in ${brickdirs}
1df6c8
+  do
1df6c8
+    echo "${list[@]}" | grep "${b}" >/dev/null
1df6c8
+    if [[ $? -eq 0 ]]; then
1df6c8
+      restorecon -R "${b}"
1df6c8
+    fi
1df6c8
+  done
1df6c8
+}
1df6c8
+
1df6c8
+SELINUX_STATE=$(which getenforce && getenforce)
1df6c8
+[ "${SELINUX_STATE}" = 'Disabled' ] && exit 0
1df6c8
+
1df6c8
+parse_args "$@"
1df6c8
+[ -z "${VOL}" ] && exit 1
1df6c8
+
1df6c8
+set_brick_labels "${VOL}"
1df6c8
+
1df6c8
+exit 0
1df6c8
-- 
1df6c8
1.8.3.1
1df6c8