blob: 540a5d98c36c70bc3f64ae5c78463283f5c96693 [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
garciadeblas4568a372021-03-24 09:19:48 +010021from osm_nbi.validation import (
22 user_new_schema,
23 user_edit_schema,
24 project_new_schema,
25 project_edit_schema,
26 vim_account_new_schema,
27 vim_account_edit_schema,
28 sdn_new_schema,
29 sdn_edit_schema,
30 wim_account_new_schema,
31 wim_account_edit_schema,
32 roles_new_schema,
33 roles_edit_schema,
34 k8scluster_new_schema,
35 k8scluster_edit_schema,
36 k8srepo_new_schema,
37 k8srepo_edit_schema,
38 vca_new_schema,
39 vca_edit_schema,
40 osmrepo_new_schema,
41 osmrepo_edit_schema,
42 validate_input,
43 ValidationError,
44 is_valid_uuid,
45) # To check that User/Project Names don't look like UUIDs
tierno23acf402019-08-28 13:36:34 +000046from osm_nbi.base_topic import BaseTopic, EngineException
47from osm_nbi.authconn import AuthconnNotFoundException, AuthconnConflictException
delacruzramo01b15d32019-07-02 14:37:47 +020048from osm_common.dbbase import deep_update_rfc7396
agarwalat53471982020-10-08 13:06:14 +000049import copy
tiernob24258a2018-10-04 18:39:49 +020050
51__author__ = "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>"
52
53
54class UserTopic(BaseTopic):
55 topic = "users"
56 topic_msg = "users"
57 schema_new = user_new_schema
58 schema_edit = user_edit_schema
tierno65ca36d2019-02-12 19:27:52 +010059 multiproject = False
tiernob24258a2018-10-04 18:39:49 +020060
delacruzramo32bab472019-09-13 12:24:22 +020061 def __init__(self, db, fs, msg, auth):
62 BaseTopic.__init__(self, db, fs, msg, auth)
tiernob24258a2018-10-04 18:39:49 +020063
64 @staticmethod
tierno65ca36d2019-02-12 19:27:52 +010065 def _get_project_filter(session):
tiernob24258a2018-10-04 18:39:49 +020066 """
67 Generates a filter dictionary for querying database users.
68 Current policy is admin can show all, non admin, only its own user.
tierno65ca36d2019-02-12 19:27:52 +010069 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
tiernob24258a2018-10-04 18:39:49 +020070 :return:
71 """
72 if session["admin"]: # allows all
73 return {}
74 else:
Adurti844f6482024-11-01 06:32:13 +000075 return {"_id": session["user_id"]}
tiernob24258a2018-10-04 18:39:49 +020076
tierno65ca36d2019-02-12 19:27:52 +010077 def check_conflict_on_new(self, session, indata):
tiernob24258a2018-10-04 18:39:49 +020078 # check username not exists
garciadeblas4568a372021-03-24 09:19:48 +010079 if self.db.get_one(
80 self.topic,
81 {"username": indata.get("username")},
82 fail_on_empty=False,
83 fail_on_more=False,
84 ):
85 raise EngineException(
86 "username '{}' exists".format(indata["username"]), HTTPStatus.CONFLICT
87 )
tiernob24258a2018-10-04 18:39:49 +020088 # check projects
tierno65ca36d2019-02-12 19:27:52 +010089 if not session["force"]:
delacruzramoceb8baf2019-06-21 14:25:38 +020090 for p in indata.get("projects") or []:
delacruzramoc061f562019-04-05 11:00:02 +020091 # To allow project addressing by Name as well as ID
garciadeblas4568a372021-03-24 09:19:48 +010092 if not self.db.get_one(
93 "projects",
94 {BaseTopic.id_field("projects", p): p},
95 fail_on_empty=False,
96 fail_on_more=False,
97 ):
98 raise EngineException(
99 "project '{}' does not exist".format(p), HTTPStatus.CONFLICT
100 )
tiernob24258a2018-10-04 18:39:49 +0200101
tiernob4844ab2019-05-23 08:42:12 +0000102 def check_conflict_on_del(self, session, _id, db_content):
103 """
104 Check if deletion can be done because of dependencies if it is not force. To override
105 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
106 :param _id: internal _id
107 :param db_content: The database content of this item _id
108 :return: None if ok or raises EngineException with the conflict
109 """
tiernob24258a2018-10-04 18:39:49 +0200110 if _id == session["username"]:
garciadeblas4568a372021-03-24 09:19:48 +0100111 raise EngineException(
112 "You cannot delete your own user", http_code=HTTPStatus.CONFLICT
113 )
tiernob24258a2018-10-04 18:39:49 +0200114
115 @staticmethod
116 def format_on_new(content, project_id=None, make_public=False):
117 BaseTopic.format_on_new(content, make_public=False)
delacruzramoc061f562019-04-05 11:00:02 +0200118 # Removed so that the UUID is kept, to allow User Name modification
119 # content["_id"] = content["username"]
tiernob24258a2018-10-04 18:39:49 +0200120 salt = uuid4().hex
121 content["_admin"]["salt"] = salt
122 if content.get("password"):
garciadeblas4568a372021-03-24 09:19:48 +0100123 content["password"] = sha256(
124 content["password"].encode("utf-8") + salt.encode("utf-8")
125 ).hexdigest()
Eduardo Sousa339ed782019-05-28 14:25:00 +0100126 if content.get("project_role_mappings"):
garciadeblas4568a372021-03-24 09:19:48 +0100127 projects = [
128 mapping["project"] for mapping in content["project_role_mappings"]
129 ]
Eduardo Sousa339ed782019-05-28 14:25:00 +0100130
131 if content.get("projects"):
132 content["projects"] += projects
133 else:
134 content["projects"] = projects
tiernob24258a2018-10-04 18:39:49 +0200135
136 @staticmethod
137 def format_on_edit(final_content, edit_content):
138 BaseTopic.format_on_edit(final_content, edit_content)
139 if edit_content.get("password"):
140 salt = uuid4().hex
141 final_content["_admin"]["salt"] = salt
garciadeblas4568a372021-03-24 09:19:48 +0100142 final_content["password"] = sha256(
143 edit_content["password"].encode("utf-8") + salt.encode("utf-8")
144 ).hexdigest()
tiernobdebce92019-07-01 15:36:49 +0000145 return None
tiernob24258a2018-10-04 18:39:49 +0200146
tierno65ca36d2019-02-12 19:27:52 +0100147 def edit(self, session, _id, indata=None, kwargs=None, content=None):
tiernob24258a2018-10-04 18:39:49 +0200148 if not session["admin"]:
garciadeblas4568a372021-03-24 09:19:48 +0100149 raise EngineException(
150 "needed admin privileges", http_code=HTTPStatus.UNAUTHORIZED
151 )
delacruzramoc061f562019-04-05 11:00:02 +0200152 # Names that look like UUIDs are not allowed
153 name = (indata if indata else kwargs).get("username")
154 if is_valid_uuid(name):
garciadeblas4568a372021-03-24 09:19:48 +0100155 raise EngineException(
156 "Usernames that look like UUIDs are not allowed",
157 http_code=HTTPStatus.UNPROCESSABLE_ENTITY,
158 )
159 return BaseTopic.edit(
160 self, session, _id, indata=indata, kwargs=kwargs, content=content
161 )
tiernob24258a2018-10-04 18:39:49 +0200162
tierno65ca36d2019-02-12 19:27:52 +0100163 def new(self, rollback, session, indata=None, kwargs=None, headers=None):
tiernob24258a2018-10-04 18:39:49 +0200164 if not session["admin"]:
garciadeblas4568a372021-03-24 09:19:48 +0100165 raise EngineException(
166 "needed admin privileges", http_code=HTTPStatus.UNAUTHORIZED
167 )
delacruzramoc061f562019-04-05 11:00:02 +0200168 # Names that look like UUIDs are not allowed
169 name = indata["username"] if indata else kwargs["username"]
170 if is_valid_uuid(name):
garciadeblas4568a372021-03-24 09:19:48 +0100171 raise EngineException(
172 "Usernames that look like UUIDs are not allowed",
173 http_code=HTTPStatus.UNPROCESSABLE_ENTITY,
174 )
175 return BaseTopic.new(
176 self, rollback, session, indata=indata, kwargs=kwargs, headers=headers
177 )
tiernob24258a2018-10-04 18:39:49 +0200178
179
180class ProjectTopic(BaseTopic):
181 topic = "projects"
182 topic_msg = "projects"
183 schema_new = project_new_schema
184 schema_edit = project_edit_schema
tierno65ca36d2019-02-12 19:27:52 +0100185 multiproject = False
tiernob24258a2018-10-04 18:39:49 +0200186
delacruzramo32bab472019-09-13 12:24:22 +0200187 def __init__(self, db, fs, msg, auth):
188 BaseTopic.__init__(self, db, fs, msg, auth)
tiernob24258a2018-10-04 18:39:49 +0200189
tierno65ca36d2019-02-12 19:27:52 +0100190 @staticmethod
191 def _get_project_filter(session):
192 """
193 Generates a filter dictionary for querying database users.
194 Current policy is admin can show all, non admin, only its own user.
195 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
196 :return:
197 """
198 if session["admin"]: # allows all
199 return {}
200 else:
201 return {"_id.cont": session["project_id"]}
202
203 def check_conflict_on_new(self, session, indata):
tiernob24258a2018-10-04 18:39:49 +0200204 if not indata.get("name"):
205 raise EngineException("missing 'name'")
206 # check name not exists
garciadeblas4568a372021-03-24 09:19:48 +0100207 if self.db.get_one(
208 self.topic,
209 {"name": indata.get("name")},
210 fail_on_empty=False,
211 fail_on_more=False,
212 ):
213 raise EngineException(
214 "name '{}' exists".format(indata["name"]), HTTPStatus.CONFLICT
215 )
tiernob24258a2018-10-04 18:39:49 +0200216
217 @staticmethod
218 def format_on_new(content, project_id=None, make_public=False):
219 BaseTopic.format_on_new(content, None)
delacruzramoc061f562019-04-05 11:00:02 +0200220 # Removed so that the UUID is kept, to allow Project Name modification
221 # content["_id"] = content["name"]
tiernob24258a2018-10-04 18:39:49 +0200222
tiernob4844ab2019-05-23 08:42:12 +0000223 def check_conflict_on_del(self, session, _id, db_content):
224 """
225 Check if deletion can be done because of dependencies if it is not force. To override
226 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
227 :param _id: internal _id
228 :param db_content: The database content of this item _id
229 :return: None if ok or raises EngineException with the conflict
230 """
tierno65ca36d2019-02-12 19:27:52 +0100231 if _id in session["project_id"]:
garciadeblas4568a372021-03-24 09:19:48 +0100232 raise EngineException(
233 "You cannot delete your own project", http_code=HTTPStatus.CONFLICT
234 )
tierno65ca36d2019-02-12 19:27:52 +0100235 if session["force"]:
tiernob24258a2018-10-04 18:39:49 +0200236 return
237 _filter = {"projects": _id}
238 if self.db.get_list("users", _filter):
garciadeblas4568a372021-03-24 09:19:48 +0100239 raise EngineException(
240 "There is some USER that contains this project",
241 http_code=HTTPStatus.CONFLICT,
242 )
tiernob24258a2018-10-04 18:39:49 +0200243
tierno65ca36d2019-02-12 19:27:52 +0100244 def edit(self, session, _id, indata=None, kwargs=None, content=None):
tiernob24258a2018-10-04 18:39:49 +0200245 if not session["admin"]:
garciadeblas4568a372021-03-24 09:19:48 +0100246 raise EngineException(
247 "needed admin privileges", http_code=HTTPStatus.UNAUTHORIZED
248 )
delacruzramoc061f562019-04-05 11:00:02 +0200249 # Names that look like UUIDs are not allowed
250 name = (indata if indata else kwargs).get("name")
251 if is_valid_uuid(name):
garciadeblas4568a372021-03-24 09:19:48 +0100252 raise EngineException(
253 "Project names that look like UUIDs are not allowed",
254 http_code=HTTPStatus.UNPROCESSABLE_ENTITY,
255 )
256 return BaseTopic.edit(
257 self, session, _id, indata=indata, kwargs=kwargs, content=content
258 )
tiernob24258a2018-10-04 18:39:49 +0200259
tierno65ca36d2019-02-12 19:27:52 +0100260 def new(self, rollback, session, indata=None, kwargs=None, headers=None):
tiernob24258a2018-10-04 18:39:49 +0200261 if not session["admin"]:
garciadeblas4568a372021-03-24 09:19:48 +0100262 raise EngineException(
263 "needed admin privileges", http_code=HTTPStatus.UNAUTHORIZED
264 )
delacruzramoc061f562019-04-05 11:00:02 +0200265 # Names that look like UUIDs are not allowed
266 name = indata["name"] if indata else kwargs["name"]
267 if is_valid_uuid(name):
garciadeblas4568a372021-03-24 09:19:48 +0100268 raise EngineException(
269 "Project names that look like UUIDs are not allowed",
270 http_code=HTTPStatus.UNPROCESSABLE_ENTITY,
271 )
272 return BaseTopic.new(
273 self, rollback, session, indata=indata, kwargs=kwargs, headers=headers
274 )
tiernob24258a2018-10-04 18:39:49 +0200275
276
tiernobdebce92019-07-01 15:36:49 +0000277class CommonVimWimSdn(BaseTopic):
278 """Common class for VIM, WIM SDN just to unify methods that are equal to all of them"""
garciadeblas4568a372021-03-24 09:19:48 +0100279
280 config_to_encrypt = (
281 {}
282 ) # what keys at config must be encrypted because contains passwords
283 password_to_encrypt = "" # key that contains a password
tiernob24258a2018-10-04 18:39:49 +0200284
tiernobdebce92019-07-01 15:36:49 +0000285 @staticmethod
286 def _create_operation(op_type, params=None):
287 """
288 Creates a dictionary with the information to an operation, similar to ns-lcm-op
289 :param op_type: can be create, edit, delete
290 :param params: operation input parameters
291 :return: new dictionary with
292 """
293 now = time()
294 return {
295 "lcmOperationType": op_type,
296 "operationState": "PROCESSING",
297 "startTime": now,
298 "statusEnteredTime": now,
299 "detailed-status": "",
300 "operationParams": params,
301 }
tiernob24258a2018-10-04 18:39:49 +0200302
tierno65ca36d2019-02-12 19:27:52 +0100303 def check_conflict_on_new(self, session, indata):
tiernobdebce92019-07-01 15:36:49 +0000304 """
305 Check that the data to be inserted is valid. It is checked that name is unique
306 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
307 :param indata: data to be inserted
308 :return: None or raises EngineException
309 """
tiernob24258a2018-10-04 18:39:49 +0200310 self.check_unique_name(session, indata["name"], _id=None)
311
tierno65ca36d2019-02-12 19:27:52 +0100312 def check_conflict_on_edit(self, session, final_content, edit_content, _id):
tiernobdebce92019-07-01 15:36:49 +0000313 """
314 Check that the data to be edited/uploaded is valid. It is checked that name is unique
315 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
316 :param final_content: data once modified. This method may change it.
317 :param edit_content: incremental data that contains the modifications to apply
318 :param _id: internal _id
319 :return: None or raises EngineException
320 """
tierno65ca36d2019-02-12 19:27:52 +0100321 if not session["force"] and edit_content.get("name"):
tiernob24258a2018-10-04 18:39:49 +0200322 self.check_unique_name(session, edit_content["name"], _id=_id)
323
bravofb995ea22021-02-10 10:57:52 -0300324 return final_content
325
tiernobdebce92019-07-01 15:36:49 +0000326 def format_on_edit(self, final_content, edit_content):
327 """
328 Modifies final_content inserting admin information upon edition
329 :param final_content: final content to be stored at database
330 :param edit_content: user requested update content
331 :return: operation id
332 """
delacruzramofe598fe2019-10-23 18:25:11 +0200333 super().format_on_edit(final_content, edit_content)
tiernobdebce92019-07-01 15:36:49 +0000334
tierno92c1c7d2018-11-12 15:22:37 +0100335 # encrypt passwords
336 schema_version = final_content.get("schema_version")
337 if schema_version:
tiernobdebce92019-07-01 15:36:49 +0000338 if edit_content.get(self.password_to_encrypt):
garciadeblas4568a372021-03-24 09:19:48 +0100339 final_content[self.password_to_encrypt] = self.db.encrypt(
340 edit_content[self.password_to_encrypt],
341 schema_version=schema_version,
342 salt=final_content["_id"],
343 )
344 config_to_encrypt_keys = self.config_to_encrypt.get(
345 schema_version
346 ) or self.config_to_encrypt.get("default")
tierno468aa242019-08-01 16:35:04 +0000347 if edit_content.get("config") and config_to_encrypt_keys:
tierno468aa242019-08-01 16:35:04 +0000348 for p in config_to_encrypt_keys:
tierno92c1c7d2018-11-12 15:22:37 +0100349 if edit_content["config"].get(p):
garciadeblas4568a372021-03-24 09:19:48 +0100350 final_content["config"][p] = self.db.encrypt(
351 edit_content["config"][p],
352 schema_version=schema_version,
353 salt=final_content["_id"],
354 )
yshah93909d22024-08-12 09:13:28 +0000355 if edit_content.get("config", {}).get("credentials"):
356 cloud_credentials = edit_content["config"]["credentials"]
357 if cloud_credentials.get("clientSecret"):
358 edit_content["config"]["credentials"][
359 "clientSecret"
360 ] = self.db.encrypt(
361 edit_content["config"]["credentials"]["clientSecret"],
362 schema_version=schema_version,
363 salt=edit_content["_id"],
364 )
365 elif cloud_credentials.get("SecretAccessKey"):
366 edit_content["config"]["credentials"][
367 "SecretAccessKey"
368 ] = self.db.encrypt(
369 edit_content["config"]["credentials"]["SecretAccessKey"],
370 schema_version=schema_version,
371 salt=edit_content["_id"],
372 )
tiernobdebce92019-07-01 15:36:49 +0000373
374 # create edit operation
375 final_content["_admin"]["operations"].append(self._create_operation("edit"))
garciadeblas4568a372021-03-24 09:19:48 +0100376 return "{}:{}".format(
377 final_content["_id"], len(final_content["_admin"]["operations"]) - 1
378 )
tierno92c1c7d2018-11-12 15:22:37 +0100379
380 def format_on_new(self, content, project_id=None, make_public=False):
tiernobdebce92019-07-01 15:36:49 +0000381 """
382 Modifies content descriptor to include _admin and insert create operation
383 :param content: descriptor to be modified
384 :param project_id: if included, it add project read/write permissions. Can be None or a list
385 :param make_public: if included it is generated as public for reading.
386 :return: op_id: operation id on asynchronous operation, None otherwise. In addition content is modified
387 """
388 super().format_on_new(content, project_id=project_id, make_public=make_public)
tierno468aa242019-08-01 16:35:04 +0000389 content["schema_version"] = schema_version = "1.11"
yshah53cc9eb2024-07-05 13:06:31 +0000390 content["key"] = "registered"
tierno92c1c7d2018-11-12 15:22:37 +0100391
392 # encrypt passwords
tiernobdebce92019-07-01 15:36:49 +0000393 if content.get(self.password_to_encrypt):
garciadeblas4568a372021-03-24 09:19:48 +0100394 content[self.password_to_encrypt] = self.db.encrypt(
395 content[self.password_to_encrypt],
396 schema_version=schema_version,
397 salt=content["_id"],
398 )
399 config_to_encrypt_keys = self.config_to_encrypt.get(
400 schema_version
401 ) or self.config_to_encrypt.get("default")
tierno468aa242019-08-01 16:35:04 +0000402 if content.get("config") and config_to_encrypt_keys:
403 for p in config_to_encrypt_keys:
tierno92c1c7d2018-11-12 15:22:37 +0100404 if content["config"].get(p):
garciadeblas4568a372021-03-24 09:19:48 +0100405 content["config"][p] = self.db.encrypt(
406 content["config"][p],
407 schema_version=schema_version,
408 salt=content["_id"],
409 )
yshah93909d22024-08-12 09:13:28 +0000410 if content.get("config", {}).get("credentials"):
411 cloud_credentials = content["config"]["credentials"]
412 if cloud_credentials.get("clientSecret"):
413 content["config"]["credentials"]["clientSecret"] = self.db.encrypt(
414 content["config"]["credentials"]["clientSecret"],
415 schema_version=schema_version,
416 salt=content["_id"],
417 )
418 elif cloud_credentials.get("SecretAccessKey"):
419 content["config"]["credentials"]["SecretAccessKey"] = self.db.encrypt(
420 content["config"]["credentials"]["SecretAccessKey"],
421 schema_version=schema_version,
422 salt=content["_id"],
423 )
tierno92c1c7d2018-11-12 15:22:37 +0100424
tiernob24258a2018-10-04 18:39:49 +0200425 content["_admin"]["operationalState"] = "PROCESSING"
426
tiernobdebce92019-07-01 15:36:49 +0000427 # create operation
428 content["_admin"]["operations"] = [self._create_operation("create")]
429 content["_admin"]["current_operation"] = None
vijay.rd1eaf982021-05-14 11:54:59 +0000430 # create Resource in Openstack based VIM
431 if content.get("vim_type"):
432 if content["vim_type"] == "openstack":
433 compute = {
garciadeblasf2af4a12023-01-24 16:56:54 +0100434 "ram": {"total": None, "used": None},
435 "vcpus": {"total": None, "used": None},
436 "instances": {"total": None, "used": None},
vijay.rd1eaf982021-05-14 11:54:59 +0000437 }
438 storage = {
garciadeblasf2af4a12023-01-24 16:56:54 +0100439 "volumes": {"total": None, "used": None},
440 "snapshots": {"total": None, "used": None},
441 "storage": {"total": None, "used": None},
vijay.rd1eaf982021-05-14 11:54:59 +0000442 }
443 network = {
garciadeblasf2af4a12023-01-24 16:56:54 +0100444 "networks": {"total": None, "used": None},
445 "subnets": {"total": None, "used": None},
446 "floating_ips": {"total": None, "used": None},
vijay.rd1eaf982021-05-14 11:54:59 +0000447 }
garciadeblasf2af4a12023-01-24 16:56:54 +0100448 content["resources"] = {
449 "compute": compute,
450 "storage": storage,
451 "network": network,
452 }
vijay.rd1eaf982021-05-14 11:54:59 +0000453
tiernobdebce92019-07-01 15:36:49 +0000454 return "{}:0".format(content["_id"])
455
tiernobee3bad2019-12-05 12:26:01 +0000456 def delete(self, session, _id, dry_run=False, not_send_msg=None):
tiernob24258a2018-10-04 18:39:49 +0200457 """
458 Delete item by its internal _id
tierno65ca36d2019-02-12 19:27:52 +0100459 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
tiernob24258a2018-10-04 18:39:49 +0200460 :param _id: server internal id
tiernob24258a2018-10-04 18:39:49 +0200461 :param dry_run: make checking but do not delete
tiernobee3bad2019-12-05 12:26:01 +0000462 :param not_send_msg: To not send message (False) or store content (list) instead
tiernobdebce92019-07-01 15:36:49 +0000463 :return: operation id if it is ordered to delete. None otherwise
tiernob24258a2018-10-04 18:39:49 +0200464 """
tiernobdebce92019-07-01 15:36:49 +0000465
466 filter_q = self._get_project_filter(session)
467 filter_q["_id"] = _id
468 db_content = self.db.get_one(self.topic, filter_q)
469
470 self.check_conflict_on_del(session, _id, db_content)
471 if dry_run:
472 return None
473
tiernof5f2e3f2020-03-23 14:42:10 +0000474 # remove reference from project_read if there are more projects referencing it. If it last one,
475 # do not remove reference, but order via kafka to delete it
selvi.ja5e05112023-04-28 11:00:21 +0000476 if session["project_id"]:
garciadeblas4568a372021-03-24 09:19:48 +0100477 other_projects_referencing = next(
478 (
479 p
480 for p in db_content["_admin"]["projects_read"]
481 if p not in session["project_id"] and p != "ANY"
482 ),
483 None,
484 )
tiernobdebce92019-07-01 15:36:49 +0000485
tiernof5f2e3f2020-03-23 14:42:10 +0000486 # check if there are projects referencing it (apart from ANY, that means, public)....
487 if other_projects_referencing:
488 # remove references but not delete
garciadeblas4568a372021-03-24 09:19:48 +0100489 update_dict_pull = {
490 "_admin.projects_read": session["project_id"],
491 "_admin.projects_write": session["project_id"],
492 }
493 self.db.set_one(
494 self.topic, filter_q, update_dict=None, pull_list=update_dict_pull
495 )
tiernof5f2e3f2020-03-23 14:42:10 +0000496 return None
497 else:
garciadeblas4568a372021-03-24 09:19:48 +0100498 can_write = next(
499 (
500 p
501 for p in db_content["_admin"]["projects_write"]
502 if p == "ANY" or p in session["project_id"]
503 ),
504 None,
505 )
tiernof5f2e3f2020-03-23 14:42:10 +0000506 if not can_write:
garciadeblas4568a372021-03-24 09:19:48 +0100507 raise EngineException(
508 "You have not write permission to delete it",
509 http_code=HTTPStatus.UNAUTHORIZED,
510 )
tiernobdebce92019-07-01 15:36:49 +0000511
512 # It must be deleted
513 if session["force"]:
514 self.db.del_one(self.topic, {"_id": _id})
515 op_id = None
garciadeblas4568a372021-03-24 09:19:48 +0100516 self._send_msg(
517 "deleted", {"_id": _id, "op_id": op_id}, not_send_msg=not_send_msg
518 )
tiernobdebce92019-07-01 15:36:49 +0000519 else:
tiernof5f2e3f2020-03-23 14:42:10 +0000520 update_dict = {"_admin.to_delete": True}
garciadeblas4568a372021-03-24 09:19:48 +0100521 self.db.set_one(
522 self.topic,
523 {"_id": _id},
524 update_dict=update_dict,
525 push={"_admin.operations": self._create_operation("delete")},
526 )
tiernobdebce92019-07-01 15:36:49 +0000527 # the number of operations is the operation_id. db_content does not contains the new operation inserted,
528 # so the -1 is not needed
garciadeblas4568a372021-03-24 09:19:48 +0100529 op_id = "{}:{}".format(
530 db_content["_id"], len(db_content["_admin"]["operations"])
531 )
532 self._send_msg(
533 "delete", {"_id": _id, "op_id": op_id}, not_send_msg=not_send_msg
534 )
tiernobdebce92019-07-01 15:36:49 +0000535 return op_id
tiernob24258a2018-10-04 18:39:49 +0200536
537
tiernobdebce92019-07-01 15:36:49 +0000538class VimAccountTopic(CommonVimWimSdn):
539 topic = "vim_accounts"
540 topic_msg = "vim_account"
541 schema_new = vim_account_new_schema
542 schema_edit = vim_account_edit_schema
543 multiproject = True
544 password_to_encrypt = "vim_password"
garciadeblas4568a372021-03-24 09:19:48 +0100545 config_to_encrypt = {
546 "1.1": ("admin_password", "nsx_password", "vcenter_password"),
547 "default": (
548 "admin_password",
549 "nsx_password",
550 "vcenter_password",
551 "vrops_password",
552 ),
553 }
tiernobdebce92019-07-01 15:36:49 +0000554
delacruzramo35c998b2019-11-21 11:09:16 +0100555 def check_conflict_on_del(self, session, _id, db_content):
556 """
557 Check if deletion can be done because of dependencies if it is not force. To override
558 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
559 :param _id: internal _id
560 :param db_content: The database content of this item _id
561 :return: None if ok or raises EngineException with the conflict
562 """
563 if session["force"]:
564 return
565 # check if used by VNF
566 if self.db.get_list("vnfrs", {"vim-account-id": _id}):
garciadeblas4568a372021-03-24 09:19:48 +0100567 raise EngineException(
568 "There is at least one VNF using this VIM account",
569 http_code=HTTPStatus.CONFLICT,
570 )
delacruzramo35c998b2019-11-21 11:09:16 +0100571 super().check_conflict_on_del(session, _id, db_content)
572
tiernobdebce92019-07-01 15:36:49 +0000573
574class WimAccountTopic(CommonVimWimSdn):
tierno55ba2e62018-12-11 17:22:22 +0000575 topic = "wim_accounts"
576 topic_msg = "wim_account"
577 schema_new = wim_account_new_schema
578 schema_edit = wim_account_edit_schema
tierno65ca36d2019-02-12 19:27:52 +0100579 multiproject = True
gifrerenom44f5ec12022-03-07 16:57:25 +0000580 password_to_encrypt = "password"
tierno468aa242019-08-01 16:35:04 +0000581 config_to_encrypt = {}
tierno55ba2e62018-12-11 17:22:22 +0000582
583
tiernobdebce92019-07-01 15:36:49 +0000584class SdnTopic(CommonVimWimSdn):
tiernob24258a2018-10-04 18:39:49 +0200585 topic = "sdns"
586 topic_msg = "sdn"
tierno6b02b052020-06-02 10:07:41 +0000587 quota_name = "sdn_controllers"
tiernob24258a2018-10-04 18:39:49 +0200588 schema_new = sdn_new_schema
589 schema_edit = sdn_edit_schema
tierno65ca36d2019-02-12 19:27:52 +0100590 multiproject = True
tiernobdebce92019-07-01 15:36:49 +0000591 password_to_encrypt = "password"
tierno468aa242019-08-01 16:35:04 +0000592 config_to_encrypt = {}
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100593
tierno7adaeb02019-12-17 16:46:12 +0000594 def _obtain_url(self, input, create):
595 if input.get("ip") or input.get("port"):
garciadeblas4568a372021-03-24 09:19:48 +0100596 if not input.get("ip") or not input.get("port") or input.get("url"):
597 raise ValidationError(
598 "You must provide both 'ip' and 'port' (deprecated); or just 'url' (prefered)"
599 )
600 input["url"] = "http://{}:{}/".format(input["ip"], input["port"])
tierno7adaeb02019-12-17 16:46:12 +0000601 del input["ip"]
602 del input["port"]
garciadeblas4568a372021-03-24 09:19:48 +0100603 elif create and not input.get("url"):
tierno7adaeb02019-12-17 16:46:12 +0000604 raise ValidationError("You must provide 'url'")
605 return input
606
607 def _validate_input_new(self, input, force=False):
608 input = super()._validate_input_new(input, force)
609 return self._obtain_url(input, True)
610
Frank Brydendeba68e2020-07-27 13:55:11 +0000611 def _validate_input_edit(self, input, content, force=False):
612 input = super()._validate_input_edit(input, content, force)
tierno7adaeb02019-12-17 16:46:12 +0000613 return self._obtain_url(input, False)
614
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100615
delacruzramofe598fe2019-10-23 18:25:11 +0200616class K8sClusterTopic(CommonVimWimSdn):
617 topic = "k8sclusters"
618 topic_msg = "k8scluster"
619 schema_new = k8scluster_new_schema
620 schema_edit = k8scluster_edit_schema
621 multiproject = True
622 password_to_encrypt = None
623 config_to_encrypt = {}
624
625 def format_on_new(self, content, project_id=None, make_public=False):
626 oid = super().format_on_new(content, project_id, make_public)
garciadeblas4568a372021-03-24 09:19:48 +0100627 self.db.encrypt_decrypt_fields(
628 content["credentials"],
629 "encrypt",
630 ["password", "secret"],
631 schema_version=content["schema_version"],
632 salt=content["_id"],
633 )
delacruzramoc2d5fc62020-02-05 11:50:21 +0000634 # Add Helm/Juju Repo lists
635 repos = {"helm-chart": [], "juju-bundle": []}
636 for proj in content["_admin"]["projects_read"]:
garciadeblas4568a372021-03-24 09:19:48 +0100637 if proj != "ANY":
638 for repo in self.db.get_list(
639 "k8srepos", {"_admin.projects_read": proj}
640 ):
delacruzramoc2d5fc62020-02-05 11:50:21 +0000641 if repo["_id"] not in repos[repo["type"]]:
642 repos[repo["type"]].append(repo["_id"])
643 for k in repos:
garciadeblas4568a372021-03-24 09:19:48 +0100644 content["_admin"][k.replace("-", "_") + "_repos"] = repos[k]
delacruzramofe598fe2019-10-23 18:25:11 +0200645 return oid
646
647 def format_on_edit(self, final_content, edit_content):
648 if final_content.get("schema_version") and edit_content.get("credentials"):
garciadeblas4568a372021-03-24 09:19:48 +0100649 self.db.encrypt_decrypt_fields(
650 edit_content["credentials"],
651 "encrypt",
652 ["password", "secret"],
653 schema_version=final_content["schema_version"],
654 salt=final_content["_id"],
655 )
656 deep_update_rfc7396(
657 final_content["credentials"], edit_content["credentials"]
658 )
delacruzramofe598fe2019-10-23 18:25:11 +0200659 oid = super().format_on_edit(final_content, edit_content)
660 return oid
661
delacruzramoc2d5fc62020-02-05 11:50:21 +0000662 def check_conflict_on_edit(self, session, final_content, edit_content, _id):
garciadeblas4568a372021-03-24 09:19:48 +0100663 final_content = super(CommonVimWimSdn, self).check_conflict_on_edit(
664 session, final_content, edit_content, _id
665 )
666 final_content = super().check_conflict_on_edit(
667 session, final_content, edit_content, _id
668 )
delacruzramoc2d5fc62020-02-05 11:50:21 +0000669 # Update Helm/Juju Repo lists
670 repos = {"helm-chart": [], "juju-bundle": []}
671 for proj in session.get("set_project", []):
garciadeblas4568a372021-03-24 09:19:48 +0100672 if proj != "ANY":
673 for repo in self.db.get_list(
674 "k8srepos", {"_admin.projects_read": proj}
675 ):
delacruzramoc2d5fc62020-02-05 11:50:21 +0000676 if repo["_id"] not in repos[repo["type"]]:
677 repos[repo["type"]].append(repo["_id"])
678 for k in repos:
garciadeblas4568a372021-03-24 09:19:48 +0100679 rlist = k.replace("-", "_") + "_repos"
delacruzramoc2d5fc62020-02-05 11:50:21 +0000680 if rlist not in final_content["_admin"]:
681 final_content["_admin"][rlist] = []
682 final_content["_admin"][rlist] += repos[k]
bravofb995ea22021-02-10 10:57:52 -0300683 return final_content
delacruzramoc2d5fc62020-02-05 11:50:21 +0000684
tiernoe19707b2020-04-21 13:08:04 +0000685 def check_conflict_on_del(self, session, _id, db_content):
686 """
687 Check if deletion can be done because of dependencies if it is not force. To override
688 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
689 :param _id: internal _id
690 :param db_content: The database content of this item _id
691 :return: None if ok or raises EngineException with the conflict
692 """
693 if session["force"]:
694 return
695 # check if used by VNF
696 filter_q = {"kdur.k8s-cluster.id": _id}
697 if session["project_id"]:
698 filter_q["_admin.projects_read.cont"] = session["project_id"]
699 if self.db.get_list("vnfrs", filter_q):
garciadeblas4568a372021-03-24 09:19:48 +0100700 raise EngineException(
701 "There is at least one VNF using this k8scluster",
702 http_code=HTTPStatus.CONFLICT,
703 )
tiernoe19707b2020-04-21 13:08:04 +0000704 super().check_conflict_on_del(session, _id, db_content)
705
delacruzramofe598fe2019-10-23 18:25:11 +0200706
David Garciaecb41322021-03-31 19:10:46 +0200707class VcaTopic(CommonVimWimSdn):
708 topic = "vca"
709 topic_msg = "vca"
710 schema_new = vca_new_schema
711 schema_edit = vca_edit_schema
712 multiproject = True
713 password_to_encrypt = None
714
715 def format_on_new(self, content, project_id=None, make_public=False):
716 oid = super().format_on_new(content, project_id, make_public)
717 content["schema_version"] = schema_version = "1.11"
718 for key in ["secret", "cacert"]:
719 content[key] = self.db.encrypt(
garciadeblas4568a372021-03-24 09:19:48 +0100720 content[key], schema_version=schema_version, salt=content["_id"]
David Garciaecb41322021-03-31 19:10:46 +0200721 )
722 return oid
723
724 def format_on_edit(self, final_content, edit_content):
725 oid = super().format_on_edit(final_content, edit_content)
726 schema_version = final_content.get("schema_version")
727 for key in ["secret", "cacert"]:
728 if key in edit_content:
729 final_content[key] = self.db.encrypt(
730 edit_content[key],
731 schema_version=schema_version,
garciadeblas4568a372021-03-24 09:19:48 +0100732 salt=final_content["_id"],
David Garciaecb41322021-03-31 19:10:46 +0200733 )
734 return oid
735
736 def check_conflict_on_del(self, session, _id, db_content):
737 """
738 Check if deletion can be done because of dependencies if it is not force. To override
739 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
740 :param _id: internal _id
741 :param db_content: The database content of this item _id
742 :return: None if ok or raises EngineException with the conflict
743 """
744 if session["force"]:
745 return
746 # check if used by VNF
747 filter_q = {"vca": _id}
748 if session["project_id"]:
749 filter_q["_admin.projects_read.cont"] = session["project_id"]
750 if self.db.get_list("vim_accounts", filter_q):
garciadeblas4568a372021-03-24 09:19:48 +0100751 raise EngineException(
752 "There is at least one VIM account using this vca",
753 http_code=HTTPStatus.CONFLICT,
754 )
David Garciaecb41322021-03-31 19:10:46 +0200755 super().check_conflict_on_del(session, _id, db_content)
756
757
delacruzramofe598fe2019-10-23 18:25:11 +0200758class K8sRepoTopic(CommonVimWimSdn):
759 topic = "k8srepos"
760 topic_msg = "k8srepo"
761 schema_new = k8srepo_new_schema
762 schema_edit = k8srepo_edit_schema
763 multiproject = True
764 password_to_encrypt = None
765 config_to_encrypt = {}
766
delacruzramoc2d5fc62020-02-05 11:50:21 +0000767 def format_on_new(self, content, project_id=None, make_public=False):
768 oid = super().format_on_new(content, project_id, make_public)
769 # Update Helm/Juju Repo lists
garciadeblas4568a372021-03-24 09:19:48 +0100770 repo_list = content["type"].replace("-", "_") + "_repos"
delacruzramoc2d5fc62020-02-05 11:50:21 +0000771 for proj in content["_admin"]["projects_read"]:
garciadeblas4568a372021-03-24 09:19:48 +0100772 if proj != "ANY":
773 self.db.set_list(
774 "k8sclusters",
775 {
776 "_admin.projects_read": proj,
777 "_admin." + repo_list + ".ne": content["_id"],
778 },
779 {},
780 push={"_admin." + repo_list: content["_id"]},
781 )
delacruzramoc2d5fc62020-02-05 11:50:21 +0000782 return oid
783
784 def delete(self, session, _id, dry_run=False, not_send_msg=None):
785 type = self.db.get_one("k8srepos", {"_id": _id})["type"]
786 oid = super().delete(session, _id, dry_run, not_send_msg)
787 if oid:
788 # Remove from Helm/Juju Repo lists
garciadeblas4568a372021-03-24 09:19:48 +0100789 repo_list = type.replace("-", "_") + "_repos"
790 self.db.set_list(
791 "k8sclusters",
792 {"_admin." + repo_list: _id},
793 {},
794 pull={"_admin." + repo_list: _id},
795 )
delacruzramoc2d5fc62020-02-05 11:50:21 +0000796 return oid
797
delacruzramofe598fe2019-10-23 18:25:11 +0200798
Felipe Vicensb66b0412020-05-06 10:11:00 +0200799class OsmRepoTopic(BaseTopic):
800 topic = "osmrepos"
801 topic_msg = "osmrepos"
802 schema_new = osmrepo_new_schema
803 schema_edit = osmrepo_edit_schema
804 multiproject = True
805 # TODO: Implement user/password
806
807
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100808class UserTopicAuth(UserTopic):
tierno65ca36d2019-02-12 19:27:52 +0100809 # topic = "users"
agarwalat53471982020-10-08 13:06:14 +0000810 topic_msg = "users"
Eduardo Sousaa16a4fa2019-05-23 01:41:18 +0100811 schema_new = user_new_schema
812 schema_edit = user_edit_schema
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100813
814 def __init__(self, db, fs, msg, auth):
delacruzramo32bab472019-09-13 12:24:22 +0200815 UserTopic.__init__(self, db, fs, msg, auth)
816 # self.auth = auth
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100817
tierno65ca36d2019-02-12 19:27:52 +0100818 def check_conflict_on_new(self, session, indata):
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100819 """
820 Check that the data to be inserted is valid
821
tierno65ca36d2019-02-12 19:27:52 +0100822 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100823 :param indata: data to be inserted
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100824 :return: None or raises EngineException
825 """
826 username = indata.get("username")
tiernocf042d32019-06-13 09:06:40 +0000827 if is_valid_uuid(username):
garciadeblas4568a372021-03-24 09:19:48 +0100828 raise EngineException(
829 "username '{}' cannot have a uuid format".format(username),
830 HTTPStatus.UNPROCESSABLE_ENTITY,
831 )
tiernocf042d32019-06-13 09:06:40 +0000832
833 # Check that username is not used, regardless keystone already checks this
834 if self.auth.get_user_list(filter_q={"name": username}):
garciadeblas4568a372021-03-24 09:19:48 +0100835 raise EngineException(
836 "username '{}' is already used".format(username), HTTPStatus.CONFLICT
837 )
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100838
Eduardo Sousa339ed782019-05-28 14:25:00 +0100839 if "projects" in indata.keys():
tierno701018c2019-06-25 11:13:14 +0000840 # convert to new format project_role_mappings
delacruzramo01b15d32019-07-02 14:37:47 +0200841 role = self.auth.get_role_list({"name": "project_admin"})
842 if not role:
843 role = self.auth.get_role_list()
844 if not role:
garciadeblas4568a372021-03-24 09:19:48 +0100845 raise AuthconnNotFoundException(
846 "Can't find default role for user '{}'".format(username)
847 )
delacruzramo01b15d32019-07-02 14:37:47 +0200848 rid = role[0]["_id"]
tierno701018c2019-06-25 11:13:14 +0000849 if not indata.get("project_role_mappings"):
850 indata["project_role_mappings"] = []
851 for project in indata["projects"]:
delacruzramo01b15d32019-07-02 14:37:47 +0200852 pid = self.auth.get_project(project)["_id"]
853 prm = {"project": pid, "role": rid}
854 if prm not in indata["project_role_mappings"]:
855 indata["project_role_mappings"].append(prm)
tierno701018c2019-06-25 11:13:14 +0000856 # raise EngineException("Format invalid: the keyword 'projects' is not allowed for keystone authentication",
857 # HTTPStatus.BAD_REQUEST)
Eduardo Sousa339ed782019-05-28 14:25:00 +0100858
tierno65ca36d2019-02-12 19:27:52 +0100859 def check_conflict_on_edit(self, session, final_content, edit_content, _id):
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100860 """
861 Check that the data to be edited/uploaded is valid
862
tierno65ca36d2019-02-12 19:27:52 +0100863 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100864 :param final_content: data once modified
865 :param edit_content: incremental data that contains the modifications to apply
866 :param _id: internal _id
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100867 :return: None or raises EngineException
868 """
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100869
tiernocf042d32019-06-13 09:06:40 +0000870 if "username" in edit_content:
871 username = edit_content.get("username")
872 if is_valid_uuid(username):
garciadeblas4568a372021-03-24 09:19:48 +0100873 raise EngineException(
874 "username '{}' cannot have an uuid format".format(username),
875 HTTPStatus.UNPROCESSABLE_ENTITY,
876 )
tiernocf042d32019-06-13 09:06:40 +0000877
878 # Check that username is not used, regardless keystone already checks this
879 if self.auth.get_user_list(filter_q={"name": username}):
garciadeblas4568a372021-03-24 09:19:48 +0100880 raise EngineException(
881 "username '{}' is already used".format(username),
882 HTTPStatus.CONFLICT,
883 )
tiernocf042d32019-06-13 09:06:40 +0000884
885 if final_content["username"] == "admin":
886 for mapping in edit_content.get("remove_project_role_mappings", ()):
garciadeblas4568a372021-03-24 09:19:48 +0100887 if mapping["project"] == "admin" and mapping.get("role") in (
888 None,
889 "system_admin",
890 ):
tiernocf042d32019-06-13 09:06:40 +0000891 # TODO make this also available for project id and role id
garciadeblas4568a372021-03-24 09:19:48 +0100892 raise EngineException(
893 "You cannot remove system_admin role from admin user",
894 http_code=HTTPStatus.FORBIDDEN,
895 )
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100896
bravofb995ea22021-02-10 10:57:52 -0300897 return final_content
898
tiernob4844ab2019-05-23 08:42:12 +0000899 def check_conflict_on_del(self, session, _id, db_content):
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100900 """
901 Check if deletion can be done because of dependencies if it is not force. To override
tierno65ca36d2019-02-12 19:27:52 +0100902 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100903 :param _id: internal _id
tiernob4844ab2019-05-23 08:42:12 +0000904 :param db_content: The database content of this item _id
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100905 :return: None if ok or raises EngineException with the conflict
906 """
Adurti844f6482024-11-01 06:32:13 +0000907 if db_content["_id"] == session["user_id"]:
garciadeblas4568a372021-03-24 09:19:48 +0100908 raise EngineException(
909 "You cannot delete your own login user ", http_code=HTTPStatus.CONFLICT
910 )
delacruzramo01b15d32019-07-02 14:37:47 +0200911 # TODO: Check that user is not logged in ? How? (Would require listing current tokens)
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100912
Eduardo Sousaa16a4fa2019-05-23 01:41:18 +0100913 @staticmethod
914 def format_on_show(content):
915 """
Eduardo Sousa44603902019-06-04 08:10:32 +0100916 Modifies the content of the role information to separate the role
Eduardo Sousaa16a4fa2019-05-23 01:41:18 +0100917 metadata from the role definition.
918 """
919 project_role_mappings = []
920
delacruzramo01b15d32019-07-02 14:37:47 +0200921 if "projects" in content:
922 for project in content["projects"]:
923 for role in project["roles"]:
garciadeblas4568a372021-03-24 09:19:48 +0100924 project_role_mappings.append(
925 {
926 "project": project["_id"],
927 "project_name": project["name"],
928 "role": role["_id"],
929 "role_name": role["name"],
930 }
931 )
delacruzramo01b15d32019-07-02 14:37:47 +0200932 del content["projects"]
Eduardo Sousaa16a4fa2019-05-23 01:41:18 +0100933 content["project_role_mappings"] = project_role_mappings
934
Eduardo Sousa0b1d61b2019-05-30 19:55:52 +0100935 return content
936
tierno65ca36d2019-02-12 19:27:52 +0100937 def new(self, rollback, session, indata=None, kwargs=None, headers=None):
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100938 """
939 Creates a new entry into the authentication backend.
940
941 NOTE: Overrides BaseTopic functionality because it doesn't require access to database.
942
943 :param rollback: list to append created items at database in case a rollback may to be done
tierno65ca36d2019-02-12 19:27:52 +0100944 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100945 :param indata: data to be inserted
946 :param kwargs: used to override the indata descriptor
947 :param headers: http request headers
delacruzramo01b15d32019-07-02 14:37:47 +0200948 :return: _id: identity of the inserted data, operation _id (None)
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100949 """
950 try:
951 content = BaseTopic._remove_envelop(indata)
952
953 # Override descriptor with query string kwargs
954 BaseTopic._update_input_with_kwargs(content, kwargs)
tierno65ca36d2019-02-12 19:27:52 +0100955 content = self._validate_input_new(content, session["force"])
956 self.check_conflict_on_new(session, content)
tiernocf042d32019-06-13 09:06:40 +0000957 # self.format_on_new(content, session["project_id"], make_public=session["public"])
delacruzramo01b15d32019-07-02 14:37:47 +0200958 now = time()
959 content["_admin"] = {"created": now, "modified": now}
960 prms = []
961 for prm in content.get("project_role_mappings", []):
962 proj = self.auth.get_project(prm["project"], not session["force"])
963 role = self.auth.get_role(prm["role"], not session["force"])
964 pid = proj["_id"] if proj else None
965 rid = role["_id"] if role else None
966 prl = {"project": pid, "role": rid}
967 if prl not in prms:
968 prms.append(prl)
969 content["project_role_mappings"] = prms
970 # _id = self.auth.create_user(content["username"], content["password"])["_id"]
971 _id = self.auth.create_user(content)["_id"]
Eduardo Sousa44603902019-06-04 08:10:32 +0100972
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100973 rollback.append({"topic": self.topic, "_id": _id})
tiernocf042d32019-06-13 09:06:40 +0000974 # del content["password"]
agarwalat53471982020-10-08 13:06:14 +0000975 self._send_msg("created", content, not_send_msg=None)
delacruzramo01b15d32019-07-02 14:37:47 +0200976 return _id, None
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100977 except ValidationError as e:
978 raise EngineException(e, HTTPStatus.UNPROCESSABLE_ENTITY)
979
K Sai Kiran57589552021-01-27 21:38:34 +0530980 def show(self, session, _id, filter_q=None, api_req=False):
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100981 """
982 Get complete information on an topic
983
tierno65ca36d2019-02-12 19:27:52 +0100984 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
tierno5ec768a2020-03-31 09:46:44 +0000985 :param _id: server internal id or username
K Sai Kiran57589552021-01-27 21:38:34 +0530986 :param filter_q: dict: query parameter
K Sai Kirand010e3e2020-08-28 15:11:48 +0530987 :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 +0100988 :return: dictionary, raise exception if not found.
989 """
tiernocf042d32019-06-13 09:06:40 +0000990 # Allow _id to be a name or uuid
tiernoad6d5332020-02-19 14:29:49 +0000991 filter_q = {"username": _id}
delacruzramo029405d2019-09-26 10:52:56 +0200992 # users = self.auth.get_user_list(filter_q)
garciadeblas4568a372021-03-24 09:19:48 +0100993 users = self.list(session, filter_q) # To allow default filtering (Bug 853)
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100994 if len(users) == 1:
tierno1546f2a2019-08-20 15:38:11 +0000995 return users[0]
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100996 elif len(users) > 1:
garciadeblas4568a372021-03-24 09:19:48 +0100997 raise EngineException(
998 "Too many users found for '{}'".format(_id), HTTPStatus.CONFLICT
999 )
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001000 else:
garciadeblas4568a372021-03-24 09:19:48 +01001001 raise EngineException(
1002 "User '{}' not found".format(_id), HTTPStatus.NOT_FOUND
1003 )
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001004
tierno65ca36d2019-02-12 19:27:52 +01001005 def edit(self, session, _id, indata=None, kwargs=None, content=None):
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001006 """
1007 Updates an user entry.
1008
tierno65ca36d2019-02-12 19:27:52 +01001009 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001010 :param _id:
1011 :param indata: data to be inserted
1012 :param kwargs: used to override the indata descriptor
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001013 :param content:
1014 :return: _id: identity of the inserted data.
1015 """
1016 indata = self._remove_envelop(indata)
1017
1018 # Override descriptor with query string kwargs
1019 if kwargs:
1020 BaseTopic._update_input_with_kwargs(indata, kwargs)
1021 try:
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001022 if not content:
1023 content = self.show(session, _id)
yshah53cc9eb2024-07-05 13:06:31 +00001024
Frank Brydendeba68e2020-07-27 13:55:11 +00001025 indata = self._validate_input_edit(indata, content, force=session["force"])
bravofb995ea22021-02-10 10:57:52 -03001026 content = self.check_conflict_on_edit(session, content, indata, _id=_id)
tiernocf042d32019-06-13 09:06:40 +00001027 # self.format_on_edit(content, indata)
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001028
garciadeblas4568a372021-03-24 09:19:48 +01001029 if not (
1030 "password" in indata
1031 or "username" in indata
1032 or indata.get("remove_project_role_mappings")
1033 or indata.get("add_project_role_mappings")
1034 or indata.get("project_role_mappings")
1035 or indata.get("projects")
1036 or indata.get("add_projects")
garciadeblas6d83f8f2023-06-19 22:34:49 +02001037 or indata.get("unlock")
1038 or indata.get("renew")
jeganbe1a3df2024-06-04 12:05:19 +00001039 or indata.get("email_id")
garciadeblas4568a372021-03-24 09:19:48 +01001040 ):
tiernocf042d32019-06-13 09:06:40 +00001041 return _id
garciadeblas4568a372021-03-24 09:19:48 +01001042 if indata.get("project_role_mappings") and (
1043 indata.get("remove_project_role_mappings")
1044 or indata.get("add_project_role_mappings")
1045 ):
1046 raise EngineException(
1047 "Option 'project_role_mappings' is incompatible with 'add_project_role_mappings"
1048 "' or 'remove_project_role_mappings'",
1049 http_code=HTTPStatus.BAD_REQUEST,
1050 )
Eduardo Sousa44603902019-06-04 08:10:32 +01001051
delacruzramo01b15d32019-07-02 14:37:47 +02001052 if indata.get("projects") or indata.get("add_projects"):
1053 role = self.auth.get_role_list({"name": "project_admin"})
1054 if not role:
1055 role = self.auth.get_role_list()
1056 if not role:
garciadeblas4568a372021-03-24 09:19:48 +01001057 raise AuthconnNotFoundException(
1058 "Can't find a default role for user '{}'".format(
1059 content["username"]
1060 )
1061 )
delacruzramo01b15d32019-07-02 14:37:47 +02001062 rid = role[0]["_id"]
1063 if "add_project_role_mappings" not in indata:
1064 indata["add_project_role_mappings"] = []
tierno1546f2a2019-08-20 15:38:11 +00001065 if "remove_project_role_mappings" not in indata:
1066 indata["remove_project_role_mappings"] = []
1067 if isinstance(indata.get("projects"), dict):
1068 # backward compatible
1069 for k, v in indata["projects"].items():
1070 if k.startswith("$") and v is None:
garciadeblas4568a372021-03-24 09:19:48 +01001071 indata["remove_project_role_mappings"].append(
1072 {"project": k[1:]}
1073 )
tierno1546f2a2019-08-20 15:38:11 +00001074 elif k.startswith("$+"):
garciadeblas4568a372021-03-24 09:19:48 +01001075 indata["add_project_role_mappings"].append(
1076 {"project": v, "role": rid}
1077 )
tierno1546f2a2019-08-20 15:38:11 +00001078 del indata["projects"]
delacruzramo01b15d32019-07-02 14:37:47 +02001079 for proj in indata.get("projects", []) + indata.get("add_projects", []):
garciadeblas4568a372021-03-24 09:19:48 +01001080 indata["add_project_role_mappings"].append(
1081 {"project": proj, "role": rid}
1082 )
Adurti76d4b762024-05-07 06:04:37 +00001083 if (
1084 indata.get("remove_project_role_mappings")
1085 or indata.get("add_project_role_mappings")
1086 or indata.get("project_role_mappings")
1087 ):
1088 user_details = self.db.get_one("users", {"_id": session.get("user_id")})
1089 edit_role = False
1090 for pr in user_details["project_role_mappings"]:
1091 role_id = pr.get("role")
1092 role_details = self.db.get_one("roles", {"_id": role_id})
1093 if role_details["permissions"].get("default"):
1094 if "roles" not in role_details["permissions"] or role_details[
1095 "permissions"
1096 ].get("roles"):
1097 edit_role = True
1098 elif role_details["permissions"].get("roles"):
1099 edit_role = True
1100 if not edit_role:
1101 raise EngineException(
1102 "User {} has no privileges to edit or delete project-role mappings".format(
1103 session.get("username")
1104 ),
1105 http_code=HTTPStatus.UNPROCESSABLE_ENTITY,
1106 )
delacruzramo01b15d32019-07-02 14:37:47 +02001107
Adurti9f2ac992024-03-25 10:58:29 +00001108 # check before deleting project-role
1109 delete_session_project = False
1110 if indata.get("remove_project_role_mappings"):
1111 for pr in indata["remove_project_role_mappings"]:
1112 project_name = pr.get("project")
1113 project_details = self.db.get_one(
1114 "projects", {"_id": session.get("project_id")[0]}
1115 )
1116 if project_details["name"] == project_name:
1117 delete_session_project = True
1118
37177091c0322024-11-01 08:55:59 +00001119 # password change
1120 if indata.get("password"):
1121 if not session.get("admin_show"):
1122 if not indata.get("system_admin_id"):
1123 if _id != session["user_id"]:
1124 raise EngineException(
1125 "You are not allowed to change other users password",
1126 http_code=HTTPStatus.BAD_REQUEST,
1127 )
1128 if not indata.get("old_password"):
1129 raise EngineException(
1130 "Password change requires old password or admin ID",
1131 http_code=HTTPStatus.BAD_REQUEST,
1132 )
1133
adurti3e95b0f2025-03-06 14:12:36 +00001134 # username change
1135 if indata.get("username"):
1136 if not session.get("admin_show"):
1137 if not indata.get("system_admin_id"):
1138 if _id != session["user_id"]:
1139 raise EngineException(
1140 "You are not allowed to change other users username",
1141 http_code=HTTPStatus.BAD_REQUEST,
1142 )
1143
delacruzramo01b15d32019-07-02 14:37:47 +02001144 # user = self.show(session, _id) # Already in 'content'
1145 original_mapping = content["project_role_mappings"]
Eduardo Sousa44603902019-06-04 08:10:32 +01001146
tiernocf042d32019-06-13 09:06:40 +00001147 mappings_to_add = []
1148 mappings_to_remove = []
Eduardo Sousa44603902019-06-04 08:10:32 +01001149
tiernocf042d32019-06-13 09:06:40 +00001150 # remove
1151 for to_remove in indata.get("remove_project_role_mappings", ()):
1152 for mapping in original_mapping:
garciadeblas4568a372021-03-24 09:19:48 +01001153 if to_remove["project"] in (
1154 mapping["project"],
1155 mapping["project_name"],
1156 ):
1157 if not to_remove.get("role") or to_remove["role"] in (
1158 mapping["role"],
1159 mapping["role_name"],
1160 ):
tiernocf042d32019-06-13 09:06:40 +00001161 mappings_to_remove.append(mapping)
jegan9b0ce4d2024-03-31 07:39:39 +00001162 if len(original_mapping) == 0 or len(mappings_to_remove) == 0:
1163 pid = self.auth.get_project(to_remove["project"])["_id"]
1164 if to_remove.get("role"):
1165 rid = self.auth.get_role(to_remove["role"])["_id"]
1166
1167 raise AuthconnNotFoundException(
1168 "User is not mapped with project '{}' or role '{}'".format(
1169 to_remove["project"], to_remove["role"]
1170 )
1171 )
1172 raise AuthconnNotFoundException(
1173 "User is not mapped with project '{}'".format(
1174 to_remove["project"]
1175 )
1176 )
Eduardo Sousa44603902019-06-04 08:10:32 +01001177
tiernocf042d32019-06-13 09:06:40 +00001178 # add
1179 for to_add in indata.get("add_project_role_mappings", ()):
1180 for mapping in original_mapping:
garciadeblas4568a372021-03-24 09:19:48 +01001181 if to_add["project"] in (
1182 mapping["project"],
1183 mapping["project_name"],
1184 ) and to_add["role"] in (
1185 mapping["role"],
1186 mapping["role_name"],
1187 ):
garciadeblas4568a372021-03-24 09:19:48 +01001188 if mapping in mappings_to_remove: # do not remove
tiernocf042d32019-06-13 09:06:40 +00001189 mappings_to_remove.remove(mapping)
1190 break # do not add, it is already at user
1191 else:
delacruzramo01b15d32019-07-02 14:37:47 +02001192 pid = self.auth.get_project(to_add["project"])["_id"]
1193 rid = self.auth.get_role(to_add["role"])["_id"]
1194 mappings_to_add.append({"project": pid, "role": rid})
tiernocf042d32019-06-13 09:06:40 +00001195
1196 # set
1197 if indata.get("project_role_mappings"):
Adurti16e6edd2024-03-25 08:21:36 +00001198 duplicates = []
1199 for pr in indata.get("project_role_mappings"):
1200 if pr not in duplicates:
1201 duplicates.append(pr)
1202 if len(indata.get("project_role_mappings")) > len(duplicates):
1203 raise EngineException(
1204 "Project-role combination should not be repeated",
1205 http_code=HTTPStatus.UNPROCESSABLE_ENTITY,
1206 )
tiernocf042d32019-06-13 09:06:40 +00001207 for to_set in indata["project_role_mappings"]:
1208 for mapping in original_mapping:
garciadeblas4568a372021-03-24 09:19:48 +01001209 if to_set["project"] in (
1210 mapping["project"],
1211 mapping["project_name"],
1212 ) and to_set["role"] in (
1213 mapping["role"],
1214 mapping["role_name"],
1215 ):
1216 if mapping in mappings_to_remove: # do not remove
tiernocf042d32019-06-13 09:06:40 +00001217 mappings_to_remove.remove(mapping)
1218 break # do not add, it is already at user
1219 else:
delacruzramo01b15d32019-07-02 14:37:47 +02001220 pid = self.auth.get_project(to_set["project"])["_id"]
1221 rid = self.auth.get_role(to_set["role"])["_id"]
1222 mappings_to_add.append({"project": pid, "role": rid})
tiernocf042d32019-06-13 09:06:40 +00001223 for mapping in original_mapping:
1224 for to_set in indata["project_role_mappings"]:
garciadeblas4568a372021-03-24 09:19:48 +01001225 if to_set["project"] in (
1226 mapping["project"],
1227 mapping["project_name"],
1228 ) and to_set["role"] in (
1229 mapping["role"],
1230 mapping["role_name"],
1231 ):
tiernocf042d32019-06-13 09:06:40 +00001232 break
1233 else:
1234 # delete
garciadeblas4568a372021-03-24 09:19:48 +01001235 if mapping not in mappings_to_remove: # do not remove
tiernocf042d32019-06-13 09:06:40 +00001236 mappings_to_remove.append(mapping)
1237
garciadeblas4568a372021-03-24 09:19:48 +01001238 self.auth.update_user(
1239 {
1240 "_id": _id,
1241 "username": indata.get("username"),
1242 "password": indata.get("password"),
selvi.ja9a1fc82022-04-04 06:54:30 +00001243 "old_password": indata.get("old_password"),
garciadeblas4568a372021-03-24 09:19:48 +01001244 "add_project_role_mappings": mappings_to_add,
1245 "remove_project_role_mappings": mappings_to_remove,
garciadeblas6d83f8f2023-06-19 22:34:49 +02001246 "system_admin_id": indata.get("system_admin_id"),
1247 "unlock": indata.get("unlock"),
1248 "renew": indata.get("renew"),
garciadeblasf53612b2024-07-12 14:44:37 +02001249 "session_user": session.get("username"),
jeganbe1a3df2024-06-04 12:05:19 +00001250 "email_id": indata.get("email_id"),
Adurti9f2ac992024-03-25 10:58:29 +00001251 "remove_session_project": delete_session_project,
garciadeblas4568a372021-03-24 09:19:48 +01001252 }
1253 )
1254 data_to_send = {"_id": _id, "changes": indata}
agarwalat53471982020-10-08 13:06:14 +00001255 self._send_msg("edited", data_to_send, not_send_msg=None)
tiernocf042d32019-06-13 09:06:40 +00001256
delacruzramo01b15d32019-07-02 14:37:47 +02001257 # return _id
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001258 except ValidationError as e:
1259 raise EngineException(e, HTTPStatus.UNPROCESSABLE_ENTITY)
1260
tiernoc4e07d02020-08-14 14:25:32 +00001261 def list(self, session, filter_q=None, api_req=False):
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001262 """
1263 Get a list of the topic that matches a filter
tierno65ca36d2019-02-12 19:27:52 +01001264 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001265 :param filter_q: filter of data to be applied
K Sai Kirand010e3e2020-08-28 15:11:48 +05301266 :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 +01001267 :return: The list, it can be empty if no one match the filter.
1268 """
delacruzramo029405d2019-09-26 10:52:56 +02001269 user_list = self.auth.get_user_list(filter_q)
1270 if not session["allow_show_user_project_role"]:
1271 # Bug 853 - Default filtering
Adurti844f6482024-11-01 06:32:13 +00001272 user_list = [usr for usr in user_list if usr["_id"] == session["user_id"]]
delacruzramo029405d2019-09-26 10:52:56 +02001273 return user_list
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001274
tiernobee3bad2019-12-05 12:26:01 +00001275 def delete(self, session, _id, dry_run=False, not_send_msg=None):
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001276 """
1277 Delete item by its internal _id
1278
tierno65ca36d2019-02-12 19:27:52 +01001279 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001280 :param _id: server internal id
1281 :param force: indicates if deletion must be forced in case of conflict
1282 :param dry_run: make checking but do not delete
tiernobee3bad2019-12-05 12:26:01 +00001283 :param not_send_msg: To not send message (False) or store content (list) instead
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001284 :return: dictionary with deleted item _id. It raises EngineException on error: not found, conflict, ...
1285 """
tiernocf042d32019-06-13 09:06:40 +00001286 # Allow _id to be a name or uuid
delacruzramo01b15d32019-07-02 14:37:47 +02001287 user = self.auth.get_user(_id)
1288 uid = user["_id"]
1289 self.check_conflict_on_del(session, uid, user)
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001290 if not dry_run:
delacruzramo01b15d32019-07-02 14:37:47 +02001291 v = self.auth.delete_user(uid)
agarwalat53471982020-10-08 13:06:14 +00001292 self._send_msg("deleted", user, not_send_msg=not_send_msg)
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001293 return v
1294 return None
1295
1296
1297class ProjectTopicAuth(ProjectTopic):
tierno65ca36d2019-02-12 19:27:52 +01001298 # topic = "projects"
agarwalat53471982020-10-08 13:06:14 +00001299 topic_msg = "project"
Eduardo Sousa44603902019-06-04 08:10:32 +01001300 schema_new = project_new_schema
1301 schema_edit = project_edit_schema
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001302
1303 def __init__(self, db, fs, msg, auth):
delacruzramo32bab472019-09-13 12:24:22 +02001304 ProjectTopic.__init__(self, db, fs, msg, auth)
1305 # self.auth = auth
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001306
tierno65ca36d2019-02-12 19:27:52 +01001307 def check_conflict_on_new(self, session, indata):
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001308 """
1309 Check that the data to be inserted is valid
1310
tierno65ca36d2019-02-12 19:27:52 +01001311 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001312 :param indata: data to be inserted
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001313 :return: None or raises EngineException
1314 """
tiernocf042d32019-06-13 09:06:40 +00001315 project_name = indata.get("name")
1316 if is_valid_uuid(project_name):
garciadeblas4568a372021-03-24 09:19:48 +01001317 raise EngineException(
1318 "project name '{}' cannot have an uuid format".format(project_name),
1319 HTTPStatus.UNPROCESSABLE_ENTITY,
1320 )
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001321
tiernocf042d32019-06-13 09:06:40 +00001322 project_list = self.auth.get_project_list(filter_q={"name": project_name})
1323
1324 if project_list:
garciadeblas4568a372021-03-24 09:19:48 +01001325 raise EngineException(
1326 "project '{}' exists".format(project_name), HTTPStatus.CONFLICT
1327 )
tiernocf042d32019-06-13 09:06:40 +00001328
1329 def check_conflict_on_edit(self, session, final_content, edit_content, _id):
1330 """
1331 Check that the data to be edited/uploaded is valid
1332
1333 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
1334 :param final_content: data once modified
1335 :param edit_content: incremental data that contains the modifications to apply
1336 :param _id: internal _id
1337 :return: None or raises EngineException
1338 """
1339
1340 project_name = edit_content.get("name")
delacruzramo01b15d32019-07-02 14:37:47 +02001341 if project_name != final_content["name"]: # It is a true renaming
tiernocf042d32019-06-13 09:06:40 +00001342 if is_valid_uuid(project_name):
garciadeblas4568a372021-03-24 09:19:48 +01001343 raise EngineException(
1344 "project name '{}' cannot have an uuid format".format(project_name),
1345 HTTPStatus.UNPROCESSABLE_ENTITY,
1346 )
tiernocf042d32019-06-13 09:06:40 +00001347
delacruzramo01b15d32019-07-02 14:37:47 +02001348 if final_content["name"] == "admin":
garciadeblas4568a372021-03-24 09:19:48 +01001349 raise EngineException(
1350 "You cannot rename project 'admin'", http_code=HTTPStatus.CONFLICT
1351 )
delacruzramo01b15d32019-07-02 14:37:47 +02001352
tiernocf042d32019-06-13 09:06:40 +00001353 # Check that project name is not used, regardless keystone already checks this
garciadeblas4568a372021-03-24 09:19:48 +01001354 if project_name and self.auth.get_project_list(
1355 filter_q={"name": project_name}
1356 ):
1357 raise EngineException(
1358 "project '{}' is already used".format(project_name),
1359 HTTPStatus.CONFLICT,
1360 )
bravofb995ea22021-02-10 10:57:52 -03001361 return final_content
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001362
tiernob4844ab2019-05-23 08:42:12 +00001363 def check_conflict_on_del(self, session, _id, db_content):
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001364 """
1365 Check if deletion can be done because of dependencies if it is not force. To override
1366
tierno65ca36d2019-02-12 19:27:52 +01001367 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001368 :param _id: internal _id
tiernob4844ab2019-05-23 08:42:12 +00001369 :param db_content: The database content of this item _id
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001370 :return: None if ok or raises EngineException with the conflict
1371 """
delacruzramo01b15d32019-07-02 14:37:47 +02001372
1373 def check_rw_projects(topic, title, id_field):
1374 for desc in self.db.get_list(topic):
garciadeblas4568a372021-03-24 09:19:48 +01001375 if (
1376 _id
1377 in desc["_admin"]["projects_read"]
1378 + desc["_admin"]["projects_write"]
1379 ):
1380 raise EngineException(
1381 "Project '{}' ({}) is being used by {} '{}'".format(
1382 db_content["name"], _id, title, desc[id_field]
1383 ),
1384 HTTPStatus.CONFLICT,
1385 )
delacruzramo01b15d32019-07-02 14:37:47 +02001386
1387 if _id in session["project_id"]:
garciadeblas4568a372021-03-24 09:19:48 +01001388 raise EngineException(
1389 "You cannot delete your own project", http_code=HTTPStatus.CONFLICT
1390 )
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001391
delacruzramo01b15d32019-07-02 14:37:47 +02001392 if db_content["name"] == "admin":
garciadeblas4568a372021-03-24 09:19:48 +01001393 raise EngineException(
1394 "You cannot delete project 'admin'", http_code=HTTPStatus.CONFLICT
1395 )
delacruzramo01b15d32019-07-02 14:37:47 +02001396
1397 # If any user is using this project, raise CONFLICT exception
1398 if not session["force"]:
1399 for user in self.auth.get_user_list():
tierno1546f2a2019-08-20 15:38:11 +00001400 for prm in user.get("project_role_mappings"):
1401 if prm["project"] == _id:
garciadeblas4568a372021-03-24 09:19:48 +01001402 raise EngineException(
1403 "Project '{}' ({}) is being used by user '{}'".format(
1404 db_content["name"], _id, user["username"]
1405 ),
1406 HTTPStatus.CONFLICT,
1407 )
delacruzramo01b15d32019-07-02 14:37:47 +02001408
1409 # If any VNFD, NSD, NST, PDU, etc. is using this project, raise CONFLICT exception
1410 if not session["force"]:
1411 check_rw_projects("vnfds", "VNF Descriptor", "id")
1412 check_rw_projects("nsds", "NS Descriptor", "id")
1413 check_rw_projects("nsts", "NS Template", "id")
1414 check_rw_projects("pdus", "PDU Descriptor", "name")
1415
tierno65ca36d2019-02-12 19:27:52 +01001416 def new(self, rollback, session, indata=None, kwargs=None, headers=None):
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001417 """
1418 Creates a new entry into the authentication backend.
1419
1420 NOTE: Overrides BaseTopic functionality because it doesn't require access to database.
1421
1422 :param rollback: list to append created items at database in case a rollback may to be done
tierno65ca36d2019-02-12 19:27:52 +01001423 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001424 :param indata: data to be inserted
1425 :param kwargs: used to override the indata descriptor
1426 :param headers: http request headers
delacruzramo01b15d32019-07-02 14:37:47 +02001427 :return: _id: identity of the inserted data, operation _id (None)
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001428 """
1429 try:
1430 content = BaseTopic._remove_envelop(indata)
1431
1432 # Override descriptor with query string kwargs
1433 BaseTopic._update_input_with_kwargs(content, kwargs)
tierno65ca36d2019-02-12 19:27:52 +01001434 content = self._validate_input_new(content, session["force"])
1435 self.check_conflict_on_new(session, content)
garciadeblas4568a372021-03-24 09:19:48 +01001436 self.format_on_new(
1437 content, project_id=session["project_id"], make_public=session["public"]
1438 )
garciadeblasb6025472024-08-15 09:50:55 +02001439 self.create_gitname(content, session)
delacruzramo01b15d32019-07-02 14:37:47 +02001440 _id = self.auth.create_project(content)
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001441 rollback.append({"topic": self.topic, "_id": _id})
agarwalat53471982020-10-08 13:06:14 +00001442 self._send_msg("created", content, not_send_msg=None)
delacruzramo01b15d32019-07-02 14:37:47 +02001443 return _id, None
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001444 except ValidationError as e:
1445 raise EngineException(e, HTTPStatus.UNPROCESSABLE_ENTITY)
1446
K Sai Kiran57589552021-01-27 21:38:34 +05301447 def show(self, session, _id, filter_q=None, api_req=False):
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001448 """
1449 Get complete information on an topic
1450
tierno65ca36d2019-02-12 19:27:52 +01001451 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001452 :param _id: server internal id
K Sai Kiran57589552021-01-27 21:38:34 +05301453 :param filter_q: dict: query parameter
K Sai Kirand010e3e2020-08-28 15:11:48 +05301454 :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 +01001455 :return: dictionary, raise exception if not found.
1456 """
tiernocf042d32019-06-13 09:06:40 +00001457 # Allow _id to be a name or uuid
1458 filter_q = {self.id_field(self.topic, _id): _id}
delacruzramo029405d2019-09-26 10:52:56 +02001459 # projects = self.auth.get_project_list(filter_q=filter_q)
garciadeblas4568a372021-03-24 09:19:48 +01001460 projects = self.list(session, filter_q) # To allow default filtering (Bug 853)
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001461 if len(projects) == 1:
1462 return projects[0]
1463 elif len(projects) > 1:
1464 raise EngineException("Too many projects found", HTTPStatus.CONFLICT)
1465 else:
1466 raise EngineException("Project not found", HTTPStatus.NOT_FOUND)
1467
tiernoc4e07d02020-08-14 14:25:32 +00001468 def list(self, session, filter_q=None, api_req=False):
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001469 """
1470 Get a list of the topic that matches a filter
1471
tierno65ca36d2019-02-12 19:27:52 +01001472 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001473 :param filter_q: filter of data to be applied
1474 :return: The list, it can be empty if no one match the filter.
1475 """
delacruzramo029405d2019-09-26 10:52:56 +02001476 project_list = self.auth.get_project_list(filter_q)
1477 if not session["allow_show_user_project_role"]:
1478 # Bug 853 - Default filtering
Adurti844f6482024-11-01 06:32:13 +00001479 user = self.auth.get_user(session["user_id"])
delacruzramo029405d2019-09-26 10:52:56 +02001480 projects = [prm["project"] for prm in user["project_role_mappings"]]
1481 project_list = [proj for proj in project_list if proj["_id"] in projects]
1482 return project_list
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001483
tiernobee3bad2019-12-05 12:26:01 +00001484 def delete(self, session, _id, dry_run=False, not_send_msg=None):
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001485 """
1486 Delete item by its internal _id
1487
tierno65ca36d2019-02-12 19:27:52 +01001488 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001489 :param _id: server internal id
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001490 :param dry_run: make checking but do not delete
tiernobee3bad2019-12-05 12:26:01 +00001491 :param not_send_msg: To not send message (False) or store content (list) instead
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001492 :return: dictionary with deleted item _id. It raises EngineException on error: not found, conflict, ...
1493 """
tiernocf042d32019-06-13 09:06:40 +00001494 # Allow _id to be a name or uuid
delacruzramo01b15d32019-07-02 14:37:47 +02001495 proj = self.auth.get_project(_id)
1496 pid = proj["_id"]
1497 self.check_conflict_on_del(session, pid, proj)
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001498 if not dry_run:
delacruzramo01b15d32019-07-02 14:37:47 +02001499 v = self.auth.delete_project(pid)
agarwalat53471982020-10-08 13:06:14 +00001500 self._send_msg("deleted", proj, not_send_msg=None)
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001501 return v
1502 return None
1503
tierno4015b472019-06-10 13:57:29 +00001504 def edit(self, session, _id, indata=None, kwargs=None, content=None):
1505 """
1506 Updates a project entry.
1507
1508 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
1509 :param _id:
1510 :param indata: data to be inserted
1511 :param kwargs: used to override the indata descriptor
1512 :param content:
1513 :return: _id: identity of the inserted data.
1514 """
1515 indata = self._remove_envelop(indata)
1516
1517 # Override descriptor with query string kwargs
1518 if kwargs:
1519 BaseTopic._update_input_with_kwargs(indata, kwargs)
1520 try:
tierno4015b472019-06-10 13:57:29 +00001521 if not content:
1522 content = self.show(session, _id)
Frank Brydendeba68e2020-07-27 13:55:11 +00001523 indata = self._validate_input_edit(indata, content, force=session["force"])
bravofb995ea22021-02-10 10:57:52 -03001524 content = self.check_conflict_on_edit(session, content, indata, _id=_id)
delacruzramo01b15d32019-07-02 14:37:47 +02001525 self.format_on_edit(content, indata)
agarwalat53471982020-10-08 13:06:14 +00001526 content_original = copy.deepcopy(content)
delacruzramo32bab472019-09-13 12:24:22 +02001527 deep_update_rfc7396(content, indata)
delacruzramo01b15d32019-07-02 14:37:47 +02001528 self.auth.update_project(content["_id"], content)
agarwalat53471982020-10-08 13:06:14 +00001529 proj_data = {"_id": _id, "changes": indata, "original": content_original}
1530 self._send_msg("edited", proj_data, not_send_msg=None)
tierno4015b472019-06-10 13:57:29 +00001531 except ValidationError as e:
1532 raise EngineException(e, HTTPStatus.UNPROCESSABLE_ENTITY)
1533
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001534
1535class RoleTopicAuth(BaseTopic):
delacruzramoceb8baf2019-06-21 14:25:38 +02001536 topic = "roles"
garciadeblas4568a372021-03-24 09:19:48 +01001537 topic_msg = None # "roles"
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001538 schema_new = roles_new_schema
1539 schema_edit = roles_edit_schema
tierno65ca36d2019-02-12 19:27:52 +01001540 multiproject = False
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001541
tierno9e87a7f2020-03-23 09:24:10 +00001542 def __init__(self, db, fs, msg, auth):
delacruzramo32bab472019-09-13 12:24:22 +02001543 BaseTopic.__init__(self, db, fs, msg, auth)
1544 # self.auth = auth
tierno9e87a7f2020-03-23 09:24:10 +00001545 self.operations = auth.role_permissions
delacruzramo01b15d32019-07-02 14:37:47 +02001546 # self.topic = "roles_operations" if isinstance(auth, AuthconnKeystone) else "roles"
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001547
1548 @staticmethod
1549 def validate_role_definition(operations, role_definitions):
1550 """
1551 Validates the role definition against the operations defined in
1552 the resources to operations files.
1553
1554 :param operations: operations list
1555 :param role_definitions: role definition to test
1556 :return: None if ok, raises ValidationError exception on error
1557 """
tierno1f029d82019-06-13 22:37:04 +00001558 if not role_definitions.get("permissions"):
1559 return
1560 ignore_fields = ["admin", "default"]
1561 for role_def in role_definitions["permissions"].keys():
Eduardo Sousa37de0912019-05-23 02:17:22 +01001562 if role_def in ignore_fields:
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001563 continue
Eduardo Sousac7689372019-06-04 16:01:46 +01001564 if role_def[-1] == ":":
tierno1f029d82019-06-13 22:37:04 +00001565 raise ValidationError("Operation cannot end with ':'")
Eduardo Sousac5a18892019-06-06 14:51:23 +01001566
garciadeblas4568a372021-03-24 09:19:48 +01001567 match = next(
1568 (
1569 op
1570 for op in operations
1571 if op == role_def or op.startswith(role_def + ":")
1572 ),
1573 None,
1574 )
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001575
tierno97639b42020-08-04 12:48:15 +00001576 if not match:
tierno1f029d82019-06-13 22:37:04 +00001577 raise ValidationError("Invalid permission '{}'".format(role_def))
Eduardo Sousa37de0912019-05-23 02:17:22 +01001578
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001579 def _validate_input_new(self, input, force=False):
1580 """
1581 Validates input user content for a new entry.
1582
1583 :param input: user input content for the new topic
1584 :param force: may be used for being more tolerant
1585 :return: The same input content, or a changed version of it.
1586 """
1587 if self.schema_new:
1588 validate_input(input, self.schema_new)
Eduardo Sousa37de0912019-05-23 02:17:22 +01001589 self.validate_role_definition(self.operations, input)
Eduardo Sousac4650362019-06-04 13:24:22 +01001590
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001591 return input
1592
Frank Brydendeba68e2020-07-27 13:55:11 +00001593 def _validate_input_edit(self, input, content, force=False):
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001594 """
1595 Validates input user content for updating an entry.
1596
1597 :param input: user input content for the new topic
1598 :param force: may be used for being more tolerant
1599 :return: The same input content, or a changed version of it.
1600 """
1601 if self.schema_edit:
1602 validate_input(input, self.schema_edit)
Eduardo Sousa37de0912019-05-23 02:17:22 +01001603 self.validate_role_definition(self.operations, input)
Eduardo Sousac4650362019-06-04 13:24:22 +01001604
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001605 return input
1606
tierno65ca36d2019-02-12 19:27:52 +01001607 def check_conflict_on_new(self, session, indata):
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001608 """
1609 Check that the data to be inserted is valid
1610
tierno65ca36d2019-02-12 19:27:52 +01001611 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001612 :param indata: data to be inserted
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001613 :return: None or raises EngineException
1614 """
delacruzramo79e40f42019-10-10 16:36:40 +02001615 # check name is not uuid
1616 role_name = indata.get("name")
1617 if is_valid_uuid(role_name):
garciadeblas4568a372021-03-24 09:19:48 +01001618 raise EngineException(
1619 "role name '{}' cannot have an uuid format".format(role_name),
1620 HTTPStatus.UNPROCESSABLE_ENTITY,
1621 )
tierno1f029d82019-06-13 22:37:04 +00001622 # check name not exists
delacruzramo01b15d32019-07-02 14:37:47 +02001623 name = indata["name"]
1624 # if self.db.get_one(self.topic, {"name": indata.get("name")}, fail_on_empty=False, fail_on_more=False):
1625 if self.auth.get_role_list({"name": name}):
garciadeblas4568a372021-03-24 09:19:48 +01001626 raise EngineException(
1627 "role name '{}' exists".format(name), HTTPStatus.CONFLICT
1628 )
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001629
tierno65ca36d2019-02-12 19:27:52 +01001630 def check_conflict_on_edit(self, session, final_content, edit_content, _id):
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001631 """
1632 Check that the data to be edited/uploaded is valid
1633
tierno65ca36d2019-02-12 19:27:52 +01001634 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001635 :param final_content: data once modified
1636 :param edit_content: incremental data that contains the modifications to apply
1637 :param _id: internal _id
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001638 :return: None or raises EngineException
1639 """
tierno1f029d82019-06-13 22:37:04 +00001640 if "default" not in final_content["permissions"]:
1641 final_content["permissions"]["default"] = False
1642 if "admin" not in final_content["permissions"]:
1643 final_content["permissions"]["admin"] = False
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001644
delacruzramo79e40f42019-10-10 16:36:40 +02001645 # check name is not uuid
1646 role_name = edit_content.get("name")
1647 if is_valid_uuid(role_name):
garciadeblas4568a372021-03-24 09:19:48 +01001648 raise EngineException(
1649 "role name '{}' cannot have an uuid format".format(role_name),
1650 HTTPStatus.UNPROCESSABLE_ENTITY,
1651 )
delacruzramo79e40f42019-10-10 16:36:40 +02001652
1653 # Check renaming of admin roles
1654 role = self.auth.get_role(_id)
1655 if role["name"] in ["system_admin", "project_admin"]:
garciadeblas4568a372021-03-24 09:19:48 +01001656 raise EngineException(
1657 "You cannot rename role '{}'".format(role["name"]),
1658 http_code=HTTPStatus.FORBIDDEN,
1659 )
delacruzramo79e40f42019-10-10 16:36:40 +02001660
tierno1f029d82019-06-13 22:37:04 +00001661 # check name not exists
1662 if "name" in edit_content:
1663 role_name = edit_content["name"]
delacruzramo01b15d32019-07-02 14:37:47 +02001664 # if self.db.get_one(self.topic, {"name":role_name,"_id.ne":_id}, fail_on_empty=False, fail_on_more=False):
1665 roles = self.auth.get_role_list({"name": role_name})
1666 if roles and roles[0][BaseTopic.id_field("roles", _id)] != _id:
garciadeblas4568a372021-03-24 09:19:48 +01001667 raise EngineException(
1668 "role name '{}' exists".format(role_name), HTTPStatus.CONFLICT
1669 )
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001670
bravofb995ea22021-02-10 10:57:52 -03001671 return final_content
1672
tiernob4844ab2019-05-23 08:42:12 +00001673 def check_conflict_on_del(self, session, _id, db_content):
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001674 """
1675 Check if deletion can be done because of dependencies if it is not force. To override
1676
tierno65ca36d2019-02-12 19:27:52 +01001677 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001678 :param _id: internal _id
tiernob4844ab2019-05-23 08:42:12 +00001679 :param db_content: The database content of this item _id
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001680 :return: None if ok or raises EngineException with the conflict
1681 """
delacruzramo01b15d32019-07-02 14:37:47 +02001682 role = self.auth.get_role(_id)
1683 if role["name"] in ["system_admin", "project_admin"]:
garciadeblas4568a372021-03-24 09:19:48 +01001684 raise EngineException(
1685 "You cannot delete role '{}'".format(role["name"]),
1686 http_code=HTTPStatus.FORBIDDEN,
1687 )
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001688
delacruzramo01b15d32019-07-02 14:37:47 +02001689 # If any user is using this role, raise CONFLICT exception
delacruzramoad682a52019-12-10 16:26:34 +01001690 if not session["force"]:
1691 for user in self.auth.get_user_list():
1692 for prm in user.get("project_role_mappings"):
1693 if prm["role"] == _id:
garciadeblas4568a372021-03-24 09:19:48 +01001694 raise EngineException(
1695 "Role '{}' ({}) is being used by user '{}'".format(
1696 role["name"], _id, user["username"]
1697 ),
1698 HTTPStatus.CONFLICT,
1699 )
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001700
1701 @staticmethod
garciadeblas4568a372021-03-24 09:19:48 +01001702 def format_on_new(content, project_id=None, make_public=False): # TO BE REMOVED ?
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001703 """
1704 Modifies content descriptor to include _admin
1705
1706 :param content: descriptor to be modified
1707 :param project_id: if included, it add project read/write permissions
1708 :param make_public: if included it is generated as public for reading.
1709 :return: None, but content is modified
1710 """
1711 now = time()
1712 if "_admin" not in content:
1713 content["_admin"] = {}
1714 if not content["_admin"].get("created"):
1715 content["_admin"]["created"] = now
1716 content["_admin"]["modified"] = now
Eduardo Sousac4650362019-06-04 13:24:22 +01001717
tierno1f029d82019-06-13 22:37:04 +00001718 if "permissions" not in content:
1719 content["permissions"] = {}
Eduardo Sousac4650362019-06-04 13:24:22 +01001720
tierno1f029d82019-06-13 22:37:04 +00001721 if "default" not in content["permissions"]:
1722 content["permissions"]["default"] = False
1723 if "admin" not in content["permissions"]:
1724 content["permissions"]["admin"] = False
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001725
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001726 @staticmethod
1727 def format_on_edit(final_content, edit_content):
1728 """
1729 Modifies final_content descriptor to include the modified date.
1730
1731 :param final_content: final descriptor generated
1732 :param edit_content: alterations to be include
1733 :return: None, but final_content is modified
1734 """
delacruzramo01b15d32019-07-02 14:37:47 +02001735 if "_admin" in final_content:
1736 final_content["_admin"]["modified"] = time()
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001737
tierno1f029d82019-06-13 22:37:04 +00001738 if "permissions" not in final_content:
1739 final_content["permissions"] = {}
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001740
tierno1f029d82019-06-13 22:37:04 +00001741 if "default" not in final_content["permissions"]:
1742 final_content["permissions"]["default"] = False
1743 if "admin" not in final_content["permissions"]:
1744 final_content["permissions"]["admin"] = False
tiernobdebce92019-07-01 15:36:49 +00001745 return None
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001746
K Sai Kiran57589552021-01-27 21:38:34 +05301747 def show(self, session, _id, filter_q=None, api_req=False):
delacruzramo01b15d32019-07-02 14:37:47 +02001748 """
1749 Get complete information on an topic
Eduardo Sousac4650362019-06-04 13:24:22 +01001750
delacruzramo01b15d32019-07-02 14:37:47 +02001751 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
1752 :param _id: server internal id
K Sai Kiran57589552021-01-27 21:38:34 +05301753 :param filter_q: dict: query parameter
K Sai Kirand010e3e2020-08-28 15:11:48 +05301754 :param api_req: True if this call is serving an external API request. False if serving internal request.
delacruzramo01b15d32019-07-02 14:37:47 +02001755 :return: dictionary, raise exception if not found.
1756 """
1757 filter_q = {BaseTopic.id_field(self.topic, _id): _id}
delacruzramo029405d2019-09-26 10:52:56 +02001758 # roles = self.auth.get_role_list(filter_q)
garciadeblas4568a372021-03-24 09:19:48 +01001759 roles = self.list(session, filter_q) # To allow default filtering (Bug 853)
delacruzramo01b15d32019-07-02 14:37:47 +02001760 if not roles:
garciadeblas4568a372021-03-24 09:19:48 +01001761 raise AuthconnNotFoundException(
1762 "Not found any role with filter {}".format(filter_q)
1763 )
delacruzramo01b15d32019-07-02 14:37:47 +02001764 elif len(roles) > 1:
garciadeblas4568a372021-03-24 09:19:48 +01001765 raise AuthconnConflictException(
1766 "Found more than one role with filter {}".format(filter_q)
1767 )
delacruzramo01b15d32019-07-02 14:37:47 +02001768 return roles[0]
1769
tiernoc4e07d02020-08-14 14:25:32 +00001770 def list(self, session, filter_q=None, api_req=False):
delacruzramo01b15d32019-07-02 14:37:47 +02001771 """
1772 Get a list of the topic that matches a filter
1773
1774 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
1775 :param filter_q: filter of data to be applied
1776 :return: The list, it can be empty if no one match the filter.
1777 """
delacruzramo029405d2019-09-26 10:52:56 +02001778 role_list = self.auth.get_role_list(filter_q)
1779 if not session["allow_show_user_project_role"]:
1780 # Bug 853 - Default filtering
Adurti844f6482024-11-01 06:32:13 +00001781 user = self.auth.get_user(session["user_id"])
delacruzramo029405d2019-09-26 10:52:56 +02001782 roles = [prm["role"] for prm in user["project_role_mappings"]]
1783 role_list = [role for role in role_list if role["_id"] in roles]
1784 return role_list
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001785
tierno65ca36d2019-02-12 19:27:52 +01001786 def new(self, rollback, session, indata=None, kwargs=None, headers=None):
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001787 """
1788 Creates a new entry into database.
1789
1790 :param rollback: list to append created items at database in case a rollback may to be done
tierno65ca36d2019-02-12 19:27:52 +01001791 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001792 :param indata: data to be inserted
1793 :param kwargs: used to override the indata descriptor
1794 :param headers: http request headers
delacruzramo01b15d32019-07-02 14:37:47 +02001795 :return: _id: identity of the inserted data, operation _id (None)
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001796 """
1797 try:
tierno1f029d82019-06-13 22:37:04 +00001798 content = self._remove_envelop(indata)
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001799
1800 # Override descriptor with query string kwargs
tierno1f029d82019-06-13 22:37:04 +00001801 self._update_input_with_kwargs(content, kwargs)
tierno65ca36d2019-02-12 19:27:52 +01001802 content = self._validate_input_new(content, session["force"])
1803 self.check_conflict_on_new(session, content)
garciadeblas4568a372021-03-24 09:19:48 +01001804 self.format_on_new(
1805 content, project_id=session["project_id"], make_public=session["public"]
1806 )
delacruzramo01b15d32019-07-02 14:37:47 +02001807 # role_name = content["name"]
1808 rid = self.auth.create_role(content)
1809 content["_id"] = rid
1810 # _id = self.db.create(self.topic, content)
1811 rollback.append({"topic": self.topic, "_id": rid})
tiernobee3bad2019-12-05 12:26:01 +00001812 # self._send_msg("created", content, not_send_msg=not_send_msg)
delacruzramo01b15d32019-07-02 14:37:47 +02001813 return rid, None
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001814 except ValidationError as e:
1815 raise EngineException(e, HTTPStatus.UNPROCESSABLE_ENTITY)
1816
tiernobee3bad2019-12-05 12:26:01 +00001817 def delete(self, session, _id, dry_run=False, not_send_msg=None):
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001818 """
1819 Delete item by its internal _id
1820
tierno65ca36d2019-02-12 19:27:52 +01001821 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001822 :param _id: server internal id
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001823 :param dry_run: make checking but do not delete
tiernobee3bad2019-12-05 12:26:01 +00001824 :param not_send_msg: To not send message (False) or store content (list) instead
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001825 :return: dictionary with deleted item _id. It raises EngineException on error: not found, conflict, ...
1826 """
delacruzramo01b15d32019-07-02 14:37:47 +02001827 filter_q = {BaseTopic.id_field(self.topic, _id): _id}
1828 roles = self.auth.get_role_list(filter_q)
1829 if not roles:
garciadeblas4568a372021-03-24 09:19:48 +01001830 raise AuthconnNotFoundException(
1831 "Not found any role with filter {}".format(filter_q)
1832 )
delacruzramo01b15d32019-07-02 14:37:47 +02001833 elif len(roles) > 1:
garciadeblas4568a372021-03-24 09:19:48 +01001834 raise AuthconnConflictException(
1835 "Found more than one role with filter {}".format(filter_q)
1836 )
delacruzramo01b15d32019-07-02 14:37:47 +02001837 rid = roles[0]["_id"]
1838 self.check_conflict_on_del(session, rid, None)
delacruzramoceb8baf2019-06-21 14:25:38 +02001839 # filter_q = {"_id": _id}
delacruzramo01b15d32019-07-02 14:37:47 +02001840 # filter_q = {BaseTopic.id_field(self.topic, _id): _id} # To allow role addressing by name
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001841 if not dry_run:
delacruzramo01b15d32019-07-02 14:37:47 +02001842 v = self.auth.delete_role(rid)
1843 # v = self.db.del_one(self.topic, filter_q)
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001844 return v
1845 return None
1846
tierno65ca36d2019-02-12 19:27:52 +01001847 def edit(self, session, _id, indata=None, kwargs=None, content=None):
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001848 """
1849 Updates a role entry.
1850
tierno65ca36d2019-02-12 19:27:52 +01001851 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001852 :param _id:
1853 :param indata: data to be inserted
1854 :param kwargs: used to override the indata descriptor
Eduardo Sousa5c01e192019-05-08 02:35:47 +01001855 :param content:
1856 :return: _id: identity of the inserted data.
1857 """
delacruzramo01b15d32019-07-02 14:37:47 +02001858 if kwargs:
1859 self._update_input_with_kwargs(indata, kwargs)
1860 try:
delacruzramo01b15d32019-07-02 14:37:47 +02001861 if not content:
1862 content = self.show(session, _id)
Frank Brydendeba68e2020-07-27 13:55:11 +00001863 indata = self._validate_input_edit(indata, content, force=session["force"])
delacruzramo01b15d32019-07-02 14:37:47 +02001864 deep_update_rfc7396(content, indata)
bravofb995ea22021-02-10 10:57:52 -03001865 content = self.check_conflict_on_edit(session, content, indata, _id=_id)
delacruzramo01b15d32019-07-02 14:37:47 +02001866 self.format_on_edit(content, indata)
1867 self.auth.update_role(content)
1868 except ValidationError as e:
1869 raise EngineException(e, HTTPStatus.UNPROCESSABLE_ENTITY)