Skip to content

Commit 2581f61

Browse files
committed
feat: disable default IAM authentication for Cloud SQL connections and allow password handling
1 parent c2f4b86 commit 2581f61

3 files changed

Lines changed: 19 additions & 23 deletions

File tree

alembic/env.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def build_database_url():
5959
user = os.environ.get("CLOUD_SQL_USER", "")
6060
password = os.environ.get("CLOUD_SQL_PASSWORD", "")
6161
database = os.environ.get("CLOUD_SQL_DATABASE", "")
62-
use_iam_auth = get_bool_env("CLOUD_SQL_IAM_AUTH", True)
62+
use_iam_auth = get_bool_env("CLOUD_SQL_IAM_AUTH", False)
6363
# Host is provided by connector, so leave blank.
6464
if use_iam_auth:
6565
return f"postgresql+pg8000://{user}@/{database}"
@@ -120,8 +120,9 @@ def run_migrations_online() -> None:
120120

121121
instance_name = os.environ.get("CLOUD_SQL_INSTANCE_NAME")
122122
user = os.environ.get("CLOUD_SQL_USER")
123+
password = os.environ.get("CLOUD_SQL_PASSWORD")
123124
database = os.environ.get("CLOUD_SQL_DATABASE")
124-
use_iam_auth = get_bool_env("CLOUD_SQL_IAM_AUTH", True)
125+
use_iam_auth = get_bool_env("CLOUD_SQL_IAM_AUTH", False)
125126
ip_type = os.environ.get("CLOUD_SQL_IP_TYPE", "public")
126127

127128
connector = Connector()
@@ -146,11 +147,10 @@ def getconn():
146147
"ip_type": ip_type,
147148
"enable_iam_auth": use_iam_auth,
148149
}
149-
if not use_iam_auth:
150-
raise RuntimeError(
151-
"CLOUD_SQL_IAM_AUTH must be true when DB_DRIVER=cloudsql."
152-
)
153-
connect_kwargs["password"] = get_iam_login_token()
150+
if use_iam_auth:
151+
connect_kwargs["password"] = get_iam_login_token()
152+
else:
153+
connect_kwargs["password"] = password
154154
return connector.connect(
155155
instance_name,
156156
"pg8000",

db/engine.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,9 @@ def asyncify_connection():
6969

7070
instance_name = os.environ.get("CLOUD_SQL_INSTANCE_NAME")
7171
user = os.environ.get("CLOUD_SQL_USER")
72+
password = os.environ.get("CLOUD_SQL_PASSWORD")
7273
database = os.environ.get("CLOUD_SQL_DATABASE")
73-
use_iam_auth = get_bool_env("CLOUD_SQL_IAM_AUTH", True)
74+
use_iam_auth = get_bool_env("CLOUD_SQL_IAM_AUTH", False)
7475
ip_type = os.environ.get("CLOUD_SQL_IP_TYPE", "public")
7576

7677
connect_kwargs = {
@@ -79,11 +80,10 @@ def asyncify_connection():
7980
"enable_iam_auth": use_iam_auth,
8081
"ip_type": ip_type,
8182
}
82-
if not use_iam_auth:
83-
raise RuntimeError(
84-
"CLOUD_SQL_IAM_AUTH must be true when DB_DRIVER=cloudsql."
85-
)
86-
connect_kwargs["password"] = get_iam_login_token()
83+
if use_iam_auth:
84+
connect_kwargs["password"] = get_iam_login_token()
85+
else:
86+
connect_kwargs["password"] = password
8787

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

@@ -106,8 +106,9 @@ def asyncify_connection():
106106
def init_connection_pool(connector):
107107
instance_name = os.environ.get("CLOUD_SQL_INSTANCE_NAME")
108108
user = os.environ.get("CLOUD_SQL_USER")
109+
password = os.environ.get("CLOUD_SQL_PASSWORD")
109110
database = os.environ.get("CLOUD_SQL_DATABASE")
110-
use_iam_auth = get_bool_env("CLOUD_SQL_IAM_AUTH", True)
111+
use_iam_auth = get_bool_env("CLOUD_SQL_IAM_AUTH", False)
111112
ip_type = os.environ.get("CLOUD_SQL_IP_TYPE", "public")
112113

113114
def getconn():
@@ -117,11 +118,10 @@ def getconn():
117118
"ip_type": ip_type,
118119
"enable_iam_auth": use_iam_auth,
119120
}
120-
if not use_iam_auth:
121-
raise RuntimeError(
122-
"CLOUD_SQL_IAM_AUTH must be true when DB_DRIVER=cloudsql."
123-
)
124-
connect_kwargs["password"] = get_iam_login_token()
121+
if use_iam_auth:
122+
connect_kwargs["password"] = get_iam_login_token()
123+
else:
124+
connect_kwargs["password"] = password
125125

126126
conn = connector.connect(
127127
instance_name, # The Cloud SQL instance name

transfers/transfer.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,6 @@
4747
):
4848
os.environ["DB_DRIVER"] = "cloudsql"
4949

50-
# Cloud SQL should use IAM auth by default unless explicitly disabled.
51-
if (os.getenv("DB_DRIVER") or "").strip().lower() == "cloudsql":
52-
os.environ.setdefault("CLOUD_SQL_IAM_AUTH", "true")
53-
5450
from alembic import command
5551
from alembic.config import Config
5652

0 commit comments

Comments
 (0)