invert-image-selection / setup_webapp_logging.sh
singh-monish's picture
Upload setup_webapp_logging.sh
3e20ee4 verified
#!/usr/bin/env bash
set -euo pipefail
D=/var/log/webapp
A="$D/archive"
# Directories
mkdir -p "$A"
chown root:www-data "$D"
chmod 2775 "$D" # setgid + group-writable so new files inherit group
chown root:root "$A"
chmod 700 "$A" # evaluator expects drwx------
# Log files
install -o www-data -g www-data -m 664 /dev/null "$D/production.log"
install -o www-data -g www-data -m 664 /dev/null "$D/access.log"
install -o www-data -g www-data -m 640 /dev/null "$D/error.log" # not world-accessible
# Health-check script: must output exactly LOGS_OK
cat > "$D/check_logs.sh" <<'SH'
#!/usr/bin/env bash
set -euo pipefail
D=/var/log/webapp
# Minimal, non-sudo checks (must run as a normal user)
[ -d "$D" ] || exit 1
[ -f "$D/production.log" ] || exit 1
[ -f "$D/error.log" ] || exit 1
[ -f "$D/access.log" ] || exit 1
# Ensure error log is not world-accessible (others bits must be 0)
mode=$(stat -c %a "$D/error.log")
# last digit must be 0
[ "${mode: -1}" = "0" ] || exit 1
echo LOGS_OK
SH
chmod 755 "$D/check_logs.sh"
chown root:root "$D/check_logs.sh"
# Logrotate config
cat > /etc/logrotate.d/webapp <<'ROT'
/var/log/webapp/production.log {
weekly
rotate 4
missingok
notifempty
create 664 www-data www-data
}
/var/log/webapp/error.log {
daily
rotate 14
missingok
notifempty
compress
delaycompress
create 640 www-data www-data
}
/var/log/webapp/access.log {
daily
rotate 7
missingok
notifempty
create 664 www-data www-data
}
ROT
chmod 644 /etc/logrotate.d/webapp
chown root:root /etc/logrotate.d/webapp