Esempio n. 1
0
def test_get_plugins_failure(no_requests):
    from python_moonutilities import configuration
    no_requests.register_uri(
        'GET',
        'http://consul:8500/v1/kv/components/pipeline',
        json=[{
            'Key':
            'components/pipeline',
            'Value':
            'eyJjb250YWluZXIiOiAid3Vrb25nc3VuL21vb25fYXV0aHo6djQuMyIsICJwb3J0IjogODA4MX0='
        }])
    with pytest.raises(Exception) as exception_info:
        configuration.get_plugins()
    assert str(exception_info.value) == '500: Consul Content error'
Esempio n. 2
0
def create_server():
    configuration.init_logging()

    component_id = os.getenv("UUID")
    component_type = os.getenv("TYPE")
    tcp_port = os.getenv("PORT")
    pdp_id = os.getenv("PDP_ID")
    meta_rule_id = os.getenv("META_RULE_ID")
    keystone_project_id = os.getenv("KEYSTONE_PROJECT_ID")
    logger.info("component_type={}".format(component_type))
    conf = configuration.get_plugins()
    # conf = configuration.get_configuration("plugins/{}".format(component_type))
    # conf["plugins/{}".format(component_type)]['id'] = component_id
    if component_type not in conf:
        raise exceptions.ConsulComponentNotFound(
            "{} not found".format(component_type))
    hostname = conf[component_type].get('hostname', component_id)
    port = conf[component_type].get('port', tcp_port)
    bind = conf[component_type].get('bind', "0.0.0.0")

    logger.info("Starting server with IP {} on port {} bind to {}".format(
        hostname, port, bind))
    server = Server(host=bind,
                    port=int(port),
                    component_data={
                        'component_id': component_id,
                        'component_type': component_type,
                        'pdp_id': pdp_id,
                        'meta_rule_id': meta_rule_id,
                        'keystone_project_id': keystone_project_id,
                    })
    return server
Esempio n. 3
0
def test_get_plugins_success():
    from python_moonutilities import configuration
    plugin = configuration.get_plugins()
    assert plugin is not None
Esempio n. 4
0
    def create_pipeline(self, keystone_project_id,
                        pdp_id, policy_ids, manager_data=None,
                        active_context=None,
                        slave_name=None):
        """ Create security functions

        :param keystone_project_id: the Keystone project id
        :param pdp_id: the PDP ID mapped to this pipeline
        :param policy_ids: the policy IDs mapped to this pipeline
        :param manager_data: data needed to create pods
        :param active_context: if present, add the security function in this
                               context
        :param slave_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
        """
        if not manager_data:
            manager_data = dict()
        for key, value in self.get_pods().items():
            for _pod in value:
                if _pod.get('keystone_project_id') == keystone_project_id:
                    logger.warning("A pod for this Keystone project {} "
                                   "already exists.".format(keystone_project_id))
                    return

        plugins = configuration.get_plugins()
        conf = configuration.get_configuration("components/pipeline")
        # i_hostname = conf["components/pipeline"].get("interface").get("hostname", "interface")
        i_port = conf["components/pipeline"].get("interface").get("port", 80)
        i_container = conf["components/pipeline"].get("interface").get(
            "container",
            "wukongsun/moon_interface:v4.3")
        data = [
            {
                "name": "pipeline-" + get_random_name(),
                "container": i_container,
                "port": i_port,
                'pdp_id': pdp_id,
                'genre': "interface",
                'keystone_project_id': keystone_project_id,
                "namespace": "moon"
            },
        ]
        logger.debug("data={}".format(data))
        #   When policies and models are empty, is it right that it returns 200 ?
        #   Should it return no found policies or models ?
        policies = manager_data.get('policies')
        if not policies:
            logger.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())
        logger.debug("policies={}".format(policies))
        models = manager_data.get('models')
        if not models:
            logger.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())
        logger.debug("models={}".format(models))

        if not policy_ids:
            raise exceptions.PolicyUnknown
        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"
                        })
        logger.debug("data={}".format(data))
        contexts, _active_context = self.get_contexts()
        logger.debug("active_context_name={}".format(slave_name))
        logger.debug("active_context={}".format(active_context))
        if slave_name:
            for _context in contexts:
                if _context["name"] == slave_name:
                    active_context = _context
                    break
        if active_context:
            active_context = _active_context
            _config = config.new_client_from_config(
                context=active_context['name'])
            logger.debug("_config={}".format(_config))
            api_client = client.CoreV1Api(_config)
            ext_client = client.ExtensionsV1beta1Api(_config)
            self.load_deployment_and_service(data, api_client, ext_client, expose=False)
            return
        logger.debug("contexts={}".format(contexts))
        for _ctx in contexts:
            if slave_name and slave_name != _ctx['name']:
                continue
            _config = config.new_client_from_config(context=_ctx['name'])
            logger.debug("_config={}".format(_config))
            api_client = client.CoreV1Api(_config)
            ext_client = client.ExtensionsV1beta1Api(_config)
            self.load_deployment_and_service(data, api_client, ext_client, expose=False)