Debian Codegraph @

keystoneclient/auth/identity/generic/cli.py

raw symbols SCIP
1 # Licensed under the Apache License, Version 2.0 (the "License"); you may
2 # not use this file except in compliance with the License. You may obtain
3 # a copy of the License at
4 #
5 # http://www.apache.org/licenses/LICENSE-2.0
6 #
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 # License for the specific language governing permissions and limitations
11 # under the License.
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 # if a user provides a token then they must also provide an
63 # endpoint because we aren't fetching a token to get a catalog from
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 # if you don't provide a token you are going to provide at least an
68 # auth_url with which to authenticate.
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 # we do this after the load so that the base plugin has an
77 # opportunity to prompt the user for a password
78 raise exc.CommandError(_('Expecting a password provided via '
79 'either --os-password, env[OS_PASSWORD], '
80 'or prompted response'))
81
82 return plugin