fix bw-vault-setup: create sudoers.d dir, add includedir, use tee instead of heredoc-in-bash-c

This commit is contained in:
Viktor Barzin 2026-03-15 15:04:59 +00:00
parent 70208da97e
commit b80dc4a069

View file

@ -73,16 +73,26 @@ fi
# Step 4: Configure scoped sudo for bw-vault # Step 4: Configure scoped sudo for bw-vault
info "Configuring scoped sudo for bw-vault..." info "Configuring scoped sudo for bw-vault..."
SUDOERS_FILE="/etc/sudoers.d/bw-vault" SUDOERS_FILE="/etc/sudoers.d/bw-vault"
# Ensure /etc/sudoers.d/ exists and is included by sudoers
if [[ ! -d /etc/sudoers.d ]]; then
info "Creating /etc/sudoers.d/ directory..."
sudo mkdir -p /etc/sudoers.d
sudo chmod 0755 /etc/sudoers.d
fi
if ! sudo grep -q '#includedir /etc/sudoers.d' /etc/sudoers; then
info "Adding #includedir directive to /etc/sudoers..."
echo '#includedir /etc/sudoers.d' | sudo tee -a /etc/sudoers >/dev/null
fi
if [[ -f "$SUDOERS_FILE" ]]; then if [[ -f "$SUDOERS_FILE" ]]; then
info "Sudoers config already exists" info "Sudoers config already exists"
else else
CURRENT_USER=$(whoami) CURRENT_USER=$(whoami)
sudo bash -c "cat > $SUDOERS_FILE << SUDOEOF # Use printf + tee (heredoc inside bash -c is fragile)
Cmnd_Alias BW_VAULT = /usr/local/bin/bw-vault-unlock printf 'Cmnd_Alias BW_VAULT = /usr/local/bin/bw-vault-unlock\nDefaults!BW_VAULT timestamp_timeout=0\n%s ALL=(root) BW_VAULT\n' "$CURRENT_USER" \
Defaults!BW_VAULT timestamp_timeout=0 | sudo tee "$SUDOERS_FILE" >/dev/null
$CURRENT_USER ALL=(root) BW_VAULT sudo chmod 0440 "$SUDOERS_FILE"
SUDOEOF
chmod 0440 $SUDOERS_FILE"
# Validate sudoers syntax # Validate sudoers syntax
if sudo visudo -cf "$SUDOERS_FILE" >/dev/null 2>&1; then if sudo visudo -cf "$SUDOERS_FILE" >/dev/null 2>&1; then
info "Sudoers config validated and installed" info "Sudoers config validated and installed"
@ -126,11 +136,10 @@ if [[ "${_store_creds:-}" == "true" ]]; then
read -rsp "Master password: " _password read -rsp "Master password: " _password
echo "" echo ""
sudo bash -c "umask 077; cat > /var/root/.bw-credentials << CREDEOF printf 'BW_CLIENTID=%s\nBW_CLIENTSECRET=%s\nBW_PASSWORD=%s\n' \
BW_CLIENTID=$_clientid "$_clientid" "$_clientsecret" "$_password" \
BW_CLIENTSECRET=$_clientsecret | sudo tee /var/root/.bw-credentials >/dev/null
BW_PASSWORD=$_password sudo chmod 0600 /var/root/.bw-credentials
CREDEOF"
unset _clientid _clientsecret _password unset _clientid _clientsecret _password
info "Credentials stored in /var/root/.bw-credentials (root:wheel 0600)" info "Credentials stored in /var/root/.bw-credentials (root:wheel 0600)"
fi fi