From 81a50cbca7ff0b0535463ed4aa21cf5d819d5cd7 Mon Sep 17 00:00:00 2001 From: dliu5 Date: Tue, 22 Nov 2022 17:36:16 +0800 Subject: [PATCH] Adjust some debug level to make consistent. Fix bug if user auth_token still not valid or obtained. Signed-off-by: dliu5 Change-Id: I29495cb60be43a0b3ee03c2e580eb5e05d8c46e7 --- o2common/authmw/authmiddleware.py | 24 ++++++++++++++++-------- o2common/authmw/authprov.py | 2 +- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/o2common/authmw/authmiddleware.py b/o2common/authmw/authmiddleware.py index cd9df4b..3141263 100644 --- a/o2common/authmw/authmiddleware.py +++ b/o2common/authmw/authmiddleware.py @@ -84,8 +84,9 @@ class authmiddleware(): self.app = app def __call__(self, environ, start_response): - logger.info(__name__ + 'authentication middleware') + logger.debug(__name__ + 'authentication middleware') req = Request(environ, populate_request=True, shallow=True) + auth_token = None try: auth_header = req.headers.get('Authorization', None) if auth_header: @@ -95,7 +96,7 @@ class authmiddleware(): # invoke underlying auth mdw to make k8s/keystone api ret = ad.authenticate(auth_token) if ret is True: - logger.info( + logger.debug( "auth success with oauth token: " + auth_token) try: return self.app(environ, start_response) @@ -123,9 +124,16 @@ class authmiddleware(): return _response_wrapper(environ, start_response, ex.dictize(), prb.serialize()) except Exception as ex: - logger.error('Internal exception happended {}'.format( - str(ex)), exc_info=True) - prb = AuthProblemDetails(500, 'Internal error.', req.path) - return \ - _internal_err_response_wrapper(environ, - start_response, prb.serialize()) + if auth_token: + logger.error('Internal exception happended {}'.format( + str(ex)), exc_info=True) + prb = AuthProblemDetails(500, 'Internal error.', req.path) + return \ + _internal_err_response_wrapper( + environ, start_response, prb.serialize()) + else: + logger.debug('Auth token missing or not obtained.') + ex = AuthRequiredExp('Bearer realm="Authentication Required"') + prb = AuthProblemDetails(401, ex.value, req.path) + return _response_wrapper(environ, start_response, + ex.dictize(), prb.serialize()) diff --git a/o2common/authmw/authprov.py b/o2common/authmw/authprov.py index 17c5349..c6f5646 100644 --- a/o2common/authmw/authprov.py +++ b/o2common/authmw/authprov.py @@ -109,7 +109,7 @@ class k8s_auth_provider(auth_definer): response = urllib.request.urlopen(req) data = json.load(response) if data['status']['authenticated'] is True: - logger.info("Authenticated.") + logger.debug("Authenticated.") return True except Exception as ex: strex = str(ex) -- 2.16.6