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