From 2e35a7e46f0929438c1c206e3116caa829f07dc6 Mon Sep 17 00:00:00 2001 From: Thomas Duval Date: Fri, 5 Oct 2018 16:54:37 +0200 Subject: Update code to 4.6 official version Change-Id: Ibd0da0e476e24b2685f54693efc11f7a58d40a62 --- moon_wrapper/Changelog | 19 ++++++ moon_wrapper/moon_wrapper/__init__.py | 2 +- moon_wrapper/moon_wrapper/__main__.py | 4 +- moon_wrapper/moon_wrapper/api/generic.py | 17 +++--- moon_wrapper/moon_wrapper/api/oslowrapper.py | 41 +++++++------ moon_wrapper/moon_wrapper/api/slaveupdate.py | 87 ++++++++++++++++++++++++++++ moon_wrapper/moon_wrapper/http_server.py | 16 +++-- moon_wrapper/moon_wrapper/server.py | 4 +- moon_wrapper/tests/unit_python/conftest.py | 10 ++-- 9 files changed, 158 insertions(+), 42 deletions(-) create mode 100644 moon_wrapper/moon_wrapper/api/slaveupdate.py (limited to 'moon_wrapper') diff --git a/moon_wrapper/Changelog b/moon_wrapper/Changelog index 071e4ef9..b2d62657 100644 --- a/moon_wrapper/Changelog +++ b/moon_wrapper/Changelog @@ -26,3 +26,22 @@ CHANGES 4.5.1 ----- - use the threading capability of Flask app + +4.5.2 +----- +- apply pylint rules + +4.5.3 +----- +- Fix bug when OpenStack requests Moon + - bug on Keystone project ID + - bug when filtering the pipeline container name + +4.5.4 +----- +- Fix a bug in retrieval of object from OpenStack +- Fix a bug in rule element + +4.6.0 +----- +- Add the Update API diff --git a/moon_wrapper/moon_wrapper/__init__.py b/moon_wrapper/moon_wrapper/__init__.py index 98a98146..f0887748 100644 --- a/moon_wrapper/moon_wrapper/__init__.py +++ b/moon_wrapper/moon_wrapper/__init__.py @@ -3,4 +3,4 @@ # license which can be found in the file 'LICENSE' in this package distribution # or at 'http://www.apache.org/licenses/LICENSE-2.0'. -__version__ = "4.5.1" +__version__ = "4.6.0" diff --git a/moon_wrapper/moon_wrapper/__main__.py b/moon_wrapper/moon_wrapper/__main__.py index 46cafa76..3a403293 100644 --- a/moon_wrapper/moon_wrapper/__main__.py +++ b/moon_wrapper/moon_wrapper/__main__.py @@ -1,4 +1,4 @@ from moon_wrapper.server import main -server = main() -server.run() +SERVER = main() +SERVER.run() diff --git a/moon_wrapper/moon_wrapper/api/generic.py b/moon_wrapper/moon_wrapper/api/generic.py index 498513c7..e492b327 100644 --- a/moon_wrapper/moon_wrapper/api/generic.py +++ b/moon_wrapper/moon_wrapper/api/generic.py @@ -6,14 +6,14 @@ Those API are helping API used to manage the Moon platform. """ -from flask_restful import Resource, request import logging +from flask_restful import Resource, request import moon_wrapper.api from python_moonutilities.security_functions import check_auth __version__ = "0.1.0" -logger = logging.getLogger("moon.manager.api." + __name__) +LOGGER = logging.getLogger("moon.manager.api." + __name__) class Status(Resource): @@ -35,7 +35,7 @@ class Status(Resource): } } """ - raise NotImplemented + raise NotImplementedError class Logs(Resource): @@ -70,7 +70,7 @@ class Logs(Resource): args["to"] = to_str args["event_number"] = event_number - raise NotImplemented + raise NotImplementedError class API(Resource): @@ -112,20 +112,23 @@ class API(Resource): if "__version__" in dir(group_api_obj): api_desc[api_name]["version"] = group_api_obj.__version__ object_list = list(filter(lambda x: "__" not in x, dir(group_api_obj))) - for obj in map(lambda x: eval("moon_interface.api.{}.{}".format(api_name, x)), object_list): + for obj in map(lambda x: eval("moon_interface.api.{}.{}".format(api_name, x)), + object_list): if "__urls__" in dir(obj): api_desc[api_name][obj.__name__] = dict() api_desc[api_name][obj.__name__]["urls"] = obj.__urls__ api_desc[api_name][obj.__name__]["methods"] = dict() for _method in filter(lambda x: x in __methods, dir(obj)): - docstring = eval("moon_interface.api.{}.{}.{}.__doc__".format(api_name, obj.__name__, _method)) + docstring = eval( + "moon_interface.api.{}.{}.{}.__doc__".format(api_name, obj.__name__, + _method)) api_desc[api_name][obj.__name__]["methods"][_method] = docstring api_desc[api_name][obj.__name__]["description"] = str(obj.__doc__) if group_id in api_desc: if endpoint_id in api_desc[group_id]: return {group_id: {endpoint_id: api_desc[group_id][endpoint_id]}} elif len(endpoint_id) > 0: - logger.error("Unknown endpoint_id {}".format(endpoint_id)) + LOGGER.error("Unknown endpoint_id {}".format(endpoint_id)) return {"error": "Unknown endpoint_id {}".format(endpoint_id)} return {group_id: api_desc[group_id]} return api_desc diff --git a/moon_wrapper/moon_wrapper/api/oslowrapper.py b/moon_wrapper/moon_wrapper/api/oslowrapper.py index 905c32db..39128621 100644 --- a/moon_wrapper/moon_wrapper/api/oslowrapper.py +++ b/moon_wrapper/moon_wrapper/api/oslowrapper.py @@ -6,17 +6,17 @@ Authz is the endpoint to get authorization response """ +import logging +import json import flask from flask import request from flask_restful import Resource -import logging -import json import requests from python_moonutilities import exceptions __version__ = "0.1.0" -logger = logging.getLogger("moon.wrapper.api." + __name__) +LOGGER = logging.getLogger("moon.wrapper.api." + __name__) class OsloWrapper(Resource): @@ -35,15 +35,15 @@ class OsloWrapper(Resource): self.TIMEOUT = 5 def post(self): - logger.debug("POST {}".format(request.form)) + LOGGER.debug("POST {}".format(request.form)) response = flask.make_response("False") try: if self.manage_data(): response = flask.make_response("True") - except exceptions.AuthzException as e: - logger.error(e, exc_info=True) - except Exception as e: - logger.error(e, exc_info=True) + except exceptions.AuthzException as exception: + LOGGER.error(exception, exc_info=True) + except Exception as exception: + LOGGER.error(exception, exc_info=True) response.headers['content-type'] = 'application/octet-stream' return response @@ -64,20 +64,22 @@ class OsloWrapper(Resource): pass # note: default case - return target.get("project_id", "none") + return "none" @staticmethod def __get_project_id(target, credentials): - logger.info("__get_project_id {}".format(target)) - return target.get("project_id", "none") + project_id = target.get("project_id", None) + if not project_id: + project_id = credentials.get("project_id", None) + return project_id def get_interface_url(self, project_id): - logger.debug("project_id {}".format(project_id)) + LOGGER.debug("project_id {}".format(project_id)) for containers in self.CACHE.containers.values(): - logger.info("containers {}".format(containers)) + LOGGER.info("containers {}".format(containers)) for container in containers: if container.get("keystone_project_id") == project_id: - if "interface" in container['name']: + if "pipeline" in container['name']: return "http://{}:{}".format( container['name'], container['port']) @@ -86,7 +88,7 @@ class OsloWrapper(Resource): for containers in self.CACHE.containers.values(): for container in containers: if container.get("keystone_project_id") == project_id: - if "interface" in container['name']: + if "pipeline" in container['name']: return "http://{}:{}".format( container['name'], container['port']) @@ -100,14 +102,15 @@ class OsloWrapper(Resource): data = json.loads(request.data.decode("utf-8")) target = json.loads(data.get('target', {})) credentials = json.loads(data.get('credentials', {})) - rule = data.get('rule', "") + rule = data.get('rule', "").strip('"').strip("'") _subject = self.__get_subject(target, credentials) _object = self.__get_object(target, credentials) _action = rule + LOGGER.info("authz {} {} {}".format(_subject, _object, _action)) _project_id = self.__get_project_id(target, credentials) _pdp_id = self.CACHE.get_pdp_from_keystone_project(_project_id) interface_url = self.get_interface_url(_project_id) - logger.debug("interface_url={}".format(interface_url)) + LOGGER.debug("interface_url={}".format(interface_url)) req = requests.get("{}/authz/{}/{}/{}/{}".format( interface_url, _pdp_id, @@ -116,9 +119,9 @@ class OsloWrapper(Resource): _action )) - logger.debug("Get interface {}".format(req.text)) + LOGGER.debug("Get interface {}".format(req.text)) if req.status_code == 200: if req.json().get("result", False): return True - raise exceptions.AuthzException("error in authz request") \ No newline at end of file + raise exceptions.AuthzException("error in authz request") diff --git a/moon_wrapper/moon_wrapper/api/slaveupdate.py b/moon_wrapper/moon_wrapper/api/slaveupdate.py new file mode 100644 index 00000000..b2ce22f0 --- /dev/null +++ b/moon_wrapper/moon_wrapper/api/slaveupdate.py @@ -0,0 +1,87 @@ +# Copyright 2015 Open Platform for NFV Project, Inc. and its contributors +# This software is distributed under the terms and conditions of the 'Apache-2.0' +# license which can be found in the file 'LICENSE' in this package distribution +# or at 'http://www.apache.org/licenses/LICENSE-2.0'. +""" +Authz is the endpoint to get authorization response +""" + +import logging +import json +import flask +from flask import request +from flask_restful import Resource +import requests +from python_moonutilities import exceptions + +__version__ = "0.1.0" + +LOGGER = logging.getLogger("moon.wrapper.api." + __name__) + + +class SlaveUpdate(Resource): + """ + Endpoint for authz requests + """ + + __urls__ = ( + "/update", + "/update/", + ) + + def __init__(self, **kwargs): + self.port = kwargs.get("port") + self.CACHE = kwargs.get("cache", {}) + self.TIMEOUT = 5 + + def put(self): + LOGGER.warning("PUT {}".format(request.form)) + response = flask.make_response("False") + try: + if self.update_slave(): + response = flask.make_response("True") + except Exception as exception: + LOGGER.error(exception, exc_info=True) + + response.headers['content-type'] = 'application/octet-stream' + return response + + def get_interface_url(self, pdp_id): + LOGGER.debug("pdp_id {}".format(pdp_id)) + for containers in self.CACHE.containers.values(): + LOGGER.info("containers0 {}".format(containers)) + for container in containers: + if container.get("pdp_id") == pdp_id: + if "pipeline" in container['name']: + yield "http://{}:{}".format( + container['name'], + container['port']) + self.CACHE.update() + # Note (asteroide): test an other time after the update + for containers in self.CACHE.containers.values(): + LOGGER.info("containers1 {}".format(containers)) + for container in containers: + if container.get("pdp_id") == pdp_id: + if "pipeline" in container['name']: + yield "http://{}:{}".format( + container['name'], + container['port']) + + def update_slave(self): + result = {} + result_list = [] + for _pdp_id in self.CACHE.pdp: + result[_pdp_id] = {} + for interface_url in self.get_interface_url(_pdp_id): + + req = requests.put("{}/update".format(interface_url), request.form) + + if req.status_code == 200: + if req.json().get("result", False): + result[_pdp_id][interface_url] = True + result_list.append(True) + continue + LOGGER.warning("Error in {} {}: {}".format(_pdp_id, interface_url, req.text)) + result[_pdp_id][interface_url] = False + result_list.append(False) + return all(result_list) diff --git a/moon_wrapper/moon_wrapper/http_server.py b/moon_wrapper/moon_wrapper/http_server.py index dfbaed9f..015bb285 100644 --- a/moon_wrapper/moon_wrapper/http_server.py +++ b/moon_wrapper/moon_wrapper/http_server.py @@ -10,17 +10,17 @@ import logging from moon_wrapper import __version__ from moon_wrapper.api.generic import Status, Logs, API from moon_wrapper.api.oslowrapper import OsloWrapper +from moon_wrapper.api.slaveupdate import SlaveUpdate from python_moonutilities.cache import Cache from python_moonutilities import configuration, exceptions -logger = logging.getLogger("moon.wrapper.http_server") - +LOGGER = logging.getLogger("moon.wrapper.http_server") CACHE = Cache() __API__ = ( Status, Logs, API - ) +) class Server: @@ -71,7 +71,7 @@ class Root(Resource): """ The root of the web service """ - __urls__ = ("/", ) + __urls__ = ("/",) __methods = ("get", "post", "put", "delete", "options") def get(self): @@ -111,7 +111,6 @@ class HTTPServer(Server): self.__hook_errors() def __hook_errors(self): - def get_404_json(e): return flask.make_response("False") @@ -134,7 +133,12 @@ class HTTPServer(Server): "cache": CACHE, } ) + self.api.add_resource(SlaveUpdate, *SlaveUpdate.__urls__, + resource_class_kwargs={ + "orchestrator_url": self.orchestrator_url, + "cache": CACHE, + } + ) def run(self): self.app.run(host=self._host, port=self._port, threaded=True) # nosec - diff --git a/moon_wrapper/moon_wrapper/server.py b/moon_wrapper/moon_wrapper/server.py index 280fdb68..77def174 100644 --- a/moon_wrapper/moon_wrapper/server.py +++ b/moon_wrapper/moon_wrapper/server.py @@ -28,5 +28,5 @@ def main(): if __name__ == '__main__': - server = main() - server.run() + SERVER = main() + SERVER.run() diff --git a/moon_wrapper/tests/unit_python/conftest.py b/moon_wrapper/tests/unit_python/conftest.py index 2c332c89..a6677849 100644 --- a/moon_wrapper/tests/unit_python/conftest.py +++ b/moon_wrapper/tests/unit_python/conftest.py @@ -288,10 +288,10 @@ def set_consul_and_db(monkeypatch): "pdp_id": "b3d3e18abf3340e8b635fd49e6634ccd", "port": 8080, "genre": "interface", - "name": "interface-paltry", + "name": "pipeline-paltry", "keystone_project_id": "a64beb1cc224474fb4badd43173e7101", "namespace": "moon", - "container": "wukongsun/moon_interface:v4.3" + "container": "wukongsun/moon_pipeline:v4.3" }, { "pdp_id": "b3d3e18abf3340e8b635fd49e6634ccd", @@ -308,7 +308,7 @@ def set_consul_and_db(monkeypatch): "pdp_id": "invalid_pdp_id", "port": 8080, "genre": "interface", - "name": "interface-paltry", + "name": "pipeline-paltry", "keystone_project_id": "invalid_project_id", "namespace": "moon", "container": "wukongsun/moon_authz:v4.3" @@ -696,7 +696,7 @@ def set_consul_and_db(monkeypatch): content=get_pickled_context() ) m.register_uri( - 'GET', 'http://interface-paltry:8080/authz/{}/{}/{}/{}'.format( + 'GET', 'http://pipeline-paltry:8080/authz/{}/{}/{}/{}'.format( CONTEXT.get("pdp_id"), CONTEXT.get("subject_name"), CONTEXT.get("object_name"), @@ -705,7 +705,7 @@ def set_consul_and_db(monkeypatch): json={"result": True, "message": "================"} ) m.register_uri( - 'GET', 'http://interface-paltry:8080/authz/{}/{}/{}/{}'.format( + 'GET', 'http://pipeline-paltry:8080/authz/{}/{}/{}/{}'.format( CONTEXT.get("invalid_pdp_id"), CONTEXT.get("subject_name"), CONTEXT.get("object_name"), -- cgit 1.2.3-korg