From 862f48aa929cb898ec184c5e7f284e9b54674d63 Mon Sep 17 00:00:00 2001 From: Flynn Date: Fri, 16 Aug 2024 15:35:39 -0400 Subject: [PATCH] Clean up range checking in format_duration. Signed-off-by: Flynn --- kubernetes/utils/duration.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/kubernetes/utils/duration.py b/kubernetes/utils/duration.py index 326d11a6c..cacb7be05 100644 --- a/kubernetes/utils/duration.py +++ b/kubernetes/utils/duration.py @@ -21,6 +21,9 @@ import durationpy # really be a big deal. reDuration = re.compile(r'^([0-9]{1,5}(h|m|s|ms)){1,4}$') +# maxDuration_ms is the maximum duration that GEP-2257 can support, in milliseconds. +maxDuration_ms = (((99999 * 3600) + (59 * 60) + 59) * 1_000) + 999 + def parse_duration(duration) -> datetime.timedelta: """ Parse GEP-2257 Duration format to a datetime.timedelta object. @@ -65,6 +68,13 @@ def format_duration(delta: datetime.timedelta) -> str: if delta == datetime.timedelta(0): return "0s" + # Check range early. + if delta < datetime.timedelta(0): + raise ValueError("Cannot express negative durations in GEP-2257: {}".format(delta)) + + if delta > datetime.timedelta(milliseconds=maxDuration_ms): + raise ValueError("Cannot express durations longer than 99999h59m59s999ms in GEP-2257: {}".format(delta)) + # durationpy.to_str() is happy to use floating-point seconds, which # GEP-2257 is _not_ happy with. So start by peeling off any microseconds # from our delta. @@ -90,8 +100,5 @@ def format_duration(delta: datetime.timedelta) -> str: delta_str += f"{delta_ms}ms" - if not reDuration.match(delta_str): - raise ValueError("Invalid duration format: {}".format(durationpy.to_str(delta))) - return delta_str