You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
8000
Repository navigation
Kubernetes workload identity for operator-managed clients: ServiceAccount references in KeycloakOIDCClient
#55712
I'd like to propose letting a KeycloakOIDCClient reference the Kubernetes ServiceAccount of the workload that uses it, so the workload authenticates with its projected ServiceAccount token instead of a static client secret. Below are the problem, the idea, a proof of concept and a few questions.
The problem
A workload in Kubernetes that calls an OIDC-protected API usually authenticates to Keycloak with a static client secret. That secret is created, copied into a Kubernetes Secret, mounted and rotated, and anyone who can read it can use it. When the Keycloak Operator manages the client, KeycloakOIDCClient supports exactly this pattern through auth.secretRef, and no other authentication method.
Keycloak already has the better answer. Federated client authentication with Kubernetes ServiceAccount tokens has been supported since 26.6. The Pod presents its projected, short-lived, audience-bound ServiceAccount token as a client assertion, and no secret exists at all. Neither Admin API v2 nor the operator can configure such a client today. It works only through Admin API v1 or the admin console, with raw client attributes, so there is no way to declare it next to the workload in GitOps.
The idea
I'd like a KeycloakOIDCClient to reference the ServiceAccount whose token the workload presents:
# namespace iam, next to the Keycloak CRapiVersion: k8s.keycloak.org/v2alpha1kind: KeycloakOIDCClientmetadata:
name: payments-apispec:
keycloakCRName: keycloakrealm: myrealmclient:
loginFlows: [SERVICE_ACCOUNT]auth:
method: federated-jwtidentityProvider: kubernetesserviceAccountRef:
namespace: paymentsname: payments-api
---
# namespace payments: the workload's consentapiVersion: v1kind: ServiceAccountmetadata:
name: payments-apiannotations:
operator.keycloak.org/allowed-oidc-client: iam/payments-api@myrealm
The operator derives the subject (system:serviceaccount:payments:payments-api), so a CR author can bind only ServiceAccounts, never an arbitrary subject. Client CRs live in the namespace of the Keycloak CR, while workloads run elsewhere, so the reference needs consent from the other side. The ServiceAccount names the client CR and realm in an annotation, and a RoleBinding in its namespace grants the operator read access to ServiceAccounts. Without both, no client is created. If consent is withdrawn later, the operator disables the client.
This needs three changes, which I would propose as separate issues and PRs:
Admin API v2: federated client authentication. Two fields on OIDCClientRepresentation.Auth for the identity provider and the subject, mapped to the attributes the federated-jwt authenticator already reads. No Kubernetes coupling. This is useful to anyone managing federated clients through v2.
Operator: act only on the Keycloak client that a CR applied. Today the base controller updates and deletes whatever client has the CR's clientId, including a client it never created. The fix records the applied client's uuid in the status and checks it. This is a bug fix for every client CR, independent of the rest.
Operator: serviceAccountRef with consent, as above, built on 1 and 2.
While testing, I also stumbled on a bug that stands on its own. Admin API v2 runs the client policies on create and update, but it does not persist the settings the executors auto-configure. So, for example, full-scope-disabled or pkce-enforcer silently has no effect on a client created through v2. I reproduced this on current main: pkce-enforcer leaves the PKCE method empty. I'd file it as a separate bug report; it touches the same area as #47576.
The end-to-end tests deploy the patched Keycloak and operator and check, among other things:
a workload gets an access token using only its ServiceAccount token
a wrong ServiceAccount, or the same name in another namespace, is refused
no client secret exists anywhere for the workload
without consent no client is created
withdrawn consent disables the client, and restored consent re-enables it
consent naming another client or realm grants nothing
the CRD rejects invalid auth combinations at admission
The fork's own operator and Admin API v2 tests pass as well. The PoC tests and the fork's tests run in GitHub Actions.
Questions
Is this direction welcome for the operator? In Refine operator authentication into Keycloak #45804, access based on Kubernetes ServiceAccounts is already named as an option for how the operator itself authenticates to Keycloak. This proposal applies the same idea to the workloads whose clients the operator manages.
For the Admin API v2 shape, should identityProvider and subject be flat fields next to secret and certificate on Auth, or should they live in a nested object?
For consent, is an annotation on the ServiceAccount plus a RoleBinding that grants the operator read access in that namespace the right amount of machinery, or would you prefer something simpler (for example, the annotation alone)?
Is the split into the three changes plus the bug fix right for review? As a new contributor I can have two PRs open at a time, so I'd send them in sequence.
A step further: in the review of Operator logic for clients in admin api v2 #45316, separate namespaces per client admin were called the proper solution and deferred. I have a design (not implemented) for client CRs in application namespaces within limits set by a platform team: ADR 0003. Is that worth a separate discussion once this part is settled?
Related threads
#50186 (Client Admin API v2 feedback), #45804 (operator authentication), #47576 (client policies and v2 representations), #48130 (Kubernetes ServiceAccount federation feedback).
Disclosure: much of the PoC code and this post were written with AI coding agents. I have reviewed and tested the result and can maintain it.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
I'd like to propose letting a
KeycloakOIDCClientreference the Kubernetes ServiceAccount of the workload that uses it, so the workload authenticates with its projected ServiceAccount token instead of a static client secret. Below are the problem, the idea, a proof of concept and a few questions.The problem
A workload in Kubernetes that calls an OIDC-protected API usually authenticates to Keycloak with a static client secret. That secret is created, copied into a Kubernetes Secret, mounted and rotated, and anyone who can read it can use it. When the Keycloak Operator manages the client,
KeycloakOIDCClientsupports exactly this pattern throughauth.secretRef, and no other authentication method.Keycloak already has the better answer. Federated client authentication with Kubernetes ServiceAccount tokens has been supported since 26.6. The Pod presents its projected, short-lived, audience-bound ServiceAccount token as a client assertion, and no secret exists at all. Neither Admin API v2 nor the operator can configure such a client today. It works only through Admin API v1 or the admin console, with raw client attributes, so there is no way to declare it next to the workload in GitOps.
The idea
I'd like a
KeycloakOIDCClientto reference the ServiceAccount whose token the workload presents:The operator derives the subject (
system:serviceaccount:payments:payments-api), so a CR author can bind only ServiceAccounts, never an arbitrary subject. Client CRs live in the namespace of theKeycloakCR, while workloads run elsewhere, so the reference needs consent from the other side. The ServiceAccount names the client CR and realm in an annotation, and a RoleBinding in its namespace grants the operator read access to ServiceAccounts. Without both, no client is created. If consent is withdrawn later, the operator disables the client.This needs three changes, which I would propose as separate issues and PRs:
OIDCClientRepresentation.Authfor the identity provider and the subject, mapped to the attributes thefederated-jwtauthenticator already reads. No Kubernetes coupling. This is useful to anyone managing federated clients through v2.serviceAccountRefwith consent, as above, built on 1 and 2.While testing, I also stumbled on a bug that stands on its own. Admin API v2 runs the client policies on create and update, but it does not persist the settings the executors auto-configure. So, for example,
full-scope-disabledorpkce-enforcersilently has no effect on a client created through v2. I reproduced this on currentmain:pkce-enforcerleaves the PKCE method empty. I'd file it as a separate bug report; it touches the same area as #47576.Proof of concept
mainas of 1 December 2026: https://github.com/crumohr/keycloak/tree/poc/kubernetes-workload-identityThe end-to-end tests deploy the patched Keycloak and operator and check, among other things:
authcombinations at admissionThe fork's own operator and Admin API v2 tests pass as well. The PoC tests and the fork's tests run in GitHub Actions.
Questions
identityProviderandsubjectbe flat fields next tosecretandcertificateonAuth, or should they live in a nested object?Related threads
#50186 (Client Admin API v2 feedback), #45804 (operator authentication), #47576 (client policies and v2 representations), #48130 (Kubernetes ServiceAccount federation feedback).
Disclosure: much of the PoC code and this post were written with AI coding agents. I have reviewed and tested the result and can maintain it.
All reactions