adk-python icon indicating copy to clipboard operation
adk-python copied to clipboard

Enhance DatabaseSessionService to support IAM Authentication for Cloud SQL

Open hasebe opened this issue 1 year ago • 4 comments

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

  1. Creating a subclass of DatabaseSessionService: While I was able to achieve the desired functionality by subclassing, it required duplicating a significant portion of the DatabaseSessionService constructor's logic. This is due to the create_engine(db_url) call being directly embedded in the parent constructor, preventing a straightforward super().__init__(db_url) call while also adding the creator parameter.
  2. Making the SQLAlchemy Engine configurable: SQLAlchemy offers mechanisms like sqlalchemy.events.DialectEvents.do_connect for extending connection behavior. However, implementing this would necessitate exposing the SQLAlchemy Engine object directly from ADK, which might not align with the intended architectural design of the DatabaseSessionService.

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.

hasebe avatar May 26 '25 04:05 hasebe

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

hsuabina avatar Sep 17 '25 09:09 hsuabina

We have the exact same issue. It would be nice to have the syntax above.

feketedavid1012 avatar Nov 03 '25 12:11 feketedavid1012

@hsuabina Thanks for bringing this up we have noted the feature request . This feature is currently under review. Thank you for your understanding.

klateefa avatar Nov 19 '25 06:11 klateefa

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.

DeanChensj avatar Jul 09 '26 22:07 DeanChensj