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