-
Notifications
You must be signed in to change notification settings - Fork 125
/
Copy pathldap_attribute_store.py
618 lines (528 loc) · 23.5 KB
/
ldap_attribute_store.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
"""
SATOSA microservice that uses an identifier asserted by
the home organization SAML IdP as a key to search an LDAP
directory for a record and then consume attributes from
the record and assert them to the receiving SP.
"""
import copy
import logging
import random
import string
import urllib
import ldap3
from ldap3.core.exceptions import LDAPException
import satosa.logging_util as lu
from satosa.exception import SATOSAError
from satosa.micro_services.base import ResponseMicroService
from satosa.response import Redirect
from satosa.frontends.saml2 import SAMLVirtualCoFrontend
from satosa.routing import STATE_KEY as ROUTING_STATE_KEY
logger = logging.getLogger(__name__)
KEY_FOUND_LDAP_RECORD = "ldap_attribute_store_found_record"
class LdapAttributeStoreError(SATOSAError):
"""
LDAP attribute store error
"""
class LdapAttributeStore(ResponseMicroService):
"""
Use identifier provided by the backend authentication service
to lookup a person record in LDAP and obtain attributes
to assert about the user to the frontend receiving service.
"""
config_defaults = {
"bind_dn": None,
"bind_password": None,
"clear_input_attributes": False,
"ignore": False,
"ldap_identifier_attribute": None,
"ldap_url": None,
"ldap_to_internal_map": None,
"on_ldap_search_result_empty": None,
"ordered_identifier_candidates": None,
"overwrite_existing_attributes": True,
"search_base": None,
"query_return_attributes": None,
"search_return_attributes": None,
"user_id_from_attrs": [],
"read_only": True,
"version": 3,
"auto_bind": "AUTO_BIND_TLS_BEFORE_BIND",
"client_strategy": "REUSABLE",
"pool_size": 10,
"pool_keepalive": 10,
}
def __init__(self, config, *args, **kwargs):
super().__init__(*args, **kwargs)
if "default" in config and "" in config:
msg = """Use either 'default' or "" in config but not both"""
logger.error(msg)
raise LdapAttributeStoreError(msg)
if "" in config:
config["default"] = config.pop("")
if "default" not in config:
msg = "No default configuration is present"
logger.error(msg)
raise LdapAttributeStoreError(msg)
self.config = {}
# Process the default configuration first then any per-SP overrides.
sp_list = ["default"]
sp_list.extend([key for key in config.keys() if key != "default"])
connections = {}
for sp in sp_list:
if not isinstance(config[sp], dict):
msg = "Configuration value for {} must be a dictionary"
logger.error(msg)
raise LdapAttributeStoreError(msg)
# Initialize configuration using module defaults then update
# with configuration defaults and then per-SP overrides.
# sp_config = copy.deepcopy(LdapAttributeStore.config_defaults)
sp_config = copy.deepcopy(self.config_defaults)
if "default" in self.config:
sp_config.update(self.config["default"])
sp_config.update(config[sp])
# Tuple to index existing LDAP connections so they can be
# re-used if there are no changes in parameters.
connection_params = (
sp_config["bind_dn"],
sp_config["bind_password"],
sp_config["ldap_url"],
sp_config["search_base"],
)
if connection_params in connections:
sp_config["connection"] = connections[connection_params]
msg = "Reusing LDAP connection for SP {}".format(sp)
logger.debug(msg)
else:
try:
connection = self._ldap_connection_factory(sp_config)
connections[connection_params] = connection
sp_config["connection"] = connection
msg = "Created new LDAP connection for SP {}".format(sp)
logger.debug(msg)
except LdapAttributeStoreError:
# It is acceptable to not have a default LDAP connection
# but all SP overrides must have a connection, either
# inherited from the default or directly configured.
if sp != "default":
msg = "No LDAP connection can be initialized for SP {}"
msg = msg.format(sp)
logger.error(msg)
raise LdapAttributeStoreError(msg)
self.config[sp] = sp_config
msg = "LDAP Attribute Store microservice initialized"
logger.info(msg)
def _construct_filter_value(
self, candidate, name_id_value, name_id_format, issuer, attributes
):
"""
Construct and return a LDAP directory search filter value from the
candidate identifier.
Argument 'canidate' is a dictionary with one required key and
two optional keys:
key required value
--------------- -------- ---------------------------------
attribute_names Y list of identifier names
name_id_format N NameID format (string)
add_scope N "issuer_entityid" or other string
Argument 'data' is that object passed into the microservice
method process().
If the attribute_names list consists of more than one identifier
name then the values of the identifiers will be concatenated together
to create the filter value.
If one of the identifier names in the attribute_names is the string
'name_id' then the NameID value with format name_id_format
will be concatenated to the filter value.
If the add_scope key is present with value 'issuer_entityid' then the
entityID for the IdP will be concatenated to "scope" the value. If the
string is any other value it will be directly concatenated.
"""
# Get the values configured list of identifier names for this candidate
# and substitute None if there are no values for a configured
# identifier.
values = [
attr_value[0] if isinstance(attr_value, list) else attr_value
for identifier_name in candidate["attribute_names"]
for attr_value in [attributes.get(identifier_name)]
]
msg = "Found candidate values {}".format(values)
logger.debug(msg)
# If one of the configured identifier names is name_id then if there is
# also a configured name_id_format add the value for the NameID of that
# format if it was asserted by the IdP or else add the value None.
if "name_id" in candidate["attribute_names"]:
candidate_nameid_value = None
candidate_name_id_format = candidate.get("name_id_format")
if (
name_id_value
and candidate_name_id_format
and candidate_name_id_format == name_id_format
):
msg = "IdP asserted NameID {}".format(name_id_value)
logger.debug(msg)
candidate_nameid_value = name_id_value
# Only add the NameID value asserted by the IdP if it is not
# already in the list of values. This is necessary because some
# non-compliant IdPs have been known, for example, to assert the
# value of eduPersonPrincipalName in the value for SAML2 persistent
# NameID as well as asserting eduPersonPrincipalName.
if candidate_nameid_value not in values:
msg = "Added NameID {} to candidate values"
msg = msg.format(candidate_nameid_value)
logger.debug(msg)
values.append(candidate_nameid_value)
else:
msg = "NameID {} value also asserted as attribute value"
msg = msg.format(candidate_nameid_value)
logger.warning(msg)
# If no value was asserted by the IdP for one of the configured list of
# identifier names for this candidate then go onto the next candidate.
if None in values:
msg = "Candidate is missing value so skipping"
logger.debug(msg)
return None
# All values for the configured list of attribute names are present
# so we can create a value. Add a scope if configured
# to do so.
if "add_scope" in candidate:
scope = (
issuer
if candidate["add_scope"] == "issuer_entityid"
else candidate["add_scope"]
)
msg = "Added scope {} to values".format(scope)
logger.debug(msg)
values.append(scope)
# Concatenate all values to create the filter value.
value = "".join(values)
msg = "Constructed filter value {}".format(value)
logger.debug(msg)
return value
def _filter_config(self, config, fields=None):
"""
Filter sensitive details like passwords from a configuration
dictionary.
"""
filter_fields_default = ["bind_password", "connection"]
filter_fields = fields or filter_fields_default
result = {
field: "<hidden>" if field in filter_fields else value
for field, value in config.items()
}
return result
def _ldap_connection_factory(self, config):
"""
Use the input configuration to instantiate and return
a ldap3 Connection object.
"""
ldap_url = config["ldap_url"]
bind_dn = config["bind_dn"]
bind_password = config["bind_password"]
if not ldap_url:
raise LdapAttributeStoreError("ldap_url is not configured")
if not bind_dn:
raise LdapAttributeStoreError("bind_dn is not configured")
if not bind_password:
raise LdapAttributeStoreError("bind_password is not configured")
client_strategy_string = config["client_strategy"]
client_strategy_map = {
"SYNC": ldap3.SYNC,
"ASYNC": ldap3.ASYNC,
"LDIF": ldap3.LDIF,
"RESTARTABLE": ldap3.RESTARTABLE,
"REUSABLE": ldap3.REUSABLE,
"MOCK_SYNC": ldap3.MOCK_SYNC
}
client_strategy = client_strategy_map[client_strategy_string]
args = {'host': config["ldap_url"]}
if client_strategy == ldap3.MOCK_SYNC:
args['get_info'] = ldap3.OFFLINE_SLAPD_2_4
server = ldap3.Server(**args)
msg = "Creating a new LDAP connection"
logger.debug(msg)
msg = "Using LDAP URL {}".format(ldap_url)
logger.debug(msg)
msg = "Using bind DN {}".format(bind_dn)
logger.debug(msg)
auto_bind_string = config["auto_bind"]
auto_bind_map = {
"AUTO_BIND_NONE": ldap3.AUTO_BIND_NONE,
"AUTO_BIND_NO_TLS": ldap3.AUTO_BIND_NO_TLS,
"AUTO_BIND_TLS_AFTER_BIND": ldap3.AUTO_BIND_TLS_AFTER_BIND,
"AUTO_BIND_TLS_BEFORE_BIND": ldap3.AUTO_BIND_TLS_BEFORE_BIND,
}
auto_bind = auto_bind_map[auto_bind_string]
read_only = config["read_only"]
version = config["version"]
pool_size = config["pool_size"]
pool_keepalive = config["pool_keepalive"]
pool_name = ''.join(random.sample(string.ascii_lowercase, 6))
if client_strategy == ldap3.REUSABLE:
msg = "Using pool size {}".format(pool_size)
logger.debug(msg)
msg = "Using pool keep alive {}".format(pool_keepalive)
logger.debug(msg)
try:
connection = ldap3.Connection(
server,
bind_dn,
bind_password,
auto_bind=auto_bind,
client_strategy=client_strategy,
read_only=read_only,
version=version,
pool_name=pool_name,
pool_size=pool_size,
pool_keepalive=pool_keepalive,
)
msg = "Successfully connected to LDAP server"
logger.debug(msg)
except LDAPException as e:
msg = "Caught exception when connecting to LDAP server: {}"
msg = msg.format(e)
logger.error(msg)
raise LdapAttributeStoreError(msg)
msg = "Successfully connected to LDAP server"
logger.debug(msg)
return connection
def _populate_attributes(self, config, record):
"""
Use a record found in LDAP to populate attributes.
"""
ldap_attributes = record.get("attributes", None)
if not ldap_attributes:
msg = "No attributes returned with LDAP record"
logger.debug(msg)
return
ldap_to_internal_map = (
config["ldap_to_internal_map"]
if config["ldap_to_internal_map"]
# Deprecated configuration. Will be removed in future.
else config["search_return_attributes"]
)
attributes = {}
for attr, values in ldap_attributes.items():
internal_attr = ldap_to_internal_map.get(attr, None)
if not internal_attr and ";" in attr:
internal_attr = ldap_to_internal_map.get(attr.split(";")[0],
None)
if internal_attr and values:
attributes[internal_attr] = (
values
if isinstance(values, list)
else [values]
)
msg = "Recording internal attribute {} with values {}"
logline = msg.format(internal_attr, attributes[internal_attr])
logger.debug(logline)
return attributes
def _populate_input_for_name_id(self, config, record, data):
"""
Use a record found in LDAP to populate input for
NameID generation.
"""
user_id_from_attrs = config["user_id_from_attrs"]
user_ids = [
sorted_list_value
for attr in user_id_from_attrs
for value in [record["attributes"].get(attr)]
if value
for list_value in [value if type(value) is list else [value]]
for sorted_list_value in sorted(list_value)
]
return user_ids
def process(self, context, data):
"""
Default interface for microservices. Process the input data for
the input context.
"""
state = context.state
session_id = lu.get_session_id(state)
requester = data.requester
issuer = data.auth_info.issuer
frontend_name = state.get(ROUTING_STATE_KEY)
co_entity_id_key = SAMLVirtualCoFrontend.KEY_CO_ENTITY_ID
co_entity_id = state.get(frontend_name, {}).get(co_entity_id_key)
entity_ids = [requester, issuer, co_entity_id, "default"]
config, entity_id = next((self.config.get(e), e)
for e in entity_ids if self.config.get(e))
msg = {
"message": "entityID for the involved entities",
"requester": requester,
"issuer": issuer,
"config": self._filter_config(config),
}
if co_entity_id:
msg["co_entity_id"] = co_entity_id
logline = lu.LOG_FMT.format(id=session_id, message=msg)
logger.debug(logline)
# Ignore this entityID entirely if so configured.
if config["ignore"]:
msg = "Ignoring entityID {}".format(entity_id)
logline = lu.LOG_FMT.format(id=session_id, message=msg)
logger.info(logline)
return super().process(context, data)
# The list of values for the LDAP search filters that will be tried in
# order to find the LDAP directory record for the user.
filter_values = [
filter_value
for candidate in config["ordered_identifier_candidates"]
# Consider and find asserted values to construct the ordered list
# of values for the LDAP search filters.
for filter_value in [
self._construct_filter_value(
candidate,
data.subject_id,
data.subject_type,
issuer,
data.attributes,
)
]
# If we have constructed a non empty value then add it as the next
# filter value to use when searching for the user record.
if filter_value
]
msg = {"message": "Search filters", "filter_values": filter_values}
logline = lu.LOG_FMT.format(id=session_id, message=msg)
logger.debug(logline)
# Initialize an empty LDAP record. The first LDAP record found using
# the ordered # list of search filter values will be the record used.
record = None
results = None
exp_msg = None
connection = config["connection"]
msg = {
"message": "LDAP server host",
"server host": connection.server.host,
}
logline = lu.LOG_FMT.format(id=session_id, message=msg)
logger.debug(logline)
for filter_val in filter_values:
ldap_ident_attr = config["ldap_identifier_attribute"]
search_filter = "({0}={1})".format(ldap_ident_attr, filter_val)
msg = {
"message": "LDAP query with constructed search filter",
"search filter": search_filter,
}
logline = lu.LOG_FMT.format(id=session_id, message=msg)
logger.debug(logline)
attributes = (
config["query_return_attributes"]
if config["query_return_attributes"]
# Deprecated configuration. Will be removed in future.
else config["search_return_attributes"].keys()
)
try:
results = connection.search(
config["search_base"], search_filter, attributes=attributes
)
except LDAPException as err:
exp_msg = "Caught LDAP exception: {}".format(err)
except LdapAttributeStoreError as err:
exp_msg = "Caught LDAP Attribute Store exception: {}"
exp_msg = exp_msg.format(err)
except Exception as err:
exp_msg = "Caught unhandled exception: {}".format(err)
if exp_msg:
logline = lu.LOG_FMT.format(id=session_id, message=exp_msg)
logger.error(logline)
return super().process(context, data)
if not results:
msg = "Querying LDAP server: No results for {}."
msg = msg.format(filter_val)
logline = lu.LOG_FMT.format(id=session_id, message=msg)
logger.debug(logline)
continue
if isinstance(results, bool):
responses = connection.entries
else:
responses = connection.get_response(results)[0]
msg = "Done querying LDAP server"
logline = lu.LOG_FMT.format(id=session_id, message=msg)
logger.debug(logline)
msg = "LDAP server returned {} records".format(len(responses))
logline = lu.LOG_FMT.format(id=session_id, message=msg)
logger.info(logline)
# For now consider only the first record found (if any).
if len(responses) > 0:
if len(responses) > 1:
msg = "LDAP server returned {} records using search filter"
msg = msg + " value {}"
msg = msg.format(len(responses), filter_val)
logline = lu.LOG_FMT.format(id=session_id, message=msg)
logger.warning(logline)
record = responses[0]
break
# Before using a found record, if any, to populate attributes
# clear any attributes incoming to this microservice if so configured.
if config["clear_input_attributes"]:
msg = "Clearing values for these input attributes: {}"
msg = msg.format(data.attributes)
logline = lu.LOG_FMT.format(id=session_id, message=msg)
logger.debug(logline)
data.attributes = {}
# This adapts records with different search and connection strategy
# (sync without pool), it should be tested with anonimous bind with
# message_id.
if isinstance(results, bool) and record:
record = {
"dn": record.entry_dn if hasattr(record, "entry_dn") else "",
"attributes": (
record.entry_attributes_as_dict
if hasattr(record, "entry_attributes_as_dict")
else {}
),
}
# Use a found record, if any, to populate attributes and input for
# NameID
if record:
msg = {
"message": "Using record with DN and attributes",
"DN": record["dn"],
"attributes": record["attributes"],
}
logline = lu.LOG_FMT.format(id=session_id, message=msg)
logger.debug(logline)
# Populate attributes as configured.
new_attrs = self._populate_attributes(config, record)
overwrite = config["overwrite_existing_attributes"]
for attr, values in new_attrs.items():
if not overwrite:
values = list(set(data.attributes.get(attr, []) + values))
data.attributes[attr] = values
# Populate input for NameID if configured. SATOSA core does the
# hashing of input to create a persistent NameID.
user_ids = self._populate_input_for_name_id(config, record, data)
if user_ids:
data.subject_id = "".join(user_ids)
msg = "NameID value is {}".format(data.subject_id)
logger.debug(msg)
# Add the record to the context so that later microservices
# may use it if required.
context.decorate(KEY_FOUND_LDAP_RECORD, record)
msg = "Added record {} to context".format(record)
logline = lu.LOG_FMT.format(id=session_id, message=msg)
logger.debug(logline)
else:
msg = "No record found in LDAP so no attributes will be added"
logline = lu.LOG_FMT.format(id=session_id, message=msg)
logger.warning(logline)
on_ldap_search_result_empty = config["on_ldap_search_result_empty"]
if on_ldap_search_result_empty:
# Redirect to the configured URL with
# the entityIDs for the target SP and IdP used by the user
# as query string parameters (URL encoded).
encoded_sp_entity_id = urllib.parse.quote_plus(requester)
encoded_idp_entity_id = urllib.parse.quote_plus(issuer)
url = "{}?sp={}&idp={}".format(
on_ldap_search_result_empty,
encoded_sp_entity_id,
encoded_idp_entity_id,
)
msg = "Redirecting to {}".format(url)
logline = lu.LOG_FMT.format(id=session_id, message=msg)
logger.info(logline)
return Redirect(url)
msg = "Returning data.attributes {}".format(data.attributes)
logline = lu.LOG_FMT.format(id=session_id, message=msg)
logger.debug(logline)
return super().process(context, data)