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

Add support for password functions (useful for RDS IAM auth) #554

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion asyncpg/__init__.py
Expand Up @@ -31,4 +31,4 @@
# snapshots will automatically include the git revision
# in __version__, for example: '0.16.0.dev0+ge06ad03'

__version__ = '0.20.1'
__version__ = '0.21.0.dev0'
7 changes: 6 additions & 1 deletion asyncpg/connect_utils.py
Expand Up @@ -601,6 +601,11 @@ async def _connect_addr(*, addr, loop, timeout, params, config,
raise asyncio.TimeoutError

connected = _create_future(loop)

params_input = params
if callable(params.password):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also add support for coroutines (via inspect.iscoroutinefunction()) to support async callbacks.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, updated the branch with support for async password callbacks and an additional test for that case as well.

params = params._replace(password=params.password())

proto_factory = lambda: protocol.Protocol(
addr, connected, params, loop)

Expand Down Expand Up @@ -633,7 +638,7 @@ async def _connect_addr(*, addr, loop, timeout, params, config,
tr.close()
raise

con = connection_class(pr, tr, loop, addr, config, params)
con = connection_class(pr, tr, loop, addr, config, params_input)
pr.set_connection(con)
return con

Expand Down
20 changes: 20 additions & 0 deletions tests/test_connect.py
Expand Up @@ -204,6 +204,26 @@ async def test_auth_password_cleartext(self):
user='password_user',
password='wrongpassword')

async def test_auth_password_cleartext_callable(self):
def get_correctpassword():
return 'correctpassword'

def get_wrongpassword():
return 'wrongpassword'

conn = await self.connect(
user='password_user',
password=get_correctpassword)
await conn.close()

with self.assertRaisesRegex(
asyncpg.InvalidPasswordError,
'password authentication failed for user "password_user"'):
await self._try_connect(
user='password_user',
password=get_wrongpassword)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like flake8 is unhappy about the extra newline here


async def test_auth_password_md5(self):
conn = await self.connect(
user='md5_user', password='correctpassword')
Expand Down