diff --git a/src/rhsm/connection.py b/src/rhsm/connection.py index 54302c0099..2d7f5c1018 100644 --- a/src/rhsm/connection.py +++ b/src/rhsm/connection.py @@ -500,10 +500,14 @@ class RateLimitExceededException(RestlibException): The retry_after attribute may not be included in the response. """ - def __init__(self, code: int, msg: str = None, headers: str = None) -> None: + def __init__(self, code: int, msg: str = None, headers: dict = None) -> None: super(RateLimitExceededException, self).__init__(code, msg) self.headers = headers or {} - self.retry_after = safe_int(self.headers.get("retry-after")) + self.retry_after = None + for header, value in self.headers.items(): + if header.lower() == "retry-after": + self.retry_after = safe_int(value) + break self.msg = msg or "Access rate limit exceeded" if self.retry_after is not None: self.msg += ", retry access after: %s seconds." % self.retry_after diff --git a/src/subscription_manager/scripts/rhsmcertd_worker.py b/src/subscription_manager/scripts/rhsmcertd_worker.py index 8ca4f39582..925bd0cd39 100644 --- a/src/subscription_manager/scripts/rhsmcertd_worker.py +++ b/src/subscription_manager/scripts/rhsmcertd_worker.py @@ -288,15 +288,20 @@ def _auto_register_anonymous(uep: "UEPConnection", token: Dict[str, str]) -> Non log.debug(report) return except connection.RateLimitExceededException as exc: - if exc.headers.get("Retry-After", None) is None: + if exc.retry_after is None: + log.warning( + "Server did not include Retry-After header in rate-limited response. " + f"headers={exc.headers}" + ) raise - delay = int(exc.headers["Retry-After"]) + delay = exc.retry_after log.debug( f"Got response with status code {exc.code} and Retry-After header, " f"will try again in {delay} seconds." ) time.sleep(delay) - except Exception: + except Exception as exc: + log.warning(f"Anonymous registration failed, server returned {exc}.") raise # In theory, this should not happen, it means that something has gone wrong server-side. diff --git a/test/rhsm/unit/test_connection.py b/test/rhsm/unit/test_connection.py index 90c750bb26..e57da87a91 100644 --- a/test/rhsm/unit/test_connection.py +++ b/test/rhsm/unit/test_connection.py @@ -871,6 +871,18 @@ def test_429_body(self): else: self.fail("Should have raised a RateLimitExceededException") + def test_429_weird_case(self): + content = '{"errors": ["TooFast"]}' + headers = {"RETry-aFteR": 20} + try: + self.vr("429", content, headers) + except RateLimitExceededException as e: + self.assertEqual(20, e.retry_after) + self.assertEqual("TooFast, retry access after: 20 seconds.", e.msg) + self.assertEqual("429", e.code) + else: + self.fail("Should have raised a RateLimitExceededException") + def test_500_empty(self): try: self.vr("500", "")