def process_request(self, request): import re if is_text_payload(request) and request.body: body = str(request.body) body = re.sub(r'"accessToken": "([0-9a-f-]{36})"', r'"accessToken": 00000000-0000-0000-0000-000000000000', body) request.body = body return request
def test_text_payload(self): http_entity = mock.MagicMock() headers = {} http_entity.headers = headers headers["content-type"] = "foo/" self.assertFalse(is_text_payload(http_entity)) headers["content-type"] = "text/html; charset=utf-8" self.assertTrue(is_text_payload(http_entity)) headers["content-type"] = "APPLICATION/JSON; charset=utf-8" self.assertTrue(is_text_payload(http_entity)) headers["content-type"] = "APPLICATION/xml" self.assertTrue(is_text_payload(http_entity)) http_entity.headers = None # default to text mode if there is no header self.assertTrue(is_text_payload(http_entity))
def process_request(self, request): request = super(RENameReplacer, self).process_request(request) for expr, new in self.patterns: if is_text_payload(request) and request.body: if isinstance(request.body, dict): continue body = six.ensure_str(request.body) for old in re.findall(expr, body): request.body = body.replace(old, new) return request
def process_request(self, request): for old, new in self.names_name: request.uri = request.uri.replace(old, new) if is_text_payload(request) and request.body: try: body = str(request.body, 'utf-8') if isinstance(request.body, bytes) else str(request.body) except TypeError: # python 2 doesn't allow decoding through str body = str(request.body) if old in body: request.body = body.replace(old, new) return request
def process_response(self, response): import json fake_data = "hidden" sensitive_data = "apiKey" if is_text_payload(response) and response["body"]["string"]: try: props = json.loads(response["body"]["string"]) if sensitive_data in props: del props[sensitive_data] props[fake_data] = fake_data response["body"]["string"] = json.dumps(props) except TypeError: pass return response
def process_response(self, response): if is_text_payload(response) and response['body']['string']: body_string = _py3_byte_to_str(response['body']['string']) if self._activated: body = json.loads(body_string) self._process_key_response(body, "primary") self._process_key_response(body, "secondary") self._activated = False for replacement in self._replacements.values(): for key in replacement.seen_keys: response['body']['string'] = body_string.replace( key, replacement.key_value) return response
def process_response(self, response): response = super(RENameReplacer, self).process_response(response) for expr, new in self.patterns: if is_text_payload(response) and response["body"]["string"]: if isinstance(response["body"]["string"], bytes): body = response["body"]["string"].decode( "utf8", "backslashreplace") else: body = response["body"]["string"] for old in re.findall(expr, body): body = body.replace(old, new) if isinstance(response["body"]["string"], bytes): response["body"]["string"] = body.encode( "utf8", "backslashreplace") else: response["body"]["string"] = body return response
def process_response(self, response): response = super(RENameReplacer, self).process_response(response) for expr, new in self.patterns: if is_text_payload(response) and response['body']['string']: if isinstance(response['body']['string'], bytes): body = response['body']['string'].decode( 'utf8', 'backslashreplace') else: body = response['body']['string'] for old in re.findall(expr, body): body = body.replace(old, new) if isinstance(response['body']['string'], bytes): response['body']['string'] = body.encode( 'utf8', 'backslashreplace') else: response['body']['string'] = body return response
def process_request(self, request): body_string = None body = None if is_text_payload(request) and request.body: body_string = _py3_byte_to_str(request.body) body = json.loads(body_string) pattern = r"/providers/Microsoft\.Batch/batchAccounts/[^/]+/(list|regenerate)Keys$" search_result = re.search(pattern, request.path, re.I) if search_result: self._activated = True if search_result.group(1) == "regenerate": if body and body["keyName"]: replacement = self._get_replacement(body["keyName"]) replacement.should_regen = True if body_string: for replacement in self._replacements.values(): for key in replacement.seen_keys: request.body = body_string.replace(key, replacement.key_value) return request
def process_response(self, response): if is_text_payload(response) and response['body']['string']: response['body']['string'] = self._replace_keys(response['body']['string']) return response
def process_request(self, request): if is_text_payload(request) and request.body: request.body = self._replace_keys(request.body.decode()).encode() return request
def process_request(self, request): if is_text_payload(request) and isinstance(request.body, bytes): request.body = self._replace_byte_keys(request.body) elif is_text_payload(request) and isinstance(request.body, str): request.body = self._replace_string_keys(request.body) return request
def process_response(self, response): if is_text_payload(response) and response["body"]["string"]: body_string = _py3_byte_to_str(response["body"]["string"]) response["body"]["string"] = self._replace_sas(body_string) return response