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

Commit 823f44e

Browse files
committed
Add proper GCP config loader and refresher
1 parent 1110248 commit 823f44e

File tree

3 files changed

+367
-16
lines changed

3 files changed

+367
-16
lines changed

Diff for: config/kube_config.py

+71-16
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,21 @@
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
23-
2426
from kubernetes.client import ApiClient, ConfigurationObject, configuration
2527

2628
from .config_exception import ConfigException
29+
from .rfc3339 import tf_from_timestamp, timestamp_from_tf
2730

31+
EXPIRY_SKEW_PREVENTION_DELAY_S = 600
2832
KUBE_CONFIG_DEFAULT_LOCATION = os.environ.get('KUBECONFIG', '~/.kube/config')
2933
_temp_files = {}
3034

@@ -54,6 +58,17 @@ def _create_temp_file_with_content(content):
5458
return name
5559

5660

61+
def _is_expired(expiry):
62+
tf = tf_from_timestamp(expiry)
63+
n = time.time()
64+
return tf + EXPIRY_SKEW_PREVENTION_DELAY_S <= n
65+
66+
67+
def _datetime_to_rfc3339(dt):
68+
tf = (dt - datetime.datetime.utcfromtimestamp(0)).total_seconds()
69+
return timestamp_from_tf(tf, time_offset="Z")
70+
71+
5772
class FileOrData(object):
5873
"""Utility class to read content of obj[%data_key_name] or file's
5974
content of obj[%file_key_name] and represent it as file or data.
@@ -110,19 +125,26 @@ class KubeConfigLoader(object):
110125
def __init__(self, config_dict, active_context=None,
111126
get_google_credentials=None,
112127
client_configuration=configuration,
113-
config_base_path=""):
128+
config_base_path="",
129+
config_persister=None):
114130
self._config = ConfigNode('kube-config', config_dict)
115131
self._current_context = None
116132
self._user = None
117133
self._cluster = None
118134
self.set_active_context(active_context)
119135
self._config_base_path = config_base_path
136+
self._config_persister = config_persister
137+
138+
def _refresh_credentials():
139+
credentials, project_id = google.auth.default()
140+
request = google.auth.transport.requests.Request()
141+
credentials.refresh(request)
142+
return credentials
143+
120144
if get_google_credentials:
121145
self._get_google_credentials = get_google_credentials
122146
else:
123-
self._get_google_credentials = lambda: (
124-
GoogleCredentials.get_application_default()
125-
.get_access_token().access_token)
147+
self._get_google_credentials = _refresh_credentials
126148
self._client_configuration = client_configuration
127149

128150
def set_active_context(self, context_name=None):
@@ -166,16 +188,32 @@ def _load_authentication(self):
166188
def _load_gcp_token(self):
167189
if 'auth-provider' not in self._user:
168190
return
169-
if 'name' not in self._user['auth-provider']:
191+
provider = self._user['auth-provider']
192+
if 'name' not in provider:
170193
return
171-
if self._user['auth-provider']['name'] != 'gcp':
194+
if provider['name'] != 'gcp':
172195
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()
196+
197+
if (('config' not in provider) or
198+
('access-token' not in provider['config']) or
199+
('expiry' in provider['config'] and
200+
_is_expired(provider['config']['expiry']))):
201+
# token is not available or expired, refresh it
202+
self._refresh_gcp_token()
203+
204+
self.token = "Bearer %s" % provider['config']['access-token']
177205
return self.token
178206

207+
def _refresh_gcp_token(self):
208+
if 'config' not in self._user['auth-provider']:
209+
self._user['auth-provider'].value['config'] = {}
210+
provider = self._user['auth-provider']['config']
211+
credentials = self._get_google_credentials()
212+
provider.value['access-token'] = credentials.token
213+
provider.value['expiry'] = _datetime_to_rfc3339(credentials.expiry)
214+
if self._config_persister:
215+
self._config_persister(self._config.value)
216+
179217
def _load_user_token(self):
180218
token = FileOrData(
181219
self._user, 'tokenFile', 'token',
@@ -289,6 +327,11 @@ def _get_kube_config_loader_for_yaml_file(filename, **kwargs):
289327
**kwargs)
290328

291329

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

294337
if config_file is None:
@@ -299,7 +342,8 @@ def list_kube_config_contexts(config_file=None):
299342

300343

301344
def load_kube_config(config_file=None, context=None,
302-
client_configuration=configuration):
345+
client_configuration=configuration,
346+
persist_config=True):
303347
"""Loads authentication and cluster information from kube-config file
304348
and stores them in kubernetes.client.configuration.
305349
@@ -308,21 +352,32 @@ def load_kube_config(config_file=None, context=None,
308352
from config file will be used.
309353
:param client_configuration: The kubernetes.client.ConfigurationObject to
310354
set configs to.
355+
:param persist_config: If True and config changed (e.g. GCP token refresh)
356+
the provided config file will be updated.
311357
"""
312358

313359
if config_file is None:
314360
config_file = os.path.expanduser(KUBE_CONFIG_DEFAULT_LOCATION)
315361

362+
config_persister = None
363+
if persist_config:
364+
config_persister = lambda config_map, config_file=config_file: (
365+
_save_kube_config(config_file, config_map))
316366
_get_kube_config_loader_for_yaml_file(
317367
config_file, active_context=context,
318-
client_configuration=client_configuration).load_and_set()
368+
client_configuration=client_configuration,
369+
config_persister=config_persister).load_and_set()
319370

320371

321-
def new_client_from_config(config_file=None, context=None):
372+
def new_client_from_config(
373+
config_file=None,
374+
context=None,
375+
persist_config=True):
322376
"""Loads configuration the same as load_kube_config but returns an ApiClient
323377
to be used with any API object. This will allow the caller to concurrently
324378
talk with multiple clusters."""
325379
client_config = ConfigurationObject()
326380
load_kube_config(config_file=config_file, context=context,
327-
client_configuration=client_config)
381+
client_configuration=client_config,
382+
persist_config=persist_config)
328383
return ApiClient(config=client_config)

Diff for: 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)