def send_enforcer_telemetry_activity(config, update_reason): details = { 'enforcer_configs': config.telemetry_config, 'node_name': socket.gethostname(), 'os_name': sys.platform, 'update_reason': update_reason, 'module_version': config.module_version } body = { 'type': px_constants.TELEMETRY_ACTIVITY, 'timestamp': time.time(), 'px_app_id': config.app_id, 'details': details } headers = { 'Authorization': 'Bearer ' + config.auth_token, 'Content-Type': 'application/json' } config.logger.debug('Sending telemetry activity to PerimeterX servers') px_httpc.send(full_url=config.server_host + px_constants.API_ENFORCER_TELEMETRY, body=json.dumps(body), headers=headers, config=config, method='POST')
def _send_activities_chunk(): global ACTIVITIES_BUFFER default_headers = { 'Authorization': 'Bearer ' + CONFIG.auth_token, 'Content-Type': 'application/json' } full_url = CONFIG.server_host + px_constants.API_ACTIVITIES chunk = ACTIVITIES_BUFFER[:10] for _ in range(len(chunk)): ACTIVITIES_BUFFER.pop(0) px_httpc.send(full_url=full_url, body=json.dumps(chunk), headers=default_headers, config=CONFIG, method='POST')
def send_reverse_client_request(self, config, ctx): if not config.first_party: headers = {'Content-Type': 'application/javascript'} return '200 OK', headers, '' client_request_uri = '/{}/main.min.js'.format(config.app_id) msg = 'Forwarding request from {} to client at {}{}' self._logger.debug( msg.format(ctx.uri.lower(), px_constants.CLIENT_HOST, client_request_uri)) headers = { 'host': px_constants.CLIENT_HOST, px_constants.FIRST_PARTY_HEADER: '1', px_constants.ENFORCER_TRUE_IP_HEADER: ctx.ip } filtered_headers = px_utils.handle_proxy_headers( ctx.headers, ctx.ip, self.is_gae) filtered_headers = px_utils.merge_two_dicts(filtered_headers, headers) delete_extra_headers(filtered_headers) px_response = px_httpc.send(full_url=px_constants.CLIENT_HOST + client_request_uri, body='', headers=filtered_headers, config=config, method='GET') if self.is_gae: data = px_response.raw.read(decode_content=True) else: data = px_response.raw.read() headers = px_response.headers status = str(px_response.status_code) + ' ' + str(px_response.reason) return status, headers, data
def send_reverse_captcha_request(self, config, ctx): if not config.first_party: status = '200 OK' response_headers = {'Content-Type': 'application/javascript'} return status, response_headers, '' uri = '/{}{}?{}'.format( config.app_id, ctx.uri.lower().replace(self.captcha_reverse_prefix, ''), ctx.query_params) host = px_constants.CAPTCHA_HOST headers = { 'host': px_constants.CAPTCHA_HOST, px_constants.FIRST_PARTY_HEADER: '1', px_constants.ENFORCER_TRUE_IP_HEADER: ctx.ip } filtered_headers = px_utils.handle_proxy_headers( ctx.headers, ctx.ip, self.is_gae) filtered_headers = px_utils.merge_two_dicts(filtered_headers, headers) delete_extra_headers(filtered_headers) self._logger.debug( 'Forwarding request from {} to client at {}{}'.format( ctx.uri.lower(), host, uri)) px_response = px_httpc.send(full_url=host + uri, body='', headers=filtered_headers, config=config, method='GET') if self.is_gae: data = px_response.raw.read(decode_content=True) else: data = px_response.raw.read() headers = px_response.headers status = str(px_response.status_code) + ' ' + str(px_response.reason) return status, headers, data
def send_risk_request(ctx, config): """ :param PxContext ctx: :param PxConfig config: :return dict: """ start = time.time() body = prepare_risk_body(ctx, config) default_headers = { 'Authorization': 'Bearer ' + config.auth_token, 'Content-Type': 'application/json' } try: response = px_httpc.send(full_url=config.server_host + px_constants.API_RISK, body=json.dumps(body), config=config, headers=default_headers, method='POST', raise_error = True) if response: config.logger.debug('Risk response: {}'.format(response.content)) return json.loads(response.content) return False except requests.exceptions.Timeout: ctx.pass_reason = 's2s_timeout' risk_rtt = time.time() - start config.logger.debug('Risk API timed out, round_trip_time: {}'.format(risk_rtt)) return False except requests.exceptions.RequestException as e: ctx.pass_reason = 's2s_error' config.logger.debug('Unexpected exception in Risk API call: {}'.format(e)) return False
def test_send(self): with requests_mock.mock() as m: config = PxConfig({'app_id': 'PXfake_app_id'}) full_url = 'this_url.com/uri' method = 'POST' body = 'content to post' headers = {'content-type': 'application/json'} m.post('https://' + full_url) response = px_httpc.send(full_url=full_url, config=config, method=method, body=body, headers=headers) m.called m.get('https://' + full_url) method = 'GET' response = px_httpc.send(full_url=full_url, config=config, method=method, body=body, headers=headers) self.assertEqual(m.call_count, 2)
def send_reverse_xhr_request(self, config, ctx, body): uri = ctx.uri if not config.first_party or not config.first_party_xhr_enabled: body, response_headers = self.return_default_response(uri) return '200 OK', response_headers, body xhr_path_index = uri.find('/' + px_constants.XHR_FP_PATH) suffix_uri = uri[xhr_path_index + 4:] host = config.collector_host headers = { 'host': host, px_constants.FIRST_PARTY_HEADER: '1', px_constants.ENFORCER_TRUE_IP_HEADER: ctx.ip } if ctx.vid is not None: headers['cookie'] = 'pxvid=' + ctx.vid filtered_headers = px_utils.handle_proxy_headers( ctx.headers, ctx.ip, self.is_gae) filtered_headers = px_utils.merge_two_dicts(filtered_headers, headers) msg = 'Forwarding request from {} to client at {}{}' self._logger.debug(msg.format(ctx.uri.lower(), host, suffix_uri)) px_response = px_httpc.send(full_url=host + suffix_uri + "?" + ctx.query_params, body=body, headers=filtered_headers, config=config, method=ctx.http_method) if px_response.status_code >= 400: data, response_headers = self.return_default_response(uri) self._logger.debug('Error reversing the http call: {}'.format( px_response.reason)) return '200 OK', response_headers, data data = px_response.content headers = px_response.headers status = str(px_response.status_code) + ' ' + str(px_response.reason) return status, headers, data