Skip to content

Commit

Permalink
add log endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
edenhaus committed Oct 1, 2022
1 parent 03e94c0 commit d759fc6
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
2 changes: 1 addition & 1 deletion bumper/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ async def start() -> None:
global mqtt_helperbot
mqtt_helperbot = HelperBot(bumper_listen, mqtt_listen_port)
global web_server
web_server = WebServer(web_server_bindings, bumper_proxy_web)
web_server = WebServer(web_server_bindings, bumper_proxy_web, bumper_debug)
global xmpp_server
xmpp_server = XMPPServer(bumper_listen, xmpp_listen_port)

Expand Down
36 changes: 32 additions & 4 deletions bumper/web/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from bumper.db import _db_get, bot_get, bot_remove, client_get, client_remove
from bumper.dns import get_resolver_with_public_nameserver
from bumper.util import get_logger
from bumper.web.middlewares import log_all_requests
from bumper.web.middlewares import CustomEncoder, log_all_requests
from bumper.web.plugins import add_plugins


Expand All @@ -39,6 +39,7 @@ def filter(self, record: logging.LogRecord) -> bool:
# Add logging filter above to aiohttp.access
logging.getLogger("aiohttp.access").addFilter(_AiohttpFilter())
_LOGGER_PROXY = logging.getLogger("web_proxy")
_LOGGER_WEB_LOG = logging.getLogger("web_log")


@dataclasses.dataclass(frozen=True)
Expand All @@ -54,7 +55,10 @@ class WebServer:
"""Web server."""

def __init__(
self, bindings: list[WebserverBinding] | WebserverBinding, proxy_mode: bool
self,
bindings: list[WebserverBinding] | WebserverBinding,
proxy_mode: bool,
debug: bool = False,
):
self._runners: list[web.AppRunner] = []

Expand All @@ -73,10 +77,10 @@ def __init__(
os.path.join(bumper.bumper_dir, "bumper", "web", "templates")
),
)
self._add_routes(proxy_mode)
self._add_routes(proxy_mode, debug)
self._app.freeze() # no modification allowed anymore

def _add_routes(self, proxy_mode: bool) -> None:
def _add_routes(self, proxy_mode: bool, debug: bool) -> None:
self._app.add_routes(
[
web.get("/bot/remove/{did}", self._handle_remove_bot),
Expand Down Expand Up @@ -105,6 +109,12 @@ def _add_routes(self, proxy_mode: bool) -> None:
web.post("/newauth.do", self._handle_newauth),
]
)
if debug:
self._app.add_routes(
[
web.post("/log", self._handle_log),
]
)
add_plugins(self._app)

async def start(self) -> None:
Expand Down Expand Up @@ -367,3 +377,21 @@ async def _handle_proxy(self, request: Request) -> Response:
_LOGGER_PROXY.exception("An exception occurred", exc_info=True)

raise HTTPInternalServerError

async def _handle_log(self, request: Request) -> Response:
to_log = {}
try:
to_log = {
"query_string": request.query_string,
"headers": set(request.headers.items()),
}
if request.content_length:
to_log["body"] = set(await request.post())
except Exception: # pylint: disable=broad-except
_LOGGER_WEB_LOG.exception(
"An exception occurred during logging the request.", exc_info=True
)
finally:
_LOGGER_WEB_LOG.debug(json.dumps(to_log, cls=CustomEncoder))

return web.Response()
4 changes: 3 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ async def helper_bot(mqtt_server: MQTTServer):

@pytest.fixture
async def webserver_client(aiohttp_client):
webserver = bumper.WebServer(WebserverBinding(HOST, WEBSERVER_PORT, False), False)
webserver = bumper.WebServer(
WebserverBinding(HOST, WEBSERVER_PORT, False), False, True
)
client = await aiohttp_client(webserver._app)

yield client
Expand Down

0 comments on commit d759fc6

Please sign in to comment.