The Simplest Way to Make Cloud SQL and OAuth Work Like They Should

Published
Length
3 min read

Luis Felipe Dias

All posts

Teams pair cloud sql and oauth to replace static database passwords with short-lived tokens. This setup links every database query to a verified user identity in production. Shared database passwords create big security risks for growing engineering teams. When one person leaves the team, you must rotate the password everywhere. Using identity tokens fixes this problem by tying database logins to your identity provider. Developers log in with their work accounts instead of copying shared secret

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.

Teams pair cloud sql and oauth to replace static database passwords with short-lived tokens. This setup links every database query to a verified user identity in production.

Shared database passwords create big security risks for growing engineering teams. When one person leaves the team, you must rotate the password everywhere. Using identity tokens fixes this problem by tying database logins to your identity provider. Developers log in with their work accounts instead of copying shared secret keys.

How cloud sql and oauth handle logins and permissions

In this setup, the identity flow starts before a user reaches the database. A user or app requests an access token from the OAuth identity provider. The provider checks the user identity and returns a signed token with a short lifespan. Next, the client sends this token to the Cloud SQL instance as the password. The database checks the token and matches it to a mapped database user.

Once the user logs in, standard database roles control what data they can read. Cloud SQL for PostgreSQL is a managed service, so customers cannot get true superuser attributes. Google manages a system user named cloudsqladmin that holds actual superuser privileges on the database. Customers cannot delete or change these built-in system roles or users.

Instead, admins use the cloudsqlsuperuser role for high-level tasks. This role lets you set up extensions, event triggers, and replication users. The default postgres admin user belongs to this role and can create roles and databases. Still, the default postgres user does not have superuser or replication attributes. It also starts without a password, so you must set one before logging in. When you map OAuth identities, you should assign plain read or write roles instead.

Common failure modes and how to scope roles

Many teams set up token logins but still fail to scope permissions well. A common mistake is mapping every developer token to the cloudsqlsuperuser role. This gives too many people the power to drop tables or read private data. If an attacker steals a broad token, they can change the whole database.

Shared service accounts cause another major problem in production systems. Teams often use one service account token for many different backend apps. When a bad query slows down the database, logs only show the shared account name. You cannot tell which app or user ran the broken query. Unlogged queries also hide who read customer emails, phone numbers, or card numbers.

OAuth 2.0 + End-to-End Encryption: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

To fix this, give each app and team member a distinct database role. Grant read-only permissions to users who only need to check data or fix bugs. Keep the cloudsqlsuperuser role only for rare schema changes and setup tasks. Make sure your token lifespan is short so old tokens expire fast. You should also restrict network paths so only approved networks reach the database port.

Managing token refresh and role hygiene at scale

Running token flows across dozens of databases brings daily work for platform teams. Access tokens expire fast, so client tools must refresh them on a set schedule. Some older database tools do not support token refresh steps out of the box. Developers then waste time running manual scripts just to keep their sessions alive.

Role drift is another issue that builds up over months of fast work. An engineer might get temporary write rights during an outage and keep them forever. You need a repeatable way to review who holds each role across all instances. Pairing clean database roles with central identity checks keeps your production data safe.

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

Does the default postgres user in Cloud SQL have full superuser rights?

No, Cloud SQL for PostgreSQL does not give customers true superuser attributes. The default postgres user belongs to cloudsqlsuperuser and can create roles and databases. A Google system user named cloudsqladmin holds the actual superuser privileges.

Why should teams avoid shared service accounts with OAuth tokens?

Shared accounts hide which person or app ran a specific query in your logs. Using one identity per user or app gives you a clear audit trail. It also lets you revoke access for one app without breaking others.

How should we assign database permissions to OAuth users?

Map each OAuth identity to a database role with the smallest needed permissions. Give most users read-only roles and save cloudsqlsuperuser for admin tasks. Mask sensitive fields like emails, phone numbers, and national IDs in query results.

The query runs. The emails stay hidden.