From 3ba02b9cec58e3a50d8cff44356a5dfbd9658179 Mon Sep 17 00:00:00 2001 From: lethliel Date: Thu, 29 Aug 2019 14:27:30 +0200 Subject: [PATCH] Refactor user extraction Implement _extract_user_compat() for gnomekeyring special case. --- osc/conf.py | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/osc/conf.py b/osc/conf.py index 7e9ec9d5..9b518f0b 100644 --- a/osc/conf.py +++ b/osc/conf.py @@ -694,10 +694,8 @@ def config_set_option(section, opt, val=None, delete=False, update=True, creds_m run = False if val: if opt == 'pass': - user = cp.get(section, 'user') creds_mgr = _get_credentials_manager(section, cp) - if user is None and hasattr(creds_mgr, 'get_user'): - user = creds_mgr.get_user(section) + user = _extract_user_compat(cp, section, creds_mgr) old_pw = creds_mgr.get_password(section, user, defer=False) try: creds_mgr.delete_password(section, user) @@ -721,10 +719,8 @@ def config_set_option(section, opt, val=None, delete=False, update=True, creds_m run = True elif delete and (cp.has_option(section, opt) or opt == 'pass'): if opt == 'pass': - user = cp.get(section, 'user') creds_mgr = _get_credentials_manager(section, cp) - if user is None and hasattr(creds_mgr, 'get_user'): - user = creds_mgr.get_user(section) + user = _extract_user_compar(cp, section, creds_mgr) creds_mgr.delete_password(section, user) else: cp.remove_option(section, opt) @@ -740,6 +736,16 @@ def config_set_option(section, opt, val=None, delete=False, update=True, creds_m return (opt, cp.get(section, opt, raw=True)) return (opt, None) +def _extract_user_compat(cp, section, creds_mgr): + """ + This extracts the user either from the ConfigParser or + the creds_mgr. Only needed for deprecated Gnome Keyring + """ + user = cp.get(section, 'user') + if user is None and hasattr(creds_mgr, 'get_user'): + user = creds_mgr.get_user(section) + return user + def write_initial_config(conffile, entries, custom_template='', creds_mgr_descriptor=None): """ write osc's intial configuration file. entries is a dict which contains values @@ -878,14 +884,11 @@ def get_config(override_conffile=None, # backward compatiblity scheme, host, path = parse_apisrv_url(config.get('scheme', 'https'), url) apiurl = urljoin(scheme, host, path) - user = cp.get(url, 'user', raw=True) creds_mgr = _get_credentials_manager(url, cp) - # currently, this is only needed for the deprecated gnomekeyring - actually, we - # we should use the apiurl instead of url (that's what the old code did), but - # this makes things more complex (also, it is very unlikely that url and - # apiurl differ) - if user is None and hasattr(creds_mgr, 'get_user'): - user = creds_mgr.get_user(url) + # if the deprecated gnomekeyring is used we should use the apiurl instead of url + # (that's what the old code did), but this makes things more complex + # (also, it is very unlikely that url and apiurl differ) + user = _extract_user_compat(cp, url, creds_mgr) if user is None: raise oscerr.ConfigMissingCredentialsError('No user found in section %s' % url, conffile, url) password = creds_mgr.get_password(url, user)