187ca821a02593f29d2fb3aed374b033b8aa6730
1 # -*- coding: utf-8 -*-
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
17 from uuid
import uuid4
18 from hashlib
import sha256
19 from http
import HTTPStatus
21 from validation
import user_new_schema
, user_edit_schema
, project_new_schema
, project_edit_schema
22 from validation
import vim_account_new_schema
, vim_account_edit_schema
, sdn_new_schema
, sdn_edit_schema
23 from validation
import wim_account_new_schema
, wim_account_edit_schema
, roles_new_schema
, roles_edit_schema
24 from validation
import validate_input
25 from validation
import ValidationError
26 from validation
import is_valid_uuid
# To check that User/Project Names don't look like UUIDs
27 from base_topic
import BaseTopic
, EngineException
28 from authconn_keystone
import AuthconnKeystone
30 __author__
= "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>"
33 class UserTopic(BaseTopic
):
36 schema_new
= user_new_schema
37 schema_edit
= user_edit_schema
40 def __init__(self
, db
, fs
, msg
):
41 BaseTopic
.__init
__(self
, db
, fs
, msg
)
44 def _get_project_filter(session
):
46 Generates a filter dictionary for querying database users.
47 Current policy is admin can show all, non admin, only its own user.
48 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
51 if session
["admin"]: # allows all
54 return {"username": session
["username"]}
56 def check_conflict_on_new(self
, session
, indata
):
57 # check username not exists
58 if self
.db
.get_one(self
.topic
, {"username": indata
.get("username")}, fail_on_empty
=False, fail_on_more
=False):
59 raise EngineException("username '{}' exists".format(indata
["username"]), HTTPStatus
.CONFLICT
)
61 if not session
["force"]:
62 for p
in indata
.get("projects") or []:
63 # To allow project addressing by Name as well as ID
64 if not self
.db
.get_one("projects", {BaseTopic
.id_field("projects", p
): p
}, fail_on_empty
=False,
66 raise EngineException("project '{}' does not exist".format(p
), HTTPStatus
.CONFLICT
)
68 def check_conflict_on_del(self
, session
, _id
, db_content
):
70 Check if deletion can be done because of dependencies if it is not force. To override
71 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
72 :param _id: internal _id
73 :param db_content: The database content of this item _id
74 :return: None if ok or raises EngineException with the conflict
76 if _id
== session
["username"]:
77 raise EngineException("You cannot delete your own user", http_code
=HTTPStatus
.CONFLICT
)
80 def format_on_new(content
, project_id
=None, make_public
=False):
81 BaseTopic
.format_on_new(content
, make_public
=False)
82 # Removed so that the UUID is kept, to allow User Name modification
83 # content["_id"] = content["username"]
85 content
["_admin"]["salt"] = salt
86 if content
.get("password"):
87 content
["password"] = sha256(content
["password"].encode('utf-8') + salt
.encode('utf-8')).hexdigest()
88 if content
.get("project_role_mappings"):
89 projects
= [mapping
[0] for mapping
in content
["project_role_mappings"]]
91 if content
.get("projects"):
92 content
["projects"] += projects
94 content
["projects"] = projects
97 def format_on_edit(final_content
, edit_content
):
98 BaseTopic
.format_on_edit(final_content
, edit_content
)
99 if edit_content
.get("password"):
101 final_content
["_admin"]["salt"] = salt
102 final_content
["password"] = sha256(edit_content
["password"].encode('utf-8') +
103 salt
.encode('utf-8')).hexdigest()
106 def edit(self
, session
, _id
, indata
=None, kwargs
=None, content
=None):
107 if not session
["admin"]:
108 raise EngineException("needed admin privileges", http_code
=HTTPStatus
.UNAUTHORIZED
)
109 # Names that look like UUIDs are not allowed
110 name
= (indata
if indata
else kwargs
).get("username")
111 if is_valid_uuid(name
):
112 raise EngineException("Usernames that look like UUIDs are not allowed",
113 http_code
=HTTPStatus
.UNPROCESSABLE_ENTITY
)
114 return BaseTopic
.edit(self
, session
, _id
, indata
=indata
, kwargs
=kwargs
, content
=content
)
116 def new(self
, rollback
, session
, indata
=None, kwargs
=None, headers
=None):
117 if not session
["admin"]:
118 raise EngineException("needed admin privileges", http_code
=HTTPStatus
.UNAUTHORIZED
)
119 # Names that look like UUIDs are not allowed
120 name
= indata
["username"] if indata
else kwargs
["username"]
121 if is_valid_uuid(name
):
122 raise EngineException("Usernames that look like UUIDs are not allowed",
123 http_code
=HTTPStatus
.UNPROCESSABLE_ENTITY
)
124 return BaseTopic
.new(self
, rollback
, session
, indata
=indata
, kwargs
=kwargs
, headers
=headers
)
127 class ProjectTopic(BaseTopic
):
129 topic_msg
= "projects"
130 schema_new
= project_new_schema
131 schema_edit
= project_edit_schema
134 def __init__(self
, db
, fs
, msg
):
135 BaseTopic
.__init
__(self
, db
, fs
, msg
)
138 def _get_project_filter(session
):
140 Generates a filter dictionary for querying database users.
141 Current policy is admin can show all, non admin, only its own user.
142 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
145 if session
["admin"]: # allows all
148 return {"_id.cont": session
["project_id"]}
150 def check_conflict_on_new(self
, session
, indata
):
151 if not indata
.get("name"):
152 raise EngineException("missing 'name'")
153 # check name not exists
154 if self
.db
.get_one(self
.topic
, {"name": indata
.get("name")}, fail_on_empty
=False, fail_on_more
=False):
155 raise EngineException("name '{}' exists".format(indata
["name"]), HTTPStatus
.CONFLICT
)
158 def format_on_new(content
, project_id
=None, make_public
=False):
159 BaseTopic
.format_on_new(content
, None)
160 # Removed so that the UUID is kept, to allow Project Name modification
161 # content["_id"] = content["name"]
163 def check_conflict_on_del(self
, session
, _id
, db_content
):
165 Check if deletion can be done because of dependencies if it is not force. To override
166 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
167 :param _id: internal _id
168 :param db_content: The database content of this item _id
169 :return: None if ok or raises EngineException with the conflict
171 if _id
in session
["project_id"]:
172 raise EngineException("You cannot delete your own project", http_code
=HTTPStatus
.CONFLICT
)
175 _filter
= {"projects": _id
}
176 if self
.db
.get_list("users", _filter
):
177 raise EngineException("There is some USER that contains this project", http_code
=HTTPStatus
.CONFLICT
)
179 def edit(self
, session
, _id
, indata
=None, kwargs
=None, content
=None):
180 if not session
["admin"]:
181 raise EngineException("needed admin privileges", http_code
=HTTPStatus
.UNAUTHORIZED
)
182 # Names that look like UUIDs are not allowed
183 name
= (indata
if indata
else kwargs
).get("name")
184 if is_valid_uuid(name
):
185 raise EngineException("Project names that look like UUIDs are not allowed",
186 http_code
=HTTPStatus
.UNPROCESSABLE_ENTITY
)
187 return BaseTopic
.edit(self
, session
, _id
, indata
=indata
, kwargs
=kwargs
, content
=content
)
189 def new(self
, rollback
, session
, indata
=None, kwargs
=None, headers
=None):
190 if not session
["admin"]:
191 raise EngineException("needed admin privileges", http_code
=HTTPStatus
.UNAUTHORIZED
)
192 # Names that look like UUIDs are not allowed
193 name
= indata
["name"] if indata
else kwargs
["name"]
194 if is_valid_uuid(name
):
195 raise EngineException("Project names that look like UUIDs are not allowed",
196 http_code
=HTTPStatus
.UNPROCESSABLE_ENTITY
)
197 return BaseTopic
.new(self
, rollback
, session
, indata
=indata
, kwargs
=kwargs
, headers
=headers
)
200 class CommonVimWimSdn(BaseTopic
):
201 """Common class for VIM, WIM SDN just to unify methods that are equal to all of them"""
202 config_to_encrypt
= () # what keys at config must be encrypted because contains passwords
203 password_to_encrypt
= "" # key that contains a password
206 def _create_operation(op_type
, params
=None):
208 Creates a dictionary with the information to an operation, similar to ns-lcm-op
209 :param op_type: can be create, edit, delete
210 :param params: operation input parameters
211 :return: new dictionary with
215 "lcmOperationType": op_type
,
216 "operationState": "PROCESSING",
218 "statusEnteredTime": now
,
219 "detailed-status": "",
220 "operationParams": params
,
223 def check_conflict_on_new(self
, session
, indata
):
225 Check that the data to be inserted is valid. It is checked that name is unique
226 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
227 :param indata: data to be inserted
228 :return: None or raises EngineException
230 self
.check_unique_name(session
, indata
["name"], _id
=None)
232 def check_conflict_on_edit(self
, session
, final_content
, edit_content
, _id
):
234 Check that the data to be edited/uploaded is valid. It is checked that name is unique
235 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
236 :param final_content: data once modified. This method may change it.
237 :param edit_content: incremental data that contains the modifications to apply
238 :param _id: internal _id
239 :return: None or raises EngineException
241 if not session
["force"] and edit_content
.get("name"):
242 self
.check_unique_name(session
, edit_content
["name"], _id
=_id
)
244 def format_on_edit(self
, final_content
, edit_content
):
246 Modifies final_content inserting admin information upon edition
247 :param final_content: final content to be stored at database
248 :param edit_content: user requested update content
249 :return: operation id
253 schema_version
= final_content
.get("schema_version")
255 if edit_content
.get(self
.password_to_encrypt
):
256 final_content
[self
.password_to_encrypt
] = self
.db
.encrypt(edit_content
[self
.password_to_encrypt
],
257 schema_version
=schema_version
,
258 salt
=final_content
["_id"])
259 if edit_content
.get("config") and self
.config_to_encrypt
:
260 for p
in self
.config_to_encrypt
:
261 if edit_content
["config"].get(p
):
262 final_content
["config"][p
] = self
.db
.encrypt(edit_content
["config"][p
],
263 schema_version
=schema_version
,
264 salt
=final_content
["_id"])
266 # create edit operation
267 final_content
["_admin"]["operations"].append(self
._create
_operation
("edit"))
268 return "{}:{}".format(final_content
["_id"], len(final_content
["_admin"]["operations"]) - 1)
270 def format_on_new(self
, content
, project_id
=None, make_public
=False):
272 Modifies content descriptor to include _admin and insert create operation
273 :param content: descriptor to be modified
274 :param project_id: if included, it add project read/write permissions. Can be None or a list
275 :param make_public: if included it is generated as public for reading.
276 :return: op_id: operation id on asynchronous operation, None otherwise. In addition content is modified
278 super().format_on_new(content
, project_id
=project_id
, make_public
=make_public
)
279 content
["schema_version"] = schema_version
= "1.1"
282 if content
.get(self
.password_to_encrypt
):
283 content
[self
.password_to_encrypt
] = self
.db
.encrypt(content
[self
.password_to_encrypt
],
284 schema_version
=schema_version
,
286 if content
.get("config") and self
.config_to_encrypt
:
287 for p
in self
.config_to_encrypt
:
288 if content
["config"].get(p
):
289 content
["config"][p
] = self
.db
.encrypt(content
["config"][p
],
290 schema_version
=schema_version
,
293 content
["_admin"]["operationalState"] = "PROCESSING"
296 content
["_admin"]["operations"] = [self
._create
_operation
("create")]
297 content
["_admin"]["current_operation"] = None
299 return "{}:0".format(content
["_id"])
301 def delete(self
, session
, _id
, dry_run
=False):
303 Delete item by its internal _id
304 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
305 :param _id: server internal id
306 :param dry_run: make checking but do not delete
307 :return: operation id if it is ordered to delete. None otherwise
310 filter_q
= self
._get
_project
_filter
(session
)
311 filter_q
["_id"] = _id
312 db_content
= self
.db
.get_one(self
.topic
, filter_q
)
314 self
.check_conflict_on_del(session
, _id
, db_content
)
318 # remove reference from project_read. If not last delete
319 if session
["project_id"]:
320 for project_id
in session
["project_id"]:
321 if project_id
in db_content
["_admin"]["projects_read"]:
322 db_content
["_admin"]["projects_read"].remove(project_id
)
323 if project_id
in db_content
["_admin"]["projects_write"]:
324 db_content
["_admin"]["projects_write"].remove(project_id
)
326 db_content
["_admin"]["projects_read"].clear()
327 db_content
["_admin"]["projects_write"].clear()
329 update_dict
= {"_admin.projects_read": db_content
["_admin"]["projects_read"],
330 "_admin.projects_write": db_content
["_admin"]["projects_write"]
333 # check if there are projects referencing it (apart from ANY that means public)....
334 if db_content
["_admin"]["projects_read"] and (len(db_content
["_admin"]["projects_read"]) > 1 or
335 db_content
["_admin"]["projects_read"][0] != "ANY"):
336 self
.db
.set_one(self
.topic
, filter_q
, update_dict
=update_dict
) # remove references but not delete
341 self
.db
.del_one(self
.topic
, {"_id": _id
})
343 self
._send
_msg
("deleted", {"_id": _id
, "op_id": op_id
})
345 update_dict
["_admin.to_delete"] = True
346 self
.db
.set_one(self
.topic
, {"_id": _id
},
347 update_dict
=update_dict
,
348 push
={"_admin.operations": self
._create
_operation
("delete")}
350 # the number of operations is the operation_id. db_content does not contains the new operation inserted,
351 # so the -1 is not needed
352 op_id
= "{}:{}".format(db_content
["_id"], len(db_content
["_admin"]["operations"]))
353 self
._send
_msg
("delete", {"_id": _id
, "op_id": op_id
})
357 class VimAccountTopic(CommonVimWimSdn
):
358 topic
= "vim_accounts"
359 topic_msg
= "vim_account"
360 schema_new
= vim_account_new_schema
361 schema_edit
= vim_account_edit_schema
363 password_to_encrypt
= "vim_password"
364 config_to_encrypt
= ("admin_password", "nsx_password", "vcenter_password")
367 class WimAccountTopic(CommonVimWimSdn
):
368 topic
= "wim_accounts"
369 topic_msg
= "wim_account"
370 schema_new
= wim_account_new_schema
371 schema_edit
= wim_account_edit_schema
373 password_to_encrypt
= "wim_password"
374 config_to_encrypt
= ()
377 class SdnTopic(CommonVimWimSdn
):
380 schema_new
= sdn_new_schema
381 schema_edit
= sdn_edit_schema
383 password_to_encrypt
= "password"
384 config_to_encrypt
= ()
387 class UserTopicAuth(UserTopic
):
389 # topic_msg = "users"
390 schema_new
= user_new_schema
391 schema_edit
= user_edit_schema
393 def __init__(self
, db
, fs
, msg
, auth
):
394 UserTopic
.__init
__(self
, db
, fs
, msg
)
397 def check_conflict_on_new(self
, session
, indata
):
399 Check that the data to be inserted is valid
401 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
402 :param indata: data to be inserted
403 :return: None or raises EngineException
405 username
= indata
.get("username")
406 if is_valid_uuid(username
):
407 raise EngineException("username '{}' cannot have a uuid format".format(username
),
408 HTTPStatus
.UNPROCESSABLE_ENTITY
)
410 # Check that username is not used, regardless keystone already checks this
411 if self
.auth
.get_user_list(filter_q
={"name": username
}):
412 raise EngineException("username '{}' is already used".format(username
), HTTPStatus
.CONFLICT
)
414 if "projects" in indata
.keys():
415 # convert to new format project_role_mappings
416 if not indata
.get("project_role_mappings"):
417 indata
["project_role_mappings"] = []
418 for project
in indata
["projects"]:
419 indata
["project_role_mappings"].append({"project": project
, "role": "project_user"})
420 # raise EngineException("Format invalid: the keyword 'projects' is not allowed for keystone authentication",
421 # HTTPStatus.BAD_REQUEST)
423 def check_conflict_on_edit(self
, session
, final_content
, edit_content
, _id
):
425 Check that the data to be edited/uploaded is valid
427 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
428 :param final_content: data once modified
429 :param edit_content: incremental data that contains the modifications to apply
430 :param _id: internal _id
431 :return: None or raises EngineException
434 if "username" in edit_content
:
435 username
= edit_content
.get("username")
436 if is_valid_uuid(username
):
437 raise EngineException("username '{}' cannot have an uuid format".format(username
),
438 HTTPStatus
.UNPROCESSABLE_ENTITY
)
440 # Check that username is not used, regardless keystone already checks this
441 if self
.auth
.get_user_list(filter_q
={"name": username
}):
442 raise EngineException("username '{}' is already used".format(username
), HTTPStatus
.CONFLICT
)
444 if final_content
["username"] == "admin":
445 for mapping
in edit_content
.get("remove_project_role_mappings", ()):
446 if mapping
["project"] == "admin" and mapping
.get("role") in (None, "system_admin"):
447 # TODO make this also available for project id and role id
448 raise EngineException("You cannot remove system_admin role from admin user",
449 http_code
=HTTPStatus
.FORBIDDEN
)
451 def check_conflict_on_del(self
, session
, _id
, db_content
):
453 Check if deletion can be done because of dependencies if it is not force. To override
454 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
455 :param _id: internal _id
456 :param db_content: The database content of this item _id
457 :return: None if ok or raises EngineException with the conflict
459 if db_content
["username"] == session
["username"]:
460 raise EngineException("You cannot delete your own login user ", http_code
=HTTPStatus
.CONFLICT
)
463 # def format_on_new(content, project_id=None, make_public=False):
465 # Modifies content descriptor to include _id.
467 # NOTE: No password salt required because the authentication backend
468 # should handle these security concerns.
470 # :param content: descriptor to be modified
471 # :param make_public: if included it is generated as public for reading.
472 # :return: None, but content is modified
474 # BaseTopic.format_on_new(content, make_public=False)
475 # content["_id"] = content["username"]
476 # content["password"] = content["password"]
479 # def format_on_edit(final_content, edit_content):
481 # Modifies final_content descriptor to include the modified date.
483 # NOTE: No password salt required because the authentication backend
484 # should handle these security concerns.
486 # :param final_content: final descriptor generated
487 # :param edit_content: alterations to be include
488 # :return: None, but final_content is modified
490 # BaseTopic.format_on_edit(final_content, edit_content)
491 # if "password" in edit_content:
492 # final_content["password"] = edit_content["password"]
494 # final_content["project_role_mappings"] = edit_content["project_role_mappings"]
497 def format_on_show(content
):
499 Modifies the content of the role information to separate the role
500 metadata from the role definition.
502 project_role_mappings
= []
504 for project
in content
["projects"]:
505 for role
in project
["roles"]:
506 project_role_mappings
.append({"project": project
["_id"],
507 "project_name": project
["name"],
509 "role_name": role
["name"]})
511 del content
["projects"]
512 content
["project_role_mappings"] = project_role_mappings
516 def new(self
, rollback
, session
, indata
=None, kwargs
=None, headers
=None):
518 Creates a new entry into the authentication backend.
520 NOTE: Overrides BaseTopic functionality because it doesn't require access to database.
522 :param rollback: list to append created items at database in case a rollback may to be done
523 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
524 :param indata: data to be inserted
525 :param kwargs: used to override the indata descriptor
526 :param headers: http request headers
527 :return: _id: identity of the inserted data.
530 content
= BaseTopic
._remove
_envelop
(indata
)
532 # Override descriptor with query string kwargs
533 BaseTopic
._update
_input
_with
_kwargs
(content
, kwargs
)
534 content
= self
._validate
_input
_new
(content
, session
["force"])
535 self
.check_conflict_on_new(session
, content
)
536 # self.format_on_new(content, session["project_id"], make_public=session["public"])
537 _id
= self
.auth
.create_user(content
["username"], content
["password"])["_id"]
539 if "project_role_mappings" in content
.keys():
540 for mapping
in content
["project_role_mappings"]:
541 self
.auth
.assign_role_to_user(_id
, mapping
["project"], mapping
["role"])
543 rollback
.append({"topic": self
.topic
, "_id": _id
})
544 # del content["password"]
545 # self._send_msg("create", content)
547 except ValidationError
as e
:
548 raise EngineException(e
, HTTPStatus
.UNPROCESSABLE_ENTITY
)
550 def show(self
, session
, _id
):
552 Get complete information on an topic
554 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
555 :param _id: server internal id
556 :return: dictionary, raise exception if not found.
558 # Allow _id to be a name or uuid
559 filter_q
= {self
.id_field(self
.topic
, _id
): _id
}
560 users
= self
.auth
.get_user_list(filter_q
)
563 return self
.format_on_show(users
[0])
565 raise EngineException("Too many users found", HTTPStatus
.CONFLICT
)
567 raise EngineException("User not found", HTTPStatus
.NOT_FOUND
)
569 def edit(self
, session
, _id
, indata
=None, kwargs
=None, content
=None):
571 Updates an user entry.
573 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
575 :param indata: data to be inserted
576 :param kwargs: used to override the indata descriptor
578 :return: _id: identity of the inserted data.
580 indata
= self
._remove
_envelop
(indata
)
582 # Override descriptor with query string kwargs
584 BaseTopic
._update
_input
_with
_kwargs
(indata
, kwargs
)
586 indata
= self
._validate
_input
_edit
(indata
, force
=session
["force"])
589 content
= self
.show(session
, _id
)
590 self
.check_conflict_on_edit(session
, content
, indata
, _id
=_id
)
591 # self.format_on_edit(content, indata)
593 if "password" in indata
or "username" in indata
:
594 self
.auth
.update_user(_id
, new_name
=indata
.get("username"), new_password
=indata
.get("password"))
595 if not indata
.get("remove_project_role_mappings") and not indata
.get("add_project_role_mappings") and \
596 not indata
.get("project_role_mappings"):
598 if indata
.get("project_role_mappings") and \
599 (indata
.get("remove_project_role_mappings") or indata
.get("add_project_role_mappings")):
600 raise EngineException("Option 'project_role_mappings' is incompatible with 'add_project_role_mappings"
601 "' or 'remove_project_role_mappings'", http_code
=HTTPStatus
.BAD_REQUEST
)
603 user
= self
.show(session
, _id
)
604 original_mapping
= user
["project_role_mappings"]
607 mappings_to_remove
= []
610 for to_remove
in indata
.get("remove_project_role_mappings", ()):
611 for mapping
in original_mapping
:
612 if to_remove
["project"] in (mapping
["project"], mapping
["project_name"]):
613 if not to_remove
.get("role") or to_remove
["role"] in (mapping
["role"], mapping
["role_name"]):
614 mappings_to_remove
.append(mapping
)
617 for to_add
in indata
.get("add_project_role_mappings", ()):
618 for mapping
in original_mapping
:
619 if to_add
["project"] in (mapping
["project"], mapping
["project_name"]) and \
620 to_add
["role"] in (mapping
["role"], mapping
["role_name"]):
622 if mapping
in mappings_to_remove
: # do not remove
623 mappings_to_remove
.remove(mapping
)
624 break # do not add, it is already at user
626 mappings_to_add
.append(to_add
)
629 if indata
.get("project_role_mappings"):
630 for to_set
in indata
["project_role_mappings"]:
631 for mapping
in original_mapping
:
632 if to_set
["project"] in (mapping
["project"], mapping
["project_name"]) and \
633 to_set
["role"] in (mapping
["role"], mapping
["role_name"]):
635 if mapping
in mappings_to_remove
: # do not remove
636 mappings_to_remove
.remove(mapping
)
637 break # do not add, it is already at user
639 mappings_to_add
.append(to_set
)
640 for mapping
in original_mapping
:
641 for to_set
in indata
["project_role_mappings"]:
642 if to_set
["project"] in (mapping
["project"], mapping
["project_name"]) and \
643 to_set
["role"] in (mapping
["role"], mapping
["role_name"]):
647 if mapping
not in mappings_to_remove
: # do not remove
648 mappings_to_remove
.append(mapping
)
650 for mapping
in mappings_to_remove
:
651 self
.auth
.remove_role_from_user(
657 for mapping
in mappings_to_add
:
658 self
.auth
.assign_role_to_user(
665 except ValidationError
as e
:
666 raise EngineException(e
, HTTPStatus
.UNPROCESSABLE_ENTITY
)
668 def list(self
, session
, filter_q
=None):
670 Get a list of the topic that matches a filter
671 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
672 :param filter_q: filter of data to be applied
673 :return: The list, it can be empty if no one match the filter.
675 users
= [self
.format_on_show(user
) for user
in self
.auth
.get_user_list(filter_q
)]
679 def delete(self
, session
, _id
, dry_run
=False):
681 Delete item by its internal _id
683 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
684 :param _id: server internal id
685 :param force: indicates if deletion must be forced in case of conflict
686 :param dry_run: make checking but do not delete
687 :return: dictionary with deleted item _id. It raises EngineException on error: not found, conflict, ...
689 # Allow _id to be a name or uuid
690 filter_q
= {self
.id_field(self
.topic
, _id
): _id
}
691 user_list
= self
.auth
.get_user_list(filter_q
)
693 raise EngineException("User '{}' not found".format(_id
), http_code
=HTTPStatus
.NOT_FOUND
)
694 _id
= user_list
[0]["_id"]
695 self
.check_conflict_on_del(session
, _id
, user_list
[0])
697 v
= self
.auth
.delete_user(_id
)
702 class ProjectTopicAuth(ProjectTopic
):
704 # topic_msg = "projects"
705 schema_new
= project_new_schema
706 schema_edit
= project_edit_schema
708 def __init__(self
, db
, fs
, msg
, auth
):
709 ProjectTopic
.__init
__(self
, db
, fs
, msg
)
712 def check_conflict_on_new(self
, session
, indata
):
714 Check that the data to be inserted is valid
716 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
717 :param indata: data to be inserted
718 :return: None or raises EngineException
720 project_name
= indata
.get("name")
721 if is_valid_uuid(project_name
):
722 raise EngineException("project name '{}' cannot have an uuid format".format(project_name
),
723 HTTPStatus
.UNPROCESSABLE_ENTITY
)
725 project_list
= self
.auth
.get_project_list(filter_q
={"name": project_name
})
728 raise EngineException("project '{}' exists".format(project_name
), HTTPStatus
.CONFLICT
)
730 def check_conflict_on_edit(self
, session
, final_content
, edit_content
, _id
):
732 Check that the data to be edited/uploaded is valid
734 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
735 :param final_content: data once modified
736 :param edit_content: incremental data that contains the modifications to apply
737 :param _id: internal _id
738 :return: None or raises EngineException
741 project_name
= edit_content
.get("name")
743 if is_valid_uuid(project_name
):
744 raise EngineException("project name '{}' cannot be an uuid format".format(project_name
),
745 HTTPStatus
.UNPROCESSABLE_ENTITY
)
747 # Check that project name is not used, regardless keystone already checks this
748 if self
.auth
.get_project_list(filter_q
={"name": project_name
}):
749 raise EngineException("project '{}' is already used".format(project_name
), HTTPStatus
.CONFLICT
)
751 def check_conflict_on_del(self
, session
, _id
, db_content
):
753 Check if deletion can be done because of dependencies if it is not force. To override
755 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
756 :param _id: internal _id
757 :param db_content: The database content of this item _id
758 :return: None if ok or raises EngineException with the conflict
760 # projects = self.auth.get_project_list()
761 # current_project = [project for project in projects
762 # if project["name"] in session["project_id"]][0]
763 # TODO check that any user is using this project, raise CONFLICT exception
764 if _id
== session
["project_id"]:
765 raise EngineException("You cannot delete your own project", http_code
=HTTPStatus
.CONFLICT
)
767 def new(self
, rollback
, session
, indata
=None, kwargs
=None, headers
=None):
769 Creates a new entry into the authentication backend.
771 NOTE: Overrides BaseTopic functionality because it doesn't require access to database.
773 :param rollback: list to append created items at database in case a rollback may to be done
774 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
775 :param indata: data to be inserted
776 :param kwargs: used to override the indata descriptor
777 :param headers: http request headers
778 :return: _id: identity of the inserted data.
781 content
= BaseTopic
._remove
_envelop
(indata
)
783 # Override descriptor with query string kwargs
784 BaseTopic
._update
_input
_with
_kwargs
(content
, kwargs
)
785 content
= self
._validate
_input
_new
(content
, session
["force"])
786 self
.check_conflict_on_new(session
, content
)
787 self
.format_on_new(content
, project_id
=session
["project_id"], make_public
=session
["public"])
788 _id
= self
.auth
.create_project(content
["name"])
789 rollback
.append({"topic": self
.topic
, "_id": _id
})
790 # self._send_msg("create", content)
792 except ValidationError
as e
:
793 raise EngineException(e
, HTTPStatus
.UNPROCESSABLE_ENTITY
)
795 def show(self
, session
, _id
):
797 Get complete information on an topic
799 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
800 :param _id: server internal id
801 :return: dictionary, raise exception if not found.
803 # Allow _id to be a name or uuid
804 filter_q
= {self
.id_field(self
.topic
, _id
): _id
}
805 projects
= self
.auth
.get_project_list(filter_q
=filter_q
)
807 if len(projects
) == 1:
809 elif len(projects
) > 1:
810 raise EngineException("Too many projects found", HTTPStatus
.CONFLICT
)
812 raise EngineException("Project not found", HTTPStatus
.NOT_FOUND
)
814 def list(self
, session
, filter_q
=None):
816 Get a list of the topic that matches a filter
818 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
819 :param filter_q: filter of data to be applied
820 :return: The list, it can be empty if no one match the filter.
822 return self
.auth
.get_project_list(filter_q
)
824 def delete(self
, session
, _id
, dry_run
=False):
826 Delete item by its internal _id
828 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
829 :param _id: server internal id
830 :param dry_run: make checking but do not delete
831 :return: dictionary with deleted item _id. It raises EngineException on error: not found, conflict, ...
833 # Allow _id to be a name or uuid
834 filter_q
= {self
.id_field(self
.topic
, _id
): _id
}
835 project_list
= self
.auth
.get_project_list(filter_q
)
837 raise EngineException("Project '{}' not found".format(_id
), http_code
=HTTPStatus
.NOT_FOUND
)
838 _id
= project_list
[0]["_id"]
839 self
.check_conflict_on_del(session
, _id
, project_list
[0])
841 v
= self
.auth
.delete_project(_id
)
845 def edit(self
, session
, _id
, indata
=None, kwargs
=None, content
=None):
847 Updates a project entry.
849 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
851 :param indata: data to be inserted
852 :param kwargs: used to override the indata descriptor
854 :return: _id: identity of the inserted data.
856 indata
= self
._remove
_envelop
(indata
)
858 # Override descriptor with query string kwargs
860 BaseTopic
._update
_input
_with
_kwargs
(indata
, kwargs
)
862 indata
= self
._validate
_input
_edit
(indata
, force
=session
["force"])
865 content
= self
.show(session
, _id
)
866 self
.check_conflict_on_edit(session
, content
, indata
, _id
=_id
)
867 # self.format_on_edit(content, indata)
870 self
.auth
.update_project(content
["_id"], indata
["name"])
871 except ValidationError
as e
:
872 raise EngineException(e
, HTTPStatus
.UNPROCESSABLE_ENTITY
)
875 class RoleTopicAuth(BaseTopic
):
877 topic_msg
= None # "roles"
878 schema_new
= roles_new_schema
879 schema_edit
= roles_edit_schema
882 def __init__(self
, db
, fs
, msg
, auth
, ops
):
883 BaseTopic
.__init
__(self
, db
, fs
, msg
)
885 self
.operations
= ops
886 self
.topic
= "roles_operations" if isinstance(auth
, AuthconnKeystone
) else "roles"
889 def validate_role_definition(operations
, role_definitions
):
891 Validates the role definition against the operations defined in
892 the resources to operations files.
894 :param operations: operations list
895 :param role_definitions: role definition to test
896 :return: None if ok, raises ValidationError exception on error
898 if not role_definitions
.get("permissions"):
900 ignore_fields
= ["admin", "default"]
901 for role_def
in role_definitions
["permissions"].keys():
902 if role_def
in ignore_fields
:
904 if role_def
[-1] == ":":
905 raise ValidationError("Operation cannot end with ':'")
907 role_def_matches
= [op
for op
in operations
if op
.startswith(role_def
)]
909 if len(role_def_matches
) == 0:
910 raise ValidationError("Invalid permission '{}'".format(role_def
))
912 def _validate_input_new(self
, input, force
=False):
914 Validates input user content for a new entry.
916 :param input: user input content for the new topic
917 :param force: may be used for being more tolerant
918 :return: The same input content, or a changed version of it.
921 validate_input(input, self
.schema_new
)
922 self
.validate_role_definition(self
.operations
, input)
926 def _validate_input_edit(self
, input, force
=False):
928 Validates input user content for updating an entry.
930 :param input: user input content for the new topic
931 :param force: may be used for being more tolerant
932 :return: The same input content, or a changed version of it.
935 validate_input(input, self
.schema_edit
)
936 self
.validate_role_definition(self
.operations
, input)
940 def check_conflict_on_new(self
, session
, indata
):
942 Check that the data to be inserted is valid
944 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
945 :param indata: data to be inserted
946 :return: None or raises EngineException
948 # check name not exists
949 if self
.db
.get_one(self
.topic
, {"name": indata
.get("name")}, fail_on_empty
=False, fail_on_more
=False):
950 raise EngineException("role name '{}' exists".format(indata
["name"]), HTTPStatus
.CONFLICT
)
952 def check_conflict_on_edit(self
, session
, final_content
, edit_content
, _id
):
954 Check that the data to be edited/uploaded is valid
956 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
957 :param final_content: data once modified
958 :param edit_content: incremental data that contains the modifications to apply
959 :param _id: internal _id
960 :return: None or raises EngineException
962 if "default" not in final_content
["permissions"]:
963 final_content
["permissions"]["default"] = False
964 if "admin" not in final_content
["permissions"]:
965 final_content
["permissions"]["admin"] = False
967 # check name not exists
968 if "name" in edit_content
:
969 role_name
= edit_content
["name"]
970 if self
.db
.get_one(self
.topic
, {"name": role_name
, "_id.ne": _id
}, fail_on_empty
=False, fail_on_more
=False):
971 raise EngineException("role name '{}' exists".format(role_name
), HTTPStatus
.CONFLICT
)
973 def check_conflict_on_del(self
, session
, _id
, db_content
):
975 Check if deletion can be done because of dependencies if it is not force. To override
977 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
978 :param _id: internal _id
979 :param db_content: The database content of this item _id
980 :return: None if ok or raises EngineException with the conflict
982 roles
= self
.auth
.get_role_list()
983 system_admin_roles
= [role
for role
in roles
if role
["name"] == "system_admin"]
985 if system_admin_roles
and _id
== system_admin_roles
[0]["_id"]:
986 raise EngineException("You cannot delete system_admin role", http_code
=HTTPStatus
.FORBIDDEN
)
989 def format_on_new(content
, project_id
=None, make_public
=False):
991 Modifies content descriptor to include _admin
993 :param content: descriptor to be modified
994 :param project_id: if included, it add project read/write permissions
995 :param make_public: if included it is generated as public for reading.
996 :return: None, but content is modified
999 if "_admin" not in content
:
1000 content
["_admin"] = {}
1001 if not content
["_admin"].get("created"):
1002 content
["_admin"]["created"] = now
1003 content
["_admin"]["modified"] = now
1005 if "permissions" not in content
:
1006 content
["permissions"] = {}
1008 if "default" not in content
["permissions"]:
1009 content
["permissions"]["default"] = False
1010 if "admin" not in content
["permissions"]:
1011 content
["permissions"]["admin"] = False
1014 def format_on_edit(final_content
, edit_content
):
1016 Modifies final_content descriptor to include the modified date.
1018 :param final_content: final descriptor generated
1019 :param edit_content: alterations to be include
1020 :return: None, but final_content is modified
1022 final_content
["_admin"]["modified"] = time()
1024 if "permissions" not in final_content
:
1025 final_content
["permissions"] = {}
1027 if "default" not in final_content
["permissions"]:
1028 final_content
["permissions"]["default"] = False
1029 if "admin" not in final_content
["permissions"]:
1030 final_content
["permissions"]["admin"] = False
1034 # def format_on_show(content):
1036 # Modifies the content of the role information to separate the role
1037 # metadata from the role definition. Eases the reading process of the
1040 # :param definition: role definition to be processed
1042 # content["_id"] = str(content["_id"])
1044 # def show(self, session, _id):
1046 # Get complete information on an topic
1048 # :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
1049 # :param _id: server internal id
1050 # :return: dictionary, raise exception if not found.
1052 # filter_db = {"_id": _id}
1053 # filter_db = { BaseTopic.id_field(self.topic, _id): _id } # To allow role addressing by name
1055 # role = self.db.get_one(self.topic, filter_db)
1056 # new_role = dict(role)
1057 # self.format_on_show(new_role)
1061 # def list(self, session, filter_q=None):
1063 # Get a list of the topic that matches a filter
1065 # :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
1066 # :param filter_q: filter of data to be applied
1067 # :return: The list, it can be empty if no one match the filter.
1072 # if ":" in filter_q:
1073 # filter_q["root"] = filter_q[":"]
1075 # for key in filter_q.keys():
1078 # filter_q[key] = filter_q[key] in ["True", "true"]
1080 # roles = self.db.get_list(self.topic, filter_q)
1083 # for role in roles:
1084 # new_role = dict(role)
1085 # self.format_on_show(new_role)
1086 # new_roles.append(new_role)
1090 def new(self
, rollback
, session
, indata
=None, kwargs
=None, headers
=None):
1092 Creates a new entry into database.
1094 :param rollback: list to append created items at database in case a rollback may to be done
1095 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
1096 :param indata: data to be inserted
1097 :param kwargs: used to override the indata descriptor
1098 :param headers: http request headers
1099 :return: _id: identity of the inserted data.
1102 content
= self
._remove
_envelop
(indata
)
1104 # Override descriptor with query string kwargs
1105 self
._update
_input
_with
_kwargs
(content
, kwargs
)
1106 content
= self
._validate
_input
_new
(content
, session
["force"])
1107 self
.check_conflict_on_new(session
, content
)
1108 self
.format_on_new(content
, project_id
=session
["project_id"], make_public
=session
["public"])
1109 role_name
= content
["name"]
1110 role_id
= self
.auth
.create_role(role_name
)
1111 content
["_id"] = role_id
1112 _id
= self
.db
.create(self
.topic
, content
)
1113 rollback
.append({"topic": self
.topic
, "_id": _id
})
1114 # self._send_msg("create", content)
1116 except ValidationError
as e
:
1117 raise EngineException(e
, HTTPStatus
.UNPROCESSABLE_ENTITY
)
1119 def delete(self
, session
, _id
, dry_run
=False):
1121 Delete item by its internal _id
1123 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
1124 :param _id: server internal id
1125 :param dry_run: make checking but do not delete
1126 :return: dictionary with deleted item _id. It raises EngineException on error: not found, conflict, ...
1128 self
.check_conflict_on_del(session
, _id
, None)
1129 # filter_q = {"_id": _id}
1130 filter_q
= {BaseTopic
.id_field(self
.topic
, _id
): _id
} # To allow role addressing by name
1132 self
.auth
.delete_role(_id
)
1133 v
= self
.db
.del_one(self
.topic
, filter_q
)
1137 def edit(self
, session
, _id
, indata
=None, kwargs
=None, content
=None):
1139 Updates a role entry.
1141 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
1143 :param indata: data to be inserted
1144 :param kwargs: used to override the indata descriptor
1146 :return: _id: identity of the inserted data.
1148 _id
= super().edit(session
, _id
, indata
, kwargs
, content
)
1149 if indata
.get("name"):
1150 self
.auth
.update_role(_id
, name
=indata
.get("name"))