diff --git a/.env.sample b/.env.sample index 0f3e898..7b65d9f 100644 --- a/.env.sample +++ b/.env.sample @@ -6,3 +6,10 @@ COMPOSE_FILE="compose.yml" SECRET_JWT_VERSION=v1 # length=32 SECRET_DB_PASSWORD_VERSION=v1 + +## Multi-tenant mode: per-tenant JWT secrets. +# See README "Multi-tenant mode" +#COMPOSE_FILE="$COMPOSE_FILE:compose.tenants.yml" +#TENANTS_BASE_DOMAIN=euro-office.example.com +# Must be a valid Traefik rule expression for subdomains per tenant +#TENANTS_TRAEFIK_RULE=Host(`a.euro-office.example.com`) || Host(`b.euro-office.example.com`) diff --git a/README.md b/README.md index 56c6abb..7aecefa 100644 --- a/README.md +++ b/README.md @@ -36,3 +36,41 @@ occ app:install eurooffice occ config:app:set eurooffice DocumentServerUrl --value="https:///" occ config:app:set eurooffice jwt_secret --value="" ``` + +## Multi-tenant mode + +The single `jwt` Docker secret above should not be shared across more than +one Nextcloud instance for security reasons. Enabling `compose.tenants.yml` +turns on multi-tenant mode instead, giving each Nextcloud instance its own +secret: + +``` +COMPOSE_FILE="$COMPOSE_FILE:compose.tenants.yml" +TENANTS_BASE_DOMAIN=euro-office.example.com +TENANTS_TRAEFIK_RULE=Host(`a.euro-office.example.com`) || Host(`b.euro-office.example.com`) +``` + +A tenant is identified purely by the Host header of the request reaching this +server, so any instance still pointed at the bare `DOMAIN` keeps using +the `jwt` secret. Only instances pointed at their own subdomain/slug get +their own jwt. + +`TENANTS_BASE_DOMAIN` can be the same as `DOMAIN`, but doesn't have to. +`TENANTS_TRAEFIK_RULE` must be valid Traefik rule syntax. With the default +HTTP-01 certresolver, each tenant subdomain needs its own explicit +`Host(...)` entry in the rule, OR'd together, since HTTP-01 can't issue a +certificate for a regex/wildcard host. If a DNS-01 challenge is set up for +this domain instead, a single wildcard rule can be used: +`` Host(`*.euro-office.example.com`) ``. + +To onboard a tenant: + +1. Point a DNS record at this server for the new subdomain. +2. Add its `Host(...)` clause to `TENANTS_TRAEFIK_RULE` and redeploy. +3. Generate (or fetch, idempotently) its secret: + ``` + abra app cmd -T app -- get_tenant_jwt + ``` + Pass `renew` as a second argument to rotate an existing tenant's secret. +4. Point that Nextcloud instance at `https://./` + with the secret from step 3, as in "Connecting with Nextcloud" above. diff --git a/abra.sh b/abra.sh index e6c130b..248eb0c 100755 --- a/abra.sh +++ b/abra.sh @@ -1 +1,25 @@ -export ENTRYPOINT_VERSION=v1 +export ENTRYPOINT_VERSION=v2 + +# Prints a tenant's JWT secret, generating one on first call. +# Needs compose.tenants.yml enabled in .env. +# +# Usage: +# abra app cmd app -- get_tenant_jwt [renew] +get_tenant_jwt() { + local subdomain="$1" + local renew="${2:-}" + local tenant_dir="/var/www/euro-office/Data/tenants/${subdomain}" + local secret_file="${tenant_dir}/secret.key" + + if [ "$renew" = "renew" ]; then + rm -f "$secret_file" + fi + + mkdir -p "$tenant_dir" + chmod 0750 "$tenant_dir" + if [ ! -s "$secret_file" ]; then + printf '%s' "$(openssl rand -hex 32)" > "$secret_file" + chmod 0440 "$secret_file" + fi + cat "$secret_file" +} diff --git a/compose.tenants.yml b/compose.tenants.yml new file mode 100644 index 0000000..7acbec4 --- /dev/null +++ b/compose.tenants.yml @@ -0,0 +1,13 @@ +--- +version: "3.8" + +services: + app: + environment: + - TENANTS_BASE_DOMAIN + deploy: + labels: + - "traefik.http.routers.${STACK_NAME}-tenants.rule=${TENANTS_TRAEFIK_RULE}" + - "traefik.http.routers.${STACK_NAME}-tenants.entrypoints=web-secure" + - "traefik.http.routers.${STACK_NAME}-tenants.tls.certresolver=${LETS_ENCRYPT_ENV}" + - "traefik.http.routers.${STACK_NAME}-tenants.service=${STACK_NAME}" diff --git a/entrypoint.sh.tmpl b/entrypoint.sh.tmpl index c88c223..b541360 100644 --- a/entrypoint.sh.tmpl +++ b/entrypoint.sh.tmpl @@ -28,4 +28,18 @@ file_env() { file_env "JWT_SECRET" file_env "DB_PWD" +# Enable multi-tenant mode (per-tenant JWT secrets, +# tenants//secret.key) by merging a 'tenants' key into local.json +# before the upstream entrypoint does its own update on that same file. +TENANTS_BASE_DOMAIN="{{ env "TENANTS_BASE_DOMAIN" }}" + +if [ -n "$TENANTS_BASE_DOMAIN" ]; then + LOCAL_JSON=/etc/euro-office/documentserver/local.json + mkdir -p "$(dirname "$LOCAL_JSON")" + [ -s "$LOCAL_JSON" ] || echo '{}' > "$LOCAL_JSON" + jq --arg baseDomain "$TENANTS_BASE_DOMAIN" \ + '.tenants = {baseDir: "/var/www/euro-office/Data/tenants", baseDomain: $baseDomain, defaultTenant: "default"}' \ + "$LOCAL_JSON" > "${LOCAL_JSON}.tmp" && mv "${LOCAL_JSON}.tmp" "$LOCAL_JSON" +fi + exec /entrypoint.sh diff --git a/releases/next b/releases/next new file mode 100644 index 0000000..d54d7bb --- /dev/null +++ b/releases/next @@ -0,0 +1 @@ +- Added multi-tenant support (per-tenant JWT secrets). See README "Multi-tenant mode".