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