| Eduardo Sousa | 819d34c | 2018-07-31 01:20:02 +0100 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | |
| Eduardo Sousa | d795f87 | 2019-02-05 16:05:53 +0000 | [diff] [blame] | 3 | # Copyright 2018 Whitestack, LLC |
| 4 | # Copyright 2018 Telefonica S.A. |
| tierno | 0ea204e | 2019-01-25 14:16:24 +0000 | [diff] [blame] | 5 | # |
| Eduardo Sousa | d795f87 | 2019-02-05 16:05:53 +0000 | [diff] [blame] | 6 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 7 | # not use this file except in compliance with the License. You may obtain |
| 8 | # a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| tierno | 0ea204e | 2019-01-25 14:16:24 +0000 | [diff] [blame] | 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| Eduardo Sousa | d795f87 | 2019-02-05 16:05:53 +0000 | [diff] [blame] | 13 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 14 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 15 | # License for the specific language governing permissions and limitations |
| 16 | # under the License. |
| 17 | # |
| 18 | # For those usages not covered by the Apache License, Version 2.0 please |
| 19 | # contact: esousa@whitestack.com or alfonso.tiernosepulveda@telefonica.com |
| 20 | ## |
| tierno | 0ea204e | 2019-01-25 14:16:24 +0000 | [diff] [blame] | 21 | |
| 22 | |
| Eduardo Sousa | 819d34c | 2018-07-31 01:20:02 +0100 | [diff] [blame] | 23 | """ |
| 24 | Authenticator is responsible for authenticating the users, |
| 25 | create the tokens unscoped and scoped, retrieve the role |
| 26 | list inside the projects that they are inserted |
| 27 | """ |
| 28 | |
| tierno | 0ea204e | 2019-01-25 14:16:24 +0000 | [diff] [blame] | 29 | __author__ = "Eduardo Sousa <esousa@whitestack.com>; Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>" |
| Eduardo Sousa | 819d34c | 2018-07-31 01:20:02 +0100 | [diff] [blame] | 30 | __date__ = "$27-jul-2018 23:59:59$" |
| 31 | |
| Eduardo Sousa | d1b525d | 2018-10-04 04:24:18 +0100 | [diff] [blame] | 32 | import cherrypy |
| Eduardo Sousa | 819d34c | 2018-07-31 01:20:02 +0100 | [diff] [blame] | 33 | import logging |
| Eduardo Sousa | 29933fc | 2018-11-14 06:36:35 +0000 | [diff] [blame] | 34 | import yaml |
| Eduardo Sousa | 2f98821 | 2018-07-26 01:04:11 +0100 | [diff] [blame] | 35 | from base64 import standard_b64decode |
| Eduardo Sousa | 819d34c | 2018-07-31 01:20:02 +0100 | [diff] [blame] | 36 | from copy import deepcopy |
| tierno | 38dcfeb | 2019-06-10 16:44:00 +0000 | [diff] [blame] | 37 | # from functools import reduce |
| Eduardo Sousa | 2f98821 | 2018-07-26 01:04:11 +0100 | [diff] [blame] | 38 | from http import HTTPStatus |
| Eduardo Sousa | 819d34c | 2018-07-31 01:20:02 +0100 | [diff] [blame] | 39 | from time import time |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 40 | from os import path |
| Eduardo Sousa | 2f98821 | 2018-07-26 01:04:11 +0100 | [diff] [blame] | 41 | |
| tierno | c23a9bb | 2020-06-24 10:54:11 +0000 | [diff] [blame] | 42 | from osm_nbi.authconn import AuthException, AuthconnException, AuthExceptionUnauthorized |
| tierno | 9c63011 | 2019-08-29 14:21:41 +0000 | [diff] [blame] | 43 | from osm_nbi.authconn_keystone import AuthconnKeystone |
| delacruzramo | ad682a5 | 2019-12-10 16:26:34 +0100 | [diff] [blame] | 44 | from osm_nbi.authconn_internal import AuthconnInternal |
| K Sai Kiran | 7ddb073 | 2020-10-30 11:14:44 +0530 | [diff] [blame] | 45 | from osm_nbi.authconn_tacacs import AuthconnTacacs |
| delacruzramo | ad682a5 | 2019-12-10 16:26:34 +0100 | [diff] [blame] | 46 | from osm_common import dbmemory, dbmongo, msglocal, msgkafka |
| Eduardo Sousa | d1b525d | 2018-10-04 04:24:18 +0100 | [diff] [blame] | 47 | from osm_common.dbbase import DbException |
| delacruzramo | 029405d | 2019-09-26 10:52:56 +0200 | [diff] [blame] | 48 | from osm_nbi.validation import is_valid_uuid |
| tierno | 701018c | 2019-06-25 11:13:14 +0000 | [diff] [blame] | 49 | from itertools import chain |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 50 | from uuid import uuid4 |
| delacruzramo | ceb8baf | 2019-06-21 14:25:38 +0200 | [diff] [blame] | 51 | |
| Eduardo Sousa | 2f98821 | 2018-07-26 01:04:11 +0100 | [diff] [blame] | 52 | |
| Eduardo Sousa | 819d34c | 2018-07-31 01:20:02 +0100 | [diff] [blame] | 53 | class Authenticator: |
| 54 | """ |
| 55 | This class should hold all the mechanisms for User Authentication and |
| 56 | Authorization. Initially it should support Openstack Keystone as a |
| 57 | backend through a plugin model where more backends can be added and a |
| 58 | RBAC model to manage permissions on operations. |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 59 | This class must be threading safe |
| Eduardo Sousa | 819d34c | 2018-07-31 01:20:02 +0100 | [diff] [blame] | 60 | """ |
| Eduardo Sousa | 2f98821 | 2018-07-26 01:04:11 +0100 | [diff] [blame] | 61 | |
| Eduardo Sousa | 29933fc | 2018-11-14 06:36:35 +0000 | [diff] [blame] | 62 | periodin_db_pruning = 60 * 30 # for the internal backend only. every 30 minutes expired tokens will be pruned |
| delacruzramo | ad682a5 | 2019-12-10 16:26:34 +0100 | [diff] [blame] | 63 | token_limit = 500 # when reached, the token cache will be cleared |
| tierno | 0ea204e | 2019-01-25 14:16:24 +0000 | [diff] [blame] | 64 | |
| tierno | 701018c | 2019-06-25 11:13:14 +0000 | [diff] [blame] | 65 | def __init__(self, valid_methods, valid_query_string): |
| Eduardo Sousa | 819d34c | 2018-07-31 01:20:02 +0100 | [diff] [blame] | 66 | """ |
| 67 | Authenticator initializer. Setup the initial state of the object, |
| 68 | while it waits for the config dictionary and database initialization. |
| Eduardo Sousa | 819d34c | 2018-07-31 01:20:02 +0100 | [diff] [blame] | 69 | """ |
| Eduardo Sousa | 819d34c | 2018-07-31 01:20:02 +0100 | [diff] [blame] | 70 | self.backend = None |
| 71 | self.config = None |
| 72 | self.db = None |
| delacruzramo | ad682a5 | 2019-12-10 16:26:34 +0100 | [diff] [blame] | 73 | self.msg = None |
| tierno | 0ea204e | 2019-01-25 14:16:24 +0000 | [diff] [blame] | 74 | self.tokens_cache = dict() |
| 75 | self.next_db_prune_time = 0 # time when next cleaning of expired tokens must be done |
| Eduardo Sousa | 29933fc | 2018-11-14 06:36:35 +0000 | [diff] [blame] | 76 | self.roles_to_operations_file = None |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 77 | # self.roles_to_operations_table = None |
| Eduardo Sousa | 29933fc | 2018-11-14 06:36:35 +0000 | [diff] [blame] | 78 | self.resources_to_operations_mapping = {} |
| 79 | self.operation_to_allowed_roles = {} |
| Eduardo Sousa | 819d34c | 2018-07-31 01:20:02 +0100 | [diff] [blame] | 80 | self.logger = logging.getLogger("nbi.authenticator") |
| tierno | 701018c | 2019-06-25 11:13:14 +0000 | [diff] [blame] | 81 | self.role_permissions = [] |
| 82 | self.valid_methods = valid_methods |
| 83 | self.valid_query_string = valid_query_string |
| tierno | d4a705a | 2020-06-22 10:58:26 +0000 | [diff] [blame] | 84 | self.system_admin_role_id = None # system_role id |
| 85 | self.test_project_id = None # test_project_id |
| Eduardo Sousa | 819d34c | 2018-07-31 01:20:02 +0100 | [diff] [blame] | 86 | |
| 87 | def start(self, config): |
| 88 | """ |
| 89 | Method to configure the Authenticator object. This method should be called |
| 90 | after object creation. It is responsible by initializing the selected backend, |
| 91 | as well as the initialization of the database connection. |
| 92 | |
| 93 | :param config: dictionary containing the relevant parameters for this object. |
| 94 | """ |
| 95 | self.config = config |
| 96 | |
| 97 | try: |
| Eduardo Sousa | 819d34c | 2018-07-31 01:20:02 +0100 | [diff] [blame] | 98 | if not self.db: |
| Eduardo Sousa | d1b525d | 2018-10-04 04:24:18 +0100 | [diff] [blame] | 99 | if config["database"]["driver"] == "mongo": |
| 100 | self.db = dbmongo.DbMongo() |
| 101 | self.db.db_connect(config["database"]) |
| 102 | elif config["database"]["driver"] == "memory": |
| 103 | self.db = dbmemory.DbMemory() |
| 104 | self.db.db_connect(config["database"]) |
| 105 | else: |
| 106 | raise AuthException("Invalid configuration param '{}' at '[database]':'driver'" |
| 107 | .format(config["database"]["driver"])) |
| delacruzramo | ad682a5 | 2019-12-10 16:26:34 +0100 | [diff] [blame] | 108 | if not self.msg: |
| 109 | if config["message"]["driver"] == "local": |
| 110 | self.msg = msglocal.MsgLocal() |
| 111 | self.msg.connect(config["message"]) |
| 112 | elif config["message"]["driver"] == "kafka": |
| 113 | self.msg = msgkafka.MsgKafka() |
| 114 | self.msg.connect(config["message"]) |
| 115 | else: |
| 116 | raise AuthException("Invalid configuration param '{}' at '[message]':'driver'" |
| 117 | .format(config["message"]["driver"])) |
| tierno | 0ea204e | 2019-01-25 14:16:24 +0000 | [diff] [blame] | 118 | if not self.backend: |
| 119 | if config["authentication"]["backend"] == "keystone": |
| tierno | 9e87a7f | 2020-03-23 09:24:10 +0000 | [diff] [blame] | 120 | self.backend = AuthconnKeystone(self.config["authentication"], self.db, self.role_permissions) |
| tierno | 0ea204e | 2019-01-25 14:16:24 +0000 | [diff] [blame] | 121 | elif config["authentication"]["backend"] == "internal": |
| tierno | 9e87a7f | 2020-03-23 09:24:10 +0000 | [diff] [blame] | 122 | self.backend = AuthconnInternal(self.config["authentication"], self.db, self.role_permissions) |
| K Sai Kiran | 7ddb073 | 2020-10-30 11:14:44 +0530 | [diff] [blame] | 123 | self._internal_tokens_prune("tokens") |
| 124 | elif config["authentication"]["backend"] == "tacacs": |
| 125 | self.backend = AuthconnTacacs(self.config["authentication"], self.db, self.role_permissions) |
| 126 | self._internal_tokens_prune("tokens_tacacs") |
| tierno | 0ea204e | 2019-01-25 14:16:24 +0000 | [diff] [blame] | 127 | else: |
| 128 | raise AuthException("Unknown authentication backend: {}" |
| 129 | .format(config["authentication"]["backend"])) |
| tierno | 701018c | 2019-06-25 11:13:14 +0000 | [diff] [blame] | 130 | |
| Eduardo Sousa | 29933fc | 2018-11-14 06:36:35 +0000 | [diff] [blame] | 131 | if not self.roles_to_operations_file: |
| 132 | if "roles_to_operations" in config["rbac"]: |
| 133 | self.roles_to_operations_file = config["rbac"]["roles_to_operations"] |
| 134 | else: |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 135 | possible_paths = ( |
| 136 | __file__[:__file__.rfind("auth.py")] + "roles_to_operations.yml", |
| 137 | "./roles_to_operations.yml" |
| 138 | ) |
| 139 | for config_file in possible_paths: |
| Eduardo Sousa | 29933fc | 2018-11-14 06:36:35 +0000 | [diff] [blame] | 140 | if path.isfile(config_file): |
| 141 | self.roles_to_operations_file = config_file |
| 142 | break |
| tierno | 701018c | 2019-06-25 11:13:14 +0000 | [diff] [blame] | 143 | if not self.roles_to_operations_file: |
| 144 | raise AuthException("Invalid permission configuration: roles_to_operations file missing") |
| 145 | |
| tierno | 701018c | 2019-06-25 11:13:14 +0000 | [diff] [blame] | 146 | # load role_permissions |
| 147 | def load_role_permissions(method_dict): |
| 148 | for k in method_dict: |
| 149 | if k == "ROLE_PERMISSION": |
| 150 | for method in chain(method_dict.get("METHODS", ()), method_dict.get("TODO", ())): |
| 151 | permission = method_dict["ROLE_PERMISSION"] + method.lower() |
| 152 | if permission not in self.role_permissions: |
| 153 | self.role_permissions.append(permission) |
| 154 | elif k in ("TODO", "METHODS"): |
| 155 | continue |
| tierno | 74b5358 | 2020-06-18 10:52:37 +0000 | [diff] [blame] | 156 | elif method_dict[k]: |
| tierno | 701018c | 2019-06-25 11:13:14 +0000 | [diff] [blame] | 157 | load_role_permissions(method_dict[k]) |
| 158 | |
| 159 | load_role_permissions(self.valid_methods) |
| 160 | for query_string in self.valid_query_string: |
| 161 | for method in ("get", "put", "patch", "post", "delete"): |
| 162 | permission = query_string.lower() + ":" + method |
| 163 | if permission not in self.role_permissions: |
| 164 | self.role_permissions.append(permission) |
| 165 | |
| tierno | d4a705a | 2020-06-22 10:58:26 +0000 | [diff] [blame] | 166 | # get ids of role system_admin and test project |
| 167 | role_system_admin = self.db.get_one("roles", {"name": "system_admin"}, fail_on_empty=False) |
| 168 | if role_system_admin: |
| 169 | self.system_admin_role_id = role_system_admin["_id"] |
| 170 | test_project_name = self.config["authentication"].get("project_not_authorized", "admin") |
| 171 | test_project = self.db.get_one("projects", {"name": test_project_name}, fail_on_empty=False) |
| 172 | if test_project: |
| 173 | self.test_project_id = test_project["_id"] |
| 174 | |
| Eduardo Sousa | 819d34c | 2018-07-31 01:20:02 +0100 | [diff] [blame] | 175 | except Exception as e: |
| 176 | raise AuthException(str(e)) |
| 177 | |
| Eduardo Sousa | d1b525d | 2018-10-04 04:24:18 +0100 | [diff] [blame] | 178 | def stop(self): |
| 179 | try: |
| 180 | if self.db: |
| 181 | self.db.db_disconnect() |
| 182 | except DbException as e: |
| 183 | raise AuthException(str(e), http_code=e.http_code) |
| 184 | |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 185 | def create_admin_project(self): |
| 186 | """ |
| 187 | Creates a new project 'admin' into database if it doesn't exist. Useful for initialization. |
| 188 | :return: _id identity of the 'admin' project |
| 189 | """ |
| 190 | |
| 191 | # projects = self.db.get_one("projects", fail_on_empty=False, fail_on_more=False) |
| 192 | project_desc = {"name": "admin"} |
| 193 | projects = self.backend.get_project_list(project_desc) |
| 194 | if projects: |
| 195 | return projects[0]["_id"] |
| 196 | now = time() |
| 197 | project_desc["_id"] = str(uuid4()) |
| 198 | project_desc["_admin"] = {"created": now, "modified": now} |
| 199 | pid = self.backend.create_project(project_desc) |
| 200 | self.logger.info("Project '{}' created at database".format(project_desc["name"])) |
| 201 | return pid |
| 202 | |
| 203 | def create_admin_user(self, project_id): |
| 204 | """ |
| 205 | Creates a new user admin/admin into database if database is empty. Useful for initialization |
| 206 | :return: _id identity of the inserted data, or None |
| 207 | """ |
| 208 | # users = self.db.get_one("users", fail_on_empty=False, fail_on_more=False) |
| 209 | users = self.backend.get_user_list() |
| 210 | if users: |
| 211 | return None |
| 212 | # user_desc = {"username": "admin", "password": "admin", "projects": [project_id]} |
| 213 | now = time() |
| 214 | user_desc = {"username": "admin", "password": "admin", "_admin": {"created": now, "modified": now}} |
| 215 | if project_id: |
| 216 | pid = project_id |
| 217 | else: |
| 218 | # proj = self.db.get_one("projects", {"name": "admin"}, fail_on_empty=False, fail_on_more=False) |
| 219 | proj = self.backend.get_project_list({"name": "admin"}) |
| 220 | pid = proj[0]["_id"] if proj else None |
| 221 | # role = self.db.get_one("roles", {"name": "system_admin"}, fail_on_empty=False, fail_on_more=False) |
| 222 | roles = self.backend.get_role_list({"name": "system_admin"}) |
| 223 | if pid and roles: |
| 224 | user_desc["project_role_mappings"] = [{"project": pid, "role": roles[0]["_id"]}] |
| 225 | uid = self.backend.create_user(user_desc) |
| 226 | self.logger.info("User '{}' created at database".format(user_desc["username"])) |
| 227 | return uid |
| 228 | |
| Eduardo Sousa | 29933fc | 2018-11-14 06:36:35 +0000 | [diff] [blame] | 229 | def init_db(self, target_version='1.0'): |
| Eduardo Sousa | 819d34c | 2018-07-31 01:20:02 +0100 | [diff] [blame] | 230 | """ |
| Eduardo Sousa | 29933fc | 2018-11-14 06:36:35 +0000 | [diff] [blame] | 231 | Check if the database has been initialized, with at least one user. If not, create the required tables |
| Eduardo Sousa | 819d34c | 2018-07-31 01:20:02 +0100 | [diff] [blame] | 232 | and insert the predefined mappings between roles and permissions. |
| 233 | |
| 234 | :param target_version: schema version that should be present in the database. |
| 235 | :return: None if OK, exception if error or version is different. |
| 236 | """ |
| delacruzramo | ceb8baf | 2019-06-21 14:25:38 +0200 | [diff] [blame] | 237 | |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 238 | records = self.backend.get_role_list() |
| Eduardo Sousa | 29933fc | 2018-11-14 06:36:35 +0000 | [diff] [blame] | 239 | |
| tierno | c23a9bb | 2020-06-24 10:54:11 +0000 | [diff] [blame] | 240 | # Loading permissions to AUTH. At lease system_admin must be present. |
| 241 | if not records or not next((r for r in records if r["name"] == "system_admin"), None): |
| Eduardo Sousa | 29933fc | 2018-11-14 06:36:35 +0000 | [diff] [blame] | 242 | with open(self.roles_to_operations_file, "r") as stream: |
| delacruzramo | b19cadc | 2019-10-08 10:18:02 +0200 | [diff] [blame] | 243 | roles_to_operations_yaml = yaml.load(stream, Loader=yaml.Loader) |
| Eduardo Sousa | 29933fc | 2018-11-14 06:36:35 +0000 | [diff] [blame] | 244 | |
| tierno | 1f029d8 | 2019-06-13 22:37:04 +0000 | [diff] [blame] | 245 | role_names = [] |
| 246 | for role_with_operations in roles_to_operations_yaml["roles"]: |
| 247 | # Verifying if role already exists. If it does, raise exception |
| 248 | if role_with_operations["name"] not in role_names: |
| 249 | role_names.append(role_with_operations["name"]) |
| Eduardo Sousa | 29933fc | 2018-11-14 06:36:35 +0000 | [diff] [blame] | 250 | else: |
| tierno | 1f029d8 | 2019-06-13 22:37:04 +0000 | [diff] [blame] | 251 | raise AuthException("Duplicated role name '{}' at file '{}''" |
| 252 | .format(role_with_operations["name"], self.roles_to_operations_file)) |
| 253 | |
| 254 | if not role_with_operations["permissions"]: |
| Eduardo Sousa | 29933fc | 2018-11-14 06:36:35 +0000 | [diff] [blame] | 255 | continue |
| 256 | |
| tierno | 1f029d8 | 2019-06-13 22:37:04 +0000 | [diff] [blame] | 257 | for permission, is_allowed in role_with_operations["permissions"].items(): |
| Eduardo Sousa | 29933fc | 2018-11-14 06:36:35 +0000 | [diff] [blame] | 258 | if not isinstance(is_allowed, bool): |
| tierno | 1f029d8 | 2019-06-13 22:37:04 +0000 | [diff] [blame] | 259 | raise AuthException("Invalid value for permission '{}' at role '{}'; at file '{}'" |
| 260 | .format(permission, role_with_operations["name"], |
| 261 | self.roles_to_operations_file)) |
| Eduardo Sousa | 29933fc | 2018-11-14 06:36:35 +0000 | [diff] [blame] | 262 | |
| tierno | c23a9bb | 2020-06-24 10:54:11 +0000 | [diff] [blame] | 263 | # TODO check permission is ok |
| tierno | 1f029d8 | 2019-06-13 22:37:04 +0000 | [diff] [blame] | 264 | if permission[-1] == ":": |
| 265 | raise AuthException("Invalid permission '{}' terminated in ':' for role '{}'; at file {}" |
| 266 | .format(permission, role_with_operations["name"], |
| 267 | self.roles_to_operations_file)) |
| Eduardo Sousa | 29933fc | 2018-11-14 06:36:35 +0000 | [diff] [blame] | 268 | |
| tierno | 1f029d8 | 2019-06-13 22:37:04 +0000 | [diff] [blame] | 269 | if "default" not in role_with_operations["permissions"]: |
| 270 | role_with_operations["permissions"]["default"] = False |
| 271 | if "admin" not in role_with_operations["permissions"]: |
| 272 | role_with_operations["permissions"]["admin"] = False |
| Eduardo Sousa | 29933fc | 2018-11-14 06:36:35 +0000 | [diff] [blame] | 273 | |
| 274 | now = time() |
| tierno | 1f029d8 | 2019-06-13 22:37:04 +0000 | [diff] [blame] | 275 | role_with_operations["_admin"] = { |
| 276 | "created": now, |
| 277 | "modified": now, |
| Eduardo Sousa | 29933fc | 2018-11-14 06:36:35 +0000 | [diff] [blame] | 278 | } |
| 279 | |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 280 | # self.db.create(self.roles_to_operations_table, role_with_operations) |
| tierno | c23a9bb | 2020-06-24 10:54:11 +0000 | [diff] [blame] | 281 | try: |
| 282 | self.backend.create_role(role_with_operations) |
| 283 | self.logger.info("Role '{}' created".format(role_with_operations["name"])) |
| 284 | except (AuthException, AuthconnException) as e: |
| 285 | if role_with_operations["name"] == "system_admin": |
| 286 | raise |
| 287 | self.logger.error("Role '{}' cannot be created: {}".format(role_with_operations["name"], e)) |
| Eduardo Sousa | 29933fc | 2018-11-14 06:36:35 +0000 | [diff] [blame] | 288 | |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 289 | # Create admin project&user if required |
| 290 | pid = self.create_admin_project() |
| tierno | 9ebbf85 | 2019-09-03 14:58:09 +0000 | [diff] [blame] | 291 | user_id = self.create_admin_user(pid) |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 292 | |
| tierno | 9ebbf85 | 2019-09-03 14:58:09 +0000 | [diff] [blame] | 293 | # try to assign system_admin role to user admin if not any user has this role |
| 294 | if not user_id: |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 295 | try: |
| tierno | 9ebbf85 | 2019-09-03 14:58:09 +0000 | [diff] [blame] | 296 | users = self.backend.get_user_list() |
| 297 | roles = self.backend.get_role_list({"name": "system_admin"}) |
| 298 | role_id = roles[0]["_id"] |
| 299 | user_with_system_admin = False |
| 300 | user_admin_id = None |
| 301 | for user in users: |
| 302 | if not user_admin_id: |
| 303 | user_admin_id = user["_id"] |
| 304 | if user["username"] == "admin": |
| 305 | user_admin_id = user["_id"] |
| 306 | for prm in user.get("project_role_mappings", ()): |
| 307 | if prm["role"] == role_id: |
| 308 | user_with_system_admin = True |
| 309 | break |
| 310 | if user_with_system_admin: |
| 311 | break |
| 312 | if not user_with_system_admin: |
| 313 | self.backend.update_user({"_id": user_admin_id, |
| 314 | "add_project_role_mappings": [{"project": pid, "role": role_id}]}) |
| 315 | self.logger.info("Added role system admin to user='{}' project=admin".format(user_admin_id)) |
| delacruzramo | 15ec706 | 2019-12-26 10:09:04 +0000 | [diff] [blame] | 316 | except Exception as e: |
| 317 | self.logger.error("Error in Authorization DataBase initialization: {}: {}".format(type(e).__name__, e)) |
| tierno | 1f029d8 | 2019-06-13 22:37:04 +0000 | [diff] [blame] | 318 | |
| 319 | self.load_operation_to_allowed_roles() |
| 320 | |
| 321 | def load_operation_to_allowed_roles(self): |
| 322 | """ |
| tierno | 701018c | 2019-06-25 11:13:14 +0000 | [diff] [blame] | 323 | Fills the internal self.operation_to_allowed_roles based on database role content and self.role_permissions |
| tierno | a6bb45d | 2019-06-14 09:45:39 +0000 | [diff] [blame] | 324 | It works in a shadow copy and replace at the end to allow other threads working with the old copy |
| tierno | 1f029d8 | 2019-06-13 22:37:04 +0000 | [diff] [blame] | 325 | :return: None |
| 326 | """ |
| 327 | |
| tierno | 701018c | 2019-06-25 11:13:14 +0000 | [diff] [blame] | 328 | permissions = {oper: [] for oper in self.role_permissions} |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 329 | # records = self.db.get_list(self.roles_to_operations_table) |
| 330 | records = self.backend.get_role_list() |
| Eduardo Sousa | 29933fc | 2018-11-14 06:36:35 +0000 | [diff] [blame] | 331 | |
| tierno | a6bb45d | 2019-06-14 09:45:39 +0000 | [diff] [blame] | 332 | ignore_fields = ["_id", "_admin", "name", "default"] |
| Eduardo Sousa | 29933fc | 2018-11-14 06:36:35 +0000 | [diff] [blame] | 333 | for record in records: |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 334 | if not record.get("permissions"): |
| 335 | continue |
| tierno | 701018c | 2019-06-25 11:13:14 +0000 | [diff] [blame] | 336 | record_permissions = {oper: record["permissions"].get("default", False) for oper in self.role_permissions} |
| tierno | 1f029d8 | 2019-06-13 22:37:04 +0000 | [diff] [blame] | 337 | operations_joined = [(oper, value) for oper, value in record["permissions"].items() |
| 338 | if oper not in ignore_fields] |
| Eduardo Sousa | 29933fc | 2018-11-14 06:36:35 +0000 | [diff] [blame] | 339 | operations_joined.sort(key=lambda x: x[0].count(":")) |
| 340 | |
| 341 | for oper in operations_joined: |
| 342 | match = list(filter(lambda x: x.find(oper[0]) == 0, record_permissions.keys())) |
| 343 | |
| 344 | for m in match: |
| 345 | record_permissions[m] = oper[1] |
| 346 | |
| 347 | allowed_operations = [k for k, v in record_permissions.items() if v is True] |
| 348 | |
| 349 | for allowed_op in allowed_operations: |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 350 | permissions[allowed_op].append(record["name"]) |
| Eduardo Sousa | 29933fc | 2018-11-14 06:36:35 +0000 | [diff] [blame] | 351 | |
| tierno | a6bb45d | 2019-06-14 09:45:39 +0000 | [diff] [blame] | 352 | self.operation_to_allowed_roles = permissions |
| Eduardo Sousa | 29933fc | 2018-11-14 06:36:35 +0000 | [diff] [blame] | 353 | |
| delacruzramo | 029405d | 2019-09-26 10:52:56 +0200 | [diff] [blame] | 354 | def authorize(self, role_permission=None, query_string_operations=None, item_id=None): |
| Eduardo Sousa | 2f98821 | 2018-07-26 01:04:11 +0100 | [diff] [blame] | 355 | token = None |
| 356 | user_passwd64 = None |
| 357 | try: |
| 358 | # 1. Get token Authorization bearer |
| 359 | auth = cherrypy.request.headers.get("Authorization") |
| 360 | if auth: |
| 361 | auth_list = auth.split(" ") |
| 362 | if auth_list[0].lower() == "bearer": |
| 363 | token = auth_list[-1] |
| 364 | elif auth_list[0].lower() == "basic": |
| 365 | user_passwd64 = auth_list[-1] |
| 366 | if not token: |
| 367 | if cherrypy.session.get("Authorization"): |
| 368 | # 2. Try using session before request a new token. If not, basic authentication will generate |
| 369 | token = cherrypy.session.get("Authorization") |
| 370 | if token == "logout": |
| tierno | 0ea204e | 2019-01-25 14:16:24 +0000 | [diff] [blame] | 371 | token = None # force Unauthorized response to insert user password again |
| Eduardo Sousa | 2f98821 | 2018-07-26 01:04:11 +0100 | [diff] [blame] | 372 | elif user_passwd64 and cherrypy.request.config.get("auth.allow_basic_authentication"): |
| 373 | # 3. Get new token from user password |
| 374 | user = None |
| 375 | passwd = None |
| 376 | try: |
| 377 | user_passwd = standard_b64decode(user_passwd64).decode() |
| 378 | user, _, passwd = user_passwd.partition(":") |
| 379 | except Exception: |
| 380 | pass |
| Eduardo Sousa | 819d34c | 2018-07-31 01:20:02 +0100 | [diff] [blame] | 381 | outdata = self.new_token(None, {"username": user, "password": passwd}) |
| tierno | 701018c | 2019-06-25 11:13:14 +0000 | [diff] [blame] | 382 | token = outdata["_id"] |
| Eduardo Sousa | 2f98821 | 2018-07-26 01:04:11 +0100 | [diff] [blame] | 383 | cherrypy.session['Authorization'] = token |
| tierno | a6bb45d | 2019-06-14 09:45:39 +0000 | [diff] [blame] | 384 | |
| delacruzramo | ceb8baf | 2019-06-21 14:25:38 +0200 | [diff] [blame] | 385 | if not token: |
| 386 | raise AuthException("Needed a token or Authorization http header", |
| 387 | http_code=HTTPStatus.UNAUTHORIZED) |
| delacruzramo | ad682a5 | 2019-12-10 16:26:34 +0100 | [diff] [blame] | 388 | |
| 389 | # try to get from cache first |
| 390 | now = time() |
| 391 | token_info = self.tokens_cache.get(token) |
| 392 | if token_info and token_info["expires"] < now: |
| 393 | # delete token. MUST be done with care, as another thread maybe already delete it. Do not use del |
| 394 | self.tokens_cache.pop(token, None) |
| 395 | token_info = None |
| 396 | |
| 397 | # get from database if not in cache |
| 398 | if not token_info: |
| 399 | token_info = self.backend.validate_token(token) |
| 400 | # Clear cache if token limit reached |
| 401 | if len(self.tokens_cache) > self.token_limit: |
| 402 | self.tokens_cache.clear() |
| 403 | self.tokens_cache[token] = token_info |
| delacruzramo | ceb8baf | 2019-06-21 14:25:38 +0200 | [diff] [blame] | 404 | # TODO add to token info remote host, port |
| 405 | |
| tierno | 701018c | 2019-06-25 11:13:14 +0000 | [diff] [blame] | 406 | if role_permission: |
| delacruzramo | 029405d | 2019-09-26 10:52:56 +0200 | [diff] [blame] | 407 | RBAC_auth = self.check_permissions(token_info, cherrypy.request.method, role_permission, |
| 408 | query_string_operations, item_id) |
| 409 | token_info["allow_show_user_project_role"] = RBAC_auth |
| 410 | |
| delacruzramo | ceb8baf | 2019-06-21 14:25:38 +0200 | [diff] [blame] | 411 | return token_info |
| Eduardo Sousa | d1b525d | 2018-10-04 04:24:18 +0100 | [diff] [blame] | 412 | except AuthException as e: |
| tierno | c844536 | 2019-06-14 12:07:15 +0000 | [diff] [blame] | 413 | if not isinstance(e, AuthExceptionUnauthorized): |
| 414 | if cherrypy.session.get('Authorization'): |
| 415 | del cherrypy.session['Authorization'] |
| 416 | cherrypy.response.headers["WWW-Authenticate"] = 'Bearer realm="{}"'.format(e) |
| tierno | d4a705a | 2020-06-22 10:58:26 +0000 | [diff] [blame] | 417 | if self.config["authentication"].get("user_not_authorized"): |
| 418 | return {"id": "testing-token", "_id": "testing-token", |
| 419 | "project_id": self.test_project_id, |
| 420 | "username": self.config["authentication"]["user_not_authorized"], |
| 421 | "roles": [self.system_admin_role_id], |
| 422 | "admin": True, "allow_show_user_project_role": True} |
| tierno | c844536 | 2019-06-14 12:07:15 +0000 | [diff] [blame] | 423 | raise |
| Eduardo Sousa | 2f98821 | 2018-07-26 01:04:11 +0100 | [diff] [blame] | 424 | |
| tierno | 701018c | 2019-06-25 11:13:14 +0000 | [diff] [blame] | 425 | def new_token(self, token_info, indata, remote): |
| 426 | new_token_info = self.backend.authenticate( |
| tierno | 6486f74 | 2020-02-13 16:30:14 +0000 | [diff] [blame] | 427 | credentials=indata, |
| tierno | 701018c | 2019-06-25 11:13:14 +0000 | [diff] [blame] | 428 | token_info=token_info, |
| delacruzramo | ceb8baf | 2019-06-21 14:25:38 +0200 | [diff] [blame] | 429 | ) |
| Eduardo Sousa | 819d34c | 2018-07-31 01:20:02 +0100 | [diff] [blame] | 430 | |
| tierno | 701018c | 2019-06-25 11:13:14 +0000 | [diff] [blame] | 431 | new_token_info["remote_port"] = remote.port |
| 432 | if not new_token_info.get("expires"): |
| 433 | new_token_info["expires"] = time() + 3600 |
| 434 | if not new_token_info.get("admin"): |
| 435 | new_token_info["admin"] = True if new_token_info.get("project_name") == "admin" else False |
| 436 | # TODO put admin in RBAC |
| Eduardo Sousa | 819d34c | 2018-07-31 01:20:02 +0100 | [diff] [blame] | 437 | |
| delacruzramo | ceb8baf | 2019-06-21 14:25:38 +0200 | [diff] [blame] | 438 | if remote.name: |
| tierno | 701018c | 2019-06-25 11:13:14 +0000 | [diff] [blame] | 439 | new_token_info["remote_host"] = remote.name |
| delacruzramo | ceb8baf | 2019-06-21 14:25:38 +0200 | [diff] [blame] | 440 | elif remote.ip: |
| tierno | 701018c | 2019-06-25 11:13:14 +0000 | [diff] [blame] | 441 | new_token_info["remote_host"] = remote.ip |
| Eduardo Sousa | 819d34c | 2018-07-31 01:20:02 +0100 | [diff] [blame] | 442 | |
| tierno | 701018c | 2019-06-25 11:13:14 +0000 | [diff] [blame] | 443 | # TODO call self._internal_tokens_prune(now) ? |
| 444 | return deepcopy(new_token_info) |
| Eduardo Sousa | 2f98821 | 2018-07-26 01:04:11 +0100 | [diff] [blame] | 445 | |
| tierno | 701018c | 2019-06-25 11:13:14 +0000 | [diff] [blame] | 446 | def get_token_list(self, token_info): |
| Eduardo Sousa | 819d34c | 2018-07-31 01:20:02 +0100 | [diff] [blame] | 447 | if self.config["authentication"]["backend"] == "internal": |
| tierno | 701018c | 2019-06-25 11:13:14 +0000 | [diff] [blame] | 448 | return self._internal_get_token_list(token_info) |
| Eduardo Sousa | 819d34c | 2018-07-31 01:20:02 +0100 | [diff] [blame] | 449 | else: |
| tierno | 0ea204e | 2019-01-25 14:16:24 +0000 | [diff] [blame] | 450 | # TODO: check if this can be avoided. Backend may provide enough information |
| 451 | return [deepcopy(token) for token in self.tokens_cache.values() |
| tierno | 701018c | 2019-06-25 11:13:14 +0000 | [diff] [blame] | 452 | if token["username"] == token_info["username"]] |
| Eduardo Sousa | 2f98821 | 2018-07-26 01:04:11 +0100 | [diff] [blame] | 453 | |
| tierno | 701018c | 2019-06-25 11:13:14 +0000 | [diff] [blame] | 454 | def get_token(self, token_info, token): |
| Eduardo Sousa | 819d34c | 2018-07-31 01:20:02 +0100 | [diff] [blame] | 455 | if self.config["authentication"]["backend"] == "internal": |
| tierno | 701018c | 2019-06-25 11:13:14 +0000 | [diff] [blame] | 456 | return self._internal_get_token(token_info, token) |
| Eduardo Sousa | 819d34c | 2018-07-31 01:20:02 +0100 | [diff] [blame] | 457 | else: |
| tierno | 0ea204e | 2019-01-25 14:16:24 +0000 | [diff] [blame] | 458 | # TODO: check if this can be avoided. Backend may provide enough information |
| 459 | token_value = self.tokens_cache.get(token) |
| Eduardo Sousa | 819d34c | 2018-07-31 01:20:02 +0100 | [diff] [blame] | 460 | if not token_value: |
| Eduardo Sousa | d1b525d | 2018-10-04 04:24:18 +0100 | [diff] [blame] | 461 | raise AuthException("token not found", http_code=HTTPStatus.NOT_FOUND) |
| tierno | 701018c | 2019-06-25 11:13:14 +0000 | [diff] [blame] | 462 | if token_value["username"] != token_info["username"] and not token_info["admin"]: |
| Eduardo Sousa | d1b525d | 2018-10-04 04:24:18 +0100 | [diff] [blame] | 463 | raise AuthException("needed admin privileges", http_code=HTTPStatus.UNAUTHORIZED) |
| Eduardo Sousa | 819d34c | 2018-07-31 01:20:02 +0100 | [diff] [blame] | 464 | return token_value |
| Eduardo Sousa | 2f98821 | 2018-07-26 01:04:11 +0100 | [diff] [blame] | 465 | |
| Eduardo Sousa | 819d34c | 2018-07-31 01:20:02 +0100 | [diff] [blame] | 466 | def del_token(self, token): |
| delacruzramo | ceb8baf | 2019-06-21 14:25:38 +0200 | [diff] [blame] | 467 | try: |
| 468 | self.backend.revoke_token(token) |
| delacruzramo | ad682a5 | 2019-12-10 16:26:34 +0100 | [diff] [blame] | 469 | # self.tokens_cache.pop(token, None) |
| 470 | self.remove_token_from_cache(token) |
| delacruzramo | ceb8baf | 2019-06-21 14:25:38 +0200 | [diff] [blame] | 471 | return "token '{}' deleted".format(token) |
| 472 | except KeyError: |
| 473 | raise AuthException("Token '{}' not found".format(token), http_code=HTTPStatus.NOT_FOUND) |
| Eduardo Sousa | d1b525d | 2018-10-04 04:24:18 +0100 | [diff] [blame] | 474 | |
| delacruzramo | 029405d | 2019-09-26 10:52:56 +0200 | [diff] [blame] | 475 | def check_permissions(self, token_info, method, role_permission=None, query_string_operations=None, item_id=None): |
| tierno | 701018c | 2019-06-25 11:13:14 +0000 | [diff] [blame] | 476 | """ |
| 477 | Checks that operation has permissions to be done, base on the assigned roles to this user project |
| 478 | :param token_info: Dictionary that contains "roles" with a list of assigned roles. |
| 479 | This method fills the token_info["admin"] with True or False based on assigned tokens, if any allows admin |
| 480 | This will be used among others to hide or not the _admin content of topics |
| 481 | :param method: GET,PUT, POST, ... |
| 482 | :param role_permission: role permission name of the operation required |
| 483 | :param query_string_operations: list of possible admin query strings provided by user. It is checked that the |
| 484 | assigned role allows this query string for this method |
| delacruzramo | 029405d | 2019-09-26 10:52:56 +0200 | [diff] [blame] | 485 | :param item_id: item identifier if included in the URL, None otherwise |
| 486 | :return: True if access granted by permission rules, False if access granted by default rules (Bug 853) |
| 487 | :raises: AuthExceptionUnauthorized if access denied |
| tierno | 701018c | 2019-06-25 11:13:14 +0000 | [diff] [blame] | 488 | """ |
| Eduardo Sousa | 29933fc | 2018-11-14 06:36:35 +0000 | [diff] [blame] | 489 | |
| tierno | 701018c | 2019-06-25 11:13:14 +0000 | [diff] [blame] | 490 | roles_required = self.operation_to_allowed_roles[role_permission] |
| 491 | roles_allowed = [role["name"] for role in token_info["roles"]] |
| Eduardo Sousa | 29933fc | 2018-11-14 06:36:35 +0000 | [diff] [blame] | 492 | |
| tierno | 701018c | 2019-06-25 11:13:14 +0000 | [diff] [blame] | 493 | # fills token_info["admin"] if some roles allows it |
| 494 | token_info["admin"] = False |
| tierno | a6bb45d | 2019-06-14 09:45:39 +0000 | [diff] [blame] | 495 | for role in roles_allowed: |
| tierno | 701018c | 2019-06-25 11:13:14 +0000 | [diff] [blame] | 496 | if role in self.operation_to_allowed_roles["admin:" + method.lower()]: |
| 497 | token_info["admin"] = True |
| tierno | a6bb45d | 2019-06-14 09:45:39 +0000 | [diff] [blame] | 498 | break |
| Eduardo Sousa | 29933fc | 2018-11-14 06:36:35 +0000 | [diff] [blame] | 499 | |
| 500 | if "anonymous" in roles_required: |
| delacruzramo | 029405d | 2019-09-26 10:52:56 +0200 | [diff] [blame] | 501 | return True |
| tierno | 701018c | 2019-06-25 11:13:14 +0000 | [diff] [blame] | 502 | operation_allowed = False |
| Eduardo Sousa | 29933fc | 2018-11-14 06:36:35 +0000 | [diff] [blame] | 503 | for role in roles_allowed: |
| 504 | if role in roles_required: |
| tierno | 701018c | 2019-06-25 11:13:14 +0000 | [diff] [blame] | 505 | operation_allowed = True |
| 506 | # if query_string operations, check if this role allows it |
| 507 | if not query_string_operations: |
| delacruzramo | 029405d | 2019-09-26 10:52:56 +0200 | [diff] [blame] | 508 | return True |
| tierno | 701018c | 2019-06-25 11:13:14 +0000 | [diff] [blame] | 509 | for query_string_operation in query_string_operations: |
| 510 | if role not in self.operation_to_allowed_roles[query_string_operation]: |
| 511 | break |
| 512 | else: |
| delacruzramo | 029405d | 2019-09-26 10:52:56 +0200 | [diff] [blame] | 513 | return True |
| 514 | |
| 515 | # Bug 853 - Final Solution |
| 516 | # User/Project/Role whole listings are filtered elsewhere |
| 517 | # uid, pid, rid = ("user_id", "project_id", "id") if is_valid_uuid(id) else ("username", "project_name", "name") |
| 518 | uid = "user_id" if is_valid_uuid(item_id) else "username" |
| 519 | if (role_permission in ["projects:get", "projects:id:get", "roles:get", "roles:id:get", "users:get"]) \ |
| 520 | or (role_permission == "users:id:get" and item_id == token_info[uid]): |
| 521 | # or (role_permission == "projects:id:get" and item_id == token_info[pid]) \ |
| 522 | # or (role_permission == "roles:id:get" and item_id in [role[rid] for role in token_info["roles"]]): |
| 523 | return False |
| Eduardo Sousa | 29933fc | 2018-11-14 06:36:35 +0000 | [diff] [blame] | 524 | |
| tierno | 701018c | 2019-06-25 11:13:14 +0000 | [diff] [blame] | 525 | if not operation_allowed: |
| 526 | raise AuthExceptionUnauthorized("Access denied: lack of permissions.") |
| 527 | else: |
| 528 | raise AuthExceptionUnauthorized("Access denied: You have not permissions to use these admin query string") |
| Eduardo Sousa | 29933fc | 2018-11-14 06:36:35 +0000 | [diff] [blame] | 529 | |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 530 | def get_user_list(self): |
| 531 | return self.backend.get_user_list() |
| 532 | |
| Eduardo Sousa | 29933fc | 2018-11-14 06:36:35 +0000 | [diff] [blame] | 533 | def _normalize_url(self, url, method): |
| tierno | 701018c | 2019-06-25 11:13:14 +0000 | [diff] [blame] | 534 | # DEPRECATED !!! |
| Eduardo Sousa | 29933fc | 2018-11-14 06:36:35 +0000 | [diff] [blame] | 535 | # Removing query strings |
| 536 | normalized_url = url if '?' not in url else url[:url.find("?")] |
| 537 | normalized_url_splitted = normalized_url.split("/") |
| 538 | parameters = {} |
| 539 | |
| 540 | filtered_keys = [key for key in self.resources_to_operations_mapping.keys() |
| 541 | if method in key.split()[0]] |
| 542 | |
| 543 | for idx, path_part in enumerate(normalized_url_splitted): |
| 544 | tmp_keys = [] |
| 545 | for tmp_key in filtered_keys: |
| 546 | splitted = tmp_key.split()[1].split("/") |
| Eduardo Sousa | cc02e9a | 2019-03-20 17:32:36 +0000 | [diff] [blame] | 547 | if idx >= len(splitted): |
| 548 | continue |
| 549 | elif "<" in splitted[idx] and ">" in splitted[idx]: |
| Eduardo Sousa | 29933fc | 2018-11-14 06:36:35 +0000 | [diff] [blame] | 550 | if splitted[idx] == "<artifactPath>": |
| 551 | tmp_keys.append(tmp_key) |
| 552 | continue |
| 553 | elif idx == len(normalized_url_splitted) - 1 and \ |
| 554 | len(normalized_url_splitted) != len(splitted): |
| 555 | continue |
| 556 | else: |
| 557 | tmp_keys.append(tmp_key) |
| 558 | elif splitted[idx] == path_part: |
| 559 | if idx == len(normalized_url_splitted) - 1 and \ |
| 560 | len(normalized_url_splitted) != len(splitted): |
| 561 | continue |
| 562 | else: |
| 563 | tmp_keys.append(tmp_key) |
| 564 | filtered_keys = tmp_keys |
| 565 | if len(filtered_keys) == 1 and \ |
| 566 | filtered_keys[0].split("/")[-1] == "<artifactPath>": |
| 567 | break |
| 568 | |
| 569 | if len(filtered_keys) == 0: |
| 570 | raise AuthException("Cannot make an authorization decision. URL not found. URL: {0}".format(url)) |
| 571 | elif len(filtered_keys) > 1: |
| 572 | raise AuthException("Cannot make an authorization decision. Multiple URLs found. URL: {0}".format(url)) |
| 573 | |
| 574 | filtered_key = filtered_keys[0] |
| 575 | |
| 576 | for idx, path_part in enumerate(filtered_key.split()[1].split("/")): |
| 577 | if "<" in path_part and ">" in path_part: |
| 578 | if path_part == "<artifactPath>": |
| 579 | parameters[path_part[1:-1]] = "/".join(normalized_url_splitted[idx:]) |
| 580 | else: |
| 581 | parameters[path_part[1:-1]] = normalized_url_splitted[idx] |
| 582 | |
| 583 | return filtered_key, parameters |
| 584 | |
| tierno | 701018c | 2019-06-25 11:13:14 +0000 | [diff] [blame] | 585 | def _internal_get_token_list(self, token_info): |
| tierno | 0ea204e | 2019-01-25 14:16:24 +0000 | [diff] [blame] | 586 | now = time() |
| tierno | 701018c | 2019-06-25 11:13:14 +0000 | [diff] [blame] | 587 | token_list = self.db.get_list("tokens", {"username": token_info["username"], "expires.gt": now}) |
| Eduardo Sousa | d1b525d | 2018-10-04 04:24:18 +0100 | [diff] [blame] | 588 | return token_list |
| 589 | |
| tierno | 701018c | 2019-06-25 11:13:14 +0000 | [diff] [blame] | 590 | def _internal_get_token(self, token_info, token_id): |
| tierno | 0ea204e | 2019-01-25 14:16:24 +0000 | [diff] [blame] | 591 | token_value = self.db.get_one("tokens", {"_id": token_id}, fail_on_empty=False) |
| Eduardo Sousa | d1b525d | 2018-10-04 04:24:18 +0100 | [diff] [blame] | 592 | if not token_value: |
| 593 | raise AuthException("token not found", http_code=HTTPStatus.NOT_FOUND) |
| tierno | 701018c | 2019-06-25 11:13:14 +0000 | [diff] [blame] | 594 | if token_value["username"] != token_info["username"] and not token_info["admin"]: |
| Eduardo Sousa | d1b525d | 2018-10-04 04:24:18 +0100 | [diff] [blame] | 595 | raise AuthException("needed admin privileges", http_code=HTTPStatus.UNAUTHORIZED) |
| 596 | return token_value |
| 597 | |
| K Sai Kiran | 7ddb073 | 2020-10-30 11:14:44 +0530 | [diff] [blame] | 598 | def _internal_tokens_prune(self, token_collection, now=None): |
| tierno | 0ea204e | 2019-01-25 14:16:24 +0000 | [diff] [blame] | 599 | now = now or time() |
| 600 | if not self.next_db_prune_time or self.next_db_prune_time >= now: |
| K Sai Kiran | 7ddb073 | 2020-10-30 11:14:44 +0530 | [diff] [blame] | 601 | self.db.del_list(token_collection, {"expires.lt": now}) |
| tierno | 0ea204e | 2019-01-25 14:16:24 +0000 | [diff] [blame] | 602 | self.next_db_prune_time = self.periodin_db_pruning + now |
| delacruzramo | ad682a5 | 2019-12-10 16:26:34 +0100 | [diff] [blame] | 603 | # self.tokens_cache.clear() # not required any more |
| 604 | |
| 605 | def remove_token_from_cache(self, token=None): |
| 606 | if token: |
| 607 | self.tokens_cache.pop(token, None) |
| 608 | else: |
| 609 | self.tokens_cache.clear() |
| 610 | self.msg.write("admin", "revoke_token", {"_id": token} if token else None) |