| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | |
| tierno | d125caf | 2018-11-22 16:05:54 +0000 | [diff] [blame] | 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 |
| 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, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 12 | # implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 16 | # import logging |
| 17 | from uuid import uuid4 |
| 18 | from hashlib import sha256 |
| 19 | from http import HTTPStatus |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 20 | from time import time |
| tierno | 23acf40 | 2019-08-28 13:36:34 +0000 | [diff] [blame] | 21 | from osm_nbi.validation import user_new_schema, user_edit_schema, project_new_schema, project_edit_schema, \ |
| 22 | vim_account_new_schema, vim_account_edit_schema, sdn_new_schema, sdn_edit_schema, \ |
| 23 | wim_account_new_schema, wim_account_edit_schema, roles_new_schema, roles_edit_schema, \ |
| delacruzramo | fe598fe | 2019-10-23 18:25:11 +0200 | [diff] [blame] | 24 | k8scluster_new_schema, k8scluster_edit_schema, k8srepo_new_schema, k8srepo_edit_schema, \ |
| David Garcia | ecb4132 | 2021-03-31 19:10:46 +0200 | [diff] [blame] | 25 | vca_new_schema, vca_edit_schema, \ |
| Felipe Vicens | b66b041 | 2020-05-06 10:11:00 +0200 | [diff] [blame] | 26 | osmrepo_new_schema, osmrepo_edit_schema, \ |
| 27 | validate_input, ValidationError, is_valid_uuid # To check that User/Project Names don't look like UUIDs |
| tierno | 23acf40 | 2019-08-28 13:36:34 +0000 | [diff] [blame] | 28 | from osm_nbi.base_topic import BaseTopic, EngineException |
| 29 | from osm_nbi.authconn import AuthconnNotFoundException, AuthconnConflictException |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 30 | from osm_common.dbbase import deep_update_rfc7396 |
| agarwalat | 5347198 | 2020-10-08 13:06:14 +0000 | [diff] [blame] | 31 | import copy |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 32 | |
| 33 | __author__ = "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>" |
| 34 | |
| 35 | |
| 36 | class UserTopic(BaseTopic): |
| 37 | topic = "users" |
| 38 | topic_msg = "users" |
| 39 | schema_new = user_new_schema |
| 40 | schema_edit = user_edit_schema |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 41 | multiproject = False |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 42 | |
| delacruzramo | 32bab47 | 2019-09-13 12:24:22 +0200 | [diff] [blame] | 43 | def __init__(self, db, fs, msg, auth): |
| 44 | BaseTopic.__init__(self, db, fs, msg, auth) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 45 | |
| 46 | @staticmethod |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 47 | def _get_project_filter(session): |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 48 | """ |
| 49 | Generates a filter dictionary for querying database users. |
| 50 | Current policy is admin can show all, non admin, only its own user. |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 51 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 52 | :return: |
| 53 | """ |
| 54 | if session["admin"]: # allows all |
| 55 | return {} |
| 56 | else: |
| 57 | return {"username": session["username"]} |
| 58 | |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 59 | def check_conflict_on_new(self, session, indata): |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 60 | # check username not exists |
| 61 | if self.db.get_one(self.topic, {"username": indata.get("username")}, fail_on_empty=False, fail_on_more=False): |
| 62 | raise EngineException("username '{}' exists".format(indata["username"]), HTTPStatus.CONFLICT) |
| 63 | # check projects |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 64 | if not session["force"]: |
| delacruzramo | ceb8baf | 2019-06-21 14:25:38 +0200 | [diff] [blame] | 65 | for p in indata.get("projects") or []: |
| delacruzramo | c061f56 | 2019-04-05 11:00:02 +0200 | [diff] [blame] | 66 | # To allow project addressing by Name as well as ID |
| 67 | if not self.db.get_one("projects", {BaseTopic.id_field("projects", p): p}, fail_on_empty=False, |
| 68 | fail_on_more=False): |
| 69 | raise EngineException("project '{}' does not exist".format(p), HTTPStatus.CONFLICT) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 70 | |
| tierno | b4844ab | 2019-05-23 08:42:12 +0000 | [diff] [blame] | 71 | def check_conflict_on_del(self, session, _id, db_content): |
| 72 | """ |
| 73 | Check if deletion can be done because of dependencies if it is not force. To override |
| 74 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| 75 | :param _id: internal _id |
| 76 | :param db_content: The database content of this item _id |
| 77 | :return: None if ok or raises EngineException with the conflict |
| 78 | """ |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 79 | if _id == session["username"]: |
| 80 | raise EngineException("You cannot delete your own user", http_code=HTTPStatus.CONFLICT) |
| 81 | |
| 82 | @staticmethod |
| 83 | def format_on_new(content, project_id=None, make_public=False): |
| 84 | BaseTopic.format_on_new(content, make_public=False) |
| delacruzramo | c061f56 | 2019-04-05 11:00:02 +0200 | [diff] [blame] | 85 | # Removed so that the UUID is kept, to allow User Name modification |
| 86 | # content["_id"] = content["username"] |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 87 | salt = uuid4().hex |
| 88 | content["_admin"]["salt"] = salt |
| 89 | if content.get("password"): |
| 90 | content["password"] = sha256(content["password"].encode('utf-8') + salt.encode('utf-8')).hexdigest() |
| Eduardo Sousa | 339ed78 | 2019-05-28 14:25:00 +0100 | [diff] [blame] | 91 | if content.get("project_role_mappings"): |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 92 | projects = [mapping["project"] for mapping in content["project_role_mappings"]] |
| Eduardo Sousa | 339ed78 | 2019-05-28 14:25:00 +0100 | [diff] [blame] | 93 | |
| 94 | if content.get("projects"): |
| 95 | content["projects"] += projects |
| 96 | else: |
| 97 | content["projects"] = projects |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 98 | |
| 99 | @staticmethod |
| 100 | def format_on_edit(final_content, edit_content): |
| 101 | BaseTopic.format_on_edit(final_content, edit_content) |
| 102 | if edit_content.get("password"): |
| 103 | salt = uuid4().hex |
| 104 | final_content["_admin"]["salt"] = salt |
| 105 | final_content["password"] = sha256(edit_content["password"].encode('utf-8') + |
| 106 | salt.encode('utf-8')).hexdigest() |
| tierno | bdebce9 | 2019-07-01 15:36:49 +0000 | [diff] [blame] | 107 | return None |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 108 | |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 109 | def edit(self, session, _id, indata=None, kwargs=None, content=None): |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 110 | if not session["admin"]: |
| 111 | raise EngineException("needed admin privileges", http_code=HTTPStatus.UNAUTHORIZED) |
| delacruzramo | c061f56 | 2019-04-05 11:00:02 +0200 | [diff] [blame] | 112 | # Names that look like UUIDs are not allowed |
| 113 | name = (indata if indata else kwargs).get("username") |
| 114 | if is_valid_uuid(name): |
| 115 | raise EngineException("Usernames that look like UUIDs are not allowed", |
| 116 | http_code=HTTPStatus.UNPROCESSABLE_ENTITY) |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 117 | return BaseTopic.edit(self, session, _id, indata=indata, kwargs=kwargs, content=content) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 118 | |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 119 | def new(self, rollback, session, indata=None, kwargs=None, headers=None): |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 120 | if not session["admin"]: |
| 121 | raise EngineException("needed admin privileges", http_code=HTTPStatus.UNAUTHORIZED) |
| delacruzramo | c061f56 | 2019-04-05 11:00:02 +0200 | [diff] [blame] | 122 | # Names that look like UUIDs are not allowed |
| 123 | name = indata["username"] if indata else kwargs["username"] |
| 124 | if is_valid_uuid(name): |
| 125 | raise EngineException("Usernames that look like UUIDs are not allowed", |
| 126 | http_code=HTTPStatus.UNPROCESSABLE_ENTITY) |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 127 | return BaseTopic.new(self, rollback, session, indata=indata, kwargs=kwargs, headers=headers) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 128 | |
| 129 | |
| 130 | class ProjectTopic(BaseTopic): |
| 131 | topic = "projects" |
| 132 | topic_msg = "projects" |
| 133 | schema_new = project_new_schema |
| 134 | schema_edit = project_edit_schema |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 135 | multiproject = False |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 136 | |
| delacruzramo | 32bab47 | 2019-09-13 12:24:22 +0200 | [diff] [blame] | 137 | def __init__(self, db, fs, msg, auth): |
| 138 | BaseTopic.__init__(self, db, fs, msg, auth) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 139 | |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 140 | @staticmethod |
| 141 | def _get_project_filter(session): |
| 142 | """ |
| 143 | Generates a filter dictionary for querying database users. |
| 144 | Current policy is admin can show all, non admin, only its own user. |
| 145 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| 146 | :return: |
| 147 | """ |
| 148 | if session["admin"]: # allows all |
| 149 | return {} |
| 150 | else: |
| 151 | return {"_id.cont": session["project_id"]} |
| 152 | |
| 153 | def check_conflict_on_new(self, session, indata): |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 154 | if not indata.get("name"): |
| 155 | raise EngineException("missing 'name'") |
| 156 | # check name not exists |
| 157 | if self.db.get_one(self.topic, {"name": indata.get("name")}, fail_on_empty=False, fail_on_more=False): |
| 158 | raise EngineException("name '{}' exists".format(indata["name"]), HTTPStatus.CONFLICT) |
| 159 | |
| 160 | @staticmethod |
| 161 | def format_on_new(content, project_id=None, make_public=False): |
| 162 | BaseTopic.format_on_new(content, None) |
| delacruzramo | c061f56 | 2019-04-05 11:00:02 +0200 | [diff] [blame] | 163 | # Removed so that the UUID is kept, to allow Project Name modification |
| 164 | # content["_id"] = content["name"] |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 165 | |
| tierno | b4844ab | 2019-05-23 08:42:12 +0000 | [diff] [blame] | 166 | def check_conflict_on_del(self, session, _id, db_content): |
| 167 | """ |
| 168 | Check if deletion can be done because of dependencies if it is not force. To override |
| 169 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| 170 | :param _id: internal _id |
| 171 | :param db_content: The database content of this item _id |
| 172 | :return: None if ok or raises EngineException with the conflict |
| 173 | """ |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 174 | if _id in session["project_id"]: |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 175 | raise EngineException("You cannot delete your own project", http_code=HTTPStatus.CONFLICT) |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 176 | if session["force"]: |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 177 | return |
| 178 | _filter = {"projects": _id} |
| 179 | if self.db.get_list("users", _filter): |
| 180 | raise EngineException("There is some USER that contains this project", http_code=HTTPStatus.CONFLICT) |
| 181 | |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 182 | def edit(self, session, _id, indata=None, kwargs=None, content=None): |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 183 | if not session["admin"]: |
| 184 | raise EngineException("needed admin privileges", http_code=HTTPStatus.UNAUTHORIZED) |
| delacruzramo | c061f56 | 2019-04-05 11:00:02 +0200 | [diff] [blame] | 185 | # Names that look like UUIDs are not allowed |
| 186 | name = (indata if indata else kwargs).get("name") |
| 187 | if is_valid_uuid(name): |
| 188 | raise EngineException("Project names that look like UUIDs are not allowed", |
| 189 | http_code=HTTPStatus.UNPROCESSABLE_ENTITY) |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 190 | return BaseTopic.edit(self, session, _id, indata=indata, kwargs=kwargs, content=content) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 191 | |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 192 | def new(self, rollback, session, indata=None, kwargs=None, headers=None): |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 193 | if not session["admin"]: |
| 194 | raise EngineException("needed admin privileges", http_code=HTTPStatus.UNAUTHORIZED) |
| delacruzramo | c061f56 | 2019-04-05 11:00:02 +0200 | [diff] [blame] | 195 | # Names that look like UUIDs are not allowed |
| 196 | name = indata["name"] if indata else kwargs["name"] |
| 197 | if is_valid_uuid(name): |
| 198 | raise EngineException("Project names that look like UUIDs are not allowed", |
| 199 | http_code=HTTPStatus.UNPROCESSABLE_ENTITY) |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 200 | return BaseTopic.new(self, rollback, session, indata=indata, kwargs=kwargs, headers=headers) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 201 | |
| 202 | |
| tierno | bdebce9 | 2019-07-01 15:36:49 +0000 | [diff] [blame] | 203 | class CommonVimWimSdn(BaseTopic): |
| 204 | """Common class for VIM, WIM SDN just to unify methods that are equal to all of them""" |
| tierno | 468aa24 | 2019-08-01 16:35:04 +0000 | [diff] [blame] | 205 | config_to_encrypt = {} # what keys at config must be encrypted because contains passwords |
| tierno | bdebce9 | 2019-07-01 15:36:49 +0000 | [diff] [blame] | 206 | password_to_encrypt = "" # key that contains a password |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 207 | |
| tierno | bdebce9 | 2019-07-01 15:36:49 +0000 | [diff] [blame] | 208 | @staticmethod |
| 209 | def _create_operation(op_type, params=None): |
| 210 | """ |
| 211 | Creates a dictionary with the information to an operation, similar to ns-lcm-op |
| 212 | :param op_type: can be create, edit, delete |
| 213 | :param params: operation input parameters |
| 214 | :return: new dictionary with |
| 215 | """ |
| 216 | now = time() |
| 217 | return { |
| 218 | "lcmOperationType": op_type, |
| 219 | "operationState": "PROCESSING", |
| 220 | "startTime": now, |
| 221 | "statusEnteredTime": now, |
| 222 | "detailed-status": "", |
| 223 | "operationParams": params, |
| 224 | } |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 225 | |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 226 | def check_conflict_on_new(self, session, indata): |
| tierno | bdebce9 | 2019-07-01 15:36:49 +0000 | [diff] [blame] | 227 | """ |
| 228 | Check that the data to be inserted is valid. It is checked that name is unique |
| 229 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| 230 | :param indata: data to be inserted |
| 231 | :return: None or raises EngineException |
| 232 | """ |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 233 | self.check_unique_name(session, indata["name"], _id=None) |
| 234 | |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 235 | def check_conflict_on_edit(self, session, final_content, edit_content, _id): |
| tierno | bdebce9 | 2019-07-01 15:36:49 +0000 | [diff] [blame] | 236 | """ |
| 237 | Check that the data to be edited/uploaded is valid. It is checked that name is unique |
| 238 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| 239 | :param final_content: data once modified. This method may change it. |
| 240 | :param edit_content: incremental data that contains the modifications to apply |
| 241 | :param _id: internal _id |
| 242 | :return: None or raises EngineException |
| 243 | """ |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 244 | if not session["force"] and edit_content.get("name"): |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 245 | self.check_unique_name(session, edit_content["name"], _id=_id) |
| 246 | |
| bravof | b995ea2 | 2021-02-10 10:57:52 -0300 | [diff] [blame] | 247 | return final_content |
| 248 | |
| tierno | bdebce9 | 2019-07-01 15:36:49 +0000 | [diff] [blame] | 249 | def format_on_edit(self, final_content, edit_content): |
| 250 | """ |
| 251 | Modifies final_content inserting admin information upon edition |
| 252 | :param final_content: final content to be stored at database |
| 253 | :param edit_content: user requested update content |
| 254 | :return: operation id |
| 255 | """ |
| delacruzramo | fe598fe | 2019-10-23 18:25:11 +0200 | [diff] [blame] | 256 | super().format_on_edit(final_content, edit_content) |
| tierno | bdebce9 | 2019-07-01 15:36:49 +0000 | [diff] [blame] | 257 | |
| tierno | 92c1c7d | 2018-11-12 15:22:37 +0100 | [diff] [blame] | 258 | # encrypt passwords |
| 259 | schema_version = final_content.get("schema_version") |
| 260 | if schema_version: |
| tierno | bdebce9 | 2019-07-01 15:36:49 +0000 | [diff] [blame] | 261 | if edit_content.get(self.password_to_encrypt): |
| 262 | final_content[self.password_to_encrypt] = self.db.encrypt(edit_content[self.password_to_encrypt], |
| 263 | schema_version=schema_version, |
| 264 | salt=final_content["_id"]) |
| tierno | 468aa24 | 2019-08-01 16:35:04 +0000 | [diff] [blame] | 265 | config_to_encrypt_keys = self.config_to_encrypt.get(schema_version) or self.config_to_encrypt.get("default") |
| 266 | if edit_content.get("config") and config_to_encrypt_keys: |
| 267 | |
| 268 | for p in config_to_encrypt_keys: |
| tierno | 92c1c7d | 2018-11-12 15:22:37 +0100 | [diff] [blame] | 269 | if edit_content["config"].get(p): |
| 270 | final_content["config"][p] = self.db.encrypt(edit_content["config"][p], |
| tierno | bdebce9 | 2019-07-01 15:36:49 +0000 | [diff] [blame] | 271 | schema_version=schema_version, |
| 272 | salt=final_content["_id"]) |
| 273 | |
| 274 | # create edit operation |
| 275 | final_content["_admin"]["operations"].append(self._create_operation("edit")) |
| 276 | return "{}:{}".format(final_content["_id"], len(final_content["_admin"]["operations"]) - 1) |
| tierno | 92c1c7d | 2018-11-12 15:22:37 +0100 | [diff] [blame] | 277 | |
| 278 | def format_on_new(self, content, project_id=None, make_public=False): |
| tierno | bdebce9 | 2019-07-01 15:36:49 +0000 | [diff] [blame] | 279 | """ |
| 280 | Modifies content descriptor to include _admin and insert create operation |
| 281 | :param content: descriptor to be modified |
| 282 | :param project_id: if included, it add project read/write permissions. Can be None or a list |
| 283 | :param make_public: if included it is generated as public for reading. |
| 284 | :return: op_id: operation id on asynchronous operation, None otherwise. In addition content is modified |
| 285 | """ |
| 286 | super().format_on_new(content, project_id=project_id, make_public=make_public) |
| tierno | 468aa24 | 2019-08-01 16:35:04 +0000 | [diff] [blame] | 287 | content["schema_version"] = schema_version = "1.11" |
| tierno | 92c1c7d | 2018-11-12 15:22:37 +0100 | [diff] [blame] | 288 | |
| 289 | # encrypt passwords |
| tierno | bdebce9 | 2019-07-01 15:36:49 +0000 | [diff] [blame] | 290 | if content.get(self.password_to_encrypt): |
| 291 | content[self.password_to_encrypt] = self.db.encrypt(content[self.password_to_encrypt], |
| 292 | schema_version=schema_version, |
| 293 | salt=content["_id"]) |
| tierno | 468aa24 | 2019-08-01 16:35:04 +0000 | [diff] [blame] | 294 | config_to_encrypt_keys = self.config_to_encrypt.get(schema_version) or self.config_to_encrypt.get("default") |
| 295 | if content.get("config") and config_to_encrypt_keys: |
| 296 | for p in config_to_encrypt_keys: |
| tierno | 92c1c7d | 2018-11-12 15:22:37 +0100 | [diff] [blame] | 297 | if content["config"].get(p): |
| tierno | bdebce9 | 2019-07-01 15:36:49 +0000 | [diff] [blame] | 298 | content["config"][p] = self.db.encrypt(content["config"][p], |
| 299 | schema_version=schema_version, |
| tierno | 92c1c7d | 2018-11-12 15:22:37 +0100 | [diff] [blame] | 300 | salt=content["_id"]) |
| 301 | |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 302 | content["_admin"]["operationalState"] = "PROCESSING" |
| 303 | |
| tierno | bdebce9 | 2019-07-01 15:36:49 +0000 | [diff] [blame] | 304 | # create operation |
| 305 | content["_admin"]["operations"] = [self._create_operation("create")] |
| 306 | content["_admin"]["current_operation"] = None |
| 307 | |
| 308 | return "{}:0".format(content["_id"]) |
| 309 | |
| tierno | bee3bad | 2019-12-05 12:26:01 +0000 | [diff] [blame] | 310 | def delete(self, session, _id, dry_run=False, not_send_msg=None): |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 311 | """ |
| 312 | Delete item by its internal _id |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 313 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 314 | :param _id: server internal id |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 315 | :param dry_run: make checking but do not delete |
| tierno | bee3bad | 2019-12-05 12:26:01 +0000 | [diff] [blame] | 316 | :param not_send_msg: To not send message (False) or store content (list) instead |
| tierno | bdebce9 | 2019-07-01 15:36:49 +0000 | [diff] [blame] | 317 | :return: operation id if it is ordered to delete. None otherwise |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 318 | """ |
| tierno | bdebce9 | 2019-07-01 15:36:49 +0000 | [diff] [blame] | 319 | |
| 320 | filter_q = self._get_project_filter(session) |
| 321 | filter_q["_id"] = _id |
| 322 | db_content = self.db.get_one(self.topic, filter_q) |
| 323 | |
| 324 | self.check_conflict_on_del(session, _id, db_content) |
| 325 | if dry_run: |
| 326 | return None |
| 327 | |
| tierno | f5f2e3f | 2020-03-23 14:42:10 +0000 | [diff] [blame] | 328 | # remove reference from project_read if there are more projects referencing it. If it last one, |
| 329 | # do not remove reference, but order via kafka to delete it |
| 330 | if session["project_id"] and session["project_id"]: |
| 331 | other_projects_referencing = next((p for p in db_content["_admin"]["projects_read"] |
| tierno | 20e74d2 | 2020-06-22 12:17:22 +0000 | [diff] [blame] | 332 | if p not in session["project_id"] and p != "ANY"), None) |
| tierno | bdebce9 | 2019-07-01 15:36:49 +0000 | [diff] [blame] | 333 | |
| tierno | f5f2e3f | 2020-03-23 14:42:10 +0000 | [diff] [blame] | 334 | # check if there are projects referencing it (apart from ANY, that means, public).... |
| 335 | if other_projects_referencing: |
| 336 | # remove references but not delete |
| tierno | 20e74d2 | 2020-06-22 12:17:22 +0000 | [diff] [blame] | 337 | update_dict_pull = {"_admin.projects_read": session["project_id"], |
| 338 | "_admin.projects_write": session["project_id"]} |
| 339 | self.db.set_one(self.topic, filter_q, update_dict=None, pull_list=update_dict_pull) |
| tierno | f5f2e3f | 2020-03-23 14:42:10 +0000 | [diff] [blame] | 340 | return None |
| 341 | else: |
| 342 | can_write = next((p for p in db_content["_admin"]["projects_write"] if p == "ANY" or |
| 343 | p in session["project_id"]), None) |
| 344 | if not can_write: |
| 345 | raise EngineException("You have not write permission to delete it", |
| 346 | http_code=HTTPStatus.UNAUTHORIZED) |
| tierno | bdebce9 | 2019-07-01 15:36:49 +0000 | [diff] [blame] | 347 | |
| 348 | # It must be deleted |
| 349 | if session["force"]: |
| 350 | self.db.del_one(self.topic, {"_id": _id}) |
| 351 | op_id = None |
| tierno | bee3bad | 2019-12-05 12:26:01 +0000 | [diff] [blame] | 352 | self._send_msg("deleted", {"_id": _id, "op_id": op_id}, not_send_msg=not_send_msg) |
| tierno | bdebce9 | 2019-07-01 15:36:49 +0000 | [diff] [blame] | 353 | else: |
| tierno | f5f2e3f | 2020-03-23 14:42:10 +0000 | [diff] [blame] | 354 | update_dict = {"_admin.to_delete": True} |
| tierno | bdebce9 | 2019-07-01 15:36:49 +0000 | [diff] [blame] | 355 | self.db.set_one(self.topic, {"_id": _id}, |
| 356 | update_dict=update_dict, |
| 357 | push={"_admin.operations": self._create_operation("delete")} |
| 358 | ) |
| 359 | # the number of operations is the operation_id. db_content does not contains the new operation inserted, |
| 360 | # so the -1 is not needed |
| 361 | op_id = "{}:{}".format(db_content["_id"], len(db_content["_admin"]["operations"])) |
| tierno | bee3bad | 2019-12-05 12:26:01 +0000 | [diff] [blame] | 362 | self._send_msg("delete", {"_id": _id, "op_id": op_id}, not_send_msg=not_send_msg) |
| tierno | bdebce9 | 2019-07-01 15:36:49 +0000 | [diff] [blame] | 363 | return op_id |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 364 | |
| 365 | |
| tierno | bdebce9 | 2019-07-01 15:36:49 +0000 | [diff] [blame] | 366 | class VimAccountTopic(CommonVimWimSdn): |
| 367 | topic = "vim_accounts" |
| 368 | topic_msg = "vim_account" |
| 369 | schema_new = vim_account_new_schema |
| 370 | schema_edit = vim_account_edit_schema |
| 371 | multiproject = True |
| 372 | password_to_encrypt = "vim_password" |
| tierno | 468aa24 | 2019-08-01 16:35:04 +0000 | [diff] [blame] | 373 | config_to_encrypt = {"1.1": ("admin_password", "nsx_password", "vcenter_password"), |
| 374 | "default": ("admin_password", "nsx_password", "vcenter_password", "vrops_password")} |
| tierno | bdebce9 | 2019-07-01 15:36:49 +0000 | [diff] [blame] | 375 | |
| delacruzramo | 35c998b | 2019-11-21 11:09:16 +0100 | [diff] [blame] | 376 | def check_conflict_on_del(self, session, _id, db_content): |
| 377 | """ |
| 378 | Check if deletion can be done because of dependencies if it is not force. To override |
| 379 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| 380 | :param _id: internal _id |
| 381 | :param db_content: The database content of this item _id |
| 382 | :return: None if ok or raises EngineException with the conflict |
| 383 | """ |
| 384 | if session["force"]: |
| 385 | return |
| 386 | # check if used by VNF |
| 387 | if self.db.get_list("vnfrs", {"vim-account-id": _id}): |
| 388 | raise EngineException("There is at least one VNF using this VIM account", http_code=HTTPStatus.CONFLICT) |
| 389 | super().check_conflict_on_del(session, _id, db_content) |
| 390 | |
| tierno | bdebce9 | 2019-07-01 15:36:49 +0000 | [diff] [blame] | 391 | |
| 392 | class WimAccountTopic(CommonVimWimSdn): |
| tierno | 55ba2e6 | 2018-12-11 17:22:22 +0000 | [diff] [blame] | 393 | topic = "wim_accounts" |
| 394 | topic_msg = "wim_account" |
| 395 | schema_new = wim_account_new_schema |
| 396 | schema_edit = wim_account_edit_schema |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 397 | multiproject = True |
| tierno | bdebce9 | 2019-07-01 15:36:49 +0000 | [diff] [blame] | 398 | password_to_encrypt = "wim_password" |
| tierno | 468aa24 | 2019-08-01 16:35:04 +0000 | [diff] [blame] | 399 | config_to_encrypt = {} |
| tierno | 55ba2e6 | 2018-12-11 17:22:22 +0000 | [diff] [blame] | 400 | |
| 401 | |
| tierno | bdebce9 | 2019-07-01 15:36:49 +0000 | [diff] [blame] | 402 | class SdnTopic(CommonVimWimSdn): |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 403 | topic = "sdns" |
| 404 | topic_msg = "sdn" |
| tierno | 6b02b05 | 2020-06-02 10:07:41 +0000 | [diff] [blame] | 405 | quota_name = "sdn_controllers" |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 406 | schema_new = sdn_new_schema |
| 407 | schema_edit = sdn_edit_schema |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 408 | multiproject = True |
| tierno | bdebce9 | 2019-07-01 15:36:49 +0000 | [diff] [blame] | 409 | password_to_encrypt = "password" |
| tierno | 468aa24 | 2019-08-01 16:35:04 +0000 | [diff] [blame] | 410 | config_to_encrypt = {} |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 411 | |
| tierno | 7adaeb0 | 2019-12-17 16:46:12 +0000 | [diff] [blame] | 412 | def _obtain_url(self, input, create): |
| 413 | if input.get("ip") or input.get("port"): |
| 414 | if not input.get("ip") or not input.get("port") or input.get('url'): |
| 415 | raise ValidationError("You must provide both 'ip' and 'port' (deprecated); or just 'url' (prefered)") |
| 416 | input['url'] = "http://{}:{}/".format(input["ip"], input["port"]) |
| 417 | del input["ip"] |
| 418 | del input["port"] |
| 419 | elif create and not input.get('url'): |
| 420 | raise ValidationError("You must provide 'url'") |
| 421 | return input |
| 422 | |
| 423 | def _validate_input_new(self, input, force=False): |
| 424 | input = super()._validate_input_new(input, force) |
| 425 | return self._obtain_url(input, True) |
| 426 | |
| Frank Bryden | deba68e | 2020-07-27 13:55:11 +0000 | [diff] [blame] | 427 | def _validate_input_edit(self, input, content, force=False): |
| 428 | input = super()._validate_input_edit(input, content, force) |
| tierno | 7adaeb0 | 2019-12-17 16:46:12 +0000 | [diff] [blame] | 429 | return self._obtain_url(input, False) |
| 430 | |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 431 | |
| delacruzramo | fe598fe | 2019-10-23 18:25:11 +0200 | [diff] [blame] | 432 | class K8sClusterTopic(CommonVimWimSdn): |
| 433 | topic = "k8sclusters" |
| 434 | topic_msg = "k8scluster" |
| 435 | schema_new = k8scluster_new_schema |
| 436 | schema_edit = k8scluster_edit_schema |
| 437 | multiproject = True |
| 438 | password_to_encrypt = None |
| 439 | config_to_encrypt = {} |
| 440 | |
| 441 | def format_on_new(self, content, project_id=None, make_public=False): |
| 442 | oid = super().format_on_new(content, project_id, make_public) |
| 443 | self.db.encrypt_decrypt_fields(content["credentials"], 'encrypt', ['password', 'secret'], |
| 444 | schema_version=content["schema_version"], salt=content["_id"]) |
| delacruzramo | c2d5fc6 | 2020-02-05 11:50:21 +0000 | [diff] [blame] | 445 | # Add Helm/Juju Repo lists |
| 446 | repos = {"helm-chart": [], "juju-bundle": []} |
| 447 | for proj in content["_admin"]["projects_read"]: |
| 448 | if proj != 'ANY': |
| 449 | for repo in self.db.get_list("k8srepos", {"_admin.projects_read": proj}): |
| 450 | if repo["_id"] not in repos[repo["type"]]: |
| 451 | repos[repo["type"]].append(repo["_id"]) |
| 452 | for k in repos: |
| 453 | content["_admin"][k.replace('-', '_')+"_repos"] = repos[k] |
| delacruzramo | fe598fe | 2019-10-23 18:25:11 +0200 | [diff] [blame] | 454 | return oid |
| 455 | |
| 456 | def format_on_edit(self, final_content, edit_content): |
| 457 | if final_content.get("schema_version") and edit_content.get("credentials"): |
| 458 | self.db.encrypt_decrypt_fields(edit_content["credentials"], 'encrypt', ['password', 'secret'], |
| 459 | schema_version=final_content["schema_version"], salt=final_content["_id"]) |
| 460 | deep_update_rfc7396(final_content["credentials"], edit_content["credentials"]) |
| 461 | oid = super().format_on_edit(final_content, edit_content) |
| 462 | return oid |
| 463 | |
| delacruzramo | c2d5fc6 | 2020-02-05 11:50:21 +0000 | [diff] [blame] | 464 | def check_conflict_on_edit(self, session, final_content, edit_content, _id): |
| bravof | b995ea2 | 2021-02-10 10:57:52 -0300 | [diff] [blame] | 465 | final_content = super(CommonVimWimSdn, self).check_conflict_on_edit(session, final_content, edit_content, _id) |
| 466 | final_content = super().check_conflict_on_edit(session, final_content, edit_content, _id) |
| delacruzramo | c2d5fc6 | 2020-02-05 11:50:21 +0000 | [diff] [blame] | 467 | # Update Helm/Juju Repo lists |
| 468 | repos = {"helm-chart": [], "juju-bundle": []} |
| 469 | for proj in session.get("set_project", []): |
| 470 | if proj != 'ANY': |
| 471 | for repo in self.db.get_list("k8srepos", {"_admin.projects_read": proj}): |
| 472 | if repo["_id"] not in repos[repo["type"]]: |
| 473 | repos[repo["type"]].append(repo["_id"]) |
| 474 | for k in repos: |
| 475 | rlist = k.replace('-', '_') + "_repos" |
| 476 | if rlist not in final_content["_admin"]: |
| 477 | final_content["_admin"][rlist] = [] |
| 478 | final_content["_admin"][rlist] += repos[k] |
| bravof | b995ea2 | 2021-02-10 10:57:52 -0300 | [diff] [blame] | 479 | return final_content |
| delacruzramo | c2d5fc6 | 2020-02-05 11:50:21 +0000 | [diff] [blame] | 480 | |
| tierno | e19707b | 2020-04-21 13:08:04 +0000 | [diff] [blame] | 481 | def check_conflict_on_del(self, session, _id, db_content): |
| 482 | """ |
| 483 | Check if deletion can be done because of dependencies if it is not force. To override |
| 484 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| 485 | :param _id: internal _id |
| 486 | :param db_content: The database content of this item _id |
| 487 | :return: None if ok or raises EngineException with the conflict |
| 488 | """ |
| 489 | if session["force"]: |
| 490 | return |
| 491 | # check if used by VNF |
| 492 | filter_q = {"kdur.k8s-cluster.id": _id} |
| 493 | if session["project_id"]: |
| 494 | filter_q["_admin.projects_read.cont"] = session["project_id"] |
| 495 | if self.db.get_list("vnfrs", filter_q): |
| 496 | raise EngineException("There is at least one VNF using this k8scluster", http_code=HTTPStatus.CONFLICT) |
| 497 | super().check_conflict_on_del(session, _id, db_content) |
| 498 | |
| delacruzramo | fe598fe | 2019-10-23 18:25:11 +0200 | [diff] [blame] | 499 | |
| David Garcia | ecb4132 | 2021-03-31 19:10:46 +0200 | [diff] [blame] | 500 | class VcaTopic(CommonVimWimSdn): |
| 501 | topic = "vca" |
| 502 | topic_msg = "vca" |
| 503 | schema_new = vca_new_schema |
| 504 | schema_edit = vca_edit_schema |
| 505 | multiproject = True |
| 506 | password_to_encrypt = None |
| 507 | |
| 508 | def format_on_new(self, content, project_id=None, make_public=False): |
| 509 | oid = super().format_on_new(content, project_id, make_public) |
| 510 | content["schema_version"] = schema_version = "1.11" |
| 511 | for key in ["secret", "cacert"]: |
| 512 | content[key] = self.db.encrypt( |
| 513 | content[key], |
| 514 | schema_version=schema_version, |
| 515 | salt=content["_id"] |
| 516 | ) |
| 517 | return oid |
| 518 | |
| 519 | def format_on_edit(self, final_content, edit_content): |
| 520 | oid = super().format_on_edit(final_content, edit_content) |
| 521 | schema_version = final_content.get("schema_version") |
| 522 | for key in ["secret", "cacert"]: |
| 523 | if key in edit_content: |
| 524 | final_content[key] = self.db.encrypt( |
| 525 | edit_content[key], |
| 526 | schema_version=schema_version, |
| 527 | salt=final_content["_id"] |
| 528 | ) |
| 529 | return oid |
| 530 | |
| 531 | def check_conflict_on_del(self, session, _id, db_content): |
| 532 | """ |
| 533 | Check if deletion can be done because of dependencies if it is not force. To override |
| 534 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| 535 | :param _id: internal _id |
| 536 | :param db_content: The database content of this item _id |
| 537 | :return: None if ok or raises EngineException with the conflict |
| 538 | """ |
| 539 | if session["force"]: |
| 540 | return |
| 541 | # check if used by VNF |
| 542 | filter_q = {"vca": _id} |
| 543 | if session["project_id"]: |
| 544 | filter_q["_admin.projects_read.cont"] = session["project_id"] |
| 545 | if self.db.get_list("vim_accounts", filter_q): |
| 546 | raise EngineException("There is at least one VIM account using this vca", http_code=HTTPStatus.CONFLICT) |
| 547 | super().check_conflict_on_del(session, _id, db_content) |
| 548 | |
| 549 | |
| delacruzramo | fe598fe | 2019-10-23 18:25:11 +0200 | [diff] [blame] | 550 | class K8sRepoTopic(CommonVimWimSdn): |
| 551 | topic = "k8srepos" |
| 552 | topic_msg = "k8srepo" |
| 553 | schema_new = k8srepo_new_schema |
| 554 | schema_edit = k8srepo_edit_schema |
| 555 | multiproject = True |
| 556 | password_to_encrypt = None |
| 557 | config_to_encrypt = {} |
| 558 | |
| delacruzramo | c2d5fc6 | 2020-02-05 11:50:21 +0000 | [diff] [blame] | 559 | def format_on_new(self, content, project_id=None, make_public=False): |
| 560 | oid = super().format_on_new(content, project_id, make_public) |
| 561 | # Update Helm/Juju Repo lists |
| 562 | repo_list = content["type"].replace('-', '_')+"_repos" |
| 563 | for proj in content["_admin"]["projects_read"]: |
| 564 | if proj != 'ANY': |
| 565 | self.db.set_list("k8sclusters", |
| 566 | {"_admin.projects_read": proj, "_admin."+repo_list+".ne": content["_id"]}, {}, |
| 567 | push={"_admin."+repo_list: content["_id"]}) |
| 568 | return oid |
| 569 | |
| 570 | def delete(self, session, _id, dry_run=False, not_send_msg=None): |
| 571 | type = self.db.get_one("k8srepos", {"_id": _id})["type"] |
| 572 | oid = super().delete(session, _id, dry_run, not_send_msg) |
| 573 | if oid: |
| 574 | # Remove from Helm/Juju Repo lists |
| 575 | repo_list = type.replace('-', '_') + "_repos" |
| 576 | self.db.set_list("k8sclusters", {"_admin."+repo_list: _id}, {}, pull={"_admin."+repo_list: _id}) |
| 577 | return oid |
| 578 | |
| delacruzramo | fe598fe | 2019-10-23 18:25:11 +0200 | [diff] [blame] | 579 | |
| Felipe Vicens | b66b041 | 2020-05-06 10:11:00 +0200 | [diff] [blame] | 580 | class OsmRepoTopic(BaseTopic): |
| 581 | topic = "osmrepos" |
| 582 | topic_msg = "osmrepos" |
| 583 | schema_new = osmrepo_new_schema |
| 584 | schema_edit = osmrepo_edit_schema |
| 585 | multiproject = True |
| 586 | # TODO: Implement user/password |
| 587 | |
| 588 | |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 589 | class UserTopicAuth(UserTopic): |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 590 | # topic = "users" |
| agarwalat | 5347198 | 2020-10-08 13:06:14 +0000 | [diff] [blame] | 591 | topic_msg = "users" |
| Eduardo Sousa | a16a4fa | 2019-05-23 01:41:18 +0100 | [diff] [blame] | 592 | schema_new = user_new_schema |
| 593 | schema_edit = user_edit_schema |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 594 | |
| 595 | def __init__(self, db, fs, msg, auth): |
| delacruzramo | 32bab47 | 2019-09-13 12:24:22 +0200 | [diff] [blame] | 596 | UserTopic.__init__(self, db, fs, msg, auth) |
| 597 | # self.auth = auth |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 598 | |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 599 | def check_conflict_on_new(self, session, indata): |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 600 | """ |
| 601 | Check that the data to be inserted is valid |
| 602 | |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 603 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 604 | :param indata: data to be inserted |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 605 | :return: None or raises EngineException |
| 606 | """ |
| 607 | username = indata.get("username") |
| tierno | cf042d3 | 2019-06-13 09:06:40 +0000 | [diff] [blame] | 608 | if is_valid_uuid(username): |
| delacruzramo | ceb8baf | 2019-06-21 14:25:38 +0200 | [diff] [blame] | 609 | raise EngineException("username '{}' cannot have a uuid format".format(username), |
| tierno | cf042d3 | 2019-06-13 09:06:40 +0000 | [diff] [blame] | 610 | HTTPStatus.UNPROCESSABLE_ENTITY) |
| 611 | |
| 612 | # Check that username is not used, regardless keystone already checks this |
| 613 | if self.auth.get_user_list(filter_q={"name": username}): |
| 614 | raise EngineException("username '{}' is already used".format(username), HTTPStatus.CONFLICT) |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 615 | |
| Eduardo Sousa | 339ed78 | 2019-05-28 14:25:00 +0100 | [diff] [blame] | 616 | if "projects" in indata.keys(): |
| tierno | 701018c | 2019-06-25 11:13:14 +0000 | [diff] [blame] | 617 | # convert to new format project_role_mappings |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 618 | role = self.auth.get_role_list({"name": "project_admin"}) |
| 619 | if not role: |
| 620 | role = self.auth.get_role_list() |
| 621 | if not role: |
| 622 | raise AuthconnNotFoundException("Can't find default role for user '{}'".format(username)) |
| 623 | rid = role[0]["_id"] |
| tierno | 701018c | 2019-06-25 11:13:14 +0000 | [diff] [blame] | 624 | if not indata.get("project_role_mappings"): |
| 625 | indata["project_role_mappings"] = [] |
| 626 | for project in indata["projects"]: |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 627 | pid = self.auth.get_project(project)["_id"] |
| 628 | prm = {"project": pid, "role": rid} |
| 629 | if prm not in indata["project_role_mappings"]: |
| 630 | indata["project_role_mappings"].append(prm) |
| tierno | 701018c | 2019-06-25 11:13:14 +0000 | [diff] [blame] | 631 | # raise EngineException("Format invalid: the keyword 'projects' is not allowed for keystone authentication", |
| 632 | # HTTPStatus.BAD_REQUEST) |
| Eduardo Sousa | 339ed78 | 2019-05-28 14:25:00 +0100 | [diff] [blame] | 633 | |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 634 | def check_conflict_on_edit(self, session, final_content, edit_content, _id): |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 635 | """ |
| 636 | Check that the data to be edited/uploaded is valid |
| 637 | |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 638 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 639 | :param final_content: data once modified |
| 640 | :param edit_content: incremental data that contains the modifications to apply |
| 641 | :param _id: internal _id |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 642 | :return: None or raises EngineException |
| 643 | """ |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 644 | |
| tierno | cf042d3 | 2019-06-13 09:06:40 +0000 | [diff] [blame] | 645 | if "username" in edit_content: |
| 646 | username = edit_content.get("username") |
| 647 | if is_valid_uuid(username): |
| delacruzramo | ceb8baf | 2019-06-21 14:25:38 +0200 | [diff] [blame] | 648 | raise EngineException("username '{}' cannot have an uuid format".format(username), |
| tierno | cf042d3 | 2019-06-13 09:06:40 +0000 | [diff] [blame] | 649 | HTTPStatus.UNPROCESSABLE_ENTITY) |
| 650 | |
| 651 | # Check that username is not used, regardless keystone already checks this |
| 652 | if self.auth.get_user_list(filter_q={"name": username}): |
| 653 | raise EngineException("username '{}' is already used".format(username), HTTPStatus.CONFLICT) |
| 654 | |
| 655 | if final_content["username"] == "admin": |
| 656 | for mapping in edit_content.get("remove_project_role_mappings", ()): |
| 657 | if mapping["project"] == "admin" and mapping.get("role") in (None, "system_admin"): |
| 658 | # TODO make this also available for project id and role id |
| 659 | raise EngineException("You cannot remove system_admin role from admin user", |
| 660 | http_code=HTTPStatus.FORBIDDEN) |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 661 | |
| bravof | b995ea2 | 2021-02-10 10:57:52 -0300 | [diff] [blame] | 662 | return final_content |
| 663 | |
| tierno | b4844ab | 2019-05-23 08:42:12 +0000 | [diff] [blame] | 664 | def check_conflict_on_del(self, session, _id, db_content): |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 665 | """ |
| 666 | Check if deletion can be done because of dependencies if it is not force. To override |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 667 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 668 | :param _id: internal _id |
| tierno | b4844ab | 2019-05-23 08:42:12 +0000 | [diff] [blame] | 669 | :param db_content: The database content of this item _id |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 670 | :return: None if ok or raises EngineException with the conflict |
| 671 | """ |
| tierno | cf042d3 | 2019-06-13 09:06:40 +0000 | [diff] [blame] | 672 | if db_content["username"] == session["username"]: |
| 673 | raise EngineException("You cannot delete your own login user ", http_code=HTTPStatus.CONFLICT) |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 674 | # TODO: Check that user is not logged in ? How? (Would require listing current tokens) |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 675 | |
| Eduardo Sousa | a16a4fa | 2019-05-23 01:41:18 +0100 | [diff] [blame] | 676 | @staticmethod |
| 677 | def format_on_show(content): |
| 678 | """ |
| Eduardo Sousa | 4460390 | 2019-06-04 08:10:32 +0100 | [diff] [blame] | 679 | Modifies the content of the role information to separate the role |
| Eduardo Sousa | a16a4fa | 2019-05-23 01:41:18 +0100 | [diff] [blame] | 680 | metadata from the role definition. |
| 681 | """ |
| 682 | project_role_mappings = [] |
| 683 | |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 684 | if "projects" in content: |
| 685 | for project in content["projects"]: |
| 686 | for role in project["roles"]: |
| 687 | project_role_mappings.append({"project": project["_id"], |
| 688 | "project_name": project["name"], |
| 689 | "role": role["_id"], |
| 690 | "role_name": role["name"]}) |
| 691 | del content["projects"] |
| Eduardo Sousa | a16a4fa | 2019-05-23 01:41:18 +0100 | [diff] [blame] | 692 | content["project_role_mappings"] = project_role_mappings |
| 693 | |
| Eduardo Sousa | 0b1d61b | 2019-05-30 19:55:52 +0100 | [diff] [blame] | 694 | return content |
| 695 | |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 696 | def new(self, rollback, session, indata=None, kwargs=None, headers=None): |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 697 | """ |
| 698 | Creates a new entry into the authentication backend. |
| 699 | |
| 700 | NOTE: Overrides BaseTopic functionality because it doesn't require access to database. |
| 701 | |
| 702 | :param rollback: list to append created items at database in case a rollback may to be done |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 703 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 704 | :param indata: data to be inserted |
| 705 | :param kwargs: used to override the indata descriptor |
| 706 | :param headers: http request headers |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 707 | :return: _id: identity of the inserted data, operation _id (None) |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 708 | """ |
| 709 | try: |
| 710 | content = BaseTopic._remove_envelop(indata) |
| 711 | |
| 712 | # Override descriptor with query string kwargs |
| 713 | BaseTopic._update_input_with_kwargs(content, kwargs) |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 714 | content = self._validate_input_new(content, session["force"]) |
| 715 | self.check_conflict_on_new(session, content) |
| tierno | cf042d3 | 2019-06-13 09:06:40 +0000 | [diff] [blame] | 716 | # self.format_on_new(content, session["project_id"], make_public=session["public"]) |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 717 | now = time() |
| 718 | content["_admin"] = {"created": now, "modified": now} |
| 719 | prms = [] |
| 720 | for prm in content.get("project_role_mappings", []): |
| 721 | proj = self.auth.get_project(prm["project"], not session["force"]) |
| 722 | role = self.auth.get_role(prm["role"], not session["force"]) |
| 723 | pid = proj["_id"] if proj else None |
| 724 | rid = role["_id"] if role else None |
| 725 | prl = {"project": pid, "role": rid} |
| 726 | if prl not in prms: |
| 727 | prms.append(prl) |
| 728 | content["project_role_mappings"] = prms |
| 729 | # _id = self.auth.create_user(content["username"], content["password"])["_id"] |
| 730 | _id = self.auth.create_user(content)["_id"] |
| Eduardo Sousa | 4460390 | 2019-06-04 08:10:32 +0100 | [diff] [blame] | 731 | |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 732 | rollback.append({"topic": self.topic, "_id": _id}) |
| tierno | cf042d3 | 2019-06-13 09:06:40 +0000 | [diff] [blame] | 733 | # del content["password"] |
| agarwalat | 5347198 | 2020-10-08 13:06:14 +0000 | [diff] [blame] | 734 | self._send_msg("created", content, not_send_msg=None) |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 735 | return _id, None |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 736 | except ValidationError as e: |
| 737 | raise EngineException(e, HTTPStatus.UNPROCESSABLE_ENTITY) |
| 738 | |
| K Sai Kiran | d010e3e | 2020-08-28 15:11:48 +0530 | [diff] [blame] | 739 | def show(self, session, _id, api_req=False): |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 740 | """ |
| 741 | Get complete information on an topic |
| 742 | |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 743 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| tierno | 5ec768a | 2020-03-31 09:46:44 +0000 | [diff] [blame] | 744 | :param _id: server internal id or username |
| K Sai Kiran | d010e3e | 2020-08-28 15:11:48 +0530 | [diff] [blame] | 745 | :param api_req: True if this call is serving an external API request. False if serving internal request. |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 746 | :return: dictionary, raise exception if not found. |
| 747 | """ |
| tierno | cf042d3 | 2019-06-13 09:06:40 +0000 | [diff] [blame] | 748 | # Allow _id to be a name or uuid |
| tierno | ad6d533 | 2020-02-19 14:29:49 +0000 | [diff] [blame] | 749 | filter_q = {"username": _id} |
| delacruzramo | 029405d | 2019-09-26 10:52:56 +0200 | [diff] [blame] | 750 | # users = self.auth.get_user_list(filter_q) |
| 751 | users = self.list(session, filter_q) # To allow default filtering (Bug 853) |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 752 | if len(users) == 1: |
| tierno | 1546f2a | 2019-08-20 15:38:11 +0000 | [diff] [blame] | 753 | return users[0] |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 754 | elif len(users) > 1: |
| tierno | 5ec768a | 2020-03-31 09:46:44 +0000 | [diff] [blame] | 755 | raise EngineException("Too many users found for '{}'".format(_id), HTTPStatus.CONFLICT) |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 756 | else: |
| tierno | 5ec768a | 2020-03-31 09:46:44 +0000 | [diff] [blame] | 757 | raise EngineException("User '{}' not found".format(_id), HTTPStatus.NOT_FOUND) |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 758 | |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 759 | def edit(self, session, _id, indata=None, kwargs=None, content=None): |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 760 | """ |
| 761 | Updates an user entry. |
| 762 | |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 763 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 764 | :param _id: |
| 765 | :param indata: data to be inserted |
| 766 | :param kwargs: used to override the indata descriptor |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 767 | :param content: |
| 768 | :return: _id: identity of the inserted data. |
| 769 | """ |
| 770 | indata = self._remove_envelop(indata) |
| 771 | |
| 772 | # Override descriptor with query string kwargs |
| 773 | if kwargs: |
| 774 | BaseTopic._update_input_with_kwargs(indata, kwargs) |
| 775 | try: |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 776 | if not content: |
| 777 | content = self.show(session, _id) |
| Frank Bryden | deba68e | 2020-07-27 13:55:11 +0000 | [diff] [blame] | 778 | indata = self._validate_input_edit(indata, content, force=session["force"]) |
| bravof | b995ea2 | 2021-02-10 10:57:52 -0300 | [diff] [blame] | 779 | content = self.check_conflict_on_edit(session, content, indata, _id=_id) |
| tierno | cf042d3 | 2019-06-13 09:06:40 +0000 | [diff] [blame] | 780 | # self.format_on_edit(content, indata) |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 781 | |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 782 | if not ("password" in indata or "username" in indata or indata.get("remove_project_role_mappings") or |
| 783 | indata.get("add_project_role_mappings") or indata.get("project_role_mappings") or |
| 784 | indata.get("projects") or indata.get("add_projects")): |
| tierno | cf042d3 | 2019-06-13 09:06:40 +0000 | [diff] [blame] | 785 | return _id |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 786 | if indata.get("project_role_mappings") \ |
| 787 | and (indata.get("remove_project_role_mappings") or indata.get("add_project_role_mappings")): |
| tierno | cf042d3 | 2019-06-13 09:06:40 +0000 | [diff] [blame] | 788 | raise EngineException("Option 'project_role_mappings' is incompatible with 'add_project_role_mappings" |
| 789 | "' or 'remove_project_role_mappings'", http_code=HTTPStatus.BAD_REQUEST) |
| Eduardo Sousa | 4460390 | 2019-06-04 08:10:32 +0100 | [diff] [blame] | 790 | |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 791 | if indata.get("projects") or indata.get("add_projects"): |
| 792 | role = self.auth.get_role_list({"name": "project_admin"}) |
| 793 | if not role: |
| 794 | role = self.auth.get_role_list() |
| 795 | if not role: |
| 796 | raise AuthconnNotFoundException("Can't find a default role for user '{}'" |
| 797 | .format(content["username"])) |
| 798 | rid = role[0]["_id"] |
| 799 | if "add_project_role_mappings" not in indata: |
| 800 | indata["add_project_role_mappings"] = [] |
| tierno | 1546f2a | 2019-08-20 15:38:11 +0000 | [diff] [blame] | 801 | if "remove_project_role_mappings" not in indata: |
| 802 | indata["remove_project_role_mappings"] = [] |
| 803 | if isinstance(indata.get("projects"), dict): |
| 804 | # backward compatible |
| 805 | for k, v in indata["projects"].items(): |
| 806 | if k.startswith("$") and v is None: |
| 807 | indata["remove_project_role_mappings"].append({"project": k[1:]}) |
| 808 | elif k.startswith("$+"): |
| 809 | indata["add_project_role_mappings"].append({"project": v, "role": rid}) |
| 810 | del indata["projects"] |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 811 | for proj in indata.get("projects", []) + indata.get("add_projects", []): |
| 812 | indata["add_project_role_mappings"].append({"project": proj, "role": rid}) |
| 813 | |
| 814 | # user = self.show(session, _id) # Already in 'content' |
| 815 | original_mapping = content["project_role_mappings"] |
| Eduardo Sousa | 4460390 | 2019-06-04 08:10:32 +0100 | [diff] [blame] | 816 | |
| tierno | cf042d3 | 2019-06-13 09:06:40 +0000 | [diff] [blame] | 817 | mappings_to_add = [] |
| 818 | mappings_to_remove = [] |
| Eduardo Sousa | 4460390 | 2019-06-04 08:10:32 +0100 | [diff] [blame] | 819 | |
| tierno | cf042d3 | 2019-06-13 09:06:40 +0000 | [diff] [blame] | 820 | # remove |
| 821 | for to_remove in indata.get("remove_project_role_mappings", ()): |
| 822 | for mapping in original_mapping: |
| 823 | if to_remove["project"] in (mapping["project"], mapping["project_name"]): |
| 824 | if not to_remove.get("role") or to_remove["role"] in (mapping["role"], mapping["role_name"]): |
| 825 | mappings_to_remove.append(mapping) |
| Eduardo Sousa | 4460390 | 2019-06-04 08:10:32 +0100 | [diff] [blame] | 826 | |
| tierno | cf042d3 | 2019-06-13 09:06:40 +0000 | [diff] [blame] | 827 | # add |
| 828 | for to_add in indata.get("add_project_role_mappings", ()): |
| 829 | for mapping in original_mapping: |
| 830 | if to_add["project"] in (mapping["project"], mapping["project_name"]) and \ |
| 831 | to_add["role"] in (mapping["role"], mapping["role_name"]): |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 832 | |
| tierno | cf042d3 | 2019-06-13 09:06:40 +0000 | [diff] [blame] | 833 | if mapping in mappings_to_remove: # do not remove |
| 834 | mappings_to_remove.remove(mapping) |
| 835 | break # do not add, it is already at user |
| 836 | else: |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 837 | pid = self.auth.get_project(to_add["project"])["_id"] |
| 838 | rid = self.auth.get_role(to_add["role"])["_id"] |
| 839 | mappings_to_add.append({"project": pid, "role": rid}) |
| tierno | cf042d3 | 2019-06-13 09:06:40 +0000 | [diff] [blame] | 840 | |
| 841 | # set |
| 842 | if indata.get("project_role_mappings"): |
| 843 | for to_set in indata["project_role_mappings"]: |
| 844 | for mapping in original_mapping: |
| 845 | if to_set["project"] in (mapping["project"], mapping["project_name"]) and \ |
| 846 | to_set["role"] in (mapping["role"], mapping["role_name"]): |
| tierno | cf042d3 | 2019-06-13 09:06:40 +0000 | [diff] [blame] | 847 | if mapping in mappings_to_remove: # do not remove |
| 848 | mappings_to_remove.remove(mapping) |
| 849 | break # do not add, it is already at user |
| 850 | else: |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 851 | pid = self.auth.get_project(to_set["project"])["_id"] |
| 852 | rid = self.auth.get_role(to_set["role"])["_id"] |
| 853 | mappings_to_add.append({"project": pid, "role": rid}) |
| tierno | cf042d3 | 2019-06-13 09:06:40 +0000 | [diff] [blame] | 854 | for mapping in original_mapping: |
| 855 | for to_set in indata["project_role_mappings"]: |
| 856 | if to_set["project"] in (mapping["project"], mapping["project_name"]) and \ |
| 857 | to_set["role"] in (mapping["role"], mapping["role_name"]): |
| 858 | break |
| 859 | else: |
| 860 | # delete |
| 861 | if mapping not in mappings_to_remove: # do not remove |
| 862 | mappings_to_remove.append(mapping) |
| 863 | |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 864 | self.auth.update_user({"_id": _id, "username": indata.get("username"), "password": indata.get("password"), |
| 865 | "add_project_role_mappings": mappings_to_add, |
| 866 | "remove_project_role_mappings": mappings_to_remove |
| 867 | }) |
| agarwalat | 5347198 | 2020-10-08 13:06:14 +0000 | [diff] [blame] | 868 | data_to_send = {'_id': _id, "changes": indata} |
| 869 | self._send_msg("edited", data_to_send, not_send_msg=None) |
| tierno | cf042d3 | 2019-06-13 09:06:40 +0000 | [diff] [blame] | 870 | |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 871 | # return _id |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 872 | except ValidationError as e: |
| 873 | raise EngineException(e, HTTPStatus.UNPROCESSABLE_ENTITY) |
| 874 | |
| tierno | c4e07d0 | 2020-08-14 14:25:32 +0000 | [diff] [blame] | 875 | def list(self, session, filter_q=None, api_req=False): |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 876 | """ |
| 877 | Get a list of the topic that matches a filter |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 878 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 879 | :param filter_q: filter of data to be applied |
| K Sai Kiran | d010e3e | 2020-08-28 15:11:48 +0530 | [diff] [blame] | 880 | :param api_req: True if this call is serving an external API request. False if serving internal request. |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 881 | :return: The list, it can be empty if no one match the filter. |
| 882 | """ |
| delacruzramo | 029405d | 2019-09-26 10:52:56 +0200 | [diff] [blame] | 883 | user_list = self.auth.get_user_list(filter_q) |
| 884 | if not session["allow_show_user_project_role"]: |
| 885 | # Bug 853 - Default filtering |
| 886 | user_list = [usr for usr in user_list if usr["username"] == session["username"]] |
| 887 | return user_list |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 888 | |
| tierno | bee3bad | 2019-12-05 12:26:01 +0000 | [diff] [blame] | 889 | def delete(self, session, _id, dry_run=False, not_send_msg=None): |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 890 | """ |
| 891 | Delete item by its internal _id |
| 892 | |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 893 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 894 | :param _id: server internal id |
| 895 | :param force: indicates if deletion must be forced in case of conflict |
| 896 | :param dry_run: make checking but do not delete |
| tierno | bee3bad | 2019-12-05 12:26:01 +0000 | [diff] [blame] | 897 | :param not_send_msg: To not send message (False) or store content (list) instead |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 898 | :return: dictionary with deleted item _id. It raises EngineException on error: not found, conflict, ... |
| 899 | """ |
| tierno | cf042d3 | 2019-06-13 09:06:40 +0000 | [diff] [blame] | 900 | # Allow _id to be a name or uuid |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 901 | user = self.auth.get_user(_id) |
| 902 | uid = user["_id"] |
| 903 | self.check_conflict_on_del(session, uid, user) |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 904 | if not dry_run: |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 905 | v = self.auth.delete_user(uid) |
| agarwalat | 5347198 | 2020-10-08 13:06:14 +0000 | [diff] [blame] | 906 | self._send_msg("deleted", user, not_send_msg=not_send_msg) |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 907 | return v |
| 908 | return None |
| 909 | |
| 910 | |
| 911 | class ProjectTopicAuth(ProjectTopic): |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 912 | # topic = "projects" |
| agarwalat | 5347198 | 2020-10-08 13:06:14 +0000 | [diff] [blame] | 913 | topic_msg = "project" |
| Eduardo Sousa | 4460390 | 2019-06-04 08:10:32 +0100 | [diff] [blame] | 914 | schema_new = project_new_schema |
| 915 | schema_edit = project_edit_schema |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 916 | |
| 917 | def __init__(self, db, fs, msg, auth): |
| delacruzramo | 32bab47 | 2019-09-13 12:24:22 +0200 | [diff] [blame] | 918 | ProjectTopic.__init__(self, db, fs, msg, auth) |
| 919 | # self.auth = auth |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 920 | |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 921 | def check_conflict_on_new(self, session, indata): |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 922 | """ |
| 923 | Check that the data to be inserted is valid |
| 924 | |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 925 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 926 | :param indata: data to be inserted |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 927 | :return: None or raises EngineException |
| 928 | """ |
| tierno | cf042d3 | 2019-06-13 09:06:40 +0000 | [diff] [blame] | 929 | project_name = indata.get("name") |
| 930 | if is_valid_uuid(project_name): |
| delacruzramo | ceb8baf | 2019-06-21 14:25:38 +0200 | [diff] [blame] | 931 | raise EngineException("project name '{}' cannot have an uuid format".format(project_name), |
| tierno | cf042d3 | 2019-06-13 09:06:40 +0000 | [diff] [blame] | 932 | HTTPStatus.UNPROCESSABLE_ENTITY) |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 933 | |
| tierno | cf042d3 | 2019-06-13 09:06:40 +0000 | [diff] [blame] | 934 | project_list = self.auth.get_project_list(filter_q={"name": project_name}) |
| 935 | |
| 936 | if project_list: |
| 937 | raise EngineException("project '{}' exists".format(project_name), HTTPStatus.CONFLICT) |
| 938 | |
| 939 | def check_conflict_on_edit(self, session, final_content, edit_content, _id): |
| 940 | """ |
| 941 | Check that the data to be edited/uploaded is valid |
| 942 | |
| 943 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| 944 | :param final_content: data once modified |
| 945 | :param edit_content: incremental data that contains the modifications to apply |
| 946 | :param _id: internal _id |
| 947 | :return: None or raises EngineException |
| 948 | """ |
| 949 | |
| 950 | project_name = edit_content.get("name") |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 951 | if project_name != final_content["name"]: # It is a true renaming |
| tierno | cf042d3 | 2019-06-13 09:06:40 +0000 | [diff] [blame] | 952 | if is_valid_uuid(project_name): |
| delacruzramo | 79e40f4 | 2019-10-10 16:36:40 +0200 | [diff] [blame] | 953 | raise EngineException("project name '{}' cannot have an uuid format".format(project_name), |
| tierno | cf042d3 | 2019-06-13 09:06:40 +0000 | [diff] [blame] | 954 | HTTPStatus.UNPROCESSABLE_ENTITY) |
| 955 | |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 956 | if final_content["name"] == "admin": |
| 957 | raise EngineException("You cannot rename project 'admin'", http_code=HTTPStatus.CONFLICT) |
| 958 | |
| tierno | cf042d3 | 2019-06-13 09:06:40 +0000 | [diff] [blame] | 959 | # Check that project name is not used, regardless keystone already checks this |
| delacruzramo | 32bab47 | 2019-09-13 12:24:22 +0200 | [diff] [blame] | 960 | if project_name and self.auth.get_project_list(filter_q={"name": project_name}): |
| tierno | cf042d3 | 2019-06-13 09:06:40 +0000 | [diff] [blame] | 961 | raise EngineException("project '{}' is already used".format(project_name), HTTPStatus.CONFLICT) |
| bravof | b995ea2 | 2021-02-10 10:57:52 -0300 | [diff] [blame] | 962 | return final_content |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 963 | |
| tierno | b4844ab | 2019-05-23 08:42:12 +0000 | [diff] [blame] | 964 | def check_conflict_on_del(self, session, _id, db_content): |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 965 | """ |
| 966 | Check if deletion can be done because of dependencies if it is not force. To override |
| 967 | |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 968 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 969 | :param _id: internal _id |
| tierno | b4844ab | 2019-05-23 08:42:12 +0000 | [diff] [blame] | 970 | :param db_content: The database content of this item _id |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 971 | :return: None if ok or raises EngineException with the conflict |
| 972 | """ |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 973 | |
| 974 | def check_rw_projects(topic, title, id_field): |
| 975 | for desc in self.db.get_list(topic): |
| 976 | if _id in desc["_admin"]["projects_read"] + desc["_admin"]["projects_write"]: |
| 977 | raise EngineException("Project '{}' ({}) is being used by {} '{}'" |
| 978 | .format(db_content["name"], _id, title, desc[id_field]), HTTPStatus.CONFLICT) |
| 979 | |
| 980 | if _id in session["project_id"]: |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 981 | raise EngineException("You cannot delete your own project", http_code=HTTPStatus.CONFLICT) |
| 982 | |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 983 | if db_content["name"] == "admin": |
| 984 | raise EngineException("You cannot delete project 'admin'", http_code=HTTPStatus.CONFLICT) |
| 985 | |
| 986 | # If any user is using this project, raise CONFLICT exception |
| 987 | if not session["force"]: |
| 988 | for user in self.auth.get_user_list(): |
| tierno | 1546f2a | 2019-08-20 15:38:11 +0000 | [diff] [blame] | 989 | for prm in user.get("project_role_mappings"): |
| 990 | if prm["project"] == _id: |
| 991 | raise EngineException("Project '{}' ({}) is being used by user '{}'" |
| 992 | .format(db_content["name"], _id, user["username"]), HTTPStatus.CONFLICT) |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 993 | |
| 994 | # If any VNFD, NSD, NST, PDU, etc. is using this project, raise CONFLICT exception |
| 995 | if not session["force"]: |
| 996 | check_rw_projects("vnfds", "VNF Descriptor", "id") |
| 997 | check_rw_projects("nsds", "NS Descriptor", "id") |
| 998 | check_rw_projects("nsts", "NS Template", "id") |
| 999 | check_rw_projects("pdus", "PDU Descriptor", "name") |
| 1000 | |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 1001 | def new(self, rollback, session, indata=None, kwargs=None, headers=None): |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1002 | """ |
| 1003 | Creates a new entry into the authentication backend. |
| 1004 | |
| 1005 | NOTE: Overrides BaseTopic functionality because it doesn't require access to database. |
| 1006 | |
| 1007 | :param rollback: list to append created items at database in case a rollback may to be done |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 1008 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1009 | :param indata: data to be inserted |
| 1010 | :param kwargs: used to override the indata descriptor |
| 1011 | :param headers: http request headers |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 1012 | :return: _id: identity of the inserted data, operation _id (None) |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1013 | """ |
| 1014 | try: |
| 1015 | content = BaseTopic._remove_envelop(indata) |
| 1016 | |
| 1017 | # Override descriptor with query string kwargs |
| 1018 | BaseTopic._update_input_with_kwargs(content, kwargs) |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 1019 | content = self._validate_input_new(content, session["force"]) |
| 1020 | self.check_conflict_on_new(session, content) |
| 1021 | self.format_on_new(content, project_id=session["project_id"], make_public=session["public"]) |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 1022 | _id = self.auth.create_project(content) |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1023 | rollback.append({"topic": self.topic, "_id": _id}) |
| agarwalat | 5347198 | 2020-10-08 13:06:14 +0000 | [diff] [blame] | 1024 | self._send_msg("created", content, not_send_msg=None) |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 1025 | return _id, None |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1026 | except ValidationError as e: |
| 1027 | raise EngineException(e, HTTPStatus.UNPROCESSABLE_ENTITY) |
| 1028 | |
| K Sai Kiran | d010e3e | 2020-08-28 15:11:48 +0530 | [diff] [blame] | 1029 | def show(self, session, _id, api_req=False): |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1030 | """ |
| 1031 | Get complete information on an topic |
| 1032 | |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 1033 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1034 | :param _id: server internal id |
| K Sai Kiran | d010e3e | 2020-08-28 15:11:48 +0530 | [diff] [blame] | 1035 | :param api_req: True if this call is serving an external API request. False if serving internal request. |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1036 | :return: dictionary, raise exception if not found. |
| 1037 | """ |
| tierno | cf042d3 | 2019-06-13 09:06:40 +0000 | [diff] [blame] | 1038 | # Allow _id to be a name or uuid |
| 1039 | filter_q = {self.id_field(self.topic, _id): _id} |
| delacruzramo | 029405d | 2019-09-26 10:52:56 +0200 | [diff] [blame] | 1040 | # projects = self.auth.get_project_list(filter_q=filter_q) |
| 1041 | projects = self.list(session, filter_q) # To allow default filtering (Bug 853) |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1042 | if len(projects) == 1: |
| 1043 | return projects[0] |
| 1044 | elif len(projects) > 1: |
| 1045 | raise EngineException("Too many projects found", HTTPStatus.CONFLICT) |
| 1046 | else: |
| 1047 | raise EngineException("Project not found", HTTPStatus.NOT_FOUND) |
| 1048 | |
| tierno | c4e07d0 | 2020-08-14 14:25:32 +0000 | [diff] [blame] | 1049 | def list(self, session, filter_q=None, api_req=False): |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1050 | """ |
| 1051 | Get a list of the topic that matches a filter |
| 1052 | |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 1053 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1054 | :param filter_q: filter of data to be applied |
| 1055 | :return: The list, it can be empty if no one match the filter. |
| 1056 | """ |
| delacruzramo | 029405d | 2019-09-26 10:52:56 +0200 | [diff] [blame] | 1057 | project_list = self.auth.get_project_list(filter_q) |
| 1058 | if not session["allow_show_user_project_role"]: |
| 1059 | # Bug 853 - Default filtering |
| 1060 | user = self.auth.get_user(session["username"]) |
| 1061 | projects = [prm["project"] for prm in user["project_role_mappings"]] |
| 1062 | project_list = [proj for proj in project_list if proj["_id"] in projects] |
| 1063 | return project_list |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1064 | |
| tierno | bee3bad | 2019-12-05 12:26:01 +0000 | [diff] [blame] | 1065 | def delete(self, session, _id, dry_run=False, not_send_msg=None): |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1066 | """ |
| 1067 | Delete item by its internal _id |
| 1068 | |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 1069 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1070 | :param _id: server internal id |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1071 | :param dry_run: make checking but do not delete |
| tierno | bee3bad | 2019-12-05 12:26:01 +0000 | [diff] [blame] | 1072 | :param not_send_msg: To not send message (False) or store content (list) instead |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1073 | :return: dictionary with deleted item _id. It raises EngineException on error: not found, conflict, ... |
| 1074 | """ |
| tierno | cf042d3 | 2019-06-13 09:06:40 +0000 | [diff] [blame] | 1075 | # Allow _id to be a name or uuid |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 1076 | proj = self.auth.get_project(_id) |
| 1077 | pid = proj["_id"] |
| 1078 | self.check_conflict_on_del(session, pid, proj) |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1079 | if not dry_run: |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 1080 | v = self.auth.delete_project(pid) |
| agarwalat | 5347198 | 2020-10-08 13:06:14 +0000 | [diff] [blame] | 1081 | self._send_msg("deleted", proj, not_send_msg=None) |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1082 | return v |
| 1083 | return None |
| 1084 | |
| tierno | 4015b47 | 2019-06-10 13:57:29 +0000 | [diff] [blame] | 1085 | def edit(self, session, _id, indata=None, kwargs=None, content=None): |
| 1086 | """ |
| 1087 | Updates a project entry. |
| 1088 | |
| 1089 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| 1090 | :param _id: |
| 1091 | :param indata: data to be inserted |
| 1092 | :param kwargs: used to override the indata descriptor |
| 1093 | :param content: |
| 1094 | :return: _id: identity of the inserted data. |
| 1095 | """ |
| 1096 | indata = self._remove_envelop(indata) |
| 1097 | |
| 1098 | # Override descriptor with query string kwargs |
| 1099 | if kwargs: |
| 1100 | BaseTopic._update_input_with_kwargs(indata, kwargs) |
| 1101 | try: |
| tierno | 4015b47 | 2019-06-10 13:57:29 +0000 | [diff] [blame] | 1102 | if not content: |
| 1103 | content = self.show(session, _id) |
| Frank Bryden | deba68e | 2020-07-27 13:55:11 +0000 | [diff] [blame] | 1104 | indata = self._validate_input_edit(indata, content, force=session["force"]) |
| bravof | b995ea2 | 2021-02-10 10:57:52 -0300 | [diff] [blame] | 1105 | content = self.check_conflict_on_edit(session, content, indata, _id=_id) |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 1106 | self.format_on_edit(content, indata) |
| agarwalat | 5347198 | 2020-10-08 13:06:14 +0000 | [diff] [blame] | 1107 | content_original = copy.deepcopy(content) |
| delacruzramo | 32bab47 | 2019-09-13 12:24:22 +0200 | [diff] [blame] | 1108 | deep_update_rfc7396(content, indata) |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 1109 | self.auth.update_project(content["_id"], content) |
| agarwalat | 5347198 | 2020-10-08 13:06:14 +0000 | [diff] [blame] | 1110 | proj_data = {"_id": _id, "changes": indata, "original": content_original} |
| 1111 | self._send_msg("edited", proj_data, not_send_msg=None) |
| tierno | 4015b47 | 2019-06-10 13:57:29 +0000 | [diff] [blame] | 1112 | except ValidationError as e: |
| 1113 | raise EngineException(e, HTTPStatus.UNPROCESSABLE_ENTITY) |
| 1114 | |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1115 | |
| 1116 | class RoleTopicAuth(BaseTopic): |
| delacruzramo | ceb8baf | 2019-06-21 14:25:38 +0200 | [diff] [blame] | 1117 | topic = "roles" |
| 1118 | topic_msg = None # "roles" |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1119 | schema_new = roles_new_schema |
| 1120 | schema_edit = roles_edit_schema |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 1121 | multiproject = False |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1122 | |
| tierno | 9e87a7f | 2020-03-23 09:24:10 +0000 | [diff] [blame] | 1123 | def __init__(self, db, fs, msg, auth): |
| delacruzramo | 32bab47 | 2019-09-13 12:24:22 +0200 | [diff] [blame] | 1124 | BaseTopic.__init__(self, db, fs, msg, auth) |
| 1125 | # self.auth = auth |
| tierno | 9e87a7f | 2020-03-23 09:24:10 +0000 | [diff] [blame] | 1126 | self.operations = auth.role_permissions |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 1127 | # self.topic = "roles_operations" if isinstance(auth, AuthconnKeystone) else "roles" |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1128 | |
| 1129 | @staticmethod |
| 1130 | def validate_role_definition(operations, role_definitions): |
| 1131 | """ |
| 1132 | Validates the role definition against the operations defined in |
| 1133 | the resources to operations files. |
| 1134 | |
| 1135 | :param operations: operations list |
| 1136 | :param role_definitions: role definition to test |
| 1137 | :return: None if ok, raises ValidationError exception on error |
| 1138 | """ |
| tierno | 1f029d8 | 2019-06-13 22:37:04 +0000 | [diff] [blame] | 1139 | if not role_definitions.get("permissions"): |
| 1140 | return |
| 1141 | ignore_fields = ["admin", "default"] |
| 1142 | for role_def in role_definitions["permissions"].keys(): |
| Eduardo Sousa | 37de091 | 2019-05-23 02:17:22 +0100 | [diff] [blame] | 1143 | if role_def in ignore_fields: |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1144 | continue |
| Eduardo Sousa | c768937 | 2019-06-04 16:01:46 +0100 | [diff] [blame] | 1145 | if role_def[-1] == ":": |
| tierno | 1f029d8 | 2019-06-13 22:37:04 +0000 | [diff] [blame] | 1146 | raise ValidationError("Operation cannot end with ':'") |
| Eduardo Sousa | c5a1889 | 2019-06-06 14:51:23 +0100 | [diff] [blame] | 1147 | |
| tierno | 97639b4 | 2020-08-04 12:48:15 +0000 | [diff] [blame] | 1148 | match = next((op for op in operations if op == role_def or op.startswith(role_def + ":")), None) |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1149 | |
| tierno | 97639b4 | 2020-08-04 12:48:15 +0000 | [diff] [blame] | 1150 | if not match: |
| tierno | 1f029d8 | 2019-06-13 22:37:04 +0000 | [diff] [blame] | 1151 | raise ValidationError("Invalid permission '{}'".format(role_def)) |
| Eduardo Sousa | 37de091 | 2019-05-23 02:17:22 +0100 | [diff] [blame] | 1152 | |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1153 | def _validate_input_new(self, input, force=False): |
| 1154 | """ |
| 1155 | Validates input user content for a new entry. |
| 1156 | |
| 1157 | :param input: user input content for the new topic |
| 1158 | :param force: may be used for being more tolerant |
| 1159 | :return: The same input content, or a changed version of it. |
| 1160 | """ |
| 1161 | if self.schema_new: |
| 1162 | validate_input(input, self.schema_new) |
| Eduardo Sousa | 37de091 | 2019-05-23 02:17:22 +0100 | [diff] [blame] | 1163 | self.validate_role_definition(self.operations, input) |
| Eduardo Sousa | c465036 | 2019-06-04 13:24:22 +0100 | [diff] [blame] | 1164 | |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1165 | return input |
| 1166 | |
| Frank Bryden | deba68e | 2020-07-27 13:55:11 +0000 | [diff] [blame] | 1167 | def _validate_input_edit(self, input, content, force=False): |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1168 | """ |
| 1169 | Validates input user content for updating an entry. |
| 1170 | |
| 1171 | :param input: user input content for the new topic |
| 1172 | :param force: may be used for being more tolerant |
| 1173 | :return: The same input content, or a changed version of it. |
| 1174 | """ |
| 1175 | if self.schema_edit: |
| 1176 | validate_input(input, self.schema_edit) |
| Eduardo Sousa | 37de091 | 2019-05-23 02:17:22 +0100 | [diff] [blame] | 1177 | self.validate_role_definition(self.operations, input) |
| Eduardo Sousa | c465036 | 2019-06-04 13:24:22 +0100 | [diff] [blame] | 1178 | |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1179 | return input |
| 1180 | |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 1181 | def check_conflict_on_new(self, session, indata): |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1182 | """ |
| 1183 | Check that the data to be inserted is valid |
| 1184 | |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 1185 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1186 | :param indata: data to be inserted |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1187 | :return: None or raises EngineException |
| 1188 | """ |
| delacruzramo | 79e40f4 | 2019-10-10 16:36:40 +0200 | [diff] [blame] | 1189 | # check name is not uuid |
| 1190 | role_name = indata.get("name") |
| 1191 | if is_valid_uuid(role_name): |
| 1192 | raise EngineException("role name '{}' cannot have an uuid format".format(role_name), |
| 1193 | HTTPStatus.UNPROCESSABLE_ENTITY) |
| tierno | 1f029d8 | 2019-06-13 22:37:04 +0000 | [diff] [blame] | 1194 | # check name not exists |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 1195 | name = indata["name"] |
| 1196 | # if self.db.get_one(self.topic, {"name": indata.get("name")}, fail_on_empty=False, fail_on_more=False): |
| 1197 | if self.auth.get_role_list({"name": name}): |
| 1198 | raise EngineException("role name '{}' exists".format(name), HTTPStatus.CONFLICT) |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1199 | |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 1200 | def check_conflict_on_edit(self, session, final_content, edit_content, _id): |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1201 | """ |
| 1202 | Check that the data to be edited/uploaded is valid |
| 1203 | |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 1204 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1205 | :param final_content: data once modified |
| 1206 | :param edit_content: incremental data that contains the modifications to apply |
| 1207 | :param _id: internal _id |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1208 | :return: None or raises EngineException |
| 1209 | """ |
| tierno | 1f029d8 | 2019-06-13 22:37:04 +0000 | [diff] [blame] | 1210 | if "default" not in final_content["permissions"]: |
| 1211 | final_content["permissions"]["default"] = False |
| 1212 | if "admin" not in final_content["permissions"]: |
| 1213 | final_content["permissions"]["admin"] = False |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1214 | |
| delacruzramo | 79e40f4 | 2019-10-10 16:36:40 +0200 | [diff] [blame] | 1215 | # check name is not uuid |
| 1216 | role_name = edit_content.get("name") |
| 1217 | if is_valid_uuid(role_name): |
| 1218 | raise EngineException("role name '{}' cannot have an uuid format".format(role_name), |
| 1219 | HTTPStatus.UNPROCESSABLE_ENTITY) |
| 1220 | |
| 1221 | # Check renaming of admin roles |
| 1222 | role = self.auth.get_role(_id) |
| 1223 | if role["name"] in ["system_admin", "project_admin"]: |
| 1224 | raise EngineException("You cannot rename role '{}'".format(role["name"]), http_code=HTTPStatus.FORBIDDEN) |
| 1225 | |
| tierno | 1f029d8 | 2019-06-13 22:37:04 +0000 | [diff] [blame] | 1226 | # check name not exists |
| 1227 | if "name" in edit_content: |
| 1228 | role_name = edit_content["name"] |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 1229 | # if self.db.get_one(self.topic, {"name":role_name,"_id.ne":_id}, fail_on_empty=False, fail_on_more=False): |
| 1230 | roles = self.auth.get_role_list({"name": role_name}) |
| 1231 | if roles and roles[0][BaseTopic.id_field("roles", _id)] != _id: |
| tierno | 1f029d8 | 2019-06-13 22:37:04 +0000 | [diff] [blame] | 1232 | raise EngineException("role name '{}' exists".format(role_name), HTTPStatus.CONFLICT) |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1233 | |
| bravof | b995ea2 | 2021-02-10 10:57:52 -0300 | [diff] [blame] | 1234 | return final_content |
| 1235 | |
| tierno | b4844ab | 2019-05-23 08:42:12 +0000 | [diff] [blame] | 1236 | def check_conflict_on_del(self, session, _id, db_content): |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1237 | """ |
| 1238 | Check if deletion can be done because of dependencies if it is not force. To override |
| 1239 | |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 1240 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1241 | :param _id: internal _id |
| tierno | b4844ab | 2019-05-23 08:42:12 +0000 | [diff] [blame] | 1242 | :param db_content: The database content of this item _id |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1243 | :return: None if ok or raises EngineException with the conflict |
| 1244 | """ |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 1245 | role = self.auth.get_role(_id) |
| 1246 | if role["name"] in ["system_admin", "project_admin"]: |
| 1247 | raise EngineException("You cannot delete role '{}'".format(role["name"]), http_code=HTTPStatus.FORBIDDEN) |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1248 | |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 1249 | # If any user is using this role, raise CONFLICT exception |
| delacruzramo | ad682a5 | 2019-12-10 16:26:34 +0100 | [diff] [blame] | 1250 | if not session["force"]: |
| 1251 | for user in self.auth.get_user_list(): |
| 1252 | for prm in user.get("project_role_mappings"): |
| 1253 | if prm["role"] == _id: |
| 1254 | raise EngineException("Role '{}' ({}) is being used by user '{}'" |
| 1255 | .format(role["name"], _id, user["username"]), HTTPStatus.CONFLICT) |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1256 | |
| 1257 | @staticmethod |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 1258 | def format_on_new(content, project_id=None, make_public=False): # TO BE REMOVED ? |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1259 | """ |
| 1260 | Modifies content descriptor to include _admin |
| 1261 | |
| 1262 | :param content: descriptor to be modified |
| 1263 | :param project_id: if included, it add project read/write permissions |
| 1264 | :param make_public: if included it is generated as public for reading. |
| 1265 | :return: None, but content is modified |
| 1266 | """ |
| 1267 | now = time() |
| 1268 | if "_admin" not in content: |
| 1269 | content["_admin"] = {} |
| 1270 | if not content["_admin"].get("created"): |
| 1271 | content["_admin"]["created"] = now |
| 1272 | content["_admin"]["modified"] = now |
| Eduardo Sousa | c465036 | 2019-06-04 13:24:22 +0100 | [diff] [blame] | 1273 | |
| tierno | 1f029d8 | 2019-06-13 22:37:04 +0000 | [diff] [blame] | 1274 | if "permissions" not in content: |
| 1275 | content["permissions"] = {} |
| Eduardo Sousa | c465036 | 2019-06-04 13:24:22 +0100 | [diff] [blame] | 1276 | |
| tierno | 1f029d8 | 2019-06-13 22:37:04 +0000 | [diff] [blame] | 1277 | if "default" not in content["permissions"]: |
| 1278 | content["permissions"]["default"] = False |
| 1279 | if "admin" not in content["permissions"]: |
| 1280 | content["permissions"]["admin"] = False |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1281 | |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1282 | @staticmethod |
| 1283 | def format_on_edit(final_content, edit_content): |
| 1284 | """ |
| 1285 | Modifies final_content descriptor to include the modified date. |
| 1286 | |
| 1287 | :param final_content: final descriptor generated |
| 1288 | :param edit_content: alterations to be include |
| 1289 | :return: None, but final_content is modified |
| 1290 | """ |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 1291 | if "_admin" in final_content: |
| 1292 | final_content["_admin"]["modified"] = time() |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1293 | |
| tierno | 1f029d8 | 2019-06-13 22:37:04 +0000 | [diff] [blame] | 1294 | if "permissions" not in final_content: |
| 1295 | final_content["permissions"] = {} |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1296 | |
| tierno | 1f029d8 | 2019-06-13 22:37:04 +0000 | [diff] [blame] | 1297 | if "default" not in final_content["permissions"]: |
| 1298 | final_content["permissions"]["default"] = False |
| 1299 | if "admin" not in final_content["permissions"]: |
| 1300 | final_content["permissions"]["admin"] = False |
| tierno | bdebce9 | 2019-07-01 15:36:49 +0000 | [diff] [blame] | 1301 | return None |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1302 | |
| K Sai Kiran | d010e3e | 2020-08-28 15:11:48 +0530 | [diff] [blame] | 1303 | def show(self, session, _id, api_req=False): |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 1304 | """ |
| 1305 | Get complete information on an topic |
| Eduardo Sousa | c465036 | 2019-06-04 13:24:22 +0100 | [diff] [blame] | 1306 | |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 1307 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| 1308 | :param _id: server internal id |
| K Sai Kiran | d010e3e | 2020-08-28 15:11:48 +0530 | [diff] [blame] | 1309 | :param api_req: True if this call is serving an external API request. False if serving internal request. |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 1310 | :return: dictionary, raise exception if not found. |
| 1311 | """ |
| 1312 | filter_q = {BaseTopic.id_field(self.topic, _id): _id} |
| delacruzramo | 029405d | 2019-09-26 10:52:56 +0200 | [diff] [blame] | 1313 | # roles = self.auth.get_role_list(filter_q) |
| 1314 | roles = self.list(session, filter_q) # To allow default filtering (Bug 853) |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 1315 | if not roles: |
| 1316 | raise AuthconnNotFoundException("Not found any role with filter {}".format(filter_q)) |
| 1317 | elif len(roles) > 1: |
| 1318 | raise AuthconnConflictException("Found more than one role with filter {}".format(filter_q)) |
| 1319 | return roles[0] |
| 1320 | |
| tierno | c4e07d0 | 2020-08-14 14:25:32 +0000 | [diff] [blame] | 1321 | def list(self, session, filter_q=None, api_req=False): |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 1322 | """ |
| 1323 | Get a list of the topic that matches a filter |
| 1324 | |
| 1325 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| 1326 | :param filter_q: filter of data to be applied |
| 1327 | :return: The list, it can be empty if no one match the filter. |
| 1328 | """ |
| delacruzramo | 029405d | 2019-09-26 10:52:56 +0200 | [diff] [blame] | 1329 | role_list = self.auth.get_role_list(filter_q) |
| 1330 | if not session["allow_show_user_project_role"]: |
| 1331 | # Bug 853 - Default filtering |
| 1332 | user = self.auth.get_user(session["username"]) |
| 1333 | roles = [prm["role"] for prm in user["project_role_mappings"]] |
| 1334 | role_list = [role for role in role_list if role["_id"] in roles] |
| 1335 | return role_list |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1336 | |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 1337 | def new(self, rollback, session, indata=None, kwargs=None, headers=None): |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1338 | """ |
| 1339 | Creates a new entry into database. |
| 1340 | |
| 1341 | :param rollback: list to append created items at database in case a rollback may to be done |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 1342 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1343 | :param indata: data to be inserted |
| 1344 | :param kwargs: used to override the indata descriptor |
| 1345 | :param headers: http request headers |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 1346 | :return: _id: identity of the inserted data, operation _id (None) |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1347 | """ |
| 1348 | try: |
| tierno | 1f029d8 | 2019-06-13 22:37:04 +0000 | [diff] [blame] | 1349 | content = self._remove_envelop(indata) |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1350 | |
| 1351 | # Override descriptor with query string kwargs |
| tierno | 1f029d8 | 2019-06-13 22:37:04 +0000 | [diff] [blame] | 1352 | self._update_input_with_kwargs(content, kwargs) |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 1353 | content = self._validate_input_new(content, session["force"]) |
| 1354 | self.check_conflict_on_new(session, content) |
| 1355 | self.format_on_new(content, project_id=session["project_id"], make_public=session["public"]) |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 1356 | # role_name = content["name"] |
| 1357 | rid = self.auth.create_role(content) |
| 1358 | content["_id"] = rid |
| 1359 | # _id = self.db.create(self.topic, content) |
| 1360 | rollback.append({"topic": self.topic, "_id": rid}) |
| tierno | bee3bad | 2019-12-05 12:26:01 +0000 | [diff] [blame] | 1361 | # self._send_msg("created", content, not_send_msg=not_send_msg) |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 1362 | return rid, None |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1363 | except ValidationError as e: |
| 1364 | raise EngineException(e, HTTPStatus.UNPROCESSABLE_ENTITY) |
| 1365 | |
| tierno | bee3bad | 2019-12-05 12:26:01 +0000 | [diff] [blame] | 1366 | def delete(self, session, _id, dry_run=False, not_send_msg=None): |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1367 | """ |
| 1368 | Delete item by its internal _id |
| 1369 | |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 1370 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1371 | :param _id: server internal id |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1372 | :param dry_run: make checking but do not delete |
| tierno | bee3bad | 2019-12-05 12:26:01 +0000 | [diff] [blame] | 1373 | :param not_send_msg: To not send message (False) or store content (list) instead |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1374 | :return: dictionary with deleted item _id. It raises EngineException on error: not found, conflict, ... |
| 1375 | """ |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 1376 | filter_q = {BaseTopic.id_field(self.topic, _id): _id} |
| 1377 | roles = self.auth.get_role_list(filter_q) |
| 1378 | if not roles: |
| 1379 | raise AuthconnNotFoundException("Not found any role with filter {}".format(filter_q)) |
| 1380 | elif len(roles) > 1: |
| 1381 | raise AuthconnConflictException("Found more than one role with filter {}".format(filter_q)) |
| 1382 | rid = roles[0]["_id"] |
| 1383 | self.check_conflict_on_del(session, rid, None) |
| delacruzramo | ceb8baf | 2019-06-21 14:25:38 +0200 | [diff] [blame] | 1384 | # filter_q = {"_id": _id} |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 1385 | # filter_q = {BaseTopic.id_field(self.topic, _id): _id} # To allow role addressing by name |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1386 | if not dry_run: |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 1387 | v = self.auth.delete_role(rid) |
| 1388 | # v = self.db.del_one(self.topic, filter_q) |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1389 | return v |
| 1390 | return None |
| 1391 | |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 1392 | def edit(self, session, _id, indata=None, kwargs=None, content=None): |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1393 | """ |
| 1394 | Updates a role entry. |
| 1395 | |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 1396 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1397 | :param _id: |
| 1398 | :param indata: data to be inserted |
| 1399 | :param kwargs: used to override the indata descriptor |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1400 | :param content: |
| 1401 | :return: _id: identity of the inserted data. |
| 1402 | """ |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 1403 | if kwargs: |
| 1404 | self._update_input_with_kwargs(indata, kwargs) |
| 1405 | try: |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 1406 | if not content: |
| 1407 | content = self.show(session, _id) |
| Frank Bryden | deba68e | 2020-07-27 13:55:11 +0000 | [diff] [blame] | 1408 | indata = self._validate_input_edit(indata, content, force=session["force"]) |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 1409 | deep_update_rfc7396(content, indata) |
| bravof | b995ea2 | 2021-02-10 10:57:52 -0300 | [diff] [blame] | 1410 | content = self.check_conflict_on_edit(session, content, indata, _id=_id) |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 1411 | self.format_on_edit(content, indata) |
| 1412 | self.auth.update_role(content) |
| 1413 | except ValidationError as e: |
| 1414 | raise EngineException(e, HTTPStatus.UNPROCESSABLE_ENTITY) |