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