aboutsummaryrefslogtreecommitdiffstats
path: root/old/moon_wrapper/moon_wrapper
diff options
context:
space:
mode:
Diffstat (limited to 'old/moon_wrapper/moon_wrapper')
-rw-r--r--old/moon_wrapper/moon_wrapper/__init__.py6
-rw-r--r--old/moon_wrapper/moon_wrapper/__main__.py4
-rw-r--r--old/moon_wrapper/moon_wrapper/api/__init__.py0
-rw-r--r--old/moon_wrapper/moon_wrapper/api/generic.py134
-rw-r--r--old/moon_wrapper/moon_wrapper/api/oslowrapper.py127
-rw-r--r--old/moon_wrapper/moon_wrapper/api/slaveupdate.py87
-rw-r--r--old/moon_wrapper/moon_wrapper/http_server.py144
-rw-r--r--old/moon_wrapper/moon_wrapper/server.py32
8 files changed, 534 insertions, 0 deletions
diff --git a/old/moon_wrapper/moon_wrapper/__init__.py b/old/moon_wrapper/moon_wrapper/__init__.py
new file mode 100644
index 00000000..f0887748
--- /dev/null
+++ b/old/moon_wrapper/moon_wrapper/__init__.py
@@ -0,0 +1,6 @@
+# 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'.
+
+__version__ = "4.6.0"
diff --git a/old/moon_wrapper/moon_wrapper/__main__.py b/old/moon_wrapper/moon_wrapper/__main__.py
new file mode 100644
index 00000000..3a403293
--- /dev/null
+++ b/old/moon_wrapper/moon_wrapper/__main__.py
@@ -0,0 +1,4 @@
+from moon_wrapper.server import main
+
+SERVER = main()
+SERVER.run()
diff --git a/old/moon_wrapper/moon_wrapper/api/__init__.py b/old/moon_wrapper/moon_wrapper/api/__init__.py
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/old/moon_wrapper/moon_wrapper/api/__init__.py
diff --git a/old/moon_wrapper/moon_wrapper/api/generic.py b/old/moon_wrapper/moon_wrapper/api/generic.py
new file mode 100644
index 00000000..e492b327
--- /dev/null
+++ b/old/moon_wrapper/moon_wrapper/api/generic.py
@@ -0,0 +1,134 @@
+# 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'.
+"""
+Those API are helping API used to manage the Moon platform.
+"""
+
+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__)
+
+
+class Status(Resource):
+ """
+ Endpoint for status requests
+ """
+
+ __urls__ = ("/status", "/status/", "/status/<string:component_id>")
+
+ def get(self, component_id=None):
+ """Retrieve status of all components
+
+ :return: {
+ "orchestrator": {
+ "status": "Running"
+ },
+ "security_router": {
+ "status": "Running"
+ }
+ }
+ """
+ raise NotImplementedError
+
+
+class Logs(Resource):
+ """
+ Endpoint for logs requests
+ """
+
+ __urls__ = ("/logs", "/logs/", "/logs/<string:component_id>")
+
+ def get(self, component_id=None):
+ """Get logs from the Moon platform
+
+ :param component_id: the ID of the component your are looking for (optional)
+ :return: [
+ "2015-04-15-13:45:20
+ "2015-04-15-13:45:21
+ "2015-04-15-13:45:22
+ "2015-04-15-13:45:23
+ ]
+ """
+ filter_str = request.args.get('filter', '')
+ from_str = request.args.get('from', '')
+ to_str = request.args.get('to', '')
+ event_number = request.args.get('event_number', '')
+ try:
+ event_number = int(event_number)
+ except ValueError:
+ event_number = None
+ args = dict()
+ args["filter"] = filter_str
+ args["from"] = from_str
+ args["to"] = to_str
+ args["event_number"] = event_number
+
+ raise NotImplementedError
+
+
+class API(Resource):
+ """
+ Endpoint for API requests
+ """
+
+ __urls__ = (
+ "/api",
+ "/api/",
+ "/api/<string:group_id>",
+ "/api/<string:group_id>/",
+ "/api/<string:group_id>/<string:endpoint_id>")
+
+ @check_auth
+ def get(self, group_id="", endpoint_id="", user_id=""):
+ """Retrieve all API endpoints or a specific endpoint if endpoint_id is given
+
+ :param group_id: the name of one existing group (ie generic, ...)
+ :param endpoint_id: the name of one existing component (ie Logs, Status, ...)
+ :return: {
+ "group_name": {
+ "endpoint_name": {
+ "description": "a description",
+ "methods": {
+ "get": "description of the HTTP method"
+ },
+ "urls": ('/api', '/api/', '/api/<string:endpoint_id>')
+ }
+ }
+ """
+ __methods = ("get", "post", "put", "delete", "options", "patch")
+ api_list = filter(lambda x: "__" not in x, dir(moon_wrapper.api))
+ api_desc = dict()
+ for api_name in api_list:
+ api_desc[api_name] = {}
+ group_api_obj = eval("moon_interface.api.{}".format(api_name))
+ api_desc[api_name]["description"] = group_api_obj.__doc__
+ 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):
+ 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))
+ 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))
+ return {"error": "Unknown endpoint_id {}".format(endpoint_id)}
+ return {group_id: api_desc[group_id]}
+ return api_desc
diff --git a/old/moon_wrapper/moon_wrapper/api/oslowrapper.py b/old/moon_wrapper/moon_wrapper/api/oslowrapper.py
new file mode 100644
index 00000000..39128621
--- /dev/null
+++ b/old/moon_wrapper/moon_wrapper/api/oslowrapper.py
@@ -0,0 +1,127 @@
+# 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 OsloWrapper(Resource):
+ """
+ Endpoint for authz requests
+ """
+
+ __urls__ = (
+ "/authz/oslo",
+ "/authz/oslo/",
+ )
+
+ def __init__(self, **kwargs):
+ self.port = kwargs.get("port")
+ self.CACHE = kwargs.get("cache", {})
+ self.TIMEOUT = 5
+
+ def post(self):
+ 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 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
+
+ @staticmethod
+ def __get_subject(target, credentials):
+ _subject = target.get("user_id", "")
+ if not _subject:
+ _subject = credentials.get("user_id", "none")
+ return _subject
+
+ @staticmethod
+ def __get_object(target, credentials):
+ try:
+ # note: case of Glance
+ return target['target']['name']
+ except KeyError:
+ pass
+
+ # note: default case
+ return "none"
+
+ @staticmethod
+ def __get_project_id(target, credentials):
+ 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))
+ for containers in self.CACHE.containers.values():
+ LOGGER.info("containers {}".format(containers))
+ for container in containers:
+ if container.get("keystone_project_id") == project_id:
+ if "pipeline" in container['name']:
+ return "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():
+ for container in containers:
+ if container.get("keystone_project_id") == project_id:
+ if "pipeline" in container['name']:
+ return "http://{}:{}".format(
+ container['name'],
+ container['port'])
+ raise exceptions.AuthzException("Keystone Project "
+ "ID ({}) is unknown or not mapped "
+ "to a PDP.".format(project_id))
+
+ def manage_data(self):
+ data = request.form
+ if not dict(request.form):
+ data = json.loads(request.data.decode("utf-8"))
+ target = json.loads(data.get('target', {}))
+ credentials = json.loads(data.get('credentials', {}))
+ 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))
+ req = requests.get("{}/authz/{}/{}/{}/{}".format(
+ interface_url,
+ _pdp_id,
+ _subject,
+ _object,
+ _action
+ ))
+
+ 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")
diff --git a/old/moon_wrapper/moon_wrapper/api/slaveupdate.py b/old/moon_wrapper/moon_wrapper/api/slaveupdate.py
new file mode 100644
index 00000000..b2ce22f0
--- /dev/null
+++ b/old/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/old/moon_wrapper/moon_wrapper/http_server.py b/old/moon_wrapper/moon_wrapper/http_server.py
new file mode 100644
index 00000000..015bb285
--- /dev/null
+++ b/old/moon_wrapper/moon_wrapper/http_server.py
@@ -0,0 +1,144 @@
+# 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'.
+
+import flask
+from flask import Flask, jsonify
+from flask_restful import Resource, Api
+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")
+
+CACHE = Cache()
+
+__API__ = (
+ Status, Logs, API
+)
+
+
+class Server:
+ """Base class for HTTP server"""
+
+ def __init__(self, host="localhost", port=80, api=None, **kwargs):
+ """Run a server
+
+ :param host: hostname of the server
+ :param port: port for the running server
+ :param kwargs: optional parameters
+ :return: a running server
+ """
+ self._host = host
+ self._port = port
+ self._api = api
+ self._extra = kwargs
+
+ @property
+ def host(self):
+ return self._host
+
+ @host.setter
+ def host(self, name):
+ self._host = name
+
+ @host.deleter
+ def host(self):
+ self._host = ""
+
+ @property
+ def port(self):
+ return self._port
+
+ @port.setter
+ def port(self, number):
+ self._port = number
+
+ @port.deleter
+ def port(self):
+ self._port = 80
+
+ def run(self):
+ raise NotImplementedError()
+
+
+class Root(Resource):
+ """
+ The root of the web service
+ """
+ __urls__ = ("/",)
+ __methods = ("get", "post", "put", "delete", "options")
+
+ def get(self):
+ tree = {"/": {"methods": ("get",),
+ "description": "List all methods for that service."}}
+ for item in __API__:
+ tree[item.__name__] = {"urls": item.__urls__}
+ _methods = []
+ for _method in self.__methods:
+ if _method in dir(item):
+ _methods.append(_method)
+ tree[item.__name__]["methods"] = _methods
+ tree[item.__name__]["description"] = item.__doc__.strip()
+ return {
+ "version": __version__,
+ "tree": tree
+ }
+
+
+class HTTPServer(Server):
+
+ def __init__(self, host="localhost", port=80, **kwargs):
+ super(HTTPServer, self).__init__(host=host, port=port, **kwargs)
+ self.app = Flask(__name__)
+ self.port = port
+ conf = configuration.get_configuration("components/orchestrator")
+ _hostname = conf["components/orchestrator"].get("hostname",
+ "orchestrator")
+ _port = conf["components/orchestrator"].get("port", 80)
+ _protocol = conf["components/orchestrator"].get("protocol", "http")
+ self.orchestrator_url = "{}://{}:{}".format(
+ _protocol, _hostname, _port)
+ # Todo : specify only few urls instead of *
+ # CORS(self.app)
+ self.api = Api(self.app)
+ self.__set_route()
+ self.__hook_errors()
+
+ def __hook_errors(self):
+ def get_404_json(e):
+ return flask.make_response("False")
+
+ self.app.register_error_handler(404, get_404_json)
+
+ def get_400_json(e):
+ return flask.make_response("False")
+
+ self.app.register_error_handler(400, lambda e: get_400_json)
+ self.app.register_error_handler(403, exceptions.AuthException)
+
+ def __set_route(self):
+ self.api.add_resource(Root, '/')
+
+ for api in __API__:
+ self.api.add_resource(api, *api.__urls__)
+ self.api.add_resource(OsloWrapper, *OsloWrapper.__urls__,
+ resource_class_kwargs={
+ "orchestrator_url": self.orchestrator_url,
+ "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/old/moon_wrapper/moon_wrapper/server.py b/old/moon_wrapper/moon_wrapper/server.py
new file mode 100644
index 00000000..77def174
--- /dev/null
+++ b/old/moon_wrapper/moon_wrapper/server.py
@@ -0,0 +1,32 @@
+# 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'.
+
+import logging
+from python_moonutilities import configuration, exceptions
+from moon_wrapper.http_server import HTTPServer
+
+LOG = logging.getLogger("moon.wrapper.server")
+
+
+def main():
+ configuration.init_logging()
+ try:
+ conf = configuration.get_configuration("components/wrapper")
+ LOG.debug("wrapper.conf={}".format(conf))
+ hostname = conf["components/wrapper"].get("hostname", "wrapper")
+ port = conf["components/wrapper"].get("port", 80)
+ bind = conf["components/wrapper"].get("bind", "127.0.0.1")
+ except exceptions.ConsulComponentNotFound:
+ hostname = "wrapper"
+ bind = "127.0.0.1"
+ port = 80
+ configuration.add_component(uuid="wrapper", name=hostname, port=port, bind=bind)
+ LOG.info("Starting server with IP {} on port {} bind to {}".format(hostname, port, bind))
+ return HTTPServer(host=bind, port=port)
+
+
+if __name__ == '__main__':
+ SERVER = main()
+ SERVER.run()