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