Self-Hosted Sign-In With Kanidm in Docker
Run Kanidm in Docker behind a Cloudflare Tunnel with passkey sign-in, then connect a web app over OAuth2. The exact setup that signs people in to toneprobe.
Kanidm is a self-hosted identity server: one Rust binary with its own database, passkeys and two-step codes built in, and OAuth2/OpenID Connect so your apps can hand sign-in to it. In Docker behind a Cloudflare Tunnel, the whole setup is a short server.toml, a self-signed certificate Kanidm makes for itself, a container bound to 127.0.0.1, and a tunnel route that connects over HTTPS. This is the setup behind the sign-in on toneprobe, at id.toneprobe.net.
Why Kanidm for a small homelab
I wanted passkeys, a proper OAuth2 server, and something that sits comfortably next to the site on a VM. Keycloak and Authentik both do the job, but each brings an external database to run and back up, and Keycloak brings Java. Kanidm keeps its data in a single file, can take its own nightly backups of it, and treats passkeys as the normal way to sign in rather than an add-on.
The trade-off is that Kanidm is opinionated. It insists on HTTPS everywhere, including between your proxy and Kanidm, and some settings are deliberately hard to change later. Both points shape the steps below.
Decide the domain before anything else
Kanidm also needs a hostname of its own, served from the root (/), with no other service on it. origin must be the same name as domain or a subdomain of it, or the server refuses to start.
Write server.toml
Make a data folder on the host and put this in /srv/kanidm/data/server.toml, with your own domain:
version = "2"
# Only cloudflared, on this machine, talks to Kanidm.
bindaddress = "127.0.0.1:8443"
db_path = "/data/kanidm.db"
# Self-signed certificate made by kanidmd (next step). Cloudflare serves the public one.
tls_chain = "/data/chain.pem"
tls_key = "/data/key.pem"
# Decide once: passkeys are tied to these.
domain = "id.example.com"
origin = "https://id.example.com"
# Believe the visitor address that cloudflared passes on, and only from cloudflared.
[http_client_address_info]
x-forward-for = ["127.0.0.1"]
# A nightly copy of the database, keeping a week.
[online_backup]
path = "/data/backups/"
schedule = "00 22 * * *"
versions = 7
Binding to 127.0.0.1 means nothing on your network can reach Kanidm directly. The only way in is through the tunnel. Before starting it, kanidmd configtest checks the file: the version = "2" format refuses keys it doesn’t know, including the older trust_x_forward_for.
Generate the certificate and start the container
Kanidm won’t start without TLS, even behind a proxy: its documentation insists the whole path between client and server is encrypted, proxy hop included. It can make its own certificate authority and server certificate:
sudo mkdir -p /srv/kanidm/data/backups
docker run --rm -i -t -v /srv/kanidm/data:/data \
docker.io/kanidm/server:1.11.2 kanidmd cert-generate
Then a docker-compose.yml:
services:
kanidm:
image: docker.io/kanidm/server:1.11.2 # pin it; upgrade on purpose
container_name: kanidmd
restart: unless-stopped
network_mode: host # listen on the host's 127.0.0.1
volumes:
- /srv/kanidm/data:/data
Host networking lets Kanidm listen on the host’s loopback address and see cloudflared as 127.0.0.1, which is what the x-forward-for setting trusts. Start it with docker compose up -d.
Recover the two admin accounts
Kanidm has two built-in administrators, and you set both passwords from the server:
docker exec -i -t kanidmd kanidmd recover-account admin
docker exec -i -t kanidmd kanidmd recover-account idm_admin
adminmanages Kanidm’s own configuration, such as the domain’s display name.idm_adminmanages people, groups and OAuth2 clients, which is most of the work below.
Each command prints a password. Put both in your password manager. Kanidm treats them as break-glass accounts rather than everyday logins, so once things are running, give your own person account the rights you need and keep these two for emergencies.
Route the tunnel to Kanidm over HTTPS
In the Cloudflare dashboard, add a published application route on your tunnel: subdomain id, your domain, and service https://127.0.0.1:8443. Then open the route’s additional settings:
- Origin Server Name:
id.example.com, the name in Kanidm’s certificate. - Disable TLS certificate verification: on.
If you manage cloudflared from a config file instead, the same thing looks like this:
ingress:
- hostname: id.example.com
service: https://127.0.0.1:8443
originRequest:
originServerName: id.example.com
noTLSVerify: true
- service: http_status:404
Turning verification off is the practical choice here, and safe enough because this hop never leaves the machine. The certificate cert-generate makes is labelled “NOT FOR PRODUCTION”, the server certificate expires after 5 days and its authority after 30, and nothing renews them. Kanidm carries on serving the expired certificate, and cloudflared accepts it with verification off. Pointing cloudflared’s caPool at Kanidm’s ca.pem instead looks stricter, but it stops working within days. If you want verification on, issue Kanidm a longer-lived certificate from your own authority and use caPool with that. The zone’s SSL/TLS mode doesn’t affect the tunnel’s connection to Kanidm, so leave it as it is, including Full (strict).
Open https://id.example.com. A 502 Bad Gateway means cloudflared can’t complete TLS to Kanidm, almost always because the origin settings above are missing. The Kanidm sign-in page means it works.
Run the admin tool without installing it
Kanidm’s command-line tool ships in its own container, so nothing needs installing on the host. Save this as kanidm.sh:
#!/bin/sh
# The kanidm CLI from Kanidm's tools container. Usage: sh kanidm.sh login -D idm_admin
set -eu
CONF="$HOME/.config/kanidm-cli"
mkdir -p "$CONF"
[ -f "$CONF/config" ] || printf 'uri = "https://id.example.com"\n' > "$CONF/config"
[ -s "$CONF/tokens" ] || { echo '{"instances":{}}' > "$CONF/tokens"; chmod 600 "$CONF/tokens"; }
exec docker run --rm -i -t \
--mount "type=bind,src=$CONF/config,target=/root/.config/kanidm,readonly" \
--mount "type=bind,src=$CONF/tokens,target=/root/.cache/kanidm_tokens" \
--mount "type=bind,src=$PWD,target=/work,readonly" -w /work \
docker.io/kanidm/tools:1.11.2 /sbin/kanidm "$@"
Add alias kanidm='sh ~/kanidm.sh' to your shell. It talks to Kanidm through its public address on purpose: invite links are built from that address, and passkeys only register on the exact public origin.
Connect a web app over OAuth2
Create a group for the people allowed in, and an OAuth2 client for the app:
kanidm login -D idm_admin
kanidm group create myapp_users -D idm_admin
kanidm system oauth2 create myapp "My App" https://app.example.com -D idm_admin
kanidm system oauth2 add-redirect-url myapp https://app.example.com/auth/callback -D idm_admin
kanidm system oauth2 update-scope-map myapp myapp_users openid profile groups_name -D idm_admin
kanidm system oauth2 prefer-short-username myapp -D idm_admin
kanidm system oauth2 show-basic-secret myapp -D idm_admin
The scope map is also the access list: someone outside myapp_users can’t sign in to the app at all, even with a valid Kanidm account. groups_name puts the user’s group names in the token, so the app can tell an admin from everyone else without its own user table.
Give the app the client ID (myapp), the secret from show-basic-secret, and the discovery address https://id.example.com/oauth2/openid/myapp/.well-known/openid-configuration. Kanidm requires PKCE (S256) for every client by default and signs tokens with ES256, both of which any current OpenID Connect library handles for you.
To make this app’s users passkey-only, raise the group’s credential policy:
kanidm group account-policy enable myapp_users -D idm_admin
kanidm group account-policy credential-type-minimum myapp_users passkey -D idm_admin
Members can then only add passkeys, not passwords, and where a person is in several groups the strictest policy wins. In Kanidm 1.11 the policy applies when credentials are added, not at sign-in, so remove any password a member already has.
Add people with an invite link
kanidm person create jane "Jane Smith" -D idm_admin
kanidm group add-members myapp_users jane -D idm_admin
kanidm person credential create-reset-token jane --ttl 86400 -D idm_admin
The last command prints a link valid for a day (86 400 seconds, the longest allowed; the default is an hour). Jane opens it and registers a passkey, or sets a password with a two-step code if her groups allow passwords. You never handle her credentials.
Backups and upgrades
- Backups: the
[online_backup]section writes a consistent copy to/srv/kanidm/data/backupsevery night and keeps seven. Copy that folder off the machine with the rest of your homelab backups. - Upgrades: keep the image version pinned, read the release notes, and take a backup first. Kanidm must be upgraded through each version in sequence, so going from 1.9 to 1.11 means 1.9 to 1.10 first.
kanidmd domain upgrade-checkreports anything that would block the next upgrade. Change the server and tools images together. - Visitor addresses: Kanidm logs the first address in the
X-Forwarded-Forheader, and Cloudflare adds to any such header a visitor sends rather than replacing it. Treat the address in Kanidm’s logs as a hint, not proof. - Themes: the sign-in pages can be restyled by mounting your own stylesheet into the container, which is how
id.toneprobe.netmatches the site.