| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
import abc |
| 14 |
import logging |
| 15 |
|
| 16 |
from oslo_config import cfg |
| 17 |
from oslo_serialization import jsonutils |
| 18 |
|
| 19 |
from keystoneclient import access |
| 20 |
from keystoneclient.auth.identity import base |
| 21 |
from keystoneclient import exceptions |
| 22 |
from keystoneclient.i18n import _ |
| 23 |
|
| 24 |
_logger = logging.getLogger(__name__) |
| 25 |
|
| 26 |
__all__ = ('Auth', 'AuthMethod', 'AuthConstructor', 'BaseAuth') |
| 27 |
|
| 28 |
|
| 29 |
class BaseAuth(base.BaseIdentityPlugin, metaclass=abc.ABCMeta): |
| 30 |
"""Identity V3 Authentication Plugin. |
| 31 |
|
| 32 |
:param string auth_url: Identity service endpoint for authentication. |
| 33 |
:param List auth_methods: A collection of methods to authenticate with. |
| 34 |
:param string trust_id: Trust ID for trust scoping. |
| 35 |
:param string domain_id: Domain ID for domain scoping. |
| 36 |
:param string domain_name: Domain name for domain scoping. |
| 37 |
:param string project_id: Project ID for project scoping. |
| 38 |
:param string project_name: Project name for project scoping. |
| 39 |
:param string project_domain_id: Project's domain ID for project. |
| 40 |
:param string project_domain_name: Project's domain name for project. |
| 41 |
:param bool reauthenticate: Allow fetching a new token if the current one |
| 42 |
is going to expire. (optional) default True |
| 43 |
:param bool include_catalog: Include the service catalog in the returned |
| 44 |
token. (optional) default True. |
| 45 |
""" |
| 46 |
|
| 47 |
def __init__(self, auth_url, |
| 48 |
trust_id=None, |
| 49 |
domain_id=None, |
| 50 |
domain_name=None, |
| 51 |
project_id=None, |
| 52 |
project_name=None, |
| 53 |
project_domain_id=None, |
| 54 |
project_domain_name=None, |
| 55 |
reauthenticate=True, |
| 56 |
include_catalog=True): |
| 57 |
super(BaseAuth, self).__init__(auth_url=auth_url, |
| 58 |
reauthenticate=reauthenticate) |
| 59 |
self._trust_id = trust_id |
| 60 |
self.domain_id = domain_id |
| 61 |
self.domain_name = domain_name |
| 62 |
self.project_id = project_id |
| 63 |
self.project_name = project_name |
| 64 |
self.project_domain_id = project_domain_id |
| 65 |
self.project_domain_name = project_domain_name |
| 66 |
self.include_catalog = include_catalog |
| 67 |
|
| 68 |
@property |
| 69 |
def trust_id(self): |
| 70 |
|
| 71 |
return self._trust_id |
| 72 |
|
| 73 |
@trust_id.setter |
| 74 |
def trust_id(self, value): |
| 75 |
|
| 76 |
self._trust_id = value |
| 77 |
|
| 78 |
@property |
| 79 |
def token_url(self): |
| 80 |
"""The full URL where we will send authentication data.""" |
| 81 |
return '%s/auth/tokens' % self.auth_url.rstrip('/') |
| 82 |
|
| 83 |
@abc.abstractmethod |
| 84 |
def get_auth_ref(self, session, **kwargs): |
| 85 |
return None |
| 86 |
|
| 87 |
@classmethod |
| 88 |
def get_options(cls): |
| 89 |
options = super(BaseAuth, cls).get_options() |
| 90 |
|
| 91 |
options.extend([ |
| 92 |
cfg.StrOpt('domain-id', help='Domain ID to scope to'), |
| 93 |
cfg.StrOpt('domain-name', help='Domain name to scope to'), |
| 94 |
cfg.StrOpt('project-id', help='Project ID to scope to'), |
| 95 |
cfg.StrOpt('project-name', help='Project name to scope to'), |
| 96 |
cfg.StrOpt('project-domain-id', |
| 97 |
help='Domain ID containing project'), |
| 98 |
cfg.StrOpt('project-domain-name', |
| 99 |
help='Domain name containing project'), |
| 100 |
cfg.StrOpt('trust-id', help='Trust ID'), |
| 101 |
]) |
| 102 |
|
| 103 |
return options |
| 104 |
|
| 105 |
|
| 106 |
class Auth(BaseAuth): |
| 107 |
"""Identity V3 Authentication Plugin. |
| 108 |
|
| 109 |
:param string auth_url: Identity service endpoint for authentication. |
| 110 |
:param List auth_methods: A collection of methods to authenticate with. |
| 111 |
:param string trust_id: Trust ID for trust scoping. |
| 112 |
:param string domain_id: Domain ID for domain scoping. |
| 113 |
:param string domain_name: Domain name for domain scoping. |
| 114 |
:param string project_id: Project ID for project scoping. |
| 115 |
:param string project_name: Project name for project scoping. |
| 116 |
:param string project_domain_id: Project's domain ID for project. |
| 117 |
:param string project_domain_name: Project's domain name for project. |
| 118 |
:param bool reauthenticate: Allow fetching a new token if the current one |
| 119 |
is going to expire. (optional) default True |
| 120 |
:param bool include_catalog: Include the service catalog in the returned |
| 121 |
token. (optional) default True. |
| 122 |
:param bool unscoped: Force the return of an unscoped token. This will make |
| 123 |
the keystone server return an unscoped token even if |
| 124 |
a default_project_id is set for this user. |
| 125 |
""" |
| 126 |
|
| 127 |
def __init__(self, auth_url, auth_methods, **kwargs): |
| 128 |
self.unscoped = kwargs.pop('unscoped', False) |
| 129 |
super(Auth, self).__init__(auth_url=auth_url, **kwargs) |
| 130 |
self.auth_methods = auth_methods |
| 131 |
|
| 132 |
def get_auth_ref(self, session, **kwargs): |
| 133 |
headers = {'Accept': 'application/json'} |
| 134 |
body = {'auth': {'identity': {}}} |
| 135 |
ident = body['auth']['identity'] |
| 136 |
rkwargs = {} |
| 137 |
|
| 138 |
for method in self.auth_methods: |
| 139 |
name, auth_data = method.get_auth_data(session, |
| 140 |
self, |
| 141 |
headers, |
| 142 |
request_kwargs=rkwargs) |
| 143 |
ident.setdefault('methods', []).append(name) |
| 144 |
ident[name] = auth_data |
| 145 |
|
| 146 |
if not ident: |
| 147 |
raise exceptions.AuthorizationFailure( |
| 148 |
_('Authentication method required (e.g. password)')) |
| 149 |
|
| 150 |
mutual_exclusion = [bool(self.domain_id or self.domain_name), |
| 151 |
bool(self.project_id or self.project_name), |
| 152 |
bool(self.trust_id), |
| 153 |
bool(self.unscoped)] |
| 154 |
|
| 155 |
if sum(mutual_exclusion) > 1: |
| 156 |
raise exceptions.AuthorizationFailure( |
| 157 |
_('Authentication cannot be scoped to multiple targets. Pick ' |
| 158 |
'one of: project, domain, trust or unscoped')) |
| 159 |
|
| 160 |
if self.domain_id: |
| 161 |
body['auth']['scope'] = {'domain': {'id': self.domain_id}} |
| 162 |
elif self.domain_name: |
| 163 |
body['auth']['scope'] = {'domain': {'name': self.domain_name}} |
| 164 |
elif self.project_id: |
| 165 |
body['auth']['scope'] = {'project': {'id': self.project_id}} |
| 166 |
elif self.project_name: |
| 167 |
scope = body['auth']['scope'] = {'project': {}} |
| 168 |
scope['project']['name'] = self.project_name |
| 169 |
|
| 170 |
if self.project_domain_id: |
| 171 |
scope['project']['domain'] = {'id': self.project_domain_id} |
| 172 |
elif self.project_domain_name: |
| 173 |
scope['project']['domain'] = {'name': self.project_domain_name} |
| 174 |
elif self.trust_id: |
| 175 |
body['auth']['scope'] = {'OS-TRUST:trust': {'id': self.trust_id}} |
| 176 |
elif self.unscoped: |
| 177 |
body['auth']['scope'] = {'unscoped': {}} |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
token_url = self.token_url |
| 182 |
if not self.include_catalog: |
| 183 |
token_url += '?nocatalog' |
| 184 |
|
| 185 |
_logger.debug('Making authentication request to %s', token_url) |
| 186 |
resp = session.post(token_url, json=body, headers=headers, |
| 187 |
authenticated=False, log=False, **rkwargs) |
| 188 |
|
| 189 |
try: |
| 190 |
_logger.debug(jsonutils.dumps(resp.json())) |
| 191 |
resp_data = resp.json()['token'] |
| 192 |
except (KeyError, ValueError): |
| 193 |
raise exceptions.InvalidResponse(response=resp) |
| 194 |
|
| 195 |
return access.AccessInfoV3(resp.headers['X-Subject-Token'], |
| 196 |
**resp_data) |
| 197 |
|
| 198 |
|
| 199 |
class AuthMethod(object, metaclass=abc.ABCMeta): |
| 200 |
"""One part of a V3 Authentication strategy. |
| 201 |
|
| 202 |
V3 Tokens allow multiple methods to be presented when authentication |
| 203 |
against the server. Each one of these methods is implemented by an |
| 204 |
AuthMethod. |
| 205 |
|
| 206 |
Note: When implementing an AuthMethod use the method_parameters |
| 207 |
and do not use positional arguments. Otherwise they can't be picked up by |
| 208 |
the factory method and don't work as well with AuthConstructors. |
| 209 |
""" |
| 210 |
|
| 211 |
_method_parameters = [] |
| 212 |
|
| 213 |
def __init__(self, **kwargs): |
| 214 |
for param in self._method_parameters: |
| 215 |
setattr(self, param, kwargs.pop(param, None)) |
| 216 |
|
| 217 |
if kwargs: |
| 218 |
msg = _("Unexpected Attributes: %s") % ", ".join(kwargs) |
| 219 |
raise AttributeError(msg) |
| 220 |
|
| 221 |
@classmethod |
| 222 |
def _extract_kwargs(cls, kwargs): |
| 223 |
"""Remove parameters related to this method from other kwargs.""" |
| 224 |
return dict([(p, kwargs.pop(p, None)) |
| 225 |
for p in cls._method_parameters]) |
| 226 |
|
| 227 |
@abc.abstractmethod |
| 228 |
def get_auth_data(self, session, auth, headers, **kwargs): |
| 229 |
"""Return the authentication section of an auth plugin. |
| 230 |
|
| 231 |
:param session: The communication session. |
| 232 |
:type session: keystoneclient.session.Session |
| 233 |
:param base.Auth auth: The auth plugin calling the method. |
| 234 |
:param dict headers: The headers that will be sent with the auth |
| 235 |
request if a plugin needs to add to them. |
| 236 |
:return: The identifier of this plugin and a dict of authentication |
| 237 |
data for the auth type. |
| 238 |
:rtype: tuple(string, dict) |
| 239 |
""" |
| 240 |
pass |
| 241 |
|
| 242 |
|
| 243 |
class AuthConstructor(Auth, metaclass=abc.ABCMeta): |
| 244 |
"""Abstract base class for creating an Auth Plugin. |
| 245 |
|
| 246 |
The Auth Plugin created contains only one authentication method. This |
| 247 |
is generally the required usage. |
| 248 |
|
| 249 |
An AuthConstructor creates an AuthMethod based on the method's |
| 250 |
arguments and the auth_method_class defined by the plugin. It then |
| 251 |
creates the auth plugin with only that authentication method. |
| 252 |
""" |
| 253 |
|
| 254 |
_auth_method_class = None |
| 255 |
|
| 256 |
def __init__(self, auth_url, *args, **kwargs): |
| 257 |
method_kwargs = self._auth_method_class._extract_kwargs(kwargs) |
| 258 |
method = self._auth_method_class(*args, **method_kwargs) |
| 259 |
super(AuthConstructor, self).__init__(auth_url, [method], **kwargs) |