From 7bb53c64da2dcf88894bfd31503accdd81498f3d Mon Sep 17 00:00:00 2001 From: Thomas Duval Date: Wed, 3 Jun 2020 10:06:52 +0200 Subject: Update to new version 5.4 Signed-off-by: Thomas Duval Change-Id: Idcd868133d75928a1ffd74d749ce98503e0555ea --- moon_engine/moon_engine/api/wrapper/router.py | 115 ++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 moon_engine/moon_engine/api/wrapper/router.py (limited to 'moon_engine/moon_engine/api/wrapper/router.py') diff --git a/moon_engine/moon_engine/api/wrapper/router.py b/moon_engine/moon_engine/api/wrapper/router.py new file mode 100644 index 00000000..db6b6e24 --- /dev/null +++ b/moon_engine/moon_engine/api/wrapper/router.py @@ -0,0 +1,115 @@ +# Software Name: MOON + +# Version: 5.4 + +# SPDX-FileCopyrightText: Copyright (c) 2018-2020 Orange and its contributors +# SPDX-License-Identifier: Apache-2.0 + +# This software is distributed under the 'Apache License 2.0', +# the text of which is available at 'http://www.apache.org/licenses/LICENSE-2.0.txt' +# or see the "LICENSE" file for more details. + + +from moon_utilities import exceptions +from moon_cache.cache import Cache +from uuid import uuid4 +import logging +import requests +from moon_engine.api.configuration import get_configuration + +LOGGER = logging.getLogger("moon.engine.wrapper." + __name__) + + +class Router(object): + __CACHE = None + + def __init__(self, project_id, subject_name, object_name, action_name): + + if not self.__CACHE: + self.__CACHE = Cache.getInstance(manager_url=get_configuration("manager_url"), + incremental=get_configuration("incremental_updates"), + manager_api_key=get_configuration("api_token")) + + self.pipeline_id = self.__check_pdp_from_cache(project_id) + + self.request_id = uuid4().hex + + self.ctx = { + "project_id": project_id, + "subject_name": subject_name, + "object_name": object_name, + "action_name": action_name + } + + # ToDo add status of request + self.__CACHE.authz_requests[self.request_id] = {} + + pdp_id = self.__CACHE.get_pdp_from_vim_project(project_id) + self.__CACHE.update(pipeline=pdp_id) + self.pipeline = [] + if self.pipeline_id in self.__CACHE.pipelines: + self.pipeline = self.__CACHE.pipelines[self.pipeline_id] + + if len(self.pipeline) == 0 or not all( + k in self.pipeline for k in ("host", "port")): + raise exceptions.MoonError('Void container chaining') + + def __enter__(self): + return self + + def __exit__(self, exc_type, exc_val, exc_tb): + self.__CACHE.authz_requests.pop(self.request_id) + + def auth_request(self): + req = None + endpoint = self.__CACHE.get_pipeline_url(self.ctx["project_id"]) + + try: + req = requests.get("{}/authz/{}/{}/{}".format( + endpoint, + self.ctx["subject_name"], + self.ctx["object_name"], + self.ctx["action_name"]), + timeout=2 + ) + + if req.status_code != 200 and req.status_code != 202 and req.status_code != 204: + raise exceptions.AuthzException( + "Receive bad response from Authz function (with address - {})" + .format(req.status_code)) + + except requests.exceptions.ConnectionError: + LOGGER.error("Cannot connect to {}".format( + "{}/authz".format(endpoint)) + ) + except requests.exceptions.ReadTimeout: + LOGGER.error("Timeout error") + return {"result": False, "message": "Timeout during request for pipeline"}, 400 + except Exception as e: + LOGGER.error("Unexpected error:", e) + return {"result": False, "message": e}, 400 + + if not req: + raise exceptions.AuthzException("Cannot connect to Authz function") + + if req.status_code == 204: + return {"result": True, "message": ""} + return {"result": False, "message": req.content}, 400 + + def __check_pdp_from_cache(self, uuid): + """Check if a PDP exist with this ID in the cache of this component + + :param uuid: Keystone Project ID + :return: True or False + """ + + if self.__CACHE.get_pdp_from_vim_project(uuid): + return self.__CACHE.get_pipeline_id_from_project_id(uuid) + + self.__CACHE.update() + + if self.__CACHE.get_pdp_from_vim_project(uuid): + return self.__CACHE.get_pipeline_id_from_project_id(uuid) + + raise exceptions.MoonError("Unknown Project ID {}".format(uuid)) + -- cgit 1.2.3-korg