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.

sql.py 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. # Copyright 2012-13 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. from keystone.assignment.backends import base
  15. from keystone.common import sql
  16. from keystone import exception
  17. from keystone.i18n import _
  18. class AssignmentType(object):
  19. USER_PROJECT = 'UserProject'
  20. GROUP_PROJECT = 'GroupProject'
  21. USER_DOMAIN = 'UserDomain'
  22. GROUP_DOMAIN = 'GroupDomain'
  23. @classmethod
  24. def calculate_type(cls, user_id, group_id, project_id, domain_id):
  25. if user_id:
  26. if project_id:
  27. return cls.USER_PROJECT
  28. if domain_id:
  29. return cls.USER_DOMAIN
  30. if group_id:
  31. if project_id:
  32. return cls.GROUP_PROJECT
  33. if domain_id:
  34. return cls.GROUP_DOMAIN
  35. # Invalid parameters combination
  36. raise exception.AssignmentTypeCalculationError(**locals())
  37. class Assignment(base.AssignmentDriverBase):
  38. @classmethod
  39. def default_role_driver(cls):
  40. return 'sql'
  41. def create_grant(self, role_id, user_id=None, group_id=None,
  42. domain_id=None, project_id=None,
  43. inherited_to_projects=False):
  44. assignment_type = AssignmentType.calculate_type(
  45. user_id, group_id, project_id, domain_id)
  46. try:
  47. with sql.session_for_write() as session:
  48. session.add(RoleAssignment(
  49. type=assignment_type,
  50. actor_id=user_id or group_id,
  51. target_id=project_id or domain_id,
  52. role_id=role_id,
  53. inherited=inherited_to_projects))
  54. except sql.DBDuplicateEntry: # nosec : The v3 grant APIs are silent if
  55. # the assignment already exists
  56. pass
  57. def list_grant_role_ids(self, user_id=None, group_id=None,
  58. domain_id=None, project_id=None,
  59. inherited_to_projects=False):
  60. with sql.session_for_read() as session:
  61. q = session.query(RoleAssignment.role_id)
  62. q = q.filter(RoleAssignment.actor_id == (user_id or group_id))
  63. q = q.filter(RoleAssignment.target_id == (project_id or domain_id))
  64. q = q.filter(RoleAssignment.inherited == inherited_to_projects)
  65. return [x.role_id for x in q.all()]
  66. def _build_grant_filter(self, session, role_id, user_id, group_id,
  67. domain_id, project_id, inherited_to_projects):
  68. q = session.query(RoleAssignment)
  69. q = q.filter_by(actor_id=user_id or group_id)
  70. q = q.filter_by(target_id=project_id or domain_id)
  71. q = q.filter_by(role_id=role_id)
  72. q = q.filter_by(inherited=inherited_to_projects)
  73. return q
  74. def check_grant_role_id(self, role_id, user_id=None, group_id=None,
  75. domain_id=None, project_id=None,
  76. inherited_to_projects=False):
  77. with sql.session_for_read() as session:
  78. try:
  79. q = self._build_grant_filter(
  80. session, role_id, user_id, group_id, domain_id, project_id,
  81. inherited_to_projects)
  82. q.one()
  83. except sql.NotFound:
  84. actor_id = user_id or group_id
  85. target_id = domain_id or project_id
  86. raise exception.RoleAssignmentNotFound(role_id=role_id,
  87. actor_id=actor_id,
  88. target_id=target_id)
  89. def delete_grant(self, role_id, user_id=None, group_id=None,
  90. domain_id=None, project_id=None,
  91. inherited_to_projects=False):
  92. with sql.session_for_write() as session:
  93. q = self._build_grant_filter(
  94. session, role_id, user_id, group_id, domain_id, project_id,
  95. inherited_to_projects)
  96. if not q.delete(False):
  97. actor_id = user_id or group_id
  98. target_id = domain_id or project_id
  99. raise exception.RoleAssignmentNotFound(role_id=role_id,
  100. actor_id=actor_id,
  101. target_id=target_id)
  102. def add_role_to_user_and_project(self, user_id, project_id, role_id):
  103. try:
  104. with sql.session_for_write() as session:
  105. session.add(RoleAssignment(
  106. type=AssignmentType.USER_PROJECT,
  107. actor_id=user_id, target_id=project_id,
  108. role_id=role_id, inherited=False))
  109. except sql.DBDuplicateEntry:
  110. msg = ('User %s already has role %s in tenant %s'
  111. % (user_id, role_id, project_id))
  112. raise exception.Conflict(type='role grant', details=msg)
  113. def remove_role_from_user_and_project(self, user_id, project_id, role_id):
  114. with sql.session_for_write() as session:
  115. q = session.query(RoleAssignment)
  116. q = q.filter_by(actor_id=user_id)
  117. q = q.filter_by(target_id=project_id)
  118. q = q.filter_by(role_id=role_id)
  119. if q.delete() == 0:
  120. raise exception.RoleNotFound(message=_(
  121. 'Cannot remove role that has not been granted, %s') %
  122. role_id)
  123. def _get_user_assignment_types(self):
  124. return [AssignmentType.USER_PROJECT, AssignmentType.USER_DOMAIN]
  125. def _get_group_assignment_types(self):
  126. return [AssignmentType.GROUP_PROJECT, AssignmentType.GROUP_DOMAIN]
  127. def _get_project_assignment_types(self):
  128. return [AssignmentType.USER_PROJECT, AssignmentType.GROUP_PROJECT]
  129. def _get_domain_assignment_types(self):
  130. return [AssignmentType.USER_DOMAIN, AssignmentType.GROUP_DOMAIN]
  131. def _get_assignment_types(self, user, group, project, domain):
  132. """Return a list of role assignment types based on provided entities.
  133. If one of user or group (the "actor") as well as one of project or
  134. domain (the "target") are provided, the list will contain the role
  135. assignment type for that specific pair of actor and target.
  136. If only an actor or target is provided, the list will contain the
  137. role assignment types that satisfy the specified entity.
  138. For example, if user and project are provided, the return will be:
  139. [AssignmentType.USER_PROJECT]
  140. However, if only user was provided, the return would be:
  141. [AssignmentType.USER_PROJECT, AssignmentType.USER_DOMAIN]
  142. It is not expected that user and group (or project and domain) are
  143. specified - but if they are, the most fine-grained value will be
  144. chosen (i.e. user over group, project over domain).
  145. """
  146. actor_types = []
  147. if user:
  148. actor_types = self._get_user_assignment_types()
  149. elif group:
  150. actor_types = self._get_group_assignment_types()
  151. target_types = []
  152. if project:
  153. target_types = self._get_project_assignment_types()
  154. elif domain:
  155. target_types = self._get_domain_assignment_types()
  156. if actor_types and target_types:
  157. return list(set(actor_types).intersection(target_types))
  158. return actor_types or target_types
  159. def list_role_assignments(self, role_id=None,
  160. user_id=None, group_ids=None,
  161. domain_id=None, project_ids=None,
  162. inherited_to_projects=None):
  163. def denormalize_role(ref):
  164. assignment = {}
  165. if ref.type == AssignmentType.USER_PROJECT:
  166. assignment['user_id'] = ref.actor_id
  167. assignment['project_id'] = ref.target_id
  168. elif ref.type == AssignmentType.USER_DOMAIN:
  169. assignment['user_id'] = ref.actor_id
  170. assignment['domain_id'] = ref.target_id
  171. elif ref.type == AssignmentType.GROUP_PROJECT:
  172. assignment['group_id'] = ref.actor_id
  173. assignment['project_id'] = ref.target_id
  174. elif ref.type == AssignmentType.GROUP_DOMAIN:
  175. assignment['group_id'] = ref.actor_id
  176. assignment['domain_id'] = ref.target_id
  177. else:
  178. raise exception.Error(message=_(
  179. 'Unexpected assignment type encountered, %s') %
  180. ref.type)
  181. assignment['role_id'] = ref.role_id
  182. if ref.inherited:
  183. assignment['inherited_to_projects'] = 'projects'
  184. return assignment
  185. with sql.session_for_read() as session:
  186. assignment_types = self._get_assignment_types(
  187. user_id, group_ids, project_ids, domain_id)
  188. targets = None
  189. if project_ids:
  190. targets = project_ids
  191. elif domain_id:
  192. targets = [domain_id]
  193. actors = None
  194. if group_ids:
  195. actors = group_ids
  196. elif user_id:
  197. actors = [user_id]
  198. query = session.query(RoleAssignment)
  199. if role_id:
  200. query = query.filter_by(role_id=role_id)
  201. if actors:
  202. query = query.filter(RoleAssignment.actor_id.in_(actors))
  203. if targets:
  204. query = query.filter(RoleAssignment.target_id.in_(targets))
  205. if assignment_types:
  206. query = query.filter(RoleAssignment.type.in_(assignment_types))
  207. if inherited_to_projects is not None:
  208. query = query.filter_by(inherited=inherited_to_projects)
  209. return [denormalize_role(ref) for ref in query.all()]
  210. def delete_project_assignments(self, project_id):
  211. with sql.session_for_write() as session:
  212. q = session.query(RoleAssignment)
  213. q = q.filter_by(target_id=project_id).filter(
  214. RoleAssignment.type.in_((AssignmentType.USER_PROJECT,
  215. AssignmentType.GROUP_PROJECT))
  216. )
  217. q.delete(False)
  218. def delete_role_assignments(self, role_id):
  219. with sql.session_for_write() as session:
  220. q = session.query(RoleAssignment)
  221. q = q.filter_by(role_id=role_id)
  222. q.delete(False)
  223. def delete_domain_assignments(self, domain_id):
  224. with sql.session_for_write() as session:
  225. q = session.query(RoleAssignment)
  226. q = q.filter(RoleAssignment.target_id == domain_id).filter(
  227. (RoleAssignment.type == AssignmentType.USER_DOMAIN) |
  228. (RoleAssignment.type == AssignmentType.GROUP_DOMAIN))
  229. q.delete(False)
  230. def delete_user_assignments(self, user_id):
  231. with sql.session_for_write() as session:
  232. q = session.query(RoleAssignment)
  233. q = q.filter_by(actor_id=user_id).filter(
  234. RoleAssignment.type.in_((AssignmentType.USER_PROJECT,
  235. AssignmentType.USER_DOMAIN))
  236. )
  237. q.delete(False)
  238. def delete_group_assignments(self, group_id):
  239. with sql.session_for_write() as session:
  240. q = session.query(RoleAssignment)
  241. q = q.filter_by(actor_id=group_id).filter(
  242. RoleAssignment.type.in_((AssignmentType.GROUP_PROJECT,
  243. AssignmentType.GROUP_DOMAIN))
  244. )
  245. q.delete(False)
  246. def create_system_grant(self, role_id, actor_id, target_id,
  247. assignment_type, inherited):
  248. try:
  249. with sql.session_for_write() as session:
  250. session.add(
  251. SystemRoleAssignment(
  252. type=assignment_type,
  253. actor_id=actor_id,
  254. target_id=target_id,
  255. role_id=role_id,
  256. inherited=inherited
  257. )
  258. )
  259. except sql.DBDuplicateEntry: # nosec : The v3 grant APIs are silent if
  260. # the assignment already exists
  261. pass
  262. def list_system_grants(self, actor_id, target_id, assignment_type):
  263. with sql.session_for_read() as session:
  264. query = session.query(SystemRoleAssignment)
  265. if actor_id:
  266. query = query.filter_by(actor_id=actor_id)
  267. if target_id:
  268. query = query.filter_by(target_id=target_id)
  269. if assignment_type:
  270. query = query.filter_by(type=assignment_type)
  271. results = query.all()
  272. return [role.to_dict() for role in results]
  273. def list_system_grants_by_role(self, role_id):
  274. with sql.session_for_read() as session:
  275. query = session.query(SystemRoleAssignment)
  276. query = query.filter_by(role_id=role_id)
  277. return query.all()
  278. def check_system_grant(self, role_id, actor_id, target_id, inherited):
  279. with sql.session_for_read() as session:
  280. try:
  281. q = session.query(SystemRoleAssignment)
  282. q = q.filter_by(actor_id=actor_id)
  283. q = q.filter_by(target_id=target_id)
  284. q = q.filter_by(role_id=role_id)
  285. q = q.filter_by(inherited=inherited)
  286. q.one()
  287. except sql.NotFound:
  288. raise exception.RoleAssignmentNotFound(
  289. role_id=role_id, actor_id=actor_id, target_id=target_id
  290. )
  291. def delete_system_grant(self, role_id, actor_id, target_id, inherited):
  292. with sql.session_for_write() as session:
  293. q = session.query(SystemRoleAssignment)
  294. q = q.filter_by(actor_id=actor_id)
  295. q = q.filter_by(target_id=target_id)
  296. q = q.filter_by(role_id=role_id)
  297. q = q.filter_by(inherited=inherited)
  298. if not q.delete(False):
  299. raise exception.RoleAssignmentNotFound(
  300. role_id=role_id, actor_id=actor_id, target_id=target_id
  301. )
  302. class RoleAssignment(sql.ModelBase, sql.ModelDictMixin):
  303. __tablename__ = 'assignment'
  304. attributes = ['type', 'actor_id', 'target_id', 'role_id', 'inherited']
  305. # NOTE(henry-nash): Postgres requires a name to be defined for an Enum
  306. type = sql.Column(
  307. sql.Enum(AssignmentType.USER_PROJECT, AssignmentType.GROUP_PROJECT,
  308. AssignmentType.USER_DOMAIN, AssignmentType.GROUP_DOMAIN,
  309. name='type'),
  310. nullable=False)
  311. actor_id = sql.Column(sql.String(64), nullable=False)
  312. target_id = sql.Column(sql.String(64), nullable=False)
  313. role_id = sql.Column(sql.String(64), nullable=False)
  314. inherited = sql.Column(sql.Boolean, default=False, nullable=False)
  315. __table_args__ = (
  316. sql.PrimaryKeyConstraint('type', 'actor_id', 'target_id', 'role_id',
  317. 'inherited'),
  318. sql.Index('ix_actor_id', 'actor_id'),
  319. )
  320. def to_dict(self):
  321. """Override parent method with a simpler implementation.
  322. RoleAssignment doesn't have non-indexed 'extra' attributes, so the
  323. parent implementation is not applicable.
  324. """
  325. return dict(self.items())
  326. class SystemRoleAssignment(sql.ModelBase, sql.ModelDictMixin):
  327. __tablename__ = 'system_assignment'
  328. attributes = ['type', 'actor_id', 'target_id', 'role_id', 'inherited']
  329. type = sql.Column(sql.String(64), nullable=False)
  330. actor_id = sql.Column(sql.String(64), nullable=False)
  331. target_id = sql.Column(sql.String(64), nullable=False)
  332. role_id = sql.Column(sql.String(64), nullable=False)
  333. inherited = sql.Column(sql.Boolean, default=False, nullable=False)
  334. __table_args__ = (
  335. sql.PrimaryKeyConstraint('type', 'actor_id', 'target_id', 'role_id',
  336. 'inherited'),
  337. sql.Index('ix_system_actor_id', 'actor_id'),
  338. )
  339. def to_dict(self):
  340. """Override parent method with a simpler implementation.
  341. RoleAssignment doesn't have non-indexed 'extra' attributes, so the
  342. parent implementation is not applicable.
  343. """
  344. return dict(self.items())