| 1 |
# Copyright 2013 OpenStack Foundation |
| 2 |
# Copyright 2013 Spanish National Research Council. |
| 3 |
# All Rights Reserved. |
| 4 |
# |
| 5 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 6 |
# not use this file except in compliance with the License. You may obtain |
| 7 |
# a copy of the License at |
| 8 |
# |
| 9 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
# |
| 11 |
# Unless required by applicable law or agreed to in writing, software |
| 12 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 |
# License for the specific language governing permissions and limitations |
| 15 |
# under the License. |
| 16 |
|
| 17 |
# E0202: An attribute inherited from %s hide this method |
| 18 |
# pylint: disable=E0202 |
| 19 |
|
| 20 |
import abc |
| 21 |
import argparse |
| 22 |
import os |
| 23 |
|
| 24 |
from stevedore import extension |
| 25 |
|
| 26 |
from troveclient.apiclient import exceptions |
| 27 |
|
| 28 |
|
| 29 |
_discovered_plugins = {} |
| 30 |
|
| 31 |
|
| 32 |
def discover_auth_systems(): |
| 33 |
"""Discover the available auth-systems. |
| 34 |
|
| 35 |
This won't take into account the old style auth-systems. |
| 36 |
""" |
| 37 |
global _discovered_plugins |
| 38 |
_discovered_plugins = {} |
| 39 |
|
| 40 |
def add_plugin(ext): |
| 41 |
_discovered_plugins[ext.name] = ext.plugin |
| 42 |
|
| 43 |
ep_namespace = "troveclient.apiclient.auth" |
| 44 |
mgr = extension.ExtensionManager(ep_namespace) |
| 45 |
mgr.map(add_plugin) |
| 46 |
|
| 47 |
|
| 48 |
def load_auth_system_opts(parser): |
| 49 |
"""Load options needed by the available auth-systems into a parser. |
| 50 |
|
| 51 |
This function will try to populate the parser with options from the |
| 52 |
available plugins. |
| 53 |
""" |
| 54 |
group = parser.add_argument_group("Common auth options") |
| 55 |
BaseAuthPlugin.add_common_opts(group) |
| 56 |
for name, auth_plugin in _discovered_plugins.items(): |
| 57 |
group = parser.add_argument_group( |
| 58 |
"Auth-system '%s' options" % name, |
| 59 |
conflict_handler="resolve") |
| 60 |
auth_plugin.add_opts(group) |
| 61 |
|
| 62 |
|
| 63 |
def load_plugin(auth_system): |
| 64 |
try: |
| 65 |
plugin_class = _discovered_plugins[auth_system] |
| 66 |
except KeyError: |
| 67 |
raise exceptions.AuthSystemNotFound(auth_system) |
| 68 |
return plugin_class(auth_system=auth_system) |
| 69 |
|
| 70 |
|
| 71 |
def load_plugin_from_args(args): |
| 72 |
"""Load required plugin and populate it with options. |
| 73 |
|
| 74 |
Try to guess auth system if it is not specified. Systems are tried in |
| 75 |
alphabetical order. |
| 76 |
|
| 77 |
:type args: argparse.Namespace |
| 78 |
:raises: AuthPluginOptionsMissing |
| 79 |
""" |
| 80 |
auth_system = args.os_auth_system |
| 81 |
if auth_system: |
| 82 |
plugin = load_plugin(auth_system) |
| 83 |
plugin.parse_opts(args) |
| 84 |
plugin.sufficient_options() |
| 85 |
return plugin |
| 86 |
|
| 87 |
for plugin_auth_system in sorted(_discovered_plugins.keys()): |
| 88 |
plugin_class = _discovered_plugins[plugin_auth_system] |
| 89 |
plugin = plugin_class() |
| 90 |
plugin.parse_opts(args) |
| 91 |
try: |
| 92 |
plugin.sufficient_options() |
| 93 |
except exceptions.AuthPluginOptionsMissing: |
| 94 |
continue |
| 95 |
return plugin |
| 96 |
raise exceptions.AuthPluginOptionsMissing(["auth_system"]) |
| 97 |
|
| 98 |
|
| 99 |
class BaseAuthPlugin(metaclass=abc.ABCMeta): |
| 100 |
"""Base class for authentication plugins. |
| 101 |
|
| 102 |
An authentication plugin needs to override at least the authenticate |
| 103 |
method to be a valid plugin. |
| 104 |
""" |
| 105 |
|
| 106 |
auth_system = None |
| 107 |
opt_names = [] |
| 108 |
common_opt_names = [ |
| 109 |
"auth_system", |
| 110 |
"username", |
| 111 |
"password", |
| 112 |
"tenant_name", |
| 113 |
"token", |
| 114 |
"auth_url", |
| 115 |
] |
| 116 |
|
| 117 |
def __init__(self, auth_system=None, **kwargs): |
| 118 |
self.auth_system = auth_system or self.auth_system |
| 119 |
self.opts = dict((name, kwargs.get(name)) |
| 120 |
for name in self.opt_names) |
| 121 |
|
| 122 |
@staticmethod |
| 123 |
def _parser_add_opt(parser, opt): |
| 124 |
"""Add an option to parser in two variants. |
| 125 |
|
| 126 |
:param opt: option name (with underscores) |
| 127 |
""" |
| 128 |
dashed_opt = opt.replace("_", "-") |
| 129 |
env_var = "OS_%s" % opt.upper() |
| 130 |
arg_default = os.environ.get(env_var, "") |
| 131 |
arg_help = "Defaults to env[%s]." % env_var |
| 132 |
parser.add_argument( |
| 133 |
"--os-%s" % dashed_opt, |
| 134 |
metavar="<%s>" % dashed_opt, |
| 135 |
default=arg_default, |
| 136 |
help=arg_help) |
| 137 |
parser.add_argument( |
| 138 |
"--os_%s" % opt, |
| 139 |
metavar="<%s>" % dashed_opt, |
| 140 |
help=argparse.SUPPRESS) |
| 141 |
|
| 142 |
@classmethod |
| 143 |
def add_opts(cls, parser): |
| 144 |
"""Populate the parser with the options for this plugin. |
| 145 |
""" |
| 146 |
for opt in cls.opt_names: |
| 147 |
# use `BaseAuthPlugin.common_opt_names` since it is never |
| 148 |
# changed in child classes |
| 149 |
if opt not in BaseAuthPlugin.common_opt_names: |
| 150 |
cls._parser_add_opt(parser, opt) |
| 151 |
|
| 152 |
@classmethod |
| 153 |
def add_common_opts(cls, parser): |
| 154 |
"""Add options that are common for several plugins. |
| 155 |
""" |
| 156 |
for opt in cls.common_opt_names: |
| 157 |
cls._parser_add_opt(parser, opt) |
| 158 |
|
| 159 |
@staticmethod |
| 160 |
def get_opt(opt_name, args): |
| 161 |
"""Return option name and value. |
| 162 |
|
| 163 |
:param opt_name: name of the option, e.g., "username" |
| 164 |
:param args: parsed arguments |
| 165 |
""" |
| 166 |
return (opt_name, getattr(args, "os_%s" % opt_name, None)) |
| 167 |
|
| 168 |
def parse_opts(self, args): |
| 169 |
"""Parse the actual auth-system options if any. |
| 170 |
|
| 171 |
This method is expected to populate the attribute `self.opts` with a |
| 172 |
dict containing the options and values needed to make authentication. |
| 173 |
""" |
| 174 |
self.opts.update(dict(self.get_opt(opt_name, args) |
| 175 |
for opt_name in self.opt_names)) |
| 176 |
|
| 177 |
def authenticate(self, http_client): |
| 178 |
"""Authenticate using plugin defined method. |
| 179 |
|
| 180 |
The method usually analyses `self.opts` and performs |
| 181 |
a request to authentication server. |
| 182 |
|
| 183 |
:param http_client: client object that needs authentication |
| 184 |
:type http_client: troveclient.client.HTTPClient |
| 185 |
:raises: AuthorizationFailure |
| 186 |
""" |
| 187 |
self.sufficient_options() |
| 188 |
self._do_authenticate(http_client) |
| 189 |
|
| 190 |
@abc.abstractmethod |
| 191 |
def _do_authenticate(self, http_client): |
| 192 |
"""Protected method for authentication. |
| 193 |
""" |
| 194 |
|
| 195 |
def sufficient_options(self): |
| 196 |
"""Check if all required options are present. |
| 197 |
|
| 198 |
:raises: AuthPluginOptionsMissing |
| 199 |
""" |
| 200 |
missing = [opt |
| 201 |
for opt in self.opt_names |
| 202 |
if not self.opts.get(opt)] |
| 203 |
if missing: |
| 204 |
raise exceptions.AuthPluginOptionsMissing(missing) |
| 205 |
|
| 206 |
@abc.abstractmethod |
| 207 |
def token_and_endpoint(self, endpoint_type, service_type): |
| 208 |
"""Return token and endpoint. |
| 209 |
|
| 210 |
:param service_type: Service type of the endpoint |
| 211 |
:type service_type: string |
| 212 |
:param endpoint_type: Type of endpoint. |
| 213 |
Possible values: public or publicURL, |
| 214 |
internal or internalURL, |
| 215 |
admin or adminURL |
| 216 |
:type endpoint_type: string |
| 217 |
:returns: tuple of token and endpoint strings |
| 218 |
:raises: EndpointException |
| 219 |
""" |