generated from coop-cloud/example
feat: Add multi-tenant support (per-tenant JWT secrets) #13
@@ -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.
|
||||
|
dannygroenewegen marked this conversation as resolved
Outdated
|
||||
# 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`)
|
||||
|
||||
@@ -36,3 +36,41 @@ occ app:install eurooffice
|
||||
occ config:app:set eurooffice DocumentServerUrl --value="https://<this-recipe-DOMAIN>/"
|
||||
occ config:app:set eurooffice jwt_secret --value="<jwt secret>"
|
||||
```
|
||||
|
||||
## 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 <this-recipe-domain> app -- get_tenant_jwt <tenant-subdomain>
|
||||
```
|
||||
Pass `renew` as a second argument to rotate an existing tenant's secret.
|
||||
4. Point that Nextcloud instance at `https://<tenant-subdomain>.<TENANTS_BASE_DOMAIN>/`
|
||||
with the secret from step 3, as in "Connecting with Nextcloud" above.
|
||||
|
||||
@@ -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 <domain> app -- get_tenant_jwt <tenant-subdomain> [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"
|
||||
}
|
||||
|
||||
@@ -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}"
|
||||
@@ -28,4 +28,18 @@ file_env() {
|
||||
file_env "JWT_SECRET"
|
||||
file_env "DB_PWD"
|
||||
|
||||
# Enable multi-tenant mode (per-tenant JWT secrets,
|
||||
# tenants/<tenant>/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
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
- Added multi-tenant support (per-tenant JWT secrets). See README "Multi-tenant mode".
|
||||
Reference in New Issue
Block a user
Nitpick - I looked at https://git.coopcloud.tech/coop-cloud/nextcloud/src/branch/main/.env.sample, and it's all over the place whether to use
##,#, a space or not after the hash.