aboutsummaryrefslogtreecommitdiffstats
path: root/moonv4/moon_orchestrator/moon_orchestrator/http_server.py
blob: e6a5ee578184a81f6529a9dfed079ba82a3ad115 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# 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'.

from flask import Flask, jsonify
from flask_cors import CORS, cross_origin
from flask_restful import Resource, Api
import logging
from kubernetes import client, config
import random
import requests
import time
from moon_orchestrator import __version__
from moon_orchestrator.api.pods import Pods
from moon_orchestrator.api.generic import Logs, Status
from python_moonutilities import configuration, exceptions
from python_moonutilities.misc import get_random_name
from moon_orchestrator.drivers import get_driver

LOG = logging.getLogger("moon.orchestrator.http")


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()

__API__ = (
    Status, Logs
 )


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__)
        conf = configuration.get_configuration("components/orchestrator")
        self.orchestrator_hostname = conf["components/orchestrator"].get("hostname", "orchestrator")
        self.orchestrator_port = conf["components/orchestrator"].get("port", 80)
        conf = configuration.get_configuration("components/manager")
        self.manager_hostname = conf["components/manager"].get("hostname", "manager")
        self.manager_port = conf["components/manager"].get("port", 80)
        # TODO : specify only few urls instead of *
        # CORS(self.app)
        self.api = Api(self.app)
        self.driver = get_driver()
        LOG.info("Driver = {}".format(self.driver.__class__))
        self.__set_route()
        self.__hook_errors()
        pdp = None
        while True:
            try:
                pdp = requests.get(
                    "http://{}:{}/pdp".format(self.manager_hostname,
                                              self.manager_port))
            except requests.exceptions.ConnectionError:
                LOG.warning("Manager is not ready, standby...")
                time.sleep(1)
            except KeyError:
                LOG.warning("Manager is not ready, standby...")
                time.sleep(1)
            else:
                if "pdps" in pdp.json():
                    break
        LOG.debug("pdp={}".format(pdp))
        self.create_wrappers()
        for _pdp_key, _pdp_value in pdp.json()['pdps'].items():
            if _pdp_value.get('keystone_project_id'):
                # TODO: select context to add security function
                self.create_security_function(
                    keystone_project_id=_pdp_value.get('keystone_project_id'),
                    pdp_id=_pdp_key,
                    policy_ids=_pdp_value.get('security_pipeline', []))

    def __hook_errors(self):

        def get_404_json(e):
            return jsonify({"result": False, "code": 404, "description": str(e)}), 404
        self.app.register_error_handler(404, get_404_json)

        def get_400_json(e):
            return jsonify({"result": False, "code": 400, "description": str(e)}), 400
        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(Pods, *Pods.__urls__,
                              resource_class_kwargs={
                                  "driver": self.driver,
                                  "create_security_function_hook":
                                      self.create_security_function,
                              })

    def run(self):
        self.app.run(host=self._host, port=self._port)  # nosec

    @staticmethod
    def __filter_str(data):
        return data.replace("@", "-")

    def create_wrappers(self):
        contexts, active_context = self.driver.get_slaves()
        LOG.debug("contexts: {}".format(contexts))
        LOG.debug("active_context: {}".format(active_context))
        conf = configuration.get_configuration("components/wrapper")
        hostname = conf["components/wrapper"].get(
            "hostname", "wrapper")
        port = conf["components/wrapper"].get("port", 80)
        container = conf["components/wrapper"].get(
            "container",
            "wukongsun/moon_wrapper:v4.3")
        for _ctx in contexts:
            _config = config.new_client_from_config(context=_ctx['name'])
            LOG.debug("_config={}".format(_config))
            api_client = client.CoreV1Api(_config)
            ext_client = client.ExtensionsV1beta1Api(_config)
            # TODO: get data from consul
            data = [{
                "name": hostname + "-" + get_random_name(),
                "container": container,
                "port": port,
                "namespace": "moon"
            }, ]
            pod = self.driver.load_pod(data, api_client, ext_client, expose=True)
            LOG.debug('wrapper pod={}'.format(pod))

    def create_security_function(self, keystone_project_id,
                                 pdp_id, policy_ids, manager_data={},
                                 active_context=None,
                                 active_context_name=None):
        """ Create security functions

        :param policy_id: the policy ID mapped to this security function
        :param active_context: if present, add the security function in this
                               context
        :param active_context_name: if present,  add the security function in
                                    this context name
        if active_context_name and active_context are not present, add the
        security function in all context (ie, in all slaves)
        :return: None
        """
        for key, value in self.driver.get_pods().items():
            for _pod in value:
                if _pod.get('keystone_project_id') == keystone_project_id:
                    LOG.warning("A pod for this Keystone project {} "
                                "already exists.".format(keystone_project_id))
                    return

        plugins = configuration.get_plugins()
        conf = configuration.get_configuration("components/interface")
        i_hostname = conf["components/interface"].get("hostname", "interface")
        i_port = conf["components/interface"].get("port", 80)
        i_container = conf["components/interface"].get(
            "container",
            "wukongsun/moon_interface:v4.3")
        data = [
            {
                "name": i_hostname + "-" + get_random_name(),
                "container": i_container,
                "port": i_port,
                'pdp_id': pdp_id,
                'genre': "interface",
                'keystone_project_id': keystone_project_id,
                "namespace": "moon"
            },
        ]
        LOG.info("data={}".format(data))
        policies = manager_data.get('policies')
        if not policies:
            LOG.info("No policy data from Manager, trying to get them")
            policies = requests.get("http://{}:{}/policies".format(
                self.manager_hostname, self.manager_port)).json().get(
                "policies", dict())
        LOG.info("policies={}".format(policies))
        models = manager_data.get('models')
        if not models:
            LOG.info("No models data from Manager, trying to get them")
            models = requests.get("http://{}:{}/models".format(
                self.manager_hostname, self.manager_port)).json().get(
                "models", dict())
        LOG.info("models={}".format(models))

        for policy_id in policy_ids:
            if policy_id in policies:
                genre = policies[policy_id].get("genre", "authz")
                if genre in plugins:
                    for meta_rule in models[policies[policy_id]['model_id']]['meta_rules']:
                        data.append({
                            "name": genre + "-" + get_random_name(),
                            "container": plugins[genre]['container'],
                            'pdp_id': pdp_id,
                            "port": plugins[genre].get('port', 8080),
                            'genre': genre,
                            'policy_id': policy_id,
                            'meta_rule_id': meta_rule,
                            'keystone_project_id': keystone_project_id,
                            "namespace": "moon"
                        })
        LOG.info("data={}".format(data))
        contexts, _active_context = self.driver.get_slaves()
        LOG.info("active_context_name={}".format(active_context_name))
        LOG.info("active_context={}".format(active_context))
        if active_context_name:
            for _context in contexts:
                if _context["name"] == active_context_name:
                    active_context = _context
                    break
        if active_context:
            active_context = _active_context
            _config = config.new_client_from_config(
                context=active_context['name'])
            LOG.debug("_config={}".format(_config))
            api_client = client.CoreV1Api(_config)
            ext_client = client.ExtensionsV1beta1Api(_config)
            self.driver.load_pod(data, api_client, ext_client, expose=False)
            return
        LOG.info("contexts={}".format(contexts))
        for _ctx in contexts:
            _config = config.new_client_from_config(context=_ctx['name'])
            LOG.debug("_config={}".format(_config))
            api_client = client.CoreV1Api(_config)
            ext_client = client.ExtensionsV1beta1Api(_config)
            self.driver.load_pod(data, api_client, ext_client, expose=False)