Skip to content

Keycloak: invalid_client because the Kubernetes IdP cannot load keys

Identitytested on Ubuntu 24.04, k3s v1.37.2+k3s1, Keycloak 26.8.2

A colleague "cleaned up" keycloak.conf and restarted Keycloak. Since then the pod no longer gets a Keycloak token with its service account token. Nothing changed on the client or the cluster – the error only shows up in Keycloak's journal.

Symptom

kubectl exec -n laden kasse -- curl -s $URL --data-urlencode grant_type=client_credentials --data-urlencode client_assertion_type=$TYP --data-urlencode client_assertion@/var/run/secrets/empfang/token
{"error":"invalid_client","error_description":"Invalid client or Invalid client credentials"}

The answer to the client deliberately reveals little.

Diagnosis

Search Keycloak's journal for warnings in general. Then look at the configuration.

sudo journalctl -u keycloak --no-pager -o cat --since "-5min" --grep "Failed to load|CLIENT_LOGIN_ERROR"
WARN  [org.keycloak.broker.kubernetes.KubernetesIdentityProvider] (executor-thread-1)
  Failed to load public keys for Kubernetes IdP 'cluster' with issuer 'https://172.16.0.126:6443':
  java.lang.RuntimeException: Error when loading public keys: javax.net.ssl.SSLHandshakeException:
  (certificate_unknown) PKIX path building failed: …
  unable to find valid certification path to requested target

cat /opt/keycloak/conf/keycloak.conf
# Ausweisstelle des Bürogebäudes – Laborbetrieb (start-dev)
http-host=0.0.0.0
http-port=8080
hostname=http://172.16.0.126:8080

Causes

truststore-pathsThe cluster CA is missing from the truststore

PKIX path building failed means: Java does not know the CA that issued the API server's certificate. Keycloak cannot fetch the JWKS and therefore rejects every credential.

Restartkeycloak.conf only takes effect at startup

Keycloak reads keycloak.conf only at startup. Until the restart it kept running with the old configuration.

kidLoaded keys stay in memory

According to the source (KubernetesIdentityProvider), Keycloak only reloads the JWKS when a credential with an unknown kid arrives. If the cluster rotates its signing key, federation can therefore fail although nobody touched Keycloak – not reproduced in the course.

Fix

Add the line back, restart Keycloak and check in the journal that the truststore was loaded.
sudo nano /opt/keycloak/conf/keycloak.conf
truststore-paths=/opt/keycloak/conf/cluster-ca.crt

sudo journalctl -u keycloak --no-pager -o cat --since "-1min" --grep "truststore"
2026-12-01 10:47:02,556 INFO  [org.keycloak.truststore.TruststoreBuilder] (main) Found the following truststore files in the truststore paths [/opt/keycloak/conf/cluster-ca.crt]

Don't: Search on the client. invalid_client deliberately says nothing about the cause – the reason is only in Keycloak's journal.

▶
Break it yourself and find it – free
Workload and AI agent identity hands-on, day 4: federation – Keycloak accepts the cluster's service account tokens as credentials.
Go to day 4 →

FAQ

What does truststore-paths do in Keycloak?

The option lists additional certificates Keycloak trusts for outgoing HTTPS connections – here the CA of the Kubernetes API server.

Why did it work until the restart?

Keycloak reads keycloak.conf only at startup and keeps keys it has loaded in memory.

Related error patterns

More