Skip to content

Commit c2f4b86

Browse files
committed
feat: enforce IAM authentication for Cloud SQL connections by removing password handling
1 parent 4ea1c80 commit c2f4b86

2 files changed

Lines changed: 15 additions & 15 deletions

File tree

alembic/env.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@ 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")
124123
database = os.environ.get("CLOUD_SQL_DATABASE")
125124
use_iam_auth = get_bool_env("CLOUD_SQL_IAM_AUTH", True)
126125
ip_type = os.environ.get("CLOUD_SQL_IP_TYPE", "public")
@@ -147,10 +146,11 @@ def getconn():
147146
"ip_type": ip_type,
148147
"enable_iam_auth": use_iam_auth,
149148
}
150-
if use_iam_auth:
151-
connect_kwargs["password"] = get_iam_login_token()
152-
else:
153-
connect_kwargs["password"] = password
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()
154154
return connector.connect(
155155
instance_name,
156156
"pg8000",

db/engine.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ 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")
7372
database = os.environ.get("CLOUD_SQL_DATABASE")
7473
use_iam_auth = get_bool_env("CLOUD_SQL_IAM_AUTH", True)
7574
ip_type = os.environ.get("CLOUD_SQL_IP_TYPE", "public")
@@ -80,10 +79,11 @@ def asyncify_connection():
8079
"enable_iam_auth": use_iam_auth,
8180
"ip_type": ip_type,
8281
}
83-
if use_iam_auth:
84-
connect_kwargs["password"] = get_iam_login_token()
85-
else:
86-
connect_kwargs["password"] = password
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()
8787

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

@@ -106,7 +106,6 @@ 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")
110109
database = os.environ.get("CLOUD_SQL_DATABASE")
111110
use_iam_auth = get_bool_env("CLOUD_SQL_IAM_AUTH", True)
112111
ip_type = os.environ.get("CLOUD_SQL_IP_TYPE", "public")
@@ -118,10 +117,11 @@ def getconn():
118117
"ip_type": ip_type,
119118
"enable_iam_auth": use_iam_auth,
120119
}
121-
if use_iam_auth:
122-
connect_kwargs["password"] = get_iam_login_token()
123-
else:
124-
connect_kwargs["password"] = password
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()
125125

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

0 commit comments

Comments
 (0)