Grant access Keycloack roles to specific subdomains with traefik

Viewed 86

My goal

overall goal

I want to serve various applications running on docker containers hosted on the same server, each on a subdomain of company.com. And I want only people from my organization (Microsoft AD azure) to access subdomains, in some cases even only people having specific AD groups.

goal specific to Keycloak

I simply want to have specific realm roles have access to specific subdomains, and nothing else. And this, using only a single realm client (see further for explanation).

what I have achieved to do

I have linked several applications on a server and serving each using traefik on a specific subdomain. For example app1.company.com and app2.company.com.

I have also made a middleware so that all routers using it will make sure users must login. I have used a thomseddon/traefik-forward-auth container that I called oauth. I am using Keycloak and I have successfully linked an Azure Active Directory as identity provider. For this, I enabled a single-tenant application and used its client id & key. I also mapped successfully some AD group to a Keycloak role.

Within oauth configuration, I have added the client id & secret from a single keycloak client rather than from the azure application. I believe this is mandatory if we want to use traefik to redirect trafic.

Now, only people from within my organization can access each app. Success!

Some documentations I used

Homelab Single Sign-On & TLS

How to Configure Microsoft Azure Active Directory as Keycloak Identity Provider to Enable Single Sign-On

what I want to do

Each application has its own subdomain. However I cannot figure out where to make it so that some subdomains are accessible only if a user has a specific realm role (linked automatically depending on a AD group thanks to the above-mentioned mapper).

  • I thought of adding resources within the Keycloak client, but I don't find how to do it using subdomains.
  • other alternative is to make one client per subdomain, but this means I need to run one oauth container per subdomain... This seems overkill & a waste of resources without counting maintenance.
1 Answers

The only relatively simple solution I found was to create one Keycloak client per type of permission rules (aka Roles in Keycloak) I wanted.

Then for each type, I had to make a traefik-forward-auth container and connect it to each Keycloak client. I am still using the same Identity provider, so once you've configured it to generate roles, you only need to tell which roles may use which client.

To give an example, you want to have 3 types of permission rules: one for admin, one for trusted users, and one for untrusted user. It could look like this:

  • admin.example.com
  • trustedusers.example.com/app1 and trustedusers.example.com/app2
  • untrustedusers.example.com/app3 and untrustedusers.example.com/app4

You would then have 3 forwardAuth middlewares within traefik.

It is a little bit more cumbersome, but your applications are still connected to your OIDC provider using a single key, then you manage the details using keycloak and these clients.

Of course there is still the solution to have one client per application as is originally planned. This may be unnecessary, and cumbursome if you have a large number of applications (one additional container per application + additional middleware).

Related