Skip to content

Commit

Permalink
Merge pull request #63 from Moesif/Support-Governance
Browse files Browse the repository at this point in the history
fixed the bug on no-rules in config
  • Loading branch information
praves77 authored Jul 12, 2022
2 parents 2b50acf + b8c44b3 commit efd6a53
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 36 deletions.
20 changes: 10 additions & 10 deletions moesifdjango/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def __init__(self, get_response):

self.config = self.app_config.get_config(self.api_client, self.DEBUG)
self.gov_rule_helper = MoesifGovRuleHelper()
self.entity_rules = self.gov_rule_helper.fetch_entity_rules_from_app_config(self.config)
self.entity_rules = self.gov_rule_helper.fetch_entity_rules_from_app_config(self.config, self.DEBUG)

self.gov_rules_cacher = GovernanceRulesCacher(self.api_client)
self.user_governance_rules, self.company_governance_rules, self.regex_governance_rules \
Expand Down Expand Up @@ -126,7 +126,7 @@ def event_listener(self, event):
self.job_scheduler.fetch_app_config(self.config, self.config_etag,
self.sampling_percentage,
self.last_updated_time, self.api_client, self.DEBUG)
self.entity_rules = self.gov_rule_helper.fetch_entity_rules_from_app_config(self.config)
self.entity_rules = self.gov_rule_helper.fetch_entity_rules_from_app_config(self.config, self.DEBUG)

except Exception as ex:
if self.DEBUG:
Expand Down Expand Up @@ -254,14 +254,14 @@ def __call__(self, request):
event_model = self.logger_helper.mask_event(event_model, self.middleware_settings, self.DEBUG)

updated_Response = self.gov_rule_helper.govern_request(event_model,
user_id,
company_id,
req_body_transfer_encoding, # could be json or base64
self.entity_rules,
self.user_governance_rules,
self.company_governance_rules,
self.regex_governance_rules,
self.DEBUG)
user_id,
company_id,
req_body_transfer_encoding, # could be json or base64
self.entity_rules,
self.user_governance_rules,
self.company_governance_rules,
self.regex_governance_rules,
self.DEBUG)

if updated_Response:
response.content = self.parse_body.encode_response_body(updated_Response.block_response_body)
Expand Down
62 changes: 37 additions & 25 deletions moesifdjango/moesif_gov.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,26 @@ def __init__(self):
pass

@classmethod
def fetch_entity_rules_from_app_config(cls, config):
def fetch_entity_rules_from_app_config(cls, config, debug):
"""
get fetch entity rules from config
:param config:
:return:
:return: None, if there is no rules in config (no dynamic variable set with gov rules with the app)
Otherwise, return mapping with keys user_rules and company_rules
"""
entity_rules = {}
entity_rules = {'user_rules': {}, 'company_rules': {}}
try:
raw_body = json.loads(config.raw_body)

try:
user_rules = raw_body.get('user_rules')
if user_rules:
entity_rules['user_rules'] = user_rules
except KeyError:
print(f"[moesif] {raw_body}'s attribute [user_rules] is unknown.")

try:
company_rules = raw_body.get('company_rules')
if company_rules:
entity_rules['company_rules'] = company_rules
except KeyError:
print(f"[moesif] {raw_body}'s attribute [company_rules] is unknown.")
entity_rules = {
'user_rules': raw_body.get('user_rules', {}),
'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")

except KeyError:
print(f"[moesif] config attribute ['raw_body'] is unknown.")
except Exception as e:
print("[moesif] Error when fetching raw_body from config, ", e)

return entity_rules

Expand Down Expand Up @@ -301,11 +295,29 @@ def block_request_based_on_entity_governance_rule(self,
:param DEBUG:
:return: object of updated response status, headers and body, if criteria is matched and block is true, otherwise return None
"""
entity_rules = entity_rules[rule_entity_type][entity_id]

response_buffer = BlockResponseBufferList()
for rule_and_values in entity_rules:
rule_id = rule_and_values['rules']

entity_id_rules_mapping = None

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))
except Exception as 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

for rule_and_values in entity_id_rules_mapping:

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(
rule_entity_type, entity_id, ke))
continue

governance_rule = governance_rules.get(rule_id, None)

if not governance_rule or 'response' not in governance_rule or 'status' not in governance_rule['response'] \
Expand Down Expand Up @@ -388,7 +400,7 @@ def block_request_based_on_governance_rule_regex_config(self, event, ready_for_b
print('[moesif] no regex rule matched with the request')
else:
for rule_id in matched_rules_id:
governance_rule = regex_governance_rules[rule_id]
governance_rule = regex_governance_rules.get(rule_id)
if not governance_rule:
if DEBUG:
print(
Expand Down Expand Up @@ -451,7 +463,7 @@ def prepare_request_config_based_on_regex_config(cls, event: EventModel, ready_f

# Config mapping for response.status
if event.response.status:
regex_config["response.status"] = event.response.status
regex_config['response.status'] = event.response.status

return regex_config

Expand Down Expand Up @@ -534,7 +546,7 @@ def govern_request(self,
'company_rules',
company_id_entity,
DEBUG)
if not company_response_buffer.blocked: # No match or No need block
if not company_response_buffer.blocked:
if DEBUG:
print('[moesif] No blocking from company: ', company_id_entity)

Expand Down
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.0',
version='2.1.1',

description='Moesif Middleware for Python Django',
long_description=long_description,
Expand Down

0 comments on commit efd6a53

Please sign in to comment.