-
Notifications
You must be signed in to change notification settings - Fork 70
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Service Usage Consumer
permission is required but missing from error message
#1199
Comments
Hi @uriyyo thanks for raising an issue on the Cloud SQL Python Connector! 😄 I am curious as to where you are deploying the application? Is it Cloud Run, GKE, et?. I wonder if the |
Hi @jackwotherspoon, We are using PostgreSQL instance together with Cloud Run Funciton |
Thanks for the quick reply @uriyyo 😄 Let me try and quickly reproduce the issue in my own Cloud Run Function, then I will take a look at your PR, thanks again! 👏 |
@uriyyo I have not been able to reproduce this issue yet. I am able to successfully connect with the Python Connector in a Cloud Run Function with purely the Is there any other part of your setup I should know about that may be causing the need for the |
Hi @jackwotherspoon, Sorry for long response. This issue happens when we connect to database from external instance that is not part In order to connect to SQL Instance we created separate service account and granted it Here is steps to reproduce:
Here is code snippet: from google.cloud.sql.connector import Connector
from google.oauth2.service_account import Credentials
from sqlalchemy import create_engine, text
credentials = Credentials.from_service_account_file("service-account-key.json")
PROJECT_ID = "your-project-id"
connector = Connector(
ip_type="public",
quota_project=PROJECT_ID,
credentials=credentials,
)
def getconn() -> Any:
return connector.connect(
"project:region:instance",
"pg8000",
user="service-account-email",
db="db-name",
enable_iam_auth=True,
)
pool = create_engine(
"postgresql+pg8000://",
creator=getconn,
)
with pool.connect() as conn:
conn.execute(text("SELECT 1")) |
Hi @uriyyo thanks for the detailed explanation, appreciate it. So I think this now makes sense to me, connecting either to a Cloud SQL instance in a separate GCP project or from an external VM instance (not GCP) would indeed require the Because you are needing to set the Service Usage Consumer role (docs):
I am hoping to be able to work on #477 which should remove the need for the custom error message we are throwing currently about the IAM roles. That being said I think your PR is worthwhile in the interim, I will take a look at it shortly. Thanks again for this. |
Cloud SQL Client
is not grated when actually it's Service Usage Consumer
permission is missedService Usage Consumer
permission is required but missing from error message
Hi @uriyyo, quick update from my side. I have worked on properly exposing the actual Cloud SQL Admin API error responses to the end user in #1201 I believe once merged this should fix the issue here, I have not tested the service usage consumer error message path yet but I believe there should be one that would have pointed you in the right direction. This is a better solution in my opinion then the custom error message I previously had implemented. |
Closing this as the new error messages should point at missing permissions. Release will go out on Dec. 10th 🚀 If the new error messages do not help point at the missing |
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 theService Usage Consumer
permission. Under the hood, thegoogle.cloud.sql.connector.Connector
uses theService Usage Consumer
permission to get service metadata. So, you need to grant theService 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
cloud-sql-python-connector/google/cloud/sql/connector/client.py
Lines 128 to 131 in d622575
Here we override exception message:
cloud-sql-python-connector/google/cloud/sql/connector/instance.py
Lines 136 to 137 in d622575
Because of this issue, the error message is misleading and confusing (I spent a lot of time debugging this issue 😅 ).
Example code (or command)
Stacktrace
No response
Steps to reproduce?
Environment
Additional Details
No response
The text was updated successfully, but these errors were encountered: