Skip to content

Commit 7d19b60

Browse files
Merge pull request #309 from sebulibah/frontends.saml2
Improve logging - satosa.frontends.saml2
2 parents 983b2d3 + 669c1a7 commit 7d19b60

File tree

1 file changed

+71
-37
lines changed

1 file changed

+71
-37
lines changed

src/satosa/frontends/saml2.py

Lines changed: 71 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@
3030
from satosa.base import SAMLBaseModule
3131
from satosa.context import Context
3232
from .base import FrontendModule
33-
from ..logging_util import satosa_logging
3433
from ..response import Response
3534
from ..response import ServiceError
3635
from ..saml_util import make_saml_response
3736
from satosa.exception import SATOSAError
3837
import satosa.util as util
3938

39+
import satosa.logging_util as lu
4040
from satosa.internal import InternalData
4141
from satosa.deprecated import saml_name_id_format_to_hash_type
4242
from satosa.deprecated import hash_type_to_saml_name_id_format
@@ -190,15 +190,19 @@ def _handle_authn_request(self, context, binding_in, idp):
190190
"""
191191
req_info = idp.parse_authn_request(context.request["SAMLRequest"], binding_in)
192192
authn_req = req_info.message
193-
satosa_logging(logger, logging.DEBUG, "%s" % authn_req, context.state)
193+
msg = "{}".format(authn_req)
194+
logline = lu.LOG_FMT.format(id=lu.get_session_id(context.state), message=msg)
195+
logger.debug(logline)
194196

195197
# keep the ForceAuthn value to be used by plugins
196198
context.decorate(Context.KEY_FORCE_AUTHN, authn_req.force_authn)
197199

198200
try:
199201
resp_args = idp.response_args(authn_req)
200202
except SAMLError as e:
201-
satosa_logging(logger, logging.ERROR, "Could not find necessary info about entity: %s" % e, context.state)
203+
msg = "Could not find necessary info about entity: {}".format(e)
204+
logline = lu.LOG_FMT.format(id=lu.get_session_id(context.state), message=msg)
205+
logger.error(logline)
202206
return ServiceError("Incorrect request from requester: %s" % e)
203207

204208
requester = resp_args["sp_entity_id"]
@@ -273,7 +277,9 @@ def _get_approved_attributes(self, idp, idp_policy, sp_entity_id, state):
273277
attribute_filter = list(idp_policy.restrict(all_attributes, sp_entity_id, idp.metadata).keys())
274278
break
275279
attribute_filter = self.converter.to_internal_filter(self.attribute_profile, attribute_filter)
276-
satosa_logging(logger, logging.DEBUG, "Filter: %s" % attribute_filter, state)
280+
msg = "Filter: {}".format(attribute_filter)
281+
logline = lu.LOG_FMT.format(id=lu.get_session_id(state), message=msg)
282+
logger.debug(logline)
277283
return attribute_filter
278284

279285
def _filter_attributes(self, idp, internal_response, context,):
@@ -343,8 +349,9 @@ def _handle_authn_response(self, context, internal_response, idp):
343349
name_qualifier=None,
344350
)
345351

346-
dbgmsg = "returning attributes %s" % json.dumps(ava)
347-
satosa_logging(logger, logging.DEBUG, dbgmsg, context.state)
352+
msg = "returning attributes {}".format(json.dumps(ava))
353+
logline = lu.LOG_FMT.format(id=lu.get_session_id(context.state), message=msg)
354+
logger.debug(logline)
348355

349356
policies = self.idp_config.get(
350357
'service', {}).get('idp', {}).get('policy', {})
@@ -376,22 +383,26 @@ def _handle_authn_response(self, context, internal_response, idp):
376383
try:
377384
args['sign_alg'] = getattr(xmldsig, sign_alg)
378385
except AttributeError as e:
379-
errmsg = "Unsupported sign algorithm %s" % sign_alg
380-
satosa_logging(logger, logging.ERROR, errmsg, context.state)
381-
raise Exception(errmsg) from e
386+
msg = "Unsupported sign algorithm {}".format(sign_alg)
387+
logline = lu.LOG_FMT.format(id=lu.get_session_id(context.state), message=msg)
388+
logger.error(logline)
389+
raise Exception(msg) from e
382390
else:
383-
dbgmsg = "signing with algorithm %s" % args['sign_alg']
384-
satosa_logging(logger, logging.DEBUG, dbgmsg, context.state)
391+
msg = "signing with algorithm {}".format(args['sign_alg'])
392+
logline = lu.LOG_FMT.format(id=lu.get_session_id(context.state), message=msg)
393+
logger.debug(logline)
385394

386395
try:
387396
args['digest_alg'] = getattr(xmldsig, digest_alg)
388397
except AttributeError as e:
389-
errmsg = "Unsupported digest algorithm %s" % digest_alg
390-
satosa_logging(logger, logging.ERROR, errmsg, context.state)
391-
raise Exception(errmsg) from e
398+
msg = "Unsupported digest algorithm {}".format(digest_alg)
399+
logline = lu.LOG_FMT.format(id=lu.get_session_id(context.state), message=msg)
400+
logger.error(logline)
401+
raise Exception(msg) from e
392402
else:
393-
dbgmsg = "using digest algorithm %s" % args['digest_alg']
394-
satosa_logging(logger, logging.DEBUG, dbgmsg, context.state)
403+
msg = "using digest algorithm {}".format(args['digest_alg'])
404+
logline = lu.LOG_FMT.format(id=lu.get_session_id(context.state), message=msg)
405+
logger.debug(logline)
395406

396407
resp = idp.create_authn_response(**args)
397408
http_args = idp.apply_binding(
@@ -426,7 +437,9 @@ def _handle_backend_error(self, exception, idp):
426437
http_args = idp.apply_binding(resp_args["binding"], str(error_resp), resp_args["destination"], relay_state,
427438
response=True)
428439

429-
satosa_logging(logger, logging.DEBUG, "HTTPargs: %s" % http_args, exception.state)
440+
msg = "HTTPSards: {}".format(http_args)
441+
logline = lu.LOG_FMT.format(id=lu.get_session_id(exception.state), message=msg)
442+
logger.debug(logline)
430443
return make_saml_response(resp_args["binding"], http_args)
431444

432445
def _metadata_endpoint(self, context):
@@ -438,7 +451,9 @@ def _metadata_endpoint(self, context):
438451
:param context: The current context
439452
:return: response with metadata
440453
"""
441-
satosa_logging(logger, logging.DEBUG, "Sending metadata response", context.state)
454+
msg = "Sending metadata response"
455+
logline = lu.LOG_FMT.format(id=lu.get_session_id(context.state), message=msg)
456+
logger.debug(logline)
442457
metadata_string = create_metadata_string(None, self.idp.config, 4, None, None, None, None,
443458
None).decode("utf-8")
444459
return Response(metadata_string, content="text/xml")
@@ -478,15 +493,21 @@ def _set_common_domain_cookie(self, internal_response, http_args, context):
478493
cookie = SimpleCookie(context.cookie)
479494
if '_saml_idp' in cookie:
480495
common_domain_cookie = cookie['_saml_idp']
481-
satosa_logging(logger, logging.DEBUG, "Found existing common domain cookie {}".format(common_domain_cookie), context.state)
496+
msg = "Found existing common domain cookie {}".format(common_domain_cookie)
497+
logline = lu.LOG_FMT.format(id=lu.get_session_id(context.state), message=msg)
498+
logger.debug(logline)
482499
space_separated_b64_idp_string = unquote(common_domain_cookie.value)
483500
b64_idp_list = space_separated_b64_idp_string.split()
484501
idp_list = [urlsafe_b64decode(b64_idp).decode('utf-8') for b64_idp in b64_idp_list]
485502
else:
486-
satosa_logging(logger, logging.DEBUG, "No existing common domain cookie found", context.state)
503+
msg = "No existing common domain cookie found"
504+
logline = lu.LOG_FMT.format(id=lu.get_session_id(context.state), message=msg)
505+
logger.debug(logline)
487506
idp_list = []
488507

489-
satosa_logging(logger, logging.DEBUG, "Common domain cookie list of IdPs is {}".format(idp_list), context.state)
508+
msg = "Common domain cookie list of IdPs is {}".format(idp_list)
509+
logline = lu.LOG_FMT.format(id=lu.get_session_id(context.state), message=msg)
510+
logger.debug(logline)
490511

491512
# Identity the current IdP just used for authentication in this flow.
492513
this_flow_idp = internal_response.auth_info.issuer
@@ -496,8 +517,12 @@ def _set_common_domain_cookie(self, internal_response, http_args, context):
496517

497518
# Append the current IdP.
498519
idp_list.append(this_flow_idp)
499-
satosa_logging(logger, logging.DEBUG, "Added IdP {} to common domain cookie list of IdPs".format(this_flow_idp), context.state)
500-
satosa_logging(logger, logging.DEBUG, "Common domain cookie list of IdPs is now {}".format(idp_list), context.state)
520+
msg = "Added IdP {} to common domain cookie list of IdPs".format(this_flow_idp)
521+
logline = lu.LOG_FMT.format(id=lu.get_session_id(context.state), message=msg)
522+
logger.debug(logline)
523+
msg = "Common domain cookie list of IdPs is now {}".format(idp_list)
524+
logline = lu.LOG_FMT.format(id=lu.get_session_id(context.state), message=msg)
525+
logger.debug(logline)
501526

502527
# Construct the cookie.
503528
b64_idp_list = [urlsafe_b64encode(idp.encode()).decode("utf-8") for idp in idp_list]
@@ -523,7 +548,9 @@ def _set_common_domain_cookie(self, internal_response, http_args, context):
523548
cookie['_saml_idp']['secure'] = True
524549

525550
# Set the cookie.
526-
satosa_logging(logger, logging.DEBUG, "Setting common domain cookie with {}".format(cookie.output()), context.state)
551+
msg = "Setting common domain cookie with {}".format(cookie.output())
552+
logline = lu.LOG_FMT.format(id=lu.get_session_id(context.state), message=msg)
553+
logger.debug(logline)
527554
http_args['headers'].append(tuple(cookie.output().split(": ", 1)))
528555

529556
def _build_idp_config_endpoints(self, config, providers):
@@ -840,10 +867,12 @@ def _get_co_name(self, context):
840867
"""
841868
try:
842869
co_name = context.state[self.name][self.KEY_CO_NAME]
843-
logger.debug("Found CO {} from state".format(co_name))
870+
logline = "Found CO {} from state".format(co_name)
871+
logger.debug(logline)
844872
except KeyError:
845873
co_name = self._get_co_name_from_path(context)
846-
logger.debug("Found CO {} from request path".format(co_name))
874+
logline = "Found CO {} from request path".format(co_name)
875+
logger.debug(logline)
847876

848877
return co_name
849878

@@ -985,9 +1014,9 @@ def _create_co_virtual_idp(self, context):
9851014
if co_name not in co_names:
9861015
msg = "CO {} not in configured list of COs {}".format(co_name,
9871016
co_names)
988-
satosa_logging(logger, logging.WARN, msg, context.state)
1017+
logline = lu.LOG_FMT.format(id=lu.get_session_id(context.state), message=msg)
1018+
logger.warn(logline)
9891019
raise SATOSAError(msg)
990-
9911020
# Make a copy of the general IdP config that we will then overwrite
9921021
# with mappings between SAML bindings and CO specific URL endpoints,
9931022
# and the entityID for the CO virtual IdP.
@@ -1038,27 +1067,30 @@ def _register_endpoints(self, backend_names):
10381067

10391068
# Create a regex pattern that will match any of the backend names.
10401069
backend_url_pattern = "|^".join(backend_names)
1041-
logger.debug("Input backend names are {}".format(backend_names))
1042-
logger.debug("Created backend regex '{}'".format(backend_url_pattern))
1070+
logline = "Input backend names are {}".format(backend_names)
1071+
logger.debug(logline)
1072+
logline = "Created backend regex '{}'".format(backend_url_pattern)
1073+
logger.debug(logline)
10431074

10441075
# Hold a list of tuples containing URL regex patterns and the callables
10451076
# that handle them.
10461077
url_to_callable_mappings = []
10471078

10481079
# Loop over IdP endpoint categories, e.g., single_sign_on_service.
10491080
for endpoint_category in self.endpoints:
1050-
logger.debug("Examining endpoint category {}".format(
1051-
endpoint_category))
1081+
logline = "Examining endpoint category {}".format(endpoint_category)
1082+
logger.debug(logline)
10521083

10531084
# For each endpoint category loop of the bindings and their
10541085
# assigned endpoints.
10551086
for binding, endpoint in self.endpoints[endpoint_category].items():
1056-
logger.debug("Found binding {} and endpoint {}".format(binding,
1057-
endpoint))
1087+
logline = "Found binding {} and endpoint {}".format(binding, endpoint)
1088+
logger.debug(logline)
10581089

10591090
# Parse out the path from the endpoint.
10601091
endpoint_path = urlparse(endpoint).path
1061-
logger.debug("Using path {}".format(endpoint_path))
1092+
logline = "Using path {}".format(endpoint_path)
1093+
logger.debug(logline)
10621094

10631095
# Use the backend URL pattern and the endpoint path to create
10641096
# a regex that will match and that includes a pattern for
@@ -1067,7 +1099,8 @@ def _register_endpoints(self, backend_names):
10671099
backend_url_pattern,
10681100
co_name_pattern,
10691101
endpoint_path)
1070-
logger.debug("Created URL regex {}".format(regex_pattern))
1102+
logline = "Created URL regex {}".format(regex_pattern)
1103+
logger.debug(logline)
10711104

10721105
# Map the regex pattern to a callable.
10731106
the_callable = functools.partial(self.handle_authn_request,
@@ -1076,6 +1109,7 @@ def _register_endpoints(self, backend_names):
10761109

10771110
mapping = (regex_pattern, the_callable)
10781111
url_to_callable_mappings.append(mapping)
1079-
logger.debug("Adding mapping {}".format(mapping))
1112+
logline = "Adding mapping {}".format(mapping)
1113+
logger.debug(logline)
10801114

10811115
return url_to_callable_mappings

0 commit comments

Comments
 (0)