re-enable log_all_requests
re-enable log_all_request - with excludelogging for internal
This commit is contained in:
parent
70fdfdb1b3
commit
e4130e4626
2 changed files with 120 additions and 86 deletions
|
|
@ -47,18 +47,19 @@ class ConfServer:
|
|||
self.app = None
|
||||
self.site = None
|
||||
self.runner = None
|
||||
self.excludelogging = ["base", "remove-bot", "remove-client", "restart-service"]
|
||||
|
||||
def get_milli_time(self, timetoconvert):
|
||||
return int(round(timetoconvert * 1000))
|
||||
|
||||
def confserver_app(self):
|
||||
self.app = web.Application(loop=asyncio.get_event_loop())#, middlewares=[self.log_all_requests])
|
||||
self.app = web.Application(loop=asyncio.get_event_loop(), middlewares=[self.log_all_requests])
|
||||
aiohttp_jinja2.setup(self.app, loader=jinja2.FileSystemLoader(os.path.join(bumper.data_dir, "web","templates")))
|
||||
|
||||
self.app.add_routes(
|
||||
[
|
||||
|
||||
web.get("", self.handle_base),
|
||||
web.get("", self.handle_base, name="base"),
|
||||
web.get("/bot/remove/{did}", self.handle_RemoveBot, name='remove-bot'),
|
||||
web.get("/client/remove/{resource}", self.handle_RemoveClient, name='remove-client'),
|
||||
web.get("/restart_{service}", self.handle_RestartService, name='restart-service'),
|
||||
|
|
@ -240,94 +241,99 @@ class ConfServer:
|
|||
|
||||
@web.middleware
|
||||
async def log_all_requests(self, request, handler):
|
||||
|
||||
try:
|
||||
if request.content_length:
|
||||
if request.content_type == "application/x-www-form-urlencoded":
|
||||
postbody = await request.post()
|
||||
|
||||
elif request.content_type == "application/json":
|
||||
try:
|
||||
postbody = json.loads(await request.text())
|
||||
except Exception as e:
|
||||
confserverlog.error("Request body not json: {} - {}".format(e, e.doc))
|
||||
postbody = e.doc
|
||||
|
||||
else:
|
||||
postbody = await request.post()
|
||||
else:
|
||||
postbody = None
|
||||
|
||||
response = await handler(request)
|
||||
if not "application/octet-stream" in response.content_type:
|
||||
logall = {
|
||||
"request": {
|
||||
"route_name": f"{request.match_info.route.name}",
|
||||
"method": f"{request.method}",
|
||||
"path": f"{request.path}",
|
||||
"query_string": f"{request.query_string}",
|
||||
"raw_path": f"{request.raw_path}",
|
||||
"raw_headers": f'{",".join(map("{}".format, request.raw_headers))}',
|
||||
"body": f"{postbody}",
|
||||
},
|
||||
|
||||
"response": {
|
||||
"response_body": f"{json.loads(response.body)}",
|
||||
"status": f"{response.status}",
|
||||
}
|
||||
}
|
||||
else:
|
||||
logall = {
|
||||
"request": {
|
||||
"route_name": f"{request.match_info.route.name}",
|
||||
"method": f"{request.method}",
|
||||
"path": f"{request.path}",
|
||||
"query_string": f"{request.query_string}",
|
||||
"raw_path": f"{request.raw_path}",
|
||||
"raw_headers": f'{",".join(map("{}".format, request.raw_headers))}',
|
||||
"body": f"{postbody}",
|
||||
},
|
||||
|
||||
"response": {
|
||||
"status": f"{response.status}",
|
||||
}
|
||||
}
|
||||
|
||||
confserverlog.debug(json.dumps(logall))
|
||||
if request._match_info.route.name not in self.excludelogging:
|
||||
|
||||
return response
|
||||
try:
|
||||
if request.content_length:
|
||||
if request.content_type == "application/x-www-form-urlencoded":
|
||||
postbody = await request.post()
|
||||
|
||||
except web.HTTPNotFound as notfound:
|
||||
confserverlog.debug("Request path {} not found".format(request.raw_path))
|
||||
requestlog = {
|
||||
"request": {
|
||||
"route_name": f"{request.match_info.route.name}",
|
||||
"method": f"{request.method}",
|
||||
"path": f"{request.path}",
|
||||
"query_string": f"{request.query_string}",
|
||||
"raw_path": f"{request.raw_path}",
|
||||
"raw_headers": f'{",".join(map("{}".format, request.raw_headers))}',
|
||||
"body": f"{postbody}",
|
||||
}
|
||||
}
|
||||
confserverlog.debug(json.dumps(requestlog))
|
||||
return notfound
|
||||
elif request.content_type == "application/json":
|
||||
try:
|
||||
postbody = json.loads(await request.text())
|
||||
except Exception as e:
|
||||
confserverlog.error("Request body not json: {} - {}".format(e, e.doc))
|
||||
postbody = e.doc
|
||||
|
||||
else:
|
||||
postbody = await request.post()
|
||||
else:
|
||||
postbody = None
|
||||
|
||||
except Exception as e:
|
||||
confserverlog.exception("{}".format(e))
|
||||
requestlog = {
|
||||
"request": {
|
||||
"route_name": f"{request.match_info.route.name}",
|
||||
"method": f"{request.method}",
|
||||
"path": f"{request.path}",
|
||||
"query_string": f"{request.query_string}",
|
||||
"raw_path": f"{request.raw_path}",
|
||||
"raw_headers": f'{",".join(map("{}".format, request.raw_headers))}',
|
||||
"body": f"{postbody}",
|
||||
}
|
||||
}
|
||||
confserverlog.debug(json.dumps(requestlog))
|
||||
return e
|
||||
response = await handler(request)
|
||||
if not "application/octet-stream" in response.content_type:
|
||||
logall = {
|
||||
"request": {
|
||||
"route_name": f"{request.match_info.route.name}",
|
||||
"method": f"{request.method}",
|
||||
"path": f"{request.path}",
|
||||
"query_string": f"{request.query_string}",
|
||||
"raw_path": f"{request.raw_path}",
|
||||
"raw_headers": f'{",".join(map("{}".format, request.raw_headers))}',
|
||||
"body": f"{postbody}",
|
||||
},
|
||||
|
||||
"response": {
|
||||
"response_body": f"{json.loads(response.body)}",
|
||||
"status": f"{response.status}",
|
||||
}
|
||||
}
|
||||
else:
|
||||
logall = {
|
||||
"request": {
|
||||
"route_name": f"{request.match_info.route.name}",
|
||||
"method": f"{request.method}",
|
||||
"path": f"{request.path}",
|
||||
"query_string": f"{request.query_string}",
|
||||
"raw_path": f"{request.raw_path}",
|
||||
"raw_headers": f'{",".join(map("{}".format, request.raw_headers))}',
|
||||
"body": f"{postbody}",
|
||||
},
|
||||
|
||||
"response": {
|
||||
"status": f"{response.status}",
|
||||
}
|
||||
}
|
||||
|
||||
confserverlog.debug(json.dumps(logall))
|
||||
|
||||
return response
|
||||
|
||||
except web.HTTPNotFound as notfound:
|
||||
confserverlog.debug("Request path {} not found".format(request.raw_path))
|
||||
requestlog = {
|
||||
"request": {
|
||||
"route_name": f"{request.match_info.route.name}",
|
||||
"method": f"{request.method}",
|
||||
"path": f"{request.path}",
|
||||
"query_string": f"{request.query_string}",
|
||||
"raw_path": f"{request.raw_path}",
|
||||
"raw_headers": f'{",".join(map("{}".format, request.raw_headers))}',
|
||||
"body": f"{postbody}",
|
||||
}
|
||||
}
|
||||
confserverlog.debug(json.dumps(requestlog))
|
||||
return notfound
|
||||
|
||||
except Exception as e:
|
||||
confserverlog.exception("{}".format(e))
|
||||
requestlog = {
|
||||
"request": {
|
||||
"route_name": f"{request.match_info.route.name}",
|
||||
"method": f"{request.method}",
|
||||
"path": f"{request.path}",
|
||||
"query_string": f"{request.query_string}",
|
||||
"raw_path": f"{request.raw_path}",
|
||||
"raw_headers": f'{",".join(map("{}".format, request.raw_headers))}',
|
||||
"body": f"{postbody}",
|
||||
}
|
||||
}
|
||||
confserverlog.debug(json.dumps(requestlog))
|
||||
return e
|
||||
|
||||
else:
|
||||
return await handler(request)
|
||||
|
||||
async def restart_Helper(self):
|
||||
|
||||
|
|
|
|||
|
|
@ -9,6 +9,9 @@ import pytest_aiohttp
|
|||
import pytest_asyncio
|
||||
import datetime, time
|
||||
from aiohttp import web
|
||||
import logging
|
||||
from testfixtures import LogCapture
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
|
||||
def create_confserver():
|
||||
|
|
@ -37,6 +40,31 @@ async def test_confserver_ssl():
|
|||
conf_server.confserver_app()
|
||||
asyncio.create_task(conf_server.start_server())
|
||||
|
||||
async def test_confserver_exceptions():
|
||||
with LogCapture() as l:
|
||||
|
||||
conf_server = bumper.ConfServer(("127.0.0.1", 8007), usessl=True)
|
||||
conf_server.confserver_app()
|
||||
conf_server.site = web.TCPSite
|
||||
|
||||
#bind permission
|
||||
conf_server.site.start = mock.Mock(side_effect=OSError(1, "error while attempting to bind on address ('127.0.0.1', 8007): permission denied"))
|
||||
await conf_server.start_server()
|
||||
|
||||
#asyncio Cancel
|
||||
conf_server.site = web.TCPSite
|
||||
conf_server.site.start = mock.Mock(side_effect=asyncio.CancelledError)
|
||||
await conf_server.start_server()
|
||||
|
||||
#general exception
|
||||
conf_server.site = web.TCPSite
|
||||
conf_server.site.start = mock.Mock(side_effect=Exception(1, "general"))
|
||||
await conf_server.start_server()
|
||||
|
||||
l.check_present(
|
||||
("confserver", "ERROR", "error while attempting to bind on address ('127.0.0.1', 8007): permission denied")
|
||||
)
|
||||
|
||||
|
||||
async def test_confserver_no_ssl():
|
||||
conf_server = bumper.ConfServer(("127.0.0.1", 111111), usessl=False)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue