From 19b8a62ba5586bf3f88934e196ea7cf7080d7cfc Mon Sep 17 00:00:00 2001 From: xiluan Date: Wed, 17 Aug 2022 14:49:21 -0700 Subject: [PATCH 1/6] update moesif api version --- requirements.txt | 2 +- setup.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) 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, From bd7a38157c1c5091151175a47b05cfe08512e0a4 Mon Sep 17 00:00:00 2001 From: xiluan Date: Thu, 18 Aug 2022 10:54:30 -0700 Subject: [PATCH 2/6] add try and except for regex invalid pattern issue --- moesifdjango/moesif_gov.py | 47 +++++++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 18 deletions(-) diff --git a/moesifdjango/moesif_gov.py b/moesifdjango/moesif_gov.py index 07ec65b..7a4ee25 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 start 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 when 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] no support nested fields 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 From cabc212248924302a7fb99b9abf21154c864ad26 Mon Sep 17 00:00:00 2001 From: xiluan Date: Thu, 18 Aug 2022 10:56:31 -0700 Subject: [PATCH 3/6] bump up version to 2.1.3 --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index fb01a8c..e1e3895 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.2', + version='2.1.3', description='Moesif Middleware for Python Django', long_description=long_description, From 47def3d973dcf915c4e2a18f86002889aeb73baa Mon Sep 17 00:00:00 2001 From: xiluan Date: Fri, 19 Aug 2022 18:00:27 -0700 Subject: [PATCH 4/6] Fix invalid regex rule, change version of moesif-api to 1.4.0, bump up version to 2.1.2 --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index e1e3895..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.3', + version='2.1.2', description='Moesif Middleware for Python Django', long_description=long_description, From d261f6536a2fcc08a3340f5b9dc5f24ae44b86d6 Mon Sep 17 00:00:00 2001 From: xiluan Date: Fri, 19 Aug 2022 18:26:51 -0700 Subject: [PATCH 5/6] change error messages for governance rule --- moesifdjango/moesif_gov.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/moesifdjango/moesif_gov.py b/moesifdjango/moesif_gov.py index 7a4ee25..b30bc22 100644 --- a/moesifdjango/moesif_gov.py +++ b/moesifdjango/moesif_gov.py @@ -150,7 +150,7 @@ def check_request_with_regex_match(self, gr_regex_configs_list, request_mapping_ if not ready_for_body_request: continue except Exception as e: - print("[moesif] Error when matching path start with request\\.body\\", 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: @@ -160,7 +160,7 @@ def check_request_with_regex_match(self, gr_regex_configs_list, request_mapping_ # Perform regex matching with event value regex_matched = self.regex_pattern_match(event_data, values) except Exception as e: - print("[moesif] Error when matching condition of governance rule {} and event data {}".format(values, 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 @@ -243,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] no support nested fields for request body condition matching") + print("[moesif] nested fields are not supported") return False return True From 88df833667888eaf529b5bee42042cdd63e80ea5 Mon Sep 17 00:00:00 2001 From: xiluan Date: Fri, 19 Aug 2022 18:30:33 -0700 Subject: [PATCH 6/6] changed more messages for governance rule --- moesifdjango/moesif_gov.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/moesifdjango/moesif_gov.py b/moesifdjango/moesif_gov.py index b30bc22..b5f2375 100644 --- a/moesifdjango/moesif_gov.py +++ b/moesifdjango/moesif_gov.py @@ -243,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] nested fields are not supported") + print("[moesif] nested fields are not supported for request body condition matching") return False return True