[enh] Allow to pass the password as argument in the cli

This commit is contained in:
Jérôme Lebleu 2015-12-24 10:34:46 +01:00
parent 79a10628a4
commit db924ea911
2 changed files with 15 additions and 3 deletions

View file

@ -108,7 +108,8 @@ def api(namespaces, host='localhost', port=80, routes={},
logging.getLogger(namespaces[0]).info(m18n.g('operation_interrupted'))
return 0
def cli(namespaces, args, use_cache=True, output_as=None, parser_kwargs={}):
def cli(namespaces, args, use_cache=True, output_as=None,
password=None, parser_kwargs={}):
"""Command line interface
Execute an action with the moulinette from the CLI and print its
@ -121,6 +122,7 @@ def cli(namespaces, args, use_cache=True, output_as=None, parser_kwargs={}):
instead of using the cached one
- output_as -- Output result in another format, see
moulinette.interfaces.cli.Interface for possible values
- password -- The password to use in case of authentication
- parser_kwargs -- A dict of arguments to pass to the parser
class at construction
@ -133,7 +135,7 @@ def cli(namespaces, args, use_cache=True, output_as=None, parser_kwargs={}):
'parser_kwargs': parser_kwargs,
},
)
moulinette.run(args, output_as=output_as)
moulinette.run(args, output_as=output_as, password=password)
except MoulinetteError as e:
import logging
logging.getLogger(namespaces[0]).error(e.strerror)

View file

@ -309,7 +309,7 @@ class Interface(BaseInterface):
self.actionsmap = actionsmap
def run(self, args, output_as=None):
def run(self, args, output_as=None, password=None):
"""Run the moulinette
Process the action corresponding to the given arguments 'args'
@ -320,6 +320,7 @@ class Interface(BaseInterface):
- output_as -- Output result in another format. Possible values:
- json: return a JSON encoded string
- plain: return a script-readable output
- password -- The password to use in case of authentication
"""
if output_as and output_as not in ['json', 'plain']:
@ -328,6 +329,10 @@ class Interface(BaseInterface):
# auto-complete
argcomplete.autocomplete(self.actionsmap.parser._parser)
# Store the given password
# FIXME: improve security
self._password = password
try:
ret = self.actionsmap.process(args, timeout=5)
except KeyboardInterrupt, EOFError:
@ -358,6 +363,11 @@ class Interface(BaseInterface):
Handle the core.MoulinetteSignals.authenticate signal.
"""
# Try to use given password if any
if self._password is not None:
logger.info('using given password to authenticate')
return authenticator(password=self._password)
# TODO: Allow token authentication?
msg = m18n.n(help) if help else m18n.g('password')
return authenticator(password=self._do_prompt(msg, True, False,