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