Skip to content

Support callable passwords for credential refresh on reconnect#2476

Open
flamble wants to merge 9 commits intocelery:mainfrom
flamble:feature/callable-password-support
Open

Support callable passwords for credential refresh on reconnect#2476
flamble wants to merge 9 commits intocelery:mainfrom
flamble:feature/callable-password-support

Conversation

@flamble
Copy link

@flamble flamble commented Mar 4, 2026

Resolves #2206.

Allow the password parameter to be a callable (function, class with __call__, etc.) that is invoked fresh on every connection/reconnection attempt. This enables credential refresh for expiring tokens (OAuth, JWT, other tokens) without requiring custom transport subclasses.

The callable is resolved to a string in establish_connection() before passing to py-amqp, so downstream code is unaffected. String passwords continue to work identically (backward compatible).

The broker_failover_strategy was considered, however a fresh token is only obtained after the stale one has already failed to authenticate.

As well as the unit tests included in this commit, I've also verified with a local project that it works in the manner intended and can be used to easily fetch a fresh token on reconnect.

Resolves celery#2206.

Allow the `password` parameter to be a callable (function, class with
`__call__`, etc.) that is invoked fresh on every connection/reconnection
attempt. This enables credential refresh for expiring tokens (OAuth, JWT,
other tokens) without requiring custom transport subclasses.

The callable is resolved to a string in `establish_connection()` before
passing to py-amqp, so downstream code is unaffected. String passwords
continue to work identically (backward compatible).

The [broker_failover_strategy](https://docs.celeryq.dev/en/main/userguide/configuration.html#broker-failover-strategy) was considered,
however a fresh token is only obtained after the stale one has already failed to authenticate.

As well as the unit tests included in this commit, I've also verified
with a local project that it works in the manner intended and can be
used to easily fetch a fresh token on reconnect.
@auvipy auvipy requested review from auvipy and Copilot March 5, 2026 03:58
@auvipy auvipy added this to the 5.7.0 milestone Mar 5, 2026
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds support for providing a callable password so a fresh credential can be fetched on each connect/reconnect attempt (targeted at AMQP via pyamqp), with accompanying documentation and unit tests.

Changes:

  • Resolve callable passwords inside pyamqp.Transport.establish_connection() before creating the underlying amqp.Connection.
  • Document callable-password behavior on kombu.Connection and update Connection.as_uri() to handle callable passwords.
  • Add unit tests covering callable passwords for initial connect and reconnect behavior.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
kombu/transport/pyamqp.py Resolves callable passwords at connect-time and passes the resolved string to amqp.Connection.
kombu/connection.py Documents callable passwords and updates as_uri() to handle callables.
t/unit/transport/test_pyamqp.py Adds tests verifying callable password resolution and per-reconnect invocation for the pyamqp transport.
t/unit/test_connection.py Adds tests ensuring callable passwords are preserved on the Connection object and don’t crash as_uri().

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

flamble added 2 commits March 5, 2026 08:54
Avoids invoking a potentially expensive callable (e.g. network token
refresh) when the result will be masked anyway.
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@codecov
Copy link

codecov bot commented Mar 5, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.25%. Comparing base (e320780) to head (028adba).
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2476      +/-   ##
==========================================
+ Coverage   82.24%   82.25%   +0.01%     
==========================================
  Files          79       79              
  Lines       10086    10093       +7     
  Branches     1153     1156       +3     
==========================================
+ Hits         8295     8302       +7     
  Misses       1589     1589              
  Partials      202      202              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@auvipy auvipy requested review from Copilot and removed request for auvipy March 5, 2026 15:25
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@flamble flamble force-pushed the feature/callable-password-support branch from 15b5820 to 979ddcf Compare March 5, 2026 15:59
@auvipy auvipy requested a review from Copilot March 5, 2026 17:07
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

- Clarify comments as per copilots suggestion around callable password usage
- Address @auvipy's comment to use versionadded for 5.7
@flamble flamble requested a review from auvipy March 6, 2026 08:57
@flamble
Copy link
Author

flamble commented Mar 9, 2026

@auvipy - please let me know if you need anything else from me on this. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature request: Enable password to be a callable

3 participants