Skip to content

Commit 9b8ef2c

Browse files
committed
fix Issue #2418: parse_rfc3339 calling .groups() on None
1 parent c330b84 commit 9b8ef2c

File tree

2 files changed

+59
-22
lines changed

2 files changed

+59
-22
lines changed

kubernetes/base/config/dateutil.py

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -53,29 +53,37 @@ def parse_rfc3339(s):
5353
if not s.tzinfo:
5454
return s.replace(tzinfo=UTC)
5555
return s
56-
groups = _re_rfc3339.search(s).groups()
57-
dt = [0] * 7
58-
for x in range(6):
59-
dt[x] = int(groups[x])
60-
us = 0
61-
if groups[6] is not None:
62-
partial_sec = float(groups[6].replace(",", "."))
63-
us = int(MICROSEC_PER_SEC * partial_sec)
64-
tz = UTC
65-
if groups[7] is not None and groups[7] != 'Z' and groups[7] != 'z':
66-
tz_groups = _re_timezone.search(groups[7]).groups()
67-
hour = int(tz_groups[1])
68-
minute = 0
69-
if tz_groups[0] == "-":
70-
hour *= -1
71-
if tz_groups[2]:
72-
minute = int(tz_groups[2])
73-
tz = TimezoneInfo(hour, minute)
74-
return datetime.datetime(
75-
year=dt[0], month=dt[1], day=dt[2],
76-
hour=dt[3], minute=dt[4], second=dt[5],
77-
microsecond=us, tzinfo=tz)
7856

57+
match = _re_rfc3339.search(s)
58+
59+
if match is None:
60+
raise ValueError(f"Error in RFC339 Date Formatting {s}")
61+
try:
62+
groups = match.groups()
63+
64+
dt = [0] * 7
65+
for x in range(6):
66+
dt[x] = int(groups[x])
67+
us = 0
68+
if groups[6] is not None:
69+
partial_sec = float(groups[6].replace(",", "."))
70+
us = int(MICROSEC_PER_SEC * partial_sec)
71+
tz = UTC
72+
if groups[7] is not None and groups[7] != 'Z' and groups[7] != 'z':
73+
tz_groups = _re_timezone.search(groups[7]).groups()
74+
hour = int(tz_groups[1])
75+
minute = 0
76+
if tz_groups[0] == "-":
77+
hour *= -1
78+
if tz_groups[2]:
79+
minute = int(tz_groups[2])
80+
tz = TimezoneInfo(hour, minute)
81+
return datetime.datetime(
82+
year=dt[0], month=dt[1], day=dt[2],
83+
hour=dt[3], minute=dt[4], second=dt[5],
84+
microsecond=us, tzinfo=tz)
85+
except Exception:
86+
raise ValueError(f"Error in RFC339 Date Formatting {s}")
7987

8088
def format_rfc3339(date_time):
8189
if date_time.tzinfo is None:

kubernetes/test/test_api_client.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import kubernetes
99
from kubernetes.client.configuration import Configuration
1010
import urllib3
11+
import datetime
12+
from kubernetes.config import dateutil as kube_dateutil
1113

1214
class TestApiClient(unittest.TestCase):
1315

@@ -49,3 +51,30 @@ def test_rest_proxycare(self):
4951
# test
5052
client = kubernetes.client.ApiClient(configuration=config)
5153
self.assertEqual( expected_pool, type(client.rest_client.pool_manager) )
54+
55+
def test_1_parse_rfc3339(self):
56+
dt = datetime.datetime(2023, 1, 1, 12, 0, 0)
57+
result = kube_dateutil.parse_rfc3339(dt)
58+
self.assertIsNotNone(result.tzinfo)
59+
60+
def test_2_parse_rfc3339(self):
61+
"""Test that invalid RFC3339 strings raise ValueError with descriptive message"""
62+
invalid_inputs = [
63+
"invalid-datetime-string",
64+
"",
65+
"not-a-date",
66+
"2023", # incomplete
67+
"random text",
68+
"2023-13-01T12:00:00Z", # invalid month
69+
"not-rfc3339-format"
70+
]
71+
72+
for invalid_input in invalid_inputs:
73+
with self.subTest(input=invalid_input):
74+
with self.assertRaises(ValueError) as context:
75+
kube_dateutil.parse_rfc3339(invalid_input)
76+
77+
# Check that the error message includes the invalid input
78+
error_message = str(context.exception)
79+
self.assertIn("Error in RFC339 Date Formatting", error_message)
80+
self.assertIn(invalid_input, error_message)

0 commit comments

Comments
 (0)