blob: 3c3249c62000c2bb14181c87080dbaf2c7d2d598 [file] [log] [blame]
tiernob24258a2018-10-04 18:39:49 +02001# -*- coding: utf-8 -*-
2
tiernod125caf2018-11-22 16:05:54 +00003# 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
tiernob24258a2018-10-04 18:39:49 +020016# import logging
17from uuid import uuid4
18from hashlib import sha256
19from http import HTTPStatus
Eduardo Sousa5c01e192019-05-08 02:35:47 +010020from time import time
tierno23acf402019-08-28 13:36:34 +000021from 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, \
delacruzramofe598fe2019-10-23 18:25:11 +020024 k8scluster_new_schema, k8scluster_edit_schema, k8srepo_new_schema, k8srepo_edit_schema, \
Felipe Vicensb66b0412020-05-06 10:11:00 +020025 osmrepo_new_schema, osmrepo_edit_schema, \
26 validate_input, ValidationError, is_valid_uuid # To check that User/Project Names don't look like UUIDs
tierno23acf402019-08-28 13:36:34 +000027from osm_nbi.base_topic import BaseTopic, EngineException
28from osm_nbi.authconn import AuthconnNotFoundException, AuthconnConflictException
delacruzramo01b15d32019-07-02 14:37:47 +020029from osm_common.dbbase import deep_update_rfc7396
tiernob24258a2018-10-04 18:39:49 +020030
31__author__ = "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>"
32
33
34class UserTopic(BaseTopic):
35 topic = "users"
36 topic_msg = "users"
37 schema_new = user_new_schema
38 schema_edit = user_edit_schema
tierno65ca36d2019-02-12 19:27:52 +010039 multiproject = False
tiernob24258a2018-10-04 18:39:49 +020040
delacruzramo32bab472019-09-13 12:24:22 +020041 def __init__(self, db, fs, msg, auth):
42 BaseTopic.__init__(self, db, fs, msg, auth)
tiernob24258a2018-10-04 18:39:49 +020043
44 @staticmethod
tierno65ca36d2019-02-12 19:27:52 +010045 def _get_project_filter(session):
tiernob24258a2018-10-04 18:39:49 +020046 """
47 Generates a filter dictionary for querying database users.
48 Current policy is admin can show all, non admin, only its own user.
tierno65ca36d2019-02-12 19:27:52 +010049 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
tiernob24258a2018-10-04 18:39:49 +020050 :return:
51 """
52 if session["admin"]: # allows all
53 return {}
54 else:
55 return {"username": session["username"]}
56
tierno65ca36d2019-02-12 19:27:52 +010057 def check_conflict_on_new(self, session, indata):
tiernob24258a2018-10-04 18:39:49 +020058 # 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
tierno65ca36d2019-02-12 19:27:52 +010062 if not session["force"]:
delacruzramoceb8baf2019-06-21 14:25:38 +020063 for p in indata.get("projects") or []:
delacruzramoc061f562019-04-05 11:00:02 +020064 # 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)
tiernob24258a2018-10-04 18:39:49 +020068
tiernob4844ab2019-05-23 08:42:12 +000069 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 """
tiernob24258a2018-10-04 18:39:49 +020077 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)
delacruzramoc061f562019-04-05 11:00:02 +020083 # Removed so that the UUID is kept, to allow User Name modification
84 # content["_id"] = content["username"]
tiernob24258a2018-10-04 18:39:49 +020085 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 Sousa339ed782019-05-28 14:25:00 +010089 if content.get("project_role_mappings"):
delacruzramo01b15d32019-07-02 14:37:47 +020090 projects = [mapping["project"] for mapping in content["project_role_mappings"]]
Eduardo Sousa339ed782019-05-28 14:25:00 +010091
92 if content.get("projects"):
93 content["projects"] += projects
94 else:
95 content["projects"] = projects
tiernob24258a2018-10-04 18:39:49 +020096
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()
tiernobdebce92019-07-01 15:36:49 +0000105 return None
tiernob24258a2018-10-04 18:39:49 +0200106
tierno65ca36d2019-02-12 19:27:52 +0100107 def edit(self, session, _id, indata=None, kwargs=None, content=None):
tiernob24258a2018-10-04 18:39:49 +0200108 if not session["admin"]:
109 raise EngineException("needed admin privileges", http_code=HTTPStatus.UNAUTHORIZED)
delacruzramoc061f562019-04-05 11:00:02 +0200110 # 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)
tierno65ca36d2019-02-12 19:27:52 +0100115 return BaseTopic.edit(self, session, _id, indata=indata, kwargs=kwargs, content=content)
tiernob24258a2018-10-04 18:39:49 +0200116
tierno65ca36d2019-02-12 19:27:52 +0100117 def new(self, rollback, session, indata=None, kwargs=None, headers=None):
tiernob24258a2018-10-04 18:39:49 +0200118 if not session["admin"]:
119 raise EngineException("needed admin privileges", http_code=HTTPStatus.UNAUTHORIZED)
delacruzramoc061f562019-04-05 11:00:02 +0200120 # 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)
tierno65ca36d2019-02-12 19:27:52 +0100125 return BaseTopic.new(self, rollback, session, indata=indata, kwargs=kwargs, headers=headers)
tiernob24258a2018-10-04 18:39:49 +0200126
127
128class ProjectTopic(BaseTopic):
129 topic = "projects"
130 topic_msg = "projects"
131 schema_new = project_new_schema
132 schema_edit = project_edit_schema
tierno65ca36d2019-02-12 19:27:52 +0100133 multiproject = False
tiernob24258a2018-10-04 18:39:49 +0200134
delacruzramo32bab472019-09-13 12:24:22 +0200135 def __init__(self, db, fs, msg, auth):
136 BaseTopic.__init__(self, db, fs, msg, auth)
tiernob24258a2018-10-04 18:39:49 +0200137
tierno65ca36d2019-02-12 19:27:52 +0100138 @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):
tiernob24258a2018-10-04 18:39:49 +0200152 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)
delacruzramoc061f562019-04-05 11:00:02 +0200161 # Removed so that the UUID is kept, to allow Project Name modification
162 # content["_id"] = content["name"]
tiernob24258a2018-10-04 18:39:49 +0200163
tiernob4844ab2019-05-23 08:42:12 +0000164 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 """
tierno65ca36d2019-02-12 19:27:52 +0100172 if _id in session["project_id"]:
tiernob24258a2018-10-04 18:39:49 +0200173 raise EngineException("You cannot delete your own project", http_code=HTTPStatus.CONFLICT)
tierno65ca36d2019-02-12 19:27:52 +0100174 if session["force"]:
tiernob24258a2018-10-04 18:39:49 +0200175 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
tierno65ca36d2019-02-12 19:27:52 +0100180 def edit(self, session, _id, indata=None, kwargs=None, content=None):
tiernob24258a2018-10-04 18:39:49 +0200181 if not session["admin"]:
182 raise EngineException("needed admin privileges", http_code=HTTPStatus.UNAUTHORIZED)
delacruzramoc061f562019-04-05 11:00:02 +0200183 # 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)
tierno65ca36d2019-02-12 19:27:52 +0100188 return BaseTopic.edit(self, session, _id, indata=indata, kwargs=kwargs, content=content)
tiernob24258a2018-10-04 18:39:49 +0200189
tierno65ca36d2019-02-12 19:27:52 +0100190 def new(self, rollback, session, indata=None, kwargs=None, headers=None):
tiernob24258a2018-10-04 18:39:49 +0200191 if not session["admin"]:
192 raise EngineException("needed admin privileges", http_code=HTTPStatus.UNAUTHORIZED)
delacruzramoc061f562019-04-05 11:00:02 +0200193 # 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)
tierno65ca36d2019-02-12 19:27:52 +0100198 return BaseTopic.new(self, rollback, session, indata=indata, kwargs=kwargs, headers=headers)
tiernob24258a2018-10-04 18:39:49 +0200199
200
tiernobdebce92019-07-01 15:36:49 +0000201class CommonVimWimSdn(BaseTopic):
202 """Common class for VIM, WIM SDN just to unify methods that are equal to all of them"""
tierno468aa242019-08-01 16:35:04 +0000203 config_to_encrypt = {} # what keys at config must be encrypted because contains passwords
tiernobdebce92019-07-01 15:36:49 +0000204 password_to_encrypt = "" # key that contains a password
tiernob24258a2018-10-04 18:39:49 +0200205
tiernobdebce92019-07-01 15:36:49 +0000206 @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 }
tiernob24258a2018-10-04 18:39:49 +0200223
tierno65ca36d2019-02-12 19:27:52 +0100224 def check_conflict_on_new(self, session, indata):
tiernobdebce92019-07-01 15:36:49 +0000225 """
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 """
tiernob24258a2018-10-04 18:39:49 +0200231 self.check_unique_name(session, indata["name"], _id=None)
232
tierno65ca36d2019-02-12 19:27:52 +0100233 def check_conflict_on_edit(self, session, final_content, edit_content, _id):
tiernobdebce92019-07-01 15:36:49 +0000234 """
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 """
tierno65ca36d2019-02-12 19:27:52 +0100242 if not session["force"] and edit_content.get("name"):
tiernob24258a2018-10-04 18:39:49 +0200243 self.check_unique_name(session, edit_content["name"], _id=_id)
244
tiernobdebce92019-07-01 15:36:49 +0000245 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 """
delacruzramofe598fe2019-10-23 18:25:11 +0200252 super().format_on_edit(final_content, edit_content)
tiernobdebce92019-07-01 15:36:49 +0000253
tierno92c1c7d2018-11-12 15:22:37 +0100254 # encrypt passwords
255 schema_version = final_content.get("schema_version")
256 if schema_version:
tiernobdebce92019-07-01 15:36:49 +0000257 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"])
tierno468aa242019-08-01 16:35:04 +0000261 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:
tierno92c1c7d2018-11-12 15:22:37 +0100265 if edit_content["config"].get(p):
266 final_content["config"][p] = self.db.encrypt(edit_content["config"][p],
tiernobdebce92019-07-01 15:36:49 +0000267 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)
tierno92c1c7d2018-11-12 15:22:37 +0100273
274 def format_on_new(self, content, project_id=None, make_public=False):
tiernobdebce92019-07-01 15:36:49 +0000275 """
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)
tierno468aa242019-08-01 16:35:04 +0000283 content["schema_version"] = schema_version = "1.11"
tierno92c1c7d2018-11-12 15:22:37 +0100284
285 # encrypt passwords
tiernobdebce92019-07-01 15:36:49 +0000286 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"])
tierno468aa242019-08-01 16:35:04 +0000290 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:
tierno92c1c7d2018-11-12 15:22:37 +0100293 if content["config"].get(p):
tiernobdebce92019-07-01 15:36:49 +0000294 content["config"][p] = self.db.encrypt(content["config"][p],
295 schema_version=schema_version,
tierno92c1c7d2018-11-12 15:22:37 +0100296 salt=content["_id"])
297
tiernob24258a2018-10-04 18:39:49 +0200298 content["_admin"]["operationalState"] = "PROCESSING"
299
tiernobdebce92019-07-01 15:36:49 +0000300 # create operation
301 content["_admin"]["operations"] = [self._create_operation("create")]
302 content["_admin"]["current_operation"] = None
303
304 return "{}:0".format(content["_id"])
305
tiernobee3bad2019-12-05 12:26:01 +0000306 def delete(self, session, _id, dry_run=False, not_send_msg=None):
tiernob24258a2018-10-04 18:39:49 +0200307 """
308 Delete item by its internal _id
tierno65ca36d2019-02-12 19:27:52 +0100309 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
tiernob24258a2018-10-04 18:39:49 +0200310 :param _id: server internal id
tiernob24258a2018-10-04 18:39:49 +0200311 :param dry_run: make checking but do not delete
tiernobee3bad2019-12-05 12:26:01 +0000312 :param not_send_msg: To not send message (False) or store content (list) instead
tiernobdebce92019-07-01 15:36:49 +0000313 :return: operation id if it is ordered to delete. None otherwise
tiernob24258a2018-10-04 18:39:49 +0200314 """
tiernobdebce92019-07-01 15:36:49 +0000315
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
tiernof5f2e3f2020-03-23 14:42:10 +0000324 # 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"]
tierno20e74d22020-06-22 12:17:22 +0000328 if p not in session["project_id"] and p != "ANY"), None)
tiernobdebce92019-07-01 15:36:49 +0000329
tiernof5f2e3f2020-03-23 14:42:10 +0000330 # check if there are projects referencing it (apart from ANY, that means, public)....
331 if other_projects_referencing:
332 # remove references but not delete
tierno20e74d22020-06-22 12:17:22 +0000333 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)
tiernof5f2e3f2020-03-23 14:42:10 +0000336 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)
tiernobdebce92019-07-01 15:36:49 +0000343
344 # It must be deleted
345 if session["force"]:
346 self.db.del_one(self.topic, {"_id": _id})
347 op_id = None
tiernobee3bad2019-12-05 12:26:01 +0000348 self._send_msg("deleted", {"_id": _id, "op_id": op_id}, not_send_msg=not_send_msg)
tiernobdebce92019-07-01 15:36:49 +0000349 else:
tiernof5f2e3f2020-03-23 14:42:10 +0000350 update_dict = {"_admin.to_delete": True}
tiernobdebce92019-07-01 15:36:49 +0000351 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"]))
tiernobee3bad2019-12-05 12:26:01 +0000358 self._send_msg("delete", {"_id": _id, "op_id": op_id}, not_send_msg=not_send_msg)
tiernobdebce92019-07-01 15:36:49 +0000359 return op_id
tiernob24258a2018-10-04 18:39:49 +0200360
361
tiernobdebce92019-07-01 15:36:49 +0000362class 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"
tierno468aa242019-08-01 16:35:04 +0000369 config_to_encrypt = {"1.1": ("admin_password", "nsx_password", "vcenter_password"),
370 "default": ("admin_password", "nsx_password", "vcenter_password", "vrops_password")}
tiernobdebce92019-07-01 15:36:49 +0000371
delacruzramo35c998b2019-11-21 11:09:16 +0100372 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
tiernobdebce92019-07-01 15:36:49 +0000387
388class WimAccountTopic(CommonVimWimSdn):
tierno55ba2e62018-12-11 17:22:22 +0000389 topic = "wim_accounts"
390 topic_msg = "wim_account"
391 schema_new = wim_account_new_schema
392 schema_edit = wim_account_edit_schema
tierno65ca36d2019-02-12 19:27:52 +0100393 multiproject = True
tiernobdebce92019-07-01 15:36:49 +0000394 password_to_encrypt = "wim_password"
tierno468aa242019-08-01 16:35:04 +0000395 config_to_encrypt = {}
tierno55ba2e62018-12-11 17:22:22 +0000396
397
tiernobdebce92019-07-01 15:36:49 +0000398class SdnTopic(CommonVimWimSdn):
tiernob24258a2018-10-04 18:39:49 +0200399 topic = "sdns"
400 topic_msg = "sdn"
tierno6b02b052020-06-02 10:07:41 +0000401 quota_name = "sdn_controllers"
tiernob24258a2018-10-04 18:39:49 +0200402 schema_new = sdn_new_schema
403 schema_edit = sdn_edit_schema
tierno65ca36d2019-02-12 19:27:52 +0100404 multiproject = True
tiernobdebce92019-07-01 15:36:49 +0000405 password_to_encrypt = "password"
tierno468aa242019-08-01 16:35:04 +0000406 config_to_encrypt = {}
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100407
tierno7adaeb02019-12-17 16:46:12 +0000408 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 Brydenc0aabf92020-07-27 13:55:11 +0000423 def _validate_input_edit(self, input, content, force=False):
424 input = super()._validate_input_edit(input, content, force)
tierno7adaeb02019-12-17 16:46:12 +0000425 return self._obtain_url(input, False)
426
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100427
delacruzramofe598fe2019-10-23 18:25:11 +0200428class 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"])
delacruzramoc2d5fc62020-02-05 11:50:21 +0000441 # 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]
delacruzramofe598fe2019-10-23 18:25:11 +0200450 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
delacruzramoc2d5fc62020-02-05 11:50:21 +0000460 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
tiernoe19707b2020-04-21 13:08:04 +0000476 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
delacruzramofe598fe2019-10-23 18:25:11 +0200494
495class 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
delacruzramoc2d5fc62020-02-05 11:50:21 +0000504 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
delacruzramofe598fe2019-10-23 18:25:11 +0200524
Felipe Vicensb66b0412020-05-06 10:11:00 +0200525class 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 Sousa5c01e192019-05-08 02:35:47 +0100534class UserTopicAuth(UserTopic):
tierno65ca36d2019-02-12 19:27:52 +0100535 # topic = "users"
536 # topic_msg = "users"
Eduardo Sousaa16a4fa2019-05-23 01:41:18 +0100537 schema_new = user_new_schema
538 schema_edit = user_edit_schema
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100539
540 def __init__(self, db, fs, msg, auth):
delacruzramo32bab472019-09-13 12:24:22 +0200541 UserTopic.__init__(self, db, fs, msg, auth)
542 # self.auth = auth
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100543
tierno65ca36d2019-02-12 19:27:52 +0100544 def check_conflict_on_new(self, session, indata):
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100545 """
546 Check that the data to be inserted is valid
547
tierno65ca36d2019-02-12 19:27:52 +0100548 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100549 :param indata: data to be inserted
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100550 :return: None or raises EngineException
551 """
552 username = indata.get("username")
tiernocf042d32019-06-13 09:06:40 +0000553 if is_valid_uuid(username):
delacruzramoceb8baf2019-06-21 14:25:38 +0200554 raise EngineException("username '{}' cannot have a uuid format".format(username),
tiernocf042d32019-06-13 09:06:40 +0000555 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 Sousa5c01e192019-05-08 02:35:47 +0100560
Eduardo Sousa339ed782019-05-28 14:25:00 +0100561 if "projects" in indata.keys():
tierno701018c2019-06-25 11:13:14 +0000562 # convert to new format project_role_mappings
delacruzramo01b15d32019-07-02 14:37:47 +0200563 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"]
tierno701018c2019-06-25 11:13:14 +0000569 if not indata.get("project_role_mappings"):
570 indata["project_role_mappings"] = []
571 for project in indata["projects"]:
delacruzramo01b15d32019-07-02 14:37:47 +0200572 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)
tierno701018c2019-06-25 11:13:14 +0000576 # raise EngineException("Format invalid: the keyword 'projects' is not allowed for keystone authentication",
577 # HTTPStatus.BAD_REQUEST)
Eduardo Sousa339ed782019-05-28 14:25:00 +0100578
tierno65ca36d2019-02-12 19:27:52 +0100579 def check_conflict_on_edit(self, session, final_content, edit_content, _id):
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100580 """
581 Check that the data to be edited/uploaded is valid
582
tierno65ca36d2019-02-12 19:27:52 +0100583 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100584 :param final_content: data once modified
585 :param edit_content: incremental data that contains the modifications to apply
586 :param _id: internal _id
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100587 :return: None or raises EngineException
588 """
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100589
tiernocf042d32019-06-13 09:06:40 +0000590 if "username" in edit_content:
591 username = edit_content.get("username")
592 if is_valid_uuid(username):
delacruzramoceb8baf2019-06-21 14:25:38 +0200593 raise EngineException("username '{}' cannot have an uuid format".format(username),
tiernocf042d32019-06-13 09:06:40 +0000594 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 Sousa5c01e192019-05-08 02:35:47 +0100606
tiernob4844ab2019-05-23 08:42:12 +0000607 def check_conflict_on_del(self, session, _id, db_content):
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100608 """
609 Check if deletion can be done because of dependencies if it is not force. To override
tierno65ca36d2019-02-12 19:27:52 +0100610 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100611 :param _id: internal _id
tiernob4844ab2019-05-23 08:42:12 +0000612 :param db_content: The database content of this item _id
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100613 :return: None if ok or raises EngineException with the conflict
614 """
tiernocf042d32019-06-13 09:06:40 +0000615 if db_content["username"] == session["username"]:
616 raise EngineException("You cannot delete your own login user ", http_code=HTTPStatus.CONFLICT)
delacruzramo01b15d32019-07-02 14:37:47 +0200617 # TODO: Check that user is not logged in ? How? (Would require listing current tokens)
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100618
Eduardo Sousaa16a4fa2019-05-23 01:41:18 +0100619 @staticmethod
620 def format_on_show(content):
621 """
Eduardo Sousa44603902019-06-04 08:10:32 +0100622 Modifies the content of the role information to separate the role
Eduardo Sousaa16a4fa2019-05-23 01:41:18 +0100623 metadata from the role definition.
624 """
625 project_role_mappings = []
626
delacruzramo01b15d32019-07-02 14:37:47 +0200627 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 Sousaa16a4fa2019-05-23 01:41:18 +0100635 content["project_role_mappings"] = project_role_mappings
636
Eduardo Sousa0b1d61b2019-05-30 19:55:52 +0100637 return content
638
tierno65ca36d2019-02-12 19:27:52 +0100639 def new(self, rollback, session, indata=None, kwargs=None, headers=None):
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100640 """
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
tierno65ca36d2019-02-12 19:27:52 +0100646 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100647 :param indata: data to be inserted
648 :param kwargs: used to override the indata descriptor
649 :param headers: http request headers
delacruzramo01b15d32019-07-02 14:37:47 +0200650 :return: _id: identity of the inserted data, operation _id (None)
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100651 """
652 try:
653 content = BaseTopic._remove_envelop(indata)
654
655 # Override descriptor with query string kwargs
656 BaseTopic._update_input_with_kwargs(content, kwargs)
tierno65ca36d2019-02-12 19:27:52 +0100657 content = self._validate_input_new(content, session["force"])
658 self.check_conflict_on_new(session, content)
tiernocf042d32019-06-13 09:06:40 +0000659 # self.format_on_new(content, session["project_id"], make_public=session["public"])
delacruzramo01b15d32019-07-02 14:37:47 +0200660 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 Sousa44603902019-06-04 08:10:32 +0100674
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100675 rollback.append({"topic": self.topic, "_id": _id})
tiernocf042d32019-06-13 09:06:40 +0000676 # del content["password"]
tiernobee3bad2019-12-05 12:26:01 +0000677 # self._send_msg("created", content, not_send_msg=not_send_msg)
delacruzramo01b15d32019-07-02 14:37:47 +0200678 return _id, None
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100679 except ValidationError as e:
680 raise EngineException(e, HTTPStatus.UNPROCESSABLE_ENTITY)
681
K Sai Kiran9e260642020-08-28 15:11:48 +0530682 def show(self, session, _id, api_req=False):
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100683 """
684 Get complete information on an topic
685
tierno65ca36d2019-02-12 19:27:52 +0100686 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
tierno5ec768a2020-03-31 09:46:44 +0000687 :param _id: server internal id or username
K Sai Kiran9e260642020-08-28 15:11:48 +0530688 :param api_req: True if this call is serving an external API request. False if serving internal request.
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100689 :return: dictionary, raise exception if not found.
690 """
tiernocf042d32019-06-13 09:06:40 +0000691 # Allow _id to be a name or uuid
tiernoad6d5332020-02-19 14:29:49 +0000692 filter_q = {"username": _id}
delacruzramo029405d2019-09-26 10:52:56 +0200693 # users = self.auth.get_user_list(filter_q)
694 users = self.list(session, filter_q) # To allow default filtering (Bug 853)
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100695 if len(users) == 1:
tierno1546f2a2019-08-20 15:38:11 +0000696 return users[0]
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100697 elif len(users) > 1:
tierno5ec768a2020-03-31 09:46:44 +0000698 raise EngineException("Too many users found for '{}'".format(_id), HTTPStatus.CONFLICT)
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100699 else:
tierno5ec768a2020-03-31 09:46:44 +0000700 raise EngineException("User '{}' not found".format(_id), HTTPStatus.NOT_FOUND)
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100701
tierno65ca36d2019-02-12 19:27:52 +0100702 def edit(self, session, _id, indata=None, kwargs=None, content=None):
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100703 """
704 Updates an user entry.
705
tierno65ca36d2019-02-12 19:27:52 +0100706 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100707 :param _id:
708 :param indata: data to be inserted
709 :param kwargs: used to override the indata descriptor
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100710 :param content:
711 :return: _id: identity of the inserted data.
712 """
713 indata = self._remove_envelop(indata)
714
715 # Override descriptor with query string kwargs
716 if kwargs:
717 BaseTopic._update_input_with_kwargs(indata, kwargs)
718 try:
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100719 if not content:
720 content = self.show(session, _id)
Frank Brydenc0aabf92020-07-27 13:55:11 +0000721 indata = self._validate_input_edit(indata, content, force=session["force"])
tierno65ca36d2019-02-12 19:27:52 +0100722 self.check_conflict_on_edit(session, content, indata, _id=_id)
tiernocf042d32019-06-13 09:06:40 +0000723 # self.format_on_edit(content, indata)
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100724
delacruzramo01b15d32019-07-02 14:37:47 +0200725 if not ("password" in indata or "username" in indata or indata.get("remove_project_role_mappings") or
726 indata.get("add_project_role_mappings") or indata.get("project_role_mappings") or
727 indata.get("projects") or indata.get("add_projects")):
tiernocf042d32019-06-13 09:06:40 +0000728 return _id
delacruzramo01b15d32019-07-02 14:37:47 +0200729 if indata.get("project_role_mappings") \
730 and (indata.get("remove_project_role_mappings") or indata.get("add_project_role_mappings")):
tiernocf042d32019-06-13 09:06:40 +0000731 raise EngineException("Option 'project_role_mappings' is incompatible with 'add_project_role_mappings"
732 "' or 'remove_project_role_mappings'", http_code=HTTPStatus.BAD_REQUEST)
Eduardo Sousa44603902019-06-04 08:10:32 +0100733
delacruzramo01b15d32019-07-02 14:37:47 +0200734 if indata.get("projects") or indata.get("add_projects"):
735 role = self.auth.get_role_list({"name": "project_admin"})
736 if not role:
737 role = self.auth.get_role_list()
738 if not role:
739 raise AuthconnNotFoundException("Can't find a default role for user '{}'"
740 .format(content["username"]))
741 rid = role[0]["_id"]
742 if "add_project_role_mappings" not in indata:
743 indata["add_project_role_mappings"] = []
tierno1546f2a2019-08-20 15:38:11 +0000744 if "remove_project_role_mappings" not in indata:
745 indata["remove_project_role_mappings"] = []
746 if isinstance(indata.get("projects"), dict):
747 # backward compatible
748 for k, v in indata["projects"].items():
749 if k.startswith("$") and v is None:
750 indata["remove_project_role_mappings"].append({"project": k[1:]})
751 elif k.startswith("$+"):
752 indata["add_project_role_mappings"].append({"project": v, "role": rid})
753 del indata["projects"]
delacruzramo01b15d32019-07-02 14:37:47 +0200754 for proj in indata.get("projects", []) + indata.get("add_projects", []):
755 indata["add_project_role_mappings"].append({"project": proj, "role": rid})
756
757 # user = self.show(session, _id) # Already in 'content'
758 original_mapping = content["project_role_mappings"]
Eduardo Sousa44603902019-06-04 08:10:32 +0100759
tiernocf042d32019-06-13 09:06:40 +0000760 mappings_to_add = []
761 mappings_to_remove = []
Eduardo Sousa44603902019-06-04 08:10:32 +0100762
tiernocf042d32019-06-13 09:06:40 +0000763 # remove
764 for to_remove in indata.get("remove_project_role_mappings", ()):
765 for mapping in original_mapping:
766 if to_remove["project"] in (mapping["project"], mapping["project_name"]):
767 if not to_remove.get("role") or to_remove["role"] in (mapping["role"], mapping["role_name"]):
768 mappings_to_remove.append(mapping)
Eduardo Sousa44603902019-06-04 08:10:32 +0100769
tiernocf042d32019-06-13 09:06:40 +0000770 # add
771 for to_add in indata.get("add_project_role_mappings", ()):
772 for mapping in original_mapping:
773 if to_add["project"] in (mapping["project"], mapping["project_name"]) and \
774 to_add["role"] in (mapping["role"], mapping["role_name"]):
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100775
tiernocf042d32019-06-13 09:06:40 +0000776 if mapping in mappings_to_remove: # do not remove
777 mappings_to_remove.remove(mapping)
778 break # do not add, it is already at user
779 else:
delacruzramo01b15d32019-07-02 14:37:47 +0200780 pid = self.auth.get_project(to_add["project"])["_id"]
781 rid = self.auth.get_role(to_add["role"])["_id"]
782 mappings_to_add.append({"project": pid, "role": rid})
tiernocf042d32019-06-13 09:06:40 +0000783
784 # set
785 if indata.get("project_role_mappings"):
786 for to_set in indata["project_role_mappings"]:
787 for mapping in original_mapping:
788 if to_set["project"] in (mapping["project"], mapping["project_name"]) and \
789 to_set["role"] in (mapping["role"], mapping["role_name"]):
tiernocf042d32019-06-13 09:06:40 +0000790 if mapping in mappings_to_remove: # do not remove
791 mappings_to_remove.remove(mapping)
792 break # do not add, it is already at user
793 else:
delacruzramo01b15d32019-07-02 14:37:47 +0200794 pid = self.auth.get_project(to_set["project"])["_id"]
795 rid = self.auth.get_role(to_set["role"])["_id"]
796 mappings_to_add.append({"project": pid, "role": rid})
tiernocf042d32019-06-13 09:06:40 +0000797 for mapping in original_mapping:
798 for to_set in indata["project_role_mappings"]:
799 if to_set["project"] in (mapping["project"], mapping["project_name"]) and \
800 to_set["role"] in (mapping["role"], mapping["role_name"]):
801 break
802 else:
803 # delete
804 if mapping not in mappings_to_remove: # do not remove
805 mappings_to_remove.append(mapping)
806
delacruzramo01b15d32019-07-02 14:37:47 +0200807 self.auth.update_user({"_id": _id, "username": indata.get("username"), "password": indata.get("password"),
808 "add_project_role_mappings": mappings_to_add,
809 "remove_project_role_mappings": mappings_to_remove
810 })
tiernocf042d32019-06-13 09:06:40 +0000811
delacruzramo01b15d32019-07-02 14:37:47 +0200812 # return _id
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100813 except ValidationError as e:
814 raise EngineException(e, HTTPStatus.UNPROCESSABLE_ENTITY)
815
tierno9ed90df2020-08-14 14:25:32 +0000816 def list(self, session, filter_q=None, api_req=False):
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100817 """
818 Get a list of the topic that matches a filter
tierno65ca36d2019-02-12 19:27:52 +0100819 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100820 :param filter_q: filter of data to be applied
K Sai Kiran9e260642020-08-28 15:11:48 +0530821 :param api_req: True if this call is serving an external API request. False if serving internal request.
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100822 :return: The list, it can be empty if no one match the filter.
823 """
delacruzramo029405d2019-09-26 10:52:56 +0200824 user_list = self.auth.get_user_list(filter_q)
825 if not session["allow_show_user_project_role"]:
826 # Bug 853 - Default filtering
827 user_list = [usr for usr in user_list if usr["username"] == session["username"]]
828 return user_list
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100829
tiernobee3bad2019-12-05 12:26:01 +0000830 def delete(self, session, _id, dry_run=False, not_send_msg=None):
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100831 """
832 Delete item by its internal _id
833
tierno65ca36d2019-02-12 19:27:52 +0100834 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100835 :param _id: server internal id
836 :param force: indicates if deletion must be forced in case of conflict
837 :param dry_run: make checking but do not delete
tiernobee3bad2019-12-05 12:26:01 +0000838 :param not_send_msg: To not send message (False) or store content (list) instead
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100839 :return: dictionary with deleted item _id. It raises EngineException on error: not found, conflict, ...
840 """
tiernocf042d32019-06-13 09:06:40 +0000841 # Allow _id to be a name or uuid
delacruzramo01b15d32019-07-02 14:37:47 +0200842 user = self.auth.get_user(_id)
843 uid = user["_id"]
844 self.check_conflict_on_del(session, uid, user)
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100845 if not dry_run:
delacruzramo01b15d32019-07-02 14:37:47 +0200846 v = self.auth.delete_user(uid)
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100847 return v
848 return None
849
850
851class ProjectTopicAuth(ProjectTopic):
tierno65ca36d2019-02-12 19:27:52 +0100852 # topic = "projects"
853 # topic_msg = "projects"
Eduardo Sousa44603902019-06-04 08:10:32 +0100854 schema_new = project_new_schema
855 schema_edit = project_edit_schema
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100856
857 def __init__(self, db, fs, msg, auth):
delacruzramo32bab472019-09-13 12:24:22 +0200858 ProjectTopic.__init__(self, db, fs, msg, auth)
859 # self.auth = auth
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100860
tierno65ca36d2019-02-12 19:27:52 +0100861 def check_conflict_on_new(self, session, indata):
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100862 """
863 Check that the data to be inserted is valid
864
tierno65ca36d2019-02-12 19:27:52 +0100865 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100866 :param indata: data to be inserted
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100867 :return: None or raises EngineException
868 """
tiernocf042d32019-06-13 09:06:40 +0000869 project_name = indata.get("name")
870 if is_valid_uuid(project_name):
delacruzramoceb8baf2019-06-21 14:25:38 +0200871 raise EngineException("project name '{}' cannot have an uuid format".format(project_name),
tiernocf042d32019-06-13 09:06:40 +0000872 HTTPStatus.UNPROCESSABLE_ENTITY)
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100873
tiernocf042d32019-06-13 09:06:40 +0000874 project_list = self.auth.get_project_list(filter_q={"name": project_name})
875
876 if project_list:
877 raise EngineException("project '{}' exists".format(project_name), HTTPStatus.CONFLICT)
878
879 def check_conflict_on_edit(self, session, final_content, edit_content, _id):
880 """
881 Check that the data to be edited/uploaded is valid
882
883 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
884 :param final_content: data once modified
885 :param edit_content: incremental data that contains the modifications to apply
886 :param _id: internal _id
887 :return: None or raises EngineException
888 """
889
890 project_name = edit_content.get("name")
delacruzramo01b15d32019-07-02 14:37:47 +0200891 if project_name != final_content["name"]: # It is a true renaming
tiernocf042d32019-06-13 09:06:40 +0000892 if is_valid_uuid(project_name):
delacruzramo79e40f42019-10-10 16:36:40 +0200893 raise EngineException("project name '{}' cannot have an uuid format".format(project_name),
tiernocf042d32019-06-13 09:06:40 +0000894 HTTPStatus.UNPROCESSABLE_ENTITY)
895
delacruzramo01b15d32019-07-02 14:37:47 +0200896 if final_content["name"] == "admin":
897 raise EngineException("You cannot rename project 'admin'", http_code=HTTPStatus.CONFLICT)
898
tiernocf042d32019-06-13 09:06:40 +0000899 # Check that project name is not used, regardless keystone already checks this
delacruzramo32bab472019-09-13 12:24:22 +0200900 if project_name and self.auth.get_project_list(filter_q={"name": project_name}):
tiernocf042d32019-06-13 09:06:40 +0000901 raise EngineException("project '{}' is already used".format(project_name), HTTPStatus.CONFLICT)
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100902
tiernob4844ab2019-05-23 08:42:12 +0000903 def check_conflict_on_del(self, session, _id, db_content):
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100904 """
905 Check if deletion can be done because of dependencies if it is not force. To override
906
tierno65ca36d2019-02-12 19:27:52 +0100907 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100908 :param _id: internal _id
tiernob4844ab2019-05-23 08:42:12 +0000909 :param db_content: The database content of this item _id
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100910 :return: None if ok or raises EngineException with the conflict
911 """
delacruzramo01b15d32019-07-02 14:37:47 +0200912
913 def check_rw_projects(topic, title, id_field):
914 for desc in self.db.get_list(topic):
915 if _id in desc["_admin"]["projects_read"] + desc["_admin"]["projects_write"]:
916 raise EngineException("Project '{}' ({}) is being used by {} '{}'"
917 .format(db_content["name"], _id, title, desc[id_field]), HTTPStatus.CONFLICT)
918
919 if _id in session["project_id"]:
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100920 raise EngineException("You cannot delete your own project", http_code=HTTPStatus.CONFLICT)
921
delacruzramo01b15d32019-07-02 14:37:47 +0200922 if db_content["name"] == "admin":
923 raise EngineException("You cannot delete project 'admin'", http_code=HTTPStatus.CONFLICT)
924
925 # If any user is using this project, raise CONFLICT exception
926 if not session["force"]:
927 for user in self.auth.get_user_list():
tierno1546f2a2019-08-20 15:38:11 +0000928 for prm in user.get("project_role_mappings"):
929 if prm["project"] == _id:
930 raise EngineException("Project '{}' ({}) is being used by user '{}'"
931 .format(db_content["name"], _id, user["username"]), HTTPStatus.CONFLICT)
delacruzramo01b15d32019-07-02 14:37:47 +0200932
933 # If any VNFD, NSD, NST, PDU, etc. is using this project, raise CONFLICT exception
934 if not session["force"]:
935 check_rw_projects("vnfds", "VNF Descriptor", "id")
936 check_rw_projects("nsds", "NS Descriptor", "id")
937 check_rw_projects("nsts", "NS Template", "id")
938 check_rw_projects("pdus", "PDU Descriptor", "name")
939
tierno65ca36d2019-02-12 19:27:52 +0100940 def new(self, rollback, session, indata=None, kwargs=None, headers=None):
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100941 """
942 Creates a new entry into the authentication backend.
943
944 NOTE: Overrides BaseTopic functionality because it doesn't require access to database.
945
946 :param rollback: list to append created items at database in case a rollback may to be done
tierno65ca36d2019-02-12 19:27:52 +0100947 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100948 :param indata: data to be inserted
949 :param kwargs: used to override the indata descriptor
950 :param headers: http request headers
delacruzramo01b15d32019-07-02 14:37:47 +0200951 :return: _id: identity of the inserted data, operation _id (None)
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100952 """
953 try:
954 content = BaseTopic._remove_envelop(indata)
955
956 # Override descriptor with query string kwargs
957 BaseTopic._update_input_with_kwargs(content, kwargs)
tierno65ca36d2019-02-12 19:27:52 +0100958 content = self._validate_input_new(content, session["force"])
959 self.check_conflict_on_new(session, content)
960 self.format_on_new(content, project_id=session["project_id"], make_public=session["public"])
delacruzramo01b15d32019-07-02 14:37:47 +0200961 _id = self.auth.create_project(content)
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100962 rollback.append({"topic": self.topic, "_id": _id})
tiernobee3bad2019-12-05 12:26:01 +0000963 # self._send_msg("created", content, not_send_msg=not_send_msg)
delacruzramo01b15d32019-07-02 14:37:47 +0200964 return _id, None
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100965 except ValidationError as e:
966 raise EngineException(e, HTTPStatus.UNPROCESSABLE_ENTITY)
967
K Sai Kiran9e260642020-08-28 15:11:48 +0530968 def show(self, session, _id, api_req=False):
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100969 """
970 Get complete information on an topic
971
tierno65ca36d2019-02-12 19:27:52 +0100972 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100973 :param _id: server internal id
K Sai Kiran9e260642020-08-28 15:11:48 +0530974 :param api_req: True if this call is serving an external API request. False if serving internal request.
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100975 :return: dictionary, raise exception if not found.
976 """
tiernocf042d32019-06-13 09:06:40 +0000977 # Allow _id to be a name or uuid
978 filter_q = {self.id_field(self.topic, _id): _id}
delacruzramo029405d2019-09-26 10:52:56 +0200979 # projects = self.auth.get_project_list(filter_q=filter_q)
980 projects = self.list(session, filter_q) # To allow default filtering (Bug 853)
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100981 if len(projects) == 1:
982 return projects[0]
983 elif len(projects) > 1:
984 raise EngineException("Too many projects found", HTTPStatus.CONFLICT)
985 else:
986 raise EngineException("Project not found", HTTPStatus.NOT_FOUND)
987
tierno9ed90df2020-08-14 14:25:32 +0000988 def list(self, session, filter_q=None, api_req=False):
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100989 """
990 Get a list of the topic that matches a filter
991
tierno65ca36d2019-02-12 19:27:52 +0100992 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100993 :param filter_q: filter of data to be applied
994 :return: The list, it can be empty if no one match the filter.
995 """
delacruzramo029405d2019-09-26 10:52:56 +0200996 project_list = self.auth.get_project_list(filter_q)
997 if not session["allow_show_user_project_role"]:
998 # Bug 853 - Default filtering
999 user = self.auth.get_user(session["username"])
1000 projects = [prm["project"] for prm in user["project_role_mappings"]]
1001 project_list = [proj for proj in project_list if proj["_id"] in projects]
1002 return project_list
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001003
tiernobee3bad2019-12-05 12:26:01 +00001004 def delete(self, session, _id, dry_run=False, not_send_msg=None):
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001005 """
1006 Delete item by its internal _id
1007
tierno65ca36d2019-02-12 19:27:52 +01001008 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001009 :param _id: server internal id
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001010 :param dry_run: make checking but do not delete
tiernobee3bad2019-12-05 12:26:01 +00001011 :param not_send_msg: To not send message (False) or store content (list) instead
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001012 :return: dictionary with deleted item _id. It raises EngineException on error: not found, conflict, ...
1013 """
tiernocf042d32019-06-13 09:06:40 +00001014 # Allow _id to be a name or uuid
delacruzramo01b15d32019-07-02 14:37:47 +02001015 proj = self.auth.get_project(_id)
1016 pid = proj["_id"]
1017 self.check_conflict_on_del(session, pid, proj)
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001018 if not dry_run:
delacruzramo01b15d32019-07-02 14:37:47 +02001019 v = self.auth.delete_project(pid)
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001020 return v
1021 return None
1022
tierno4015b472019-06-10 13:57:29 +00001023 def edit(self, session, _id, indata=None, kwargs=None, content=None):
1024 """
1025 Updates a project entry.
1026
1027 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
1028 :param _id:
1029 :param indata: data to be inserted
1030 :param kwargs: used to override the indata descriptor
1031 :param content:
1032 :return: _id: identity of the inserted data.
1033 """
1034 indata = self._remove_envelop(indata)
1035
1036 # Override descriptor with query string kwargs
1037 if kwargs:
1038 BaseTopic._update_input_with_kwargs(indata, kwargs)
1039 try:
tierno4015b472019-06-10 13:57:29 +00001040 if not content:
1041 content = self.show(session, _id)
Frank Brydenc0aabf92020-07-27 13:55:11 +00001042 indata = self._validate_input_edit(indata, content, force=session["force"])
tierno4015b472019-06-10 13:57:29 +00001043 self.check_conflict_on_edit(session, content, indata, _id=_id)
delacruzramo01b15d32019-07-02 14:37:47 +02001044 self.format_on_edit(content, indata)
tierno4015b472019-06-10 13:57:29 +00001045
delacruzramo32bab472019-09-13 12:24:22 +02001046 deep_update_rfc7396(content, indata)
delacruzramo01b15d32019-07-02 14:37:47 +02001047 self.auth.update_project(content["_id"], content)
tierno4015b472019-06-10 13:57:29 +00001048 except ValidationError as e:
1049 raise EngineException(e, HTTPStatus.UNPROCESSABLE_ENTITY)
1050
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001051
1052class RoleTopicAuth(BaseTopic):
delacruzramoceb8baf2019-06-21 14:25:38 +02001053 topic = "roles"
1054 topic_msg = None # "roles"
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001055 schema_new = roles_new_schema
1056 schema_edit = roles_edit_schema
tierno65ca36d2019-02-12 19:27:52 +01001057 multiproject = False
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001058
tierno9e87a7f2020-03-23 09:24:10 +00001059 def __init__(self, db, fs, msg, auth):
delacruzramo32bab472019-09-13 12:24:22 +02001060 BaseTopic.__init__(self, db, fs, msg, auth)
1061 # self.auth = auth
tierno9e87a7f2020-03-23 09:24:10 +00001062 self.operations = auth.role_permissions
delacruzramo01b15d32019-07-02 14:37:47 +02001063 # self.topic = "roles_operations" if isinstance(auth, AuthconnKeystone) else "roles"
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001064
1065 @staticmethod
1066 def validate_role_definition(operations, role_definitions):
1067 """
1068 Validates the role definition against the operations defined in
1069 the resources to operations files.
1070
1071 :param operations: operations list
1072 :param role_definitions: role definition to test
1073 :return: None if ok, raises ValidationError exception on error
1074 """
tierno1f029d82019-06-13 22:37:04 +00001075 if not role_definitions.get("permissions"):
1076 return
1077 ignore_fields = ["admin", "default"]
1078 for role_def in role_definitions["permissions"].keys():
Eduardo Sousa37de0912019-05-23 02:17:22 +01001079 if role_def in ignore_fields:
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001080 continue
Eduardo Sousac7689372019-06-04 16:01:46 +01001081 if role_def[-1] == ":":
tierno1f029d82019-06-13 22:37:04 +00001082 raise ValidationError("Operation cannot end with ':'")
Eduardo Sousac5a18892019-06-06 14:51:23 +01001083
tiernobce14602020-08-04 12:48:15 +00001084 match = next((op for op in operations if op == role_def or op.startswith(role_def + ":")), None)
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001085
tiernobce14602020-08-04 12:48:15 +00001086 if not match:
tierno1f029d82019-06-13 22:37:04 +00001087 raise ValidationError("Invalid permission '{}'".format(role_def))
Eduardo Sousa37de0912019-05-23 02:17:22 +01001088
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001089 def _validate_input_new(self, input, force=False):
1090 """
1091 Validates input user content for a new entry.
1092
1093 :param input: user input content for the new topic
1094 :param force: may be used for being more tolerant
1095 :return: The same input content, or a changed version of it.
1096 """
1097 if self.schema_new:
1098 validate_input(input, self.schema_new)
Eduardo Sousa37de0912019-05-23 02:17:22 +01001099 self.validate_role_definition(self.operations, input)
Eduardo Sousac4650362019-06-04 13:24:22 +01001100
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001101 return input
1102
Frank Brydenc0aabf92020-07-27 13:55:11 +00001103 def _validate_input_edit(self, input, content, force=False):
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001104 """
1105 Validates input user content for updating an entry.
1106
1107 :param input: user input content for the new topic
1108 :param force: may be used for being more tolerant
1109 :return: The same input content, or a changed version of it.
1110 """
1111 if self.schema_edit:
1112 validate_input(input, self.schema_edit)
Eduardo Sousa37de0912019-05-23 02:17:22 +01001113 self.validate_role_definition(self.operations, input)
Eduardo Sousac4650362019-06-04 13:24:22 +01001114
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001115 return input
1116
tierno65ca36d2019-02-12 19:27:52 +01001117 def check_conflict_on_new(self, session, indata):
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001118 """
1119 Check that the data to be inserted is valid
1120
tierno65ca36d2019-02-12 19:27:52 +01001121 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001122 :param indata: data to be inserted
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001123 :return: None or raises EngineException
1124 """
delacruzramo79e40f42019-10-10 16:36:40 +02001125 # check name is not uuid
1126 role_name = indata.get("name")
1127 if is_valid_uuid(role_name):
1128 raise EngineException("role name '{}' cannot have an uuid format".format(role_name),
1129 HTTPStatus.UNPROCESSABLE_ENTITY)
tierno1f029d82019-06-13 22:37:04 +00001130 # check name not exists
delacruzramo01b15d32019-07-02 14:37:47 +02001131 name = indata["name"]
1132 # if self.db.get_one(self.topic, {"name": indata.get("name")}, fail_on_empty=False, fail_on_more=False):
1133 if self.auth.get_role_list({"name": name}):
1134 raise EngineException("role name '{}' exists".format(name), HTTPStatus.CONFLICT)
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001135
tierno65ca36d2019-02-12 19:27:52 +01001136 def check_conflict_on_edit(self, session, final_content, edit_content, _id):
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001137 """
1138 Check that the data to be edited/uploaded is valid
1139
tierno65ca36d2019-02-12 19:27:52 +01001140 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001141 :param final_content: data once modified
1142 :param edit_content: incremental data that contains the modifications to apply
1143 :param _id: internal _id
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001144 :return: None or raises EngineException
1145 """
tierno1f029d82019-06-13 22:37:04 +00001146 if "default" not in final_content["permissions"]:
1147 final_content["permissions"]["default"] = False
1148 if "admin" not in final_content["permissions"]:
1149 final_content["permissions"]["admin"] = False
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001150
delacruzramo79e40f42019-10-10 16:36:40 +02001151 # check name is not uuid
1152 role_name = edit_content.get("name")
1153 if is_valid_uuid(role_name):
1154 raise EngineException("role name '{}' cannot have an uuid format".format(role_name),
1155 HTTPStatus.UNPROCESSABLE_ENTITY)
1156
1157 # Check renaming of admin roles
1158 role = self.auth.get_role(_id)
1159 if role["name"] in ["system_admin", "project_admin"]:
1160 raise EngineException("You cannot rename role '{}'".format(role["name"]), http_code=HTTPStatus.FORBIDDEN)
1161
tierno1f029d82019-06-13 22:37:04 +00001162 # check name not exists
1163 if "name" in edit_content:
1164 role_name = edit_content["name"]
delacruzramo01b15d32019-07-02 14:37:47 +02001165 # if self.db.get_one(self.topic, {"name":role_name,"_id.ne":_id}, fail_on_empty=False, fail_on_more=False):
1166 roles = self.auth.get_role_list({"name": role_name})
1167 if roles and roles[0][BaseTopic.id_field("roles", _id)] != _id:
tierno1f029d82019-06-13 22:37:04 +00001168 raise EngineException("role name '{}' exists".format(role_name), HTTPStatus.CONFLICT)
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001169
tiernob4844ab2019-05-23 08:42:12 +00001170 def check_conflict_on_del(self, session, _id, db_content):
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001171 """
1172 Check if deletion can be done because of dependencies if it is not force. To override
1173
tierno65ca36d2019-02-12 19:27:52 +01001174 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001175 :param _id: internal _id
tiernob4844ab2019-05-23 08:42:12 +00001176 :param db_content: The database content of this item _id
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001177 :return: None if ok or raises EngineException with the conflict
1178 """
delacruzramo01b15d32019-07-02 14:37:47 +02001179 role = self.auth.get_role(_id)
1180 if role["name"] in ["system_admin", "project_admin"]:
1181 raise EngineException("You cannot delete role '{}'".format(role["name"]), http_code=HTTPStatus.FORBIDDEN)
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001182
delacruzramo01b15d32019-07-02 14:37:47 +02001183 # If any user is using this role, raise CONFLICT exception
delacruzramoad682a52019-12-10 16:26:34 +01001184 if not session["force"]:
1185 for user in self.auth.get_user_list():
1186 for prm in user.get("project_role_mappings"):
1187 if prm["role"] == _id:
1188 raise EngineException("Role '{}' ({}) is being used by user '{}'"
1189 .format(role["name"], _id, user["username"]), HTTPStatus.CONFLICT)
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001190
1191 @staticmethod
delacruzramo01b15d32019-07-02 14:37:47 +02001192 def format_on_new(content, project_id=None, make_public=False): # TO BE REMOVED ?
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001193 """
1194 Modifies content descriptor to include _admin
1195
1196 :param content: descriptor to be modified
1197 :param project_id: if included, it add project read/write permissions
1198 :param make_public: if included it is generated as public for reading.
1199 :return: None, but content is modified
1200 """
1201 now = time()
1202 if "_admin" not in content:
1203 content["_admin"] = {}
1204 if not content["_admin"].get("created"):
1205 content["_admin"]["created"] = now
1206 content["_admin"]["modified"] = now
Eduardo Sousac4650362019-06-04 13:24:22 +01001207
tierno1f029d82019-06-13 22:37:04 +00001208 if "permissions" not in content:
1209 content["permissions"] = {}
Eduardo Sousac4650362019-06-04 13:24:22 +01001210
tierno1f029d82019-06-13 22:37:04 +00001211 if "default" not in content["permissions"]:
1212 content["permissions"]["default"] = False
1213 if "admin" not in content["permissions"]:
1214 content["permissions"]["admin"] = False
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001215
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001216 @staticmethod
1217 def format_on_edit(final_content, edit_content):
1218 """
1219 Modifies final_content descriptor to include the modified date.
1220
1221 :param final_content: final descriptor generated
1222 :param edit_content: alterations to be include
1223 :return: None, but final_content is modified
1224 """
delacruzramo01b15d32019-07-02 14:37:47 +02001225 if "_admin" in final_content:
1226 final_content["_admin"]["modified"] = time()
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001227
tierno1f029d82019-06-13 22:37:04 +00001228 if "permissions" not in final_content:
1229 final_content["permissions"] = {}
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001230
tierno1f029d82019-06-13 22:37:04 +00001231 if "default" not in final_content["permissions"]:
1232 final_content["permissions"]["default"] = False
1233 if "admin" not in final_content["permissions"]:
1234 final_content["permissions"]["admin"] = False
tiernobdebce92019-07-01 15:36:49 +00001235 return None
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001236
K Sai Kiran9e260642020-08-28 15:11:48 +05301237 def show(self, session, _id, api_req=False):
delacruzramo01b15d32019-07-02 14:37:47 +02001238 """
1239 Get complete information on an topic
Eduardo Sousac4650362019-06-04 13:24:22 +01001240
delacruzramo01b15d32019-07-02 14:37:47 +02001241 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
1242 :param _id: server internal id
K Sai Kiran9e260642020-08-28 15:11:48 +05301243 :param api_req: True if this call is serving an external API request. False if serving internal request.
delacruzramo01b15d32019-07-02 14:37:47 +02001244 :return: dictionary, raise exception if not found.
1245 """
1246 filter_q = {BaseTopic.id_field(self.topic, _id): _id}
delacruzramo029405d2019-09-26 10:52:56 +02001247 # roles = self.auth.get_role_list(filter_q)
1248 roles = self.list(session, filter_q) # To allow default filtering (Bug 853)
delacruzramo01b15d32019-07-02 14:37:47 +02001249 if not roles:
1250 raise AuthconnNotFoundException("Not found any role with filter {}".format(filter_q))
1251 elif len(roles) > 1:
1252 raise AuthconnConflictException("Found more than one role with filter {}".format(filter_q))
1253 return roles[0]
1254
tierno9ed90df2020-08-14 14:25:32 +00001255 def list(self, session, filter_q=None, api_req=False):
delacruzramo01b15d32019-07-02 14:37:47 +02001256 """
1257 Get a list of the topic that matches a filter
1258
1259 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
1260 :param filter_q: filter of data to be applied
1261 :return: The list, it can be empty if no one match the filter.
1262 """
delacruzramo029405d2019-09-26 10:52:56 +02001263 role_list = self.auth.get_role_list(filter_q)
1264 if not session["allow_show_user_project_role"]:
1265 # Bug 853 - Default filtering
1266 user = self.auth.get_user(session["username"])
1267 roles = [prm["role"] for prm in user["project_role_mappings"]]
1268 role_list = [role for role in role_list if role["_id"] in roles]
1269 return role_list
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001270
tierno65ca36d2019-02-12 19:27:52 +01001271 def new(self, rollback, session, indata=None, kwargs=None, headers=None):
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001272 """
1273 Creates a new entry into database.
1274
1275 :param rollback: list to append created items at database in case a rollback may to be done
tierno65ca36d2019-02-12 19:27:52 +01001276 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001277 :param indata: data to be inserted
1278 :param kwargs: used to override the indata descriptor
1279 :param headers: http request headers
delacruzramo01b15d32019-07-02 14:37:47 +02001280 :return: _id: identity of the inserted data, operation _id (None)
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001281 """
1282 try:
tierno1f029d82019-06-13 22:37:04 +00001283 content = self._remove_envelop(indata)
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001284
1285 # Override descriptor with query string kwargs
tierno1f029d82019-06-13 22:37:04 +00001286 self._update_input_with_kwargs(content, kwargs)
tierno65ca36d2019-02-12 19:27:52 +01001287 content = self._validate_input_new(content, session["force"])
1288 self.check_conflict_on_new(session, content)
1289 self.format_on_new(content, project_id=session["project_id"], make_public=session["public"])
delacruzramo01b15d32019-07-02 14:37:47 +02001290 # role_name = content["name"]
1291 rid = self.auth.create_role(content)
1292 content["_id"] = rid
1293 # _id = self.db.create(self.topic, content)
1294 rollback.append({"topic": self.topic, "_id": rid})
tiernobee3bad2019-12-05 12:26:01 +00001295 # self._send_msg("created", content, not_send_msg=not_send_msg)
delacruzramo01b15d32019-07-02 14:37:47 +02001296 return rid, None
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001297 except ValidationError as e:
1298 raise EngineException(e, HTTPStatus.UNPROCESSABLE_ENTITY)
1299
tiernobee3bad2019-12-05 12:26:01 +00001300 def delete(self, session, _id, dry_run=False, not_send_msg=None):
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001301 """
1302 Delete item by its internal _id
1303
tierno65ca36d2019-02-12 19:27:52 +01001304 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001305 :param _id: server internal id
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001306 :param dry_run: make checking but do not delete
tiernobee3bad2019-12-05 12:26:01 +00001307 :param not_send_msg: To not send message (False) or store content (list) instead
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001308 :return: dictionary with deleted item _id. It raises EngineException on error: not found, conflict, ...
1309 """
delacruzramo01b15d32019-07-02 14:37:47 +02001310 filter_q = {BaseTopic.id_field(self.topic, _id): _id}
1311 roles = self.auth.get_role_list(filter_q)
1312 if not roles:
1313 raise AuthconnNotFoundException("Not found any role with filter {}".format(filter_q))
1314 elif len(roles) > 1:
1315 raise AuthconnConflictException("Found more than one role with filter {}".format(filter_q))
1316 rid = roles[0]["_id"]
1317 self.check_conflict_on_del(session, rid, None)
delacruzramoceb8baf2019-06-21 14:25:38 +02001318 # filter_q = {"_id": _id}
delacruzramo01b15d32019-07-02 14:37:47 +02001319 # filter_q = {BaseTopic.id_field(self.topic, _id): _id} # To allow role addressing by name
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001320 if not dry_run:
delacruzramo01b15d32019-07-02 14:37:47 +02001321 v = self.auth.delete_role(rid)
1322 # v = self.db.del_one(self.topic, filter_q)
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001323 return v
1324 return None
1325
tierno65ca36d2019-02-12 19:27:52 +01001326 def edit(self, session, _id, indata=None, kwargs=None, content=None):
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001327 """
1328 Updates a role entry.
1329
tierno65ca36d2019-02-12 19:27:52 +01001330 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001331 :param _id:
1332 :param indata: data to be inserted
1333 :param kwargs: used to override the indata descriptor
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001334 :param content:
1335 :return: _id: identity of the inserted data.
1336 """
delacruzramo01b15d32019-07-02 14:37:47 +02001337 if kwargs:
1338 self._update_input_with_kwargs(indata, kwargs)
1339 try:
delacruzramo01b15d32019-07-02 14:37:47 +02001340 if not content:
1341 content = self.show(session, _id)
Frank Brydenc0aabf92020-07-27 13:55:11 +00001342 indata = self._validate_input_edit(indata, content, force=session["force"])
delacruzramo01b15d32019-07-02 14:37:47 +02001343 deep_update_rfc7396(content, indata)
1344 self.check_conflict_on_edit(session, content, indata, _id=_id)
1345 self.format_on_edit(content, indata)
1346 self.auth.update_role(content)
1347 except ValidationError as e:
1348 raise EngineException(e, HTTPStatus.UNPROCESSABLE_ENTITY)