From 65fe685a9050704729993441a276b0e4a0c74e48 Mon Sep 17 00:00:00 2001 From: Alexandre Aubin Date: Tue, 20 Aug 2019 05:41:11 +0200 Subject: [PATCH] Get rid of this 'help' madness and more generally this three-tuppled containing various tuples and weird stuff ... use a simple dict everywhere instead --- moulinette/authenticators/ldap.py | 19 ++++++++----------- moulinette/core.py | 15 ++++++--------- moulinette/interfaces/__init__.py | 15 ++++----------- moulinette/interfaces/api.py | 2 +- moulinette/interfaces/cli.py | 5 +++-- 5 files changed, 22 insertions(+), 34 deletions(-) diff --git a/moulinette/authenticators/ldap.py b/moulinette/authenticators/ldap.py index f3625efc..447cbd77 100644 --- a/moulinette/authenticators/ldap.py +++ b/moulinette/authenticators/ldap.py @@ -33,23 +33,20 @@ class Authenticator(BaseAuthenticator): """ - def __init__(self, name, uri, base_dn, user_rdn=None): + def __init__(self, name, vendor, parameters, extra): + self.uri = parameters["uri"] + self.basedn = parameters["base_dn"] + self.userdn = parameters["user_rdn"] + self.extra = extra logger.debug("initialize authenticator '%s' with: uri='%s', " - "base_dn='%s', user_rdn='%s'", name, uri, base_dn, user_rdn) + "base_dn='%s', user_rdn='%s'", name, self.uri, self.basedn, self.userdn) super(Authenticator, self).__init__(name) - self.uri = uri - self.basedn = base_dn - if user_rdn: - self.userdn = user_rdn - if 'cn=external,cn=auth' in user_rdn: + if self.userdn: + if 'cn=external,cn=auth' in self.userdn: self.authenticate(None) else: self.con = None - else: - # Initialize anonymous usage - self.userdn = '' - self.authenticate(None) def __del__(self): """Disconnect and free ressources""" diff --git a/moulinette/core.py b/moulinette/core.py index c902d4c5..d0e6f9dc 100644 --- a/moulinette/core.py +++ b/moulinette/core.py @@ -287,7 +287,7 @@ class MoulinetteSignals(object): """The list of available signals""" signals = {'authenticate', 'prompt', 'display'} - def authenticate(self, authenticator, help): + def authenticate(self, authenticator): """Process the authentication Attempt to authenticate to the given authenticator and return @@ -297,7 +297,6 @@ class MoulinetteSignals(object): Keyword arguments: - authenticator -- The authenticator object to use - - help -- The translation key of the authenticator's help message Returns: The authenticator object @@ -305,7 +304,7 @@ class MoulinetteSignals(object): """ if authenticator.is_authenticated: return authenticator - return self._authenticate(authenticator, help) + return self._authenticate(authenticator) def prompt(self, message, is_password=False, confirm=False): """Prompt for a value @@ -397,7 +396,7 @@ def init_interface(name, kwargs={}, actionsmap={}): return interface(amap, **kwargs) -def init_authenticator(vendor_and_name, kwargs={}): +def init_authenticator(auth_conf): """Return a new authenticator instance Retrieve the given authenticator vendor and return a new instance of @@ -409,15 +408,13 @@ def init_authenticator(vendor_and_name, kwargs={}): - kwargs -- A dict of arguments for the authenticator profile """ - (vendor, name) = vendor_and_name try: - mod = import_module('moulinette.authenticators.%s' % vendor) + mod = import_module('moulinette.authenticators.%s' % auth_conf["vendor"]) except ImportError: - logger.exception("unable to load authenticator vendor '%s'", vendor) + logger.exception("unable to load authenticator vendor '%s'", auth_conf["vendor"]) raise MoulinetteError('error_see_log') else: - return mod.Authenticator(name, **kwargs) - + return mod.Authenticator(**auth_conf) def clean_session(session_id, profiles=[]): """Clean a session cache diff --git a/moulinette/interfaces/__init__.py b/moulinette/interfaces/__init__.py index c50c390d..813dce71 100644 --- a/moulinette/interfaces/__init__.py +++ b/moulinette/interfaces/__init__.py @@ -266,17 +266,10 @@ class BaseActionsMapParser(object): else: auths = {} for auth_name, auth_conf in auth.items(): - # Add authenticator profile as a 3-tuple - # (identifier, configuration, parameters) with - # - identifier: the authenticator vendor and its - # profile name as a 2-tuple - # - configuration: a dict of additional global - # configuration (i.e. 'help') - # - parameters: a dict of arguments for the - # authenticator profile - auths[auth_name] = ((auth_conf.get('vendor'), auth_name), - {'help': auth_conf.get('help', None)}, - auth_conf.get('parameters', {})) + auths[auth_name] = {'name': auth_name, + 'vendor': auth_conf.get('vendor'), + 'parameters': auth_conf.get('parameters', {}), + 'extra': {'help': auth_conf.get('help', None)}} conf['authenticator'] = auths else: logger.error("expecting a dict of profile(s) or a profile name " diff --git a/moulinette/interfaces/api.py b/moulinette/interfaces/api.py index 16853cb3..ebe36f9a 100644 --- a/moulinette/interfaces/api.py +++ b/moulinette/interfaces/api.py @@ -461,7 +461,7 @@ class _ActionsMapPlugin(object): # Signals handlers - def _do_authenticate(self, authenticator, help): + def _do_authenticate(self, authenticator): """Process the authentication Handle the core.MoulinetteSignals.authenticate signal. diff --git a/moulinette/interfaces/cli.py b/moulinette/interfaces/cli.py index 82365d2a..80eb69d2 100644 --- a/moulinette/interfaces/cli.py +++ b/moulinette/interfaces/cli.py @@ -435,7 +435,7 @@ class Interface(BaseInterface): # Set handler for authentication if password: msignals.set_handler('authenticate', - lambda a, h: a(password=password)) + lambda a: a(password=password)) try: ret = self.actionsmap.process(args, timeout=timeout) @@ -460,13 +460,14 @@ class Interface(BaseInterface): # Signals handlers - def _do_authenticate(self, authenticator, help): + def _do_authenticate(self, authenticator): """Process the authentication Handle the core.MoulinetteSignals.authenticate signal. """ # TODO: Allow token authentication? + help = authenticator.extra.get("help") msg = m18n.n(help) if help else m18n.g('password') return authenticator(password=self._do_prompt(msg, True, False, color='yellow'))