You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

247 lines
7.6 KiB
Markdown

4 years ago
# Bitwarden_rs in docker
4 years ago
###### guide-by-example
4 years ago
4 years ago
![logo](https://i.imgur.com/tT3FQLJ.png)
4 years ago
4 years ago
# Purpose & Overview
4 years ago
4 years ago
Password manager.
4 years ago
* [Official site](https://bitwarden.com/)
* [Github](https://github.com/dani-garcia/bitwarden_rs)
4 years ago
* [DockerHub](https://hub.docker.com/r/bitwardenrs/server)
4 years ago
4 years ago
Bitwarden is a modern popular open source password manager
with wide cross platform support.
4 years ago
But the official Bitwarden server is bit over-engineered,
requiring Microsoft SQL server among other things,
which makes it not an ideal fit for smaller deployments
4 years ago
So here is where Bitwarden_rs by Daniel García comes in.</br>
It is a Bitwarden API implementation written in Rust.
It's very resource efficient, uses about 10MB of RAM,
and close to no CPU.</br>
4 years ago
Webapp part is build using Rocket, a web framework for Rust,
4 years ago
and user data are stored in a simple sqlite database file.
All the client apps are still officials coming from bitwarden,
only the server is a different implementation.
4 years ago
# Files and directory structure
4 years ago
4 years ago
```
4 years ago
/home/
└── ~/
└── docker/
└── bitwarden/
├── bitwarden-data/
├── .env
├── docker-compose.yml
└── bitwarden-backup-script.sh
4 years ago
```
4 years ago
4 years ago
* `bitwarden-data/` - a directory where bitwarden will store its database and other data
4 years ago
* `.env` - a file containing environment variables for docker compose
4 years ago
* `docker-compose.yml` - a docker compose file, telling docker how to run the container
4 years ago
* `bitwarden-backup-script.sh` - a backup script if you want it
You only need to provide the files.</br>
The directory is created by docker compose on the first run.
4 years ago
# docker-compose
4 years ago
4 years ago
[Documentation](https://github.com/dani-garcia/bitwarden_rs/wiki/Using-Docker-Compose) on compose.
`docker-compose.yml`
```yml
version: "3"
services:
bitwarden:
image: bitwardenrs/server
container_name: bitwarden
hostname: bitwarden
restart: unless-stopped
env_file: .env
volumes:
- ./bitwarden-data/:/data/
networks:
default:
external:
4 years ago
name: $DOCKER_MY_NETWORK
4 years ago
```
`.env`
4 years ago
```bash
4 years ago
# GENERAL
4 years ago
MY_DOMAIN=example.com
4 years ago
DOCKER_MY_NETWORK=caddy_net
4 years ago
TZ=Europe/Bratislava
4 years ago
# BITWARDEN
ADMIN_TOKEN=YdLo1TM4MYEQ948GOVZ29IF4fABSrZMpk9
SIGNUPS_ALLOWED=false
WEBSOCKET_ENABLED=true
# USING SENDGRID FOR SENDING EMAILS
4 years ago
DOMAIN=https://passwd.example.com
4 years ago
SMTP_SSL=true
SMTP_EXPLICIT_TLS=true
SMTP_HOST=smtp.sendgrid.net
SMTP_PORT=465
4 years ago
SMTP_FROM=admin@example.com
4 years ago
SMTP_USERNAME=apikey
4 years ago
SMTP_PASSWORD=<sendgrid-api-key-goes-here>
4 years ago
```
**All containers must be on the same network**.</br>
4 years ago
Which is named in the `.env` file.</br>
4 years ago
If one does not exist yet: `docker network create caddy_net`
4 years ago
4 years ago
# Reverse proxy
4 years ago
4 years ago
Caddy v2 is used, details
[here](https://github.com/DoTheEvo/selfhosted-apps-docker/tree/master/caddy_v2).</br>
Bitwarden_rs documentation has a
[section on reverse proxy.](https://github.com/dani-garcia/bitwarden_rs/wiki/Proxy-examples)
4 years ago
`Caddyfile`
```
4 years ago
bitwarden.{$MY_DOMAIN} {
4 years ago
encode gzip
4 years ago
header {
# Enable cross-site filter (XSS) and tell browser to block detected attacks
X-XSS-Protection "1; mode=block"
# Disallow the site to be rendered within a frame (clickjacking protection)
X-Frame-Options "DENY"
# Prevent search engines from indexing (optional)
X-Robots-Tag "none"
# Server name removing
-Server
}
# Notifications redirected to the websockets server
4 years ago
reverse_proxy /notifications/hub bitwarden:3012
4 years ago
# Proxy the Root directory to Rocket
4 years ago
reverse_proxy bitwarden:80
}
```
4 years ago
4 years ago
# Forward port 3012 TCP on your router
4 years ago
4 years ago
[WebSocket](https://youtu.be/2Nt-ZrNP22A) protocol is used for notifications
so that all web based clients, including desktop app,
4 years ago
can immediately sync when a change happens on the server.
4 years ago
4 years ago
* environment variable `WEBSOCKET_ENABLED=true` needs to be set in the `.env` file</br>
4 years ago
* reverse proxy needs to route `/notifications/hub` to port 3012</br>
* your router/firewall needs to **forward port 3012** to the docker host,
same as port 80 and 443 are forwarded
4 years ago
To test if websocket works, have the desktop app open
and make changes through browser extension, or through the website.
4 years ago
Changes should immediately appear in the desktop app. If it's not working,
4 years ago
you need to manually sync for changes to appear.
4 years ago
# Extra info
4 years ago
4 years ago
**Bitwarden can be managed** at `<url>/admin` and entering `ADMIN_TOKEN`
4 years ago
set in the `.env` file. Especially if sign ups are disabled it is the only way
4 years ago
to invite users.
4 years ago
4 years ago
**Push notifications** are not working at this moment.
4 years ago
[Github issue](https://github.com/dani-garcia/bitwarden_rs/issues/126).</br>
The purpose of [Push notifications](https://www.youtube.com/watch?v=8D1NAezC-Dk)
4 years ago
is the same as WebSocket notifications, to tell the clients that a change
4 years ago
happened on the server so that they are synced immediately.
4 years ago
But they are for apps on mobile devices and it would likely take releasing and
4 years ago
maintaining own bitwarden_rs version of the Android/iOS mobile apps
4 years ago
to have them working.</br>
4 years ago
So you better manually sync before making changes.
4 years ago
4 years ago
---
4 years ago
![interface-pic](https://i.imgur.com/5LxEUsA.png)
4 years ago
# Update
4 years ago
4 years ago
[Watchtower](https://github.com/DoTheEvo/selfhosted-apps-docker/tree/master/watchtower)
updates the image automatically.
4 years ago
4 years ago
Manual image update:
- `docker-compose pull`</br>
- `docker-compose up -d`</br>
- `docker image prune`
4 years ago
4 years ago
# Backup and restore
4 years ago
4 years ago
#### Backup
Using [borg](https://github.com/DoTheEvo/selfhosted-apps-docker/tree/master/borg_backup)
that makes daily snapshot of the entire directory.
#### Restore
* down the bitwarden container `docker-compose down`</br>
* delete the entire bitwarden directory</br>
* from the backup copy back the bitwarden directory</br>
* start the container `docker-compose up -d`
4 years ago
4 years ago
# Backup of just user data
4 years ago
4 years ago
Users data daily export using the
[official procedure.](https://github.com/dani-garcia/bitwarden_rs/wiki/Backing-up-your-vault)</br>
4 years ago
For bitwarden_rs it means sqlite database dump and backing up `attachments` directory.</br>
4 years ago
4 years ago
Daily [borg](https://github.com/DoTheEvo/selfhosted-apps-docker/tree/master/borg_backup) run
4 years ago
takes care of backing up the directory.
4 years ago
So only database dump is needed.</br>
4 years ago
The created backup sqlite3 file is overwritten on every run of the script,
4 years ago
but that's ok since borg is making daily snapshots.
4 years ago
4 years ago
#### Create a backup script
Placed inside `bitwarden` directory on the host.
`bitwarden-backup-script.sh`
4 years ago
```bash
4 years ago
#!/bin/bash
# CREATE SQLITE BACKUP
docker container exec bitwarden sqlite3 /data/db.sqlite3 ".backup '/data/BACKUP.bitwarden.db.sqlite3'"
```
4 years ago
the script must be **executable** - `chmod +x bitwarden-backup-script.sh`
4 years ago
4 years ago
#### Cronjob
4 years ago
4 years ago
Running on the host, so that the script will be periodically run.
4 years ago
4 years ago
* `su` - switch to root
* `crontab -e` - add new cron job</br>
* `0 21 * * * /home/bastard/docker/bitwarden/bitwarden-backup-script.sh`</br>
runs it every day [at 21:00](https://crontab.guru/#0_21_*_*_*)
* `crontab -l` - list cronjobs to check
4 years ago
4 years ago
# Restore the user data
4 years ago
4 years ago
Assuming clean start.
4 years ago
4 years ago
* start the bitwarden container: `docker-compose up -d`
* let it run so it creates its file structure
* down the container `docker-compose down`
* in `bitwarden/bitwarden-data/`</br>
replace `db.sqlite3` with the backup one `BACKUP.bitwarden.db.sqlite3`</br>
4 years ago
replace `attachments` directory with the one from the borg repository
4 years ago
* start the container `docker-compose up -d`
4 years ago
4 years ago
Again, the above steps are based on the
[official procedure.](https://github.com/dani-garcia/bitwarden_rs/wiki/Backing-up-your-vault)