Skip to content
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

Allow just-in-time compution of credentials for new connections in a pool #903

Open
1 task done
moretea opened this issue Sep 26, 2023 · 0 comments
Open
1 task done

Comments

@moretea
Copy link

moretea commented Sep 26, 2023

Is your feature request related to a problem?

I'd like to use procastinate in my Django project, mainly because of it's awesome name ;).

I'm deploying my application on AWS, and would like to enable automatic secrets management for the RDS database that I'm using there. This implies that the database credentials can (and will) be rotated out, and that one should fetch fresh database credentials that have the newly applied password.

Currently, it is not possible to implement this using an aiopg pool; as far as I can see, new connections to the database are made here:

aiopg/aiopg/pool.py

Lines 336 to 343 in 7b01833

conn = await connect(
self._dsn,
timeout=self._timeout,
enable_json=self._enable_json,
enable_hstore=self._enable_hstore,
enable_uuid=self._enable_uuid,
echo=self._echo,
**self._conn_kwargs,

Describe the solution you'd like

Would aiopg be open to add an additional callback to provide a way to override the values in self._dsn and self._conn_kwargs?

I have noticed that a callback exists that is invoked after a connection is made:

aiopg/aiopg/pool.py

Lines 345 to 346 in 7b01833

if self._on_connect is not None:
await self._on_connect(conn)

I would propose to implement this using the following code, and add the callback to the constructor of the pool, like is done for on_connect.

                if self._before_connect is not None:
                    conn_dsn, conn_kwargs = await self._before_connect(self._dsn, self._conn_kwargs)
                else:
                    conn_dsn = self._dsn
                    conn_kwargs = self._conn_kwargs
                conn = await connect(
                    conn_dsn,
                    timeout=self._timeout,
                    enable_json=self._enable_json,
                    enable_hstore=self._enable_hstore,
                    enable_uuid=self._enable_uuid,
                    echo=self._echo,
                    **conn_kwargs,
                )
                if self._on_connect is not None:
                    await self._on_connect(conn)

I'm not an expert on python async programming, but would be willing to implement this, if you think this has a chance of being merged.

Describe alternatives you've considered

I have disabled password rotation for now, so I am not blocked.

I looked at the synchronous pathways in procrastinate. I'm able to subclass a connector, and override the function which creates a database connection. I'd rather not do that with the Pool class, the _fill_free_pool function does way more than just establishing a connection.

Al alternative could be to extract the following lines into a def _create_connection(dsn, timeout, enable_json, ....), and to subclass the Pool, and override this helper function. Please correct me if I'm wrong, but after browsing through the source code, I have the feeling that this project prefers composition over using inheritance.

aiopg/aiopg/pool.py

Lines 336 to 344 in 7b01833

conn = await connect(
self._dsn,
timeout=self._timeout,
enable_json=self._enable_json,
enable_hstore=self._enable_hstore,
enable_uuid=self._enable_uuid,
echo=self._echo,
**self._conn_kwargs,
)

Additional context

No response

Code of Conduct

  • I agree to follow the aio-libs Code of Conduct
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant