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