15
15
from ldap3 .core .exceptions import LDAPException
16
16
17
17
from satosa .exception import SATOSAError
18
- from satosa .logging_util import satosa_logging
19
18
from satosa .micro_services .base import ResponseMicroService
20
19
from satosa .response import Redirect
21
20
22
-
21
+ import satosa . logging_util as lu
23
22
logger = logging .getLogger (__name__ )
24
23
25
24
KEY_FOUND_LDAP_RECORD = "ldap_attribute_store_found_record"
@@ -66,15 +65,15 @@ def __init__(self, config, *args, **kwargs):
66
65
67
66
if "default" in config and "" in config :
68
67
msg = """Use either 'default' or "" in config but not both"""
69
- satosa_logging ( logger , logging . ERROR , msg , None )
68
+ logger . error ( msg )
70
69
raise LdapAttributeStoreError (msg )
71
70
72
71
if "" in config :
73
72
config ["default" ] = config .pop ("" )
74
73
75
74
if "default" not in config :
76
75
msg = "No default configuration is present"
77
- satosa_logging ( logger , logging . ERROR , msg , None )
76
+ logger . error ( msg )
78
77
raise LdapAttributeStoreError (msg )
79
78
80
79
self .config = {}
@@ -88,7 +87,7 @@ def __init__(self, config, *args, **kwargs):
88
87
for sp in sp_list :
89
88
if not isinstance (config [sp ], dict ):
90
89
msg = "Configuration value for {} must be a dictionary"
91
- satosa_logging ( logger , logging . ERROR , msg , None )
90
+ logger . error ( msg )
92
91
raise LdapAttributeStoreError (msg )
93
92
94
93
# Initialize configuration using module defaults then update
@@ -111,28 +110,28 @@ def __init__(self, config, *args, **kwargs):
111
110
if connection_params in connections :
112
111
sp_config ["connection" ] = connections [connection_params ]
113
112
msg = "Reusing LDAP connection for SP {}" .format (sp )
114
- satosa_logging ( logger , logging . DEBUG , msg , None )
113
+ logger . debug ( msg )
115
114
else :
116
115
try :
117
116
connection = self ._ldap_connection_factory (sp_config )
118
117
connections [connection_params ] = connection
119
118
sp_config ["connection" ] = connection
120
119
msg = "Created new LDAP connection for SP {}" .format (sp )
121
- satosa_logging ( logger , logging . DEBUG , msg , None )
120
+ logger . debug ( msg )
122
121
except LdapAttributeStoreError :
123
122
# It is acceptable to not have a default LDAP connection
124
123
# but all SP overrides must have a connection, either
125
124
# inherited from the default or directly configured.
126
125
if sp != "default" :
127
126
msg = "No LDAP connection can be initialized for SP {}"
128
127
msg = msg .format (sp )
129
- satosa_logging ( logger , logging . ERROR , msg , None )
128
+ logger . error ( msg )
130
129
raise LdapAttributeStoreError (msg )
131
130
132
131
self .config [sp ] = sp_config
133
132
134
133
msg = "LDAP Attribute Store microservice initialized"
135
- satosa_logging ( logger , logging . INFO , msg , None )
134
+ logger . info ( msg )
136
135
137
136
def _construct_filter_value (
138
137
self , candidate , name_id_value , name_id_format , issuer , attributes
@@ -176,7 +175,7 @@ def _construct_filter_value(
176
175
for attr_value in [attributes .get (identifier_name )]
177
176
]
178
177
msg = "Found candidate values {}" .format (values )
179
- satosa_logging ( logger , logging . DEBUG , msg , None )
178
+ logger . debug ( msg )
180
179
181
180
# If one of the configured identifier names is name_id then if there is
182
181
# also a configured name_id_format add the value for the NameID of that
@@ -190,7 +189,7 @@ def _construct_filter_value(
190
189
and candidate_name_id_format == name_id_format
191
190
):
192
191
msg = "IdP asserted NameID {}" .format (name_id_value )
193
- satosa_logging ( logger , logging . DEBUG , msg , None )
192
+ logger . debug ( msg )
194
193
candidate_nameid_value = name_id_value
195
194
196
195
# Only add the NameID value asserted by the IdP if it is not
@@ -201,18 +200,18 @@ def _construct_filter_value(
201
200
if candidate_nameid_value not in values :
202
201
msg = "Added NameID {} to candidate values"
203
202
msg = msg .format (candidate_nameid_value )
204
- satosa_logging ( logger , logging . DEBUG , msg , None )
203
+ logger . debug ( msg )
205
204
values .append (candidate_nameid_value )
206
205
else :
207
206
msg = "NameID {} value also asserted as attribute value"
208
207
msg = msg .format (candidate_nameid_value )
209
- satosa_logging ( logger , logging . WARN , msg , None )
208
+ logger . warning ( msg )
210
209
211
210
# If no value was asserted by the IdP for one of the configured list of
212
211
# identifier names for this candidate then go onto the next candidate.
213
212
if None in values :
214
213
msg = "Candidate is missing value so skipping"
215
- satosa_logging ( logger , logging . DEBUG , msg , None )
214
+ logger . debug ( msg )
216
215
return None
217
216
218
217
# All values for the configured list of attribute names are present
@@ -225,14 +224,14 @@ def _construct_filter_value(
225
224
else candidate ["add_scope" ]
226
225
)
227
226
msg = "Added scope {} to values" .format (scope )
228
- satosa_logging ( logger , logging . DEBUG , msg , None )
227
+ logger . debug ( msg )
229
228
values .append (scope )
230
229
231
230
# Concatenate all values to create the filter value.
232
231
value = "" .join (values )
233
232
234
233
msg = "Constructed filter value {}" .format (value )
235
- satosa_logging ( logger , logging . DEBUG , msg , None )
234
+ logger . debug ( msg )
236
235
237
236
return value
238
237
@@ -283,13 +282,13 @@ def _ldap_connection_factory(self, config):
283
282
server = ldap3 .Server (** args )
284
283
285
284
msg = "Creating a new LDAP connection"
286
- satosa_logging ( logger , logging . DEBUG , msg , None )
285
+ logger . debug ( msg )
287
286
288
287
msg = "Using LDAP URL {}" .format (ldap_url )
289
- satosa_logging ( logger , logging . DEBUG , msg , None )
288
+ logger . debug ( msg )
290
289
291
290
msg = "Using bind DN {}" .format (bind_dn )
292
- satosa_logging ( logger , logging . DEBUG , msg , None )
291
+ logger . debug ( msg )
293
292
294
293
auto_bind_string = config ["auto_bind" ]
295
294
auto_bind_map = {
@@ -309,9 +308,9 @@ def _ldap_connection_factory(self, config):
309
308
310
309
if client_strategy == ldap3 .REUSABLE :
311
310
msg = "Using pool size {}" .format (pool_size )
312
- satosa_logging ( logger , logging . DEBUG , msg , None )
311
+ logger . debug ( msg )
313
312
msg = "Using pool keep alive {}" .format (pool_keepalive )
314
- satosa_logging ( logger , logging . DEBUG , msg , None )
313
+ logger . debug ( msg )
315
314
316
315
try :
317
316
connection = ldap3 .Connection (
@@ -327,16 +326,16 @@ def _ldap_connection_factory(self, config):
327
326
pool_keepalive = pool_keepalive ,
328
327
)
329
328
msg = "Successfully connected to LDAP server"
330
- satosa_logging ( logger , logging . DEBUG , msg , None )
329
+ logger . debug ( msg )
331
330
332
331
except LDAPException as e :
333
332
msg = "Caught exception when connecting to LDAP server: {}"
334
333
msg = msg .format (e )
335
- satosa_logging ( logger , logging . ERROR , msg , None )
334
+ logger . error ( msg )
336
335
raise LdapAttributeStoreError (msg )
337
336
338
337
msg = "Successfully connected to LDAP server"
339
- satosa_logging ( logger , logging . DEBUG , msg , None )
338
+ logger . debug ( msg )
340
339
341
340
return connection
342
341
@@ -348,7 +347,7 @@ def _populate_attributes(self, config, record):
348
347
ldap_attributes = record .get ("attributes" , None )
349
348
if not ldap_attributes :
350
349
msg = "No attributes returned with LDAP record"
351
- satosa_logging ( logger , logging . DEBUG , msg , None )
350
+ logger . debug ( msg )
352
351
return
353
352
354
353
ldap_to_internal_map = (
@@ -374,7 +373,7 @@ def _populate_attributes(self, config, record):
374
373
)
375
374
msg = "Recording internal attribute {} with values {}"
376
375
msg = msg .format (internal_attr , attributes [internal_attr ])
377
- satosa_logging ( logger , logging . DEBUG , msg , None )
376
+ logger . debug ( msg )
378
377
379
378
return attributes
380
379
@@ -408,12 +407,14 @@ def process(self, context, data):
408
407
"issuer" : issuer ,
409
408
"config" : self ._filter_config (config ),
410
409
}
411
- satosa_logging (logger , logging .DEBUG , msg , context .state )
410
+ logline = lu .LOG_FMT .format (id = lu .get_session_id (context .state ), message = msg )
411
+ logger .debug (logline )
412
412
413
413
# Ignore this SP entirely if so configured.
414
414
if config ["ignore" ]:
415
415
msg = "Ignoring SP {}" .format (requester )
416
- satosa_logging (logger , logging .INFO , msg , context .state )
416
+ logline = lu .LOG_FMT .format (id = lu .get_session_id (context .state ), message = msg )
417
+ logger .info (logline )
417
418
return super ().process (context , data )
418
419
419
420
# The list of values for the LDAP search filters that will be tried in
@@ -437,7 +438,8 @@ def process(self, context, data):
437
438
if filter_value
438
439
]
439
440
msg = {"message" : "Search filters" , "filter_values" : filter_values }
440
- satosa_logging (logger , logging .DEBUG , msg , context .state )
441
+ logline = lu .LOG_FMT .format (id = lu .get_session_id (context .state ), message = msg )
442
+ logger .debug (logline )
441
443
442
444
# Initialize an empty LDAP record. The first LDAP record found using
443
445
# the ordered # list of search filter values will be the record used.
@@ -459,7 +461,8 @@ def process(self, context, data):
459
461
"message" : "LDAP query with constructed search filter" ,
460
462
"search filter" : search_filter ,
461
463
}
462
- satosa_logging (logger , logging .DEBUG , msg , context .state )
464
+ logline = lu .LOG_FMT .format (id = lu .get_session_id (context .state ), message = msg )
465
+ logger .debug (logline )
463
466
464
467
attributes = (
465
468
config ["query_return_attributes" ]
@@ -480,13 +483,15 @@ def process(self, context, data):
480
483
exp_msg = "Caught unhandled exception: {}" .format (err )
481
484
482
485
if exp_msg :
483
- satosa_logging (logger , logging .ERROR , exp_msg , context .state )
486
+ logline = lu .LOG_FMT .format (id = lu .get_session_id (context .state ), message = exp_msg )
487
+ logger .error (logline )
484
488
return super ().process (context , data )
485
489
486
490
if not results :
487
491
msg = "Querying LDAP server: No results for {}."
488
492
msg = msg .format (filter_val )
489
- satosa_logging (logger , logging .DEBUG , msg , context .state )
493
+ logline = lu .LOG_FMT .format (id = lu .get_session_id (context .state ), message = msg )
494
+ logger .debug (logline )
490
495
continue
491
496
492
497
if isinstance (results , bool ):
@@ -495,17 +500,20 @@ def process(self, context, data):
495
500
responses = connection .get_response (results )[0 ]
496
501
497
502
msg = "Done querying LDAP server"
498
- satosa_logging (logger , logging .DEBUG , msg , context .state )
503
+ logline = lu .LOG_FMT .format (id = lu .get_session_id (context .state ), message = msg )
504
+ logger .debug (logline )
499
505
msg = "LDAP server returned {} records" .format (len (responses ))
500
- satosa_logging (logger , logging .INFO , msg , context .state )
506
+ logline = lu .LOG_FMT .format (id = lu .get_session_id (context .state ), message = msg )
507
+ logger .info (logline )
501
508
502
509
# For now consider only the first record found (if any).
503
510
if len (responses ) > 0 :
504
511
if len (responses ) > 1 :
505
512
msg = "LDAP server returned {} records using search filter"
506
513
msg = msg + " value {}"
507
514
msg = msg .format (len (responses ), filter_val )
508
- satosa_logging (logger , logging .WARN , msg , context .state )
515
+ logline = lu .LOG_FMT .format (id = lu .get_session_id (context .state ), message = msg )
516
+ logger .warning (logline )
509
517
record = responses [0 ]
510
518
break
511
519
@@ -514,7 +522,8 @@ def process(self, context, data):
514
522
if config ["clear_input_attributes" ]:
515
523
msg = "Clearing values for these input attributes: {}"
516
524
msg = msg .format (data .attributes )
517
- satosa_logging (logger , logging .DEBUG , msg , context .state )
525
+ logline = lu .LOG_FMT .format (id = lu .get_session_id (context .state ), message = msg )
526
+ logger .debug (logline )
518
527
data .attributes = {}
519
528
520
529
# This adapts records with different search and connection strategy
@@ -538,7 +547,8 @@ def process(self, context, data):
538
547
"DN" : record ["dn" ],
539
548
"attributes" : record ["attributes" ],
540
549
}
541
- satosa_logging (logger , logging .DEBUG , msg , context .state )
550
+ logline = lu .LOG_FMT .format (id = lu .get_session_id (context .state ), message = msg )
551
+ logger .debug (logline )
542
552
543
553
# Populate attributes as configured.
544
554
new_attrs = self ._populate_attributes (config , record )
@@ -555,16 +565,18 @@ def process(self, context, data):
555
565
if user_ids :
556
566
data .subject_id = "" .join (user_ids )
557
567
msg = "NameID value is {}" .format (data .subject_id )
558
- satosa_logging ( logger , logging . DEBUG , msg , None )
568
+ logger . debug ( msg )
559
569
560
570
# Add the record to the context so that later microservices
561
571
# may use it if required.
562
572
context .decorate (KEY_FOUND_LDAP_RECORD , record )
563
573
msg = "Added record {} to context" .format (record )
564
- satosa_logging (logger , logging .DEBUG , msg , context .state )
574
+ logline = lu .LOG_FMT .format (id = lu .get_session_id (context .state ), message = msg )
575
+ logger .debug (logline )
565
576
else :
566
577
msg = "No record found in LDAP so no attributes will be added"
567
- satosa_logging (logger , logging .WARN , msg , context .state )
578
+ logline = lu .LOG_FMT .format (id = lu .get_session_id (context .state ), message = msg )
579
+ logger .warning (msg )
568
580
on_ldap_search_result_empty = config ["on_ldap_search_result_empty" ]
569
581
if on_ldap_search_result_empty :
570
582
# Redirect to the configured URL with
@@ -578,9 +590,11 @@ def process(self, context, data):
578
590
encoded_idp_entity_id ,
579
591
)
580
592
msg = "Redirecting to {}" .format (url )
581
- satosa_logging (logger , logging .INFO , msg , context .state )
593
+ logline = lu .LOG_FMT .format (id = lu .get_session_id (context .state ), message = msg )
594
+ logger .info (msg )
582
595
return Redirect (url )
583
596
584
597
msg = "Returning data.attributes {}" .format (data .attributes )
585
- satosa_logging (logger , logging .DEBUG , msg , context .state )
598
+ logline = lu .LOG_FMT .format (id = lu .get_session_id (context .state ), message = msg )
599
+ logger .debug (msg )
586
600
return super ().process (context , data )
0 commit comments