1
0
mirror of https://github.com/openSUSE/osc.git synced 2024-12-27 18:26:15 +01:00

Refactor user extraction

Implement _extract_user_compat() for gnomekeyring special case.
This commit is contained in:
lethliel 2019-08-29 14:27:30 +02:00
parent cba4b58bbe
commit 3ba02b9cec

View File

@ -694,10 +694,8 @@ def config_set_option(section, opt, val=None, delete=False, update=True, creds_m
run = False run = False
if val: if val:
if opt == 'pass': if opt == 'pass':
user = cp.get(section, 'user')
creds_mgr = _get_credentials_manager(section, cp) creds_mgr = _get_credentials_manager(section, cp)
if user is None and hasattr(creds_mgr, 'get_user'): user = _extract_user_compat(cp, section, creds_mgr)
user = creds_mgr.get_user(section)
old_pw = creds_mgr.get_password(section, user, defer=False) old_pw = creds_mgr.get_password(section, user, defer=False)
try: try:
creds_mgr.delete_password(section, user) 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 run = True
elif delete and (cp.has_option(section, opt) or opt == 'pass'): elif delete and (cp.has_option(section, opt) or opt == 'pass'):
if opt == 'pass': if opt == 'pass':
user = cp.get(section, 'user')
creds_mgr = _get_credentials_manager(section, cp) creds_mgr = _get_credentials_manager(section, cp)
if user is None and hasattr(creds_mgr, 'get_user'): user = _extract_user_compar(cp, section, creds_mgr)
user = creds_mgr.get_user(section)
creds_mgr.delete_password(section, user) creds_mgr.delete_password(section, user)
else: else:
cp.remove_option(section, opt) 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, cp.get(section, opt, raw=True))
return (opt, None) 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): 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 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 # backward compatiblity
scheme, host, path = parse_apisrv_url(config.get('scheme', 'https'), url) scheme, host, path = parse_apisrv_url(config.get('scheme', 'https'), url)
apiurl = urljoin(scheme, host, path) apiurl = urljoin(scheme, host, path)
user = cp.get(url, 'user', raw=True)
creds_mgr = _get_credentials_manager(url, cp) creds_mgr = _get_credentials_manager(url, cp)
# currently, this is only needed for the deprecated gnomekeyring - actually, we # if the deprecated gnomekeyring is used we should use the apiurl instead of url
# we should use the apiurl instead of url (that's what the old code did), but # (that's what the old code did), but this makes things more complex
# this makes things more complex (also, it is very unlikely that url and # (also, it is very unlikely that url and apiurl differ)
# apiurl differ) user = _extract_user_compat(cp, url, creds_mgr)
if user is None and hasattr(creds_mgr, 'get_user'):
user = creds_mgr.get_user(url)
if user is None: if user is None:
raise oscerr.ConfigMissingCredentialsError('No user found in section %s' % url, conffile, url) raise oscerr.ConfigMissingCredentialsError('No user found in section %s' % url, conffile, url)
password = creds_mgr.get_password(url, user) password = creds_mgr.get_password(url, user)