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