- Jinja 65.7%
- Python 34.3%
| Filename | Latest commit message | Latest commit date |
|---|---|---|
| .forgejo/workflows | ||
| defaults | ||
| files | ||
| meta | ||
| tasks | ||
| templates | ||
| vars | ||
| .gitignore | ||
| CHANGELOG.md | ||
| LICENSE | ||
| README.md | ||
bulwark
Ansible role that runs Bulwark, a JMAP webmail for Stalwart, as a Docker Compose service behind Traefik. Optionally users log in through an OIDC provider (e.g. Keycloak) instead of Bulwark's own login form.
Bulwark keeps no mail: everything stays on the JMAP server. The host only holds Bulwark's small state directory and the secrets.
How the SSO login works
browser ──► Traefik ──┬─ Bulwark session cookie, /api/auth/impersonate, static assets ──► Bulwark
└─ everything else ──► oauth2-proxy (IdP login) ──► handoff ──► 303 /api/auth/impersonate?token=…
- Without a Bulwark session, Traefik sends the request to oauth2-proxy, which logs the user in at the IdP. Only verified email addresses are accepted.
- The handoff service (
files/handoff.py, only reachable from oauth2-proxy) signs a single-use, 60-second HS256 token for the mailbox of that email address. - Bulwark verifies the token and logs in to Stalwart as
<mailbox>%<master_user>with the master password, then creates an 8-hour app password for the mailbox. The session runs on that app password.
The master account needs the impersonate permission on Stalwart, so whoever holds its password can open every mailbox. Limit its credential to this host's address (Stalwart's allowedIps on the password) and deny privileged mailboxes in bulwark_sso.denied_mailboxes.
Set bulwark_sso.email_domains to the domains of this IdP's customer: oauth2-proxy and the handoff both refuse other addresses. The master account itself can impersonate every mailbox on the server (per-domain limits need Stalwart Enterprise), so this list is what separates the customers. Run one instance per customer and IdP (bulwark_instance, bulwark_docker_path, bulwark_data_path, bulwark_domain differ), each with its own master account.
A Keycloak user without a Stalwart account of the same address gets an error from Bulwark ("Could not open the mailbox"). Stalwart limits app passwords per user (maxAppPasswords, default 5); every SSO login uses one for 8 hours.
Requirements
- Docker with the Compose plugin on the host.
- Traefik on the host with the external network
traefik_web(whenbulwark_traefikis true). - SSO: an OIDC client (confidential, redirect URI
https://<bulwark_domain>/oauth2/callback) and a Stalwart account with theimpersonatepermission.
Variables
| Variable | Default | Description |
|---|---|---|
bulwark_instance |
bulwark |
Compose project name and container prefix. |
bulwark_docker_path |
/docker/bulwark |
Compose file, handoff script, secrets. |
bulwark_data_path |
/data/bulwark |
Bulwark state (/app/data). |
bulwark_version |
1.18.0 |
ghcr.io/bulwarkmail/webmail tag. |
bulwark_domain |
~ |
Required. Public domain. |
bulwark_jmap_url |
~ |
Required. JMAP server URL, e.g. https://mail.example.org. |
bulwark_app_name |
Webmail |
Name in the browser tab and on the login page. |
bulwark_traefik |
true |
Traefik labels and the traefik_web network. |
bulwark_traefik_certresolver |
default |
Traefik certificate resolver. |
bulwark_extra_hosts |
{} |
/etc/hosts entries for all containers (no NAT hairpin). |
bulwark_env |
{} |
Extra Bulwark environment variables, merged over the role's. |
bulwark_sso |
~ |
OIDC login: issuer, client_id, client_secret, master_user, master_password (required), email_domains (default ["*"]), denied_mailboxes (default []). |
bulwark_oauth2_proxy_version |
v7.15.5 |
quay.io/oauth2-proxy/oauth2-proxy tag. |
bulwark_handoff_image |
python:3.13-alpine |
Image of the handoff service. |
bulwark_uninstall |
false |
Remove containers and the compose directory. |
bulwark_purge |
false |
With bulwark_uninstall, also remove bulwark_data_path. |
Example
- hosts: webmail
roles:
- bulwark
vars:
bulwark_domain: webmail.example.org
bulwark_jmap_url: https://mail.example.org
bulwark_sso:
issuer: https://auth.example.org/realms/example
client_id: webmail
client_secret: "{{ vault_webmail_client_secret }}"
master_user: webmail@example.org
master_password: "{{ vault_webmail_master_password }}"
denied_mailboxes: [admin@example.org, webmail@example.org]
Notes
-
Stuck on Bulwark's sign-in page. The frontend adopts a handoff session only within 60 seconds of the redirect. If that is missed (reload, restart), the browser keeps a valid but unused session cookie and Traefik keeps sending it to Bulwark.
https://<bulwark_domain>/ssoalways starts a fresh SSO login, and Traefik redirects Bulwark's sign-in page (/<locale>/login) there. The flip side: if Stalwart rejects the mailbox after a successful IdP login (no account, banned client IP), the browser loops between the IdP and/ssoinstead of showing an error. -
Failed logins are cached. After a 401 from Stalwart, Bulwark refuses the same credential for one hour without asking Stalwart again (in memory). A user who logs in before their Stalwart account exists keeps getting "Could not open the mailbox" for that hour;
docker restart <instance>-appclears it. -
Logout. Bulwark's logout ends the Bulwark session, but the oauth2-proxy session (and the IdP session) remains, so the next visit logs in again. Log out at the IdP to end both.
-
Secrets. The session, cookie and handoff signing secrets are generated once on the host (
<docker>/secrets). Deleting them logs everyone out. The handoff self-test:HANDOFF_SELFTEST=1 python3 files/handoff.py.