| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
from oslo_config import cfg |
| 14 |
|
| 15 |
from keystoneclient.auth.identity.generic import password |
| 16 |
from keystoneclient import exceptions as exc |
| 17 |
from keystoneclient.i18n import _ |
| 18 |
|
| 19 |
|
| 20 |
class DefaultCLI(password.Password): |
| 21 |
"""A Plugin that provides typical authentication options for CLIs. |
| 22 |
|
| 23 |
This plugin provides standard username and password authentication options |
| 24 |
as well as allowing users to override with a custom token and endpoint. |
| 25 |
""" |
| 26 |
|
| 27 |
def __init__(self, endpoint=None, token=None, **kwargs): |
| 28 |
super(DefaultCLI, self).__init__(**kwargs) |
| 29 |
|
| 30 |
self._token = token |
| 31 |
self._endpoint = endpoint |
| 32 |
|
| 33 |
@classmethod |
| 34 |
def get_options(cls): |
| 35 |
options = super(DefaultCLI, cls).get_options() |
| 36 |
options.extend([cfg.StrOpt('endpoint', |
| 37 |
help='A URL to use instead of a catalog'), |
| 38 |
cfg.StrOpt('token', |
| 39 |
secret=True, |
| 40 |
help='Always use the specified token')]) |
| 41 |
return options |
| 42 |
|
| 43 |
def get_token(self, *args, **kwargs): |
| 44 |
if self._token: |
| 45 |
return self._token |
| 46 |
|
| 47 |
return super(DefaultCLI, self).get_token(*args, **kwargs) |
| 48 |
|
| 49 |
def get_endpoint(self, *args, **kwargs): |
| 50 |
if self._endpoint: |
| 51 |
return self._endpoint |
| 52 |
|
| 53 |
return super(DefaultCLI, self).get_endpoint(*args, **kwargs) |
| 54 |
|
| 55 |
@classmethod |
| 56 |
def load_from_argparse_arguments(cls, namespace, **kwargs): |
| 57 |
token = kwargs.get('token') or namespace.os_token |
| 58 |
endpoint = kwargs.get('endpoint') or namespace.os_endpoint |
| 59 |
auth_url = kwargs.get('auth_url') or namespace.os_auth_url |
| 60 |
|
| 61 |
if token and not endpoint: |
| 62 |
|
| 63 |
|
| 64 |
msg = _('A service URL must be provided with a token') |
| 65 |
raise exc.CommandError(msg) |
| 66 |
elif (not token) and (not auth_url): |
| 67 |
|
| 68 |
|
| 69 |
raise exc.CommandError(_('Expecting an auth URL via either ' |
| 70 |
'--os-auth-url or env[OS_AUTH_URL]')) |
| 71 |
|
| 72 |
plugin = super(DefaultCLI, cls).load_from_argparse_arguments(namespace, |
| 73 |
**kwargs) |
| 74 |
|
| 75 |
if (not token) and (not plugin._password): |
| 76 |
|
| 77 |
|
| 78 |
raise exc.CommandError(_('Expecting a password provided via ' |
| 79 |
'either --os-password, env[OS_PASSWORD], ' |
| 80 |
'or prompted response')) |
| 81 |
|
| 82 |
return plugin |