Enhance DatabaseSessionService to support IAM Authentication for Cloud SQL
Please make sure you read the contribution guide and file the issues in the right place. Contribution guide.
Is your feature request related to a problem? Please describe.
The DatabaseSessionService in ADK provides a convenient way to manage sessions using relational databases. However, its current implementation only exposes db_url in the constructor. This design limits the ability to leverage IAM authentication for secure connections to Cloud SQL instances on Google Cloud, which is a powerful tool that simplifies database user management, enhances security, reduces administrative burden.
Given that DatabaseSessionService relies heavily on SQLAlchemy, which inherently supports configuring secure connections to Cloud SQL instances with IAM authentication (as demonstrated in this code sample), it would be beneficial to expose this capability within ADK.
Describe the solution you'd like
I propose exposing an additional parameter, specifically creator, in the DatabaseSessionService constructor. This would enable direct configuration of IAM authentication for Cloud SQL connections, aligning with secure connection patterns.
Describe alternatives you've considered
- Creating a subclass of DatabaseSessionService: While I was able to achieve the desired functionality by subclassing, it required duplicating a significant portion of the
DatabaseSessionServiceconstructor's logic. This is due to thecreate_engine(db_url)call being directly embedded in the parent constructor, preventing a straightforwardsuper().__init__(db_url)call while also adding thecreatorparameter. - Making the SQLAlchemy Engine configurable: SQLAlchemy offers mechanisms like
sqlalchemy.events.DialectEvents.do_connectfor extending connection behavior. However, implementing this would necessitate exposing the SQLAlchemyEngineobject directly from ADK, which might not align with the intended architectural design of theDatabaseSessionService.
Additional context
I believe that updating the signature of the DatabaseSessionService needs thorough consideration, such as whether we should expose other parameters. However, I'm happy to contribute to solving this issue if no one else is currently working on it.
Our workloads (running in GKE) are using a sidecar container with the Cloud SQL Auth Proxy. I guess that means the connection made from the DatabaseSessionService to a given port in localhost is insecure, but the actual proxied connection to the cloud sql instance is secure and managed by the proxy.
In order to leverage IAM Authentication, I would recommend this approach until secure connections can be made directly from the DatabaseSessionService.
Here is an extract of a Deployment resource showing the cloud sql proxy container configuration, in case it helps:
containers:
- name: cloud-sql-proxy
image: gcr.io/cloud-sql-connectors/cloud-sql-proxy:2.14.1
args:
# Authorize using Cloud IAM credentials
- "--auto-iam-authn"
# Enable structured logging with LogEntry format:
- "--structured-logs"
# Use private IP
- "--private-ip"
# Defie port
- "--port=5432"
# Cloud SQL Instance connection name
- "some-project:us-central1:some-instance"
securityContext:
# The default Cloud SQL proxy image runs as the
# "nonroot" user and group (uid: 65532) by default.
runAsNonRoot: true
The db_url passed to the DatabaseSessionService constructor would look like:
postgresql://service-account-name%40service-account-project.iam@localhost:5432/database_name
We have the exact same issue. It would be nice to have the syntax above.
@hsuabina Thanks for bringing this up we have noted the feature request . This feature is currently under review. Thank you for your understanding.
You can now configure and pass your own custom AsyncEngine (e.g., using the Google Cloud SQL Python Connector with IAM authentication enabled) directly to the DatabaseSessionService constructor. Alternatively, any arbitrary keyword arguments passed to the constructor will be forwarded to SQLAlchemy's create_async_engine().
See code reference:
src/google/adk/sessions/database_session_service.py#L234-L260
Closing this issue as it is already supported.