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