top of page

Beyond the golden image, part 4. The Bourne Supremacy: Keycloak, Entra ID and who's really pulling the strings

Foto van schrijver: Edwin de Bruin
Edwin de Bruin
16 uur geleden
13 minuten om te lezen

Part 4: The exit door, and why you pick it on day one


In part 3 Himmelblau talked straight to Entra ID, and the laptop behaved like an Entra joined Windows device. Great experience. It also ties every laptop directly to Microsoft.


So here's the plan I had: put Keycloak in front of Entra ID. For now Keycloak just passes everyone on to Entra, so nothing changes for the users. But later, when we're ready, Keycloak could stand on its own, with its own users or another provider behind it, and the laptops wouldn't need to know. A sovereign exit door, built in from the start.


And in part 1 I claimed that switching identity provider would be a change in one file, not a migration project. So I tested both at once: I moved my own laptop from native Entra ID to Keycloak. Spoiler: the config was one file. The login broke anyway.


If part 3 was the Identity, this is the Supremacy: the laptop still finds out who you are, but now someone in the background decides who gets to tell it.



Why put something in between?


Because of the exit door. With Keycloak in the middle, the laptop no longer speaks Entra. It speaks plain OpenID Connect to Keycloak, and Keycloak decides where the user actually authenticates. Today that's Entra ID. Tomorrow it could be another provider, or Keycloak's own user store, and not a single laptop needs to know.


"Keycloak decides" sounds like Keycloak takes over the identity. It doesn't. In my setup Keycloak holds no passwords at all. It's federated with Entra ID: when a user signs in, Keycloak hands them straight on to Entra. Entra checks the password, does the number matching and applies Conditional Access, exactly as before. Then Entra sends Keycloak a token saying who this is, Keycloak creates the user on the fly if it hasn't seen them before, and issues its own token to the laptop.


So there are two hops of trust: the laptop trusts Keycloak, and Keycloak trusts Entra. The laptop never talks to Entra directly anymore. If you've ever put Workspace ONE Access in front of Entra ID, with Entra as the identity provider behind it, you know this pattern: the broker in the middle, the real identity behind it.


That's also what makes the exit door work. Swap the identity provider behind Keycloak, and the laptops don't notice. At least, that's the idea. More on that later.


In EUC terms: it's the broker in front of your desktops. Users connect to the broker; what sits behind it can change without touching the client. And for a series that started with sovereignty, that's the piece that turns "we use Entra ID today" into "we use Entra ID today, and we're not stuck with it".


Setting it up


The chain has three links, and each one is a few minutes of clicking.


1. Entra ID: an app for Keycloak


  • An app registration called Keycloak, single tenant, with a Web redirect URI pointing at Keycloak's broker endpoint: https://<keycloak>/realms/<realm>/broker/entra/endpoint.

  • A client secret. It lives in Keycloak and nowhere else, certainly not in the repo. It also expires, so put the date in your calendar.

  • Use the Application (client) ID in Keycloak, not the tenant ID. Mix them up and Entra answers with AADSTS700016. Rooky mistake, sorry

  • On the enterprise application: Assignment required = Yes, and assign only the users or groups that may log in. More on why below.


2. Keycloak: Entra as identity provider


  • Identity providers, OpenID Connect v1.0, alias entra.

  • Discovery endpoint: https://login.microsoftonline.com/<tenant-id>/v2.0/.well-known/openid-configuration, plus the client ID and secret from step 1. Scopes: openid profile email.


The first time a user signs in, Keycloak creates the account on the fly (just-in-time). Nobody provisions anything.


3. Keycloak: a client for the laptops


  • A new OpenID Connect client, for example NixOS-Werkplek. Public client, no secret.

  • Enable OAuth 2.0 Device Authorization Grant. Leave the redirect URLs empty; Himmelblau doesn't need them.

  • Make sure offline_access is an allowed scope. Himmelblau needs refresh tokens for the Hello PIN.


4. The laptop: one file


And this is the part I was curious about. Same Himmelblau, same module, different settings in the partner folder:

# Partners/<partner>/Config-system-identity.nix
{ ... }:
{
  services.himmelblau.settings = {
    oidc_issuer_url = "https://<keycloak>/realms/<realm>";
    app_id = "NixOS-Werkplek";   # the Keycloak client ID
    allow_console_password_only = false;
    shell  = "/run/current-system/sw/bin/bash";
  };
}

The oidc_issuer_url has to match the issuer in Keycloak's discovery document exactly. domain is ignored in this mode. That's it, at least on paper. The theory from part 1 holds for the config: switching identity provider is one file per partner. On a laptop that's already in use, it's a different story. More on that below.


The laptop never sees a password. It starts the flow, shows a code, and waits. The real sign-in, including number matching, happens in a browser on another device.


The price of the exit door


It's one file, but it isn't free. Everything that made part 3 feel like Windows comes from Himmelblau speaking Entra's own protocols. Put Keycloak in between and Himmelblau speaks generic OIDC, which means the device flow. Here's what that costs.


Onboarding goes through a second device


The login screen can't host a browser, so on the first login the laptop shows a URL and a code. The user opens that URL on their phone or another computer, types the code, and gets redirected to the normal Microsoft sign-in page. Number matching happens in that browser, not on the laptop. Once Keycloak confirms, the laptop logs the user in and offers a PIN.


It works, and it's a one-time thing per user per laptop. But typing a code from one screen into another is not what I'd call a friendly first day at work. Himmelblau has a QR greeter extension for GNOME's login screen that turns the URL and code into a QR code: scan, sign in on your phone, done. The NixOS module doesn't install it for you, so that's on my to-do list.


Device code phishing is a real thing


This is the one I want every reader to take away. The device flow is exactly what Storm-2372 has been abusing since August 2024: the attacker starts a legitimate device flow, sends the code to the victim dressed up as a Teams meeting invite, and the victim signs in on the attacker's behalf. Microsoft's own advice is to block the device code flow wherever you can.


And here I am, building a login that depends on it. The same trick works against any public client with device grant enabled, including mine. Two things make it worse here:


  • Entra ID never sees a device code. As far as Entra is concerned, the user did a normal browser sign-in to the Keycloak app. A Conditional Access policy that blocks device code flow in Entra simply doesn't apply.

  • Device-based Conditional Access checks the device where the browser runs. In this flow, that's usually the user's phone, not the laptop.


So the controls move to Keycloak and to the user. Keep the device code lifetime short, limit who can log in at all (next point), and teach one simple rule: only enter a code that is on your own laptop's screen, right now.


Just-in-time means just anyone, unless you say otherwise


Keycloak creates accounts on the first login, and Himmelblau creates the local account after that. Two just-in-time steps, zero provisioning. Lovely, until a guest account from a completely different tenant logged into my test laptop. Every identity that can sign in to Entra can, by default, sign in to the Keycloak app.


The fix is that Assignment required switch on the Entra enterprise app, plus removing guests you don't need. A group filter on the laptop side (pam_allow_groups) adds a second lock on the door.


What you lose compared to part 3


  • Device join. The laptop is not a device in Entra ID, so no device-based Conditional Access for the laptop itself.

  • Browser SSO. The linux-entra-sso integration is built for Entra. Users get normal web SSO through their Keycloak session instead: one sign-in per day, not a silent one.

  • Intune compliance. Himmelblau's documentation says it doesn't apply to OIDC flows.


What you keep: the Hello PIN. After the first device flow login, Himmelblau offers a PIN, and from then on the user logs in with that. Mind you, that's Himmelblau's own PIN on the laptop, backed by a refresh token from Keycloak, not a Windows Hello PIN that Entra knows about. For the user it feels the same. Whether offline login with that PIN behaves like it does in part 3 is the next thing I'm testing.


A way out of the device flow?


Himmelblau 4.x has an orchestrator mode (orchestrator_enabled = true) that does the Keycloak login through the normal PAM prompt, on the laptop itself. Device grant still has to be enabled on the Keycloak client. Whether that plays nicely with Keycloak redirecting to Entra for the actual sign-in, I don't know yet. That's a test for a later part.


The claim from part 1, tested


So that's the price of Keycloak in front. Now the real test, the one from the intro: a laptop that was already running on native Entra ID, with a user who had logged in and set a PIN, moving to Keycloak. On a freshly installed laptop, the one-file story holds. On this one, it didn't.


The laptop remembers the old identity


Himmelblau keeps a local cache of the users it has seen, including their Hello PIN. That cache is what makes offline login possible, and normally that's exactly what you want. But it doesn't know you switched identity provider. After the rebuild, the laptop still held on to the old user and the old PIN from Entra, while the login now went through Keycloak. Old identity in the cache, new identity at the door. That doesn't end well.


The fix is to empty the cache, so the next login starts clean:

sudo systemctl stop himmelblaud himmelblaud-tasks himmelblaud.socket
sudo rm -f /var/cache/himmelblaud/himmelblau.cache.db /var/cache/nss-himmelblau/*.db
sudo systemctl start himmelblaud.socket

Two things made this harder than it looks:


  • aad-tool wasn't there. Himmelblau has its own tool for managing the cache, but the NixOS module doesn't put it on the PATH. That's why I cleared the cache by hand, with the commands above.

  • sudo goes through Himmelblau too. Once you stop the Himmelblau socket, sudo with an Entra account can fail halfway with "connection refused". Do this from the local break-glass account, not from your Entra account.


Doing this by hand once is fine. Explaining it to a service desk is not. So it ended up in nixmgt, my little management menu on the laptops: under troubleshooting there's now a "Clear login cache" option. It runs as root, so the sudo problem is gone, and it stops the services, clears the cache and starts them again in one go.


There's also a cleaner route I haven't tested yet: the Himmelblau module has an option, aadToolPackage, that should let you put aad-tool on the PATH with one line in the org layer. If that works, aad-tool can clear the cache itself. That's for another afternoon.


And then the session closed after one second


With a clean cache the Keycloak login worked: device flow, Entra, number matching, the lot. The desktop appeared, and one second later I was back at the login screen. The log said gdm-wayland-session: Unable to run session.


The user's file under /var/lib/AccountsService/users/ was empty, so GDM didn't know which session to start for this new user. Adding Session=plasma to that file fixed it. Whether that was a leftover from the switch or something structural, I couldn't tell: I fixed it and restarted a service in the same go. A clean reinstall, logging in with the Keycloak user first, is the real test.


What this means for the design


Part of what fooled me: on the laptop, nothing seems to change. Same Himmelblau, same NixOS module, same login screen, same PIN prompt. It feels like you're tweaking a setting of the tool you already have. But Himmelblau is only the messenger. The moment oidc_issuer_url replaces domain, you're switching identity provider: a different party now vouches for who you are, with different tokens and different user IDs. Same doorman, new list of who's allowed in.


In hindsight, it makes perfect sense that it broke. Everything that makes the login nice is tied to the provider that handed it out. The PIN unlocks keys that were set up with the old provider. The tokens in the cache were issued by the old provider. The user the laptop remembers is the user the old provider described. The new provider doesn't know any of that, and has no reason to accept it.

And the laptop can't simply throw it away either. The whole point of that cache is offline login: trusting what it saw last time, without asking anyone. A laptop on a train can't tell "the provider changed" from "I just don't have WiFi". So it holds on to the old identity, exactly as it was built to do. That's not a bug, that's the feature working against you for one afternoon.


In EUC terms: switching identity provider on a laptop that's already in use isn't changing a setting. It's like moving a Windows device from one tenant to another. You don't flip a switch, you leave and join again.


For the config, it really is one file. For the laptops already out there, plan it as a re-enrollment: clear the cache, have users log in again and set a new PIN, and keep an eye on the account name. If the new provider hands out a different username than the old one, the laptop sees a new user, with a new, empty home folder. Or skip all of that and reinstall, which with this setup is the quicker option anyway.


Could this be automated? Probably. A small service that notices the identity settings changed and clears the cache for you is not hard to imagine, and it's on my list. But that only removes the manual step. Every user still logs in online once and sets a new PIN, so it's still a planned change, not a seamless one.


So pick your exit door up front


The lesson isn't "never switch". It's "don't switch what Himmelblau talks to". The login only breaks when the party Himmelblau trusts for your identity changes. So keep that party the same, and do the switching behind it:


  • Keep Keycloak in front, change what's behind it. Himmelblau keeps talking to Keycloak. Keycloak first sends users to Entra ID, and later to its own user store, or to Workspace ONE Access. As long as the users in Keycloak stay the same users, Himmelblau shouldn't notice a thing. That's the exit door from earlier, used the way it was meant.

  • Or keep Entra in front, and federate Entra. Himmelblau talks to Entra ID natively, like in part 3, and Entra itself hands the actual sign-in to Keycloak or Workspace ONE Access. Himmelblau still sees the same Entra tenant and the same user objects.


The catch in both: the user has to stay the same user. If the new provider creates fresh accounts instead of linking them to the existing ones, you're back at square one, with a new user and an empty home folder.


Fair warning: I haven't tested either of these yet. And since my conclusion leans on the first one, that's not a footnote, it's the next test. What has to hold is simple to say and less simple to prove: when the provider behind Keycloak changes, Keycloak has to keep handing Himmelblau the same user, with the same ID and the same username. If it does, the cache, the PIN and the home folder on the laptop stay valid, and the switch is invisible. If Keycloak creates a new account for the same person instead, we're right back at the broken login from this post.


So that's the plan for a next part: a laptop on Keycloak-with-Entra, a user with a PIN, and then pulling Entra out from behind Keycloak. If the laptop doesn't notice, the exit door is real. If it does, I'll tell you that too.


Side by side


Part 3: direct to Entra ID

Part 4: via Keycloak

First login

UPN, password, number on the login screen

URL and code (or QR), sign-in on a second device

Daily login

Hello PIN

Hello PIN (Himmelblau's own)

MFA

Entra, at the login prompt

Entra, in the browser on the second device

Offline login

Yes

Still to be tested

Device object in Entra

Yes

No

Browser SSO

Yes, silent

Through the Keycloak session

Intune compliance

Possible (beta)

No

Device code flow

Not used

Required

Swap the identity provider

New Himmelblau settings, and every laptop in use re-enrolls

Change it behind Keycloak, laptops unchanged (in theory)

Secrets on the laptop

None

None (the Entra secret lives in Keycloak)

Gotchas I collected along the way


  • Switching isn't as seamless as I hoped. The config is one file, but a laptop that already had users keeps their old identity and PIN in Himmelblau's cache. Clear that cache from the local account, or reinstall. See "The claim from part 1, tested".

  • The issuer URL. Copy issuer from Keycloak's .well-known/openid-configuration, don't type it. Some Keycloak installs, mine included, still have /auth in the path.

  • Testing without a laptop. You can start a device flow against Keycloak with a single curl to the device endpoint. If you get a user_code back, the Keycloak side is ready for Himmelblau.


Where things stand


  • The Keycloak path works end to end: device flow, Keycloak, Entra with number matching, just-in-time accounts, and a PIN afterwards.

  • Not done yet: offline login with the PIN, the QR greeter, and the orchestrator.


My conclusion: decide where you want to end up before the laptops go out. The config makes switching look like one file, but what Himmelblau talks to is part of every laptop's identity, and changing it later means every user logs in again and sets a new PIN.


  • Entra ID is, and will stay, your identity provider? Go native, like in part 3. Best experience, best security story.

  • Want the option to move away from Entra one day? Put Keycloak in front from day one, even if it only passes everyone on to Entra for now. You pay with the device flow, device join and silent SSO. In return, the switch you might make later happens behind Keycloak, where the laptops shouldn't notice it.


What I wouldn't do is what I just did: start native and move to Keycloak later. It works, but it's a re-enrollment, not a change. You can make this choice per partner, so make it per partner, and make it early.


Would you put a broker like Keycloak in front of Entra ID from day one, just to keep the exit door open, or is the device flow a deal breaker for you? And has anyone already tamed the device code phishing risk in a way I haven't thought of? Let me know in the comments.


Blog Series



Sources



Written with the help of AI, because I’m a busy guy. It uses a writing skill built entirely on me, following the second brain concept: my blogs, my notes, my frustrations, the occasional moment of me crying in despair, and my way of explaining things. The storylines are mine, and every issue and gotcha described here is real and happened on my own mach

Opmerkingen


Post: Blog2_Post
bottom of page