fix user/project creation with domain_name. 17/8617/8
authortierno <alfonso.tiernosepulveda@telefonica.com>
Wed, 19 Feb 2020 14:29:49 +0000 (14:29 +0000)
committertierno <alfonso.tiernosepulveda@telefonica.com>
Fri, 28 Feb 2020 15:41:26 +0000 (15:41 +0000)
Provide the domain when listing users/projects
Allow filtering by domain

Change-Id: Ieccf9ad40f3ef7896e99ee26128d0b590cf615be
Signed-off-by: tierno <alfonso.tiernosepulveda@telefonica.com>
Dockerfile.local
osm_nbi/admin_topics.py
osm_nbi/authconn_keystone.py
osm_nbi/validation.py

index 7c91bc6..81ba1cb 100644 (file)
@@ -85,7 +85,10 @@ ENV OSMNBI_AUTHENTICATION_BACKEND               internal
 #ENV OSMNBI_AUTHENTICATION_AUTH_URL              keystone
 #ENV OSMNBI_AUTHENTICATION_AUTH_PORT             5000
 
-# DOMAIN_NAME can be a single value or a comma separated list of values
+# DOMAIN_NAME can be a single value or a comma separated list of values. The first value is for internal domain
+# meanwhile the rest ones are external identity services as e.g. LDAP
+# for the case of USER_DOMAIN_NAME each item can be suffixed with ":ro" (read only). In this case NBI will reject any
+# operationi of create/update/delete users in this domain. Do not suffix PROJECT_DOMAIN_NAME
 #ENV OSMNBI_AUTHENTICATION_USER_DOMAIN_NAME      default
 #ENV OSMNBI_AUTHENTICATION_PROJECT_DOMAIN_NAME   default
 #ENV OSMNBI_AUTHENTICATION_SERVICE_USERNAME      nbi
index a564eae..565d763 100644 (file)
@@ -660,7 +660,7 @@ class UserTopicAuth(UserTopic):
         :return: dictionary, raise exception if not found.
         """
         # Allow _id to be a name or uuid
-        filter_q = {self.id_field(self.topic, _id): _id}
+        filter_q = {"username": _id}
         # users = self.auth.get_user_list(filter_q)
         users = self.list(session, filter_q)   # To allow default filtering (Bug 853)
         if len(users) == 1:
index 748b2e7..c69d64b 100644 (file)
@@ -49,10 +49,17 @@ class AuthconnKeystone(Authconn):
         Authconn.__init__(self, config, db)
 
         self.logger = logging.getLogger("nbi.authenticator.keystone")
+        self.domains_id2name = {}
+        self.domains_name2id = {}
 
         self.auth_url = "http://{0}:{1}/v3".format(config.get("auth_url", "keystone"), config.get("auth_port", "5000"))
         self.user_domain_name_list = config.get("user_domain_name", "default")
         self.user_domain_name_list = self.user_domain_name_list.split(",")
+        # read only domain list
+        self.user_domain_ro_list = [x[:-3] for x in self.user_domain_name_list if x.endswith(":ro")]
+        # remove the ":ro"
+        self.user_domain_name_list = [x if not x.endswith(":ro") else x[:-3] for x in self.user_domain_name_list]
+
         self.admin_project = config.get("service_project", "service")
         self.admin_username = config.get("service_username", "nbi")
         self.admin_password = config.get("service_password", "nbi")
@@ -235,6 +242,37 @@ class AuthconnKeystone(Authconn):
             raise AuthException("Error during token revocation using Keystone: {}".format(e),
                                 http_code=HTTPStatus.UNAUTHORIZED)
 
+    def _get_domain_id(self, domain_name, fail_if_not_found=True):
+        """
+        Get the domain id from  the domain_name
+        :param domain_name: Can be the name or id
+        :param fail_if_not_found: If False it returns None instead of raising an exception if not found
+        :return: str or None/exception if domain is not found
+        """
+        domain_id = self.domains_name2id.get(domain_name)
+        if not domain_id:
+            self._get_domains()
+            domain_id = self.domains_name2id.get(domain_name)
+        if not domain_id and domain_name in self.domains_id2name:
+            # domain_name is already an id
+            return domain_name
+        if not domain_id and fail_if_not_found:
+            raise AuthconnNotFoundException("Domain {} cannot be found".format(domain_name))
+        return domain_id
+
+    def _get_domains(self):
+        """
+        Obtain a dictionary with domain_id to domain_name, stored at self.domains_id2name
+        and from domain_name to domain_id, sored at self.domains_name2id
+        :return: None. Exceptions are ignored
+        """
+        try:
+            domains = self.keystone.domains.list()
+            self.domains_id2name = {x.id: x.name for x in domains}
+            self.domains_name2id = {x.name: x.id for x in domains}
+        except Exception:
+            pass
+
     def create_user(self, user_info):
         """
         Create a user.
@@ -244,13 +282,18 @@ class AuthconnKeystone(Authconn):
         :return: returns the id of the user in keystone.
         """
         try:
+
+            if user_info.get("domain_name") and user_info["domain_name"] in self.user_domain_ro_list:
+                raise AuthconnConflictException("Cannot create a user in the read only domain {}".
+                                                format(user_info["domain_name"]))
+
             new_user = self.keystone.users.create(
                 user_info["username"], password=user_info["password"],
-                domain=user_info.get("user_domain_name", self.user_domain_name_list[0]),
+                domain=self._get_domain_id(user_info.get("domain_name", self.user_domain_name_list[0])),
                 _admin=user_info["_admin"])
             if "project_role_mappings" in user_info.keys():
                 for mapping in user_info["project_role_mappings"]:
-                    self.assign_role_to_user(new_user.id, mapping["project"], mapping["role"])
+                    self.assign_role_to_user(new_user, mapping["project"], mapping["role"])
             return {"username": new_user.name, "_id": new_user.id}
         except Conflict as e:
             # self.logger.exception("Error during user creation using keystone: {}".format(e))
@@ -268,23 +311,42 @@ class AuthconnKeystone(Authconn):
         """
         try:
             user = user_info.get("_id") or user_info.get("username")
-            if is_valid_uuid(user):
-                user_obj_list = [self.keystone.users.get(user)]
-            else:
-                user_obj_list = self.keystone.users.list(name=user)
-            if not user_obj_list:
-                raise AuthconnNotFoundException("User '{}' not found".format(user))
-            user_obj = user_obj_list[0]
+            try:
+                user_obj = self.keystone.users.get(user)
+            except Exception:
+                user_obj = None
+            if not user_obj:
+                for user_domain in self.user_domain_name_list:
+                    domain_id = self._get_domain_id(user_domain, fail_if_not_found=False)
+                    if not domain_id:
+                        continue
+                    user_obj_list = self.keystone.users.list(name=user, domain=domain_id)
+                    if user_obj_list:
+                        user_obj = user_obj_list[0]
+                        break
+                else:   # user not found
+                    raise AuthconnNotFoundException("User '{}' not found".format(user))
+
             user_id = user_obj.id
-            if user_info.get("password") or user_info.get("username") \
+            domain_id = user_obj.domain_id
+            domain_name = self.domains_id2name.get(domain_id)
+
+            if domain_name in self.user_domain_ro_list:
+                if user_info.get("password") or user_info.get("username"):
+                    raise AuthconnConflictException("Cannot update the user {} belonging to a read only domain {}".
+                                                    format(user, domain_name))
+
+            elif user_info.get("password") or user_info.get("username") \
                     or user_info.get("add_project_role_mappings") or user_info.get("remove_project_role_mappings"):
+                # if user_index>0, it is an external domain, that should not be updated
                 ctime = user_obj._admin.get("created", 0) if hasattr(user_obj, "_admin") else 0
                 self.keystone.users.update(user_id, password=user_info.get("password"), name=user_info.get("username"),
                                            _admin={"created": ctime, "modified": time.time()})
+
             for mapping in user_info.get("remove_project_role_mappings", []):
-                self.remove_role_from_user(user_id, mapping["project"], mapping["role"])
+                self.remove_role_from_user(user_obj, mapping["project"], mapping["role"])
             for mapping in user_info.get("add_project_role_mappings", []):
-                self.assign_role_to_user(user_id, mapping["project"], mapping["role"])
+                self.assign_role_to_user(user_obj, mapping["project"], mapping["role"])
         except ClientException as e:
             # self.logger.exception("Error during user password/name update using keystone: {}".format(e))
             raise AuthconnOperationException("Error during user update using Keystone: {}".format(e))
@@ -297,6 +359,13 @@ class AuthconnKeystone(Authconn):
         :raises AuthconnOperationException: if user deletion failed.
         """
         try:
+            user_obj = self.keystone.users.get(user_id)
+            domain_id = user_obj.domain_id
+            domain_name = self.domains_id2name.get(domain_id)
+            if domain_name in self.user_domain_ro_list:
+                raise AuthconnConflictException("Cannot delete user {} belonging to a read only domain {}".
+                                                format(user_id, domain_name))
+
             result, detail = self.keystone.users.delete(user_id)
             if result.status_code != 204:
                 raise ClientException("error {} {}".format(result.status_code, detail))
@@ -309,19 +378,53 @@ class AuthconnKeystone(Authconn):
         """
         Get user list.
 
-        :param filter_q: dictionary to filter user list by name (username is also admited) and/or _id
+        :param filter_q: dictionary to filter user list by one or several
+            _id:
+            name (username is also admitted).  If a user id is equal to the filter name, it is also provided
+            domain_id, domain_name
         :return: returns a list of users.
         """
         try:
-            filter_name = None
+            self._get_domains()
+            filter_name = filter_domain = None
             if filter_q:
                 filter_name = filter_q.get("name") or filter_q.get("username")
-            users = self.keystone.users.list(name=filter_name)
+                if filter_q.get("domain_name"):
+                    filter_domain = self._get_domain_id(filter_q["domain_name"], fail_if_not_found=False)
+                    # If domain is not found, use the same name to obtain an empty list
+                    filter_domain = filter_domain or filter_q["domain_name"]
+                if filter_q.get("domain_id"):
+                    filter_domain = filter_q["domain_id"]
+
+            users = self.keystone.users.list(name=filter_name, domain=filter_domain)
+            # get users from user_domain_name_list[1:], because it will not be provided in case of LDAP
+            if filter_domain is None and len(self.user_domain_name_list) > 1:
+                for user_domain in self.user_domain_name_list[1:]:
+                    domain_id = self._get_domain_id(user_domain, fail_if_not_found=False)
+                    if not domain_id:
+                        continue
+                    # find if users of this domain are already provided. In this case ignore
+                    for u in users:
+                        if u.domain_id == domain_id:
+                            break
+                    else:
+                        users += self.keystone.users.list(name=filter_name, domain=domain_id)
+
+            # if filter name matches a user id, provide it also
+            if filter_name:
+                try:
+                    user_obj = self.keystone.users.get(filter_name)
+                    if user_obj not in users:
+                        users.append(user_obj)
+                except Exception:
+                    pass
+
             users = [{
                 "username": user.name,
                 "_id": user.id,
                 "id": user.id,
-                "_admin": user.to_dict().get("_admin", {})   # TODO: REVISE
+                "_admin": user.to_dict().get("_admin", {}),   # TODO: REVISE
+                "domain_name": self.domains_id2name.get(user.domain_id)
             } for user in users if user.name != self.admin_username]
 
             if filter_q and filter_q.get("_id"):
@@ -440,16 +543,23 @@ class AuthconnKeystone(Authconn):
         :return: list of projects
         """
         try:
-            filter_name = None
+            self._get_domains()
+            filter_name = filter_domain = None
             if filter_q:
                 filter_name = filter_q.get("name")
-            projects = self.keystone.projects.list(name=filter_name)
+                if filter_q.get("domain_name"):
+                    filter_domain = self.domains_name2id.get(filter_q["domain_name"])
+                if filter_q.get("domain_id"):
+                    filter_domain = filter_q["domain_id"]
+
+            projects = self.keystone.projects.list(name=filter_name, domain=filter_domain)
 
             projects = [{
                 "name": project.name,
                 "_id": project.id,
                 "_admin": project.to_dict().get("_admin", {}),  # TODO: REVISE
                 "quotas": project.to_dict().get("quotas", {}),  # TODO: REVISE
+                "domain_name": self.domains_id2name.get(project.domain_id)
             } for project in projects]
 
             if filter_q and filter_q.get("_id"):
@@ -473,7 +583,7 @@ class AuthconnKeystone(Authconn):
         try:
             result = self.keystone.projects.create(
                 project_info["name"],
-                project_info.get("project_domain_name", self.project_domain_name_list[0]),
+                domain=self._get_domain_id(project_info.get("domain_name", self.project_domain_name_list[0])),
                 _admin=project_info["_admin"],
                 quotas=project_info.get("quotas", {})
             )
@@ -519,35 +629,27 @@ class AuthconnKeystone(Authconn):
             # self.logger.exception("Error during project update using keystone: {}".format(e))
             raise AuthconnOperationException("Error during project update using Keystone: {}".format(e))
 
-    def assign_role_to_user(self, user, project, role):
+    def assign_role_to_user(self, user_obj, project, role):
         """
         Assigning a role to a user in a project.
 
-        :param user: username.
+        :param user_obj: user object, obtained with keystone.users.get or list.
         :param project: project name.
         :param role: role name.
         :raises AuthconnOperationException: if role assignment failed.
         """
         try:
-            if is_valid_uuid(user):
-                user_obj = self.keystone.users.get(user)
-            else:
-                user_obj_list = self.keystone.users.list(name=user)
-                if not user_obj_list:
-                    raise AuthconnNotFoundException("User '{}' not found".format(user))
-                user_obj = user_obj_list[0]
-
-            if is_valid_uuid(project):
+            try:
                 project_obj = self.keystone.projects.get(project)
-            else:
+            except Exception:
                 project_obj_list = self.keystone.projects.list(name=project)
                 if not project_obj_list:
                     raise AuthconnNotFoundException("Project '{}' not found".format(project))
                 project_obj = project_obj_list[0]
 
-            if is_valid_uuid(role):
+            try:
                 role_obj = self.keystone.roles.get(role)
-            else:
+            except Exception:
                 role_obj_list = self.keystone.roles.list(name=role)
                 if not role_obj_list:
                     raise AuthconnNotFoundException("Role '{}' not found".format(role))
@@ -557,38 +659,30 @@ class AuthconnKeystone(Authconn):
         except ClientException as e:
             # self.logger.exception("Error during user role assignment using keystone: {}".format(e))
             raise AuthconnOperationException("Error during role '{}' assignment to user '{}' and project '{}' using "
-                                             "Keystone: {}".format(role, user, project, e))
+                                             "Keystone: {}".format(role, user_obj.name, project, e))
 
-    def remove_role_from_user(self, user, project, role):
+    def remove_role_from_user(self, user_obj, project, role):
         """
         Remove a role from a user in a project.
 
-        :param user: username.
+        :param user_obj: user object, obtained with keystone.users.get or list.
         :param project: project name or id.
         :param role: role name or id.
 
         :raises AuthconnOperationException: if role assignment revocation failed.
         """
         try:
-            if is_valid_uuid(user):
-                user_obj = self.keystone.users.get(user)
-            else:
-                user_obj_list = self.keystone.users.list(name=user)
-                if not user_obj_list:
-                    raise AuthconnNotFoundException("User '{}' not found".format(user))
-                user_obj = user_obj_list[0]
-
-            if is_valid_uuid(project):
+            try:
                 project_obj = self.keystone.projects.get(project)
-            else:
+            except Exception:
                 project_obj_list = self.keystone.projects.list(name=project)
                 if not project_obj_list:
                     raise AuthconnNotFoundException("Project '{}' not found".format(project))
                 project_obj = project_obj_list[0]
 
-            if is_valid_uuid(role):
+            try:
                 role_obj = self.keystone.roles.get(role)
-            else:
+            except Exception:
                 role_obj_list = self.keystone.roles.list(name=role)
                 if not role_obj_list:
                     raise AuthconnNotFoundException("Role '{}' not found".format(role))
@@ -598,4 +692,4 @@ class AuthconnKeystone(Authconn):
         except ClientException as e:
             # self.logger.exception("Error during user role revocation using keystone: {}".format(e))
             raise AuthconnOperationException("Error during role '{}' revocation to user '{}' and project '{}' using "
-                                             "Keystone: {}".format(role, user, project, e))
+                                             "Keystone: {}".format(role, user_obj.name, project, e))
index 13b16d7..3615943 100644 (file)
@@ -734,7 +734,7 @@ user_new_schema = {
     "type": "object",
     "properties": {
         "username": shortname_schema,
-        "user_domain_name": shortname_schema,
+        "domain_name": shortname_schema,
         "password": passwd_schema,
         "projects": nameshort_list_schema,
         "project_role_mappings": project_role_mappings,
@@ -773,7 +773,7 @@ project_new_schema = {
     "properties": {
         "name": shortname_schema,
         "admin": bool_schema,
-        "project_domain_name": shortname_schema,
+        "domain_name": shortname_schema,
         "quotas": {
             "type": "object",
             "properties": {topic: integer0_schema for topic in topics_with_quota},