confserver revamp #88
2 changed files with 120 additions and 86 deletions
|
|
@ -47,18 +47,19 @@ class ConfServer:
|
||||||
self.app = None
|
self.app = None
|
||||||
self.site = None
|
self.site = None
|
||||||
self.runner = None
|
self.runner = None
|
||||||
|
self.excludelogging = ["base", "remove-bot", "remove-client", "restart-service"]
|
||||||
|
|
||||||
def get_milli_time(self, timetoconvert):
|
def get_milli_time(self, timetoconvert):
|
||||||
return int(round(timetoconvert * 1000))
|
return int(round(timetoconvert * 1000))
|
||||||
|
|
||||||
def confserver_app(self):
|
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")))
|
aiohttp_jinja2.setup(self.app, loader=jinja2.FileSystemLoader(os.path.join(bumper.data_dir, "web","templates")))
|
||||||
|
|
||||||
self.app.add_routes(
|
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("/bot/remove/{did}", self.handle_RemoveBot, name='remove-bot'),
|
||||||
web.get("/client/remove/{resource}", self.handle_RemoveClient, name='remove-client'),
|
web.get("/client/remove/{resource}", self.handle_RemoveClient, name='remove-client'),
|
||||||
web.get("/restart_{service}", self.handle_RestartService, name='restart-service'),
|
web.get("/restart_{service}", self.handle_RestartService, name='restart-service'),
|
||||||
|
|
@ -240,94 +241,99 @@ class ConfServer:
|
||||||
|
|
||||||
@web.middleware
|
@web.middleware
|
||||||
async def log_all_requests(self, request, handler):
|
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":
|
if request._match_info.route.name not in self.excludelogging:
|
||||||
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))
|
|
||||||
|
|
||||||
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:
|
elif request.content_type == "application/json":
|
||||||
confserverlog.debug("Request path {} not found".format(request.raw_path))
|
try:
|
||||||
requestlog = {
|
postbody = json.loads(await request.text())
|
||||||
"request": {
|
except Exception as e:
|
||||||
"route_name": f"{request.match_info.route.name}",
|
confserverlog.error("Request body not json: {} - {}".format(e, e.doc))
|
||||||
"method": f"{request.method}",
|
postbody = e.doc
|
||||||
"path": f"{request.path}",
|
|
||||||
"query_string": f"{request.query_string}",
|
else:
|
||||||
"raw_path": f"{request.raw_path}",
|
postbody = await request.post()
|
||||||
"raw_headers": f'{",".join(map("{}".format, request.raw_headers))}',
|
else:
|
||||||
"body": f"{postbody}",
|
postbody = None
|
||||||
}
|
|
||||||
}
|
|
||||||
confserverlog.debug(json.dumps(requestlog))
|
|
||||||
return notfound
|
|
||||||
|
|
||||||
except Exception as e:
|
response = await handler(request)
|
||||||
confserverlog.exception("{}".format(e))
|
if not "application/octet-stream" in response.content_type:
|
||||||
requestlog = {
|
logall = {
|
||||||
"request": {
|
"request": {
|
||||||
"route_name": f"{request.match_info.route.name}",
|
"route_name": f"{request.match_info.route.name}",
|
||||||
"method": f"{request.method}",
|
"method": f"{request.method}",
|
||||||
"path": f"{request.path}",
|
"path": f"{request.path}",
|
||||||
"query_string": f"{request.query_string}",
|
"query_string": f"{request.query_string}",
|
||||||
"raw_path": f"{request.raw_path}",
|
"raw_path": f"{request.raw_path}",
|
||||||
"raw_headers": f'{",".join(map("{}".format, request.raw_headers))}',
|
"raw_headers": f'{",".join(map("{}".format, request.raw_headers))}',
|
||||||
"body": f"{postbody}",
|
"body": f"{postbody}",
|
||||||
}
|
},
|
||||||
}
|
|
||||||
confserverlog.debug(json.dumps(requestlog))
|
"response": {
|
||||||
return e
|
"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):
|
async def restart_Helper(self):
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,9 @@ import pytest_aiohttp
|
||||||
import pytest_asyncio
|
import pytest_asyncio
|
||||||
import datetime, time
|
import datetime, time
|
||||||
from aiohttp import web
|
from aiohttp import web
|
||||||
|
import logging
|
||||||
|
from testfixtures import LogCapture
|
||||||
|
from unittest.mock import MagicMock
|
||||||
|
|
||||||
|
|
||||||
def create_confserver():
|
def create_confserver():
|
||||||
|
|
@ -37,6 +40,31 @@ async def test_confserver_ssl():
|
||||||
conf_server.confserver_app()
|
conf_server.confserver_app()
|
||||||
asyncio.create_task(conf_server.start_server())
|
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():
|
async def test_confserver_no_ssl():
|
||||||
conf_server = bumper.ConfServer(("127.0.0.1", 111111), usessl=False)
|
conf_server = bumper.ConfServer(("127.0.0.1", 111111), usessl=False)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue