Skip to content

Commit f368f07

Browse files
committed
Merge pull request #929 from docker/927-config-parsing
Don't break when parsing unknown config keys
2 parents 575305f + dc198be commit f368f07

File tree

2 files changed

+48
-4
lines changed

2 files changed

+48
-4
lines changed

docker/auth/auth.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def resolve_repository_name(repo_name):
4646

4747
def resolve_index_name(index_name):
4848
index_name = convert_to_hostname(index_name)
49-
if index_name == 'index.'+INDEX_NAME:
49+
if index_name == 'index.' + INDEX_NAME:
5050
index_name = INDEX_NAME
5151
return index_name
5252

@@ -102,19 +102,34 @@ def encode_header(auth):
102102
return base64.urlsafe_b64encode(auth_json)
103103

104104

105-
def parse_auth(entries):
105+
def parse_auth(entries, raise_on_error=False):
106106
"""
107107
Parses authentication entries
108108
109109
Args:
110-
entries: Dict of authentication entries.
110+
entries: Dict of authentication entries.
111+
raise_on_error: If set to true, an invalid format will raise
112+
InvalidConfigFile
111113
112114
Returns:
113115
Authentication registry.
114116
"""
115117

116118
conf = {}
117119
for registry, entry in six.iteritems(entries):
120+
if not (isinstance(entry, dict) and 'auth' in entry):
121+
log.debug(
122+
'Config entry for key {0} is not auth config'.format(registry)
123+
)
124+
# We sometimes fall back to parsing the whole config as if it was
125+
# the auth config by itself, for legacy purposes. In that case, we
126+
# fail silently and return an empty conf if any of the keys is not
127+
# formatted properly.
128+
if raise_on_error:
129+
raise errors.InvalidConfigFile(
130+
'Invalid configuration for registry {0}'.format(registry)
131+
)
132+
return {}
118133
username, password = decode_auth(entry['auth'])
119134
log.debug(
120135
'Found entry (registry={0}, username={1})'
@@ -170,7 +185,7 @@ def load_config(config_path=None):
170185
res = {}
171186
if data.get('auths'):
172187
log.debug("Found 'auths' section")
173-
res.update(parse_auth(data['auths']))
188+
res.update(parse_auth(data['auths'], raise_on_error=True))
174189
if data.get('HttpHeaders'):
175190
log.debug("Found 'HttpHeaders' section")
176191
res.update({'HttpHeaders': data['HttpHeaders']})

tests/unit/auth_test.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,3 +433,32 @@ def test_load_config_custom_config_env_with_headers(self):
433433

434434
self.assertEqual(cfg['Name'], 'Spike')
435435
self.assertEqual(cfg['Surname'], 'Spiegel')
436+
437+
def test_load_config_unknown_keys(self):
438+
folder = tempfile.mkdtemp()
439+
self.addCleanup(shutil.rmtree, folder)
440+
dockercfg_path = os.path.join(folder, 'config.json')
441+
config = {
442+
'detachKeys': 'ctrl-q, ctrl-u, ctrl-i'
443+
}
444+
with open(dockercfg_path, 'w') as f:
445+
json.dump(config, f)
446+
447+
cfg = auth.load_config(dockercfg_path)
448+
assert cfg == {}
449+
450+
def test_load_config_invalid_auth_dict(self):
451+
folder = tempfile.mkdtemp()
452+
self.addCleanup(shutil.rmtree, folder)
453+
dockercfg_path = os.path.join(folder, 'config.json')
454+
config = {
455+
'auths': {
456+
'scarlet.net': {'sakuya': 'izayoi'}
457+
}
458+
}
459+
with open(dockercfg_path, 'w') as f:
460+
json.dump(config, f)
461+
462+
self.assertRaises(
463+
errors.InvalidConfigFile, auth.load_config, dockercfg_path
464+
)

0 commit comments

Comments
 (0)