aboutsummaryrefslogtreecommitdiff
path: root/scripts/hostclass/bitwarden_server
blob: f300b0d2b9f2519819fe72f756f76983ee2af691 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/bin/sh

: ${vaultwarden_username:='s-vaultwarden'}
: ${vaultwarden_dbname:='vaultwarden'}
: ${vaultwarden_dbhost:="$postgres_host"}
: ${vaultwarden_fqdn:="$fqdn"}

vaultwarden_dn="uid=${vaultwarden_username},${robots_basedn}"
vaultwarden_local_username=$nginx_user
vaultwarden_https_cert="${nginx_conf_dir}/vaultwarden.crt"
vaultwarden_https_key="${nginx_conf_dir}/vaultwarden.key"
vaultwarden_home=/usr/local/www/vaultwarden
vaultwarden_port=8080
vaultwarden_client_keytab="${keytab_dir}/vaultwarden.client.keytab"

pkg install -y \
  vaultwarden \
  ca_root_nss \
  nginx

# Create vaultwarden principal and keytab.
ldap_add "$vaultwarden_dn" <<EOF
objectClass: account
uid: ${vaultwarden_username}
EOF
add_principal -nokey -x "dn=${vaultwarden_dn}" "$vaultwarden_username"

ktadd -k "$vaultwarden_client_keytab" "$vaultwarden_username"
chgrp "$vaultwarden_local_username" "$vaultwarden_client_keytab"
chmod 640 "$vaultwarden_client_keytab"

vaultwarden_uid=$(id -u "$vaultwarden_local_username")
install_directory -o "$vaultwarden_local_username" -m 0700 "/var/krb5/user/${vaultwarden_uid}"
ln -snfv "$vaultwarden_client_keytab" "/var/krb5/user/${vaultwarden_uid}/client.keytab"

# Create postgres user and database.
postgres_create_role "$vaultwarden_dbhost" "$vaultwarden_username"
postgres_create_database "$vaultwarden_dbhost" "$vaultwarden_dbname" "$vaultwarden_username"

# Generate vaultwarden configuration.
install_template -m 0644 /usr/local/etc/rc.conf.d/vaultwarden

# Copy TLS certificate for nginx.
install_certificate     nginx "$vaultwarden_https_cert"
install_certificate_key nginx "$vaultwarden_https_key"

# Generate nginx configuration.
install_template -m 0644 \
  /usr/local/etc/nginx/nginx.conf \
  /usr/local/etc/nginx/vhosts.conf

# Enable and start daemons.
sysrc -v \
  vaultwarden_enable=YES \
  vaultwarden_user="$vaultwarden_local_username" \
  vaultwarden_group="$vaultwarden_local_username" \
  nginx_enable=YES \

service nginx restart

# The vaultwarden rc script seems to hold onto open descriptors, which causes
# the parent boxconf SSH process to never close.
echo 'Restarting vaultwarden.'
service vaultwarden restart > /dev/null 2>&1 < /dev/null