Skip to content

Refresh relay auth tokens before they expire - #172

Merged
steventux merged 2 commits into
mainfrom
DTOSS-13334-auto-renew-relay-token
Jul 28, 2026
Merged

Refresh relay auth tokens before they expire#172
steventux merged 2 commits into
mainfrom
DTOSS-13334-auto-renew-relay-token

Conversation

@steventux

@steventux steventux commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

Description

Monitor the connection age of the Azure Relay Listener and refresh the credential token on the connection url before it times out.
This prevents daily disconnects in production, the Managed Identity token expiry is 24 hours.

Jira link

https://nhsd-jira.digital.nhs.uk/browse/DTOSS-13334

Review notes

Review checklist

  • Check database queries are correctly scoped to current_provider

@steventux steventux changed the title Ensure connection tokens are refreshed before they expire Refresh relay auth tokens before they expire Jul 22, 2026
@steventux
steventux force-pushed the DTOSS-13334-auto-renew-relay-token branch 3 times, most recently from 99df33b to a5ac501 Compare July 23, 2026 07:00
@steventux
steventux marked this pull request as ready for review July 23, 2026 07:02

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Updates the Azure Relay listener to proactively reconnect based on token expiry time, aiming to prevent daily production disconnects caused by Managed Identity token expiry.

Changes:

  • Refactors RelayListener.listen() into a reconnect loop that refreshes the connection when a timeout occurs.
  • Introduces RelayURI.connection_details() to return both the connection URL and token expiry time (bearer/SAS).
  • Updates unit/integration test scaffolding to accommodate the new listen/connection flow and logging format.

Reviewed changes

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

File Description
src/relay_listener.py Adds token-expiry-aware connection lifecycle and URL/expiry retrieval via connection_details()
tests/test_relay_listener.py Refactors tests to target _listen_on_connection() and verifies reconnect behavior and logging
tests/conftest.py Updates fake_relay harness to align with the new listener behavior in integration-style tests

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

Comment thread src/relay_listener.py
Comment thread src/relay_listener.py Outdated
The relay connection token expires every 24 hours for MI tokens and has
a predefined expiry for SAS tokens.

By monitoring the time the connection has been open in an outer loop we
can force the refresh of the token before it expires.
@steventux
steventux force-pushed the DTOSS-13334-auto-renew-relay-token branch from a5ac501 to 2aeea01 Compare July 23, 2026 07:13
Comment thread src/relay_listener.py Outdated
async with self._connect(connection_url) as websocket:
logger.info("Connected - waiting for worklist actions...")
await self._listen_on_connection(websocket, refresh_at)
except asyncio.TimeoutError:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think this also catches an open-handshake timeout from self._connect() on line 86. Suggest a custom signal eg TokenRefreshDue(Exception) raised at line 97 and caught here. Relay connection failures then fall through to main()'s existing backoff.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

carlosmartinez
carlosmartinez previously approved these changes Jul 23, 2026
Distinguish between TimeoutError and a token expired event by raising a custom error.
@steventux
steventux merged commit e3733dc into main Jul 28, 2026
9 checks passed
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.

3 participants