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