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