chg: [backend] Make everything async relying on asyncio

This commit is contained in:
Sami Mokaddem 2024-07-03 11:51:44 +02:00
parent f191af8573
commit 96d5b6d89e
2 changed files with 29 additions and 18 deletions

View file

@ -4,6 +4,7 @@ import json
from datetime import timedelta
from typing import Union
from urllib.parse import urljoin
import asyncio
import requests # type: ignore
import requests.adapters # type: ignore
from requests_cache import CachedSession
@ -18,7 +19,7 @@ requestSession.mount('https://', adapterCache)
requestSession.mount('http://', adapterCache)
def get(url, data={}, api_key=misp_apikey):
async def get(url, data={}, api_key=misp_apikey):
headers = {
'User-Agent': 'misp-exercise-dashboard',
"Authorization": api_key,
@ -27,17 +28,22 @@ def get(url, data={}, api_key=misp_apikey):
}
full_url = urljoin(misp_url, url)
try:
response = requestSession.get(full_url, data=data, headers=headers, verify=not misp_skipssl)
loop = asyncio.get_event_loop()
job = lambda: requestSession.get(full_url, data=data, headers=headers, verify=not misp_skipssl)
runningJob = loop.run_in_executor(None, job)
response = await runningJob
except requests.exceptions.ConnectionError as e:
logger.info('Could not perform request on MISP. %s', e)
return None
except Exception as e:
logger.warning('Could not perform request on MISP. %s', e)
try:
return response.json() if response.headers['content-type'].startswith('application/json') else response.text
except requests.exceptions.JSONDecodeError:
return response.text
def post(url, data={}, api_key=misp_apikey):
async def post(url, data={}, api_key=misp_apikey):
headers = {
'User-Agent': 'misp-exercise-dashboard',
"Authorization": api_key,
@ -46,32 +52,37 @@ def post(url, data={}, api_key=misp_apikey):
}
full_url = urljoin(misp_url, url)
try:
response = requestSession.post(full_url, data=json.dumps(data), headers=headers, verify=not misp_skipssl)
loop = asyncio.get_event_loop()
job = lambda: requestSession.post(full_url, data=json.dumps(data), headers=headers, verify=not misp_skipssl)
runningJob = loop.run_in_executor(None, job)
response = await runningJob
except requests.exceptions.ConnectionError as e:
logger.info('Could not perform request on MISP. %s', e)
return None
except Exception as e:
logger.warning('Could not perform request on MISP. %s', e)
try:
return response.json() if response.headers['content-type'].startswith('application/json') else response.text
except requests.exceptions.JSONDecodeError:
return response.text
def getEvent(event_id: int) -> Union[None, dict]:
return get(f'/events/view/{event_id}')
async def getEvent(event_id: int) -> Union[None, dict]:
return await get(f'/events/view/{event_id}')
def doRestQuery(authkey: str, request_method: str, url: str, payload: dict = {}) -> Union[None, dict]:
async def doRestQuery(authkey: str, request_method: str, url: str, payload: dict = {}) -> Union[None, dict]:
if request_method == 'POST':
return post(url, payload, api_key=authkey)
return await post(url, payload, api_key=authkey)
else:
return get(url, payload, api_key=authkey)
return await get(url, payload, api_key=authkey)
def getVersion() -> Union[None, dict]:
return get(f'/servers/getVersion.json')
async def getVersion() -> Union[None, dict]:
return await get(f'/servers/getVersion.json')
def getSettings() -> Union[None, dict]:
async def getSettings() -> Union[None, dict]:
SETTING_TO_QUERY = [
'Plugin.ZeroMQ_enable',
'Plugin.ZeroMQ_audit_notifications_enable',
@ -83,7 +94,7 @@ def getSettings() -> Union[None, dict]:
'MISP.log_auth',
'Security.allow_unsafe_cleartext_apikey_logging',
]
settings = get(f'/servers/serverSettings.json')
settings = await get(f'/servers/serverSettings.json')
if not settings:
return None
return {

View file

@ -106,7 +106,7 @@ async def reset_notifications(sid):
@sio.event
async def get_diagnostic(sid):
return getDiagnostic()
return await getDiagnostic()
@sio.event
async def toggle_verbose_mode(sid, payload):
@ -144,7 +144,7 @@ async def handleMessage(topic, s, message):
context = get_context(data)
succeeded_once = exercise_model.check_active_tasks(user_id, data, context)
if succeeded_once:
sendRefreshScore()
await sendRefreshScore()
@debounce(debounce_seconds=1)
@ -163,16 +163,16 @@ def get_context(data: dict) -> dict:
return context
def getDiagnostic() -> dict:
async def getDiagnostic() -> dict:
global ZMQ_MESSAGE_COUNT
diagnostic = {}
misp_version = misp_api.getVersion()
misp_version = await misp_api.getVersion()
if misp_version is None:
diagnostic['online'] = False
return diagnostic
diagnostic['version'] = misp_version
misp_settings = misp_api.getSettings()
misp_settings = await misp_api.getSettings()
diagnostic['settings'] = misp_settings
diagnostic['zmq_message_count'] = ZMQ_MESSAGE_COUNT
return diagnostic