-
Notifications
You must be signed in to change notification settings - Fork 7
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
Error in connecting to AlloyDB database with IAM user #151
Comments
@ff-sdesai This is because automatic IAM database authentication is not currently a supported feature of the AlloyDB Python Connector. It is currently being worked and will be released in the near future. I have opened #152 as a public tracking bug for this feature. However, you can actually configure/wire your own SQLAlchemy engine to connect using IAM authentication with some very minimal code. We have an example that uses this method with psycopg2 to connect directly without using the AlloyDB Python Connector. |
@jackwotherspoon I am trying the second work-around you provided with pg8000. When I run this code, I am getting an error Can you please check? |
You do not need to import engine, the event listener should be set on the variable |
I am using sqlalchemy 2.0.23 |
It looks like |
Thanks @jackwotherspoon . Will try this approach |
@jackwotherspoon If we decide to use non-IAM user with alloydb-connector, will this non-IAM user also need access to the GCP project? |
@ff-sdesai Not the built-in database user no, but the IAM Principal (service account or user) used in the environment to authorize connections will still require the AlloyDB client IAM role. |
@jackwotherspoon I tried the solution you suggested using |
You should not pass in the complete IAM service account name, it should be in the format You most likely have missed a configuration step, I would read through https://cloud.google.com/alloydb/docs/manage-iam-authn and make sure all steps have been followed in detail. TLDR steps;
@ff-sdesai have you granted the IAM principal the |
@jackwotherspoon Yes, I have granted those roles. Only thing I have not done is enabling |
@ff-sdesai Let me know if that resolves the connection problems 🤞 |
@jackwotherspoon I am able to connect to AlloyDB from my application now. Thanks for your help! |
Is there a way to use IAM authentication using Python and Cloud Function? Do you have documentation about this? I was able to connect CF to an AlloyDB instance using psycopg2 library but using postgres user and their password. Thanks! |
@Nofugnosis you can use the following psycopg2 sample to make your own IAM authN in Cloud Functions without the Python Connector: https://github.com/GoogleCloudPlatform/alloydb-python-connector/blob/main/tests/system/test_psycopg2_direct_connection.py |
In addition, the Python connector now supports IAM authentication regardless of where you're running your code. |
Thanks @jackwotherspoon and @enocom ! And when I executed the CF, I got this error: File "/layers/google.python.pip/pip/lib/python3.7/site-packages/sqlalchemy/engine/create.py", line 643, in connect The above exception was the direct cause of the following exception: File "/layers/google.python.pip/pip/lib/python3.7/site-packages/sqlalchemy/engine/create.py", line 643, in connect Do you have any idea? Sorry, I don't know if this the correct place to ask this. Thanks in advance! |
This usually means the IAM principal that you're running as isn't the same as your IAM user in your database. Does your function execute as name@projectID.iam? |
Thanks @enocom !! |
Now I have another doubt. alloydb_connector.py: from google.cloud.alloydb.connector import Connector connector = Connector() def getconn(): print("running pool()...") def select_query(query): main.py import sqlalchemy def main(request): |
@Nofugnosis would you mind opening a new issue? We can try to address it and see if there's a general fix for other people having the same question. |
Bug Description
For Postgre Cloudsql, we have to use google cloud-sql-python-connector to connect to DB. When we establish a connection using IAM user, we don't have to pass any password if we pass a flag enable_iam_auth as true . I confirmed this by looking at source-code of this connector that password is optional (Line 58 here) However, for AlloyDB, we have to use alloydb-python-connector and when I am not passing any password for IAM user, it is resulting an error. This can be confirmed from source code where password is not optional (Line 50 here). I tried setting password as blank string or None but it didn't work.I also tried setting the flag enable_iam_auth but I got an error that it's an invalid argument.
The error is because alloy-db-connector has not specified any default value while poping password. Is there any other way we can use this connector with AlloyDB?
Example code (or command)
No response
Stacktrace
No response
Steps to reproduce?
Try connecting to any AlloyDB using an IAM user
Environment
Additional Details
No response
The text was updated successfully, but these errors were encountered: