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