blob: a30f60c806769b7f95c619753fd640902d1a1eeb [file] [log] [blame]
Eduardo Sousa819d34c2018-07-31 01:20:02 +01001# -*- coding: utf-8 -*-
2
Eduardo Sousad795f872019-02-05 16:05:53 +00003# Copyright 2018 Whitestack, LLC
4# Copyright 2018 Telefonica S.A.
tierno0ea204e2019-01-25 14:16:24 +00005#
Eduardo Sousad795f872019-02-05 16:05:53 +00006# 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
tierno0ea204e2019-01-25 14:16:24 +000011#
12# Unless required by applicable law or agreed to in writing, software
Eduardo Sousad795f872019-02-05 16:05:53 +000013# 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##
tierno0ea204e2019-01-25 14:16:24 +000021
22
Eduardo Sousa819d34c2018-07-31 01:20:02 +010023"""
24Authenticator is responsible for authenticating the users,
25create the tokens unscoped and scoped, retrieve the role
26list inside the projects that they are inserted
27"""
28
tierno0ea204e2019-01-25 14:16:24 +000029__author__ = "Eduardo Sousa <esousa@whitestack.com>; Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>"
Eduardo Sousa819d34c2018-07-31 01:20:02 +010030__date__ = "$27-jul-2018 23:59:59$"
31
Eduardo Sousad1b525d2018-10-04 04:24:18 +010032import cherrypy
Eduardo Sousa819d34c2018-07-31 01:20:02 +010033import logging
Eduardo Sousa29933fc2018-11-14 06:36:35 +000034import yaml
Eduardo Sousa2f988212018-07-26 01:04:11 +010035from base64 import standard_b64decode
Eduardo Sousa819d34c2018-07-31 01:20:02 +010036from copy import deepcopy
garciadeblas4568a372021-03-24 09:19:48 +010037
tierno38dcfeb2019-06-10 16:44:00 +000038# from functools import reduce
Eduardo Sousa2f988212018-07-26 01:04:11 +010039from http import HTTPStatus
Eduardo Sousa819d34c2018-07-31 01:20:02 +010040from time import time
Eduardo Sousa5c01e192019-05-08 02:35:47 +010041from os import path
Eduardo Sousa2f988212018-07-26 01:04:11 +010042
tiernoc23a9bb2020-06-24 10:54:11 +000043from osm_nbi.authconn import AuthException, AuthconnException, AuthExceptionUnauthorized
tierno9c630112019-08-29 14:21:41 +000044from osm_nbi.authconn_keystone import AuthconnKeystone
delacruzramoad682a52019-12-10 16:26:34 +010045from osm_nbi.authconn_internal import AuthconnInternal
K Sai Kiran7ddb0732020-10-30 11:14:44 +053046from osm_nbi.authconn_tacacs import AuthconnTacacs
elumalai7802ff82023-04-24 20:38:32 +053047from osm_nbi.utils import cef_event, cef_event_builder
delacruzramoad682a52019-12-10 16:26:34 +010048from osm_common import dbmemory, dbmongo, msglocal, msgkafka
Eduardo Sousad1b525d2018-10-04 04:24:18 +010049from osm_common.dbbase import DbException
delacruzramo029405d2019-09-26 10:52:56 +020050from osm_nbi.validation import is_valid_uuid
tierno701018c2019-06-25 11:13:14 +000051from itertools import chain
delacruzramo01b15d32019-07-02 14:37:47 +020052from uuid import uuid4
delacruzramoceb8baf2019-06-21 14:25:38 +020053
Eduardo Sousa2f988212018-07-26 01:04:11 +010054
Eduardo Sousa819d34c2018-07-31 01:20:02 +010055class Authenticator:
56 """
57 This class should hold all the mechanisms for User Authentication and
58 Authorization. Initially it should support Openstack Keystone as a
59 backend through a plugin model where more backends can be added and a
60 RBAC model to manage permissions on operations.
tierno65ca36d2019-02-12 19:27:52 +010061 This class must be threading safe
Eduardo Sousa819d34c2018-07-31 01:20:02 +010062 """
Eduardo Sousa2f988212018-07-26 01:04:11 +010063
garciadeblas4568a372021-03-24 09:19:48 +010064 periodin_db_pruning = (
65 60 * 30
66 ) # for the internal backend only. every 30 minutes expired tokens will be pruned
67 token_limit = 500 # when reached, the token cache will be cleared
tierno0ea204e2019-01-25 14:16:24 +000068
tierno701018c2019-06-25 11:13:14 +000069 def __init__(self, valid_methods, valid_query_string):
Eduardo Sousa819d34c2018-07-31 01:20:02 +010070 """
71 Authenticator initializer. Setup the initial state of the object,
72 while it waits for the config dictionary and database initialization.
Eduardo Sousa819d34c2018-07-31 01:20:02 +010073 """
Eduardo Sousa819d34c2018-07-31 01:20:02 +010074 self.backend = None
75 self.config = None
76 self.db = None
delacruzramoad682a52019-12-10 16:26:34 +010077 self.msg = None
tierno0ea204e2019-01-25 14:16:24 +000078 self.tokens_cache = dict()
garciadeblas4568a372021-03-24 09:19:48 +010079 self.next_db_prune_time = (
80 0 # time when next cleaning of expired tokens must be done
81 )
Eduardo Sousa29933fc2018-11-14 06:36:35 +000082 self.roles_to_operations_file = None
delacruzramo01b15d32019-07-02 14:37:47 +020083 # self.roles_to_operations_table = None
Eduardo Sousa29933fc2018-11-14 06:36:35 +000084 self.resources_to_operations_mapping = {}
85 self.operation_to_allowed_roles = {}
Eduardo Sousa819d34c2018-07-31 01:20:02 +010086 self.logger = logging.getLogger("nbi.authenticator")
tierno701018c2019-06-25 11:13:14 +000087 self.role_permissions = []
88 self.valid_methods = valid_methods
89 self.valid_query_string = valid_query_string
garciadeblas4568a372021-03-24 09:19:48 +010090 self.system_admin_role_id = None # system_role id
tiernod4a705a2020-06-22 10:58:26 +000091 self.test_project_id = None # test_project_id
elumalai7802ff82023-04-24 20:38:32 +053092 self.cef_logger = None
Eduardo Sousa819d34c2018-07-31 01:20:02 +010093
94 def start(self, config):
95 """
96 Method to configure the Authenticator object. This method should be called
97 after object creation. It is responsible by initializing the selected backend,
98 as well as the initialization of the database connection.
99
100 :param config: dictionary containing the relevant parameters for this object.
101 """
102 self.config = config
elumalai7802ff82023-04-24 20:38:32 +0530103 self.cef_logger = cef_event_builder(config["authentication"])
Eduardo Sousa819d34c2018-07-31 01:20:02 +0100104
105 try:
Eduardo Sousa819d34c2018-07-31 01:20:02 +0100106 if not self.db:
Eduardo Sousad1b525d2018-10-04 04:24:18 +0100107 if config["database"]["driver"] == "mongo":
108 self.db = dbmongo.DbMongo()
109 self.db.db_connect(config["database"])
110 elif config["database"]["driver"] == "memory":
111 self.db = dbmemory.DbMemory()
112 self.db.db_connect(config["database"])
113 else:
garciadeblas4568a372021-03-24 09:19:48 +0100114 raise AuthException(
115 "Invalid configuration param '{}' at '[database]':'driver'".format(
116 config["database"]["driver"]
117 )
118 )
delacruzramoad682a52019-12-10 16:26:34 +0100119 if not self.msg:
120 if config["message"]["driver"] == "local":
121 self.msg = msglocal.MsgLocal()
122 self.msg.connect(config["message"])
123 elif config["message"]["driver"] == "kafka":
124 self.msg = msgkafka.MsgKafka()
125 self.msg.connect(config["message"])
126 else:
garciadeblas4568a372021-03-24 09:19:48 +0100127 raise AuthException(
128 "Invalid configuration param '{}' at '[message]':'driver'".format(
129 config["message"]["driver"]
130 )
131 )
tierno0ea204e2019-01-25 14:16:24 +0000132 if not self.backend:
133 if config["authentication"]["backend"] == "keystone":
garciadeblas4568a372021-03-24 09:19:48 +0100134 self.backend = AuthconnKeystone(
135 self.config["authentication"], self.db, self.role_permissions
136 )
tierno0ea204e2019-01-25 14:16:24 +0000137 elif config["authentication"]["backend"] == "internal":
garciadeblas4568a372021-03-24 09:19:48 +0100138 self.backend = AuthconnInternal(
139 self.config["authentication"], self.db, self.role_permissions
140 )
K Sai Kiran7ddb0732020-10-30 11:14:44 +0530141 self._internal_tokens_prune("tokens")
142 elif config["authentication"]["backend"] == "tacacs":
garciadeblas4568a372021-03-24 09:19:48 +0100143 self.backend = AuthconnTacacs(
144 self.config["authentication"], self.db, self.role_permissions
145 )
K Sai Kiran7ddb0732020-10-30 11:14:44 +0530146 self._internal_tokens_prune("tokens_tacacs")
tierno0ea204e2019-01-25 14:16:24 +0000147 else:
garciadeblas4568a372021-03-24 09:19:48 +0100148 raise AuthException(
149 "Unknown authentication backend: {}".format(
150 config["authentication"]["backend"]
151 )
152 )
tierno701018c2019-06-25 11:13:14 +0000153
Eduardo Sousa29933fc2018-11-14 06:36:35 +0000154 if not self.roles_to_operations_file:
155 if "roles_to_operations" in config["rbac"]:
garciadeblas4568a372021-03-24 09:19:48 +0100156 self.roles_to_operations_file = config["rbac"][
157 "roles_to_operations"
158 ]
Eduardo Sousa29933fc2018-11-14 06:36:35 +0000159 else:
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100160 possible_paths = (
garciadeblas4568a372021-03-24 09:19:48 +0100161 __file__[: __file__.rfind("auth.py")]
162 + "roles_to_operations.yml",
163 "./roles_to_operations.yml",
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100164 )
165 for config_file in possible_paths:
Eduardo Sousa29933fc2018-11-14 06:36:35 +0000166 if path.isfile(config_file):
167 self.roles_to_operations_file = config_file
168 break
tierno701018c2019-06-25 11:13:14 +0000169 if not self.roles_to_operations_file:
garciadeblas4568a372021-03-24 09:19:48 +0100170 raise AuthException(
171 "Invalid permission configuration: roles_to_operations file missing"
172 )
tierno701018c2019-06-25 11:13:14 +0000173
tierno701018c2019-06-25 11:13:14 +0000174 # load role_permissions
175 def load_role_permissions(method_dict):
176 for k in method_dict:
177 if k == "ROLE_PERMISSION":
garciadeblas4568a372021-03-24 09:19:48 +0100178 for method in chain(
179 method_dict.get("METHODS", ()), method_dict.get("TODO", ())
180 ):
tierno701018c2019-06-25 11:13:14 +0000181 permission = method_dict["ROLE_PERMISSION"] + method.lower()
182 if permission not in self.role_permissions:
183 self.role_permissions.append(permission)
184 elif k in ("TODO", "METHODS"):
185 continue
tierno74b53582020-06-18 10:52:37 +0000186 elif method_dict[k]:
tierno701018c2019-06-25 11:13:14 +0000187 load_role_permissions(method_dict[k])
188
189 load_role_permissions(self.valid_methods)
190 for query_string in self.valid_query_string:
191 for method in ("get", "put", "patch", "post", "delete"):
192 permission = query_string.lower() + ":" + method
193 if permission not in self.role_permissions:
194 self.role_permissions.append(permission)
195
tiernod4a705a2020-06-22 10:58:26 +0000196 # get ids of role system_admin and test project
garciadeblas4568a372021-03-24 09:19:48 +0100197 role_system_admin = self.db.get_one(
198 "roles", {"name": "system_admin"}, fail_on_empty=False
199 )
tiernod4a705a2020-06-22 10:58:26 +0000200 if role_system_admin:
201 self.system_admin_role_id = role_system_admin["_id"]
garciadeblas4568a372021-03-24 09:19:48 +0100202 test_project_name = self.config["authentication"].get(
203 "project_not_authorized", "admin"
204 )
205 test_project = self.db.get_one(
206 "projects", {"name": test_project_name}, fail_on_empty=False
207 )
tiernod4a705a2020-06-22 10:58:26 +0000208 if test_project:
209 self.test_project_id = test_project["_id"]
210
Eduardo Sousa819d34c2018-07-31 01:20:02 +0100211 except Exception as e:
212 raise AuthException(str(e))
213
Eduardo Sousad1b525d2018-10-04 04:24:18 +0100214 def stop(self):
215 try:
216 if self.db:
217 self.db.db_disconnect()
218 except DbException as e:
219 raise AuthException(str(e), http_code=e.http_code)
220
delacruzramo01b15d32019-07-02 14:37:47 +0200221 def create_admin_project(self):
222 """
223 Creates a new project 'admin' into database if it doesn't exist. Useful for initialization.
224 :return: _id identity of the 'admin' project
225 """
226
227 # projects = self.db.get_one("projects", fail_on_empty=False, fail_on_more=False)
228 project_desc = {"name": "admin"}
229 projects = self.backend.get_project_list(project_desc)
230 if projects:
231 return projects[0]["_id"]
232 now = time()
233 project_desc["_id"] = str(uuid4())
234 project_desc["_admin"] = {"created": now, "modified": now}
garciadeblasb6025472024-08-15 09:50:55 +0200235 project_desc["git_name"] = "osm_admin"
delacruzramo01b15d32019-07-02 14:37:47 +0200236 pid = self.backend.create_project(project_desc)
garciadeblas4568a372021-03-24 09:19:48 +0100237 self.logger.info(
238 "Project '{}' created at database".format(project_desc["name"])
239 )
delacruzramo01b15d32019-07-02 14:37:47 +0200240 return pid
241
242 def create_admin_user(self, project_id):
243 """
244 Creates a new user admin/admin into database if database is empty. Useful for initialization
245 :return: _id identity of the inserted data, or None
246 """
247 # users = self.db.get_one("users", fail_on_empty=False, fail_on_more=False)
248 users = self.backend.get_user_list()
249 if users:
250 return None
251 # user_desc = {"username": "admin", "password": "admin", "projects": [project_id]}
252 now = time()
garciadeblas4568a372021-03-24 09:19:48 +0100253 user_desc = {
254 "username": "admin",
255 "password": "admin",
garciadeblas6d83f8f2023-06-19 22:34:49 +0200256 "_admin": {"created": now, "modified": now, "user_status": "always-active"},
garciadeblas4568a372021-03-24 09:19:48 +0100257 }
delacruzramo01b15d32019-07-02 14:37:47 +0200258 if project_id:
259 pid = project_id
260 else:
261 # proj = self.db.get_one("projects", {"name": "admin"}, fail_on_empty=False, fail_on_more=False)
262 proj = self.backend.get_project_list({"name": "admin"})
263 pid = proj[0]["_id"] if proj else None
264 # role = self.db.get_one("roles", {"name": "system_admin"}, fail_on_empty=False, fail_on_more=False)
265 roles = self.backend.get_role_list({"name": "system_admin"})
266 if pid and roles:
garciadeblas4568a372021-03-24 09:19:48 +0100267 user_desc["project_role_mappings"] = [
268 {"project": pid, "role": roles[0]["_id"]}
269 ]
delacruzramo01b15d32019-07-02 14:37:47 +0200270 uid = self.backend.create_user(user_desc)
271 self.logger.info("User '{}' created at database".format(user_desc["username"]))
272 return uid
273
garciadeblas4568a372021-03-24 09:19:48 +0100274 def init_db(self, target_version="1.0"):
Eduardo Sousa819d34c2018-07-31 01:20:02 +0100275 """
Eduardo Sousa29933fc2018-11-14 06:36:35 +0000276 Check if the database has been initialized, with at least one user. If not, create the required tables
Eduardo Sousa819d34c2018-07-31 01:20:02 +0100277 and insert the predefined mappings between roles and permissions.
278
279 :param target_version: schema version that should be present in the database.
280 :return: None if OK, exception if error or version is different.
281 """
delacruzramoceb8baf2019-06-21 14:25:38 +0200282
delacruzramo01b15d32019-07-02 14:37:47 +0200283 records = self.backend.get_role_list()
Eduardo Sousa29933fc2018-11-14 06:36:35 +0000284
tiernoc23a9bb2020-06-24 10:54:11 +0000285 # Loading permissions to AUTH. At lease system_admin must be present.
garciadeblas4568a372021-03-24 09:19:48 +0100286 if not records or not next(
287 (r for r in records if r["name"] == "system_admin"), None
288 ):
Eduardo Sousa29933fc2018-11-14 06:36:35 +0000289 with open(self.roles_to_operations_file, "r") as stream:
garciadeblas4cd875d2023-02-14 19:05:34 +0100290 roles_to_operations_yaml = yaml.safe_load(stream)
Eduardo Sousa29933fc2018-11-14 06:36:35 +0000291
tierno1f029d82019-06-13 22:37:04 +0000292 role_names = []
293 for role_with_operations in roles_to_operations_yaml["roles"]:
294 # Verifying if role already exists. If it does, raise exception
295 if role_with_operations["name"] not in role_names:
296 role_names.append(role_with_operations["name"])
Eduardo Sousa29933fc2018-11-14 06:36:35 +0000297 else:
garciadeblas4568a372021-03-24 09:19:48 +0100298 raise AuthException(
299 "Duplicated role name '{}' at file '{}''".format(
300 role_with_operations["name"], self.roles_to_operations_file
301 )
302 )
tierno1f029d82019-06-13 22:37:04 +0000303
304 if not role_with_operations["permissions"]:
Eduardo Sousa29933fc2018-11-14 06:36:35 +0000305 continue
306
garciadeblas4568a372021-03-24 09:19:48 +0100307 for permission, is_allowed in role_with_operations[
308 "permissions"
309 ].items():
Eduardo Sousa29933fc2018-11-14 06:36:35 +0000310 if not isinstance(is_allowed, bool):
garciadeblas4568a372021-03-24 09:19:48 +0100311 raise AuthException(
312 "Invalid value for permission '{}' at role '{}'; at file '{}'".format(
313 permission,
314 role_with_operations["name"],
315 self.roles_to_operations_file,
316 )
317 )
Eduardo Sousa29933fc2018-11-14 06:36:35 +0000318
tiernoc23a9bb2020-06-24 10:54:11 +0000319 # TODO check permission is ok
tierno1f029d82019-06-13 22:37:04 +0000320 if permission[-1] == ":":
garciadeblas4568a372021-03-24 09:19:48 +0100321 raise AuthException(
322 "Invalid permission '{}' terminated in ':' for role '{}'; at file {}".format(
323 permission,
324 role_with_operations["name"],
325 self.roles_to_operations_file,
326 )
327 )
Eduardo Sousa29933fc2018-11-14 06:36:35 +0000328
tierno1f029d82019-06-13 22:37:04 +0000329 if "default" not in role_with_operations["permissions"]:
330 role_with_operations["permissions"]["default"] = False
331 if "admin" not in role_with_operations["permissions"]:
332 role_with_operations["permissions"]["admin"] = False
Eduardo Sousa29933fc2018-11-14 06:36:35 +0000333
334 now = time()
tierno1f029d82019-06-13 22:37:04 +0000335 role_with_operations["_admin"] = {
336 "created": now,
337 "modified": now,
Eduardo Sousa29933fc2018-11-14 06:36:35 +0000338 }
339
delacruzramo01b15d32019-07-02 14:37:47 +0200340 # self.db.create(self.roles_to_operations_table, role_with_operations)
tiernoc23a9bb2020-06-24 10:54:11 +0000341 try:
342 self.backend.create_role(role_with_operations)
garciadeblas4568a372021-03-24 09:19:48 +0100343 self.logger.info(
344 "Role '{}' created".format(role_with_operations["name"])
345 )
tiernoc23a9bb2020-06-24 10:54:11 +0000346 except (AuthException, AuthconnException) as e:
347 if role_with_operations["name"] == "system_admin":
348 raise
garciadeblas4568a372021-03-24 09:19:48 +0100349 self.logger.error(
350 "Role '{}' cannot be created: {}".format(
351 role_with_operations["name"], e
352 )
353 )
Eduardo Sousa29933fc2018-11-14 06:36:35 +0000354
delacruzramo01b15d32019-07-02 14:37:47 +0200355 # Create admin project&user if required
356 pid = self.create_admin_project()
tierno9ebbf852019-09-03 14:58:09 +0000357 user_id = self.create_admin_user(pid)
delacruzramo01b15d32019-07-02 14:37:47 +0200358
tierno9ebbf852019-09-03 14:58:09 +0000359 # try to assign system_admin role to user admin if not any user has this role
360 if not user_id:
delacruzramo01b15d32019-07-02 14:37:47 +0200361 try:
tierno9ebbf852019-09-03 14:58:09 +0000362 users = self.backend.get_user_list()
363 roles = self.backend.get_role_list({"name": "system_admin"})
364 role_id = roles[0]["_id"]
365 user_with_system_admin = False
366 user_admin_id = None
367 for user in users:
368 if not user_admin_id:
369 user_admin_id = user["_id"]
370 if user["username"] == "admin":
371 user_admin_id = user["_id"]
372 for prm in user.get("project_role_mappings", ()):
373 if prm["role"] == role_id:
374 user_with_system_admin = True
375 break
376 if user_with_system_admin:
377 break
378 if not user_with_system_admin:
garciadeblas4568a372021-03-24 09:19:48 +0100379 self.backend.update_user(
380 {
381 "_id": user_admin_id,
382 "add_project_role_mappings": [
383 {"project": pid, "role": role_id}
384 ],
385 }
386 )
387 self.logger.info(
388 "Added role system admin to user='{}' project=admin".format(
389 user_admin_id
390 )
391 )
delacruzramo15ec7062019-12-26 10:09:04 +0000392 except Exception as e:
garciadeblas4568a372021-03-24 09:19:48 +0100393 self.logger.error(
394 "Error in Authorization DataBase initialization: {}: {}".format(
395 type(e).__name__, e
396 )
397 )
tierno1f029d82019-06-13 22:37:04 +0000398
399 self.load_operation_to_allowed_roles()
400
401 def load_operation_to_allowed_roles(self):
402 """
tierno701018c2019-06-25 11:13:14 +0000403 Fills the internal self.operation_to_allowed_roles based on database role content and self.role_permissions
tiernoa6bb45d2019-06-14 09:45:39 +0000404 It works in a shadow copy and replace at the end to allow other threads working with the old copy
tierno1f029d82019-06-13 22:37:04 +0000405 :return: None
406 """
tierno701018c2019-06-25 11:13:14 +0000407 permissions = {oper: [] for oper in self.role_permissions}
delacruzramo01b15d32019-07-02 14:37:47 +0200408 # records = self.db.get_list(self.roles_to_operations_table)
409 records = self.backend.get_role_list()
Eduardo Sousa29933fc2018-11-14 06:36:35 +0000410
tiernoa6bb45d2019-06-14 09:45:39 +0000411 ignore_fields = ["_id", "_admin", "name", "default"]
Eduardo Sousa29933fc2018-11-14 06:36:35 +0000412 for record in records:
delacruzramo01b15d32019-07-02 14:37:47 +0200413 if not record.get("permissions"):
414 continue
garciadeblas4568a372021-03-24 09:19:48 +0100415 record_permissions = {
416 oper: record["permissions"].get("default", False)
417 for oper in self.role_permissions
418 }
419 operations_joined = [
420 (oper, value)
421 for oper, value in record["permissions"].items()
422 if oper not in ignore_fields
423 ]
Eduardo Sousa29933fc2018-11-14 06:36:35 +0000424 operations_joined.sort(key=lambda x: x[0].count(":"))
425
426 for oper in operations_joined:
garciadeblas4568a372021-03-24 09:19:48 +0100427 match = list(
428 filter(lambda x: x.find(oper[0]) == 0, record_permissions.keys())
429 )
Eduardo Sousa29933fc2018-11-14 06:36:35 +0000430
431 for m in match:
432 record_permissions[m] = oper[1]
433
434 allowed_operations = [k for k, v in record_permissions.items() if v is True]
435
436 for allowed_op in allowed_operations:
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100437 permissions[allowed_op].append(record["name"])
Eduardo Sousa29933fc2018-11-14 06:36:35 +0000438
tiernoa6bb45d2019-06-14 09:45:39 +0000439 self.operation_to_allowed_roles = permissions
Eduardo Sousa29933fc2018-11-14 06:36:35 +0000440
garciadeblas4568a372021-03-24 09:19:48 +0100441 def authorize(
442 self, role_permission=None, query_string_operations=None, item_id=None
443 ):
Eduardo Sousa2f988212018-07-26 01:04:11 +0100444 token = None
445 user_passwd64 = None
446 try:
447 # 1. Get token Authorization bearer
448 auth = cherrypy.request.headers.get("Authorization")
449 if auth:
450 auth_list = auth.split(" ")
451 if auth_list[0].lower() == "bearer":
452 token = auth_list[-1]
453 elif auth_list[0].lower() == "basic":
454 user_passwd64 = auth_list[-1]
455 if not token:
garciadeblasf2af4a12023-01-24 16:56:54 +0100456 if cherrypy.session.get("Authorization"): # pylint: disable=E1101
Eduardo Sousa2f988212018-07-26 01:04:11 +0100457 # 2. Try using session before request a new token. If not, basic authentication will generate
garciadeblasf2af4a12023-01-24 16:56:54 +0100458 token = cherrypy.session.get( # pylint: disable=E1101
459 "Authorization"
460 )
Eduardo Sousa2f988212018-07-26 01:04:11 +0100461 if token == "logout":
tierno0ea204e2019-01-25 14:16:24 +0000462 token = None # force Unauthorized response to insert user password again
garciadeblas4568a372021-03-24 09:19:48 +0100463 elif user_passwd64 and cherrypy.request.config.get(
464 "auth.allow_basic_authentication"
465 ):
Eduardo Sousa2f988212018-07-26 01:04:11 +0100466 # 3. Get new token from user password
467 user = None
468 passwd = None
469 try:
470 user_passwd = standard_b64decode(user_passwd64).decode()
471 user, _, passwd = user_passwd.partition(":")
472 except Exception:
473 pass
garciadeblas4568a372021-03-24 09:19:48 +0100474 outdata = self.new_token(
garciadeblasf2af4a12023-01-24 16:56:54 +0100475 None, {"username": user, "password": passwd}, None
garciadeblas4568a372021-03-24 09:19:48 +0100476 )
tierno701018c2019-06-25 11:13:14 +0000477 token = outdata["_id"]
garciadeblasf2af4a12023-01-24 16:56:54 +0100478 cherrypy.session["Authorization"] = token # pylint: disable=E1101
tiernoa6bb45d2019-06-14 09:45:39 +0000479
delacruzramoceb8baf2019-06-21 14:25:38 +0200480 if not token:
garciadeblas4568a372021-03-24 09:19:48 +0100481 raise AuthException(
482 "Needed a token or Authorization http header",
483 http_code=HTTPStatus.UNAUTHORIZED,
484 )
delacruzramoad682a52019-12-10 16:26:34 +0100485
486 # try to get from cache first
487 now = time()
488 token_info = self.tokens_cache.get(token)
489 if token_info and token_info["expires"] < now:
490 # delete token. MUST be done with care, as another thread maybe already delete it. Do not use del
491 self.tokens_cache.pop(token, None)
492 token_info = None
493
494 # get from database if not in cache
495 if not token_info:
496 token_info = self.backend.validate_token(token)
497 # Clear cache if token limit reached
498 if len(self.tokens_cache) > self.token_limit:
499 self.tokens_cache.clear()
500 self.tokens_cache[token] = token_info
delacruzramoceb8baf2019-06-21 14:25:38 +0200501 # TODO add to token info remote host, port
502
tierno701018c2019-06-25 11:13:14 +0000503 if role_permission:
garciadeblas4568a372021-03-24 09:19:48 +0100504 RBAC_auth = self.check_permissions(
505 token_info,
506 cherrypy.request.method,
507 role_permission,
508 query_string_operations,
509 item_id,
510 )
sousaedu60bf8952021-07-08 17:17:23 +0200511 self.logger.info("RBAC_auth: {}".format(RBAC_auth))
elumalai7802ff82023-04-24 20:38:32 +0530512 if RBAC_auth:
513 cef_event(
514 self.cef_logger,
515 {
516 "name": "System Access",
517 "sourceUserName": token_info.get("username"),
518 "message": "Accessing account with system privileges, Project={}".format(
519 token_info.get("project_name")
520 ),
521 },
522 )
523 self.logger.info("{}".format(self.cef_logger))
delacruzramo029405d2019-09-26 10:52:56 +0200524 token_info["allow_show_user_project_role"] = RBAC_auth
525
delacruzramoceb8baf2019-06-21 14:25:38 +0200526 return token_info
Eduardo Sousad1b525d2018-10-04 04:24:18 +0100527 except AuthException as e:
tiernoc8445362019-06-14 12:07:15 +0000528 if not isinstance(e, AuthExceptionUnauthorized):
garciadeblasf2af4a12023-01-24 16:56:54 +0100529 if cherrypy.session.get("Authorization"): # pylint: disable=E1101
530 del cherrypy.session["Authorization"] # pylint: disable=E1101
garciadeblas4568a372021-03-24 09:19:48 +0100531 cherrypy.response.headers[
532 "WWW-Authenticate"
533 ] = 'Bearer realm="{}"'.format(e)
tiernod4a705a2020-06-22 10:58:26 +0000534 if self.config["authentication"].get("user_not_authorized"):
garciadeblas4568a372021-03-24 09:19:48 +0100535 return {
536 "id": "testing-token",
537 "_id": "testing-token",
538 "project_id": self.test_project_id,
539 "username": self.config["authentication"]["user_not_authorized"],
540 "roles": [self.system_admin_role_id],
541 "admin": True,
542 "allow_show_user_project_role": True,
543 }
tiernoc8445362019-06-14 12:07:15 +0000544 raise
Eduardo Sousa2f988212018-07-26 01:04:11 +0100545
tierno701018c2019-06-25 11:13:14 +0000546 def new_token(self, token_info, indata, remote):
547 new_token_info = self.backend.authenticate(
tierno6486f742020-02-13 16:30:14 +0000548 credentials=indata,
tierno701018c2019-06-25 11:13:14 +0000549 token_info=token_info,
delacruzramoceb8baf2019-06-21 14:25:38 +0200550 )
Eduardo Sousa819d34c2018-07-31 01:20:02 +0100551
tierno701018c2019-06-25 11:13:14 +0000552 new_token_info["remote_port"] = remote.port
553 if not new_token_info.get("expires"):
554 new_token_info["expires"] = time() + 3600
555 if not new_token_info.get("admin"):
garciadeblas4568a372021-03-24 09:19:48 +0100556 new_token_info["admin"] = (
557 True if new_token_info.get("project_name") == "admin" else False
558 )
tierno701018c2019-06-25 11:13:14 +0000559 # TODO put admin in RBAC
Eduardo Sousa819d34c2018-07-31 01:20:02 +0100560
delacruzramoceb8baf2019-06-21 14:25:38 +0200561 if remote.name:
tierno701018c2019-06-25 11:13:14 +0000562 new_token_info["remote_host"] = remote.name
delacruzramoceb8baf2019-06-21 14:25:38 +0200563 elif remote.ip:
tierno701018c2019-06-25 11:13:14 +0000564 new_token_info["remote_host"] = remote.ip
Eduardo Sousa819d34c2018-07-31 01:20:02 +0100565
tierno701018c2019-06-25 11:13:14 +0000566 # TODO call self._internal_tokens_prune(now) ?
567 return deepcopy(new_token_info)
Eduardo Sousa2f988212018-07-26 01:04:11 +0100568
tierno701018c2019-06-25 11:13:14 +0000569 def get_token_list(self, token_info):
Eduardo Sousa819d34c2018-07-31 01:20:02 +0100570 if self.config["authentication"]["backend"] == "internal":
tierno701018c2019-06-25 11:13:14 +0000571 return self._internal_get_token_list(token_info)
Eduardo Sousa819d34c2018-07-31 01:20:02 +0100572 else:
tierno0ea204e2019-01-25 14:16:24 +0000573 # TODO: check if this can be avoided. Backend may provide enough information
garciadeblas4568a372021-03-24 09:19:48 +0100574 return [
575 deepcopy(token)
576 for token in self.tokens_cache.values()
577 if token["username"] == token_info["username"]
578 ]
Eduardo Sousa2f988212018-07-26 01:04:11 +0100579
tierno701018c2019-06-25 11:13:14 +0000580 def get_token(self, token_info, token):
Eduardo Sousa819d34c2018-07-31 01:20:02 +0100581 if self.config["authentication"]["backend"] == "internal":
tierno701018c2019-06-25 11:13:14 +0000582 return self._internal_get_token(token_info, token)
Eduardo Sousa819d34c2018-07-31 01:20:02 +0100583 else:
tierno0ea204e2019-01-25 14:16:24 +0000584 # TODO: check if this can be avoided. Backend may provide enough information
585 token_value = self.tokens_cache.get(token)
Eduardo Sousa819d34c2018-07-31 01:20:02 +0100586 if not token_value:
Eduardo Sousad1b525d2018-10-04 04:24:18 +0100587 raise AuthException("token not found", http_code=HTTPStatus.NOT_FOUND)
garciadeblas4568a372021-03-24 09:19:48 +0100588 if (
589 token_value["username"] != token_info["username"]
590 and not token_info["admin"]
591 ):
592 raise AuthException(
593 "needed admin privileges", http_code=HTTPStatus.UNAUTHORIZED
594 )
Eduardo Sousa819d34c2018-07-31 01:20:02 +0100595 return token_value
Eduardo Sousa2f988212018-07-26 01:04:11 +0100596
Eduardo Sousa819d34c2018-07-31 01:20:02 +0100597 def del_token(self, token):
delacruzramoceb8baf2019-06-21 14:25:38 +0200598 try:
599 self.backend.revoke_token(token)
delacruzramoad682a52019-12-10 16:26:34 +0100600 # self.tokens_cache.pop(token, None)
601 self.remove_token_from_cache(token)
delacruzramoceb8baf2019-06-21 14:25:38 +0200602 return "token '{}' deleted".format(token)
603 except KeyError:
garciadeblas4568a372021-03-24 09:19:48 +0100604 raise AuthException(
605 "Token '{}' not found".format(token), http_code=HTTPStatus.NOT_FOUND
606 )
Eduardo Sousad1b525d2018-10-04 04:24:18 +0100607
garciadeblas4568a372021-03-24 09:19:48 +0100608 def check_permissions(
609 self,
610 token_info,
611 method,
612 role_permission=None,
613 query_string_operations=None,
614 item_id=None,
615 ):
tierno701018c2019-06-25 11:13:14 +0000616 """
617 Checks that operation has permissions to be done, base on the assigned roles to this user project
618 :param token_info: Dictionary that contains "roles" with a list of assigned roles.
619 This method fills the token_info["admin"] with True or False based on assigned tokens, if any allows admin
620 This will be used among others to hide or not the _admin content of topics
621 :param method: GET,PUT, POST, ...
622 :param role_permission: role permission name of the operation required
623 :param query_string_operations: list of possible admin query strings provided by user. It is checked that the
624 assigned role allows this query string for this method
delacruzramo029405d2019-09-26 10:52:56 +0200625 :param item_id: item identifier if included in the URL, None otherwise
626 :return: True if access granted by permission rules, False if access granted by default rules (Bug 853)
627 :raises: AuthExceptionUnauthorized if access denied
tierno701018c2019-06-25 11:13:14 +0000628 """
sousaedu490d0192021-05-05 12:48:16 +0200629 self.load_operation_to_allowed_roles()
Eduardo Sousa29933fc2018-11-14 06:36:35 +0000630
tierno701018c2019-06-25 11:13:14 +0000631 roles_required = self.operation_to_allowed_roles[role_permission]
632 roles_allowed = [role["name"] for role in token_info["roles"]]
Eduardo Sousa29933fc2018-11-14 06:36:35 +0000633
tierno701018c2019-06-25 11:13:14 +0000634 # fills token_info["admin"] if some roles allows it
635 token_info["admin"] = False
tiernoa6bb45d2019-06-14 09:45:39 +0000636 for role in roles_allowed:
tierno701018c2019-06-25 11:13:14 +0000637 if role in self.operation_to_allowed_roles["admin:" + method.lower()]:
638 token_info["admin"] = True
tiernoa6bb45d2019-06-14 09:45:39 +0000639 break
Eduardo Sousa29933fc2018-11-14 06:36:35 +0000640
641 if "anonymous" in roles_required:
delacruzramo029405d2019-09-26 10:52:56 +0200642 return True
tierno701018c2019-06-25 11:13:14 +0000643 operation_allowed = False
Eduardo Sousa29933fc2018-11-14 06:36:35 +0000644 for role in roles_allowed:
645 if role in roles_required:
tierno701018c2019-06-25 11:13:14 +0000646 operation_allowed = True
647 # if query_string operations, check if this role allows it
648 if not query_string_operations:
delacruzramo029405d2019-09-26 10:52:56 +0200649 return True
tierno701018c2019-06-25 11:13:14 +0000650 for query_string_operation in query_string_operations:
garciadeblas4568a372021-03-24 09:19:48 +0100651 if (
652 role
653 not in self.operation_to_allowed_roles[query_string_operation]
654 ):
tierno701018c2019-06-25 11:13:14 +0000655 break
656 else:
delacruzramo029405d2019-09-26 10:52:56 +0200657 return True
658
659 # Bug 853 - Final Solution
660 # User/Project/Role whole listings are filtered elsewhere
661 # uid, pid, rid = ("user_id", "project_id", "id") if is_valid_uuid(id) else ("username", "project_name", "name")
662 uid = "user_id" if is_valid_uuid(item_id) else "username"
garciadeblas4568a372021-03-24 09:19:48 +0100663 if (
664 role_permission
665 in [
666 "projects:get",
667 "projects:id:get",
668 "roles:get",
669 "roles:id:get",
670 "users:get",
671 ]
672 ) or (role_permission == "users:id:get" and item_id == token_info[uid]):
delacruzramo029405d2019-09-26 10:52:56 +0200673 # or (role_permission == "projects:id:get" and item_id == token_info[pid]) \
674 # or (role_permission == "roles:id:get" and item_id in [role[rid] for role in token_info["roles"]]):
675 return False
Eduardo Sousa29933fc2018-11-14 06:36:35 +0000676
tierno701018c2019-06-25 11:13:14 +0000677 if not operation_allowed:
678 raise AuthExceptionUnauthorized("Access denied: lack of permissions.")
679 else:
garciadeblas4568a372021-03-24 09:19:48 +0100680 raise AuthExceptionUnauthorized(
681 "Access denied: You have not permissions to use these admin query string"
682 )
Eduardo Sousa29933fc2018-11-14 06:36:35 +0000683
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100684 def get_user_list(self):
685 return self.backend.get_user_list()
686
Eduardo Sousa29933fc2018-11-14 06:36:35 +0000687 def _normalize_url(self, url, method):
tierno701018c2019-06-25 11:13:14 +0000688 # DEPRECATED !!!
Eduardo Sousa29933fc2018-11-14 06:36:35 +0000689 # Removing query strings
garciadeblas4568a372021-03-24 09:19:48 +0100690 normalized_url = url if "?" not in url else url[: url.find("?")]
Eduardo Sousa29933fc2018-11-14 06:36:35 +0000691 normalized_url_splitted = normalized_url.split("/")
692 parameters = {}
693
garciadeblas4568a372021-03-24 09:19:48 +0100694 filtered_keys = [
695 key
696 for key in self.resources_to_operations_mapping.keys()
697 if method in key.split()[0]
698 ]
Eduardo Sousa29933fc2018-11-14 06:36:35 +0000699
700 for idx, path_part in enumerate(normalized_url_splitted):
701 tmp_keys = []
702 for tmp_key in filtered_keys:
703 splitted = tmp_key.split()[1].split("/")
Eduardo Sousacc02e9a2019-03-20 17:32:36 +0000704 if idx >= len(splitted):
705 continue
706 elif "<" in splitted[idx] and ">" in splitted[idx]:
Eduardo Sousa29933fc2018-11-14 06:36:35 +0000707 if splitted[idx] == "<artifactPath>":
708 tmp_keys.append(tmp_key)
709 continue
garciadeblas4568a372021-03-24 09:19:48 +0100710 elif idx == len(normalized_url_splitted) - 1 and len(
711 normalized_url_splitted
712 ) != len(splitted):
Eduardo Sousa29933fc2018-11-14 06:36:35 +0000713 continue
714 else:
715 tmp_keys.append(tmp_key)
716 elif splitted[idx] == path_part:
garciadeblas4568a372021-03-24 09:19:48 +0100717 if idx == len(normalized_url_splitted) - 1 and len(
718 normalized_url_splitted
719 ) != len(splitted):
Eduardo Sousa29933fc2018-11-14 06:36:35 +0000720 continue
721 else:
722 tmp_keys.append(tmp_key)
723 filtered_keys = tmp_keys
garciadeblas4568a372021-03-24 09:19:48 +0100724 if (
725 len(filtered_keys) == 1
726 and filtered_keys[0].split("/")[-1] == "<artifactPath>"
727 ):
Eduardo Sousa29933fc2018-11-14 06:36:35 +0000728 break
729
730 if len(filtered_keys) == 0:
garciadeblas4568a372021-03-24 09:19:48 +0100731 raise AuthException(
732 "Cannot make an authorization decision. URL not found. URL: {0}".format(
733 url
734 )
735 )
Eduardo Sousa29933fc2018-11-14 06:36:35 +0000736 elif len(filtered_keys) > 1:
garciadeblas4568a372021-03-24 09:19:48 +0100737 raise AuthException(
738 "Cannot make an authorization decision. Multiple URLs found. URL: {0}".format(
739 url
740 )
741 )
Eduardo Sousa29933fc2018-11-14 06:36:35 +0000742
743 filtered_key = filtered_keys[0]
744
745 for idx, path_part in enumerate(filtered_key.split()[1].split("/")):
746 if "<" in path_part and ">" in path_part:
747 if path_part == "<artifactPath>":
garciadeblas4568a372021-03-24 09:19:48 +0100748 parameters[path_part[1:-1]] = "/".join(
749 normalized_url_splitted[idx:]
750 )
Eduardo Sousa29933fc2018-11-14 06:36:35 +0000751 else:
752 parameters[path_part[1:-1]] = normalized_url_splitted[idx]
753
754 return filtered_key, parameters
755
tierno701018c2019-06-25 11:13:14 +0000756 def _internal_get_token_list(self, token_info):
tierno0ea204e2019-01-25 14:16:24 +0000757 now = time()
garciadeblas4568a372021-03-24 09:19:48 +0100758 token_list = self.db.get_list(
759 "tokens", {"username": token_info["username"], "expires.gt": now}
760 )
Eduardo Sousad1b525d2018-10-04 04:24:18 +0100761 return token_list
762
tierno701018c2019-06-25 11:13:14 +0000763 def _internal_get_token(self, token_info, token_id):
tierno0ea204e2019-01-25 14:16:24 +0000764 token_value = self.db.get_one("tokens", {"_id": token_id}, fail_on_empty=False)
Eduardo Sousad1b525d2018-10-04 04:24:18 +0100765 if not token_value:
766 raise AuthException("token not found", http_code=HTTPStatus.NOT_FOUND)
garciadeblas4568a372021-03-24 09:19:48 +0100767 if (
768 token_value["username"] != token_info["username"]
769 and not token_info["admin"]
770 ):
771 raise AuthException(
772 "needed admin privileges", http_code=HTTPStatus.UNAUTHORIZED
773 )
Eduardo Sousad1b525d2018-10-04 04:24:18 +0100774 return token_value
775
K Sai Kiran7ddb0732020-10-30 11:14:44 +0530776 def _internal_tokens_prune(self, token_collection, now=None):
tierno0ea204e2019-01-25 14:16:24 +0000777 now = now or time()
778 if not self.next_db_prune_time or self.next_db_prune_time >= now:
K Sai Kiran7ddb0732020-10-30 11:14:44 +0530779 self.db.del_list(token_collection, {"expires.lt": now})
tierno0ea204e2019-01-25 14:16:24 +0000780 self.next_db_prune_time = self.periodin_db_pruning + now
delacruzramoad682a52019-12-10 16:26:34 +0100781 # self.tokens_cache.clear() # not required any more
782
783 def remove_token_from_cache(self, token=None):
784 if token:
785 self.tokens_cache.pop(token, None)
786 else:
787 self.tokens_cache.clear()
788 self.msg.write("admin", "revoke_token", {"_id": token} if token else None)
selvi.ja9a1fc82022-04-04 06:54:30 +0000789
790 def check_password_expiry(self, outdata):
791 """
792 This method will check for password expiry of the user
793 :param outdata: user token information
794 """
garciadeblas6d83f8f2023-06-19 22:34:49 +0200795 user_list = None
selvi.ja9a1fc82022-04-04 06:54:30 +0000796 present_time = time()
797 user = outdata["username"]
garciadeblas6d83f8f2023-06-19 22:34:49 +0200798 if self.config["authentication"].get("user_management"):
799 user_list = self.db.get_list("users", {"username": user})
800 if user_list:
801 user_content = user_list[0]
802 if not user_content.get("username") == "admin":
803 user_content["_admin"]["modified"] = present_time
804 if user_content.get("_admin").get("password_expire_time"):
805 password_expire_time = user_content["_admin"][
806 "password_expire_time"
807 ]
808 else:
809 password_expire_time = present_time
810 uid = user_content["_id"]
811 self.db.set_one("users", {"_id": uid}, user_content)
812 if not present_time < password_expire_time:
813 return True
selvi.ja9a1fc82022-04-04 06:54:30 +0000814 else:
815 pass