9f5ccc
From 8a8c508b529f7609fc5caa10bc79ba817f5d274a Mon Sep 17 00:00:00 2001
9f5ccc
From: Milan Zink <mzink@redhat.com>
9f5ccc
Date: Mon, 5 Feb 2018 15:04:37 +0100
9f5ccc
Subject: [PATCH 343/344] extras/hooks: syntactical errors in SELinux hooks,
9f5ccc
 scipt logic improved
9f5ccc
9f5ccc
Backport of https://review.gluster.org/c/glusterfs/+/19502
9f5ccc
9f5ccc
Change-Id: Ia5fa1df81bbaec3a84653d136a331c76b457f42c
9f5ccc
BUG: 1686800
9f5ccc
Signed-off-by: Anoop C S <anoopcs@redhat.com>
9f5ccc
Reviewed-on: https://code.engineering.redhat.com/gerrit/187692
9f5ccc
Tested-by: RHGS Build Bot <nigelb@redhat.com>
9f5ccc
Reviewed-by: Niels de Vos <ndevos@redhat.com>
9f5ccc
Reviewed-by: Sunil Kumar Heggodu Gopala Acharya <sheggodu@redhat.com>
9f5ccc
---
9f5ccc
 .../create/post/S10selinux-label-brick.sh          | 13 +++--
9f5ccc
 .../delete/pre/S10selinux-del-fcontext.sh          | 60 +++++++++++++---------
9f5ccc
 tests/bugs/glusterfs-server/bug-877992.t           |  4 +-
9f5ccc
 3 files changed, 46 insertions(+), 31 deletions(-)
9f5ccc
9f5ccc
diff --git a/extras/hook-scripts/create/post/S10selinux-label-brick.sh b/extras/hook-scripts/create/post/S10selinux-label-brick.sh
9f5ccc
index de242d2..f9b4b1a 100755
9f5ccc
--- a/extras/hook-scripts/create/post/S10selinux-label-brick.sh
9f5ccc
+++ b/extras/hook-scripts/create/post/S10selinux-label-brick.sh
9f5ccc
@@ -34,18 +34,21 @@ parse_args () {
9f5ccc
 
9f5ccc
 set_brick_labels()
9f5ccc
 {
9f5ccc
-  volname=${1}
9f5ccc
+  volname="${1}"
9f5ccc
 
9f5ccc
   # grab the path for each local brick
9f5ccc
-  brickpath="/var/lib/glusterd/vols/${volname}/bricks/*"
9f5ccc
-  brickdirs=$(grep '^path=' "${brickpath}" | cut -d= -f 2 | sort -u)
9f5ccc
+  brickpath="/var/lib/glusterd/vols/${volname}/bricks/"
9f5ccc
+  brickdirs=$(
9f5ccc
+    find "${brickpath}" -type f -exec grep '^path=' {} \; | \
9f5ccc
+    cut -d= -f 2 | \
9f5ccc
+    sort -u
9f5ccc
+  )
9f5ccc
 
9f5ccc
   for b in ${brickdirs}; do
9f5ccc
     # Add a file context for each brick path and associate with the
9f5ccc
     # glusterd_brick_t SELinux type.
9f5ccc
-    pattern="${b}\(/.*\)?"
9f5ccc
+    pattern="${b}(/.*)?"
9f5ccc
     semanage fcontext --add -t glusterd_brick_t -r s0 "${pattern}"
9f5ccc
-
9f5ccc
     # Set the labels on the new brick path.
9f5ccc
     restorecon -R "${b}"
9f5ccc
   done
9f5ccc
diff --git a/extras/hook-scripts/delete/pre/S10selinux-del-fcontext.sh b/extras/hook-scripts/delete/pre/S10selinux-del-fcontext.sh
9f5ccc
index 6eba66f..e7f4e8f 100755
9f5ccc
--- a/extras/hook-scripts/delete/pre/S10selinux-del-fcontext.sh
9f5ccc
+++ b/extras/hook-scripts/delete/pre/S10selinux-del-fcontext.sh
9f5ccc
@@ -15,45 +15,55 @@ OPTSPEC="volname:"
9f5ccc
 VOL=
9f5ccc
 
9f5ccc
 function 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
-        *)
9f5ccc
-         shift
9f5ccc
-         break
9f5ccc
-         ;;
9f5ccc
-        esac
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
+      *)
9f5ccc
         shift
9f5ccc
-        done
9f5ccc
+        break
9f5ccc
+      ;;
9f5ccc
+    esac
9f5ccc
+    shift
9f5ccc
+  done
9f5ccc
 }
9f5ccc
 
9f5ccc
 function delete_brick_fcontext()
9f5ccc
 {
9f5ccc
-        volname=$1
9f5ccc
+  volname="${1}"
9f5ccc
+
9f5ccc
+  # grab the path for each local brick
9f5ccc
+  brickpath="/var/lib/glusterd/vols/${volname}/bricks/"
9f5ccc
+  brickdirs=$(
9f5ccc
+    find "${brickpath}" -type f -exec grep '^path=' {} \; | \
9f5ccc
+    cut -d= -f 2 | \
9f5ccc
+    sort -u
9f5ccc
+  )
9f5ccc
+
9f5ccc
+  for b in ${brickdirs}
9f5ccc
+  do
9f5ccc
+    # remove the file context associated with the brick path
9f5ccc
+    pattern="${b}(/.*)?"
9f5ccc
+    semanage fcontext --delete "${pattern}"
9f5ccc
 
9f5ccc
-        # grab the path for each local brick
9f5ccc
-        brickdirs=$(grep '^path=' /var/lib/glusterd/vols/${volname}/bricks/* | cut -d= -f 2)
9f5ccc
+    # remove the labels on brick path.
9f5ccc
+    restorecon -R "${b}"
9f5ccc
+ done
9f5ccc
 
9f5ccc
-        for b in $brickdirs
9f5ccc
-        do
9f5ccc
-                # remove the file context associated with the brick path
9f5ccc
-                semanage fcontext --delete $b\(/.*\)?
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
+[ -z "${VOL}" ] && exit 1
9f5ccc
 
9f5ccc
-delete_brick_fcontext $VOL
9f5ccc
+delete_brick_fcontext "${VOL}"
9f5ccc
 
9f5ccc
 # failure to delete the fcontext is not fatal
9f5ccc
 exit 0
9f5ccc
diff --git a/tests/bugs/glusterfs-server/bug-877992.t b/tests/bugs/glusterfs-server/bug-877992.t
9f5ccc
index aeb73ed..300000b 100755
9f5ccc
--- a/tests/bugs/glusterfs-server/bug-877992.t
9f5ccc
+++ b/tests/bugs/glusterfs-server/bug-877992.t
9f5ccc
@@ -46,7 +46,9 @@ TEST $CLI volume create $V0 $H0:$B0/${V0}1;
9f5ccc
 EXPECT "$V0" volinfo_field $V0 'Volume Name';
9f5ccc
 EXPECT 'Created' volinfo_field $V0 'Status';
9f5ccc
 EXPECT 'createPre' cat /tmp/pre.out;
9f5ccc
-EXPECT 'createPost' cat /tmp/post.out;
9f5ccc
+# Spost.sh comes after S10selinux-label-brick.sh under create post hook script
9f5ccc
+# list. So consider the delay in setting SELinux context on bricks
9f5ccc
+EXPECT_WITHIN 5 'createPost' cat /tmp/post.out;
9f5ccc
 hooks_cleanup 'create'
9f5ccc
 
9f5ccc
 
9f5ccc
-- 
9f5ccc
1.8.3.1
9f5ccc