Skip to content

Commit e5868d7

Browse files
Merge pull request #109 from mrvanes/master
Add custom logging microservice
2 parents 0ed1a4e + 353678b commit e5868d7

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module: satosa.micro_services.custom_logging.CustomLoggingService
2+
name: CustomLogginService
3+
config:
4+
log_target: 'var/custom.log'
5+
attrs: ['uid', 'eppn']
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
"""
2+
SATOSA microservice that outputs log in custom format.
3+
"""
4+
5+
from .base import ResponseMicroService
6+
from satosa.logging_util import satosa_logging
7+
from base64 import urlsafe_b64encode, urlsafe_b64decode
8+
9+
import json
10+
import copy
11+
import logging
12+
13+
logger = logging.getLogger(__name__)
14+
15+
class CustomLoggingService(ResponseMicroService):
16+
"""
17+
Use context and data object to create custom log output
18+
"""
19+
logprefix = "CUSTOM_LOGGING_SERVICE:"
20+
21+
def __init__(self, config, *args, **kwargs):
22+
super().__init__(*args, **kwargs)
23+
self.config = config
24+
25+
def process(self, context, data):
26+
logprefix = CustomLoggingService.logprefix
27+
28+
# Initialize the configuration to use as the default configuration
29+
# that is passed during initialization.
30+
config = self.config
31+
configClean = copy.deepcopy(config)
32+
33+
satosa_logging(logger, logging.DEBUG, "{} Using default configuration {}".format(logprefix, configClean), context.state)
34+
35+
# Find the entityID for the SP that initiated the flow and target IdP
36+
try:
37+
spEntityID = context.state.state_dict['SATOSA_BASE']['requester']
38+
idpEntityID = data.auth_info.issuer
39+
except KeyError as err:
40+
satosa_logging(logger, logging.ERROR, "{} Unable to determine the entityID's for the IdP or SP".format(logprefix), context.state)
41+
return super().process(context, data)
42+
43+
satosa_logging(logger, logging.DEBUG, "{} entityID for the SP requester is {}".format(logprefix, spEntityID), context.state)
44+
satosa_logging(logger, logging.ERROR, "{} entityID for the target IdP is {}".format(logprefix, idpEntityID), context.state)
45+
46+
# Obtain configuration details from the per-SP configuration or the default configuration
47+
try:
48+
if 'log_target' in config:
49+
log_target = config['log_target']
50+
else:
51+
log_target = self.config['log_target']
52+
53+
if 'attrs' in config:
54+
attrs = config['attrs']
55+
else:
56+
attrs = self.config['attrs']
57+
58+
59+
except KeyError as err:
60+
satosa_logging(logger, logging.ERROR, "{} Configuration '{}' is missing".format(logprefix, err), context.state)
61+
return super().process(context, data)
62+
63+
record = None
64+
65+
try:
66+
satosa_logging(logger, logging.DEBUG, "{} Using context {}".format(logprefix, context), context.state)
67+
satosa_logging(logger, logging.DEBUG, "{} Using data {}".format(logprefix, data.to_dict()), context.state)
68+
69+
# Open log_target file
70+
satosa_logging(logger, logging.DEBUG, "{} Opening log_target file {}".format(logprefix, log_target), context.state)
71+
loghandle = open(log_target,"a")
72+
73+
# This is where the logging magic happens
74+
log = {}
75+
log['router'] = context.state.state_dict['ROUTER']
76+
log['timestamp'] = data.auth_info.timestamp
77+
log['sessionid'] = context.state.state_dict['SESSION_ID']
78+
log['idp'] = idpEntityID
79+
log['sp'] = spEntityID
80+
log['attr'] = { key: data.to_dict()['attr'][key] for key in attrs }
81+
82+
print(json.dumps(log), file=loghandle, end="\n")
83+
84+
except Exception as err:
85+
satosa_logging(logger, logging.ERROR, "{} Caught exception: {0}".format(logprefix, err), None)
86+
return super().process(context, data)
87+
88+
else:
89+
satosa_logging(logger, logging.DEBUG, "{} Closing log_target file".format(logprefix), context.state)
90+
91+
# Close log_target file
92+
loghandle.close()
93+
94+
return super().process(context, data)

0 commit comments

Comments
 (0)