Skip to content

Commit

Permalink
add block_by to event model, remove blocked_by in gov response buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
HzANut committed Jul 1, 2023
1 parent b664539 commit 2d7535a
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 15 deletions.
3 changes: 0 additions & 3 deletions moesifdjango/block_response_buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,12 @@ def __init__(self):
self.responses = []
self.rule_type = None
self.blocked = False
self.blocked_by = None # holds blocked by rule_id

def update(self, block, updated_gr_status, updated_gr_headers, updated_gr_body, rule_id):
if not self.rule_type:
self.rule_type = 'regex'
if block and not self.blocked:
self.blocked = True
if self.blocked:
self.blocked_by = rule_id

gov_rule_response = GovernanceRuleBlockResponse()
gov_rule_response.update_response(updated_gr_status, updated_gr_headers, updated_gr_body, block, rule_id)
Expand Down
5 changes: 3 additions & 2 deletions moesifdjango/event_mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@ def to_response(self, rsp_time, status_code, rsp_headers, rsp_body, transfer_enc
transfer_encoding=transfer_encoding)

@classmethod
def to_event(cls, event_req, event_rsp, user_id, company_id, session_token, metadata):
def to_event(cls, event_req, event_rsp, user_id, company_id, session_token, metadata, blocked_by=None):
# Prepare Event Model
return EventModel(request=event_req,
response=event_rsp,
user_id=user_id,
company_id=company_id,
session_token=session_token,
metadata=metadata,
direction="Incoming")
direction="Incoming",
blocked_by=blocked_by)
4 changes: 1 addition & 3 deletions moesifdjango/governance_rule_response.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
class GovernanceRuleBlockResponse:
# TODO Create a empty init, and add function
def __init__(self):
self.block_response_status = None
self.block_response_headers = None
Expand All @@ -12,6 +11,5 @@ def update_response(self, gr_status, gr_headers, gr_body, block, rule_id):
self.block_response_headers = gr_headers
self.block_response_body = gr_body
self.blocked = block
if self.blocked:
self.blocked_by = rule_id
self.blocked_by = rule_id

2 changes: 1 addition & 1 deletion moesifdjango/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ def __call__(self, request):
rsp_body_transfer_encoding)
# Prepare Event Model
event_model = self.event_mapper.to_event(event_req, event_rsp, user_id, company_id, session_token,
metadata)
metadata, updated_Response.blocked_by)

# Mask Event Model
event_model = self.logger_helper.mask_event(event_model, self.middleware_settings, self.DEBUG)
Expand Down
10 changes: 6 additions & 4 deletions moesifdjango/moesif_gov.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ def prepare_request_config_based_on_regex_config(cls, event: EventModel, ready_f

def generate_blocking_response(self, response_buffers):
"""
rearrange matching rules' response, merge all of the headers, and ordered by the rules type priority
rearrange matching rules' response, merge all the headers, and ordered by the rules type priority
updated response_body and response_status with the highest priority blocked rule
:param response_buffers:
Expand All @@ -486,7 +486,8 @@ def generate_blocking_response(self, response_buffers):
updated_body = None
updated_status = None
updated_headers = {}
rule_id = None
blocked_by = None
blocked = False

for rule_type in REVERSED_PRIORITY_RULES_ORDER:
if rule_type in response_buffers:
Expand All @@ -497,11 +498,12 @@ def generate_blocking_response(self, response_buffers):
if response.blocked:
updated_body = response.block_response_body
updated_status = response.block_response_status
rule_id = response.rule_id
blocked_by = response.blocked_by
blocked = response.blocked
updated_headers.update(response.block_response_headers)

gov_rule_response = GovernanceRuleBlockResponse()
gov_rule_response.update_response(updated_status, updated_headers, updated_body, True, rule_id)
gov_rule_response.update_response(updated_status, updated_headers, updated_body, blocked, blocked_by)
return gov_rule_response

@classmethod
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.4.0
moesifapi==1.4.1
celery>=3.1.25
moesifpythonrequest==0.3.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 @@ -82,7 +82,7 @@
# your project is installed. For an analysis of "install_requires" vs pip's
# requirements files see:
# https://packaging.python.org/en/latest/requirements.html
install_requires=['requests', 'jsonpickle', 'python-dateutil', 'isodatetimehandler', 'moesifapi>=1.4.0',
install_requires=['requests', 'jsonpickle', 'python-dateutil', 'isodatetimehandler', 'moesifapi>=1.4.1',
'moesifpythonrequest>=0.3.0', 'apscheduler', 'nose'],

# List additional groups of dependencies here (e.g. development
Expand Down

0 comments on commit 2d7535a

Please sign in to comment.