Skip to content

Commit

Permalink
fail2ban: make sure the logfile exists (#1737)
Browse files Browse the repository at this point in the history
* make sure the logfile exists before reloading fail2ban

* update docs

* enhancement: fix file perms and make sure the folder exists

* fix folder path

---------

Co-authored-by: OniriCorpe <OniriCorpe@users.noreply.github.com>
  • Loading branch information
OniriCorpe and OniriCorpe authored Nov 21, 2023
1 parent b06484f commit 9a7731a
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions helpers/fail2ban
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@
# You can find some more explainations about how to make a regex here :
# https://www.fail2ban.org/wiki/index.php/MANUAL_0_8#Filters
#
# Note that the logfile need to exist before to call this helper !!
#
# To validate your regex you can test with this command:
# ```
# fail2ban-regex /var/log/YOUR_LOG_FILE_PATH /etc/fail2ban/filter.d/YOUR_APP.conf
Expand Down Expand Up @@ -102,6 +100,15 @@ ignoreregex =
ynh_add_config --template="f2b_jail.conf" --destination="/etc/fail2ban/jail.d/$app.conf"
ynh_add_config --template="f2b_filter.conf" --destination="/etc/fail2ban/filter.d/$app.conf"

# Create the folder and logfile if they doesn't exist,
# as fail2ban require an existing logfile before configuration
mkdir -p "/var/log/$app"
if [ ! -f "$logpath" ]; then
touch "$logpath"
chown -R "$app:$app" "/var/log/$app"
chmod -R u=rwX,g=rX,o= "/var/log/$app"
fi

ynh_systemd_action --service_name=fail2ban --action=reload --line_match="(Started|Reloaded) Fail2Ban Service" --log_path=systemd

local fail2ban_error="$(journalctl --no-hostname --unit=fail2ban | tail --lines=50 | grep "WARNING.*$app.*")"
Expand Down

0 comments on commit 9a7731a

Please sign in to comment.