Support postgres_pooler and add rendered example

This commit is contained in:
Daniel Berteaud 2024-01-12 22:42:09 +01:00
parent 46e0010e5f
commit 919de1f6fa
11 changed files with 353 additions and 16 deletions

9
example/LICENSE Normal file
View File

@ -0,0 +1,9 @@
MIT License
Copyright (c) 2023 nomad
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

2
example/README.md Normal file
View File

@ -0,0 +1,2 @@
# vaultwarden

View File

@ -0,0 +1,3 @@
Kind = "service-defaults"
Name = "vaultwarden"
Protocol = "http"

View File

@ -0,0 +1,15 @@
Kind = "service-intentions"
Name = "vaultwarden"
Sources = [
{
Name = "traefik"
Permissions = [
{
Action = "allow"
HTTP {
Methods = ["GET", "HEAD", "POST", "PUT", "DELETE"]
}
}
]
}
]

View File

@ -0,0 +1,68 @@
FROM rust:alpine AS build
ARG VAULTWARDEN_FEATURES=postgresql \
VAULTWARDEN_SERVER_VERSION=1.30.0 \
VAULTWARDEN_WEB_VERSION=2023.10.0
RUN set -euxo pipefail &&\
apk --no-cache upgrade &&\
apk --no-cache add \
curl \
ca-certificates \
tar \
musl-dev \
openssl-libs-static \
build-base \
postgresql15-dev \
libpq-dev \
&&\
cd /tmp &&\
curl -sSLO https://github.com/dani-garcia/vaultwarden/archive/refs/tags/${VAULTWARDEN_SERVER_VERSION}.tar.gz &&\
tar xvzf ${VAULTWARDEN_SERVER_VERSION}.tar.gz &&\
cd vaultwarden-${VAULTWARDEN_SERVER_VERSION} &&\
rustup set profile minimal &&\
rustup target add x86_64-unknown-linux-musl &&\
cargo build --features=${VAULTWARDEN_FEATURES} --profile "release" --target "x86_64-unknown-linux-musl" &&\
find ./target -type f -name vaultwarden &&\
# Move vaultwarden bin to copy it easily in the runtime stage \
mv ./target/x86_64-unknown-linux-musl/release/vaultwarden / &&\
chown root:root /vaultwarden &&\
chmod 755 /vaultwarden &&\
cd ../ &&\
curl -sSLO https://github.com/dani-garcia/bw_web_builds/releases/download/v${VAULTWARDEN_WEB_VERSION}/bw_web_v${VAULTWARDEN_WEB_VERSION}.tar.gz &&\
tar xvzf bw_web_v${VAULTWARDEN_WEB_VERSION}.tar.gz &&\
mv web-vault / &&\
chown -R root:root /web-vault
FROM danielberteaud/alpine:24.1-2
MAINTAINER Daniel Berteaud <dbd@ehtrace.com>
ENV ROCKET_PROFILE=release \
ROCKET_ADDRESS=0.0.0.0 \
ROCKET_PORT=8234 \
DATA_FOLDER=/data \
DATABASE_URL=/data/db.sqlite3
COPY --from=build /vaultwarden /usr/local/bin/
COPY --from=build /web-vault /opt/vaultwarden/web-vault
RUN set -euxo pipefail &&\
apk --no-cache upgrade &&\
apk --no-cache add \
ca-certificates \
curl \
openssl \
tzdata \
&&\
addgroup -g 8234 vaultwarden &&\
adduser --system --ingroup vaultwarden --disabled-password --uid 8234 --home /opt/vaultwarden --shell /sbin/nologin vaultwarden &&\
mkdir /data &&\
chown vaultwarden:vaultwarden /data
WORKDIR /opt/vaultwarden
USER vaultwarden
EXPOSE 8234
CMD ["vaultwarden"]

12
example/init/vault-vaultwarden Executable file
View File

@ -0,0 +1,12 @@
#!/bin/sh
set -euo pipefail
vault write database/roles/vaultwarden \
db_name="postgres" \
creation_statements="CREATE ROLE \"{{name}}\" WITH LOGIN PASSWORD '{{password}}' VALID UNTIL '{{expiration}}'; \
GRANT \"vaultwarden\" TO \"{{name}}\"; \
ALTER ROLE \"{{name}}\" SET role = \"vaultwarden\"" \
default_ttl="12h" \
max_ttl="720h"

19
example/prep.d/10-mv_conf.sh Executable file
View File

@ -0,0 +1,19 @@
#!/bin/sh
set -eu
if [ "vaultwarden" != "vaultwarden" ]; then
for DIR in vault consul nomad; do
if [ -d output/${DIR} ]; then
for FILE in $(find output/${DIR} -name "*vaultwarden*.hcl" -type f); do
NEW_FILE=$(echo "${FILE}" | sed -E "s/vaultwarden/vaultwarden/g")
mv "${FILE}" "${NEW_FILE}"
done
fi
done
fi

View File

@ -0,0 +1,7 @@
path "kv/data/service/vaultwarden" {
capabilities = ["read"]
}
path "database/creds/vaultwarden" {
capabilities = ["read"]
}

View File

@ -0,0 +1,183 @@
job "vaultwarden" {
datacenters = ["dc1"]
group "vaultwarden" {
count = 1
network {
mode = "bridge"
}
volume "data" {
source = "vaultwarden-data"
type = "csi"
access_mode = "multi-node-multi-writer"
attachment_mode = "file-system"
}
service {
name = "vaultwarden"
port = 8234
connect {
sidecar_service {
proxy {
upstreams {
destination_name = "smtp"
local_bind_port = 25
}
upstreams {
destination_name = "postgres"
local_bind_port = 5432
}
}
}
sidecar_task {
resources {
cpu = 50
memory = 64
}
}
}
check {
type = "http"
path = "/alive"
expose = true
interval = "5s"
timeout = "3s"
check_restart {
limit = 20
grace = "20s"
}
}
tags = [
"traefik.enable=true",
# Admin interface
"traefik.http.routers.vaultwarden-admin.rule=Host(`vaultwarden.example.org`) && PathPrefix(`/admin`)",
"traefik.http.routers.vaultwarden-admin.entrypoints=https",
"traefik.http.routers.vaultwarden-admin.priority=200",
"traefik.http.routers.vaultwarden.middlewares=rate-limit-std@file,inflight-std@file,security-headers@file,hsts@file,compression@file,csp-relaxed@file",
# Main interface
"traefik.http.routers.vaultwarden.rule=Host(`vaultwarden.example.org`)",
"traefik.http.routers.vaultwarden.entrypoints=https",
"traefik.http.routers.vaultwarden.priority=100",
"traefik.http.middlewares.vaultwarden-csp.headers.contentSecurityPolicy=default-src 'self'; img-src 'self' data: https://www.gravatar.com; script-src 'self' 'unsafe-inline' 'unsafe-eval'; style-src 'self' 'unsafe-inline'; font-src 'self' data:; connect-src 'self' https://api.pwnedpasswords.com https://api.2fa.directory",
"traefik.http.routers.vaultwarden.middlewares=vaultwarden-csp,rate-limit-std@file,inflight-std@file,security-headers@file,hsts@file,compression@file,csp-relaxed@file",
]
}
# wait for required services tp be ready before starting the main task
task "wait-for" {
driver = "docker"
user = 1053
config {
image = "danielberteaud/wait-for:24.1-1"
readonly_rootfs = true
pids_limit = 20
}
lifecycle {
hook = "prestart"
}
env {
SERVICE_0 = "master.postgres.service.consul"
}
resources {
cpu = 10
memory = 10
memory_max = 30
}
}
task "vaultwarden" {
driver = "docker"
user = 8234
config {
image = "vaultwarden/server:1.30.1-alpine"
pids_limit = 100
readonly_rootfs = true
}
vault {
policies = ["vaultwarden"]
env = false
disable_file = true
}
env {
ROCKET_ADDRESS = "127.0.0.1"
ROCKET_PORT = 8234
IP_HEADER = "X-Forwarded-for"
DOMAIN = "https://vaultwarden.example.org/"
DB_CONNECTION_RETRIES = 0
}
template {
data = <<_EOT
DATABASE_URL=postgresql://{{ with secret "database/creds/vaultwarden" }}{{ .Data.username }}{{ end }}:{{ with secret "database/creds/vaultwarden" }}{{ urlquery .Data.password }}{{ end }}@127.0.0.1:5432/vaultwarden]
_EOT
destination = "secrets/.db.env"
perms = 400
env = true
}
# Use a template block instead of env {} so we can fetch values from vault
template {
data = <<_EOT
EVENTS_DAYS_RETAIN=720
INCOMPLETE_2FA_TIME_LIMIT=5
LANG=fr_FR.utf8
ORG_EVENTS_ENABLED=true
SIGNUPS_VERIFY=true
SMTP_FROM=vaultwarden-no-reply@consul
SMTP_HOST=localhost
SMTP_PORT=25
SMTP_SECURITY=off
TRASH_AUTO_DELETE_DAYS=7
TZ=Europe/Paris
USER_ATTACHMENT_LIMIT=204800
_EOT
destination = "secrets/.env"
perms = 400
env = true
}
volume_mount {
volume = "data"
destination = "/data"
}
resources {
cpu = 300
memory = 128
}
}
}
}

View File

@ -20,6 +20,17 @@ vaultwarden:
features:
- postgresql
# List of vault policies to attach to the task
vault:
policies:
- '[[ .instance ]][[ .consul.suffix ]]'
# Postgres settings
postgres:
database: '[[ .instance ]]'
user: '{{ with secret "[[ .vault.prefix ]]database/creds/[[ .instance ]]" }}{{ .Data.username }}{{ end }}'
password: '{{ with secret "[[ .vault.prefix ]]database/creds/[[ .instance ]]" }}{{ .Data.password }}{{ end }}'
# Resources allocation
resources:
cpu: 300
@ -29,8 +40,12 @@ vaultwarden:
consul:
connect:
upstreams:
- service_name: '[[ .mail.smtp_service_name ]]'
- destination_name: '[[ .mail.smtp_service_name ]]'
local_bind_port: 25
- destination_name: postgres[[ .consul.suffix ]]
local_bind_port: 5432
wait_for:
- service: master.postgres[[ .consul.suffix ]]
# Environment variables to set in the container
env:
@ -62,3 +77,4 @@ vaultwarden:
data:
type: csi
source: vaultwarden-data
access_mode: multi-node-multi-writer

View File

@ -1,4 +1,4 @@
[[ $c := merge .vaultwarden.server . -]]
[[ $c := merge .vaultwarden.server .vaultwarden . -]]
job "[[ .instance ]]" {
[[ template "common/job_start" $c ]]
@ -10,14 +10,7 @@ job "[[ .instance ]]" {
mode = "bridge"
}
volume "data" {
type = [[ .vaultwarden.volumes.data.type | toJSON ]]
source = [[ .vaultwarden.volumes.data.source | toJSON ]]
[[- if ne .vaultwarden.volumes.data.type "host" ]]
attachment_mode = "file-system"
access_mode = "multi-node-multi-writer"
[[- end ]]
}
[[ template "common/volumes" $c.volumes ]]
service {
name = "[[ .instance ]][[ .consul.suffix ]]"
@ -39,7 +32,7 @@ job "[[ .instance ]]" {
}
tags = [
[[- $a := merge .vaultwarden.admin . ]]
[[- $a := merge .vaultwarden.admin .vaultwarden . ]]
"[[ $c.traefik.instance ]].enable=true",
[[- if ne $c.traefik.instance $a.traefik.instance ]]
"[[ $a.traefik.instance ]].enable=true",
@ -66,6 +59,7 @@ job "[[ .instance ]]" {
}
[[ template "common/task.wait_for" $c ]]
[[ template "common/postgres_pooler" $c ]]
task "vaultwarden" {
driver = [[ $c.nomad.driver | toJSON ]]
@ -77,11 +71,7 @@ job "[[ .instance ]]" {
readonly_rootfs = true
}
vault {
policies = ["[[ .instance ]][[ .consul.suffix ]]"]
env = false
disable_file = true
}
[[ template "common/vault.policies" $c ]]
env {
ROCKET_ADDRESS = "127.0.0.1"
@ -92,6 +82,19 @@ job "[[ .instance ]]" {
[[ template "common/proxy_env" $c ]]
}
template {
data =<<_EOT
[[- if ne $c.postgres.pooler.engine "none" ]]
DATABASE_URL=postgresql://[[ .instance ]]:{{ env "NOMAD_ALLOC_ID" }}@localhost:6432/[[ $c.postgres.database ]]
[[- else ]]
DATABASE_URL=postgresql://[[ $c.postgres.user ]]:[[ $c.postgres.password | regexp.Replace "\\.Data\\.password" "urlquery .Data.password" ]]@[[ $c.postgres.host ]]:[[ $c.postgres.port ]]/[[ $c.postgres.database ]]]
[[- end ]]
_EOT
destination = "secrets/.db.env"
perms = 400
env = true
}
[[ template "common/file_env" $c.env ]]
volume_mount {