-
Notifications
You must be signed in to change notification settings - Fork 83
Description
Bug Description
When I try to connect to a Cloud SQL instance using the google.cloud.sql.connector.Connector I'm getting error:
Forbidden: Authenticated IAM principal does not seem authorized to make API request. Verify 'Cloud SQL Admin API' is enabled within your GCP project and 'Cloud SQL Client' role has been granted to IAM principal.
But I have already granted the Cloud SQL Client role to the service account. Actually, the issue is with the Service Usage Consumer permission. Under the hood, the google.cloud.sql.connector.Connector uses the Service Usage Consumer permission to get service metadata. So, you need to grant the Service Usage Consumer permission to the service account to fix this issue.
cloud-sql-python-connector/google/cloud/sql/connector/client.py
Lines 258 to 262 in d622575
| self._get_metadata( | |
| project, | |
| region, | |
| instance, | |
| ) |
cloud-sql-python-connector/google/cloud/sql/connector/client.py
Lines 128 to 131 in d622575
| resp = await self._client.get(url, headers=headers) | |
| if resp.status >= 500: | |
| resp = await retry_50x(self._client.get, url, headers=headers) | |
| resp.raise_for_status() |
Here we override exception message:
cloud-sql-python-connector/google/cloud/sql/connector/instance.py
Lines 136 to 137 in d622575
| if e.status == 403: | |
| e.message = "Forbidden: Authenticated IAM principal does not seem authorized to make API request. Verify 'Cloud SQL Admin API' is enabled within your GCP project and 'Cloud SQL Client' role has been granted to IAM principal." |
Because of this issue, the error message is misleading and confusing (I spent a lot of time debugging this issue 😅 ).
Example code (or command)
import pymysql.connections
import sqlalchemy
from google.cloud.sql.connector import Connector
connector = Connector()
def getconn() -> pymysql.connections.Connection:
conn: pymysql.connections.Connection = connector.connect(
"project:region:instance",
"pymysql",
user="my-user",
password="my-password",
db="my-db-name",
enable_iam_auth=True,
)
return conn
pool = sqlalchemy.create_engine(
"mysql+pymysql://",
creator=getconn,
)Stacktrace
No response
Steps to reproduce?
- Try to connect to a Cloud SQL instance with SQL Admin API enabled and Cloud SQL Client role, but without Service Usage Consumer role.
Environment
- OS type and version: macOS 14.6.1
- Python version: 3.12
- Cloud SQL Python Connector version: 1.14.0
Additional Details
No response