aboutsummaryrefslogtreecommitdiffstats
path: root/moon_manager
diff options
context:
space:
mode:
Diffstat (limited to 'moon_manager')
-rw-r--r--moon_manager/Changelog30
-rw-r--r--moon_manager/moon_manager/__init__.py2
-rw-r--r--moon_manager/moon_manager/api/json_export.py25
-rw-r--r--moon_manager/moon_manager/api/json_import.py58
-rw-r--r--moon_manager/moon_manager/api/pdp.py2
-rw-r--r--moon_manager/moon_manager/api/rules.py4
-rw-r--r--moon_manager/moon_manager/http_server.py35
-rw-r--r--moon_manager/tests/functional_pod/json/mls.json16
-rw-r--r--moon_manager/tests/functional_pod/json/rbac.json16
-rw-r--r--moon_manager/tests/unit_python/api/import_export_utilities.py24
-rw-r--r--moon_manager/tests/unit_python/api/test_data.py17
-rw-r--r--moon_manager/tests/unit_python/api/test_export.py24
-rw-r--r--moon_manager/tests/unit_python/api/test_import.py45
13 files changed, 189 insertions, 109 deletions
diff --git a/moon_manager/Changelog b/moon_manager/Changelog
new file mode 100644
index 00000000..2bd01595
--- /dev/null
+++ b/moon_manager/Changelog
@@ -0,0 +1,30 @@
+# Copyright 2018 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'.
+
+
+CHANGES
+=======
+
+1.0.0
+-----
+- First version of the manager
+
+2.0.0
+-----
+- Version built inside the Keystone component
+
+3.0.0
+-----
+- Version built outside the Keystone component
+
+4.0.0
+-----
+- First micro-architecture version
+
+4.5.2
+-----
+- use the threading capability of Flask app
+- set the number of manager to 1
+- update to the latest version of the python-moondb library
diff --git a/moon_manager/moon_manager/__init__.py b/moon_manager/moon_manager/__init__.py
index af7fced5..20a70977 100644
--- a/moon_manager/moon_manager/__init__.py
+++ b/moon_manager/moon_manager/__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.0"
+__version__ = "4.5.2"
diff --git a/moon_manager/moon_manager/api/json_export.py b/moon_manager/moon_manager/api/json_export.py
index feb4fde2..1d3643e7 100644
--- a/moon_manager/moon_manager/api/json_export.py
+++ b/moon_manager/moon_manager/api/json_export.py
@@ -1,3 +1,8 @@
+# Copyright 2018 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 flask_restful import Resource
from python_moonutilities.security_functions import check_auth
@@ -34,11 +39,19 @@ class JsonExport(Resource):
JsonUtils.convert_id_to_name(policy_key, rule_dict, "policy", "policy", PolicyManager, self._user_id)
ids = rule["rule"]
rule_description = dict()
- JsonUtils.convert_ids_to_names([ids[0]], rule_description, "subject_data", "subject_data", PolicyManager, self._user_id, policy_key)
- JsonUtils.convert_ids_to_names([ids[1]], rule_description, "object_data", "object_data", PolicyManager, self._user_id, policy_key)
- JsonUtils.convert_ids_to_names([ids[2]], rule_description, "action_data", "action_data", PolicyManager, self._user_id, policy_key)
+ meta_rule = ModelManager.get_meta_rules(self._user_id, rule["meta_rule_id"])
+ meta_rule = [v for v in meta_rule.values()]
+ meta_rule = meta_rule[0]
+ index_subject_data = len(meta_rule["subject_categories"])-1
+ index_object_data = len(meta_rule["subject_categories"]) + len(meta_rule["object_categories"])-1
+ index_action_data = len(meta_rule["subject_categories"]) + len(meta_rule["object_categories"]) + len(meta_rule["action_categories"])-1
+ ids_subject_data = [ids[0]] if len(meta_rule["subject_categories"]) == 1 else ids[0:index_subject_data]
+ ids_object_data = [ids[index_object_data]] if len(meta_rule["object_categories"]) == 1 else ids[index_subject_data+1:index_object_data]
+ ids_action_date = [ids[index_action_data]] if len(meta_rule["action_categories"]) == 1 else ids[index_object_data+1:index_action_data]
+ JsonUtils.convert_ids_to_names(ids_subject_data, rule_description, "subject_data", "subject_data", PolicyManager, self._user_id, policy_key)
+ JsonUtils.convert_ids_to_names(ids_object_data, rule_description, "object_data", "object_data", PolicyManager, self._user_id, policy_key)
+ JsonUtils.convert_ids_to_names(ids_action_date, rule_description, "action_data", "action_data", PolicyManager, self._user_id, policy_key)
rule_dict["rule"] = rule_description
- logger.info("Exporting rule {}".format(rule_dict))
rules_array.append(rule_dict)
if len(rules_array) > 0:
@@ -95,8 +108,8 @@ class JsonExport(Resource):
JsonUtils.copy_field_if_exists(data_group["data"][data_key], data_dict, "name", str)
JsonUtils.copy_field_if_exists(data_group["data"][data_key], data_dict, "description", str)
else:
- JsonUtils.copy_field_if_exists(data_group["data"][data_key]["value"], data_dict, "name", str)
- JsonUtils.copy_field_if_exists(data_group["data"][data_key]["value"], data_dict, "description", str)
+ JsonUtils.copy_field_if_exists(data_group["data"][data_key], data_dict, "name", str)
+ JsonUtils.copy_field_if_exists(data_group["data"][data_key], data_dict, "description", str)
JsonUtils.convert_id_to_name(policy_id, data_dict, "policy", "policy", PolicyManager, self._user_id)
JsonUtils.convert_id_to_name(category_id, data_dict, "category", type_element + "_category", ModelManager, self._user_id, policy_key)
diff --git a/moon_manager/moon_manager/api/json_import.py b/moon_manager/moon_manager/api/json_import.py
index a048baee..ae9a21d0 100644
--- a/moon_manager/moon_manager/api/json_import.py
+++ b/moon_manager/moon_manager/api/json_import.py
@@ -79,6 +79,23 @@ class JsonImport(Resource):
"/import/",
)
+ def _reorder_rules_ids(self, rule, ordered_perimeter_categories_ids, json_data_ids, policy_id, get_function):
+ ordered_json_ids = [None]*len(ordered_perimeter_categories_ids)
+ logger.info("ordered_json_ids {}".format(ordered_json_ids))
+ logger.info("json_data_ids {}".format(json_data_ids))
+ for json_id in json_data_ids:
+ logger.info("json_id {}".format(json_id))
+ data = get_function(self._user_id, policy_id, data_id=json_id)
+ data = data[0]
+ logger.info("data {}".format(data))
+ if data["category_id"] not in ordered_perimeter_categories_ids:
+ raise InvalidJson("The category id {} of the rule {} does not match the meta rule".format(data["category_id"], rule))
+ if ordered_json_ids[ordered_perimeter_categories_ids.index(data["category_id"])] is not None:
+ raise InvalidJson("The category id {} of the rule {} shall not be used twice in the same rule".format(data["category_id"], rule))
+ ordered_json_ids[ordered_perimeter_categories_ids.index(data["category_id"])] = json_id
+ logger.info(ordered_json_ids)
+ return ordered_json_ids
+
def _import_rules(self, json_rules):
if not isinstance(json_rules, list):
raise InvalidJson("rules shall be a list!")
@@ -91,26 +108,28 @@ class JsonImport(Resource):
json_ids = dict()
JsonUtils.convert_name_to_id(json_rule, json_ids, "policy", "policy_id", "policy", PolicyManager, self._user_id)
JsonUtils.convert_name_to_id(json_rule, json_to_use, "meta_rule", "meta_rule_id", "meta_rule", ModelManager, self._user_id)
-
json_subject_ids = dict()
json_object_ids = dict()
json_action_ids = dict()
- json_rule_to_use = dict()
JsonUtils.convert_names_to_ids(json_rule["rule"], json_subject_ids, "subject_data", "subject", "subject_data", PolicyManager, self._user_id, json_ids["policy_id"])
JsonUtils.convert_names_to_ids(json_rule["rule"], json_object_ids, "object_data", "object", "object_data", PolicyManager, self._user_id, json_ids["policy_id"])
JsonUtils.convert_names_to_ids(json_rule["rule"], json_action_ids, "action_data", "action", "action_data", PolicyManager, self._user_id, json_ids["policy_id"])
- logger.info(json_rule_to_use)
- for json_subject_id in json_subject_ids["subject"]:
- for json_object_id in json_object_ids["object"]:
- for json_action_id in json_action_ids["action"]:
- json_to_use["rule"] = [json_subject_id, json_object_id, json_action_id]
- try:
- logger.info("Adding / updating a rule from json {}".format(json_to_use))
- PolicyManager.add_rule(self._user_id, json_ids["policy_id"], json_to_use["meta_rule_id"], json_to_use)
- except exceptions.RuleExisting:
- pass
- except exceptions.PolicyUnknown:
- raise UnknownPolicy("Unknown policy with id {}".format(json_ids["policy_id"]))
+
+ meta_rule = ModelManager.get_meta_rules(self._user_id, json_to_use["meta_rule_id"])
+ meta_rule = [v for v in meta_rule.values()]
+ meta_rule = meta_rule[0]
+
+ json_to_use_rule = self._reorder_rules_ids(json_rule, meta_rule["subject_categories"], json_subject_ids["subject"], json_ids["policy_id"], PolicyManager.get_subject_data)
+ json_to_use_rule = json_to_use_rule + self._reorder_rules_ids(json_rule, meta_rule["object_categories"], json_object_ids["object"], json_ids["policy_id"], PolicyManager.get_object_data)
+ json_to_use_rule = json_to_use_rule + self._reorder_rules_ids(json_rule, meta_rule["action_categories"], json_action_ids["action"], json_ids["policy_id"], PolicyManager.get_action_data)
+ json_to_use["rule"] = json_to_use_rule
+ try:
+ logger.info("Adding / updating a rule from json {}".format(json_to_use))
+ PolicyManager.add_rule(self._user_id, json_ids["policy_id"], json_to_use["meta_rule_id"], json_to_use)
+ except exceptions.RuleExisting:
+ pass
+ except exceptions.PolicyUnknown:
+ raise UnknownPolicy("Unknown policy with id {}".format(json_ids["policy_id"]))
def _import_meta_rules(self, json_meta_rules):
logger.info("Input meta rules : {}".format(json_meta_rules))
@@ -188,19 +207,20 @@ class JsonImport(Resource):
JsonUtils.copy_field_if_exists(json_item_data, json_to_use, "description", str)
json_policy = dict()
# field_mandatory : not mandatory if there is some mandatory policies
- JsonUtils.convert_name_to_id(json_item_data, json_policy, "policy", "policy_id", "policy",
+ JsonUtils.convert_names_to_ids(json_item_data, json_policy, "policies", "policy_id", "policy",
PolicyManager, self._user_id, field_mandatory=len(mandatory_policy_ids) == 0)
logger.info("json_policy {}".format(json_policy))
json_category = dict()
JsonUtils.convert_name_to_id(json_item_data, json_category, "category", "category_id", type_element+"_category",
ModelManager, self._user_id)
logger.info("json_category {}".format(json_category))
- policy_id = None
+ policy_ids = []
if "policy_id" in json_policy:
- policy_id = json_policy["policy_id"]
+ policy_ids = json_policy["policy_id"]
- if policy_id is not None and policy_id not in mandatory_policy_ids:
- mandatory_policy_ids.append(policy_id)
+ for policy_id in policy_ids:
+ if policy_id is not None and policy_id not in mandatory_policy_ids:
+ mandatory_policy_ids.append(policy_id)
if len(mandatory_policy_ids) == 0:
raise InvalidJson("Invalid data, the policy shall be set when importing {}".format(json_item_data))
diff --git a/moon_manager/moon_manager/api/pdp.py b/moon_manager/moon_manager/api/pdp.py
index 78931e1f..4bc34a24 100644
--- a/moon_manager/moon_manager/api/pdp.py
+++ b/moon_manager/moon_manager/api/pdp.py
@@ -73,7 +73,7 @@ def add_pod(uuid, data):
time.sleep(1)
else:
break
- logger.info(req.text)
+ logger.info("Pod add request answer : {}".format(req.text))
def check_keystone_pid(k_pid):
diff --git a/moon_manager/moon_manager/api/rules.py b/moon_manager/moon_manager/api/rules.py
index e6c46bf4..57dcd45c 100644
--- a/moon_manager/moon_manager/api/rules.py
+++ b/moon_manager/moon_manager/api/rules.py
@@ -40,9 +40,9 @@ class Rules(Resource):
"policy_id": "policy_id1",
"meta_rule_id": "meta_rule_id1",
"rule_id1":
- ["subject_data_id1", "object_data_id1", "action_data_id1"],
+ ["subject_data_id1", "subject_data_id2", "object_data_id1", "action_data_id1"],
"rule_id2":
- ["subject_data_id2", "object_data_id2", "action_data_id2"],
+ ["subject_data_id3", "subject_data_id4", "object_data_id2", "action_data_id2"],
]
}
:internal_api: get_rules
diff --git a/moon_manager/moon_manager/http_server.py b/moon_manager/moon_manager/http_server.py
index 28d77ea0..128d5b74 100644
--- a/moon_manager/moon_manager/http_server.py
+++ b/moon_manager/moon_manager/http_server.py
@@ -2,12 +2,10 @@
# 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 werkzeug.exceptions import HTTPException
from flask import Flask, jsonify, Response, make_response
from flask_cors import CORS, cross_origin
from json import dumps
from flask_restful import Resource, Api
-import flask_restful
import logging
import sqlalchemy.exc
import time
@@ -24,7 +22,6 @@ from moon_manager.api.data import SubjectData, ObjectData, ActionData
from moon_manager.api.assignments import SubjectAssignments, ObjectAssignments, ActionAssignments
from moon_manager.api.rules import Rules
from moon_manager.api.json_import import JsonImport
-from moon_manager.api.base_exception import BaseException
from moon_manager.api.json_export import JsonExport
from python_moonutilities import configuration
from python_moondb.core import PDPManager
@@ -112,17 +109,19 @@ class Root(Resource):
class CustomApi(Api):
- def handle_error(self, e):
+ @staticmethod
+ def handle_error(e):
try:
- error_message = dumps({'message': str(e)})
+ error_message = dumps({'message': str(e), "code": getattr(e, "code", 500)})
logger.error(error_message)
- return make_response(error_message, e.code)
- except Exception as e2: # unhandled exception in the api...
+ return make_response(error_message, getattr(e, "code", 500))
+ except Exception as e2: # unhandled exception in the api...
logger.error(str(e2))
return make_response(error_message, 500)
class HTTPServer(Server):
+
def __init__(self, host="localhost", port=80, **kwargs):
super(HTTPServer, self).__init__(host=host, port=port, **kwargs)
self.app = Flask(__name__)
@@ -135,26 +134,6 @@ class HTTPServer(Server):
CORS(self.app)
self.api = CustomApi(self.app)
self.__set_route()
- # self.__hook_errors()
-
- #def __hook_errors(self):
- # def get_500_json(e):
- # logger.error("get_500_json")
- # return jsonify({"result": False, "code": 500, "description": str(e)}), 500
- # self.app.register_error_handler(JsonUtilsException, get_500_json)
- # self.app.register_error_handler(JsonImportException, get_500_json)
- # self.app.register_error_handler(UnknownName, get_500_json)
-
- # 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(500, lambda e: get_500_json)
- # 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, '/')
@@ -179,4 +158,4 @@ class HTTPServer(Server):
def run(self):
self.__check_if_db_is_up()
- self.app.run(debug=True, host=self._host, port=self._port) # nosec
+ self.app.run(host=self._host, port=self._port, threaded=True) # nosec
diff --git a/moon_manager/tests/functional_pod/json/mls.json b/moon_manager/tests/functional_pod/json/mls.json
index d2a5c67c..01ef6deb 100644
--- a/moon_manager/tests/functional_pod/json/mls.json
+++ b/moon_manager/tests/functional_pod/json/mls.json
@@ -15,9 +15,9 @@
"subject_categories": [{ "name":"subject-security-level", "description": "" }],
- "subject_data": [{ "name":"low", "description": "", "policy": {"name" :"MLS policy example"}, "category": {"name": "subject-security-level"}},
- { "name":"medium", "description": "", "policy": {"name" :"MLS policy example"}, "category": {"name": "subject-security-level"}},
- { "name":"high", "description": "", "policy": {"name" :"MLS policy example"}, "category": {"name": "subject-security-level"}}],
+ "subject_data": [{ "name":"low", "description": "", "policies": [{"name" :"MLS policy example"}], "category": {"name": "subject-security-level"}},
+ { "name":"medium", "description": "", "policies": [{"name" :"MLS policy example"}], "category": {"name": "subject-security-level"}},
+ { "name":"high", "description": "", "policies": [{"name" :"MLS policy example"}], "category": {"name": "subject-security-level"}}],
"subject_assignments":[{ "subject" : {"name": "adminuser"}, "category" : {"name": "subject-security-level"}, "assignments": [{"name" : "high"}]},
{ "subject" : {"name": "user1"}, "category" : {"name": "subject-security-level"}, "assignments": [{"name" : "medium"}] }],
@@ -32,9 +32,9 @@
"object_categories": [{"name":"object-security-level", "description": ""}],
- "object_data": [{ "name":"low", "description": "", "policy": {"name" :"MLS policy example"}, "category": {"name": "object-security-level"}},
- { "name":"medium", "description": "", "policy": {"name" :"MLS policy example"}, "category": {"name": "object-security-level"}},
- { "name":"high", "description": "", "policy": {"name" :"MLS policy example"}, "category": {"name": "object-security-level"}}],
+ "object_data": [{ "name":"low", "description": "", "policies": [{"name" :"MLS policy example"}], "category": {"name": "object-security-level"}},
+ { "name":"medium", "description": "", "policies": [{"name" :"MLS policy example"}], "category": {"name": "object-security-level"}},
+ { "name":"high", "description": "", "policies": [{"name" :"MLS policy example"}], "category": {"name": "object-security-level"}}],
"object_assignments":[{ "object" : {"name": "vm0"}, "category" : {"name": "object-security-level"}, "assignments": [{"name" : "medium"}]},
{ "object" : {"name": "vm1"}, "category" : {"name": "object-security-level"}, "assignments": [{"name" : "low"}]}],
@@ -49,8 +49,8 @@
"action_categories": [{"name":"action-type", "description": ""}],
- "action_data": [{"name":"vm-action", "description": "", "policy": {"name": "MLS policy example"}, "category": {"name": "action-type"}},
- {"name":"storage-action", "description": "", "policy": {"name" :"MLS policy example"}, "category": {"name": "action-type"}}],
+ "action_data": [{"name":"vm-action", "description": "", "policies": [{"name": "MLS policy example"}], "category": {"name": "action-type"}},
+ {"name":"storage-action", "description": "", "policies": [{"name" :"MLS policy example"}], "category": {"name": "action-type"}}],
"action_assignments":[{ "action" : {"name": "start"}, "category" : {"name": "action-type"}, "assignments": [{"name" : "vm-action"}]},
{ "action" : {"name": "stop"}, "category" : {"name": "action-type"}, "assignments": [{"name" : "vm-action"}]}],
diff --git a/moon_manager/tests/functional_pod/json/rbac.json b/moon_manager/tests/functional_pod/json/rbac.json
index eddbb654..a75f291b 100644
--- a/moon_manager/tests/functional_pod/json/rbac.json
+++ b/moon_manager/tests/functional_pod/json/rbac.json
@@ -15,9 +15,9 @@
"subject_categories": [{ "name":"role", "description": "" }],
- "subject_data": [{ "name":"admin", "description": "", "policy": {"name" :"RBAC policy example"}, "category": {"name": "role"}},
- { "name":"employee", "description": "", "policy": {"name" :"RBAC policy example"}, "category": {"name": "role"}},
- { "name":"*", "description": "", "policy": {"name" :"RBAC policy example"}, "category": {"name": "role"}}],
+ "subject_data": [{ "name":"admin", "description": "", "policies": [{"name" :"RBAC policy example"}], "category": {"name": "role"}},
+ { "name":"employee", "description": "", "policies": [{"name" :"RBAC policy example"}], "category": {"name": "role"}},
+ { "name":"*", "description": "", "policies": [{"name" :"RBAC policy example"}], "category": {"name": "role"}}],
"subject_assignments":[{ "subject" : {"name": "adminuser"}, "category" : {"name": "role"}, "assignments": [{"name" : "admin"}, {"name" : "employee"}, {"name" : "*"}]},
{ "subject" : {"name": "user1"}, "category" : {"name": "role"}, "assignments": [{"name" : "employee"}, {"name" : "*"}] }],
@@ -32,9 +32,9 @@
"object_categories": [{"name":"id", "description": ""}],
- "object_data": [{ "name":"vm0", "description": "", "policy": {"name" :"RBAC policy example"}, "category": {"name": "id"}},
- { "name":"vm1", "description": "", "policy": {"name" :"RBAC policy example"}, "category": {"name": "id"}},
- { "name":"*", "description": "", "policy": {"name" :"RBAC policy example"}, "category": {"name": "id"}}],
+ "object_data": [{ "name":"vm0", "description": "", "policies": [{"name" :"RBAC policy example"}], "category": {"name": "id"}},
+ { "name":"vm1", "description": "", "policies": [{"name" :"RBAC policy example"}], "category": {"name": "id"}},
+ { "name":"*", "description": "", "policies": [{"name" :"RBAC policy example"}], "category": {"name": "id"}}],
"object_assignments":[{ "object" : {"name": "vm0"}, "category" : {"name": "id"}, "assignments": [{"name" : "vm0"}, {"name" : "*"}]},
{ "object" : {"name": "vm1"}, "category" : {"name": "id"}, "assignments": [{"name" : "vm1"}, {"name" : "*"}]}],
@@ -49,8 +49,8 @@
"action_categories": [{"name":"action-type", "description": ""}],
- "action_data": [{"name":"vm-action", "description": "", "policy": {"name": "RBAC policy example"}, "category": {"name": "action-type"}},
- {"name":"*", "description": "", "policy": {"name" :"RBAC policy example"}, "category": {"name": "action-type"}}],
+ "action_data": [{"name":"vm-action", "description": "", "policies": [{"name": "RBAC policy example"}], "category": {"name": "action-type"}},
+ {"name":"*", "description": "", "policies": [{"name" :"RBAC policy example"}], "category": {"name": "action-type"}}],
"action_assignments":[{ "action" : {"name": "start"}, "category" : {"name": "action-type"}, "assignments": [{"name" : "vm-action"}, {"name" : "*"}]},
{ "action" : {"name": "stop"}, "category" : {"name": "action-type"}, "assignments": [{"name" : "vm-action"}, {"name" : "*"}]}],
diff --git a/moon_manager/tests/unit_python/api/import_export_utilities.py b/moon_manager/tests/unit_python/api/import_export_utilities.py
index 15c3e333..98586d02 100644
--- a/moon_manager/tests/unit_python/api/import_export_utilities.py
+++ b/moon_manager/tests/unit_python/api/import_export_utilities.py
@@ -1,3 +1,8 @@
+# Copyright 2018 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 api.utilities as utilities
import api.test_models as test_models
import api.test_policies as test_policies
@@ -7,6 +12,10 @@ import api.test_data as test_data
import api.meta_rules_test as test_meta_rules
import api.test_assignemnt as test_assignments
import api.test_rules as test_rules
+import logging
+
+logger = logging.getLogger("moon.manager.test.api." + __name__)
+
def clean_models(client):
req, models = test_models.get_models(client)
@@ -25,9 +34,11 @@ def clean_policies(client):
def clean_subjects(client):
subjects = test_perimeter.get_subjects(client)
+ logger.info("subjects {}".format(subjects))
for key in subjects["subjects"]:
subject = subjects["subjects"][key]
policy_keys = subject["policy_list"]
+ logger.info("subjects policy_keys {}".format(policy_keys))
for policy_key in policy_keys:
client.delete("/policies/{}/subjects/{}".format(policy_key,key))
client.delete("/subjects/{}".format(key))
@@ -36,9 +47,11 @@ def clean_subjects(client):
def clean_objects(client):
objects = test_perimeter.get_objects(client)
+ logger.info("objects {}".format(objects))
for key in objects["objects"]:
object_ = objects["objects"][key]
policy_keys = object_["policy_list"]
+ logger.info("objects policy_keys {}".format(policy_keys))
for policy_key in policy_keys:
print("/policies/{}/objects/{}".format(policy_key, key))
req = client.delete("/policies/{}/objects/{}".format(policy_key, key))
@@ -48,9 +61,11 @@ def clean_objects(client):
def clean_actions(client):
actions = test_perimeter.get_actions(client)
+ logger.info("objects {}".format(actions))
for key in actions["actions"]:
action = actions["actions"][key]
policy_keys = action["policy_list"]
+ logger.info("action policy_keys {}".format(policy_keys))
for policy_key in policy_keys:
client.delete("/policies/{}/actions/{}".format(policy_key, key))
client.delete("/actions/{}".format(key))
@@ -59,19 +74,21 @@ def clean_actions(client):
def clean_subject_categories(client):
req, categories = test_categories.get_subject_categories(client)
- print(categories)
+ logger.info(categories)
for key in categories["subject_categories"]:
client.delete("/subject_categories/{}".format(key))
def clean_object_categories(client):
req, categories = test_categories.get_object_categories(client)
+ logger.info(categories)
for key in categories["object_categories"]:
client.delete("/object_categories/{}".format(key))
def clean_action_categories(client):
req, categories = test_categories.get_action_categories(client)
+ logger.info(categories)
for key in categories["action_categories"]:
client.delete("/action_categories/{}".format(key))
@@ -174,8 +191,9 @@ def clean_all(client):
clean_object_data(client)
clean_action_data(client)
- clean_policies(client)
- clean_models(client)
clean_actions(client)
clean_objects(client)
clean_subjects(client)
+
+ clean_policies(client)
+ clean_models(client) \ No newline at end of file
diff --git a/moon_manager/tests/unit_python/api/test_data.py b/moon_manager/tests/unit_python/api/test_data.py
index f636aaa5..724f919f 100644
--- a/moon_manager/tests/unit_python/api/test_data.py
+++ b/moon_manager/tests/unit_python/api/test_data.py
@@ -1,3 +1,8 @@
+# Copyright 2018 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 api.utilities as utilities
import json
@@ -106,8 +111,12 @@ def test_add_object_data():
value = object_data["object_data"]['data']
assert "object_data" in object_data
id = list(value.keys())[0]
- assert value[id]['value']['name'] == "testuser"
- assert value[id]['value']['description'] == "description of {}".format("testuser")
+ print("-----------------------")
+ print(id)
+ print(value[id])
+ print("-----------------------")
+ assert value[id]['name'] == "testuser"
+ assert value[id]['description'] == "description of {}".format("testuser")
def test_delete_object_data():
@@ -164,8 +173,8 @@ def test_add_action_data():
value = action_data["action_data"]['data']
assert "action_data" in action_data
id = list(value.keys())[0]
- assert value[id]['value']['name'] == "testuser"
- assert value[id]['value']['description'] == "description of {}".format("testuser")
+ assert value[id]['name'] == "testuser"
+ assert value[id]['description'] == "description of {}".format("testuser")
def test_delete_action_data():
diff --git a/moon_manager/tests/unit_python/api/test_export.py b/moon_manager/tests/unit_python/api/test_export.py
index 25097180..122ab927 100644
--- a/moon_manager/tests/unit_python/api/test_export.py
+++ b/moon_manager/tests/unit_python/api/test_export.py
@@ -1,3 +1,8 @@
+# Copyright 2018 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 json
import api.utilities as utilities
import api.import_export_utilities as import_export_utilities
@@ -24,9 +29,9 @@ SUBJECT_OBJECT_ACTION_DATA = {"models": [{"name": "test model", "description": "
"subject_categories": [{"name": "test subject categories", "description": "subject category description"}],
"object_categories": [{"name": "test object categories", "description": "object category description"}],
"action_categories": [{"name": "test action categories", "description": "action category description"}],
- "subject_data": [{"name": "test subject data", "description": "subject data description", "policy": {"name": "test policy"}, "category": {"name": "test subject categories"}}],
- "object_data": [{"name": "test object data", "description": "object data description", "policy": {"name": "test policy"}, "category": {"name": "test object categories"}}],
- "action_data": [{"name": "test action data", "description": "action data description", "policy": {"name": "test policy"}, "category": {"name": "test action categories"}}],
+ "subject_data": [{"name": "test subject data", "description": "subject data description", "policies": [{"name": "test policy"}], "category": {"name": "test subject categories"}}],
+ "object_data": [{"name": "test object data", "description": "object data description", "policies": [{"name": "test policy"}], "category": {"name": "test object categories"}}],
+ "action_data": [{"name": "test action data", "description": "action data description", "policies": [{"name": "test policy"}], "category": {"name": "test action categories"}}],
"meta_rules": [{"name": "meta rule", "description": "valid meta rule", "subject_categories": [{"name": "test subject categories"}], "object_categories": [{"name": "test object categories"}], "action_categories": [{"name": "test action categories"}]}]}
@@ -41,9 +46,9 @@ ASSIGNMENTS = {"models": [{"name": "test model", "description": "", "meta_rules"
"subject_categories": [{"name": "test subject categories", "description": "subject category description"}],
"object_categories": [{"name": "test object categories", "description": "object category description"}],
"action_categories": [{"name": "test action categories", "description": "action category description"}],
- "subject_data": [{"name": "test subject data", "description": "subject data description", "policy": {"name": "test policy"}, "category": {"name": "test subject categories"}}],
- "object_data": [{"name": "test object data", "description": "object data description", "policy": {"name": "test policy"}, "category": {"name": "test object categories"}}],
- "action_data": [{"name": "test action data", "description": "action data description", "policy": {"name": "test policy"}, "category": {"name": "test action categories"}}],
+ "subject_data": [{"name": "test subject data", "description": "subject data description", "policies": [{"name": "test policy"}], "category": {"name": "test subject categories"}}],
+ "object_data": [{"name": "test object data", "description": "object data description", "policies": [{"name": "test policy"}], "category": {"name": "test object categories"}}],
+ "action_data": [{"name": "test action data", "description": "action data description", "policies": [{"name": "test policy"}], "category": {"name": "test action categories"}}],
"meta_rules": [{"name": "meta rule", "description": "valid meta rule", "subject_categories": [{"name": "test subject categories"}], "object_categories": [{"name": "test object categories"}], "action_categories": [{"name": "test action categories"}]}],
"subjects": [{"name": "testuser", "description": "description of the subject", "extra": {"field_extra_subject": "value extra subject"}, "policies": [{"name": "test policy"}]}],
"objects": [{"name": "test object", "description": "description of the object", "extra": {"field_extra_object": "value extra object"}, "policies": [{"name": "test policy"}]}],
@@ -57,9 +62,9 @@ RULES = {"models": [{"name": "test model", "description": "", "meta_rules": [{"n
"subject_categories": [{"name": "test subject categories", "description": "subject category description"}],
"object_categories": [{"name": "test object categories", "description": "object category description"}],
"action_categories": [{"name": "test action categories", "description": "action category description"}],
- "subject_data": [{"name": "test subject data", "description": "subject data description", "policy": {"name": "test policy"}, "category": {"name": "test subject categories"}}],
- "object_data": [{"name": "test object data", "description": "object data description", "policy": {"name": "test policy"}, "category": {"name": "test object categories"}}],
- "action_data": [{"name": "test action data", "description": "action data description", "policy": {"name": "test policy"}, "category": {"name": "test action categories"}}],
+ "subject_data": [{"name": "test subject data", "description": "subject data description", "policies": [{"name": "test policy"}], "category": {"name": "test subject categories"}}],
+ "object_data": [{"name": "test object data", "description": "object data description", "policies": [{"name": "test policy"}], "category": {"name": "test object categories"}}],
+ "action_data": [{"name": "test action data", "description": "action data description", "policies": [{"name": "test policy"}], "category": {"name": "test action categories"}}],
"meta_rules": [{"name": "meta rule", "description": "valid meta rule", "subject_categories": [{"name": "test subject categories"}], "object_categories": [{"name": "test object categories"}], "action_categories": [{"name": "test action categories"}]}],
"subjects": [{"name": "testuser", "description": "description of the subject", "extra": {"field_extra_subject": "value extra subject"}, "policies": [{"name": "test policy"}]}],
"objects": [{"name": "test object", "description": "description of the object", "extra": {"field_extra_object": "value extra object"}, "policies": [{"name": "test policy"}]}],
@@ -169,7 +174,6 @@ def test_export_subject_object_action_categories():
req = client.get("/export")
assert req.status_code == 200
data = utilities.get_json(req.data)
- print(data)
assert "content" in data
type_elements = ["subject", "object", "action"]
for type_element in type_elements:
diff --git a/moon_manager/tests/unit_python/api/test_import.py b/moon_manager/tests/unit_python/api/test_import.py
index ef2267ed..4e970a0e 100644
--- a/moon_manager/tests/unit_python/api/test_import.py
+++ b/moon_manager/tests/unit_python/api/test_import.py
@@ -1,3 +1,8 @@
+# Copyright 2018 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 api.utilities as utilities
import api.test_models as test_models
import api.test_policies as test_policies
@@ -66,24 +71,24 @@ PRE_DATA = {"models": [{"name": "test model", "description": "", "meta_rules": [
"meta_rules": [{"name": "good meta rule", "description": "valid meta rule", "subject_categories": [{"name": "test subject categories"}], "object_categories": [{"name": "test object categories"}], "action_categories": [{"name": "test action categories"}]},
{"name": "other good meta rule", "description": "valid meta rule", "subject_categories": [{"name": "other test subject categories"}], "object_categories": [{"name": "other test object categories"}], "action_categories": [{"name": "other test action categories"}]}]}
-SUBJECT_DATA = [{"subject_data": [{"name": "not valid subject data", "description": "", "policy": {}, "category": {}}]},
- {"subject_data": [{"name": "not valid subject data", "description": "", "policy": {}, "category": {"name": "test subject categories"}}]},
- {"policies": [{"name": "test policy", "genre": "authz", "description": "description", "model": {"name": "test model"}, "mandatory": True}], "subject_data": [{"name": "one valid subject data", "description": "description", "policy": {}, "category": {"name": "test subject categories"}}]},
- {"subject_data": [{"name": "valid subject data", "description": "description", "policy": {"name": "test policy"}, "category": {"name": "test subject categories"}}]},
- {"subject_data": [{"name": "valid subject data", "description": "new description", "policy": {"name": "test other policy"}, "category": {"name": "test subject categories"}}]}]
+SUBJECT_DATA = [{"subject_data": [{"name": "not valid subject data", "description": "", "policies": [{}], "category": {}}]},
+ {"subject_data": [{"name": "not valid subject data", "description": "", "policies": [{}], "category": {"name": "test subject categories"}}]},
+ {"policies": [{"name": "test policy", "genre": "authz", "description": "description", "model": {"name": "test model"}, "mandatory": True}], "subject_data": [{"name": "one valid subject data", "description": "description", "policies": [{}], "category": {"name": "test subject categories"}}]},
+ {"subject_data": [{"name": "valid subject data", "description": "description", "policies": [{"name": "test policy"}], "category": {"name": "test subject categories"}}]},
+ {"subject_data": [{"name": "valid subject data", "description": "new description", "policies": [{"name": "test other policy"}], "category": {"name": "test subject categories"}}]}]
-OBJECT_DATA = [{"object_data": [{"name": "not valid object data", "description": "", "policy": {}, "category": {}}]},
- {"object_data": [{"name": "not valid object data", "description": "", "policy": {}, "category": {"name": "test object categories"}}]},
- {"policies": [{"name": "test policy", "genre": "authz", "description": "description", "model": {"name": "test model"}, "mandatory": True}], "object_data": [{"name": "one valid object data", "description": "description", "policy": {}, "category": {"name": "test object categories"}}]},
- {"object_data": [{"name": "valid object data", "description": "description", "policy": {"name": "test policy"}, "category": {"name": "test object categories"}}]},
- {"object_data": [{"name": "valid object data", "description": "new description", "policy": {"name": "test other policy"}, "category": {"name": "test object categories"}}]}]
+OBJECT_DATA = [{"object_data": [{"name": "not valid object data", "description": "", "policies": [{}], "category": {}}]},
+ {"object_data": [{"name": "not valid object data", "description": "", "policies": [{}], "category": {"name": "test object categories"}}]},
+ {"policies": [{"name": "test policy", "genre": "authz", "description": "description", "model": {"name": "test model"}, "mandatory": True}], "object_data": [{"name": "one valid object data", "description": "description", "policies": [{}], "category": {"name": "test object categories"}}]},
+ {"object_data": [{"name": "valid object data", "description": "description", "policies": [{"name": "test policy"}], "category": {"name": "test object categories"}}]},
+ {"object_data": [{"name": "valid object data", "description": "new description", "policies": [{"name": "test other policy"}], "category": {"name": "test object categories"}}]}]
-ACTION_DATA = [{"action_data": [{"name": "not valid action data", "description": "", "policy": {}, "category": {}}]},
- {"action_data": [{"name": "not valid action data", "description": "", "policy": {}, "category": {"name": "test action categories"}}]},
- {"policies": [{"name": "test policy", "genre": "authz", "description": "description", "model": {"name": "test model"}, "mandatory": True}], "action_data": [{"name": "one valid action data", "description": "description", "policy": {}, "category": {"name": "test action categories"}}]},
- {"action_data": [{"name": "valid action data", "description": "description", "policy": {"name": "test policy"}, "category": {"name": "test action categories"}}]},
- {"action_data": [{"name": "valid action data", "description": "new description", "policy": {"name": "test other policy"}, "category": {"name": "test action categories"}}]}]
+ACTION_DATA = [{"action_data": [{"name": "not valid action data", "description": "", "policies": [{}], "category": {}}]},
+ {"action_data": [{"name": "not valid action data", "description": "", "policies": [{}], "category": {"name": "test action categories"}}]},
+ {"policies": [{"name": "test policy", "genre": "authz", "description": "description", "model": {"name": "test model"}, "mandatory": True}], "action_data": [{"name": "one valid action data", "description": "description", "policies": [{}], "category": {"name": "test action categories"}}]},
+ {"action_data": [{"name": "valid action data", "description": "description", "policies": [{"name": "test policy"}], "category": {"name": "test action categories"}}]},
+ {"action_data": [{"name": "valid action data", "description": "new description", "policies": [{"name": "test other policy"}], "category": {"name": "test action categories"}}]}]
PRE_META_RULES = {"subject_categories": [{"name": "test subject categories", "description": "subject category description"}],
@@ -104,9 +109,9 @@ PRE_ASSIGNMENTS = {"models": [{"name": "test model", "description": "", "meta_ru
"objects": [{"name": "test object", "description": "description of the object", "extra": {}, "policies": [{"name": "test policy"}]}],
"actions": [{"name": "test action", "description": "description of the action", "extra": {}, "policies": [{"name": "test policy"}]}],
"meta_rules": [{"name": "good meta rule", "description": "valid meta rule", "subject_categories": [{"name": "test subject categories"}], "object_categories": [{"name": "test object categories"}], "action_categories": [{"name": "test action categories"}]}],
- "subject_data": [{"name": "subject data", "description": "test subject data", "policy": {"name": "test policy"}, "category": {"name": "test subject categories"}}],
- "object_data": [{"name": "object data", "description": "test object data", "policy": {"name": "test policy"}, "category": {"name": "test object categories"}}],
- "action_data": [{"name": "action data", "description": "test action data", "policy": {"name": "test policy"}, "category": {"name": "test action categories"}}]}
+ "subject_data": [{"name": "subject data", "description": "test subject data", "policies": [{"name": "test policy"}], "category": {"name": "test subject categories"}}],
+ "object_data": [{"name": "object data", "description": "test object data", "policies": [{"name": "test policy"}], "category": {"name": "test object categories"}}],
+ "action_data": [{"name": "action data", "description": "test action data", "policies": [{"name": "test policy"}], "category": {"name": "test action categories"}}]}
SUBJECT_ASSIGNMENTS = [{"subject_assignments": [{"subject": {"name": "unknonw"}, "category" : {"name": "test subject categories"}, "assignments": [{"name": "subject data"}]}]},
@@ -512,4 +517,6 @@ def test_import_subject_object_action_data():
def test_clean():
client = utilities.register_client()
- import_export_utilities.clean_all(client) \ No newline at end of file
+ import_export_utilities.clean_all(client)
+ #restore the database as previously
+ utilities.get_policy_id() \ No newline at end of file