Esempio n. 1
0
 def _export_subject_object_action_assignments(self, type_element,
                                               json_content):
     export_method_data = getattr(PolicyManager,
                                  'get_' + type_element + '_assignments')
     policies = PolicyManager.get_policies(self._user_id)
     element_assignments_array = []
     for policy_key in policies:
         assignments = export_method_data(self._user_id, policy_key)
         # logger.info(assignments)
         for assignment_key in assignments:
             assignment_dict = dict()
             JsonUtils.convert_id_to_name(
                 assignments[assignment_key][type_element + "_id"],
                 assignment_dict, type_element, type_element, PolicyManager,
                 self._user_id, policy_key)
             JsonUtils.convert_id_to_name(
                 assignments[assignment_key]["category_id"],
                 assignment_dict, "category", type_element + "_category",
                 ModelManager, self._user_id, policy_key)
             JsonUtils.convert_ids_to_names(
                 assignments[assignment_key]["assignments"],
                 assignment_dict, "assignments", type_element + "_data",
                 PolicyManager, self._user_id, policy_key)
             element_assignments_array.append(assignment_dict)
             logger.info("Exporting {} assignment {}".format(
                 type_element, assignment_dict))
     if len(element_assignments_array) > 0:
         json_content[type_element +
                      '_assignments'] = element_assignments_array
Esempio n. 2
0
 def _export_models(self, json_content):
     models = ModelManager.get_models(self._user_id)
     models_array = []
     for model_key in models:
         model = dict()
         JsonUtils.copy_field_if_exists(models[model_key], model, "name",
                                        str)
         JsonUtils.copy_field_if_exists(models[model_key], model,
                                        "description", str)
         # logger.info(models[model_key]["meta_rules"])
         JsonUtils.convert_ids_to_names(models[model_key]["meta_rules"],
                                        model, "meta_rules", "meta_rule",
                                        ModelManager, self._user_id)
         logger.info("Exporting model {}".format(model))
         models_array.append(model)
     if len(models_array) > 0:
         json_content["models"] = models_array
Esempio n. 3
0
 def _export_meta_rules(self, json_content):
     meta_rules = ModelManager.get_meta_rules(self._user_id)
     meta_rules_array = []
     # logger.info(meta_rules)
     for meta_rule_key in meta_rules:
         # logger.info(meta_rules[meta_rule_key])
         meta_rule_dict = dict()
         JsonUtils.copy_field_if_exists(meta_rules[meta_rule_key],
                                        meta_rule_dict, "name", str)
         JsonUtils.copy_field_if_exists(meta_rules[meta_rule_key],
                                        meta_rule_dict, "description", str)
         JsonUtils.convert_ids_to_names(
             meta_rules[meta_rule_key]["subject_categories"],
             meta_rule_dict, "subject_categories", "subject_category",
             ModelManager, self._user_id)
         JsonUtils.convert_ids_to_names(
             meta_rules[meta_rule_key]["object_categories"], meta_rule_dict,
             "object_categories", "object_category", ModelManager,
             self._user_id)
         JsonUtils.convert_ids_to_names(
             meta_rules[meta_rule_key]["action_categories"], meta_rule_dict,
             "action_categories", "action_category", ModelManager,
             self._user_id)
         logger.info("Exporting meta rule {}".format(meta_rule_dict))
         meta_rules_array.append(meta_rule_dict)
     if len(meta_rules_array) > 0:
         json_content['meta_rules'] = meta_rules_array
Esempio n. 4
0
    def _export_rules(self, json_content):
        policies = PolicyManager.get_policies(self._user_id)
        rules_array = []

        for policy_key in policies:
            rules = PolicyManager.get_rules(self._user_id, policy_key)
            rules = rules["rules"]
            # logger.info(rules)
            for rule in rules:
                rule_dict = dict()
                JsonUtils.copy_field_if_exists(rule, rule_dict, "instructions",
                                               dict)
                JsonUtils.copy_field_if_exists(rule, rule_dict, "enabled",
                                               True)
                JsonUtils.convert_id_to_name(rule["meta_rule_id"], rule_dict,
                                             "meta_rule", "meta_rule",
                                             ModelManager, self._user_id)
                JsonUtils.convert_id_to_name(policy_key, rule_dict, "policy",
                                             "policy", PolicyManager,
                                             self._user_id)
                ids = rule["rule"]
                rule_description = dict()
                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
                rules_array.append(rule_dict)

        if len(rules_array) > 0:
            json_content['rules'] = rules_array