Skip to content
This repository was archived by the owner on Mar 13, 2022. It is now read-only.

Commit 50f717d

Browse files
committed
Implement rfc3339 balanced formatter/parser for kube-config use
1 parent 253d937 commit 50f717d

File tree

4 files changed

+87
-311
lines changed

4 files changed

+87
-311
lines changed

config/dateutil.py

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# Copyright 2017 The Kubernetes Authors.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import datetime
16+
import math
17+
import re
18+
19+
20+
class TimezoneInfo(datetime.tzinfo):
21+
def __init__(self, h, m):
22+
self._name = "UTC"
23+
if h != 0 and m != 0:
24+
self._name += "%+03d:%2d" % (h, m)
25+
self._delta = datetime.timedelta(hours=h, minutes=math.copysign(m, h))
26+
27+
def utcoffset(self, dt):
28+
return self._delta
29+
30+
def tzname(self, dt):
31+
return self._name
32+
33+
def dst(self, dt):
34+
return datetime.timedelta(0)
35+
36+
37+
UTC = TimezoneInfo(0, 0)
38+
39+
# ref https://www.ietf.org/rfc/rfc3339.txt
40+
_re_rfc3339 = re.compile(r"(\d\d\d\d)-(\d\d)-(\d\d)" # Full time
41+
r"[ Tt]" # Separator
42+
r"(\d\d):(\d\d):(\d\d)([.,]\d+)?" # partial-time
43+
r"([zZ ]|[-+]\d\d?:\d\d)?", # time-offset
44+
re.VERBOSE + re.IGNORECASE)
45+
_re_timezone = re.compile(r"([-+])(\d\d?):?(\d\d)?")
46+
47+
48+
def parse_rfc3339(s):
49+
if type(s) == datetime.datetime:
50+
# no need to parse it, just make sure it has a timezone.
51+
if not s.tzinfo:
52+
return s.replace(tzinfo=UTC)
53+
return s
54+
groups = _re_rfc3339.search(s).groups()
55+
dt = [0] * 7
56+
for x in range(6):
57+
dt[x] = int(groups[x])
58+
if groups[6] is not None:
59+
dt[6] = int(groups[6])
60+
tz = UTC
61+
if groups[7] is not None and groups[7] != 'Z' and groups[7] != 'z':
62+
tz_groups = _re_timezone.search(groups[7]).groups()
63+
hour = int(tz_groups[1])
64+
minute = 0
65+
if tz_groups[0] == "-":
66+
hour *= -1
67+
if tz_groups[2]:
68+
minute = int(tz_groups[2])
69+
tz = TimezoneInfo(hour, minute)
70+
return datetime.datetime(
71+
year=dt[0], month=dt[1], day=dt[2],
72+
hour=dt[3], minute=dt[4], second=dt[5],
73+
microsecond=dt[6], tzinfo=tz)
74+
75+
76+
def format_rfc3339(date_time):
77+
if date_time.tzinfo is None:
78+
date_time = date_time.replace(tzinfo=UTC)
79+
date_time = date_time.astimezone(UTC)
80+
return date_time.strftime('%Y-%m-%dT%H:%M:%SZ')

config/kube_config.py

+7-15
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,17 @@
1717
import datetime
1818
import os
1919
import tempfile
20-
import time
2120

2221
import google.auth
2322
import google.auth.transport.requests
2423
import urllib3
2524
import yaml
2625

2726
from kubernetes.client import ApiClient, ConfigurationObject, configuration
28-
27+
from .dateutil import parse_rfc3339, format_rfc3339, UTC
2928
from .config_exception import ConfigException
30-
from .rfc3339 import tf_from_timestamp, timestamp_from_tf
3129

32-
EXPIRY_SKEW_PREVENTION_DELAY_S = 600
30+
EXPIRY_SKEW_PREVENTION_DELAY = datetime.timedelta(minutes=5)
3331
KUBE_CONFIG_DEFAULT_LOCATION = os.environ.get('KUBECONFIG', '~/.kube/config')
3432
_temp_files = {}
3533

@@ -60,14 +58,8 @@ def _create_temp_file_with_content(content):
6058

6159

6260
def _is_expired(expiry):
63-
tf = tf_from_timestamp(expiry)
64-
n = time.time()
65-
return tf + EXPIRY_SKEW_PREVENTION_DELAY_S <= n
66-
67-
68-
def _datetime_to_rfc3339(dt):
69-
tf = (dt - datetime.datetime.utcfromtimestamp(0)).total_seconds()
70-
return timestamp_from_tf(tf, time_offset="Z")
61+
return ((parse_rfc3339(expiry) + EXPIRY_SKEW_PREVENTION_DELAY) <=
62+
datetime.datetime.utcnow().replace(tzinfo=UTC))
7163

7264

7365
class FileOrData(object):
@@ -211,7 +203,7 @@ def _refresh_gcp_token(self):
211203
provider = self._user['auth-provider']['config']
212204
credentials = self._get_google_credentials()
213205
provider.value['access-token'] = credentials.token
214-
provider.value['expiry'] = _datetime_to_rfc3339(credentials.expiry)
206+
provider.value['expiry'] = format_rfc3339(credentials.expiry)
215207
if self._config_persister:
216208
self._config_persister(self._config.value)
217209

@@ -353,8 +345,8 @@ def load_kube_config(config_file=None, context=None,
353345
from config file will be used.
354346
:param client_configuration: The kubernetes.client.ConfigurationObject to
355347
set configs to.
356-
:param persist_config: If True and config changed (e.g. GCP token refresh)
357-
the provided config file will be updated.
348+
:param persist_config: IF True, config file will be updated when changed
349+
(e.g GCP token refresh).
358350
"""
359351

360352
if config_file is None:

config/rfc3339.MD

-1
This file was deleted.

0 commit comments

Comments
 (0)