Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions alembic/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def build_database_url():
user = os.environ.get("CLOUD_SQL_USER", "")
password = os.environ.get("CLOUD_SQL_PASSWORD", "")
database = os.environ.get("CLOUD_SQL_DATABASE", "")
use_iam_auth = get_bool_env("CLOUD_SQL_IAM_AUTH", True)
use_iam_auth = get_bool_env("CLOUD_SQL_IAM_AUTH", False)
# Host is provided by connector, so leave blank.
if use_iam_auth:
return f"postgresql+pg8000://{user}@/{database}"
Expand Down Expand Up @@ -120,8 +120,9 @@ def run_migrations_online() -> None:

instance_name = os.environ.get("CLOUD_SQL_INSTANCE_NAME")
user = os.environ.get("CLOUD_SQL_USER")
password = os.environ.get("CLOUD_SQL_PASSWORD")
database = os.environ.get("CLOUD_SQL_DATABASE")
use_iam_auth = get_bool_env("CLOUD_SQL_IAM_AUTH", True)
use_iam_auth = get_bool_env("CLOUD_SQL_IAM_AUTH", False)
ip_type = os.environ.get("CLOUD_SQL_IP_TYPE", "public")

connector = Connector()
Expand All @@ -146,11 +147,10 @@ def getconn():
"ip_type": ip_type,
"enable_iam_auth": use_iam_auth,
}
if not use_iam_auth:
raise RuntimeError(
"CLOUD_SQL_IAM_AUTH must be true when DB_DRIVER=cloudsql."
)
connect_kwargs["password"] = get_iam_login_token()
if use_iam_auth:
connect_kwargs["password"] = get_iam_login_token()
else:
connect_kwargs["password"] = password
Comment on lines +150 to +153
Copy link

Copilot AI Feb 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If use_iam_auth is False, password-based auth is used but there’s no validation that CLOUD_SQL_PASSWORD is set. Add an explicit check with a clear error when password is missing/blank to prevent hard-to-debug connector failures.

Copilot uses AI. Check for mistakes.
return connector.connect(
instance_name,
"pg8000",
Expand Down
24 changes: 12 additions & 12 deletions db/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,9 @@ def asyncify_connection():

instance_name = os.environ.get("CLOUD_SQL_INSTANCE_NAME")
user = os.environ.get("CLOUD_SQL_USER")
password = os.environ.get("CLOUD_SQL_PASSWORD")
database = os.environ.get("CLOUD_SQL_DATABASE")
use_iam_auth = get_bool_env("CLOUD_SQL_IAM_AUTH", True)
use_iam_auth = get_bool_env("CLOUD_SQL_IAM_AUTH", False)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Preserve IAM auth as default for Cloud SQL connections

Changing CLOUD_SQL_IAM_AUTH to default False causes DB_DRIVER=cloudsql to silently switch from IAM tokens to password auth when the env var is omitted; in IAM-only deployments (no CLOUD_SQL_PASSWORD configured), connection setup now passes a missing password and fails at startup. This is a regression from prior behavior (default IAM) and can break existing Cloud SQL runtime paths unless every environment is updated to explicitly set CLOUD_SQL_IAM_AUTH=true.

Useful? React with 👍 / 👎.

Copy link

Copilot AI Feb 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CLOUD_SQL_IAM_AUTH now defaults to False. In environments that previously relied on the implicit default (or on transfers/transfer.py setting it), this changes the default auth mode from IAM to password and can break Cloud SQL connections without an explicit CLOUD_SQL_PASSWORD. Consider defaulting to True (security/compatibility), or default based on whether CLOUD_SQL_PASSWORD is set.

Suggested change
use_iam_auth = get_bool_env("CLOUD_SQL_IAM_AUTH", False)
use_iam_auth = get_bool_env("CLOUD_SQL_IAM_AUTH", password is None)

Copilot uses AI. Check for mistakes.
ip_type = os.environ.get("CLOUD_SQL_IP_TYPE", "public")

connect_kwargs = {
Expand All @@ -79,11 +80,10 @@ def asyncify_connection():
"enable_iam_auth": use_iam_auth,
"ip_type": ip_type,
}
if not use_iam_auth:
raise RuntimeError(
"CLOUD_SQL_IAM_AUTH must be true when DB_DRIVER=cloudsql."
)
connect_kwargs["password"] = get_iam_login_token()
if use_iam_auth:
connect_kwargs["password"] = get_iam_login_token()
else:
Copy link

Copilot AI Feb 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If use_iam_auth is False, connect_kwargs["password"] is set from CLOUD_SQL_PASSWORD without any validation. When CLOUD_SQL_PASSWORD is unset/empty, this will fail later with a driver/connector error that’s hard to diagnose. Add an explicit check and raise a clear RuntimeError when password-based auth is selected but CLOUD_SQL_PASSWORD is missing.

Suggested change
else:
else:
if not password:
raise RuntimeError(
"CLOUD_SQL_PASSWORD must be set and non-empty when "
"CLOUD_SQL_IAM_AUTH is disabled (password-based auth)."
)

Copilot uses AI. Check for mistakes.
connect_kwargs["password"] = password

connection = connector.connect_async(instance_name, "asyncpg", **connect_kwargs)

Expand All @@ -106,8 +106,9 @@ def asyncify_connection():
def init_connection_pool(connector):
instance_name = os.environ.get("CLOUD_SQL_INSTANCE_NAME")
user = os.environ.get("CLOUD_SQL_USER")
password = os.environ.get("CLOUD_SQL_PASSWORD")
database = os.environ.get("CLOUD_SQL_DATABASE")
use_iam_auth = get_bool_env("CLOUD_SQL_IAM_AUTH", True)
use_iam_auth = get_bool_env("CLOUD_SQL_IAM_AUTH", False)
ip_type = os.environ.get("CLOUD_SQL_IP_TYPE", "public")

def getconn():
Expand All @@ -117,11 +118,10 @@ def getconn():
"ip_type": ip_type,
"enable_iam_auth": use_iam_auth,
}
if not use_iam_auth:
raise RuntimeError(
"CLOUD_SQL_IAM_AUTH must be true when DB_DRIVER=cloudsql."
)
connect_kwargs["password"] = get_iam_login_token()
if use_iam_auth:
connect_kwargs["password"] = get_iam_login_token()
else:
connect_kwargs["password"] = password
Comment on lines +121 to +124
Copy link

Copilot AI Feb 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If use_iam_auth is False, connect_kwargs["password"] is set from CLOUD_SQL_PASSWORD without validation. Add a clear error when password-based auth is selected but CLOUD_SQL_PASSWORD is missing/blank to avoid opaque connector failures.

Copilot uses AI. Check for mistakes.

conn = connector.connect(
instance_name, # The Cloud SQL instance name
Expand Down
4 changes: 0 additions & 4 deletions transfers/transfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,6 @@
):
os.environ["DB_DRIVER"] = "cloudsql"

# Cloud SQL should use IAM auth by default unless explicitly disabled.
if (os.getenv("DB_DRIVER") or "").strip().lower() == "cloudsql":
os.environ.setdefault("CLOUD_SQL_IAM_AUTH", "true")

from alembic import command
from alembic.config import Config

Expand Down