Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Automated cherry pick of #183 #189

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions kubernetes/config/kube_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,18 @@ class FileOrData(object):
content of obj[%file_key_name] and represent it as file or data.
Note that the data is preferred. The obj[%file_key_name] will be used iff
obj['%data_key_name'] is not set or empty. Assumption is file content is
raw data and data field is base64 string."""
raw data and data field is base64 string. The assumption can be changed
with base64_file_content flag. If set to False, the content of the file
will assumed to be base64 and read as is. The default True value will
result in base64 encode of the file content after read."""

def __init__(self, obj, file_key_name, data_key_name=None,
file_base_path=""):
file_base_path="", base64_file_content=True):
if not data_key_name:
data_key_name = file_key_name + "-data"
self._file = None
self._data = None
self._base64_file_content = base64_file_content
if data_key_name in obj:
self._data = obj[data_key_name]
elif file_key_name in obj:
Expand All @@ -78,8 +82,11 @@ def as_file(self):
decoded obj[%data_key_name] content otherwise obj[%file_key_name]."""
use_data_if_no_file = not self._file and self._data
if use_data_if_no_file:
self._file = _create_temp_file_with_content(
base64.decodestring(self._data.encode()))
if self._base64_file_content:
self._file = _create_temp_file_with_content(
base64.decodestring(self._data.encode()))
else:
self._file = _create_temp_file_with_content(self._data)
if self._file and not os.path.isfile(self._file):
raise ConfigException("File does not exists: %s" % self._file)
return self._file
Expand All @@ -90,8 +97,11 @@ def as_data(self):
use_file_if_no_data = not self._data and self._file
if use_file_if_no_data:
with open(self._file) as f:
self._data = bytes.decode(
base64.encodestring(str.encode(f.read())))
if self._base64_file_content:
self._data = bytes.decode(
base64.encodestring(str.encode(f.read())))
else:
self._data = f.read()
return self._data


Expand Down Expand Up @@ -164,7 +174,8 @@ def _load_gcp_token(self):
def _load_user_token(self):
token = FileOrData(
self._user, 'tokenFile', 'token',
file_base_path=self._config_base_path).as_data()
file_base_path=self._config_base_path,
base64_file_content=False).as_data()
if token:
self.token = "Bearer %s" % token
return True
Expand Down
15 changes: 14 additions & 1 deletion kubernetes/config/kube_config_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@ def test_file_given_data(self):
data_key_name=TEST_DATA_KEY)
self.assertEqual(TEST_DATA, self.get_file_content(t.as_file()))

def test_file_given_data_no_base64(self):
obj = {TEST_DATA_KEY: TEST_DATA}
t = FileOrData(obj=obj, file_key_name=TEST_FILE_KEY,
data_key_name=TEST_DATA_KEY, base64_file_content=False)
self.assertEqual(TEST_DATA, self.get_file_content(t.as_file()))

def test_data_given_data(self):
obj = {TEST_DATA_KEY: TEST_DATA_BASE64}
t = FileOrData(obj=obj, file_key_name=TEST_FILE_KEY,
Expand All @@ -120,6 +126,13 @@ def test_data_given_file(self):
t = FileOrData(obj=obj, file_key_name=TEST_FILE_KEY)
self.assertEqual(TEST_DATA_BASE64, t.as_data())

def test_data_given_file_no_base64(self):
obj = {
TEST_FILE_KEY: self._create_temp_file(content=TEST_DATA)}
t = FileOrData(obj=obj, file_key_name=TEST_FILE_KEY,
base64_file_content=False)
self.assertEqual(TEST_DATA, t.as_data())

def test_data_given_file_and_data(self):
obj = {
TEST_DATA_KEY: TEST_DATA_BASE64,
Expand Down Expand Up @@ -562,7 +575,7 @@ def test_ssl_with_relative_ssl_files(self):
with open(os.path.join(temp_dir, "client_key"), "wb") as fd:
fd.write(TEST_CLIENT_KEY.encode())
with open(os.path.join(temp_dir, "token_file"), "wb") as fd:
fd.write(TEST_DATA.encode())
fd.write(TEST_DATA_BASE64.encode())
KubeConfigLoader(
config_dict=self.TEST_KUBE_CONFIG,
active_context="ssl-local-file",
Expand Down