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