Repository navigation
fix(sqs): renew each predefined queue's STS credentials when they expire - #2900
xKrishnaSaxena wants to merge 1 commit into
Conversation
With sts_role_arn and more than one predefined queue, each queue gets its
own boto3 client, made with the STS credentials fetched for it. But the
channel kept a single sts_expiration, overwritten by whichever client was
created last. When the credentials expired, the first queue to be used
got a new client and moved sts_expiration forward, so every other queue
kept its client with expired credentials. Its requests got 403
ExpiredToken, and on the polling path that error stops a Celery worker
("Unrecoverable error").
The expiry is now kept per queue, and each client is renewed when its
own credentials expire. SNS fanout had the same shared expiry for
predefined exchanges and gets the same change. sts_expiration is still
set as before.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #2900 +/- ##
=======================================
Coverage 86.46% 86.47%
=======================================
Files 80 80
Lines 11860 11864 +4
Branches 1444 1444
=======================================
+ Hits 10255 10259 +4
Misses 1390 1390
Partials 215 215 ☔ View full report in Codecov by Harness. |
atishay-kasliwal
left a comment
There was a problem hiding this comment.
The two new tests with several predefined queues / exchanges fail on master: after queue-1 refreshes its credentials, queue-2 keeps its expired ones. All 14 STS tests in t/unit/transport/SQS/ pass here. asynsqs still checks hasattr(self, "sts_expiration"), which this branch keeps setting, so that path is unchanged.
can you please make your comments more simplistic and clear? I found your comments or suggestions bit unclear to understand... |
Note: Before submitting this pull request, please review our contributing
guidelines & [Anti AI spamming Guidelines] (celery/celery#10932). Before opening an AI based PR make sure you asked
in the discussion forum or in the related open issues first.
Description
Fixes #2899
With
sts_role_arnand two or more predefined queues, a worker stopped when the first STS credentials expired, withUnrecoverable error: Exception('Request HTTP Error HTTP 403 Forbidden (... ExpiredTokenException ...)')(the error in #2031).Cause. Each predefined queue has its own boto3 client, made with the STS credentials fetched for that queue. The channel kept one
sts_expirationfor all of them, set by the client created last. When the credentials expire, the first queue to be used gets a new client and movessts_expirationforward. Every other queue then passesis_sts_token_refresh_required()and keeps its client, whose credentials have expired. The poll for that queue (asynsqs()usessqs(queue)) gets 403 ExpiredToken, and the error stops the worker.SnsFanoutkept the same sharedsts_expirationfor predefined exchanges.Change.
Channelkeeps_sts_expirations(queue name => expiry of the credentials its client was made with), and_handle_sts_session()checks the queue's own expiry.SnsFanoutdoes the same per exchange.sts_expirationis still set as before. Nothing uses it to decide on a refresh any more, butasynsqs()checks that it exists, and it is a public attribute. Happy to drop it if you prefer.Another option was one set of STS credentials shared by all queues, with every client rebuilt on refresh. That needs the credentials stored on the channel, so it is a larger change. This PR keeps the current one-AssumeRole-per-queue model.
Tests.
test_SQS.py::test_sts_session_expired_with_multiple_predefined_queues: two queues get clients, time moves past the expiry, queue-1 is refreshed, and queue-2 must then come back with unexpired credentials. Each fake client is the credentials it was made with, so the test checks the credentials themselves, not a call count.test_SQS_SNS.py::test_sts_session_expired_with_multiple_predefined_exchanges: the same for SNS exchanges.sts_expirationto drive the refresh now set the per-queue / per-exchange expiry.test_sts_session_not_expiredcalledsqs(), whichtest_Channelreplaces with a mock, so it never reached this code. It now calls the real method, like the tests next to it.Both new tests fail on
mainand pass with the fix.End to end: a Celery 5.7 worker (solo pool) on two predefined queues with
sts_role_arn, against moto behind a small proxy that answers 403 ExpiredToken when the request's access key has expired. The fake STS credentials last 15 s, and one task goes to each queue every 5 s:Commands:
*
test_ack_invalid_receipt_with_backoff[...]fails onmainunder moto too and passes on goaws (moto, not this change).As the template asks, I first raised this in #2899. I used an AI assistant for this change.