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