From 99de3e2ebdb032537c2ea96a9896dac8772b3e99 Mon Sep 17 00:00:00 2001 From: Nicola Tarocco Date: Tue, 8 Feb 2022 10:24:11 +0100 Subject: [PATCH] Auth service: pass debug option as CLI arg * closes #237 --- app-config/auth-service/Dockerfile | 2 +- .../auth-service/auth_service/__init__.py | 18 ++++++------ .../auth-service/auth_service/__main__.py | 29 ++++++++++++++++--- 3 files changed, 35 insertions(+), 14 deletions(-) diff --git a/app-config/auth-service/Dockerfile b/app-config/auth-service/Dockerfile index 75ef9310..0a78f5f4 100644 --- a/app-config/auth-service/Dockerfile +++ b/app-config/auth-service/Dockerfile @@ -21,5 +21,5 @@ FROM registry.cern.ch/docker.io/library/debian COPY --from=conda /opt/app /opt/app CMD [ \ - "/opt/app/bin/python", "-m", "auth_service" \ + "/opt/app/bin/python", "-m", "auth_service", "--no-debug" \ ] diff --git a/app-config/auth-service/auth_service/__init__.py b/app-config/auth-service/auth_service/__init__.py index 6a1acfb2..74dbd5cc 100644 --- a/app-config/auth-service/auth_service/__init__.py +++ b/app-config/auth-service/auth_service/__init__.py @@ -10,15 +10,14 @@ import typing import aiohttp from keycloak.aio.realm import KeycloakRealm -import tornado.ioloop +from tornado.web import Application, RequestHandler import tornado.log -import tornado.web LOG = logging.getLogger(__name__) -class BaseHandler(tornado.web.RequestHandler): +class BaseHandler(RequestHandler): def set_session_cookie(self, session_data: dict, expiry_in_seconds: int) -> None: seconds_per_day = 60 * 60 * 24 self.set_secure_cookie( @@ -152,18 +151,19 @@ class MainHandler(BaseHandler): session = self.get_session_cookie() if session is None: return self.finish(""" - You are currently not logged in: Login + You are currently not logged in: Login """) else: return self.finish(f""" You are currently logged in as "{session['username']}": - Logout + Logout """) -def make_app(): - tornado.log.enable_pretty_logging() - return tornado.web.Application( +def make_app(debug: bool = False) -> Application: + if debug: + tornado.log.enable_pretty_logging() + return Application( [ (r"/", MainHandler), (r"/auth/probe", ProbeAuthentication), @@ -174,7 +174,7 @@ def make_app(): (r'/auth/logout', Logout), ], cookie_secret=os.environ['COOKIE_SECRET'], - debug=True, + debug=debug, oicd_server=os.environ['OIDC_SERVER'], oicd_realm=os.environ['OIDC_REALM'], client_id=os.environ['CLIENT_ID'], diff --git a/app-config/auth-service/auth_service/__main__.py b/app-config/auth-service/auth_service/__main__.py index f8e10d12..1dafbe7c 100644 --- a/app-config/auth-service/auth_service/__main__.py +++ b/app-config/auth-service/auth_service/__main__.py @@ -1,9 +1,30 @@ -import tornado.ioloop +import argparse + +from tornado.ioloop import IOLoop from . import make_app +def configure_parser(parser) -> argparse.ArgumentParser: + parser.add_argument( + "--no-debug", help="Don't enable debug mode", + action="store_false", + ) + parser.add_argument( + "--port", + help="The port to listen on", + default="8080" + ) + return parser + + +def main(): + parser = configure_parser(argparse.ArgumentParser()) + args = parser.parse_args() + app = make_app(debug=args.no_debug) + app.listen(args.port) + IOLoop.instance().start() + + if __name__ == "__main__": - app = make_app() - app.listen(8080) - tornado.ioloop.IOLoop.current().start() + main()