diff --git a/moesifdjango/moesif_gov.py b/moesifdjango/moesif_gov.py index 07ec65b..b5f2375 100644 --- a/moesifdjango/moesif_gov.py +++ b/moesifdjango/moesif_gov.py @@ -31,7 +31,8 @@ def fetch_entity_rules_from_app_config(cls, config, debug): 'company_rules': raw_body.get('company_rules', {}), } if debug: - print(f"[moesif] config got {len(entity_rules['user_rules'])} cohort users and {len(entity_rules['company_rules'])} cohort companies") + print( + f"[moesif] config got {len(entity_rules['user_rules'])} cohort users and {len(entity_rules['company_rules'])} cohort companies") except Exception as e: print("[moesif] Error when fetching raw_body from config, ", e) @@ -144,16 +145,22 @@ def check_request_with_regex_match(self, gr_regex_configs_list, request_mapping_ # Iterate through conditions table and perform `and` operation between each conditions for path, values in condition_path_value_mapping.items(): - if re.fullmatch('request\.body\..*', path): - if not ready_for_body_request: - continue + try: + if re.fullmatch('request\.body\..*', path): + if not ready_for_body_request: + continue + except Exception as e: + print("[moesif] Error when matching path starts with request\\.body\\", e) # Check if the path exists in the request config mapping if path in request_mapping_for_regex_config: - # Fetch the value of the path in request config mapping - event_data = request_mapping_for_regex_config[path] - # Perform regex matching with event value - regex_matched = self.regex_pattern_match(event_data, values) + try: + # Fetch the value of the path in request config mapping + event_data = request_mapping_for_regex_config[path] + # Perform regex matching with event value + regex_matched = self.regex_pattern_match(event_data, values) + except Exception as e: + print("[moesif] Error while matching condition of governance rule {} and event data {}".format(values, e)) else: # Path does not exist in request config mapping, so no need to match regex condition rule regex_matched = False @@ -236,7 +243,7 @@ def check_if_condition_for_request_body_field(cls, condition): return False start += len('request.body.') if '.' in path[start:]: - print("Moesif does not support nested fields") + print("[moesif] nested fields are not supported for request body condition matching") return False return True @@ -269,7 +276,7 @@ def get_updated_response_with_matched_rules(self, governance_rule, rule_and_valu try: updated_gr_values[int(k)] = v except Exception as e: - print('Error when converting entity rules values key: ', e) + print('[moesif] Error when converting entity rules values key: ', e) updated_gr_headers = self.transform_values(updated_gr_headers, updated_gr_values) updated_gr_body = self.transform_values(updated_gr_body, updated_gr_values) @@ -302,9 +309,12 @@ def block_request_based_on_entity_governance_rule(self, try: entity_id_rules_mapping = entity_rules[rule_entity_type][entity_id] except KeyError as ke: - print('[moesif] Skipped blocking request since no governance rules in type of {} with the entity Id - {}: {}'.format(rule_entity_type, entity_id, ke)) + print( + '[moesif] Skipped blocking request since no governance rules in type of {} with the entity Id - {}: {}'.format( + rule_entity_type, entity_id, ke)) except Exception as e: - print('[moesif] Skipped blocking request, Error when fetching entity rule with entity {}, {}'.format(entity_id, e)) + print('[moesif] Skipped blocking request, Error when fetching entity rule with entity {}, {}'.format( + entity_id, e)) if not entity_id_rules_mapping: return response_buffer @@ -314,7 +324,8 @@ def block_request_based_on_entity_governance_rule(self, try: rule_id = rule_and_values['rules'] # rule_id is represented as "rules" in the config schema except KeyError as ke: - print('[moesif] Skipped a rule in type of {} since the [rule_id] is not found with entity - {}, {}'.format( + print( + '[moesif] Skipped a rule in type of {} since the [rule_id] is not found with entity - {}, {}'.format( rule_entity_type, entity_id, ke)) continue @@ -333,7 +344,7 @@ def block_request_based_on_entity_governance_rule(self, gr_regex_configs = governance_rule["regex_config"] matched = self.check_event_matched_with_governance_rules(gr_regex_configs, request_mapping_for_regex_config, - ready_for_body_request) + ready_for_body_request) if not matched: if DEBUG: @@ -370,13 +381,13 @@ def get_rules_id_if_governance_rule_matched(self, regex_governance_rules, event, regex_configs = rule['regex_config'] matched = self.check_request_with_regex_match(regex_configs, request_config_mapping, - ready_for_body_request) + ready_for_body_request) if matched: try: matched_rules_id.append(rule['_id']) except KeyError as ke: - print('Error when fetching matched regex governance rule ', ke) + print('[moesif] Error when fetching matched regex governance rule ', ke) return matched_rules_id @@ -393,7 +404,7 @@ def block_request_based_on_governance_rule_regex_config(self, event, ready_for_b response_buffer = BlockResponseBufferList() matched_rules_id = self.get_rules_id_if_governance_rule_matched(regex_governance_rules, event, - ready_for_body_request) + ready_for_body_request) if not matched_rules_id: if DEBUG: @@ -420,7 +431,7 @@ def block_request_based_on_governance_rule_regex_config(self, event, ready_for_b response_buffer.update(block, gr_status, gr_header, gr_body) if DEBUG: - print('request matched with regex rule with rule_id {}'.format(rule_id)) + print('[moesif] request matched with regex rule with rule_id {}'.format(rule_id)) return response_buffer # TODO can deal with request.body in one place diff --git a/requirements.txt b/requirements.txt index 7c76734..2e543bc 100755 --- a/requirements.txt +++ b/requirements.txt @@ -3,7 +3,7 @@ jsonpickle==0.7.1 python-dateutil==2.5.3 nose==1.3.7 isodatetimehandler==1.0.2 -moesifapi==1.3.3 +moesifapi==1.4.0 celery>=3.1.25 moesifpythonrequest==0.2.0 apscheduler==3.6.1 diff --git a/setup.py b/setup.py index fb9de27..fb01a8c 100644 --- a/setup.py +++ b/setup.py @@ -28,7 +28,7 @@ # Versions should comply with PEP440. For a discussion on single-sourcing # the version across setup.py and the project code, see # https://packaging.python.org/en/latest/single_source_version.html - version='2.1.1', + version='2.1.2', description='Moesif Middleware for Python Django', long_description=long_description,