The Simplest Way to Make Keycloak and Spanner Work Like They Should
Running keycloak and spanner together links open source identity management with a global cloud database. Your team logs in through one central portal to run queries against relational cloud data. This setup serves two distinct use cases in a modern stack. First, platform teams can point the identity server to store its own state in the database. Second, engineers and apps can use identity tokens to read and write business data. Both paths need clear role rules and clean network setups to stay
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.
Running keycloak and spanner together links open source identity management with a global cloud database. Your team logs in through one central portal to run queries against relational cloud data.
This setup serves two distinct use cases in a modern stack. First, platform teams can point the identity server to store its own state in the database. Second, engineers and apps can use identity tokens to read and write business data. Both paths need clear role rules and clean network setups to stay safe.
How identity and data flow between keycloak and spanner
Each tool speaks a different trust language out of the box. The identity server issues standard web tokens after a user logs in and passes multi-factor checks. The cloud database expects cloud identity roles and signed cloud credentials before it runs a query.
To bridge them, teams set up workload identity federation inside their cloud account. The user logs in to the identity server first and receives a signed token. The cloud platform checks that token against the identity server public keys. Next, the cloud platform exchanges that token for a temporary cloud role session. Finally, the database engine checks that cloud role before it allows any table reads or writes.
When the identity server uses the database as its own backing store, the flow changes. The server uses a standard relational driver that translates queries for the cloud database dialect. A dedicated cloud service account lets the server read and write realms, users, and sessions.
Common failure modes and how to scope roles
Many teams take shortcuts when they wire up this identity bridge. The most common mistake is sharing one broad cloud service account across the whole engineering team. Everyone downloads the same static key file to their laptop to run database queries.
Shared keys erase user identity from your database audit logs. When a bad query drops a table, the log only shows the shared service account name. You cannot tell which engineer ran the command or approved the change. Broad roles also let read-only users change schema definitions or read sensitive tables by mistake.
Keycloak + End-to-End Encryption: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
Static keys also create a huge cleanup chore when someone leaves the team. You must rotate the key and update every app and laptop that uses it. During that change, scripts break and engineers lose valuable work time.
To fix this, map specific identity groups to narrow database roles. Create one group for readers, one for writers, and one for database admins. Map each group to a separate cloud role with strict permissions. Keep the database on a private network so public traffic cannot reach it. Remove static service account keys from user laptops and rely on short identity sessions instead.
Auditing queries and protecting sensitive fields
Cloud role mapping solves the login problem, but it leaves gaps inside the database session. Cloud logs record who started a session, yet fine-grained query logs can grow huge and expensive. Even worse, a valid read role still returns full tables full of private customer fields.
When an engineer runs a check on a production table, raw rows return to their screen. Those rows often hold emails, phone numbers, card numbers, or national IDs. Scoping roles at the table level cannot rewrite those specific columns on the fly.
Teams need a way to tie every single statement back to one human identity in Keycloak. They also need field masking that rewrites matched fields in results before data leaves the network. That way, developers can check production bugs while sensitive columns stay masked.
Add runtime guardrails and masking beside your databases and APIs
Login rules, role grants, and CI checks govern who connects to your stack. Yet any allowed script, engineer, or AI agent still runs commands with the full scope of its role. For workloads on Postgres, MySQL, SQL Server, MongoDB, ClickHouse, Kubernetes, SSH, or internal HTTP services, hoop.dev sits beside the service as an open-source proxy, a sidecar for your database or API.
Before a query runs, hoop.dev guardrails check the statement against your rules. An AI Analyzer you configure can score intent or hold risky calls for human review. Next, hoop.dev data masking rewrites sensitive fields in responses, and each verdict is logged per listener. Explore the MIT-licensed project on GitHub.
Frequently asked questions
Can Keycloak use Spanner to store its own user and realm data?
Yes, it can store its internal state there with the right relational driver translator. You must match the database dialect to the translator your identity server expects.
How do users log in to Spanner with their Keycloak credentials?
You link your identity realm to cloud workload identity federation as an external provider. Users trade their web token for a short cloud session that grants database permissions.
How do we stop engineers from seeing raw card numbers in query results?
Cloud roles alone only block or allow whole tables or views. Use data masking rules that rewrite card numbers, emails, and phone numbers in query results.
Related guides
In this post