Skip to content

Commit

Permalink
Merge pull request #64 from Moesif/Support-Governance
Browse files Browse the repository at this point in the history
Update moesif api version ..
  • Loading branch information
praves77 committed Aug 20, 2022
2 parents efd6a53 + 88df833 commit e2f9588
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 20 deletions.
47 changes: 29 additions & 18 deletions moesifdjango/moesif_gov.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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:
Expand Down Expand Up @@ -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

Expand All @@ -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:
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit e2f9588

Please sign in to comment.