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_response(self, response): import json if is_text_payload(response) and response['body']['string']: try: body = json.loads(response['body']['string']) body = self.recursive_hide(body) response['body']['string'] = json.dumps(body) except Exception: # pylint: disable=broad-except pass return response
def process_request(self, request): import json # hide secrets in request body if is_text_payload(request) and request.body and json.loads(request.body): body = self.recursive_hide(json.loads(request.body)) request.body = json.dumps(body) # hide token in header if 'x-ms-cupertino-test-token' in request.headers: request.headers['x-ms-cupertino-test-token'] = 'hidden' 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 = _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_request(self, request): body_string = None body = None if is_text_payload(request) and request.body: body_string = _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"]: body_string = _byte_to_str(response["body"]["string"]) response["body"]["string"] = self._replace_sas(body_string) return response
def process_response(self, response): if is_text_payload(response) and response['body']['string']: response['body']['string'] = self._replace_string_keys( response['body']['string']) return response
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