diff options
author | Joffrey F <joffrey@docker.com> | 2018-01-30 18:22:41 -0800 |
---|---|---|
committer | Joffrey F <joffrey@docker.com> | 2018-01-30 18:42:38 -0800 |
commit | ccbde11c8dbb8773aed84abac92c6361b8e11229 (patch) | |
tree | 8bf4e02458dded8990a80a8e6bdcb3a1cdea3532 /docker/auth.py | |
parent | 75e2e8ad816ac35e1c4928f6b3f9e40841ca493c (diff) | |
download | docker-py-improve_authconfig_genconfig_separation.tar.gz |
Improve separation between auth_configs and general_configsimprove_authconfig_genconfig_separation
Signed-off-by: Joffrey F <joffrey@docker.com>
Diffstat (limited to 'docker/auth.py')
-rw-r--r-- | docker/auth.py | 75 |
1 files changed, 41 insertions, 34 deletions
diff --git a/docker/auth.py b/docker/auth.py index 79f63cc..91be2b8 100644 --- a/docker/auth.py +++ b/docker/auth.py @@ -98,11 +98,12 @@ def resolve_authconfig(authconfig, registry=None): registry = resolve_index_name(registry) if registry else INDEX_NAME log.debug("Looking for auth entry for {0}".format(repr(registry))) - if registry in authconfig: + authdict = authconfig.get('auths', {}) + if registry in authdict: log.debug("Found {0}".format(repr(registry))) - return authconfig[registry] + return authdict[registry] - for key, conf in six.iteritems(authconfig): + for key, conf in six.iteritems(authdict): if resolve_index_name(key) == registry: log.debug("Found {0}".format(repr(key))) return conf @@ -220,7 +221,7 @@ def parse_auth(entries, raise_on_error=False): return conf -def load_config(config_path=None): +def load_config(config_path=None, config_dict=None): """ Loads authentication data from a Docker configuration file in the given root directory or if config_path is passed use given path. @@ -228,39 +229,45 @@ def load_config(config_path=None): explicit config_path parameter > DOCKER_CONFIG environment variable > ~/.docker/config.json > ~/.dockercfg """ - config_file = config.find_config_file(config_path) - if not config_file: - return {} + if not config_dict: + config_file = config.find_config_file(config_path) + + if not config_file: + return {} + try: + with open(config_file) as f: + config_dict = json.load(f) + except (IOError, KeyError, ValueError) as e: + # Likely missing new Docker config file or it's in an + # unknown format, continue to attempt to read old location + # and format. + log.debug(e) + return _load_legacy_config(config_file) + + res = {} + if config_dict.get('auths'): + log.debug("Found 'auths' section") + res.update({ + 'auths': parse_auth(config_dict.pop('auths'), raise_on_error=True) + }) + if config_dict.get('credsStore'): + log.debug("Found 'credsStore' section") + res.update({'credsStore': config_dict.pop('credsStore')}) + if config_dict.get('credHelpers'): + log.debug("Found 'credHelpers' section") + res.update({'credHelpers': config_dict.pop('credHelpers')}) + if res: + return res + + log.debug( + "Couldn't find auth-related section ; attempting to interpret" + "as auth-only file" + ) + return parse_auth(config_dict) - try: - with open(config_file) as f: - data = json.load(f) - res = {} - if data.get('auths'): - log.debug("Found 'auths' section") - res.update(parse_auth(data['auths'], raise_on_error=True)) - if data.get('HttpHeaders'): - log.debug("Found 'HttpHeaders' section") - res.update({'HttpHeaders': data['HttpHeaders']}) - if data.get('credsStore'): - log.debug("Found 'credsStore' section") - res.update({'credsStore': data['credsStore']}) - if data.get('credHelpers'): - log.debug("Found 'credHelpers' section") - res.update({'credHelpers': data['credHelpers']}) - if res: - return res - else: - log.debug("Couldn't find 'auths' or 'HttpHeaders' sections") - f.seek(0) - return parse_auth(json.load(f)) - except (IOError, KeyError, ValueError) as e: - # Likely missing new Docker config file or it's in an - # unknown format, continue to attempt to read old location - # and format. - log.debug(e) +def _load_legacy_config(config_file): log.debug("Attempting to parse legacy auth file format") try: data = [] |