Caddy serves port 8080 in the Keycloak image. Keycloak listens on 127.0.0.1:8081 only. KEYCLOAK_ADMIN_IP_ALLOWLIST restricts /admin/* and /realms/master/*. KEYCLOAK_TRUSTED_PROXIES sets the client IP from X-Forwarded-For, as in the frontend image. The default allows all. The build options are ENV in the runtime stage, so a start without --optimized keeps them. The HEALTHCHECK probes Caddy and the management port and follows PORT and KC_HTTP_MANAGEMENT_PORT. Add bin/smoke-test-keycloak and the make target test-keycloak-image. Remove the Scalingo buildpack for Keycloak.
5.5 KiB
Identity Provider Setup Guide
Messages uses OpenID Connect in two distinct parts. Do not confuse them.
- Authentication. Messages delegates each user login to an OIDC
provider. The provider can be ProConnect, Keycloak, or any
other OIDC provider. Every Messages instance needs one, this is
managed by the
OIDC_**environment vars. - Mailbox identities and Maildomains. A mail domain decides how a user gets a
mailbox. This part is independent of the authentication provider.
This is managed by the
IDENTITY_PROVIDERenvironment variable, for now Messages only supports Keycloak.
This guide covers the second part only. It shows how to set up Keycloak as the store that holds the mailbox identities. If you are looking for the user login, see authentication-provider.md.
How a User Gets a Mailbox
A mail domain has two independent fields. They give three configurations:
| Mail domain | Who creates the mailbox | IDENTITY_PROVIDER needed |
|---|---|---|
oidc_autojoin on |
Messages, at each login | No |
identity_sync on |
The administrator. Messages then pushes the mailbox to IDENTITY_PROVIDER |
Yes |
| Neither field on | The administrator, shared mailbox only | No |
With oidc_autojoin. Each user who logs in with an address on the
domain gets a mailbox at once. Messages creates the mailbox locally. You do
not need the IDENTITY_PROVIDER for this.
With identity_sync. The administrator creates the mailbox
user1@example.fr. Messages synchronizes it to Keycloak as a user. The
user then logs in through ProConnect, or through another provider
(the OIDC_** vars), with user1@example.fr. The user gets access
to the mailbox.
With neither field. The administrator creates shared mailboxes only. Each user logs in through the OIDC provider. The administrator then gives the user access to a shared mailboxes manually.
You therefore need this guide only when you want identity_sync.
When the Login Address Differs from the Mailbox
Messages matches a user to a mailbox by the address. The two addresses can
differ. The administrator creates the mailbox user1@aaa.fr, but the user
logs in with user1@bbb.fr. Messages does not link them.
In this case, give the user an explicit access to the mailbox. Grant
user1@bbb.fr an access to the user1@aaa.fr mailbox.
Keycloak Setup Steps
For now, Messages only supports Keycloak as an IDENTITY_PROVIDER.
-
Get the Keycloak image. Use the ghcr.io/suitenumerique/messages-keycloak image that this project publishes to the GitHub container registry. You can also build the image from
src/keycloak. The image bundles providers that Messages needs:- the
map-group-attributescript mapper, which puts group attributes into the token claims; - the
bulk-role-membershipadmin extension, which makes a bulk role-membership check faster.
A plain upstream
quay.io/keycloak/keycloakimage does not have these providers. Do not deploy a plain upstream image. - the
-
Deploy Keycloak. Deploy the image with the st-cli. st-cli wraps the same collection in a simpler command-line tool. st-cli is the recommended tool for a self-hoster. You can also deploy it with st-ansible collection or yourself with the docker compose examples.
The image filters
/admin/*and/realms/master/*by client IP whenKEYCLOAK_ADMIN_IP_ALLOWLISTis set. The backend network must be in the list, because the backend uses the admin REST API. See the Keycloak Production Image Proxy (Caddy) section ofenv.md. -
Create a realm. Create a dedicated Keycloak realm for Messages, for example
messages. -
Create the
serviceaccount-messagesclient. This client holds the service account that Messages uses for the provisioning calls. Turn "Client authentication" on. Turn "Authorization" on. Turn "Service accounts roles" on. Turn the "Standard flow" off. You can setup the Root URL to the Messages URL. -
Assign the realm-management roles. On the user in the "Service account roles" tab, assign the client roles
query-users,manage-users,view-usersandview-realm. -
Set the provisioning variables. Set
IDENTITY_PROVIDERtokeycloak. SetKEYCLOAK_URL,KEYCLOAK_REALMandKEYCLOAK_GROUP_PATH_PREFIX. SetKEYCLOAK_CLIENT_IDtoserviceaccount-messages. SetKEYCLOAK_CLIENT_SECRETto the secret of that client. See the Identity Provider (Keycloak) section ofenv.mdfor the full variable table. -
Enable
identity_syncon each mail domain. Enable this field on every mail domain that Messages must sync to Keycloak. Set the field in the Django admin, on theMailDomainchange form. -
Verify the setup. Run the resync command:
python manage.py identity resync-allThe command syncs each mail domain and each mailbox again. It reports the number of synced domains and the number of synced mailboxes.
The Authentication Provider
The steps above do not set up a user login. Messages needs an authentication provider as well. You can use Keycloak for that too, or keep another provider such as ProConnect. See authentication-provider.md.