OpenStack Identity (Keystone)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

core.py 64KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464
  1. # Copyright 2012 OpenStack Foundation
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  4. # not use this file except in compliance with the License. You may obtain
  5. # a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  11. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  12. # License for the specific language governing permissions and limitations
  13. # under the License.
  14. """Main entry point into the Identity service."""
  15. import functools
  16. import operator
  17. import os
  18. import threading
  19. import uuid
  20. from oslo_config import cfg
  21. from oslo_log import log
  22. from pycadf import reason
  23. from keystone import assignment # TODO(lbragstad): Decouple this dependency
  24. from keystone.common import cache
  25. from keystone.common import clean
  26. from keystone.common import dependency
  27. from keystone.common import driver_hints
  28. from keystone.common import manager
  29. from keystone.common.validation import validators
  30. import keystone.conf
  31. from keystone import exception
  32. from keystone.i18n import _, _LW
  33. from keystone.identity.mapping_backends import mapping
  34. from keystone import notifications
  35. from oslo_utils import timeutils
  36. CONF = keystone.conf.CONF
  37. LOG = log.getLogger(__name__)
  38. MEMOIZE = cache.get_memoization_decorator(group='identity')
  39. ID_MAPPING_REGION = cache.create_region(name='id mapping')
  40. MEMOIZE_ID_MAPPING = cache.get_memoization_decorator(group='identity',
  41. region=ID_MAPPING_REGION)
  42. DOMAIN_CONF_FHEAD = 'keystone.'
  43. DOMAIN_CONF_FTAIL = '.conf'
  44. # The number of times we will attempt to register a domain to use the SQL
  45. # driver, if we find that another process is in the middle of registering or
  46. # releasing at the same time as us.
  47. REGISTRATION_ATTEMPTS = 10
  48. # Config Registration Types
  49. SQL_DRIVER = 'SQL'
  50. @dependency.requires('domain_config_api', 'resource_api')
  51. class DomainConfigs(dict):
  52. """Discover, store and provide access to domain specific configs.
  53. The setup_domain_drivers() call will be made via the wrapper from
  54. the first call to any driver function handled by this manager.
  55. Domain specific configurations are only supported for the identity backend
  56. and the individual configurations are either specified in the resource
  57. database or in individual domain configuration files, depending on the
  58. setting of the 'domain_configurations_from_database' config option.
  59. The result will be that for each domain with a specific configuration,
  60. this class will hold a reference to a ConfigOpts and driver object that
  61. the identity manager and driver can use.
  62. """
  63. configured = False
  64. driver = None
  65. _any_sql = False
  66. lock = threading.Lock()
  67. def _load_driver(self, domain_config):
  68. return manager.load_driver(Manager.driver_namespace,
  69. domain_config['cfg'].identity.driver,
  70. domain_config['cfg'])
  71. def _load_config_from_file(self, resource_api, file_list, domain_name):
  72. def _assert_no_more_than_one_sql_driver(new_config, config_file):
  73. """Ensure there is no more than one sql driver.
  74. Check to see if the addition of the driver in this new config
  75. would cause there to be more than one sql driver.
  76. """
  77. if (new_config['driver'].is_sql and
  78. (self.driver.is_sql or self._any_sql)):
  79. # The addition of this driver would cause us to have more than
  80. # one sql driver, so raise an exception.
  81. raise exception.MultipleSQLDriversInConfig(source=config_file)
  82. self._any_sql = self._any_sql or new_config['driver'].is_sql
  83. try:
  84. domain_ref = resource_api.get_domain_by_name(domain_name)
  85. except exception.DomainNotFound:
  86. LOG.warning(
  87. _LW('Invalid domain name (%s) found in config file name'),
  88. domain_name)
  89. return
  90. # Create a new entry in the domain config dict, which contains
  91. # a new instance of both the conf environment and driver using
  92. # options defined in this set of config files. Later, when we
  93. # service calls via this Manager, we'll index via this domain
  94. # config dict to make sure we call the right driver
  95. domain_config = {}
  96. domain_config['cfg'] = cfg.ConfigOpts()
  97. keystone.conf.configure(conf=domain_config['cfg'])
  98. domain_config['cfg'](args=[], project='keystone',
  99. default_config_files=file_list,
  100. default_config_dirs=[])
  101. domain_config['driver'] = self._load_driver(domain_config)
  102. _assert_no_more_than_one_sql_driver(domain_config, file_list)
  103. self[domain_ref['id']] = domain_config
  104. def _setup_domain_drivers_from_files(self, standard_driver, resource_api):
  105. """Read the domain specific configuration files and load the drivers.
  106. Domain configuration files are stored in the domain config directory,
  107. and must be named of the form:
  108. keystone.<domain_name>.conf
  109. For each file, call the load config method where the domain_name
  110. will be turned into a domain_id and then:
  111. - Create a new config structure, adding in the specific additional
  112. options defined in this config file
  113. - Initialise a new instance of the required driver with this new config
  114. """
  115. conf_dir = CONF.identity.domain_config_dir
  116. if not os.path.exists(conf_dir):
  117. LOG.warning(_LW('Unable to locate domain config directory: %s'),
  118. conf_dir)
  119. return
  120. for r, d, f in os.walk(conf_dir):
  121. for fname in f:
  122. if (fname.startswith(DOMAIN_CONF_FHEAD) and
  123. fname.endswith(DOMAIN_CONF_FTAIL)):
  124. if fname.count('.') >= 2:
  125. self._load_config_from_file(
  126. resource_api, [os.path.join(r, fname)],
  127. fname[len(DOMAIN_CONF_FHEAD):
  128. -len(DOMAIN_CONF_FTAIL)])
  129. else:
  130. LOG.debug(('Ignoring file (%s) while scanning domain '
  131. 'config directory'),
  132. fname)
  133. def _load_config_from_database(self, domain_id, specific_config):
  134. def _assert_no_more_than_one_sql_driver(domain_id, new_config):
  135. """Ensure adding driver doesn't push us over the limit of 1.
  136. The checks we make in this method need to take into account that
  137. we may be in a multiple process configuration and ensure that
  138. any race conditions are avoided.
  139. """
  140. if not new_config['driver'].is_sql:
  141. self.domain_config_api.release_registration(domain_id)
  142. return
  143. # To ensure the current domain is the only SQL driver, we attempt
  144. # to register our use of SQL. If we get it we know we are good,
  145. # if we fail to register it then we should:
  146. #
  147. # - First check if another process has registered for SQL for our
  148. # domain, in which case we are fine
  149. # - If a different domain has it, we should check that this domain
  150. # is still valid, in case, for example, domain deletion somehow
  151. # failed to remove its registration (i.e. we self heal for these
  152. # kinds of issues).
  153. domain_registered = 'Unknown'
  154. for attempt in range(REGISTRATION_ATTEMPTS):
  155. if self.domain_config_api.obtain_registration(
  156. domain_id, SQL_DRIVER):
  157. LOG.debug('Domain %s successfully registered to use the '
  158. 'SQL driver.', domain_id)
  159. return
  160. # We failed to register our use, let's find out who is using it
  161. try:
  162. domain_registered = (
  163. self.domain_config_api.read_registration(
  164. SQL_DRIVER))
  165. except exception.ConfigRegistrationNotFound:
  166. msg = ('While attempting to register domain %(domain)s to '
  167. 'use the SQL driver, another process released it, '
  168. 'retrying (attempt %(attempt)s).')
  169. LOG.debug(msg, {'domain': domain_id,
  170. 'attempt': attempt + 1})
  171. continue
  172. if domain_registered == domain_id:
  173. # Another process already registered it for us, so we are
  174. # fine. In the race condition when another process is
  175. # in the middle of deleting this domain, we know the domain
  176. # is already disabled and hence telling the caller that we
  177. # are registered is benign.
  178. LOG.debug('While attempting to register domain %s to use '
  179. 'the SQL driver, found that another process had '
  180. 'already registered this domain. This is normal '
  181. 'in multi-process configurations.', domain_id)
  182. return
  183. # So we don't have it, but someone else does...let's check that
  184. # this domain is still valid
  185. try:
  186. self.resource_api.get_domain(domain_registered)
  187. except exception.DomainNotFound:
  188. msg = ('While attempting to register domain %(domain)s to '
  189. 'use the SQL driver, found that it was already '
  190. 'registered to a domain that no longer exists '
  191. '(%(old_domain)s). Removing this stale '
  192. 'registration and retrying (attempt %(attempt)s).')
  193. LOG.debug(msg, {'domain': domain_id,
  194. 'old_domain': domain_registered,
  195. 'attempt': attempt + 1})
  196. self.domain_config_api.release_registration(
  197. domain_registered, type=SQL_DRIVER)
  198. continue
  199. # The domain is valid, so we really do have an attempt at more
  200. # than one SQL driver.
  201. details = (
  202. _('Config API entity at /domains/%s/config') % domain_id)
  203. raise exception.MultipleSQLDriversInConfig(source=details)
  204. # We fell out of the loop without either registering our domain or
  205. # being able to find who has it...either we were very very very
  206. # unlucky or something is awry.
  207. msg = _('Exceeded attempts to register domain %(domain)s to use '
  208. 'the SQL driver, the last domain that appears to have '
  209. 'had it is %(last_domain)s, giving up') % {
  210. 'domain': domain_id, 'last_domain': domain_registered}
  211. raise exception.UnexpectedError(msg)
  212. domain_config = {}
  213. domain_config['cfg'] = cfg.ConfigOpts()
  214. keystone.conf.configure(conf=domain_config['cfg'])
  215. domain_config['cfg'](args=[], project='keystone',
  216. default_config_files=[],
  217. default_config_dirs=[])
  218. # Override any options that have been passed in as specified in the
  219. # database.
  220. for group in specific_config:
  221. for option in specific_config[group]:
  222. domain_config['cfg'].set_override(
  223. option, specific_config[group][option],
  224. group, enforce_type=True)
  225. domain_config['cfg_overrides'] = specific_config
  226. domain_config['driver'] = self._load_driver(domain_config)
  227. _assert_no_more_than_one_sql_driver(domain_id, domain_config)
  228. self[domain_id] = domain_config
  229. def _setup_domain_drivers_from_database(self, standard_driver,
  230. resource_api):
  231. """Read domain specific configuration from database and load drivers.
  232. Domain configurations are stored in the domain-config backend,
  233. so we go through each domain to find those that have a specific config
  234. defined, and for those that do we:
  235. - Create a new config structure, overriding any specific options
  236. defined in the resource backend
  237. - Initialise a new instance of the required driver with this new config
  238. """
  239. for domain in resource_api.list_domains():
  240. domain_config_options = (
  241. self.domain_config_api.
  242. get_config_with_sensitive_info(domain['id']))
  243. if domain_config_options:
  244. self._load_config_from_database(domain['id'],
  245. domain_config_options)
  246. def setup_domain_drivers(self, standard_driver, resource_api):
  247. # This is called by the api call wrapper
  248. self.driver = standard_driver
  249. if CONF.identity.domain_configurations_from_database:
  250. self._setup_domain_drivers_from_database(standard_driver,
  251. resource_api)
  252. else:
  253. self._setup_domain_drivers_from_files(standard_driver,
  254. resource_api)
  255. self.configured = True
  256. def get_domain_driver(self, domain_id):
  257. self.check_config_and_reload_domain_driver_if_required(domain_id)
  258. if domain_id in self:
  259. return self[domain_id]['driver']
  260. def get_domain_conf(self, domain_id):
  261. self.check_config_and_reload_domain_driver_if_required(domain_id)
  262. if domain_id in self:
  263. return self[domain_id]['cfg']
  264. else:
  265. return CONF
  266. def reload_domain_driver(self, domain_id):
  267. # Only used to support unit tests that want to set
  268. # new config values. This should only be called once
  269. # the domains have been configured, since it relies on
  270. # the fact that the configuration files/database have already been
  271. # read.
  272. if self.configured:
  273. if domain_id in self:
  274. self[domain_id]['driver'] = (
  275. self._load_driver(self[domain_id]))
  276. else:
  277. # The standard driver
  278. self.driver = self.driver()
  279. def check_config_and_reload_domain_driver_if_required(self, domain_id):
  280. """Check for, and load, any new domain specific config for this domain.
  281. This is only supported for the database-stored domain specific
  282. configuration.
  283. When the domain specific drivers were set up, we stored away the
  284. specific config for this domain that was available at that time. So we
  285. now read the current version and compare. While this might seem
  286. somewhat inefficient, the sensitive config call is cached, so should be
  287. light weight. More importantly, when the cache timeout is reached, we
  288. will get any config that has been updated from any other keystone
  289. process.
  290. This cache-timeout approach works for both multi-process and
  291. multi-threaded keystone configurations. In multi-threaded
  292. configurations, even though we might remove a driver object (that
  293. could be in use by another thread), this won't actually be thrown away
  294. until all references to it have been broken. When that other
  295. thread is released back and is restarted with another command to
  296. process, next time it accesses the driver it will pickup the new one.
  297. """
  298. if (not CONF.identity.domain_specific_drivers_enabled or
  299. not CONF.identity.domain_configurations_from_database):
  300. # If specific drivers are not enabled, then there is nothing to do.
  301. # If we are not storing the configurations in the database, then
  302. # we'll only re-read the domain specific config files on startup
  303. # of keystone.
  304. return
  305. latest_domain_config = (
  306. self.domain_config_api.
  307. get_config_with_sensitive_info(domain_id))
  308. domain_config_in_use = domain_id in self
  309. if latest_domain_config:
  310. if (not domain_config_in_use or
  311. latest_domain_config != self[domain_id]['cfg_overrides']):
  312. self._load_config_from_database(domain_id,
  313. latest_domain_config)
  314. elif domain_config_in_use:
  315. # The domain specific config has been deleted, so should remove the
  316. # specific driver for this domain.
  317. try:
  318. del self[domain_id]
  319. except KeyError: # nosec
  320. # Allow this error in case we are unlucky and in a
  321. # multi-threaded situation, two threads happen to be running
  322. # in lock step.
  323. pass
  324. # If we fall into the else condition, this means there is no domain
  325. # config set, and there is none in use either, so we have nothing
  326. # to do.
  327. def domains_configured(f):
  328. """Wrap API calls to lazy load domain configs after init.
  329. This is required since the assignment manager needs to be initialized
  330. before this manager, and yet this manager's init wants to be
  331. able to make assignment calls (to build the domain configs). So
  332. instead, we check if the domains have been initialized on entry
  333. to each call, and if requires load them,
  334. """
  335. @functools.wraps(f)
  336. def wrapper(self, *args, **kwargs):
  337. if (not self.domain_configs.configured and
  338. CONF.identity.domain_specific_drivers_enabled):
  339. # If domain specific driver has not been configured, acquire the
  340. # lock and proceed with loading the driver.
  341. with self.domain_configs.lock:
  342. # Check again just in case some other thread has already
  343. # completed domain config.
  344. if not self.domain_configs.configured:
  345. self.domain_configs.setup_domain_drivers(
  346. self.driver, self.resource_api)
  347. return f(self, *args, **kwargs)
  348. return wrapper
  349. def exception_translated(exception_type):
  350. """Wrap API calls to map to correct exception."""
  351. def _exception_translated(f):
  352. @functools.wraps(f)
  353. def wrapper(self, *args, **kwargs):
  354. try:
  355. return f(self, *args, **kwargs)
  356. except exception.PublicIDNotFound as e:
  357. if exception_type == 'user':
  358. raise exception.UserNotFound(user_id=str(e))
  359. elif exception_type == 'group':
  360. raise exception.GroupNotFound(group_id=str(e))
  361. elif exception_type == 'assertion':
  362. raise AssertionError(_('Invalid user / password'))
  363. else:
  364. raise
  365. return wrapper
  366. return _exception_translated
  367. @notifications.listener
  368. @dependency.provider('identity_api')
  369. @dependency.requires('assignment_api', 'credential_api', 'id_mapping_api',
  370. 'resource_api', 'revoke_api', 'shadow_users_api',
  371. 'federation_api')
  372. class Manager(manager.Manager):
  373. """Default pivot point for the Identity backend.
  374. See :mod:`keystone.common.manager.Manager` for more details on how this
  375. dynamically calls the backend.
  376. This class also handles the support of domain specific backends, by using
  377. the DomainConfigs class. The setup call for DomainConfigs is called
  378. from with the @domains_configured wrapper in a lazy loading fashion
  379. to get around the fact that we can't satisfy the assignment api it needs
  380. from within our __init__() function since the assignment driver is not
  381. itself yet initialized.
  382. Each of the identity calls are pre-processed here to choose, based on
  383. domain, which of the drivers should be called. The non-domain-specific
  384. driver is still in place, and is used if there is no specific driver for
  385. the domain in question (or we are not using multiple domain drivers).
  386. Starting with Juno, in order to be able to obtain the domain from
  387. just an ID being presented as part of an API call, a public ID to domain
  388. and local ID mapping is maintained. This mapping also allows for the local
  389. ID of drivers that do not provide simple UUIDs (such as LDAP) to be
  390. referenced via a public facing ID. The mapping itself is automatically
  391. generated as entities are accessed via the driver.
  392. This mapping is only used when:
  393. - the entity is being handled by anything other than the default driver, or
  394. - the entity is being handled by the default LDAP driver and backward
  395. compatible IDs are not required.
  396. This means that in the standard case of a single SQL backend or the default
  397. settings of a single LDAP backend (since backward compatible IDs is set to
  398. True by default), no mapping is used. An alternative approach would be to
  399. always use the mapping table, but in the cases where we don't need it to
  400. make the public and local IDs the same. It is felt that not using the
  401. mapping by default is a more prudent way to introduce this functionality.
  402. """
  403. driver_namespace = 'keystone.identity'
  404. _USER = 'user'
  405. _GROUP = 'group'
  406. def __init__(self):
  407. super(Manager, self).__init__(CONF.identity.driver)
  408. self.domain_configs = DomainConfigs()
  409. self.event_callbacks = {
  410. notifications.ACTIONS.deleted: {
  411. 'domain': [self._domain_deleted],
  412. },
  413. }
  414. def _domain_deleted(self, service, resource_type, operation,
  415. payload):
  416. domain_id = payload['resource_info']
  417. user_refs = self.list_users(domain_scope=domain_id)
  418. group_refs = self.list_groups(domain_scope=domain_id)
  419. for group in group_refs:
  420. # Cleanup any existing groups.
  421. try:
  422. self.delete_group(group['id'])
  423. except exception.GroupNotFound:
  424. LOG.debug(('Group %(groupid)s not found when deleting domain '
  425. 'contents for %(domainid)s, continuing with '
  426. 'cleanup.'),
  427. {'groupid': group['id'], 'domainid': domain_id})
  428. # And finally, delete the users themselves
  429. for user in user_refs:
  430. try:
  431. self.delete_user(user['id'])
  432. except exception.UserNotFound:
  433. LOG.debug(('User %(userid)s not found when deleting domain '
  434. 'contents for %(domainid)s, continuing with '
  435. 'cleanup.'),
  436. {'userid': user['id'], 'domainid': domain_id})
  437. # Domain ID normalization methods
  438. def _set_domain_id_and_mapping(self, ref, domain_id, driver,
  439. entity_type):
  440. """Patch the domain_id/public_id into the resulting entity(ies).
  441. :param ref: the entity or list of entities to post process
  442. :param domain_id: the domain scope used for the call
  443. :param driver: the driver used to execute the call
  444. :param entity_type: whether this is a user or group
  445. :returns: post processed entity or list or entities
  446. Called to post-process the entity being returned, using a mapping
  447. to substitute a public facing ID as necessary. This method must
  448. take into account:
  449. - If the driver is not domain aware, then we must set the domain
  450. attribute of all entities irrespective of mapping.
  451. - If the driver does not support UUIDs, then we always want to provide
  452. a mapping, except for the special case of this being the default
  453. driver and backward_compatible_ids is set to True. This is to ensure
  454. that entity IDs do not change for an existing LDAP installation (only
  455. single domain/driver LDAP configurations were previously supported).
  456. - If the driver does support UUIDs, then we always create a mapping
  457. entry, but use the local UUID as the public ID. The exception to
  458. this is that if we just have single driver (i.e. not using specific
  459. multi-domain configs), then we don't bother with the mapping at all.
  460. """
  461. conf = CONF.identity
  462. if not self._needs_post_processing(driver):
  463. # a classic case would be when running with a single SQL driver
  464. return ref
  465. LOG.debug('ID Mapping - Domain ID: %(domain)s, '
  466. 'Default Driver: %(driver)s, '
  467. 'Domains: %(aware)s, UUIDs: %(generate)s, '
  468. 'Compatible IDs: %(compat)s',
  469. {'domain': domain_id,
  470. 'driver': (driver == self.driver),
  471. 'aware': driver.is_domain_aware(),
  472. 'generate': driver.generates_uuids(),
  473. 'compat': CONF.identity_mapping.backward_compatible_ids})
  474. if isinstance(ref, dict):
  475. return self._set_domain_id_and_mapping_for_single_ref(
  476. ref, domain_id, driver, entity_type, conf)
  477. elif isinstance(ref, list):
  478. return self._set_domain_id_and_mapping_for_list(
  479. ref, domain_id, driver, entity_type, conf)
  480. else:
  481. raise ValueError(_('Expected dict or list: %s') % type(ref))
  482. def _needs_post_processing(self, driver):
  483. """Return whether entity from driver needs domain added or mapping."""
  484. return (driver is not self.driver or not driver.generates_uuids() or
  485. not driver.is_domain_aware())
  486. def _insert_new_public_id(self, local_entity, ref, driver):
  487. # Need to create a mapping. If the driver generates UUIDs
  488. # then pass the local UUID in as the public ID to use.
  489. public_id = None
  490. if driver.generates_uuids():
  491. public_id = ref['id']
  492. ref['id'] = self.id_mapping_api.create_id_mapping(
  493. local_entity, public_id)
  494. LOG.debug('Created new mapping to public ID: %s', ref['id'])
  495. def _set_domain_id_and_mapping_for_single_ref(self, ref, domain_id,
  496. driver, entity_type, conf):
  497. LOG.debug('Local ID: %s', ref['id'])
  498. ref = ref.copy()
  499. self._insert_domain_id_if_needed(ref, driver, domain_id, conf)
  500. if self._is_mapping_needed(driver):
  501. local_entity = {'domain_id': ref['domain_id'],
  502. 'local_id': ref['id'],
  503. 'entity_type': entity_type}
  504. public_id = self.id_mapping_api.get_public_id(local_entity)
  505. if public_id:
  506. ref['id'] = public_id
  507. LOG.debug('Found existing mapping to public ID: %s',
  508. ref['id'])
  509. else:
  510. self._insert_new_public_id(local_entity, ref, driver)
  511. return ref
  512. def _set_domain_id_and_mapping_for_list(self, ref_list, domain_id, driver,
  513. entity_type, conf):
  514. """Set domain id and mapping for a list of refs.
  515. The method modifies refs in-place.
  516. """
  517. if not ref_list:
  518. return []
  519. for r in ref_list:
  520. self._insert_domain_id_if_needed(r, driver, domain_id, conf)
  521. if not self._is_mapping_needed(driver):
  522. return ref_list
  523. # build a map of refs for fast look-up
  524. refs_map = {}
  525. for r in ref_list:
  526. refs_map[(r['id'], entity_type, r['domain_id'])] = r
  527. # NOTE(breton): there are cases when the driver is not domain aware and
  528. # no domain_id was explicitely provided for list operation. domain_id
  529. # gets inserted into refs, but not passed into this method. Lets use
  530. # domain_id from one of the refs.
  531. if not domain_id:
  532. domain_id = ref_list[0]['domain_id']
  533. # fetch all mappings for the domain, lookup the user at the map built
  534. # at previous step and replace his id.
  535. domain_mappings = self.id_mapping_api.get_domain_mapping_list(
  536. domain_id)
  537. for _mapping in domain_mappings:
  538. idx = (_mapping.local_id, _mapping.entity_type, _mapping.domain_id)
  539. try:
  540. ref = refs_map.pop(idx)
  541. # due to python specifics, `ref` still points to an item in
  542. # `ref_list`. That's why when we change it here, it gets
  543. # changed in `ref_list`.
  544. ref['id'] = _mapping.public_id
  545. except KeyError:
  546. pass # some old entry, skip it
  547. # at this point, all known refs were granted a public_id. For the refs
  548. # left, there are no mappings. They need to be created.
  549. for ref in refs_map.values():
  550. local_entity = {'domain_id': ref['domain_id'],
  551. 'local_id': ref['id'],
  552. 'entity_type': entity_type}
  553. self._insert_new_public_id(local_entity, ref, driver)
  554. return ref_list
  555. def _insert_domain_id_if_needed(self, ref, driver, domain_id, conf):
  556. """Insert the domain ID into the ref, if required.
  557. If the driver can't handle domains, then we need to insert the
  558. domain_id into the entity being returned. If the domain_id is
  559. None that means we are running in a single backend mode, so to
  560. remain backwardly compatible, we put in the default domain ID.
  561. """
  562. if not driver.is_domain_aware():
  563. if domain_id is None:
  564. domain_id = conf.default_domain_id
  565. ref['domain_id'] = domain_id
  566. def _is_mapping_needed(self, driver):
  567. """Return whether mapping is needed.
  568. There are two situations where we must use the mapping:
  569. - this isn't the default driver (i.e. multiple backends), or
  570. - we have a single backend that doesn't use UUIDs
  571. The exception to the above is that we must honor backward
  572. compatibility if this is the default driver (e.g. to support
  573. current LDAP)
  574. """
  575. is_not_default_driver = driver is not self.driver
  576. return (is_not_default_driver or (
  577. not driver.generates_uuids() and
  578. not CONF.identity_mapping.backward_compatible_ids))
  579. def _clear_domain_id_if_domain_unaware(self, driver, ref):
  580. """Clear domain_id details if driver is not domain aware."""
  581. if not driver.is_domain_aware() and 'domain_id' in ref:
  582. ref = ref.copy()
  583. ref.pop('domain_id')
  584. return ref
  585. def _select_identity_driver(self, domain_id):
  586. """Choose a backend driver for the given domain_id.
  587. :param domain_id: The domain_id for which we want to find a driver. If
  588. the domain_id is specified as None, then this means
  589. we need a driver that handles multiple domains.
  590. :returns: chosen backend driver
  591. If there is a specific driver defined for this domain then choose it.
  592. If the domain is None, or there no specific backend for the given
  593. domain is found, then we chose the default driver.
  594. """
  595. if domain_id is None:
  596. driver = self.driver
  597. else:
  598. driver = (self.domain_configs.get_domain_driver(domain_id) or
  599. self.driver)
  600. # If the driver is not domain aware (e.g. LDAP) then check to
  601. # ensure we are not mapping multiple domains onto it - the only way
  602. # that would happen is that the default driver is LDAP and the
  603. # domain is anything other than None or the default domain.
  604. if (not driver.is_domain_aware() and driver == self.driver and
  605. domain_id != CONF.identity.default_domain_id and
  606. domain_id is not None):
  607. LOG.warning(_LW('Found multiple domains being mapped to a '
  608. 'driver that does not support that (e.g. '
  609. 'LDAP) - Domain ID: %(domain)s, '
  610. 'Default Driver: %(driver)s'),
  611. {'domain': domain_id,
  612. 'driver': (driver == self.driver)})
  613. raise exception.DomainNotFound(domain_id=domain_id)
  614. return driver
  615. def _get_domain_driver_and_entity_id(self, public_id):
  616. """Look up details using the public ID.
  617. :param public_id: the ID provided in the call
  618. :returns: domain_id, which can be None to indicate that the driver
  619. in question supports multiple domains
  620. driver selected based on this domain
  621. entity_id which will is understood by the driver.
  622. Use the mapping table to look up the domain, driver and local entity
  623. that is represented by the provided public ID. Handle the situations
  624. where we do not use the mapping (e.g. single driver that understands
  625. UUIDs etc.)
  626. """
  627. conf = CONF.identity
  628. # First, since we don't know anything about the entity yet, we must
  629. # assume it needs mapping, so long as we are using domain specific
  630. # drivers.
  631. if conf.domain_specific_drivers_enabled:
  632. local_id_ref = self.id_mapping_api.get_id_mapping(public_id)
  633. if local_id_ref:
  634. return (
  635. local_id_ref['domain_id'],
  636. self._select_identity_driver(local_id_ref['domain_id']),
  637. local_id_ref['local_id'])
  638. # So either we are using multiple drivers but the public ID is invalid
  639. # (and hence was not found in the mapping table), or the public ID is
  640. # being handled by the default driver. Either way, the only place left
  641. # to look is in that standard driver. However, we don't yet know if
  642. # this driver also needs mapping (e.g. LDAP in non backward
  643. # compatibility mode).
  644. driver = self.driver
  645. if driver.generates_uuids():
  646. if driver.is_domain_aware:
  647. # No mapping required, and the driver can handle the domain
  648. # information itself. The classic case of this is the
  649. # current SQL driver.
  650. return (None, driver, public_id)
  651. else:
  652. # Although we don't have any drivers of this type, i.e. that
  653. # understand UUIDs but not domains, conceptually you could.
  654. return (conf.default_domain_id, driver, public_id)
  655. # So the only place left to find the ID is in the default driver which
  656. # we now know doesn't generate UUIDs
  657. if not CONF.identity_mapping.backward_compatible_ids:
  658. # We are not running in backward compatibility mode, so we
  659. # must use a mapping.
  660. local_id_ref = self.id_mapping_api.get_id_mapping(public_id)
  661. if local_id_ref:
  662. return (
  663. local_id_ref['domain_id'],
  664. driver,
  665. local_id_ref['local_id'])
  666. else:
  667. raise exception.PublicIDNotFound(id=public_id)
  668. # If we reach here, this means that the default driver
  669. # requires no mapping - but also doesn't understand domains
  670. # (e.g. the classic single LDAP driver situation). Hence we pass
  671. # back the public_ID unmodified and use the default domain (to
  672. # keep backwards compatibility with existing installations).
  673. #
  674. # It is still possible that the public ID is just invalid in
  675. # which case we leave this to the caller to check.
  676. return (conf.default_domain_id, driver, public_id)
  677. def _assert_user_and_group_in_same_backend(
  678. self, user_entity_id, user_driver, group_entity_id, group_driver):
  679. """Ensure that user and group IDs are backed by the same backend.
  680. Raise a CrossBackendNotAllowed exception if they are not from the same
  681. backend, otherwise return None.
  682. """
  683. if user_driver is not group_driver:
  684. # Determine first if either IDs don't exist by calling
  685. # the driver.get methods (which will raise a NotFound
  686. # exception).
  687. user_driver.get_user(user_entity_id)
  688. group_driver.get_group(group_entity_id)
  689. # If we get here, then someone is attempting to create a cross
  690. # backend membership, which is not allowed.
  691. raise exception.CrossBackendNotAllowed(group_id=group_entity_id,
  692. user_id=user_entity_id)
  693. def _mark_domain_id_filter_satisfied(self, hints):
  694. if hints:
  695. for filter in hints.filters:
  696. if (filter['name'] == 'domain_id' and
  697. filter['comparator'] == 'equals'):
  698. hints.filters.remove(filter)
  699. def _ensure_domain_id_in_hints(self, hints, domain_id):
  700. if (domain_id is not None and
  701. not hints.get_exact_filter_by_name('domain_id')):
  702. hints.add_filter('domain_id', domain_id)
  703. def _set_list_limit_in_hints(self, hints, driver):
  704. """Set list limit in hints from driver.
  705. If a hints list is provided, the wrapper will insert the relevant
  706. limit into the hints so that the underlying driver call can try and
  707. honor it. If the driver does truncate the response, it will update the
  708. 'truncated' attribute in the 'limit' entry in the hints list, which
  709. enables the caller of this function to know if truncation has taken
  710. place. If, however, the driver layer is unable to perform truncation,
  711. the 'limit' entry is simply left in the hints list for the caller to
  712. handle.
  713. A _get_list_limit() method is required to be present in the object
  714. class hierarchy, which returns the limit for this backend to which
  715. we will truncate.
  716. If a hints list is not provided in the arguments of the wrapped call
  717. then any limits set in the config file are ignored. This allows
  718. internal use of such wrapped methods where the entire data set is
  719. needed as input for the calculations of some other API (e.g. get role
  720. assignments for a given project).
  721. This method, specific to identity manager, is used instead of more
  722. general response_truncated, because the limit for identity entities
  723. can be overridden in domain-specific config files. The driver to use
  724. is determined during processing of the passed parameters and
  725. response_truncated is designed to set the limit before any processing.
  726. """
  727. if hints is None:
  728. return
  729. list_limit = driver._get_list_limit()
  730. if list_limit:
  731. hints.set_limit(list_limit)
  732. # The actual driver calls - these are pre/post processed here as
  733. # part of the Manager layer to make sure we:
  734. #
  735. # - select the right driver for this domain
  736. # - clear/set domain_ids for drivers that do not support domains
  737. # - create any ID mapping that might be required
  738. @notifications.emit_event('authenticate')
  739. @domains_configured
  740. @exception_translated('assertion')
  741. def authenticate(self, request, user_id, password):
  742. domain_id, driver, entity_id = (
  743. self._get_domain_driver_and_entity_id(user_id))
  744. ref = driver.authenticate(entity_id, password)
  745. ref = self._set_domain_id_and_mapping(
  746. ref, domain_id, driver, mapping.EntityType.USER)
  747. ref = self._shadow_nonlocal_user(ref)
  748. self.shadow_users_api.set_last_active_at(ref['id'])
  749. return ref
  750. def _assert_default_project_id_is_not_domain(self, default_project_id):
  751. if default_project_id:
  752. # make sure project is not a domain
  753. try:
  754. project_ref = self.resource_api.get_project(default_project_id)
  755. if project_ref['is_domain'] is True:
  756. msg = _("User's default project ID cannot be a "
  757. "domain ID: %s")
  758. raise exception.ValidationError(
  759. message=(msg % default_project_id))
  760. except exception.ProjectNotFound:
  761. # should be idempotent if project is not found so that it is
  762. # backward compatible
  763. pass
  764. @domains_configured
  765. @exception_translated('user')
  766. def create_user(self, user_ref, initiator=None):
  767. user = user_ref.copy()
  768. if 'password' in user:
  769. validators.validate_password(user['password'])
  770. user['name'] = clean.user_name(user['name'])
  771. user.setdefault('enabled', True)
  772. user['enabled'] = clean.user_enabled(user['enabled'])
  773. domain_id = user['domain_id']
  774. self.resource_api.get_domain(domain_id)
  775. self._assert_default_project_id_is_not_domain(
  776. user_ref.get('default_project_id'))
  777. # For creating a user, the domain is in the object itself
  778. domain_id = user_ref['domain_id']
  779. driver = self._select_identity_driver(domain_id)
  780. user = self._clear_domain_id_if_domain_unaware(driver, user)
  781. # Generate a local ID - in the future this might become a function of
  782. # the underlying driver so that it could conform to rules set down by
  783. # that particular driver type.
  784. user['id'] = uuid.uuid4().hex
  785. ref = driver.create_user(user['id'], user)
  786. notifications.Audit.created(self._USER, user['id'], initiator)
  787. return self._set_domain_id_and_mapping(
  788. ref, domain_id, driver, mapping.EntityType.USER)
  789. @domains_configured
  790. @exception_translated('user')
  791. @MEMOIZE
  792. def get_user(self, user_id):
  793. domain_id, driver, entity_id = (
  794. self._get_domain_driver_and_entity_id(user_id))
  795. ref = driver.get_user(entity_id)
  796. return self._set_domain_id_and_mapping(
  797. ref, domain_id, driver, mapping.EntityType.USER)
  798. def assert_user_enabled(self, user_id, user=None):
  799. """Assert the user and the user's domain are enabled.
  800. :raise AssertionError if the user or the user's domain is disabled.
  801. """
  802. if user is None:
  803. user = self.get_user(user_id)
  804. self.resource_api.assert_domain_enabled(user['domain_id'])
  805. if not user.get('enabled', True):
  806. raise AssertionError(_('User is disabled: %s') % user_id)
  807. @domains_configured
  808. @exception_translated('user')
  809. @MEMOIZE
  810. def get_user_by_name(self, user_name, domain_id):
  811. driver = self._select_identity_driver(domain_id)
  812. ref = driver.get_user_by_name(user_name, domain_id)
  813. return self._set_domain_id_and_mapping(
  814. ref, domain_id, driver, mapping.EntityType.USER)
  815. def _translate_expired_password_hints(self, hints):
  816. """Clean Up Expired Password Hints.
  817. Any `password_expires_at` filters on the `list_users` or
  818. `list_users_in_group` queries are modified so the call will
  819. return valid data.
  820. The filters `comparator` is changed to the operator specified in
  821. the call, otherwise it is assumed to be `equals`. The filters
  822. `value` becomes the timestamp specified. Both the operator and
  823. timestamp are validated, and will raise a InvalidOperatorError
  824. or ValidationTimeStampError exception respectively if invalid.
  825. """
  826. operators = {'lt': operator.lt, 'gt': operator.gt,
  827. 'eq': operator.eq, 'lte': operator.le,
  828. 'gte': operator.ge, 'neq': operator.ne}
  829. for filter_ in hints.filters:
  830. if 'password_expires_at' == filter_['name']:
  831. # password_expires_at must be in the format
  832. # 'lt:2016-11-06T15:32:17Z'. So we can assume the position
  833. # of the ':' otherwise assign the operator to equals.
  834. if ':' in filter_['value'][2:4]:
  835. op, timestamp = filter_['value'].split(':', 1)
  836. else:
  837. op = 'eq'
  838. timestamp = filter_['value']
  839. try:
  840. filter_['value'] = timeutils.parse_isotime(timestamp)
  841. except ValueError:
  842. raise exception.ValidationTimeStampError
  843. try:
  844. filter_['comparator'] = operators[op]
  845. except KeyError:
  846. raise exception.InvalidOperatorError(op)
  847. return hints
  848. def _handle_federated_attributes_in_hints(self, driver, hints):
  849. federated_attributes = ['idp_id', 'protocol_id', 'unique_id']
  850. for filter_ in hints.filters:
  851. if filter_['name'] in federated_attributes:
  852. return self.shadow_users_api.get_federated_users(hints)
  853. return driver.list_users(hints)
  854. @domains_configured
  855. @exception_translated('user')
  856. def list_users(self, domain_scope=None, hints=None):
  857. driver = self._select_identity_driver(domain_scope)
  858. self._set_list_limit_in_hints(hints, driver)
  859. hints = hints or driver_hints.Hints()
  860. if driver.is_domain_aware():
  861. # Force the domain_scope into the hint to ensure that we only get
  862. # back domains for that scope.
  863. self._ensure_domain_id_in_hints(hints, domain_scope)
  864. else:
  865. # We are effectively satisfying any domain_id filter by the above
  866. # driver selection, so remove any such filter.
  867. self._mark_domain_id_filter_satisfied(hints)
  868. hints = self._translate_expired_password_hints(hints)
  869. ref_list = self._handle_federated_attributes_in_hints(driver, hints)
  870. return self._set_domain_id_and_mapping(
  871. ref_list, domain_scope, driver, mapping.EntityType.USER)
  872. def _require_matching_domain_id(self, new_ref, orig_ref):
  873. """Ensure the current domain ID matches the reference one, if any.
  874. Provided we want domain IDs to be immutable, check whether any
  875. domain_id specified in the ref dictionary matches the existing
  876. domain_id for this entity.
  877. :param new_ref: the dictionary of new values proposed for this entity
  878. :param orig_ref: the dictionary of original values proposed for this
  879. entity
  880. :raises: :class:`keystone.exception.ValidationError`
  881. """
  882. if 'domain_id' in new_ref:
  883. if new_ref['domain_id'] != orig_ref['domain_id']:
  884. raise exception.ValidationError(_('Cannot change Domain ID'))
  885. @domains_configured
  886. @exception_translated('user')
  887. def update_user(self, user_id, user_ref, initiator=None):
  888. old_user_ref = self.get_user(user_id)
  889. user = user_ref.copy()
  890. self._require_matching_domain_id(user, old_user_ref)
  891. if 'password' in user:
  892. validators.validate_password(user['password'])
  893. if 'name' in user:
  894. user['name'] = clean.user_name(user['name'])
  895. if 'enabled' in user:
  896. user['enabled'] = clean.user_enabled(user['enabled'])
  897. if 'id' in user:
  898. if user_id != user['id']:
  899. raise exception.ValidationError(_('Cannot change user ID'))
  900. # Since any ID in the user dict is now irrelevant, remove its so as
  901. # the driver layer won't be confused by the fact the this is the
  902. # public ID not the local ID
  903. user.pop('id')
  904. self._assert_default_project_id_is_not_domain(
  905. user_ref.get('default_project_id'))
  906. domain_id, driver, entity_id = (
  907. self._get_domain_driver_and_entity_id(user_id))
  908. user = self._clear_domain_id_if_domain_unaware(driver, user)
  909. self.get_user.invalidate(self, old_user_ref['id'])
  910. self.get_user_by_name.invalidate(self, old_user_ref['name'],
  911. old_user_ref['domain_id'])
  912. ref = driver.update_user(entity_id, user)
  913. notifications.Audit.updated(self._USER, user_id, initiator)
  914. enabled_change = ((user.get('enabled') is False) and
  915. user['enabled'] != old_user_ref.get('enabled'))
  916. if enabled_change or user.get('password') is not None:
  917. self.emit_invalidate_user_token_persistence(user_id)
  918. return self._set_domain_id_and_mapping(
  919. ref, domain_id, driver, mapping.EntityType.USER)
  920. @domains_configured
  921. @exception_translated('user')
  922. def delete_user(self, user_id, initiator=None):
  923. domain_id, driver, entity_id = (
  924. self._get_domain_driver_and_entity_id(user_id))
  925. # Get user details to invalidate the cache.
  926. user_old = self.get_user(user_id)
  927. driver.delete_user(entity_id)
  928. self.assignment_api.delete_user_assignments(user_id)
  929. self.get_user.invalidate(self, user_id)
  930. self.get_user_by_name.invalidate(self, user_old['name'],
  931. user_old['domain_id'])
  932. self.credential_api.delete_credentials_for_user(user_id)
  933. self.id_mapping_api.delete_id_mapping(user_id)
  934. notifications.Audit.deleted(self._USER, user_id, initiator)
  935. # Invalidate user role assignments cache region, as it may be caching
  936. # role assignments where the actor is the specified user
  937. assignment.COMPUTED_ASSIGNMENTS_REGION.invalidate()
  938. @domains_configured
  939. @exception_translated('group')
  940. def create_group(self, group_ref, initiator=None):
  941. group = group_ref.copy()
  942. group.setdefault('description', '')
  943. domain_id = group['domain_id']
  944. self.resource_api.get_domain(domain_id)
  945. # For creating a group, the domain is in the object itself
  946. domain_id = group_ref['domain_id']
  947. driver = self._select_identity_driver(domain_id)
  948. group = self._clear_domain_id_if_domain_unaware(driver, group)
  949. # Generate a local ID - in the future this might become a function of
  950. # the underlying driver so that it could conform to rules set down by
  951. # that particular driver type.
  952. group['id'] = uuid.uuid4().hex
  953. group['name'] = clean.group_name(group['name'])
  954. ref = driver.create_group(group['id'], group)
  955. notifications.Audit.created(self._GROUP, group['id'], initiator)
  956. return self._set_domain_id_and_mapping(
  957. ref, domain_id, driver, mapping.EntityType.GROUP)
  958. @domains_configured
  959. @exception_translated('group')
  960. @MEMOIZE
  961. def get_group(self, group_id):
  962. domain_id, driver, entity_id = (
  963. self._get_domain_driver_and_entity_id(group_id))
  964. ref = driver.get_group(entity_id)
  965. return self._set_domain_id_and_mapping(
  966. ref, domain_id, driver, mapping.EntityType.GROUP)
  967. @domains_configured
  968. @exception_translated('group')
  969. def get_group_by_name(self, group_name, domain_id):
  970. driver = self._select_identity_driver(domain_id)
  971. ref = driver.get_group_by_name(group_name, domain_id)
  972. return self._set_domain_id_and_mapping(
  973. ref, domain_id, driver, mapping.EntityType.GROUP)
  974. @domains_configured
  975. @exception_translated('group')
  976. def update_group(self, group_id, group, initiator=None):
  977. old_group_ref = self.get_group(group_id)
  978. self._require_matching_domain_id(group, old_group_ref)
  979. domain_id, driver, entity_id = (
  980. self._get_domain_driver_and_entity_id(group_id))
  981. group = self._clear_domain_id_if_domain_unaware(driver, group)
  982. if 'name' in group:
  983. group['name'] = clean.group_name(group['name'])
  984. ref = driver.update_group(entity_id, group)
  985. self.get_group.invalidate(self, group_id)
  986. notifications.Audit.updated(self._GROUP, group_id, initiator)
  987. return self._set_domain_id_and_mapping(
  988. ref, domain_id, driver, mapping.EntityType.GROUP)
  989. @domains_configured
  990. @exception_translated('group')
  991. def delete_group(self, group_id, initiator=None):
  992. domain_id, driver, entity_id = (
  993. self._get_domain_driver_and_entity_id(group_id))
  994. roles = self.assignment_api.list_role_assignments(group_id=group_id)
  995. user_ids = (u['id'] for u in self.list_users_in_group(group_id))
  996. driver.delete_group(entity_id)
  997. self.get_group.invalidate(self, group_id)
  998. self.id_mapping_api.delete_id_mapping(group_id)
  999. self.assignment_api.delete_group_assignments(group_id)
  1000. notifications.Audit.deleted(self._GROUP, group_id, initiator)
  1001. # If the group has been created and has users but has no role
  1002. # assignment for the group then we do not need to revoke all the users
  1003. # tokens and can just delete the group.
  1004. if roles:
  1005. for uid in user_ids:
  1006. self.emit_invalidate_user_token_persistence(uid)
  1007. # Invalidate user role assignments cache region, as it may be caching
  1008. # role assignments expanded from the specified group to its users
  1009. assignment.COMPUTED_ASSIGNMENTS_REGION.invalidate()
  1010. @domains_configured
  1011. @exception_translated('group')
  1012. def add_user_to_group(self, user_id, group_id, initiator=None):
  1013. @exception_translated('user')
  1014. def get_entity_info_for_user(public_id):
  1015. return self._get_domain_driver_and_entity_id(public_id)
  1016. _domain_id, group_driver, group_entity_id = (
  1017. self._get_domain_driver_and_entity_id(group_id))
  1018. # Get the same info for the user_id, taking care to map any
  1019. # exceptions correctly
  1020. _domain_id, user_driver, user_entity_id = (
  1021. get_entity_info_for_user(user_id))
  1022. self._assert_user_and_group_in_same_backend(
  1023. user_entity_id, user_driver, group_entity_id, group_driver)
  1024. group_driver.add_user_to_group(user_entity_id, group_entity_id)
  1025. # Invalidate user role assignments cache region, as it may now need to
  1026. # include role assignments from the specified group to its users
  1027. assignment.COMPUTED_ASSIGNMENTS_REGION.invalidate()
  1028. notifications.Audit.added_to(self._GROUP, group_id, self._USER,
  1029. user_id, initiator)
  1030. @domains_configured
  1031. @exception_translated('group')
  1032. def remove_user_from_group(self, user_id, group_id, initiator=None):
  1033. @exception_translated('user')
  1034. def get_entity_info_for_user(public_id):
  1035. return self._get_domain_driver_and_entity_id(public_id)
  1036. _domain_id, group_driver, group_entity_id = (
  1037. self._get_domain_driver_and_entity_id(group_id))
  1038. # Get the same info for the user_id, taking care to map any
  1039. # exceptions correctly
  1040. _domain_id, user_driver, user_entity_id = (
  1041. get_entity_info_for_user(user_id))
  1042. self._assert_user_and_group_in_same_backend(
  1043. user_entity_id, user_driver, group_entity_id, group_driver)
  1044. group_driver.remove_user_from_group(user_entity_id, group_entity_id)
  1045. self.emit_invalidate_user_token_persistence(user_id)
  1046. # Invalidate user role assignments cache region, as it may be caching
  1047. # role assignments expanded from this group to this user
  1048. assignment.COMPUTED_ASSIGNMENTS_REGION.invalidate()
  1049. notifications.Audit.removed_from(self._GROUP, group_id, self._USER,
  1050. user_id, initiator)
  1051. def emit_invalidate_user_token_persistence(self, user_id):
  1052. """Emit a notification to the callback system to revoke user tokens.
  1053. This method and associated callback listener removes the need for
  1054. making a direct call to another manager to delete and revoke tokens.
  1055. :param user_id: user identifier
  1056. :type user_id: string
  1057. """
  1058. notifications.Audit.internal(
  1059. notifications.INVALIDATE_USER_TOKEN_PERSISTENCE, user_id
  1060. )
  1061. def emit_invalidate_grant_token_persistence(self, user_project):
  1062. """Emit a notification to the callback system to revoke grant tokens.
  1063. This method and associated callback listener removes the need for
  1064. making a direct call to another manager to delete and revoke tokens.
  1065. :param user_project: {'user_id': user_id, 'project_id': project_id}
  1066. :type user_project: dict
  1067. """
  1068. notifications.Audit.internal(
  1069. notifications.INVALIDATE_USER_PROJECT_TOKEN_PERSISTENCE,
  1070. user_project
  1071. )
  1072. @domains_configured
  1073. @exception_translated('user')
  1074. def list_groups_for_user(self, user_id, hints=None):
  1075. domain_id, driver, entity_id = (
  1076. self._get_domain_driver_and_entity_id(user_id))
  1077. self._set_list_limit_in_hints(hints, driver)
  1078. hints = hints or driver_hints.Hints()
  1079. if not driver.is_domain_aware():
  1080. # We are effectively satisfying any domain_id filter by the above
  1081. # driver selection, so remove any such filter
  1082. self._mark_domain_id_filter_satisfied(hints)
  1083. ref_list = driver.list_groups_for_user(entity_id, hints)
  1084. return self._set_domain_id_and_mapping(
  1085. ref_list, domain_id, driver, mapping.EntityType.GROUP)
  1086. @domains_configured
  1087. @exception_translated('group')
  1088. def list_groups(self, domain_scope=None, hints=None):
  1089. driver = self._select_identity_driver(domain_scope)
  1090. self._set_list_limit_in_hints(hints, driver)
  1091. hints = hints or driver_hints.Hints()
  1092. if driver.is_domain_aware():
  1093. # Force the domain_scope into the hint to ensure that we only get
  1094. # back domains for that scope.
  1095. self._ensure_domain_id_in_hints(hints, domain_scope)
  1096. else:
  1097. # We are effectively satisfying any domain_id filter by the above
  1098. # driver selection, so remove any such filter.
  1099. self._mark_domain_id_filter_satisfied(hints)
  1100. ref_list = driver.list_groups(hints)
  1101. return self._set_domain_id_and_mapping(
  1102. ref_list, domain_scope, driver, mapping.EntityType.GROUP)
  1103. @domains_configured
  1104. @exception_translated('group')
  1105. def list_users_in_group(self, group_id, hints=None):
  1106. domain_id, driver, entity_id = (
  1107. self._get_domain_driver_and_entity_id(group_id))
  1108. self._set_list_limit_in_hints(hints, driver)
  1109. hints = hints or driver_hints.Hints()
  1110. if not driver.is_domain_aware():
  1111. # We are effectively satisfying any domain_id filter by the above
  1112. # driver selection, so remove any such filter
  1113. self._mark_domain_id_filter_satisfied(hints)
  1114. hints = self._translate_expired_password_hints(hints)
  1115. ref_list = driver.list_users_in_group(entity_id, hints)
  1116. return self._set_domain_id_and_mapping(
  1117. ref_list, domain_id, driver, mapping.EntityType.USER)
  1118. @domains_configured
  1119. @exception_translated('group')
  1120. def check_user_in_group(self, user_id, group_id):
  1121. @exception_translated('user')
  1122. def get_entity_info_for_user(public_id):
  1123. return self._get_domain_driver_and_entity_id(public_id)
  1124. _domain_id, group_driver, group_entity_id = (
  1125. self._get_domain_driver_and_entity_id(group_id))
  1126. # Get the same info for the user_id, taking care to map any
  1127. # exceptions correctly
  1128. _domain_id, user_driver, user_entity_id = (
  1129. get_entity_info_for_user(user_id))
  1130. self._assert_user_and_group_in_same_backend(
  1131. user_entity_id, user_driver, group_entity_id, group_driver)
  1132. return group_driver.check_user_in_group(user_entity_id,
  1133. group_entity_id)
  1134. @domains_configured
  1135. def change_password(self, request, user_id, original_password,
  1136. new_password, initiator=None):
  1137. # authenticate() will raise an AssertionError if authentication fails
  1138. try:
  1139. self.authenticate(request, user_id, original_password)
  1140. except exception.PasswordExpired:
  1141. # If a password has expired, we want users to be able to change it
  1142. pass
  1143. domain_id, driver, entity_id = (
  1144. self._get_domain_driver_and_entity_id(user_id))
  1145. try:
  1146. validators.validate_password(new_password)
  1147. driver.change_password(entity_id, new_password)
  1148. except exception.PasswordValidationError as ex:
  1149. audit_reason = reason.Reason(str(ex), str(ex.code))
  1150. notifications.Audit.updated(self._USER, user_id,
  1151. initiator, reason=audit_reason)
  1152. raise
  1153. notifications.Audit.updated(self._USER, user_id, initiator)
  1154. self.emit_invalidate_user_token_persistence(user_id)
  1155. @MEMOIZE
  1156. def _shadow_nonlocal_user(self, user):
  1157. try:
  1158. return self.shadow_users_api.get_user(user['id'])
  1159. except exception.UserNotFound:
  1160. return self.shadow_users_api.create_nonlocal_user(user)
  1161. @MEMOIZE
  1162. def shadow_federated_user(self, idp_id, protocol_id, unique_id,
  1163. display_name):
  1164. """Map a federated user to a user.
  1165. :param idp_id: identity provider id
  1166. :param protocol_id: protocol id
  1167. :param unique_id: unique id for the user within the IdP
  1168. :param display_name: user's display name
  1169. :returns: dictionary of the mapped User entity
  1170. """
  1171. user_dict = {}
  1172. try:
  1173. self.shadow_users_api.update_federated_user_display_name(
  1174. idp_id, protocol_id, unique_id, display_name)
  1175. user_dict = self.shadow_users_api.get_federated_user(
  1176. idp_id, protocol_id, unique_id)
  1177. except exception.UserNotFound:
  1178. idp = self.federation_api.get_idp(idp_id)
  1179. federated_dict = {
  1180. 'idp_id': idp_id,
  1181. 'protocol_id': protocol_id,
  1182. 'unique_id': unique_id,
  1183. 'display_name': display_name
  1184. }
  1185. user_dict = (
  1186. self.shadow_users_api.create_federated_user(idp['domain_id'],
  1187. federated_dict))
  1188. self.shadow_users_api.set_last_active_at(user_dict['id'])
  1189. return user_dict
  1190. @dependency.provider('id_mapping_api')
  1191. class MappingManager(manager.Manager):
  1192. """Default pivot point for the ID Mapping backend."""
  1193. driver_namespace = 'keystone.identity.id_mapping'
  1194. def __init__(self):
  1195. super(MappingManager, self).__init__(CONF.identity_mapping.driver)
  1196. @MEMOIZE_ID_MAPPING
  1197. def _get_public_id(self, domain_id, local_id, entity_type):
  1198. return self.driver.get_public_id({'domain_id': domain_id,
  1199. 'local_id': local_id,
  1200. 'entity_type': entity_type})
  1201. def get_public_id(self, local_entity):
  1202. return self._get_public_id(local_entity['domain_id'],
  1203. local_entity['local_id'],
  1204. local_entity['entity_type'])
  1205. @MEMOIZE_ID_MAPPING
  1206. def get_id_mapping(self, public_id):
  1207. return self.driver.get_id_mapping(public_id)
  1208. def create_id_mapping(self, local_entity, public_id=None):
  1209. public_id = self.driver.create_id_mapping(local_entity, public_id)
  1210. if MEMOIZE_ID_MAPPING.should_cache(public_id):
  1211. self._get_public_id.set(public_id, self,
  1212. local_entity['domain_id'],
  1213. local_entity['local_id'],
  1214. local_entity['entity_type'])
  1215. self.get_id_mapping.set(local_entity, self, public_id)
  1216. return public_id
  1217. def delete_id_mapping(self, public_id):
  1218. local_entity = self.get_id_mapping.get(self, public_id)
  1219. self.driver.delete_id_mapping(public_id)
  1220. # Delete the key of entity from cache
  1221. if local_entity:
  1222. self._get_public_id.invalidate(self, local_entity['domain_id'],
  1223. local_entity['local_id'],
  1224. local_entity['entity_type'])
  1225. self.get_id_mapping.invalidate(self, public_id)
  1226. def purge_mappings(self, purge_filter):
  1227. # Purge mapping is rarely used and only used by the command client,
  1228. # it's quite complex to invalidate part of the cache based on the purge
  1229. # filters, so here invalidate the whole cache when purging mappings.
  1230. self.driver.purge_mappings(purge_filter)
  1231. ID_MAPPING_REGION.invalidate()
  1232. @dependency.provider('shadow_users_api')
  1233. class ShadowUsersManager(manager.Manager):
  1234. """Default pivot point for the Shadow Users backend."""
  1235. driver_namespace = 'keystone.identity.shadow_users'
  1236. def __init__(self):
  1237. shadow_driver = CONF.shadow_users.driver
  1238. super(ShadowUsersManager, self).__init__(shadow_driver)