def validate_spycloud_outputs(observable, key): catalogs = {} headers = { **current_app.config['SPYCLOUD_BASE_HEADERS'], 'X-API-Key': key } url = url_for(f'breach/data/emails/{observable["value"]}') spycloud_breach_output = get_spycloud_breach_outputs(url, headers) if spycloud_breach_output: spycloud_breach_output['observable'] = observable for result in spycloud_breach_output['results']: url = url_for(f'breach/catalog/{result["source_id"]}') spycloud_breach_catalog = get_spycloud_breach_outputs( url, headers) if spycloud_breach_catalog.get('results'): catalog = spycloud_breach_catalog['results'][0] catalogs.update({catalog['id']: catalog}) else: catalogs.update({result["source_id"]: {}}) return spycloud_breach_output, catalogs
def outdoor_closest(): lat = request.args.get('lat', default=None) long = request.args.get('long', default=None) if long is None or lat is None: abort(400) geotag = (lat, long) city = get_city(geotag) (station_name, distance) = get_closest_outdoor(city, geotag) payload = { "closest": station_name, "distance": distance, "details_uri": url_for(".station_details", city=city, id=station_name), "pm25_uri": url_for(".station_pm25", city=city, id=station_name), } return jsonify(payload), 200
def get(self, task_id): task = tasks.get(task_id) if task is None: abort(404) if 'return_value' not in task: print(str(threading.active_count())) return 'still_processing', 202, { 'Location': url_for('tasks.GetTaskStatus', task_id=task_id) } return task['return_value']
def check_spycloud_health(): url = url_for('watchlist/example.org') headers = { **current_app.config['SPYCLOUD_BASE_HEADERS'], 'X-API-Key': get_jwt() } response = requests.get(url, headers=headers) return get_response_data(response)
def check_health_abuse_ipdb_api(): url = url_for('check') headers = {'Accept': 'application/json', 'Key': get_jwt()} params = { 'ipAddress': current_app.config.get('ABUSE_IPDB_HEALTH_CHECK_IP') } response = requests.get(url, headers=headers, params=params) return get_response_data(response)
def wrapped(*args, **kwargs): def task_call(flask_app, environ): # Create a request context similar to that of the original request # so that the task can have access to flask.g, flask.request, etc. with flask_app.request_context(environ): try: tasks[task_id]['return_value'] = wrapped_function( *args, **kwargs) except HTTPException as e: tasks[task_id][ 'return_value'] = current_app.handle_http_exception(e) except Exception as e: # The function raised an exception, so we set a 500 error tasks[task_id]['return_value'] = InternalServerError() if current_app.debug: # We want to find out if something happened so reraise raise finally: # We record the time of the response, to help in garbage collecting old tasks tasks[task_id][ 'completion_timestamp'] = datetime.timestamp( datetime.utcnow()) # Assign an id to the asynchronous task task_id = uuid.uuid4().hex # Record the task, and then launch it tasks[task_id] = { 'task_thread': threading.Thread(target=task_call, args=(current_app._get_current_object(), request.environ)) } tasks[task_id]['task_thread'].start() # Return a 202 response, with a link that the client can use to obtain task status print(url_for('tasks.GetTaskStatus', task_id=task_id)) return 'accepted', 202, { 'Location': url_for('tasks.GetTaskStatus', task_id=task_id) }
def fetch_gsb_output(gsb_input): url = url_for('threatMatches:find') if url is None: # Mimic the GSB API error response payload. error = { 'code': HTTPStatus.FORBIDDEN, 'message': 'The request is missing a valid API key.', 'status': 'PERMISSION_DENIED', } return None, error return execute(requests.post, url, json=gsb_input, headers=headers())
def validate_abuse_ipdb_output(abuse_input, token): url = url_for('check') headers = { **current_app.config['USER_AGENT'], 'Accept': 'application/json', 'Key': token } params = { 'ipAddress': abuse_input, 'maxAgeInDays': current_app.config['ABUSE_IPDB_SEARCH_PERIOD'], 'verbose': 'true' } response = requests.get(url, headers=headers, params=params) return get_response_data(response)
def health(): url = url_for('threatLists') if url is None: # Mimic the GSB API error response payload. error = { 'code': HTTPStatus.FORBIDDEN, 'message': 'The request is missing a valid API key.', 'status': 'PERMISSION_DENIED', } return jsonify_errors(error) _, error = execute(requests.get, url, headers=headers()) if error: return jsonify_errors(error) else: return jsonify_data({'status': 'ok'})
def _novaclient(request, service_type='compute'): # service_type can not be 'volume'? 404 occurs user = request.ouser catalog = request.ouser.service_catalog #print catalog #LOG.debug('service_type is : %s' % service_type) # LOG.debug('service catalog is : %s' % catalog) # LOG.debug('novaclient connection created using token "%s" and url "%s"' % # (user.token, url_for(catalog, 'compute'))) management_url = url_for(catalog, service_type) c = nova_client.Client(user.name, user.token, project_id=user.tenant_id, auth_url=management_url) c.client.auth_token = user.token c.client.management_url = management_url return c
def validate_abuse_ipdb_output(abuse_input, token): url = url_for('check') headers = { 'Accept': 'application/json', 'User-Agent': ('Cisco Threat Response Integrations ' '<*****@*****.**>'), 'Key': token } params = { 'ipAddress': abuse_input, 'maxAgeInDays': current_app.config['ABUSE_IPDB_SEARCH_PERIOD'], 'verbose': 'true' } response = requests.get(url, headers=headers, params=params) return get_response_data(response)
def validate_cyberprotect_output(cyberprotect_input): url = url_for(cyberprotect_input) return get_response_data( requests.get(url, headers=current_app.config['CYBERPROTECT_HEADERS']))
def check_health_cyberprotect_api(): url = url_for(current_app.config['CYBERPROTECT_HEALTH_CHECK_IP']) return get_response_data( requests.get(url, headers=current_app.config['CYBERPROTECT_HEADERS']) )