1 # -*- coding: utf-8 -*-
3 # Copyright 2018 Whitestack, LLC
4 # Copyright 2018 Telefonica S.A.
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
10 # http://www.apache.org/licenses/LICENSE-2.0
12 # Unless required by applicable law or agreed to in writing, software
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
18 # For those usages not covered by the Apache License, Version 2.0 please
19 # contact: esousa@whitestack.com or alfonso.tiernosepulveda@telefonica.com
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
29 __author__
= "Eduardo Sousa <esousa@whitestack.com>; Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>"
30 __date__
= "$27-jul-2018 23:59:59$"
35 from base64
import standard_b64decode
36 from copy
import deepcopy
37 from functools
import reduce
38 from hashlib
import sha256
39 from http
import HTTPStatus
40 from random
import choice
as random_choice
43 from base_topic
import BaseTopic
# To allow project names in project_id
45 from authconn
import AuthException
46 from authconn_keystone
import AuthconnKeystone
47 from osm_common
import dbmongo
48 from osm_common
import dbmemory
49 from osm_common
.dbbase
import DbException
54 This class should hold all the mechanisms for User Authentication and
55 Authorization. Initially it should support Openstack Keystone as a
56 backend through a plugin model where more backends can be added and a
57 RBAC model to manage permissions on operations.
58 This class must be threading safe
61 periodin_db_pruning
= 60 * 30 # for the internal backend only. every 30 minutes expired tokens will be pruned
65 Authenticator initializer. Setup the initial state of the object,
66 while it waits for the config dictionary and database initialization.
71 self
.tokens_cache
= dict()
72 self
.next_db_prune_time
= 0 # time when next cleaning of expired tokens must be done
73 self
.resources_to_operations_file
= None
74 self
.roles_to_operations_file
= None
75 self
.resources_to_operations_mapping
= {}
76 self
.operation_to_allowed_roles
= {}
77 self
.logger
= logging
.getLogger("nbi.authenticator")
79 def start(self
, config
):
81 Method to configure the Authenticator object. This method should be called
82 after object creation. It is responsible by initializing the selected backend,
83 as well as the initialization of the database connection.
85 :param config: dictionary containing the relevant parameters for this object.
91 if config
["database"]["driver"] == "mongo":
92 self
.db
= dbmongo
.DbMongo()
93 self
.db
.db_connect(config
["database"])
94 elif config
["database"]["driver"] == "memory":
95 self
.db
= dbmemory
.DbMemory()
96 self
.db
.db_connect(config
["database"])
98 raise AuthException("Invalid configuration param '{}' at '[database]':'driver'"
99 .format(config
["database"]["driver"]))
101 if config
["authentication"]["backend"] == "keystone":
102 self
.backend
= AuthconnKeystone(self
.config
["authentication"])
103 elif config
["authentication"]["backend"] == "internal":
104 self
._internal
_tokens
_prune
()
106 raise AuthException("Unknown authentication backend: {}"
107 .format(config
["authentication"]["backend"]))
108 if not self
.resources_to_operations_file
:
109 if "resources_to_operations" in config
["rbac"]:
110 self
.resources_to_operations_file
= config
["rbac"]["resources_to_operations"]
113 __file__
[:__file__
.rfind("auth.py")] + "resources_to_operations.yml",
114 "./resources_to_operations.yml"
116 for config_file
in possible_paths
:
117 if path
.isfile(config_file
):
118 self
.resources_to_operations_file
= config_file
120 if not self
.resources_to_operations_file
:
121 raise AuthException("Invalid permission configuration: resources_to_operations file missing")
122 if not self
.roles_to_operations_file
:
123 if "roles_to_operations" in config
["rbac"]:
124 self
.roles_to_operations_file
= config
["rbac"]["roles_to_operations"]
127 __file__
[:__file__
.rfind("auth.py")] + "roles_to_operations.yml",
128 "./roles_to_operations.yml"
130 for config_file
in possible_paths
:
131 if path
.isfile(config_file
):
132 self
.roles_to_operations_file
= config_file
134 if not self
.roles_to_operations_file
:
135 raise AuthException("Invalid permission configuration: roles_to_operations file missing")
136 except Exception as e
:
137 raise AuthException(str(e
))
142 self
.db
.db_disconnect()
143 except DbException
as e
:
144 raise AuthException(str(e
), http_code
=e
.http_code
)
146 def init_db(self
, target_version
='1.0'):
148 Check if the database has been initialized, with at least one user. If not, create the required tables
149 and insert the predefined mappings between roles and permissions.
151 :param target_version: schema version that should be present in the database.
152 :return: None if OK, exception if error or version is different.
154 # Always reads operation to resource mapping from file (this is static, no need to store it in MongoDB)
155 # Operations encoding: "<METHOD> <URL>"
156 # Note: it is faster to rewrite the value than to check if it is already there or not
157 if self
.config
["authentication"]["backend"] == "internal":
161 with
open(self
.resources_to_operations_file
, "r") as stream
:
162 resources_to_operations_yaml
= yaml
.load(stream
)
164 for resource
, operation
in resources_to_operations_yaml
["resources_to_operations"].items():
165 operation_key
= operation
.replace(".", ":")
166 if operation_key
not in operations
:
167 operations
.append(operation_key
)
168 self
.resources_to_operations_mapping
[resource
] = operation_key
170 records
= self
.db
.get_list("roles_operations")
172 # Loading permissions to MongoDB. If there are permissions already in MongoDB, do nothing.
173 if len(records
) == 0:
174 with
open(self
.roles_to_operations_file
, "r") as stream
:
175 roles_to_operations_yaml
= yaml
.load(stream
)
178 for role_with_operations
in roles_to_operations_yaml
["roles_to_operations"]:
179 # Verifying if role already exists. If it does, send warning to log and ignore it.
180 if role_with_operations
["role"] not in roles
:
181 roles
.append(role_with_operations
["role"])
183 self
.logger
.warning("Duplicated role with name: {0}. Role definition is ignored."
184 .format(role_with_operations
["role"]))
190 if not role_with_operations
["operations"]:
193 for operation
, is_allowed
in role_with_operations
["operations"].items():
194 if not isinstance(is_allowed
, bool):
201 if len(operation
) != 1 and operation
[-1] == ".":
202 self
.logger
.warning("Invalid operation {0} terminated in '.'. "
203 "Operation will be discarded"
207 operation_key
= operation
.replace(".", ":")
208 if operation_key
not in role_ops
.keys():
209 role_ops
[operation_key
] = is_allowed
211 self
.logger
.info("In role {0}, the operation {1} with the value {2} was discarded due to "
212 "repetition.".format(role_with_operations
["role"], operation
, is_allowed
))
216 self
.logger
.info("Root for role {0} not defined. Default value 'False' applied."
217 .format(role_with_operations
["role"]))
220 operation_to_roles_item
= {
225 "name": role_with_operations
["role"],
229 for operation
, value
in role_ops
.items():
230 operation_to_roles_item
[operation
] = value
232 if self
.config
["authentication"]["backend"] != "internal" and \
233 role_with_operations
["role"] != "anonymous":
234 keystone_id
= self
.backend
.create_role(role_with_operations
["role"])
235 operation_to_roles_item
["_id"] = keystone_id
["_id"]
237 self
.db
.create("roles_operations", operation_to_roles_item
)
239 permissions
= {oper
: [] for oper
in operations
}
240 records
= self
.db
.get_list("roles_operations")
242 ignore_fields
= ["_id", "_admin", "name", "root"]
243 for record
in records
:
244 record_permissions
= {oper
: record
["root"] for oper
in operations
}
245 operations_joined
= [(oper
, value
) for oper
, value
in record
.items() if oper
not in ignore_fields
]
246 operations_joined
.sort(key
=lambda x
: x
[0].count(":"))
248 for oper
in operations_joined
:
249 match
= list(filter(lambda x
: x
.find(oper
[0]) == 0, record_permissions
.keys()))
252 record_permissions
[m
] = oper
[1]
254 allowed_operations
= [k
for k
, v
in record_permissions
.items() if v
is True]
256 for allowed_op
in allowed_operations
:
257 permissions
[allowed_op
].append(record
["name"])
259 for oper
, role_list
in permissions
.items():
260 self
.operation_to_allowed_roles
[oper
] = role_list
262 if self
.config
["authentication"]["backend"] != "internal":
263 self
.backend
.assign_role_to_user("admin", "admin", "system_admin")
269 # 1. Get token Authorization bearer
270 auth
= cherrypy
.request
.headers
.get("Authorization")
272 auth_list
= auth
.split(" ")
273 if auth_list
[0].lower() == "bearer":
274 token
= auth_list
[-1]
275 elif auth_list
[0].lower() == "basic":
276 user_passwd64
= auth_list
[-1]
278 if cherrypy
.session
.get("Authorization"):
279 # 2. Try using session before request a new token. If not, basic authentication will generate
280 token
= cherrypy
.session
.get("Authorization")
281 if token
== "logout":
282 token
= None # force Unauthorized response to insert user password again
283 elif user_passwd64
and cherrypy
.request
.config
.get("auth.allow_basic_authentication"):
284 # 3. Get new token from user password
288 user_passwd
= standard_b64decode(user_passwd64
).decode()
289 user
, _
, passwd
= user_passwd
.partition(":")
292 outdata
= self
.new_token(None, {"username": user
, "password": passwd
})
293 token
= outdata
["id"]
294 cherrypy
.session
['Authorization'] = token
295 if self
.config
["authentication"]["backend"] == "internal":
296 return self
._internal
_authorize
(token
)
299 raise AuthException("Needed a token or Authorization http header",
300 http_code
=HTTPStatus
.UNAUTHORIZED
)
302 self
.backend
.validate_token(token
)
303 self
.check_permissions(self
.tokens_cache
[token
], cherrypy
.request
.path_info
,
304 cherrypy
.request
.method
)
305 # TODO: check if this can be avoided. Backend may provide enough information
306 return deepcopy(self
.tokens_cache
[token
])
307 except AuthException
:
308 self
.del_token(token
)
310 except AuthException
as e
:
311 if cherrypy
.session
.get('Authorization'):
312 del cherrypy
.session
['Authorization']
313 cherrypy
.response
.headers
["WWW-Authenticate"] = 'Bearer realm="{}"'.format(e
)
314 raise AuthException(str(e
))
316 def new_token(self
, session
, indata
, remote
):
317 if self
.config
["authentication"]["backend"] == "internal":
318 return self
._internal
_new
_token
(session
, indata
, remote
)
320 if indata
.get("username"):
321 token
, projects
= self
.backend
.authenticate_with_user_password(
322 indata
.get("username"), indata
.get("password"))
324 token
, projects
= self
.backend
.authenticate_with_token(
325 session
.get("id"), indata
.get("project_id"))
327 raise AuthException("Provide credentials: username/password or Authorization Bearer token",
328 http_code
=HTTPStatus
.UNAUTHORIZED
)
330 if indata
.get("project_id"):
331 project_id
= indata
.get("project_id")
332 if project_id
not in projects
:
333 raise AuthException("Project {} not allowed for this user".format(project_id
),
334 http_code
=HTTPStatus
.UNAUTHORIZED
)
336 project_id
= projects
[0]
339 token
, projects
= self
.backend
.authenticate_with_token(token
, project_id
)
341 if project_id
== "admin":
344 session_admin
= reduce(lambda x
, y
: x
or (True if y
== "admin" else False),
352 "expires": now
+ 3600,
353 "project_id": project_id
,
354 "username": indata
.get("username") if not session
else session
.get("username"),
355 "remote_port": remote
.port
,
356 "admin": session_admin
360 new_session
["remote_host"] = remote
.name
362 new_session
["remote_host"] = remote
.ip
364 # TODO: check if this can be avoided. Backend may provide enough information
365 self
.tokens_cache
[token
] = new_session
367 return deepcopy(new_session
)
369 def get_token_list(self
, session
):
370 if self
.config
["authentication"]["backend"] == "internal":
371 return self
._internal
_get
_token
_list
(session
)
373 # TODO: check if this can be avoided. Backend may provide enough information
374 return [deepcopy(token
) for token
in self
.tokens_cache
.values()
375 if token
["username"] == session
["username"]]
377 def get_token(self
, session
, token
):
378 if self
.config
["authentication"]["backend"] == "internal":
379 return self
._internal
_get
_token
(session
, token
)
381 # TODO: check if this can be avoided. Backend may provide enough information
382 token_value
= self
.tokens_cache
.get(token
)
384 raise AuthException("token not found", http_code
=HTTPStatus
.NOT_FOUND
)
385 if token_value
["username"] != session
["username"] and not session
["admin"]:
386 raise AuthException("needed admin privileges", http_code
=HTTPStatus
.UNAUTHORIZED
)
389 def del_token(self
, token
):
390 if self
.config
["authentication"]["backend"] == "internal":
391 return self
._internal
_del
_token
(token
)
394 self
.backend
.revoke_token(token
)
395 del self
.tokens_cache
[token
]
396 return "token '{}' deleted".format(token
)
398 raise AuthException("Token '{}' not found".format(token
), http_code
=HTTPStatus
.NOT_FOUND
)
400 def check_permissions(self
, session
, url
, method
):
401 self
.logger
.info("Session: {}".format(session
))
402 self
.logger
.info("URL: {}".format(url
))
403 self
.logger
.info("Method: {}".format(method
))
405 key
, parameters
= self
._normalize
_url
(url
, method
)
407 # TODO: Check if parameters might be useful for the decision
409 operation
= self
.resources_to_operations_mapping
[key
]
410 roles_required
= self
.operation_to_allowed_roles
[operation
]
411 roles_allowed
= self
.backend
.get_user_role_list(session
["id"])
413 if "anonymous" in roles_required
:
416 for role
in roles_allowed
:
417 if role
in roles_required
:
420 raise AuthException("Access denied: lack of permissions.")
422 def get_user_list(self
):
423 return self
.backend
.get_user_list()
425 def _normalize_url(self
, url
, method
):
426 # Removing query strings
427 normalized_url
= url
if '?' not in url
else url
[:url
.find("?")]
428 normalized_url_splitted
= normalized_url
.split("/")
431 filtered_keys
= [key
for key
in self
.resources_to_operations_mapping
.keys()
432 if method
in key
.split()[0]]
434 for idx
, path_part
in enumerate(normalized_url_splitted
):
436 for tmp_key
in filtered_keys
:
437 splitted
= tmp_key
.split()[1].split("/")
438 if idx
>= len(splitted
):
440 elif "<" in splitted
[idx
] and ">" in splitted
[idx
]:
441 if splitted
[idx
] == "<artifactPath>":
442 tmp_keys
.append(tmp_key
)
444 elif idx
== len(normalized_url_splitted
) - 1 and \
445 len(normalized_url_splitted
) != len(splitted
):
448 tmp_keys
.append(tmp_key
)
449 elif splitted
[idx
] == path_part
:
450 if idx
== len(normalized_url_splitted
) - 1 and \
451 len(normalized_url_splitted
) != len(splitted
):
454 tmp_keys
.append(tmp_key
)
455 filtered_keys
= tmp_keys
456 if len(filtered_keys
) == 1 and \
457 filtered_keys
[0].split("/")[-1] == "<artifactPath>":
460 if len(filtered_keys
) == 0:
461 raise AuthException("Cannot make an authorization decision. URL not found. URL: {0}".format(url
))
462 elif len(filtered_keys
) > 1:
463 raise AuthException("Cannot make an authorization decision. Multiple URLs found. URL: {0}".format(url
))
465 filtered_key
= filtered_keys
[0]
467 for idx
, path_part
in enumerate(filtered_key
.split()[1].split("/")):
468 if "<" in path_part
and ">" in path_part
:
469 if path_part
== "<artifactPath>":
470 parameters
[path_part
[1:-1]] = "/".join(normalized_url_splitted
[idx
:])
472 parameters
[path_part
[1:-1]] = normalized_url_splitted
[idx
]
474 return filtered_key
, parameters
476 def _internal_authorize(self
, token_id
):
479 raise AuthException("Needed a token or Authorization http header", http_code
=HTTPStatus
.UNAUTHORIZED
)
480 # try to get from cache first
482 session
= self
.tokens_cache
.get(token_id
)
483 if session
and session
["expires"] < now
:
484 # delete token. MUST be done with care, as another thread maybe already delete it. Do not use del
485 self
.tokens_cache
.pop(token_id
, None)
490 # get from database if not in cache
491 session
= self
.db
.get_one("tokens", {"_id": token_id
})
492 if session
["expires"] < now
:
493 raise AuthException("Expired Token or Authorization http header", http_code
=HTTPStatus
.UNAUTHORIZED
)
494 self
.tokens_cache
[token_id
] = session
496 except DbException
as e
:
497 if e
.http_code
== HTTPStatus
.NOT_FOUND
:
498 raise AuthException("Invalid Token or Authorization http header", http_code
=HTTPStatus
.UNAUTHORIZED
)
502 except AuthException
:
503 if self
.config
["global"].get("test.user_not_authorized"):
504 return {"id": "fake-token-id-for-test",
505 "project_id": self
.config
["global"].get("test.project_not_authorized", "admin"),
506 "username": self
.config
["global"]["test.user_not_authorized"], "admin": True}
510 def _internal_new_token(self
, session
, indata
, remote
):
514 # Try using username/password
515 if indata
.get("username"):
516 user_rows
= self
.db
.get_list("users", {"username": indata
.get("username")})
518 user_content
= user_rows
[0]
519 salt
= user_content
["_admin"]["salt"]
520 shadow_password
= sha256(indata
.get("password", "").encode('utf-8') + salt
.encode('utf-8')).hexdigest()
521 if shadow_password
!= user_content
["password"]:
524 raise AuthException("Invalid username/password", http_code
=HTTPStatus
.UNAUTHORIZED
)
526 user_rows
= self
.db
.get_list("users", {"username": session
["username"]})
528 user_content
= user_rows
[0]
530 raise AuthException("Invalid token", http_code
=HTTPStatus
.UNAUTHORIZED
)
532 raise AuthException("Provide credentials: username/password or Authorization Bearer token",
533 http_code
=HTTPStatus
.UNAUTHORIZED
)
535 token_id
= ''.join(random_choice('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789')
536 for _
in range(0, 32))
537 project_id
= indata
.get("project_id")
539 if project_id
!= "admin":
540 # To allow project names in project_id
541 proj
= self
.db
.get_one("projects", {BaseTopic
.id_field("projects", project_id
): project_id
})
542 if proj
["_id"] not in user_content
["projects"] and proj
["name"] not in user_content
["projects"]:
543 raise AuthException("project {} not allowed for this user"
544 .format(project_id
), http_code
=HTTPStatus
.UNAUTHORIZED
)
546 project_id
= user_content
["projects"][0]
547 if project_id
== "admin":
550 # To allow project names in project_id
551 project
= self
.db
.get_one("projects", {BaseTopic
.id_field("projects", project_id
): project_id
})
552 session_admin
= project
.get("admin", False)
553 new_session
= {"issued_at": now
, "expires": now
+ 3600,
554 "_id": token_id
, "id": token_id
, "project_id": project_id
, "username": user_content
["username"],
555 "remote_port": remote
.port
, "admin": session_admin
}
557 new_session
["remote_host"] = remote
.name
559 new_session
["remote_host"] = remote
.ip
561 self
.tokens_cache
[token_id
] = new_session
562 self
.db
.create("tokens", new_session
)
563 # check if database must be prune
564 self
._internal
_tokens
_prune
(now
)
565 return deepcopy(new_session
)
567 def _internal_get_token_list(self
, session
):
569 token_list
= self
.db
.get_list("tokens", {"username": session
["username"], "expires.gt": now
})
572 def _internal_get_token(self
, session
, token_id
):
573 token_value
= self
.db
.get_one("tokens", {"_id": token_id
}, fail_on_empty
=False)
575 raise AuthException("token not found", http_code
=HTTPStatus
.NOT_FOUND
)
576 if token_value
["username"] != session
["username"] and not session
["admin"]:
577 raise AuthException("needed admin privileges", http_code
=HTTPStatus
.UNAUTHORIZED
)
580 def _internal_del_token(self
, token_id
):
582 self
.tokens_cache
.pop(token_id
, None)
583 self
.db
.del_one("tokens", {"_id": token_id
})
584 return "token '{}' deleted".format(token_id
)
585 except DbException
as e
:
586 if e
.http_code
== HTTPStatus
.NOT_FOUND
:
587 raise AuthException("Token '{}' not found".format(token_id
), http_code
=HTTPStatus
.NOT_FOUND
)
591 def _internal_tokens_prune(self
, now
=None):
593 if not self
.next_db_prune_time
or self
.next_db_prune_time
>= now
:
594 self
.db
.del_list("tokens", {"expires.lt": now
})
595 self
.next_db_prune_time
= self
.periodin_db_pruning
+ now
596 self
.tokens_cache
.clear() # force to reload tokens from database