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

Commit 253d937

Browse files
committed
Add proper GCP config loader and refresher
1 parent 1110248 commit 253d937

File tree

4 files changed

+413
-21
lines changed

4 files changed

+413
-21
lines changed

config/kube_config.py

+71-15
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,22 @@
1414

1515
import atexit
1616
import base64
17+
import datetime
1718
import os
1819
import tempfile
20+
import time
1921

22+
import google.auth
23+
import google.auth.transport.requests
2024
import urllib3
2125
import yaml
22-
from google.oauth2.credentials import Credentials
2326

2427
from kubernetes.client import ApiClient, ConfigurationObject, configuration
2528

2629
from .config_exception import ConfigException
30+
from .rfc3339 import tf_from_timestamp, timestamp_from_tf
2731

32+
EXPIRY_SKEW_PREVENTION_DELAY_S = 600
2833
KUBE_CONFIG_DEFAULT_LOCATION = os.environ.get('KUBECONFIG', '~/.kube/config')
2934
_temp_files = {}
3035

@@ -54,6 +59,17 @@ def _create_temp_file_with_content(content):
5459
return name
5560

5661

62+
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")
71+
72+
5773
class FileOrData(object):
5874
"""Utility class to read content of obj[%data_key_name] or file's
5975
content of obj[%file_key_name] and represent it as file or data.
@@ -110,19 +126,26 @@ class KubeConfigLoader(object):
110126
def __init__(self, config_dict, active_context=None,
111127
get_google_credentials=None,
112128
client_configuration=configuration,
113-
config_base_path=""):
129+
config_base_path="",
130+
config_persister=None):
114131
self._config = ConfigNode('kube-config', config_dict)
115132
self._current_context = None
116133
self._user = None
117134
self._cluster = None
118135
self.set_active_context(active_context)
119136
self._config_base_path = config_base_path
137+
self._config_persister = config_persister
138+
139+
def _refresh_credentials():
140+
credentials, project_id = google.auth.default()
141+
request = google.auth.transport.requests.Request()
142+
credentials.refresh(request)
143+
return credentials
144+
120145
if get_google_credentials:
121146
self._get_google_credentials = get_google_credentials
122147
else:
123-
self._get_google_credentials = lambda: (
124-
GoogleCredentials.get_application_default()
125-
.get_access_token().access_token)
148+
self._get_google_credentials = _refresh_credentials
126149
self._client_configuration = client_configuration
127150

128151
def set_active_context(self, context_name=None):
@@ -166,16 +189,32 @@ def _load_authentication(self):
166189
def _load_gcp_token(self):
167190
if 'auth-provider' not in self._user:
168191
return
169-
if 'name' not in self._user['auth-provider']:
192+
provider = self._user['auth-provider']
193+
if 'name' not in provider:
170194
return
171-
if self._user['auth-provider']['name'] != 'gcp':
195+
if provider['name'] != 'gcp':
172196
return
173-
# Ignore configs in auth-provider and rely on GoogleCredentials
174-
# caching and refresh mechanism.
175-
# TODO: support gcp command based token ("cmd-path" config).
176-
self.token = "Bearer %s" % self._get_google_credentials()
197+
198+
if (('config' not in provider) or
199+
('access-token' not in provider['config']) or
200+
('expiry' in provider['config'] and
201+
_is_expired(provider['config']['expiry']))):
202+
# token is not available or expired, refresh it
203+
self._refresh_gcp_token()
204+
205+
self.token = "Bearer %s" % provider['config']['access-token']
177206
return self.token
178207

208+
def _refresh_gcp_token(self):
209+
if 'config' not in self._user['auth-provider']:
210+
self._user['auth-provider'].value['config'] = {}
211+
provider = self._user['auth-provider']['config']
212+
credentials = self._get_google_credentials()
213+
provider.value['access-token'] = credentials.token
214+
provider.value['expiry'] = _datetime_to_rfc3339(credentials.expiry)
215+
if self._config_persister:
216+
self._config_persister(self._config.value)
217+
179218
def _load_user_token(self):
180219
token = FileOrData(
181220
self._user, 'tokenFile', 'token',
@@ -289,6 +328,11 @@ def _get_kube_config_loader_for_yaml_file(filename, **kwargs):
289328
**kwargs)
290329

291330

331+
def _save_kube_config(filename, config_map):
332+
with open(filename, 'w') as f:
333+
yaml.safe_dump(config_map, f, default_flow_style=False)
334+
335+
292336
def list_kube_config_contexts(config_file=None):
293337

294338
if config_file is None:
@@ -299,7 +343,8 @@ def list_kube_config_contexts(config_file=None):
299343

300344

301345
def load_kube_config(config_file=None, context=None,
302-
client_configuration=configuration):
346+
client_configuration=configuration,
347+
persist_config=True):
303348
"""Loads authentication and cluster information from kube-config file
304349
and stores them in kubernetes.client.configuration.
305350
@@ -308,21 +353,32 @@ def load_kube_config(config_file=None, context=None,
308353
from config file will be used.
309354
:param client_configuration: The kubernetes.client.ConfigurationObject to
310355
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.
311358
"""
312359

313360
if config_file is None:
314361
config_file = os.path.expanduser(KUBE_CONFIG_DEFAULT_LOCATION)
315362

363+
config_persister = None
364+
if persist_config:
365+
config_persister = lambda config_map, config_file=config_file: (
366+
_save_kube_config(config_file, config_map))
316367
_get_kube_config_loader_for_yaml_file(
317368
config_file, active_context=context,
318-
client_configuration=client_configuration).load_and_set()
369+
client_configuration=client_configuration,
370+
config_persister=config_persister).load_and_set()
319371

320372

321-
def new_client_from_config(config_file=None, context=None):
373+
def new_client_from_config(
374+
config_file=None,
375+
context=None,
376+
persist_config=True):
322377
"""Loads configuration the same as load_kube_config but returns an ApiClient
323378
to be used with any API object. This will allow the caller to concurrently
324379
talk with multiple clusters."""
325380
client_config = ConfigurationObject()
326381
load_kube_config(config_file=config_file, context=context,
327-
client_configuration=client_config)
382+
client_configuration=client_config,
383+
persist_config=persist_config)
328384
return ApiClient(config=client_config)

config/kube_config_test.py

+46-6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414

1515
import base64
16+
import datetime
1617
import os
1718
import shutil
1819
import tempfile
@@ -26,6 +27,7 @@
2627
_cleanup_temp_files, _create_temp_file_with_content,
2728
list_kube_config_contexts, load_kube_config,
2829
new_client_from_config)
30+
from .rfc3339 import timestamp_from_tf
2931

3032
BEARER_TOKEN_FORMAT = "Bearer %s"
3133

@@ -304,6 +306,13 @@ class TestKubeConfigLoader(BaseTestCase):
304306
"user": "gcp"
305307
}
306308
},
309+
{
310+
"name": "expired_gcp",
311+
"context": {
312+
"cluster": "default",
313+
"user": "expired_gcp"
314+
}
315+
},
307316
{
308317
"name": "user_pass",
309318
"context": {
@@ -397,7 +406,24 @@ class TestKubeConfigLoader(BaseTestCase):
397406
"user": {
398407
"auth-provider": {
399408
"name": "gcp",
400-
"access_token": "not_used",
409+
"config": {
410+
"access-token": TEST_DATA_BASE64,
411+
}
412+
},
413+
"token": TEST_DATA_BASE64, # should be ignored
414+
"username": TEST_USERNAME, # should be ignored
415+
"password": TEST_PASSWORD, # should be ignored
416+
}
417+
},
418+
{
419+
"name": "expired_gcp",
420+
"user": {
421+
"auth-provider": {
422+
"name": "gcp",
423+
"config": {
424+
"access-token": TEST_DATA_BASE64,
425+
"expiry": timestamp_from_tf(0),
426+
}
401427
},
402428
"token": TEST_DATA_BASE64, # should be ignored
403429
"username": TEST_USERNAME, # should be ignored
@@ -464,24 +490,38 @@ def test_load_user_token(self):
464490
self.assertTrue(loader._load_user_token())
465491
self.assertEqual(BEARER_TOKEN_FORMAT % TEST_DATA_BASE64, loader.token)
466492

467-
def test_gcp(self):
493+
def test_gcp_no_refresh(self):
468494
expected = FakeConfig(
469495
host=TEST_HOST,
470-
token=BEARER_TOKEN_FORMAT % TEST_ANOTHER_DATA_BASE64)
496+
token=BEARER_TOKEN_FORMAT % TEST_DATA_BASE64)
471497
actual = FakeConfig()
472498
KubeConfigLoader(
473499
config_dict=self.TEST_KUBE_CONFIG,
474500
active_context="gcp",
475501
client_configuration=actual,
476-
get_google_credentials=lambda: TEST_ANOTHER_DATA_BASE64) \
502+
get_google_credentials=lambda: "SHOULD NOT BE CALLED") \
477503
.load_and_set()
478504
self.assertEqual(expected, actual)
479505

480-
def test_load_gcp_token(self):
506+
def test_load_gcp_token_no_refresh(self):
481507
loader = KubeConfigLoader(
482508
config_dict=self.TEST_KUBE_CONFIG,
483509
active_context="gcp",
484-
get_google_credentials=lambda: TEST_ANOTHER_DATA_BASE64)
510+
get_google_credentials=lambda: "SHOULD NOT BE CALLED")
511+
self.assertTrue(loader._load_gcp_token())
512+
self.assertEqual(BEARER_TOKEN_FORMAT % TEST_DATA_BASE64,
513+
loader.token)
514+
515+
def test_load_gcp_token_with_refresh(self):
516+
517+
def cred(): return None
518+
cred.token = TEST_ANOTHER_DATA_BASE64
519+
cred.expiry = datetime.datetime.now()
520+
521+
loader = KubeConfigLoader(
522+
config_dict=self.TEST_KUBE_CONFIG,
523+
active_context="expired_gcp",
524+
get_google_credentials=lambda: cred)
485525
self.assertTrue(loader._load_gcp_token())
486526
self.assertEqual(BEARER_TOKEN_FORMAT % TEST_ANOTHER_DATA_BASE64,
487527
loader.token)

config/rfc3339.MD

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The (rfc3339.py)[rfc3339.py] file is copied from [this site](http://home.blarg.net/~steveha/pyfeed.html) because PyFeed is not available in PyPi.

0 commit comments

Comments
 (0)