The Simplest Way to Make Cloud Run and Keycloak Work Like They Should
You pair cloud run and keycloak to manage user logins for serverless containers. Keycloak handles user identity while Cloud Run hosts your apps or Keycloak itself. This setup lets your team run web apps that scale up and down fast. Users log in once through Keycloak and get tokens for your Cloud Run services. Your app containers check these tokens before they run any user requests. Teams also run Keycloak inside Cloud Run to skip managing virtual machines. How logins and roles work with cloud
Your coding agents run with your access.
Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.
You pair cloud run and keycloak to manage user logins for serverless containers. Keycloak handles user identity while Cloud Run hosts your apps or Keycloak itself.
This setup lets your team run web apps that scale up and down fast. Users log in once through Keycloak and get tokens for your Cloud Run services. Your app containers check these tokens before they run any user requests. Teams also run Keycloak inside Cloud Run to skip managing virtual machines.
How logins and roles work with cloud run and keycloak
First, a user opens your web app hosted on Cloud Run. The app redirects the user to your Keycloak login page. Keycloak checks the user password and any two-factor rules you set up. Once the check passes, Keycloak sends a signed token back to the user.
The user browser then sends this token with every request to Cloud Run. Your Cloud Run service reads the token and checks the digital signature. It pulls the public key from Keycloak to confirm the token is real. Inside the token, your app finds the user email and assigned roles. The app then decides what data the user can read or write.
Some teams also host the Keycloak server itself on Cloud Run. Since Cloud Run containers are stateless, Keycloak needs an external relational database. You link the container to a managed cloud database to store realm data. This setup keeps user accounts, client rules, and role maps safe across restarts.
Common failure modes and how to scope access
Running this pair in production brings a few common security and scaling traps. First, teams often share one broad service account across many Cloud Run services. If an attacker compromises one container, they gain permissions for every other service. You should create a unique service account for each Cloud Run app instead. Give each account only the exact cloud roles it needs to do its job.
Keycloak + End-to-End Encryption: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
Second, teams sometimes set token lifespans that last for hours or days. Long-lived tokens give attackers a wide window if a token leaks. Set Keycloak to issue short access tokens that expire in a few minutes. Let the client app refresh the session in the background while the user works.
Third, running multiple Keycloak containers on Cloud Run can break user sessions. Keycloak relies on internal caches to track active logins across nodes. Serverless platforms spin containers up and down without warning, which drops local caches. You must configure distributed caching or sticky routing so logins stay valid. Also, lock down your admin console so public internet traffic cannot reach it.
Fourth, apps often trust tokens without checking the audience or realm claims. A valid token from a test client might manage a production service by mistake. Always check the issuer, audience, and role claims inside your app logic. Finally, make sure your services log who ran each action and when. Mask sensitive fields like emails, phone numbers, or card numbers in your logs.
Govern statements and responses across databases and APIs
Good config and scoped roles are needed setup. They do not check single queries in flight or mask sensitive fields in returned rows. Across Postgres, MySQL, SQL Server, MongoDB, ClickHouse, Kubernetes APIs, SSH, and internal HTTP endpoints, hoop.dev runs beside each supported service as an open-source proxy, a sidecar for your database or API.
First, hoop.dev guardrails block banned queries before they run. Then hoop.dev data masking rewrites matched sensitive values in responses in memory. The audit trail records every statement and verdict per resource. Furthermore, hoop.dev holds SOC 2 Type II and is open source on GitHub.
Frequently asked questions
Can I run Keycloak on Cloud Run with scale to zero?
You can, but cold starts take time when a new container boots up. Most teams keep at least one instance warm to avoid slow user logins.
How does Cloud Run validate Keycloak tokens?
Your app fetches public keys from the Keycloak certificate endpoint over the network. It uses those keys to check the signature and expiration on each token.
Where should Keycloak store its data when running on Cloud Run?
Keycloak must use a managed SQL database to hold realms, users, and clients. Never store this data on the local container disk because containers reset often.
Related guides
In this post