def check_rule_in_dict(rule, in_dict): if "rule" not in in_dict: raise MoonCliException("Unexpected request result. rule not in result") if rule != in_dict["rule"]: raise MoonCliException( "Unexpected request result. Bad rule in result, expected {}". format(rule))
def check_policy_id_in_dict(policy_id, in_dict): if "policy_id" not in in_dict: raise MoonCliException( "Unexpected request result. policy_id not in result") if policy_id != in_dict["policy_id"]: raise MoonCliException( "Unexpected request result. Bad policy id in result, expected {}". format(policy_id))
def check_result(result): if type(result) is not dict or "result" not in result: raise MoonCliException( "Unexpected request result. It should be a dictionnary with a 'result' entry" ) if result["result"] is None: raise MoonCliException( "Unexpected request result. The 'result' entry shall not be null")
def check_meta_rule_id_in_dict(meta_rule_id, in_dict): if "meta_rule_id" not in in_dict: raise MoonCliException( "Unexpected request result. meta_rule_id not in result") if meta_rule_id != in_dict["meta_rule_id"]: raise MoonCliException( "Unexpected request result. Bad meta rule id in result, expected {}" .format(meta_rule_id))
def check_subject_description(description, in_dict): if description is not None: if "description" not in in_dict: raise MoonCliException( "Unexpected request result. The description of the subject shall not be empty" ) if description not in in_dict["description"]: raise MoonCliException( "Unexpected request result. The description {} shall be in the subject" .format(description))
def _check_generic_in_result(field, result, check_not_null=False): if type(field) is not str or type( result) is not dict or field not in result: raise MoonCliException( "Unexpected request result. It should be a dictionnary with a '{}' entry" .format(field)) if check_not_null is True and result[field] is None: raise MoonCliException( "Unexpected request result. The '{}' entry shall not be null". format(field))
def _check_generic_policy_in_dict(field, policy_id, in_dict): if type(field) is str: if policy_id is not None: if "policy_list" not in in_dict: raise MoonCliException( "Unexpected request result. The policy list of the {} shall not be empty" .format(field)) if policy_id not in in_dict["policy_list"]: raise MoonCliException( "Unexpected request result. The policy with id {} shall be in the {}" .format(policy_id, field))
def check_rule_id_in_list(meta_rule_id, rule_id, rule, in_dict): for item in in_dict: if "meta_rule_id" not in item: raise MoonCliException( "Unexpected request result. meta_rule_id field not in result") if meta_rule_id == item["meta_rule_id"]: if rule_id == item["id"]: if rule != item["rule"]: raise MoonCliException( "Unexpected request result. Bad rule in result, expected {}" .format(rule))
def check_meta_rules_list_in_model(meta_rule_list, model_id, result): if result["models"] is None: raise MoonCliException( "Unexpected request result. results shall not be empty") if model_id not in result['models']: raise MoonCliException("Unexpected request result. Unknown Model id") if "meta_rules" not in result['models'][model_id]: raise MoonCliException( "Unexpected request result. Meta rules related to model with id {} are empty" .format(model_id)) if meta_rule_list != result['models'][model_id]["meta_rules"]: raise MoonCliException( "Unexpected request result. Meta rule of model with id {} are different from those expected" .format(model_id))
def _check_category_in_generic_data_data(field, category_id, result): _check_generic_data_data(field, result) for _data in result[field]: if category_id != _data["category_id"]: raise MoonCliException( "Unexpected request result. Category id {} not in {} data". format(category_id, field))
def _check_id_not_in_generic_data_data(field, data_id, result): if type(field) is str: _check_generic_data_data(field, result) for _data in result[field]: if data_id in list(_data['data'].keys()): raise MoonCliException( "Unexpected request result. Data id {} shall not be in {}". format(data_id, field))
def _check_generic_name(field, name, field_elt_id, result, do_check_name=True): if type(field) is str: if result[field] is None: raise MoonCliException( "Unexpected request result : {} shall not be empty".format( field)) if field_elt_id not in result[field]: raise MoonCliException( "Unexpected request result. Unknown {} id".format(field)) if "name" not in result[field][field_elt_id]: raise MoonCliException( "Unexpected request result : {} with id {} has no name".format( field, field_elt_id)) if do_check_name and name != result[field][field_elt_id]["name"]: raise MoonCliException( "Unexpected request result : {} with id {} has a bad name. Expected {}" .format(field, field_elt_id, name))
def _check_generic_elt_id(field1, field1_id, field2, field2_id, result): if type(field1) is str and type(field2) is str: if result[field1] is None: raise MoonCliException( "Unexpected request result: {} shall not be empty".format( field1)) if field1_id not in result[field1]: raise MoonCliException( "Unexpected request result. Unknown {} with id".format(field1)) if field2 not in result[field1][field1_id]: raise MoonCliException( "Unexpected request result. {} element with id {} has no {} field" .format(field1, field1_id, field2)) if field2_id != result[field1][field1_id][field2]: raise MoonCliException( "Unexpected request result. {} element with id {} has a bad {} id. Expected {}" .format(field1, field1_id, field2, field2_id))
def check_rule_id_not_in_list(rule_id, in_dict): found_rule = False for item in in_dict: if rule_id == item["id"]: found_rule = True if found_rule is True: raise MoonCliException( "Unexpected request result. Rule with id {} shall not be in result" .format(rule_id))
def _check_not_generic_assignments(field, field_id_name, field_id, field_cat_id, field_data_id, result): if type(field) is str and type(field_id_name) is str: for key in result[field]: if field_id_name not in result[field][key]: raise MoonCliException( "Unexpected request result. subject_id not in result[{}] data" .format(field)) if "category_id" not in result[field][key]: raise MoonCliException( "Unexpected request result. category_id not in result[{}] data" .format(field)) if "assignments" not in result[field][key]: raise MoonCliException( "Unexpected request result. assignments not in result[{}] data" .format(field)) if result[field][key]['subject_id'] == field_id and \ result[field][key]["category_id"] == field_cat_id: if field_data_id in result[field][key]["assignments"]: raise MoonCliException( "Unexpected request result. {} data with id {} shall not be in result[{}][]['assignements'] data" .format(field, field_data_id, field))
def check_ocat_id_in_dict(ocat_id, in_dict): if ocat_id not in in_dict: raise MoonCliException( "Unexpected request result. Object category not in result")
def check_model_in_result(result, check_not_null=False): _check_generic_in_result("models", result) if check_not_null is True and result["models"] is None: raise MoonCliException( "Unexpected request result. The 'models' entry shall not be null")
def check_optionnal_result(result): if type(result) is not dict: raise MoonCliException( "Unexpected request result. It should be a dictionnary") if "result" in result: check_result(result)
def check_acat_id_in_dict(acat_id, in_dict): if acat_id not in in_dict: raise MoonCliException( "Unexpected request result. Action category not in result")
def check_policy_id_in_pipeline(policy_id, pipeline): if policy_id not in pipeline: raise MoonCliException( "Unexpected request result. The policy id {} shall be in the pipeline" .format(policy_id))
def check_name_in_slaves(name, slaves): if name is None: raise MoonCliException("The slave name must be provided !") names = map(lambda x: x['name'], slaves) if name not in names: raise MoonCliException("The slave '{}' was not found !".format(name))
def _check_generic_data_data(field, result): if type(field) is str: if field not in result: raise MoonCliException( "Unexpected request result. The {} field shall be in result". format(field))
def check_pdp_id(pdp_id, result): check_pdp_in_result(result) if pdp_id not in result['pdps']: raise MoonCliException("Unexpected request result. Unknown pdp id")
def test_authz_request(): from python_moonclient.core import config conf_data = config.get_config_data("consul", 8500) if not isinstance(conf_data, dict): raise MoonCliException( "Unexpected error : the conf data is not a dictionnary")