| 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): |
| Patricia Reinoso | c85d3af | 2023-03-23 09:27:08 +0000 | [diff] [blame] | 559 | if not self._is_paas_vim_type(indata): |
| 560 | return |
| 561 | if not self._is_valid_paas_config(indata.get("config")): |
| 562 | raise EngineException( |
| 563 | "Invalid config for VIM account '{}'.".format(indata["name"]), |
| 564 | HTTPStatus.UNPROCESSABLE_ENTITY, |
| 565 | ) |
| Patricia Reinoso | a04d59b | 2023-02-02 15:00:40 +0000 | [diff] [blame] | 566 | |
| Patricia Reinoso | c85d3af | 2023-03-23 09:27:08 +0000 | [diff] [blame] | 567 | def _is_valid_paas_config(self, config) -> bool: |
| 568 | if not config: |
| 569 | return False |
| 570 | paas_provider = config.get("paas_provider") |
| 571 | is_valid_paas_provider = paas_provider in self.valid_paas_providers |
| 572 | if paas_provider == "juju": |
| 573 | return self._is_valid_juju_paas_config(config) |
| 574 | return is_valid_paas_provider |
| 575 | |
| 576 | def _is_valid_juju_paas_config(self, config) -> bool: |
| 577 | if not config: |
| 578 | return False |
| 579 | config_keys = [ |
| 580 | "paas_provider", |
| 581 | "ca_cert_content", |
| 582 | "cloud", |
| 583 | "cloud_credentials", |
| 584 | "authorized_keys", |
| 585 | ] |
| 586 | return all(key in config for key in config_keys) |
| tierno | bdebce9 | 2019-07-01 15:36:49 +0000 | [diff] [blame] | 587 | |
| delacruzramo | 35c998b | 2019-11-21 11:09:16 +0100 | [diff] [blame] | 588 | def check_conflict_on_del(self, session, _id, db_content): |
| 589 | """ |
| 590 | Check if deletion can be done because of dependencies if it is not force. To override |
| 591 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| 592 | :param _id: internal _id |
| 593 | :param db_content: The database content of this item _id |
| 594 | :return: None if ok or raises EngineException with the conflict |
| 595 | """ |
| 596 | if session["force"]: |
| 597 | return |
| 598 | # check if used by VNF |
| 599 | if self.db.get_list("vnfrs", {"vim-account-id": _id}): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 600 | raise EngineException( |
| 601 | "There is at least one VNF using this VIM account", |
| 602 | http_code=HTTPStatus.CONFLICT, |
| 603 | ) |
| delacruzramo | 35c998b | 2019-11-21 11:09:16 +0100 | [diff] [blame] | 604 | super().check_conflict_on_del(session, _id, db_content) |
| 605 | |
| Patricia Reinoso | a04d59b | 2023-02-02 15:00:40 +0000 | [diff] [blame] | 606 | def _send_msg(self, action, content, not_send_msg=None): |
| 607 | if self._is_paas_vim_type(content): |
| Patricia Reinoso | 62fa673 | 2023-02-22 17:57:53 +0000 | [diff] [blame] | 608 | self.temporal.start_vim_workflow(action, content) |
| Patricia Reinoso | a04d59b | 2023-02-02 15:00:40 +0000 | [diff] [blame] | 609 | return |
| Patricia Reinoso | 62fa673 | 2023-02-22 17:57:53 +0000 | [diff] [blame] | 610 | |
| Patricia Reinoso | a04d59b | 2023-02-02 15:00:40 +0000 | [diff] [blame] | 611 | super()._send_msg(action, content, not_send_msg) |
| 612 | |
| tierno | bdebce9 | 2019-07-01 15:36:49 +0000 | [diff] [blame] | 613 | |
| 614 | class WimAccountTopic(CommonVimWimSdn): |
| tierno | 55ba2e6 | 2018-12-11 17:22:22 +0000 | [diff] [blame] | 615 | topic = "wim_accounts" |
| 616 | topic_msg = "wim_account" |
| 617 | schema_new = wim_account_new_schema |
| 618 | schema_edit = wim_account_edit_schema |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 619 | multiproject = True |
| gifrerenom | 44f5ec1 | 2022-03-07 16:57:25 +0000 | [diff] [blame] | 620 | password_to_encrypt = "password" |
| tierno | 468aa24 | 2019-08-01 16:35:04 +0000 | [diff] [blame] | 621 | config_to_encrypt = {} |
| tierno | 55ba2e6 | 2018-12-11 17:22:22 +0000 | [diff] [blame] | 622 | |
| 623 | |
| tierno | bdebce9 | 2019-07-01 15:36:49 +0000 | [diff] [blame] | 624 | class SdnTopic(CommonVimWimSdn): |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 625 | topic = "sdns" |
| 626 | topic_msg = "sdn" |
| tierno | 6b02b05 | 2020-06-02 10:07:41 +0000 | [diff] [blame] | 627 | quota_name = "sdn_controllers" |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 628 | schema_new = sdn_new_schema |
| 629 | schema_edit = sdn_edit_schema |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 630 | multiproject = True |
| tierno | bdebce9 | 2019-07-01 15:36:49 +0000 | [diff] [blame] | 631 | password_to_encrypt = "password" |
| tierno | 468aa24 | 2019-08-01 16:35:04 +0000 | [diff] [blame] | 632 | config_to_encrypt = {} |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 633 | |
| tierno | 7adaeb0 | 2019-12-17 16:46:12 +0000 | [diff] [blame] | 634 | def _obtain_url(self, input, create): |
| 635 | if input.get("ip") or input.get("port"): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 636 | if not input.get("ip") or not input.get("port") or input.get("url"): |
| 637 | raise ValidationError( |
| 638 | "You must provide both 'ip' and 'port' (deprecated); or just 'url' (prefered)" |
| 639 | ) |
| 640 | input["url"] = "http://{}:{}/".format(input["ip"], input["port"]) |
| tierno | 7adaeb0 | 2019-12-17 16:46:12 +0000 | [diff] [blame] | 641 | del input["ip"] |
| 642 | del input["port"] |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 643 | elif create and not input.get("url"): |
| tierno | 7adaeb0 | 2019-12-17 16:46:12 +0000 | [diff] [blame] | 644 | raise ValidationError("You must provide 'url'") |
| 645 | return input |
| 646 | |
| 647 | def _validate_input_new(self, input, force=False): |
| 648 | input = super()._validate_input_new(input, force) |
| 649 | return self._obtain_url(input, True) |
| 650 | |
| Frank Bryden | deba68e | 2020-07-27 13:55:11 +0000 | [diff] [blame] | 651 | def _validate_input_edit(self, input, content, force=False): |
| 652 | input = super()._validate_input_edit(input, content, force) |
| tierno | 7adaeb0 | 2019-12-17 16:46:12 +0000 | [diff] [blame] | 653 | return self._obtain_url(input, False) |
| 654 | |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 655 | |
| delacruzramo | fe598fe | 2019-10-23 18:25:11 +0200 | [diff] [blame] | 656 | class K8sClusterTopic(CommonVimWimSdn): |
| 657 | topic = "k8sclusters" |
| 658 | topic_msg = "k8scluster" |
| 659 | schema_new = k8scluster_new_schema |
| 660 | schema_edit = k8scluster_edit_schema |
| 661 | multiproject = True |
| 662 | password_to_encrypt = None |
| 663 | config_to_encrypt = {} |
| 664 | |
| 665 | def format_on_new(self, content, project_id=None, make_public=False): |
| 666 | oid = super().format_on_new(content, project_id, make_public) |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 667 | self.db.encrypt_decrypt_fields( |
| 668 | content["credentials"], |
| 669 | "encrypt", |
| 670 | ["password", "secret"], |
| 671 | schema_version=content["schema_version"], |
| 672 | salt=content["_id"], |
| 673 | ) |
| delacruzramo | c2d5fc6 | 2020-02-05 11:50:21 +0000 | [diff] [blame] | 674 | # Add Helm/Juju Repo lists |
| 675 | repos = {"helm-chart": [], "juju-bundle": []} |
| 676 | for proj in content["_admin"]["projects_read"]: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 677 | if proj != "ANY": |
| 678 | for repo in self.db.get_list( |
| 679 | "k8srepos", {"_admin.projects_read": proj} |
| 680 | ): |
| delacruzramo | c2d5fc6 | 2020-02-05 11:50:21 +0000 | [diff] [blame] | 681 | if repo["_id"] not in repos[repo["type"]]: |
| 682 | repos[repo["type"]].append(repo["_id"]) |
| 683 | for k in repos: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 684 | content["_admin"][k.replace("-", "_") + "_repos"] = repos[k] |
| delacruzramo | fe598fe | 2019-10-23 18:25:11 +0200 | [diff] [blame] | 685 | return oid |
| 686 | |
| 687 | def format_on_edit(self, final_content, edit_content): |
| 688 | if final_content.get("schema_version") and edit_content.get("credentials"): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 689 | self.db.encrypt_decrypt_fields( |
| 690 | edit_content["credentials"], |
| 691 | "encrypt", |
| 692 | ["password", "secret"], |
| 693 | schema_version=final_content["schema_version"], |
| 694 | salt=final_content["_id"], |
| 695 | ) |
| 696 | deep_update_rfc7396( |
| 697 | final_content["credentials"], edit_content["credentials"] |
| 698 | ) |
| delacruzramo | fe598fe | 2019-10-23 18:25:11 +0200 | [diff] [blame] | 699 | oid = super().format_on_edit(final_content, edit_content) |
| 700 | return oid |
| 701 | |
| delacruzramo | c2d5fc6 | 2020-02-05 11:50:21 +0000 | [diff] [blame] | 702 | def check_conflict_on_edit(self, session, final_content, edit_content, _id): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 703 | final_content = super(CommonVimWimSdn, self).check_conflict_on_edit( |
| 704 | session, final_content, edit_content, _id |
| 705 | ) |
| 706 | final_content = super().check_conflict_on_edit( |
| 707 | session, final_content, edit_content, _id |
| 708 | ) |
| delacruzramo | c2d5fc6 | 2020-02-05 11:50:21 +0000 | [diff] [blame] | 709 | # Update Helm/Juju Repo lists |
| 710 | repos = {"helm-chart": [], "juju-bundle": []} |
| 711 | for proj in session.get("set_project", []): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 712 | if proj != "ANY": |
| 713 | for repo in self.db.get_list( |
| 714 | "k8srepos", {"_admin.projects_read": proj} |
| 715 | ): |
| delacruzramo | c2d5fc6 | 2020-02-05 11:50:21 +0000 | [diff] [blame] | 716 | if repo["_id"] not in repos[repo["type"]]: |
| 717 | repos[repo["type"]].append(repo["_id"]) |
| 718 | for k in repos: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 719 | rlist = k.replace("-", "_") + "_repos" |
| delacruzramo | c2d5fc6 | 2020-02-05 11:50:21 +0000 | [diff] [blame] | 720 | if rlist not in final_content["_admin"]: |
| 721 | final_content["_admin"][rlist] = [] |
| 722 | final_content["_admin"][rlist] += repos[k] |
| bravof | b995ea2 | 2021-02-10 10:57:52 -0300 | [diff] [blame] | 723 | return final_content |
| delacruzramo | c2d5fc6 | 2020-02-05 11:50:21 +0000 | [diff] [blame] | 724 | |
| tierno | e19707b | 2020-04-21 13:08:04 +0000 | [diff] [blame] | 725 | def check_conflict_on_del(self, session, _id, db_content): |
| 726 | """ |
| 727 | Check if deletion can be done because of dependencies if it is not force. To override |
| 728 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| 729 | :param _id: internal _id |
| 730 | :param db_content: The database content of this item _id |
| 731 | :return: None if ok or raises EngineException with the conflict |
| 732 | """ |
| 733 | if session["force"]: |
| 734 | return |
| 735 | # check if used by VNF |
| 736 | filter_q = {"kdur.k8s-cluster.id": _id} |
| 737 | if session["project_id"]: |
| 738 | filter_q["_admin.projects_read.cont"] = session["project_id"] |
| 739 | if self.db.get_list("vnfrs", filter_q): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 740 | raise EngineException( |
| 741 | "There is at least one VNF using this k8scluster", |
| 742 | http_code=HTTPStatus.CONFLICT, |
| 743 | ) |
| tierno | e19707b | 2020-04-21 13:08:04 +0000 | [diff] [blame] | 744 | super().check_conflict_on_del(session, _id, db_content) |
| 745 | |
| delacruzramo | fe598fe | 2019-10-23 18:25:11 +0200 | [diff] [blame] | 746 | |
| David Garcia | ecb4132 | 2021-03-31 19:10:46 +0200 | [diff] [blame] | 747 | class VcaTopic(CommonVimWimSdn): |
| 748 | topic = "vca" |
| 749 | topic_msg = "vca" |
| 750 | schema_new = vca_new_schema |
| 751 | schema_edit = vca_edit_schema |
| 752 | multiproject = True |
| 753 | password_to_encrypt = None |
| 754 | |
| 755 | def format_on_new(self, content, project_id=None, make_public=False): |
| 756 | oid = super().format_on_new(content, project_id, make_public) |
| 757 | content["schema_version"] = schema_version = "1.11" |
| 758 | for key in ["secret", "cacert"]: |
| 759 | content[key] = self.db.encrypt( |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 760 | content[key], schema_version=schema_version, salt=content["_id"] |
| David Garcia | ecb4132 | 2021-03-31 19:10:46 +0200 | [diff] [blame] | 761 | ) |
| 762 | return oid |
| 763 | |
| 764 | def format_on_edit(self, final_content, edit_content): |
| 765 | oid = super().format_on_edit(final_content, edit_content) |
| 766 | schema_version = final_content.get("schema_version") |
| 767 | for key in ["secret", "cacert"]: |
| 768 | if key in edit_content: |
| 769 | final_content[key] = self.db.encrypt( |
| 770 | edit_content[key], |
| 771 | schema_version=schema_version, |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 772 | salt=final_content["_id"], |
| David Garcia | ecb4132 | 2021-03-31 19:10:46 +0200 | [diff] [blame] | 773 | ) |
| 774 | return oid |
| 775 | |
| 776 | def check_conflict_on_del(self, session, _id, db_content): |
| 777 | """ |
| 778 | Check if deletion can be done because of dependencies if it is not force. To override |
| 779 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| 780 | :param _id: internal _id |
| 781 | :param db_content: The database content of this item _id |
| 782 | :return: None if ok or raises EngineException with the conflict |
| 783 | """ |
| 784 | if session["force"]: |
| 785 | return |
| 786 | # check if used by VNF |
| 787 | filter_q = {"vca": _id} |
| 788 | if session["project_id"]: |
| 789 | filter_q["_admin.projects_read.cont"] = session["project_id"] |
| 790 | if self.db.get_list("vim_accounts", filter_q): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 791 | raise EngineException( |
| 792 | "There is at least one VIM account using this vca", |
| 793 | http_code=HTTPStatus.CONFLICT, |
| 794 | ) |
| David Garcia | ecb4132 | 2021-03-31 19:10:46 +0200 | [diff] [blame] | 795 | super().check_conflict_on_del(session, _id, db_content) |
| 796 | |
| 797 | |
| delacruzramo | fe598fe | 2019-10-23 18:25:11 +0200 | [diff] [blame] | 798 | class K8sRepoTopic(CommonVimWimSdn): |
| 799 | topic = "k8srepos" |
| 800 | topic_msg = "k8srepo" |
| 801 | schema_new = k8srepo_new_schema |
| 802 | schema_edit = k8srepo_edit_schema |
| 803 | multiproject = True |
| 804 | password_to_encrypt = None |
| 805 | config_to_encrypt = {} |
| 806 | |
| delacruzramo | c2d5fc6 | 2020-02-05 11:50:21 +0000 | [diff] [blame] | 807 | def format_on_new(self, content, project_id=None, make_public=False): |
| 808 | oid = super().format_on_new(content, project_id, make_public) |
| 809 | # Update Helm/Juju Repo lists |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 810 | repo_list = content["type"].replace("-", "_") + "_repos" |
| delacruzramo | c2d5fc6 | 2020-02-05 11:50:21 +0000 | [diff] [blame] | 811 | for proj in content["_admin"]["projects_read"]: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 812 | if proj != "ANY": |
| 813 | self.db.set_list( |
| 814 | "k8sclusters", |
| 815 | { |
| 816 | "_admin.projects_read": proj, |
| 817 | "_admin." + repo_list + ".ne": content["_id"], |
| 818 | }, |
| 819 | {}, |
| 820 | push={"_admin." + repo_list: content["_id"]}, |
| 821 | ) |
| delacruzramo | c2d5fc6 | 2020-02-05 11:50:21 +0000 | [diff] [blame] | 822 | return oid |
| 823 | |
| 824 | def delete(self, session, _id, dry_run=False, not_send_msg=None): |
| 825 | type = self.db.get_one("k8srepos", {"_id": _id})["type"] |
| 826 | oid = super().delete(session, _id, dry_run, not_send_msg) |
| 827 | if oid: |
| 828 | # Remove from Helm/Juju Repo lists |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 829 | repo_list = type.replace("-", "_") + "_repos" |
| 830 | self.db.set_list( |
| 831 | "k8sclusters", |
| 832 | {"_admin." + repo_list: _id}, |
| 833 | {}, |
| 834 | pull={"_admin." + repo_list: _id}, |
| 835 | ) |
| delacruzramo | c2d5fc6 | 2020-02-05 11:50:21 +0000 | [diff] [blame] | 836 | return oid |
| 837 | |
| delacruzramo | fe598fe | 2019-10-23 18:25:11 +0200 | [diff] [blame] | 838 | |
| Felipe Vicens | b66b041 | 2020-05-06 10:11:00 +0200 | [diff] [blame] | 839 | class OsmRepoTopic(BaseTopic): |
| 840 | topic = "osmrepos" |
| 841 | topic_msg = "osmrepos" |
| 842 | schema_new = osmrepo_new_schema |
| 843 | schema_edit = osmrepo_edit_schema |
| 844 | multiproject = True |
| 845 | # TODO: Implement user/password |
| 846 | |
| 847 | |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 848 | class UserTopicAuth(UserTopic): |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 849 | # topic = "users" |
| agarwalat | 5347198 | 2020-10-08 13:06:14 +0000 | [diff] [blame] | 850 | topic_msg = "users" |
| Eduardo Sousa | a16a4fa | 2019-05-23 01:41:18 +0100 | [diff] [blame] | 851 | schema_new = user_new_schema |
| 852 | schema_edit = user_edit_schema |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 853 | |
| 854 | def __init__(self, db, fs, msg, auth): |
| delacruzramo | 32bab47 | 2019-09-13 12:24:22 +0200 | [diff] [blame] | 855 | UserTopic.__init__(self, db, fs, msg, auth) |
| 856 | # self.auth = auth |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 857 | |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 858 | def check_conflict_on_new(self, session, indata): |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 859 | """ |
| 860 | Check that the data to be inserted is valid |
| 861 | |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 862 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 863 | :param indata: data to be inserted |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 864 | :return: None or raises EngineException |
| 865 | """ |
| 866 | username = indata.get("username") |
| tierno | cf042d3 | 2019-06-13 09:06:40 +0000 | [diff] [blame] | 867 | if is_valid_uuid(username): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 868 | raise EngineException( |
| 869 | "username '{}' cannot have a uuid format".format(username), |
| 870 | HTTPStatus.UNPROCESSABLE_ENTITY, |
| 871 | ) |
| tierno | cf042d3 | 2019-06-13 09:06:40 +0000 | [diff] [blame] | 872 | |
| 873 | # Check that username is not used, regardless keystone already checks this |
| 874 | if self.auth.get_user_list(filter_q={"name": username}): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 875 | raise EngineException( |
| 876 | "username '{}' is already used".format(username), HTTPStatus.CONFLICT |
| 877 | ) |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 878 | |
| Eduardo Sousa | 339ed78 | 2019-05-28 14:25:00 +0100 | [diff] [blame] | 879 | if "projects" in indata.keys(): |
| tierno | 701018c | 2019-06-25 11:13:14 +0000 | [diff] [blame] | 880 | # convert to new format project_role_mappings |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 881 | role = self.auth.get_role_list({"name": "project_admin"}) |
| 882 | if not role: |
| 883 | role = self.auth.get_role_list() |
| 884 | if not role: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 885 | raise AuthconnNotFoundException( |
| 886 | "Can't find default role for user '{}'".format(username) |
| 887 | ) |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 888 | rid = role[0]["_id"] |
| tierno | 701018c | 2019-06-25 11:13:14 +0000 | [diff] [blame] | 889 | if not indata.get("project_role_mappings"): |
| 890 | indata["project_role_mappings"] = [] |
| 891 | for project in indata["projects"]: |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 892 | pid = self.auth.get_project(project)["_id"] |
| 893 | prm = {"project": pid, "role": rid} |
| 894 | if prm not in indata["project_role_mappings"]: |
| 895 | indata["project_role_mappings"].append(prm) |
| tierno | 701018c | 2019-06-25 11:13:14 +0000 | [diff] [blame] | 896 | # raise EngineException("Format invalid: the keyword 'projects' is not allowed for keystone authentication", |
| 897 | # HTTPStatus.BAD_REQUEST) |
| Eduardo Sousa | 339ed78 | 2019-05-28 14:25:00 +0100 | [diff] [blame] | 898 | |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 899 | def check_conflict_on_edit(self, session, final_content, edit_content, _id): |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 900 | """ |
| 901 | Check that the data to be edited/uploaded is valid |
| 902 | |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 903 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 904 | :param final_content: data once modified |
| 905 | :param edit_content: incremental data that contains the modifications to apply |
| 906 | :param _id: internal _id |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 907 | :return: None or raises EngineException |
| 908 | """ |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 909 | |
| tierno | cf042d3 | 2019-06-13 09:06:40 +0000 | [diff] [blame] | 910 | if "username" in edit_content: |
| 911 | username = edit_content.get("username") |
| 912 | if is_valid_uuid(username): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 913 | raise EngineException( |
| 914 | "username '{}' cannot have an uuid format".format(username), |
| 915 | HTTPStatus.UNPROCESSABLE_ENTITY, |
| 916 | ) |
| tierno | cf042d3 | 2019-06-13 09:06:40 +0000 | [diff] [blame] | 917 | |
| 918 | # Check that username is not used, regardless keystone already checks this |
| 919 | if self.auth.get_user_list(filter_q={"name": username}): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 920 | raise EngineException( |
| 921 | "username '{}' is already used".format(username), |
| 922 | HTTPStatus.CONFLICT, |
| 923 | ) |
| tierno | cf042d3 | 2019-06-13 09:06:40 +0000 | [diff] [blame] | 924 | |
| 925 | if final_content["username"] == "admin": |
| 926 | for mapping in edit_content.get("remove_project_role_mappings", ()): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 927 | if mapping["project"] == "admin" and mapping.get("role") in ( |
| 928 | None, |
| 929 | "system_admin", |
| 930 | ): |
| tierno | cf042d3 | 2019-06-13 09:06:40 +0000 | [diff] [blame] | 931 | # TODO make this also available for project id and role id |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 932 | raise EngineException( |
| 933 | "You cannot remove system_admin role from admin user", |
| 934 | http_code=HTTPStatus.FORBIDDEN, |
| 935 | ) |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 936 | |
| bravof | b995ea2 | 2021-02-10 10:57:52 -0300 | [diff] [blame] | 937 | return final_content |
| 938 | |
| tierno | b4844ab | 2019-05-23 08:42:12 +0000 | [diff] [blame] | 939 | def check_conflict_on_del(self, session, _id, db_content): |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 940 | """ |
| 941 | 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] | 942 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 943 | :param _id: internal _id |
| tierno | b4844ab | 2019-05-23 08:42:12 +0000 | [diff] [blame] | 944 | :param db_content: The database content of this item _id |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 945 | :return: None if ok or raises EngineException with the conflict |
| 946 | """ |
| tierno | cf042d3 | 2019-06-13 09:06:40 +0000 | [diff] [blame] | 947 | if db_content["username"] == session["username"]: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 948 | raise EngineException( |
| 949 | "You cannot delete your own login user ", http_code=HTTPStatus.CONFLICT |
| 950 | ) |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 951 | # 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] | 952 | |
| Eduardo Sousa | a16a4fa | 2019-05-23 01:41:18 +0100 | [diff] [blame] | 953 | @staticmethod |
| 954 | def format_on_show(content): |
| 955 | """ |
| Eduardo Sousa | 4460390 | 2019-06-04 08:10:32 +0100 | [diff] [blame] | 956 | Modifies the content of the role information to separate the role |
| Eduardo Sousa | a16a4fa | 2019-05-23 01:41:18 +0100 | [diff] [blame] | 957 | metadata from the role definition. |
| 958 | """ |
| 959 | project_role_mappings = [] |
| 960 | |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 961 | if "projects" in content: |
| 962 | for project in content["projects"]: |
| 963 | for role in project["roles"]: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 964 | project_role_mappings.append( |
| 965 | { |
| 966 | "project": project["_id"], |
| 967 | "project_name": project["name"], |
| 968 | "role": role["_id"], |
| 969 | "role_name": role["name"], |
| 970 | } |
| 971 | ) |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 972 | del content["projects"] |
| Eduardo Sousa | a16a4fa | 2019-05-23 01:41:18 +0100 | [diff] [blame] | 973 | content["project_role_mappings"] = project_role_mappings |
| 974 | |
| Eduardo Sousa | 0b1d61b | 2019-05-30 19:55:52 +0100 | [diff] [blame] | 975 | return content |
| 976 | |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 977 | def new(self, rollback, session, indata=None, kwargs=None, headers=None): |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 978 | """ |
| 979 | Creates a new entry into the authentication backend. |
| 980 | |
| 981 | NOTE: Overrides BaseTopic functionality because it doesn't require access to database. |
| 982 | |
| 983 | :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] | 984 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 985 | :param indata: data to be inserted |
| 986 | :param kwargs: used to override the indata descriptor |
| 987 | :param headers: http request headers |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 988 | :return: _id: identity of the inserted data, operation _id (None) |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 989 | """ |
| 990 | try: |
| 991 | content = BaseTopic._remove_envelop(indata) |
| 992 | |
| 993 | # Override descriptor with query string kwargs |
| 994 | BaseTopic._update_input_with_kwargs(content, kwargs) |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 995 | content = self._validate_input_new(content, session["force"]) |
| 996 | self.check_conflict_on_new(session, content) |
| tierno | cf042d3 | 2019-06-13 09:06:40 +0000 | [diff] [blame] | 997 | # self.format_on_new(content, session["project_id"], make_public=session["public"]) |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 998 | now = time() |
| 999 | content["_admin"] = {"created": now, "modified": now} |
| 1000 | prms = [] |
| 1001 | for prm in content.get("project_role_mappings", []): |
| 1002 | proj = self.auth.get_project(prm["project"], not session["force"]) |
| 1003 | role = self.auth.get_role(prm["role"], not session["force"]) |
| 1004 | pid = proj["_id"] if proj else None |
| 1005 | rid = role["_id"] if role else None |
| 1006 | prl = {"project": pid, "role": rid} |
| 1007 | if prl not in prms: |
| 1008 | prms.append(prl) |
| 1009 | content["project_role_mappings"] = prms |
| 1010 | # _id = self.auth.create_user(content["username"], content["password"])["_id"] |
| 1011 | _id = self.auth.create_user(content)["_id"] |
| Eduardo Sousa | 4460390 | 2019-06-04 08:10:32 +0100 | [diff] [blame] | 1012 | |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1013 | rollback.append({"topic": self.topic, "_id": _id}) |
| tierno | cf042d3 | 2019-06-13 09:06:40 +0000 | [diff] [blame] | 1014 | # del content["password"] |
| agarwalat | 5347198 | 2020-10-08 13:06:14 +0000 | [diff] [blame] | 1015 | self._send_msg("created", content, not_send_msg=None) |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 1016 | return _id, None |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1017 | except ValidationError as e: |
| 1018 | raise EngineException(e, HTTPStatus.UNPROCESSABLE_ENTITY) |
| 1019 | |
| K Sai Kiran | 5758955 | 2021-01-27 21:38:34 +0530 | [diff] [blame] | 1020 | def show(self, session, _id, filter_q=None, api_req=False): |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1021 | """ |
| 1022 | Get complete information on an topic |
| 1023 | |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 1024 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| tierno | 5ec768a | 2020-03-31 09:46:44 +0000 | [diff] [blame] | 1025 | :param _id: server internal id or username |
| K Sai Kiran | 5758955 | 2021-01-27 21:38:34 +0530 | [diff] [blame] | 1026 | :param filter_q: dict: query parameter |
| K Sai Kiran | d010e3e | 2020-08-28 15:11:48 +0530 | [diff] [blame] | 1027 | :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] | 1028 | :return: dictionary, raise exception if not found. |
| 1029 | """ |
| tierno | cf042d3 | 2019-06-13 09:06:40 +0000 | [diff] [blame] | 1030 | # Allow _id to be a name or uuid |
| tierno | ad6d533 | 2020-02-19 14:29:49 +0000 | [diff] [blame] | 1031 | filter_q = {"username": _id} |
| delacruzramo | 029405d | 2019-09-26 10:52:56 +0200 | [diff] [blame] | 1032 | # users = self.auth.get_user_list(filter_q) |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1033 | users = self.list(session, filter_q) # To allow default filtering (Bug 853) |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1034 | if len(users) == 1: |
| tierno | 1546f2a | 2019-08-20 15:38:11 +0000 | [diff] [blame] | 1035 | return users[0] |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1036 | elif len(users) > 1: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1037 | raise EngineException( |
| 1038 | "Too many users found for '{}'".format(_id), HTTPStatus.CONFLICT |
| 1039 | ) |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1040 | else: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1041 | raise EngineException( |
| 1042 | "User '{}' not found".format(_id), HTTPStatus.NOT_FOUND |
| 1043 | ) |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1044 | |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 1045 | def edit(self, session, _id, indata=None, kwargs=None, content=None): |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1046 | """ |
| 1047 | Updates an user entry. |
| 1048 | |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 1049 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1050 | :param _id: |
| 1051 | :param indata: data to be inserted |
| 1052 | :param kwargs: used to override the indata descriptor |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1053 | :param content: |
| 1054 | :return: _id: identity of the inserted data. |
| 1055 | """ |
| 1056 | indata = self._remove_envelop(indata) |
| 1057 | |
| 1058 | # Override descriptor with query string kwargs |
| 1059 | if kwargs: |
| 1060 | BaseTopic._update_input_with_kwargs(indata, kwargs) |
| 1061 | try: |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1062 | if not content: |
| 1063 | content = self.show(session, _id) |
| Frank Bryden | deba68e | 2020-07-27 13:55:11 +0000 | [diff] [blame] | 1064 | indata = self._validate_input_edit(indata, content, force=session["force"]) |
| bravof | b995ea2 | 2021-02-10 10:57:52 -0300 | [diff] [blame] | 1065 | content = self.check_conflict_on_edit(session, content, indata, _id=_id) |
| tierno | cf042d3 | 2019-06-13 09:06:40 +0000 | [diff] [blame] | 1066 | # self.format_on_edit(content, indata) |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1067 | |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1068 | if not ( |
| 1069 | "password" in indata |
| 1070 | or "username" in indata |
| 1071 | or indata.get("remove_project_role_mappings") |
| 1072 | or indata.get("add_project_role_mappings") |
| 1073 | or indata.get("project_role_mappings") |
| 1074 | or indata.get("projects") |
| 1075 | or indata.get("add_projects") |
| 1076 | ): |
| tierno | cf042d3 | 2019-06-13 09:06:40 +0000 | [diff] [blame] | 1077 | return _id |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1078 | if indata.get("project_role_mappings") and ( |
| 1079 | indata.get("remove_project_role_mappings") |
| 1080 | or indata.get("add_project_role_mappings") |
| 1081 | ): |
| 1082 | raise EngineException( |
| 1083 | "Option 'project_role_mappings' is incompatible with 'add_project_role_mappings" |
| 1084 | "' or 'remove_project_role_mappings'", |
| 1085 | http_code=HTTPStatus.BAD_REQUEST, |
| 1086 | ) |
| Eduardo Sousa | 4460390 | 2019-06-04 08:10:32 +0100 | [diff] [blame] | 1087 | |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 1088 | if indata.get("projects") or indata.get("add_projects"): |
| 1089 | role = self.auth.get_role_list({"name": "project_admin"}) |
| 1090 | if not role: |
| 1091 | role = self.auth.get_role_list() |
| 1092 | if not role: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1093 | raise AuthconnNotFoundException( |
| 1094 | "Can't find a default role for user '{}'".format( |
| 1095 | content["username"] |
| 1096 | ) |
| 1097 | ) |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 1098 | rid = role[0]["_id"] |
| 1099 | if "add_project_role_mappings" not in indata: |
| 1100 | indata["add_project_role_mappings"] = [] |
| tierno | 1546f2a | 2019-08-20 15:38:11 +0000 | [diff] [blame] | 1101 | if "remove_project_role_mappings" not in indata: |
| 1102 | indata["remove_project_role_mappings"] = [] |
| 1103 | if isinstance(indata.get("projects"), dict): |
| 1104 | # backward compatible |
| 1105 | for k, v in indata["projects"].items(): |
| 1106 | if k.startswith("$") and v is None: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1107 | indata["remove_project_role_mappings"].append( |
| 1108 | {"project": k[1:]} |
| 1109 | ) |
| tierno | 1546f2a | 2019-08-20 15:38:11 +0000 | [diff] [blame] | 1110 | elif k.startswith("$+"): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1111 | indata["add_project_role_mappings"].append( |
| 1112 | {"project": v, "role": rid} |
| 1113 | ) |
| tierno | 1546f2a | 2019-08-20 15:38:11 +0000 | [diff] [blame] | 1114 | del indata["projects"] |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 1115 | for proj in indata.get("projects", []) + indata.get("add_projects", []): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1116 | indata["add_project_role_mappings"].append( |
| 1117 | {"project": proj, "role": rid} |
| 1118 | ) |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 1119 | |
| 1120 | # user = self.show(session, _id) # Already in 'content' |
| 1121 | original_mapping = content["project_role_mappings"] |
| Eduardo Sousa | 4460390 | 2019-06-04 08:10:32 +0100 | [diff] [blame] | 1122 | |
| tierno | cf042d3 | 2019-06-13 09:06:40 +0000 | [diff] [blame] | 1123 | mappings_to_add = [] |
| 1124 | mappings_to_remove = [] |
| Eduardo Sousa | 4460390 | 2019-06-04 08:10:32 +0100 | [diff] [blame] | 1125 | |
| tierno | cf042d3 | 2019-06-13 09:06:40 +0000 | [diff] [blame] | 1126 | # remove |
| 1127 | for to_remove in indata.get("remove_project_role_mappings", ()): |
| 1128 | for mapping in original_mapping: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1129 | if to_remove["project"] in ( |
| 1130 | mapping["project"], |
| 1131 | mapping["project_name"], |
| 1132 | ): |
| 1133 | if not to_remove.get("role") or to_remove["role"] in ( |
| 1134 | mapping["role"], |
| 1135 | mapping["role_name"], |
| 1136 | ): |
| tierno | cf042d3 | 2019-06-13 09:06:40 +0000 | [diff] [blame] | 1137 | mappings_to_remove.append(mapping) |
| Eduardo Sousa | 4460390 | 2019-06-04 08:10:32 +0100 | [diff] [blame] | 1138 | |
| tierno | cf042d3 | 2019-06-13 09:06:40 +0000 | [diff] [blame] | 1139 | # add |
| 1140 | for to_add in indata.get("add_project_role_mappings", ()): |
| 1141 | for mapping in original_mapping: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1142 | if to_add["project"] in ( |
| 1143 | mapping["project"], |
| 1144 | mapping["project_name"], |
| Mark Beierl | c528d88 | 2023-01-06 12:56:16 -0500 | [diff] [blame] | 1145 | ) and to_add["role"] in ( |
| 1146 | mapping["role"], |
| 1147 | mapping["role_name"], |
| 1148 | ): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1149 | if mapping in mappings_to_remove: # do not remove |
| tierno | cf042d3 | 2019-06-13 09:06:40 +0000 | [diff] [blame] | 1150 | mappings_to_remove.remove(mapping) |
| 1151 | break # do not add, it is already at user |
| 1152 | else: |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 1153 | pid = self.auth.get_project(to_add["project"])["_id"] |
| 1154 | rid = self.auth.get_role(to_add["role"])["_id"] |
| 1155 | mappings_to_add.append({"project": pid, "role": rid}) |
| tierno | cf042d3 | 2019-06-13 09:06:40 +0000 | [diff] [blame] | 1156 | |
| 1157 | # set |
| 1158 | if indata.get("project_role_mappings"): |
| 1159 | for to_set in indata["project_role_mappings"]: |
| 1160 | for mapping in original_mapping: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1161 | if to_set["project"] in ( |
| 1162 | mapping["project"], |
| 1163 | mapping["project_name"], |
| Mark Beierl | c528d88 | 2023-01-06 12:56:16 -0500 | [diff] [blame] | 1164 | ) and to_set["role"] in ( |
| 1165 | mapping["role"], |
| 1166 | mapping["role_name"], |
| 1167 | ): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1168 | if mapping in mappings_to_remove: # do not remove |
| tierno | cf042d3 | 2019-06-13 09:06:40 +0000 | [diff] [blame] | 1169 | mappings_to_remove.remove(mapping) |
| 1170 | break # do not add, it is already at user |
| 1171 | else: |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 1172 | pid = self.auth.get_project(to_set["project"])["_id"] |
| 1173 | rid = self.auth.get_role(to_set["role"])["_id"] |
| 1174 | mappings_to_add.append({"project": pid, "role": rid}) |
| tierno | cf042d3 | 2019-06-13 09:06:40 +0000 | [diff] [blame] | 1175 | for mapping in original_mapping: |
| 1176 | for to_set in indata["project_role_mappings"]: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1177 | if to_set["project"] in ( |
| 1178 | mapping["project"], |
| 1179 | mapping["project_name"], |
| Mark Beierl | c528d88 | 2023-01-06 12:56:16 -0500 | [diff] [blame] | 1180 | ) and to_set["role"] in ( |
| 1181 | mapping["role"], |
| 1182 | mapping["role_name"], |
| 1183 | ): |
| tierno | cf042d3 | 2019-06-13 09:06:40 +0000 | [diff] [blame] | 1184 | break |
| 1185 | else: |
| 1186 | # delete |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1187 | if mapping not in mappings_to_remove: # do not remove |
| tierno | cf042d3 | 2019-06-13 09:06:40 +0000 | [diff] [blame] | 1188 | mappings_to_remove.append(mapping) |
| 1189 | |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1190 | self.auth.update_user( |
| 1191 | { |
| 1192 | "_id": _id, |
| 1193 | "username": indata.get("username"), |
| 1194 | "password": indata.get("password"), |
| selvi.j | a9a1fc8 | 2022-04-04 06:54:30 +0000 | [diff] [blame] | 1195 | "old_password": indata.get("old_password"), |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1196 | "add_project_role_mappings": mappings_to_add, |
| 1197 | "remove_project_role_mappings": mappings_to_remove, |
| 1198 | } |
| 1199 | ) |
| 1200 | data_to_send = {"_id": _id, "changes": indata} |
| agarwalat | 5347198 | 2020-10-08 13:06:14 +0000 | [diff] [blame] | 1201 | self._send_msg("edited", data_to_send, not_send_msg=None) |
| tierno | cf042d3 | 2019-06-13 09:06:40 +0000 | [diff] [blame] | 1202 | |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 1203 | # return _id |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1204 | except ValidationError as e: |
| 1205 | raise EngineException(e, HTTPStatus.UNPROCESSABLE_ENTITY) |
| 1206 | |
| tierno | c4e07d0 | 2020-08-14 14:25:32 +0000 | [diff] [blame] | 1207 | def list(self, session, filter_q=None, api_req=False): |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1208 | """ |
| 1209 | Get a list of the topic that matches a filter |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 1210 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1211 | :param filter_q: filter of data to be applied |
| K Sai Kiran | d010e3e | 2020-08-28 15:11:48 +0530 | [diff] [blame] | 1212 | :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] | 1213 | :return: The list, it can be empty if no one match the filter. |
| 1214 | """ |
| delacruzramo | 029405d | 2019-09-26 10:52:56 +0200 | [diff] [blame] | 1215 | user_list = self.auth.get_user_list(filter_q) |
| 1216 | if not session["allow_show_user_project_role"]: |
| 1217 | # Bug 853 - Default filtering |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1218 | user_list = [ |
| 1219 | usr for usr in user_list if usr["username"] == session["username"] |
| 1220 | ] |
| delacruzramo | 029405d | 2019-09-26 10:52:56 +0200 | [diff] [blame] | 1221 | return user_list |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1222 | |
| tierno | bee3bad | 2019-12-05 12:26:01 +0000 | [diff] [blame] | 1223 | def delete(self, session, _id, dry_run=False, not_send_msg=None): |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1224 | """ |
| 1225 | Delete item by its internal _id |
| 1226 | |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 1227 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1228 | :param _id: server internal id |
| 1229 | :param force: indicates if deletion must be forced in case of conflict |
| 1230 | :param dry_run: make checking but do not delete |
| tierno | bee3bad | 2019-12-05 12:26:01 +0000 | [diff] [blame] | 1231 | :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] | 1232 | :return: dictionary with deleted item _id. It raises EngineException on error: not found, conflict, ... |
| 1233 | """ |
| tierno | cf042d3 | 2019-06-13 09:06:40 +0000 | [diff] [blame] | 1234 | # Allow _id to be a name or uuid |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 1235 | user = self.auth.get_user(_id) |
| 1236 | uid = user["_id"] |
| 1237 | self.check_conflict_on_del(session, uid, user) |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1238 | if not dry_run: |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 1239 | v = self.auth.delete_user(uid) |
| agarwalat | 5347198 | 2020-10-08 13:06:14 +0000 | [diff] [blame] | 1240 | self._send_msg("deleted", user, not_send_msg=not_send_msg) |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1241 | return v |
| 1242 | return None |
| 1243 | |
| 1244 | |
| 1245 | class ProjectTopicAuth(ProjectTopic): |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 1246 | # topic = "projects" |
| agarwalat | 5347198 | 2020-10-08 13:06:14 +0000 | [diff] [blame] | 1247 | topic_msg = "project" |
| Eduardo Sousa | 4460390 | 2019-06-04 08:10:32 +0100 | [diff] [blame] | 1248 | schema_new = project_new_schema |
| 1249 | schema_edit = project_edit_schema |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1250 | |
| 1251 | def __init__(self, db, fs, msg, auth): |
| delacruzramo | 32bab47 | 2019-09-13 12:24:22 +0200 | [diff] [blame] | 1252 | ProjectTopic.__init__(self, db, fs, msg, auth) |
| 1253 | # self.auth = auth |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1254 | |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 1255 | def check_conflict_on_new(self, session, indata): |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1256 | """ |
| 1257 | Check that the data to be inserted is valid |
| 1258 | |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 1259 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1260 | :param indata: data to be inserted |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1261 | :return: None or raises EngineException |
| 1262 | """ |
| tierno | cf042d3 | 2019-06-13 09:06:40 +0000 | [diff] [blame] | 1263 | project_name = indata.get("name") |
| 1264 | if is_valid_uuid(project_name): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1265 | raise EngineException( |
| 1266 | "project name '{}' cannot have an uuid format".format(project_name), |
| 1267 | HTTPStatus.UNPROCESSABLE_ENTITY, |
| 1268 | ) |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1269 | |
| tierno | cf042d3 | 2019-06-13 09:06:40 +0000 | [diff] [blame] | 1270 | project_list = self.auth.get_project_list(filter_q={"name": project_name}) |
| 1271 | |
| 1272 | if project_list: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1273 | raise EngineException( |
| 1274 | "project '{}' exists".format(project_name), HTTPStatus.CONFLICT |
| 1275 | ) |
| tierno | cf042d3 | 2019-06-13 09:06:40 +0000 | [diff] [blame] | 1276 | |
| 1277 | def check_conflict_on_edit(self, session, final_content, edit_content, _id): |
| 1278 | """ |
| 1279 | Check that the data to be edited/uploaded is valid |
| 1280 | |
| 1281 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| 1282 | :param final_content: data once modified |
| 1283 | :param edit_content: incremental data that contains the modifications to apply |
| 1284 | :param _id: internal _id |
| 1285 | :return: None or raises EngineException |
| 1286 | """ |
| 1287 | |
| 1288 | project_name = edit_content.get("name") |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 1289 | if project_name != final_content["name"]: # It is a true renaming |
| tierno | cf042d3 | 2019-06-13 09:06:40 +0000 | [diff] [blame] | 1290 | if is_valid_uuid(project_name): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1291 | raise EngineException( |
| 1292 | "project name '{}' cannot have an uuid format".format(project_name), |
| 1293 | HTTPStatus.UNPROCESSABLE_ENTITY, |
| 1294 | ) |
| tierno | cf042d3 | 2019-06-13 09:06:40 +0000 | [diff] [blame] | 1295 | |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 1296 | if final_content["name"] == "admin": |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1297 | raise EngineException( |
| 1298 | "You cannot rename project 'admin'", http_code=HTTPStatus.CONFLICT |
| 1299 | ) |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 1300 | |
| tierno | cf042d3 | 2019-06-13 09:06:40 +0000 | [diff] [blame] | 1301 | # Check that project name is not used, regardless keystone already checks this |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1302 | if project_name and self.auth.get_project_list( |
| 1303 | filter_q={"name": project_name} |
| 1304 | ): |
| 1305 | raise EngineException( |
| 1306 | "project '{}' is already used".format(project_name), |
| 1307 | HTTPStatus.CONFLICT, |
| 1308 | ) |
| bravof | b995ea2 | 2021-02-10 10:57:52 -0300 | [diff] [blame] | 1309 | return final_content |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1310 | |
| tierno | b4844ab | 2019-05-23 08:42:12 +0000 | [diff] [blame] | 1311 | def check_conflict_on_del(self, session, _id, db_content): |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1312 | """ |
| 1313 | Check if deletion can be done because of dependencies if it is not force. To override |
| 1314 | |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 1315 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1316 | :param _id: internal _id |
| tierno | b4844ab | 2019-05-23 08:42:12 +0000 | [diff] [blame] | 1317 | :param db_content: The database content of this item _id |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1318 | :return: None if ok or raises EngineException with the conflict |
| 1319 | """ |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 1320 | |
| 1321 | def check_rw_projects(topic, title, id_field): |
| 1322 | for desc in self.db.get_list(topic): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1323 | if ( |
| 1324 | _id |
| 1325 | in desc["_admin"]["projects_read"] |
| 1326 | + desc["_admin"]["projects_write"] |
| 1327 | ): |
| 1328 | raise EngineException( |
| 1329 | "Project '{}' ({}) is being used by {} '{}'".format( |
| 1330 | db_content["name"], _id, title, desc[id_field] |
| 1331 | ), |
| 1332 | HTTPStatus.CONFLICT, |
| 1333 | ) |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 1334 | |
| 1335 | if _id in session["project_id"]: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1336 | raise EngineException( |
| 1337 | "You cannot delete your own project", http_code=HTTPStatus.CONFLICT |
| 1338 | ) |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1339 | |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 1340 | if db_content["name"] == "admin": |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1341 | raise EngineException( |
| 1342 | "You cannot delete project 'admin'", http_code=HTTPStatus.CONFLICT |
| 1343 | ) |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 1344 | |
| 1345 | # If any user is using this project, raise CONFLICT exception |
| 1346 | if not session["force"]: |
| 1347 | for user in self.auth.get_user_list(): |
| tierno | 1546f2a | 2019-08-20 15:38:11 +0000 | [diff] [blame] | 1348 | for prm in user.get("project_role_mappings"): |
| 1349 | if prm["project"] == _id: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1350 | raise EngineException( |
| 1351 | "Project '{}' ({}) is being used by user '{}'".format( |
| 1352 | db_content["name"], _id, user["username"] |
| 1353 | ), |
| 1354 | HTTPStatus.CONFLICT, |
| 1355 | ) |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 1356 | |
| 1357 | # If any VNFD, NSD, NST, PDU, etc. is using this project, raise CONFLICT exception |
| 1358 | if not session["force"]: |
| 1359 | check_rw_projects("vnfds", "VNF Descriptor", "id") |
| 1360 | check_rw_projects("nsds", "NS Descriptor", "id") |
| 1361 | check_rw_projects("nsts", "NS Template", "id") |
| 1362 | check_rw_projects("pdus", "PDU Descriptor", "name") |
| 1363 | |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 1364 | def new(self, rollback, session, indata=None, kwargs=None, headers=None): |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1365 | """ |
| 1366 | Creates a new entry into the authentication backend. |
| 1367 | |
| 1368 | NOTE: Overrides BaseTopic functionality because it doesn't require access to database. |
| 1369 | |
| 1370 | :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] | 1371 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1372 | :param indata: data to be inserted |
| 1373 | :param kwargs: used to override the indata descriptor |
| 1374 | :param headers: http request headers |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 1375 | :return: _id: identity of the inserted data, operation _id (None) |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1376 | """ |
| 1377 | try: |
| 1378 | content = BaseTopic._remove_envelop(indata) |
| 1379 | |
| 1380 | # Override descriptor with query string kwargs |
| 1381 | BaseTopic._update_input_with_kwargs(content, kwargs) |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 1382 | content = self._validate_input_new(content, session["force"]) |
| 1383 | self.check_conflict_on_new(session, content) |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1384 | self.format_on_new( |
| 1385 | content, project_id=session["project_id"], make_public=session["public"] |
| 1386 | ) |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 1387 | _id = self.auth.create_project(content) |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1388 | rollback.append({"topic": self.topic, "_id": _id}) |
| agarwalat | 5347198 | 2020-10-08 13:06:14 +0000 | [diff] [blame] | 1389 | self._send_msg("created", content, not_send_msg=None) |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 1390 | return _id, None |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1391 | except ValidationError as e: |
| 1392 | raise EngineException(e, HTTPStatus.UNPROCESSABLE_ENTITY) |
| 1393 | |
| K Sai Kiran | 5758955 | 2021-01-27 21:38:34 +0530 | [diff] [blame] | 1394 | def show(self, session, _id, filter_q=None, api_req=False): |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1395 | """ |
| 1396 | Get complete information on an topic |
| 1397 | |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 1398 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1399 | :param _id: server internal id |
| K Sai Kiran | 5758955 | 2021-01-27 21:38:34 +0530 | [diff] [blame] | 1400 | :param filter_q: dict: query parameter |
| K Sai Kiran | d010e3e | 2020-08-28 15:11:48 +0530 | [diff] [blame] | 1401 | :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] | 1402 | :return: dictionary, raise exception if not found. |
| 1403 | """ |
| tierno | cf042d3 | 2019-06-13 09:06:40 +0000 | [diff] [blame] | 1404 | # Allow _id to be a name or uuid |
| 1405 | filter_q = {self.id_field(self.topic, _id): _id} |
| delacruzramo | 029405d | 2019-09-26 10:52:56 +0200 | [diff] [blame] | 1406 | # projects = self.auth.get_project_list(filter_q=filter_q) |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1407 | projects = self.list(session, filter_q) # To allow default filtering (Bug 853) |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1408 | if len(projects) == 1: |
| 1409 | return projects[0] |
| 1410 | elif len(projects) > 1: |
| 1411 | raise EngineException("Too many projects found", HTTPStatus.CONFLICT) |
| 1412 | else: |
| 1413 | raise EngineException("Project not found", HTTPStatus.NOT_FOUND) |
| 1414 | |
| tierno | c4e07d0 | 2020-08-14 14:25:32 +0000 | [diff] [blame] | 1415 | def list(self, session, filter_q=None, api_req=False): |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1416 | """ |
| 1417 | Get a list of the topic that matches a filter |
| 1418 | |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 1419 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1420 | :param filter_q: filter of data to be applied |
| 1421 | :return: The list, it can be empty if no one match the filter. |
| 1422 | """ |
| delacruzramo | 029405d | 2019-09-26 10:52:56 +0200 | [diff] [blame] | 1423 | project_list = self.auth.get_project_list(filter_q) |
| 1424 | if not session["allow_show_user_project_role"]: |
| 1425 | # Bug 853 - Default filtering |
| 1426 | user = self.auth.get_user(session["username"]) |
| 1427 | projects = [prm["project"] for prm in user["project_role_mappings"]] |
| 1428 | project_list = [proj for proj in project_list if proj["_id"] in projects] |
| 1429 | return project_list |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1430 | |
| tierno | bee3bad | 2019-12-05 12:26:01 +0000 | [diff] [blame] | 1431 | def delete(self, session, _id, dry_run=False, not_send_msg=None): |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1432 | """ |
| 1433 | Delete item by its internal _id |
| 1434 | |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 1435 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1436 | :param _id: server internal id |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1437 | :param dry_run: make checking but do not delete |
| tierno | bee3bad | 2019-12-05 12:26:01 +0000 | [diff] [blame] | 1438 | :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] | 1439 | :return: dictionary with deleted item _id. It raises EngineException on error: not found, conflict, ... |
| 1440 | """ |
| tierno | cf042d3 | 2019-06-13 09:06:40 +0000 | [diff] [blame] | 1441 | # Allow _id to be a name or uuid |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 1442 | proj = self.auth.get_project(_id) |
| 1443 | pid = proj["_id"] |
| 1444 | self.check_conflict_on_del(session, pid, proj) |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1445 | if not dry_run: |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 1446 | v = self.auth.delete_project(pid) |
| agarwalat | 5347198 | 2020-10-08 13:06:14 +0000 | [diff] [blame] | 1447 | self._send_msg("deleted", proj, not_send_msg=None) |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1448 | return v |
| 1449 | return None |
| 1450 | |
| tierno | 4015b47 | 2019-06-10 13:57:29 +0000 | [diff] [blame] | 1451 | def edit(self, session, _id, indata=None, kwargs=None, content=None): |
| 1452 | """ |
| 1453 | Updates a project entry. |
| 1454 | |
| 1455 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| 1456 | :param _id: |
| 1457 | :param indata: data to be inserted |
| 1458 | :param kwargs: used to override the indata descriptor |
| 1459 | :param content: |
| 1460 | :return: _id: identity of the inserted data. |
| 1461 | """ |
| 1462 | indata = self._remove_envelop(indata) |
| 1463 | |
| 1464 | # Override descriptor with query string kwargs |
| 1465 | if kwargs: |
| 1466 | BaseTopic._update_input_with_kwargs(indata, kwargs) |
| 1467 | try: |
| tierno | 4015b47 | 2019-06-10 13:57:29 +0000 | [diff] [blame] | 1468 | if not content: |
| 1469 | content = self.show(session, _id) |
| Frank Bryden | deba68e | 2020-07-27 13:55:11 +0000 | [diff] [blame] | 1470 | indata = self._validate_input_edit(indata, content, force=session["force"]) |
| bravof | b995ea2 | 2021-02-10 10:57:52 -0300 | [diff] [blame] | 1471 | content = self.check_conflict_on_edit(session, content, indata, _id=_id) |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 1472 | self.format_on_edit(content, indata) |
| agarwalat | 5347198 | 2020-10-08 13:06:14 +0000 | [diff] [blame] | 1473 | content_original = copy.deepcopy(content) |
| delacruzramo | 32bab47 | 2019-09-13 12:24:22 +0200 | [diff] [blame] | 1474 | deep_update_rfc7396(content, indata) |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 1475 | self.auth.update_project(content["_id"], content) |
| agarwalat | 5347198 | 2020-10-08 13:06:14 +0000 | [diff] [blame] | 1476 | proj_data = {"_id": _id, "changes": indata, "original": content_original} |
| 1477 | self._send_msg("edited", proj_data, not_send_msg=None) |
| tierno | 4015b47 | 2019-06-10 13:57:29 +0000 | [diff] [blame] | 1478 | except ValidationError as e: |
| 1479 | raise EngineException(e, HTTPStatus.UNPROCESSABLE_ENTITY) |
| 1480 | |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1481 | |
| 1482 | class RoleTopicAuth(BaseTopic): |
| delacruzramo | ceb8baf | 2019-06-21 14:25:38 +0200 | [diff] [blame] | 1483 | topic = "roles" |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1484 | topic_msg = None # "roles" |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1485 | schema_new = roles_new_schema |
| 1486 | schema_edit = roles_edit_schema |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 1487 | multiproject = False |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1488 | |
| tierno | 9e87a7f | 2020-03-23 09:24:10 +0000 | [diff] [blame] | 1489 | def __init__(self, db, fs, msg, auth): |
| delacruzramo | 32bab47 | 2019-09-13 12:24:22 +0200 | [diff] [blame] | 1490 | BaseTopic.__init__(self, db, fs, msg, auth) |
| 1491 | # self.auth = auth |
| tierno | 9e87a7f | 2020-03-23 09:24:10 +0000 | [diff] [blame] | 1492 | self.operations = auth.role_permissions |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 1493 | # self.topic = "roles_operations" if isinstance(auth, AuthconnKeystone) else "roles" |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1494 | |
| 1495 | @staticmethod |
| 1496 | def validate_role_definition(operations, role_definitions): |
| 1497 | """ |
| 1498 | Validates the role definition against the operations defined in |
| 1499 | the resources to operations files. |
| 1500 | |
| 1501 | :param operations: operations list |
| 1502 | :param role_definitions: role definition to test |
| 1503 | :return: None if ok, raises ValidationError exception on error |
| 1504 | """ |
| tierno | 1f029d8 | 2019-06-13 22:37:04 +0000 | [diff] [blame] | 1505 | if not role_definitions.get("permissions"): |
| 1506 | return |
| 1507 | ignore_fields = ["admin", "default"] |
| 1508 | for role_def in role_definitions["permissions"].keys(): |
| Eduardo Sousa | 37de091 | 2019-05-23 02:17:22 +0100 | [diff] [blame] | 1509 | if role_def in ignore_fields: |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1510 | continue |
| Eduardo Sousa | c768937 | 2019-06-04 16:01:46 +0100 | [diff] [blame] | 1511 | if role_def[-1] == ":": |
| tierno | 1f029d8 | 2019-06-13 22:37:04 +0000 | [diff] [blame] | 1512 | raise ValidationError("Operation cannot end with ':'") |
| Eduardo Sousa | c5a1889 | 2019-06-06 14:51:23 +0100 | [diff] [blame] | 1513 | |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1514 | match = next( |
| 1515 | ( |
| 1516 | op |
| 1517 | for op in operations |
| 1518 | if op == role_def or op.startswith(role_def + ":") |
| 1519 | ), |
| 1520 | None, |
| 1521 | ) |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1522 | |
| tierno | 97639b4 | 2020-08-04 12:48:15 +0000 | [diff] [blame] | 1523 | if not match: |
| tierno | 1f029d8 | 2019-06-13 22:37:04 +0000 | [diff] [blame] | 1524 | raise ValidationError("Invalid permission '{}'".format(role_def)) |
| Eduardo Sousa | 37de091 | 2019-05-23 02:17:22 +0100 | [diff] [blame] | 1525 | |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1526 | def _validate_input_new(self, input, force=False): |
| 1527 | """ |
| 1528 | Validates input user content for a new 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_new: |
| 1535 | validate_input(input, self.schema_new) |
| 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 | |
| Frank Bryden | deba68e | 2020-07-27 13:55:11 +0000 | [diff] [blame] | 1540 | def _validate_input_edit(self, input, content, force=False): |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1541 | """ |
| 1542 | Validates input user content for updating an entry. |
| 1543 | |
| 1544 | :param input: user input content for the new topic |
| 1545 | :param force: may be used for being more tolerant |
| 1546 | :return: The same input content, or a changed version of it. |
| 1547 | """ |
| 1548 | if self.schema_edit: |
| 1549 | validate_input(input, self.schema_edit) |
| Eduardo Sousa | 37de091 | 2019-05-23 02:17:22 +0100 | [diff] [blame] | 1550 | self.validate_role_definition(self.operations, input) |
| Eduardo Sousa | c465036 | 2019-06-04 13:24:22 +0100 | [diff] [blame] | 1551 | |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1552 | return input |
| 1553 | |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 1554 | def check_conflict_on_new(self, session, indata): |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1555 | """ |
| 1556 | Check that the data to be inserted is valid |
| 1557 | |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 1558 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1559 | :param indata: data to be inserted |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1560 | :return: None or raises EngineException |
| 1561 | """ |
| delacruzramo | 79e40f4 | 2019-10-10 16:36:40 +0200 | [diff] [blame] | 1562 | # check name is not uuid |
| 1563 | role_name = indata.get("name") |
| 1564 | if is_valid_uuid(role_name): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1565 | raise EngineException( |
| 1566 | "role name '{}' cannot have an uuid format".format(role_name), |
| 1567 | HTTPStatus.UNPROCESSABLE_ENTITY, |
| 1568 | ) |
| tierno | 1f029d8 | 2019-06-13 22:37:04 +0000 | [diff] [blame] | 1569 | # check name not exists |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 1570 | name = indata["name"] |
| 1571 | # if self.db.get_one(self.topic, {"name": indata.get("name")}, fail_on_empty=False, fail_on_more=False): |
| 1572 | if self.auth.get_role_list({"name": name}): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1573 | raise EngineException( |
| 1574 | "role name '{}' exists".format(name), HTTPStatus.CONFLICT |
| 1575 | ) |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1576 | |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 1577 | def check_conflict_on_edit(self, session, final_content, edit_content, _id): |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1578 | """ |
| 1579 | Check that the data to be edited/uploaded is valid |
| 1580 | |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 1581 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1582 | :param final_content: data once modified |
| 1583 | :param edit_content: incremental data that contains the modifications to apply |
| 1584 | :param _id: internal _id |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1585 | :return: None or raises EngineException |
| 1586 | """ |
| tierno | 1f029d8 | 2019-06-13 22:37:04 +0000 | [diff] [blame] | 1587 | if "default" not in final_content["permissions"]: |
| 1588 | final_content["permissions"]["default"] = False |
| 1589 | if "admin" not in final_content["permissions"]: |
| 1590 | final_content["permissions"]["admin"] = False |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1591 | |
| delacruzramo | 79e40f4 | 2019-10-10 16:36:40 +0200 | [diff] [blame] | 1592 | # check name is not uuid |
| 1593 | role_name = edit_content.get("name") |
| 1594 | if is_valid_uuid(role_name): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1595 | raise EngineException( |
| 1596 | "role name '{}' cannot have an uuid format".format(role_name), |
| 1597 | HTTPStatus.UNPROCESSABLE_ENTITY, |
| 1598 | ) |
| delacruzramo | 79e40f4 | 2019-10-10 16:36:40 +0200 | [diff] [blame] | 1599 | |
| 1600 | # Check renaming of admin roles |
| 1601 | role = self.auth.get_role(_id) |
| 1602 | if role["name"] in ["system_admin", "project_admin"]: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1603 | raise EngineException( |
| 1604 | "You cannot rename role '{}'".format(role["name"]), |
| 1605 | http_code=HTTPStatus.FORBIDDEN, |
| 1606 | ) |
| delacruzramo | 79e40f4 | 2019-10-10 16:36:40 +0200 | [diff] [blame] | 1607 | |
| tierno | 1f029d8 | 2019-06-13 22:37:04 +0000 | [diff] [blame] | 1608 | # check name not exists |
| 1609 | if "name" in edit_content: |
| 1610 | role_name = edit_content["name"] |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 1611 | # if self.db.get_one(self.topic, {"name":role_name,"_id.ne":_id}, fail_on_empty=False, fail_on_more=False): |
| 1612 | roles = self.auth.get_role_list({"name": role_name}) |
| 1613 | if roles and roles[0][BaseTopic.id_field("roles", _id)] != _id: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1614 | raise EngineException( |
| 1615 | "role name '{}' exists".format(role_name), HTTPStatus.CONFLICT |
| 1616 | ) |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1617 | |
| bravof | b995ea2 | 2021-02-10 10:57:52 -0300 | [diff] [blame] | 1618 | return final_content |
| 1619 | |
| tierno | b4844ab | 2019-05-23 08:42:12 +0000 | [diff] [blame] | 1620 | def check_conflict_on_del(self, session, _id, db_content): |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1621 | """ |
| 1622 | Check if deletion can be done because of dependencies if it is not force. To override |
| 1623 | |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 1624 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1625 | :param _id: internal _id |
| tierno | b4844ab | 2019-05-23 08:42:12 +0000 | [diff] [blame] | 1626 | :param db_content: The database content of this item _id |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1627 | :return: None if ok or raises EngineException with the conflict |
| 1628 | """ |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 1629 | role = self.auth.get_role(_id) |
| 1630 | if role["name"] in ["system_admin", "project_admin"]: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1631 | raise EngineException( |
| 1632 | "You cannot delete role '{}'".format(role["name"]), |
| 1633 | http_code=HTTPStatus.FORBIDDEN, |
| 1634 | ) |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1635 | |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 1636 | # If any user is using this role, raise CONFLICT exception |
| delacruzramo | ad682a5 | 2019-12-10 16:26:34 +0100 | [diff] [blame] | 1637 | if not session["force"]: |
| 1638 | for user in self.auth.get_user_list(): |
| 1639 | for prm in user.get("project_role_mappings"): |
| 1640 | if prm["role"] == _id: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1641 | raise EngineException( |
| 1642 | "Role '{}' ({}) is being used by user '{}'".format( |
| 1643 | role["name"], _id, user["username"] |
| 1644 | ), |
| 1645 | HTTPStatus.CONFLICT, |
| 1646 | ) |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1647 | |
| 1648 | @staticmethod |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1649 | 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] | 1650 | """ |
| 1651 | Modifies content descriptor to include _admin |
| 1652 | |
| 1653 | :param content: descriptor to be modified |
| 1654 | :param project_id: if included, it add project read/write permissions |
| 1655 | :param make_public: if included it is generated as public for reading. |
| 1656 | :return: None, but content is modified |
| 1657 | """ |
| 1658 | now = time() |
| 1659 | if "_admin" not in content: |
| 1660 | content["_admin"] = {} |
| 1661 | if not content["_admin"].get("created"): |
| 1662 | content["_admin"]["created"] = now |
| 1663 | content["_admin"]["modified"] = now |
| Eduardo Sousa | c465036 | 2019-06-04 13:24:22 +0100 | [diff] [blame] | 1664 | |
| tierno | 1f029d8 | 2019-06-13 22:37:04 +0000 | [diff] [blame] | 1665 | if "permissions" not in content: |
| 1666 | content["permissions"] = {} |
| Eduardo Sousa | c465036 | 2019-06-04 13:24:22 +0100 | [diff] [blame] | 1667 | |
| tierno | 1f029d8 | 2019-06-13 22:37:04 +0000 | [diff] [blame] | 1668 | if "default" not in content["permissions"]: |
| 1669 | content["permissions"]["default"] = False |
| 1670 | if "admin" not in content["permissions"]: |
| 1671 | content["permissions"]["admin"] = False |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1672 | |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1673 | @staticmethod |
| 1674 | def format_on_edit(final_content, edit_content): |
| 1675 | """ |
| 1676 | Modifies final_content descriptor to include the modified date. |
| 1677 | |
| 1678 | :param final_content: final descriptor generated |
| 1679 | :param edit_content: alterations to be include |
| 1680 | :return: None, but final_content is modified |
| 1681 | """ |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 1682 | if "_admin" in final_content: |
| 1683 | final_content["_admin"]["modified"] = time() |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1684 | |
| tierno | 1f029d8 | 2019-06-13 22:37:04 +0000 | [diff] [blame] | 1685 | if "permissions" not in final_content: |
| 1686 | final_content["permissions"] = {} |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1687 | |
| tierno | 1f029d8 | 2019-06-13 22:37:04 +0000 | [diff] [blame] | 1688 | if "default" not in final_content["permissions"]: |
| 1689 | final_content["permissions"]["default"] = False |
| 1690 | if "admin" not in final_content["permissions"]: |
| 1691 | final_content["permissions"]["admin"] = False |
| tierno | bdebce9 | 2019-07-01 15:36:49 +0000 | [diff] [blame] | 1692 | return None |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1693 | |
| K Sai Kiran | 5758955 | 2021-01-27 21:38:34 +0530 | [diff] [blame] | 1694 | def show(self, session, _id, filter_q=None, api_req=False): |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 1695 | """ |
| 1696 | Get complete information on an topic |
| Eduardo Sousa | c465036 | 2019-06-04 13:24:22 +0100 | [diff] [blame] | 1697 | |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 1698 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| 1699 | :param _id: server internal id |
| K Sai Kiran | 5758955 | 2021-01-27 21:38:34 +0530 | [diff] [blame] | 1700 | :param filter_q: dict: query parameter |
| K Sai Kiran | d010e3e | 2020-08-28 15:11:48 +0530 | [diff] [blame] | 1701 | :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] | 1702 | :return: dictionary, raise exception if not found. |
| 1703 | """ |
| 1704 | filter_q = {BaseTopic.id_field(self.topic, _id): _id} |
| delacruzramo | 029405d | 2019-09-26 10:52:56 +0200 | [diff] [blame] | 1705 | # roles = self.auth.get_role_list(filter_q) |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1706 | roles = self.list(session, filter_q) # To allow default filtering (Bug 853) |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 1707 | if not roles: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1708 | raise AuthconnNotFoundException( |
| 1709 | "Not found any role with filter {}".format(filter_q) |
| 1710 | ) |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 1711 | elif len(roles) > 1: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1712 | raise AuthconnConflictException( |
| 1713 | "Found more than one role with filter {}".format(filter_q) |
| 1714 | ) |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 1715 | return roles[0] |
| 1716 | |
| tierno | c4e07d0 | 2020-08-14 14:25:32 +0000 | [diff] [blame] | 1717 | def list(self, session, filter_q=None, api_req=False): |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 1718 | """ |
| 1719 | Get a list of the topic that matches a filter |
| 1720 | |
| 1721 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| 1722 | :param filter_q: filter of data to be applied |
| 1723 | :return: The list, it can be empty if no one match the filter. |
| 1724 | """ |
| delacruzramo | 029405d | 2019-09-26 10:52:56 +0200 | [diff] [blame] | 1725 | role_list = self.auth.get_role_list(filter_q) |
| 1726 | if not session["allow_show_user_project_role"]: |
| 1727 | # Bug 853 - Default filtering |
| 1728 | user = self.auth.get_user(session["username"]) |
| 1729 | roles = [prm["role"] for prm in user["project_role_mappings"]] |
| 1730 | role_list = [role for role in role_list if role["_id"] in roles] |
| 1731 | return role_list |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1732 | |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 1733 | def new(self, rollback, session, indata=None, kwargs=None, headers=None): |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1734 | """ |
| 1735 | Creates a new entry into database. |
| 1736 | |
| 1737 | :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] | 1738 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1739 | :param indata: data to be inserted |
| 1740 | :param kwargs: used to override the indata descriptor |
| 1741 | :param headers: http request headers |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 1742 | :return: _id: identity of the inserted data, operation _id (None) |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1743 | """ |
| 1744 | try: |
| tierno | 1f029d8 | 2019-06-13 22:37:04 +0000 | [diff] [blame] | 1745 | content = self._remove_envelop(indata) |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1746 | |
| 1747 | # Override descriptor with query string kwargs |
| tierno | 1f029d8 | 2019-06-13 22:37:04 +0000 | [diff] [blame] | 1748 | self._update_input_with_kwargs(content, kwargs) |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 1749 | content = self._validate_input_new(content, session["force"]) |
| 1750 | self.check_conflict_on_new(session, content) |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1751 | self.format_on_new( |
| 1752 | content, project_id=session["project_id"], make_public=session["public"] |
| 1753 | ) |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 1754 | # role_name = content["name"] |
| 1755 | rid = self.auth.create_role(content) |
| 1756 | content["_id"] = rid |
| 1757 | # _id = self.db.create(self.topic, content) |
| 1758 | rollback.append({"topic": self.topic, "_id": rid}) |
| tierno | bee3bad | 2019-12-05 12:26:01 +0000 | [diff] [blame] | 1759 | # self._send_msg("created", content, not_send_msg=not_send_msg) |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 1760 | return rid, None |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1761 | except ValidationError as e: |
| 1762 | raise EngineException(e, HTTPStatus.UNPROCESSABLE_ENTITY) |
| 1763 | |
| tierno | bee3bad | 2019-12-05 12:26:01 +0000 | [diff] [blame] | 1764 | def delete(self, session, _id, dry_run=False, not_send_msg=None): |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1765 | """ |
| 1766 | Delete item by its internal _id |
| 1767 | |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 1768 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1769 | :param _id: server internal id |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1770 | :param dry_run: make checking but do not delete |
| tierno | bee3bad | 2019-12-05 12:26:01 +0000 | [diff] [blame] | 1771 | :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] | 1772 | :return: dictionary with deleted item _id. It raises EngineException on error: not found, conflict, ... |
| 1773 | """ |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 1774 | filter_q = {BaseTopic.id_field(self.topic, _id): _id} |
| 1775 | roles = self.auth.get_role_list(filter_q) |
| 1776 | if not roles: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1777 | raise AuthconnNotFoundException( |
| 1778 | "Not found any role with filter {}".format(filter_q) |
| 1779 | ) |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 1780 | elif len(roles) > 1: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1781 | raise AuthconnConflictException( |
| 1782 | "Found more than one role with filter {}".format(filter_q) |
| 1783 | ) |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 1784 | rid = roles[0]["_id"] |
| 1785 | self.check_conflict_on_del(session, rid, None) |
| delacruzramo | ceb8baf | 2019-06-21 14:25:38 +0200 | [diff] [blame] | 1786 | # filter_q = {"_id": _id} |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 1787 | # 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] | 1788 | if not dry_run: |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 1789 | v = self.auth.delete_role(rid) |
| 1790 | # v = self.db.del_one(self.topic, filter_q) |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1791 | return v |
| 1792 | return None |
| 1793 | |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 1794 | def edit(self, session, _id, indata=None, kwargs=None, content=None): |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1795 | """ |
| 1796 | Updates a role entry. |
| 1797 | |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 1798 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1799 | :param _id: |
| 1800 | :param indata: data to be inserted |
| 1801 | :param kwargs: used to override the indata descriptor |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 1802 | :param content: |
| 1803 | :return: _id: identity of the inserted data. |
| 1804 | """ |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 1805 | if kwargs: |
| 1806 | self._update_input_with_kwargs(indata, kwargs) |
| 1807 | try: |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 1808 | if not content: |
| 1809 | content = self.show(session, _id) |
| Frank Bryden | deba68e | 2020-07-27 13:55:11 +0000 | [diff] [blame] | 1810 | indata = self._validate_input_edit(indata, content, force=session["force"]) |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 1811 | deep_update_rfc7396(content, indata) |
| bravof | b995ea2 | 2021-02-10 10:57:52 -0300 | [diff] [blame] | 1812 | content = self.check_conflict_on_edit(session, content, indata, _id=_id) |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 1813 | self.format_on_edit(content, indata) |
| 1814 | self.auth.update_role(content) |
| 1815 | except ValidationError as e: |
| 1816 | raise EngineException(e, HTTPStatus.UNPROCESSABLE_ENTITY) |