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