how to handle ever-changing password in sqlalchemy+psycopg2?

2024/9/8 9:18:52

I inherited some code that uses sqlalchemy with psycopg2, which needs to run on AWS. RDS Postgres supports iam-based authentication, but the way it does it is rather kludgy: you request a temporary password using the AWS RDS API, which is good for about 15 minutes, and you pass that as the password.

The code I have effectively does e = create_engine(make_sqlalchemy_string()), where make_sqlalchemy_string() makes an aws api call, gets the temporary password, and everything is good for a while. Except something, somewhere in the sqlalchemy code closes the connection, and then the password is no longer good. Is there already a way to make sqlalchemy request a new connection string every time it needs to reconnect?

I can think of a number of workarounds, but none of them are pleasant. Is there a standard way of achieving this?

Thanks

Answer

One approach would be to use the creator argument of create_engine():

creator – a callable which returns a DBAPI connection. This creation function will be passed to the underlying connection pool and will be used to create all new database connections. Usage of this function causes connection parameters specified in the URL argument to be bypassed.

Just modify your make_sqlalchemy_string() to produce the dsn argument for psycopg2.connect(), which might mean not having to modify it at all due to accepted connection string formats, and pass the creator:

create_engine('postgresql://', creator=lambda: psycopg2.connect(make_dsn_string()))
https://en.xdnf.cn/q/73210.html

Related Q&A

How to determine which compiler was requested

My project uses SCons to manage the build process. I want to support multiple compilers, so I decided to use AddOption so the user can specify which compiler to use on the command line (with the defaul…

Does pytest have anything like google tests non-fatal EXPECT_* behavior?

Im more familiar with the google test framework and know about the primary behavior pair they support about ASSERT_* vs EXPECT_* which are the fatal and non-fatal assert modes.From the documentation:Th…

Radon transformation in python

Here is a dummy code:def radon(img):theta = np.linspace(-90., 90., 180, endpoint=False)sinogram = skimage.transform.radon(img, theta=theta, circle=True)return sinogram # end defI need to get the sinogr…

Librosa raised OSError(sndfile library not found) in Docker

Im trying to write the Dockerfile for a small python web project and there is something wrong with the dependencies. Ive been doing some search on the internet and it said that Librosa library requires…

Implementing Tags using Django rest framework

TDLR : what is the best way to implement tags in django-rest-framework. where the tags has a created_by field which is the currently authenticated user.I am trying to achieve a very simple/common thing…

Python audiolab install, unable to install (or find) libsndfile on Mac OSX

Trying to install scikits.audiolab-0.11.0 on Mac, bit it requires libsndfile: http://www.mega-nerd.com/libsndfile/. I did install libsndfile supposedly, using libsndfile_python-1.0.0-py2.7-macosx10.5.m…

Connecting to events of another widget

This is most likely a duplicate question, but I have to ask it because other answers arent helping in my case, since I am new to pyqt (switched from tkinter few days ago).I am wondering if is it possib…

Diff multidimensional dictionaries in python

I have two dictionariesa = {home: {name: Team1, score: 0}, away: {name: Team2, score: 0}} b = {home: {name: Team1, score: 2}, away: {name: Team2, score: 0}}The keys never change but I want to get that …

Pandas DatetimeIndex vs to_datetime discrepancies

Im trying to convert a Pandas Series of epoch timestamps to human-readable times. There are at least two obvious ways to do this: pd.DatetimeIndex and pd.to_datetime(). They seem to work in quite dif…

Slicing a circle in equal segments, Python

I have a set of close of 10,000 points on the sky. They are plotted using the RA (right ascension) and DEC (declination) on the sky. When plotted, they take the shape of a circle. What I would like to …