Files

98 lines
2.8 KiB
Bash

#!/bin/bash
# Enable a hashed file audit device on this Debian OpenBao host via
# declarative config + SIGHUP (no restart, no unseal).
# Run as root on the OpenBao server:
# sudo bash enable-audit.sh
set -euo pipefail
if [[ ${EUID} -ne 0 ]]; then
echo "Run as root: sudo bash $0" >&2
exit 1
fi
UNIT=""
for candidate in openbao bao; do
if systemctl list-unit-files "${candidate}.service" &>/dev/null; then
UNIT="${candidate}"
break
fi
done
if [[ -z "${UNIT}" ]]; then
echo "Could not find openbao or bao systemd unit." >&2
exit 1
fi
USER_NAME="$(systemctl show -p User --value "${UNIT}")"
USER_NAME="${USER_NAME:-openbao}"
CONFIG=""
for candidate in /etc/openbao/openbao.hcl /etc/openbao/config.hcl /etc/bao/openbao.hcl /etc/bao/bao.hcl; do
if [[ -f "${candidate}" ]]; then
CONFIG="${candidate}"
break
fi
done
if [[ -z "${CONFIG}" ]]; then
echo "Could not find OpenBao HCL config under /etc/openbao or /etc/bao." >&2
exit 1
fi
LOG_DIR="/var/lib/openbao"
LOG_FILE="${LOG_DIR}/audit.log"
if [[ ! -d "${LOG_DIR}" ]]; then
echo "Expected data directory ${LOG_DIR} is missing." >&2
exit 1
fi
if [[ ! -f "${LOG_FILE}" ]]; then
touch "${LOG_FILE}"
fi
chown "${USER_NAME}:${USER_NAME}" "${LOG_FILE}"
chmod 640 "${LOG_FILE}"
MARKER='audit "file" "file"'
if grep -qF "${MARKER}" "${CONFIG}"; then
echo "Audit stanza already present in ${CONFIG}"
else
cp -a "${CONFIG}" "${CONFIG}.bak.$(date +%Y%m%d-%H%M%S)"
cat >> "${CONFIG}" <<'HCL'
# Hashed audit log (added by hardening). Values are HMAC'd, not raw.
# Applied on SIGHUP / systemctl reload — no unseal required.
audit "file" "file" {
description = "Hashed audit log on local disk"
file_path = "/var/lib/openbao/audit.log"
mode = "0640"
}
HCL
echo "Appended audit stanza to ${CONFIG} (backup saved beside it)."
fi
if systemctl reload "${UNIT}"; then
echo "Reloaded ${UNIT}.service via SIGHUP."
else
echo "systemctl reload failed; sending SIGHUP to MainPID."
PID="$(systemctl show -p MainPID --value "${UNIT}")"
if [[ -z "${PID}" || "${PID}" == "0" ]]; then
echo "No running PID for ${UNIT}." >&2
exit 1
fi
kill -HUP "${PID}"
echo "Sent SIGHUP to PID ${PID}."
fi
sleep 1
if [[ -s "${LOG_FILE}" ]]; then
echo "Audit log has data ($(wc -c < "${LOG_FILE}") bytes)."
echo "Inspect the latest line with: sudo tail -n 1 ${LOG_FILE}"
echo "Values should appear as hmac-sha256:... not in plaintext."
else
echo "Audit log exists but is still empty. Make an API request (e.g. bao status or a UI login), then:"
echo " sudo tail -n 2 ${LOG_FILE}"
echo "Values should appear as hmac-sha256:... not in plaintext."
fi
echo "To confirm the device is enabled (needs a token):"
echo " bao audit list"
echo "Undo: remove the audit stanza from ${CONFIG} and run: sudo systemctl reload ${UNIT}"