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

raise exception when an empty config file is passed to load_kube_config #223

Merged
merged 3 commits into from
Feb 9, 2021
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
8 changes: 8 additions & 0 deletions config/kube_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,9 @@ def _load_config_from_file_like_object(self, string):
else:
config = yaml.safe_load(string.read())

if config is None:
raise ConfigException(
'Invalid kube-config.')
if self.config_merged is None:
self.config_merged = copy.deepcopy(config)
# doesn't need to do any further merging
Expand All @@ -699,6 +702,11 @@ def load_config(self, path):
with open(path) as f:
config = yaml.safe_load(f)

if config is None:
raise ConfigException(
'Invalid kube-config. '
'%s file is empty' % path)

if self.config_merged is None:
config_merged = copy.deepcopy(config)
for item in ('clusters', 'contexts', 'users'):
Expand Down
15 changes: 15 additions & 0 deletions config/kube_config_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1290,6 +1290,21 @@ def test_load_kube_config_from_dict(self):
client_configuration=actual)
self.assertEqual(expected, actual)

def test_load_kube_config_from_empty_file_like_object(self):
config_file_like_object = io.StringIO()
self.assertRaises(
ConfigException,
load_kube_config,
config_file_like_object)

def test_load_kube_config_from_empty_file(self):
config_file = self._create_temp_file(
yaml.safe_dump(None))
self.assertRaises(
ConfigException,
load_kube_config,
config_file)

def test_list_kube_config_contexts(self):
config_file = self._create_temp_file(
yaml.safe_dump(self.TEST_KUBE_CONFIG))
Expand Down