Skip to content

Better handling for selinux errors #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: release/8.0
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 69 additions & 14 deletions scripts/postinstall.sh
Original file line number Diff line number Diff line change
@@ -1,30 +1,85 @@
#!/bin/bash

# Install SELinux policy module if SELinux is enforcing
if command -v checkmodule &> /dev/null && command -v semodule_package &> /dev/null; then
# Compile policy module
checkmodule -M -m /usr/share/selinux/packages/redis-ce.te -o /usr/share/selinux/packages/redis-ce.mod
semodule_package -m /usr/share/selinux/packages/redis-ce.mod -o /usr/share/selinux/packages/redis-ce.pp -f /usr/share/selinux/packages/redis-ce.fc
export SELINUX_ERROR=
export SELINUX_ENABLED=

# Install or update the policy module
semodule -i /usr/share/selinux/packages/redis-ce.pp
fi
selinux_enabled() {
local selinux_status
if command -v getenforce &> /dev/null; then
selinux_status=$(getenforce 2>/dev/null)
if [ "$selinux_status" = "Enforcing" ] || [ "$selinux_status" = "Permissive" ]; then
echo 1
return 0
fi
fi
return 1
}

# Allow writing to /etc/redis/sentinel/ for redis-sentinel
if command -v chcon &> /dev/null; then
chcon -t redis_conf_t '/etc/redis/sentinel' '/etc/redis/sentinel/sentinel.conf'
fi
selinux_run_command() {
local cmd_output
local cmd_res
cmd_output=$("$@" 2>&1)
cmd_res=$?
if [ $cmd_res -ne 0 ]; then
SELINUX_ERROR=1
# Output the error if SELinux is enabled
# If it's not then likely SELinux has been intentionally removed
if [ "$SELINUX_ENABLED" = "1" ]; then
echo "$cmd_output"
fi
return 1
fi
}

setup_selinux() {
# Install SELinux policy module if SELinux tools are available
if command -v checkmodule &> /dev/null && command -v semodule_package &> /dev/null; then
# Compile policy module
selinux_run_command checkmodule \
-M -m /usr/share/selinux/packages/redis-ce.te \
-o /usr/share/selinux/packages/redis-ce.mod \
|| return 1

selinux_run_command semodule_package \
-m /usr/share/selinux/packages/redis-ce.mod \
-o /usr/share/selinux/packages/redis-ce.pp \
-f /usr/share/selinux/packages/redis-ce.fc \
|| return 1

# Install or update the policy module
selinux_run_command semodule -i /usr/share/selinux/packages/redis-ce.pp || return 1
fi

# Allow writing to /etc/redis/sentinel/ for redis-sentinel
if command -v chcon &> /dev/null; then
selinux_run_command chcon \
-t redis_conf_t \
'/etc/redis/sentinel' \
'/etc/redis/sentinel/sentinel.conf' \
|| return 1
fi
}

selinux_show_error() {
if [ "$SELINUX_ENABLED" = "1" ]; then
echo "Error: Unable to setup SELinux policies. See above for details."
else
echo "Warning: SELinux policies not configured. To enable SELinux in the future, install selinux-policy-targeted and reinstall this package."
fi
}

SELINUX_ENABLED=$(selinux_enabled)
setup_selinux || selinux_show_error

#
# Handle service setup
# $1 will be 1 for initial install and 2 for upgrade
#

if [ "$1" = "1" ]; then
# This is a fresh install
systemctl daemon-reload >/dev/null 2>&1 || :
systemctl enable redis.service >/dev/null 2>&1 || :
elif [ "$1" = "2" ]; then
# This is an upgrade
systemctl daemon-reload >/dev/null 2>&1 || :
fi
fi
Loading