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