Skip to content

Commit 6ac7c42

Browse files
committed
rework error holddown based on comments from @c00kiemon5ter
1 parent a850657 commit 6ac7c42

File tree

2 files changed

+20
-24
lines changed

2 files changed

+20
-24
lines changed

src/cryptojwt/key_bundle.py

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ def __init__(
157157
keys=None,
158158
source="",
159159
cache_time=300,
160-
error_holddown=0,
160+
ignore_errors_period=0,
161161
fileformat="jwks",
162162
keytype="RSA",
163163
keyusage=None,
@@ -190,7 +190,8 @@ def __init__(
190190
self.remote = False
191191
self.local = False
192192
self.cache_time = cache_time
193-
self.error_holddown = error_holddown
193+
self.ignore_errors_period = ignore_errors_period
194+
self.ignore_errors_until = None # UNIX timestamp of last error
194195
self.time_out = 0
195196
self.etag = ""
196197
self.source = None
@@ -201,7 +202,6 @@ def __init__(
201202
self.last_updated = 0
202203
self.last_remote = None # HTTP Date of last remote update
203204
self.last_local = None # UNIX timestamp of last local update
204-
self.last_error = None # UNIX timestamp of last error
205205

206206
if httpc:
207207
self.httpc = httpc
@@ -369,15 +369,13 @@ def do_remote(self):
369369
# if self.verify_ssl is not None:
370370
# self.httpc_params["verify"] = self.verify_ssl
371371

372-
if self.last_error:
373-
t = self.last_error + self.error_holddown
374-
if time.time() < t:
375-
LOGGER.warning(
376-
"Not reading remote JWKS from %s (in error holddown until %s)",
377-
self.source,
378-
datetime.fromtimestamp(t),
379-
)
380-
return False
372+
if self.ignore_errors_until and time.time() < self.ignore_errors_until:
373+
LOGGER.warning(
374+
"Not reading remote JWKS from %s (in error holddown until %s)",
375+
self.source,
376+
datetime.fromtimestamp(self.ignore_errors_until),
377+
)
378+
return False
381379

382380
LOGGER.info("Reading remote JWKS from %s", self.source)
383381
try:
@@ -404,7 +402,7 @@ def do_remote(self):
404402
self.do_keys(self.imp_jwks["keys"])
405403
except KeyError:
406404
LOGGER.error("No 'keys' keyword in JWKS")
407-
self.last_error = time.time()
405+
self.ignore_errors_until = time.time() + self.ignore_errors_period
408406
raise UpdateFailed(MALFORMED.format(self.source))
409407

410408
if hasattr(_http_resp, "headers"):
@@ -417,15 +415,13 @@ def do_remote(self):
417415

418416
else:
419417
LOGGER.warning(
420-
"HTTP status %d reading remote JWKS from %s",
421-
_http_resp.status_code,
422-
self.source,
418+
"HTTP status %d reading remote JWKS from %s", _http_resp.status_code, self.source,
423419
)
424-
self.last_error = time.time()
420+
self.ignore_errors_until = time.time() + self.ignore_errors_period
425421
raise UpdateFailed(REMOTE_FAILED.format(self.source, _http_resp.status_code))
426422

427423
self.last_updated = time.time()
428-
self.last_error = None
424+
self.ignore_errors_until = None
429425
return True
430426

431427
def _parse_remote_response(self, response):

tests/test_03_key_bundle.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,10 +1027,10 @@ def test_remote_not_modified():
10271027
assert kb2.last_updated
10281028

10291029

1030-
def test_error_holddown():
1030+
def test_ignore_errors_period():
10311031
source_good = "https://example.com/keys.json"
10321032
source_bad = "https://example.com/keys-bad.json"
1033-
error_holddown = 1
1033+
ignore_errors_period = 1
10341034
# Mock response
10351035
with responses.RequestsMock() as rsps:
10361036
rsps.add(method="GET", url=source_good, json=JWKS_DICT, status=200)
@@ -1040,11 +1040,11 @@ def test_error_holddown():
10401040
source=source_good,
10411041
httpc=requests.request,
10421042
httpc_params=httpc_params,
1043-
error_holddown=error_holddown,
1043+
ignore_errors_period=ignore_errors_period,
10441044
)
10451045
res = kb.do_remote()
10461046
assert res == True
1047-
assert kb.last_error is None
1047+
assert kb.ignore_errors_until is None
10481048

10491049
# refetch, but fail by using a bad source
10501050
kb.source = source_bad
@@ -1055,11 +1055,11 @@ def test_error_holddown():
10551055

10561056
# retry should fail silently as we're in holddown
10571057
res = kb.do_remote()
1058-
assert kb.last_error is not None
1058+
assert kb.ignore_errors_until is not None
10591059
assert res == False
10601060

10611061
# wait until holddown
1062-
time.sleep(error_holddown + 1)
1062+
time.sleep(ignore_errors_period + 1)
10631063

10641064
# try again
10651065
kb.source = source_good

0 commit comments

Comments
 (0)