8
8
import satosa .micro_services .base
9
9
from satosa .logging_util import satosa_logging
10
10
11
+ import copy
11
12
import logging
12
13
import ldap3
13
14
@@ -19,88 +20,139 @@ class LdapAttributeStore(satosa.micro_services.base.ResponseMicroService):
19
20
to lookup a person record in LDAP and obtain attributes
20
21
to assert about the user to the frontend receiving service.
21
22
"""
23
+ logprefix = "LDAP_ATTRIBUTE_STORE:"
22
24
23
25
def __init__ (self , config , * args , ** kwargs ):
24
26
super ().__init__ (* args , ** kwargs )
25
27
self .config = config
26
28
27
29
def process (self , context , data ):
30
+ logprefix = LdapAttributeStore .logprefix
31
+
32
+ # Initialize the configuration to use as the default configuration
33
+ # that is passed during initialization.
34
+ config = self .config
35
+ configClean = copy .deepcopy (config )
36
+ if 'bind_password' in configClean :
37
+ configClean ['bind_password' ] = 'XXXXXXXX'
38
+
39
+ satosa_logging (logger , logging .DEBUG , "{} Using default configuration {}" .format (logprefix , configClean ), context .state )
40
+
41
+ # Find the entityID for the SP that initiated the flow
42
+ try :
43
+ spEntityID = context .state .state_dict ['SATOSA_BASE' ]['requester' ]
44
+ except KeyError as err :
45
+ satosa_logging (logger , logging .ERROR , "{} Unable to determine the entityID for the SP requester" .format (logprefix ), context .state )
46
+ return super ().process (context , data )
47
+
48
+ satosa_logging (logger , logging .DEBUG , "{} entityID for the SP requester is {}" .format (logprefix , spEntityID ), context .state )
49
+
50
+ # Examine our configuration to determine if there is a per-SP configuration
51
+ if spEntityID in self .config :
52
+ config = self .config [spEntityID ]
53
+ configClean = copy .deepcopy (config )
54
+ if 'bind_password' in configClean :
55
+ configClean ['bind_password' ] = 'XXXXXXXX'
56
+ satosa_logging (logger , logging .DEBUG , "{} For SP {} using configuration {}" .format (logprefix , spEntityID , configClean ), context .state )
57
+
58
+ # Obtain configuration details from the per-SP configuration or the default configuration
28
59
try :
29
- ldap_url = self .config ['ldap_url' ]
30
- bind_dn = self .config ['bind_dn' ]
31
- bind_password = self .config ['bind_password' ]
32
- search_base = self .config ['search_base' ]
33
- search_return_attributes = self .config ['search_return_attributes' ]
34
- idp_identifiers = self .config ['idp_identifiers' ]
35
- ldap_identifier_attribute = self .config ['ldap_identifier_attribute' ]
60
+ if 'ldap_url' in config :
61
+ ldap_url = config ['ldap_url' ]
62
+ else :
63
+ ldap_url = self .config ['ldap_url' ]
64
+ if 'bind_dn' in config :
65
+ bind_dn = config ['bind_dn' ]
66
+ else :
67
+ bind_dn = self .config ['bind_dn' ]
68
+ if 'bind_dn' in config :
69
+ bind_password = config ['bind_password' ]
70
+ else :
71
+ bind_password = self .config ['bind_password' ]
72
+ if 'search_base' in config :
73
+ search_base = config ['search_base' ]
74
+ else :
75
+ search_base = self .config ['search_base' ]
76
+ if 'search_return_attributes' in config :
77
+ search_return_attributes = config ['search_return_attributes' ]
78
+ else :
79
+ search_return_attributes = self .config ['search_return_attributes' ]
80
+ if 'idp_identifiers' in config :
81
+ idp_identifiers = config ['idp_identifiers' ]
82
+ else :
83
+ idp_identifiers = self .config ['idp_identifiers' ]
84
+ if 'ldap_identifier_attribute' in config :
85
+ ldap_identifier_attribute = config ['ldap_identifier_attribute' ]
86
+ else :
87
+ ldap_identifier_attribute = self .config ['ldap_identifier_attribute' ]
36
88
37
89
except KeyError as err :
38
- satosa_logging (logger , logging .ERROR , "Configuration '{key }' is missing" .format (key = err ), context .state )
90
+ satosa_logging (logger , logging .ERROR , "{} Configuration '{}' is missing" .format (logprefix , err ), context .state )
39
91
return super ().process (context , data )
40
92
41
93
entry = None
42
94
43
95
try :
44
- satosa_logging (logger , logging .DEBUG , "Using LDAP URL {}" .format (ldap_url ), context .state )
96
+ satosa_logging (logger , logging .DEBUG , "{} Using LDAP URL {}" .format (logprefix , ldap_url ), context .state )
45
97
server = ldap3 .Server (ldap_url )
46
98
47
- satosa_logging (logger , logging .DEBUG , "Using bind DN {}" .format (bind_dn ), context .state )
99
+ satosa_logging (logger , logging .DEBUG , "{} Using bind DN {}" .format (logprefix , bind_dn ), context .state )
48
100
connection = ldap3 .Connection (server , bind_dn , bind_password , auto_bind = True )
49
- satosa_logging (logger , logging .DEBUG , "Connected to LDAP server" , context .state )
101
+ satosa_logging (logger , logging .DEBUG , "{} Connected to LDAP server" . format ( logprefix ) , context .state )
50
102
51
103
52
104
for identifier in idp_identifiers :
53
105
if entry :
54
106
break
55
107
56
- satosa_logging (logger , logging .DEBUG , "Using IdP asserted attribute {}" .format (identifier ), context .state )
108
+ satosa_logging (logger , logging .DEBUG , "{} Using IdP asserted attribute {}" .format (logprefix , identifier ), context .state )
57
109
58
110
if identifier in data .attributes :
59
- satosa_logging (logger , logging .DEBUG , "IdP asserted {} values for attribute {}" .format (len (data .attributes [identifier ]),identifier ), context .state )
111
+ satosa_logging (logger , logging .DEBUG , "{} IdP asserted {} values for attribute {}" .format (logprefix , len (data .attributes [identifier ]),identifier ), context .state )
60
112
61
113
for identifier_value in data .attributes [identifier ]:
62
- satosa_logging (logger , logging .DEBUG , "Considering IdP asserted value {} for attribute {}" .format (identifier_value , identifier ), context .state )
114
+ satosa_logging (logger , logging .DEBUG , "{} Considering IdP asserted value {} for attribute {}" .format (logprefix , identifier_value , identifier ), context .state )
63
115
64
116
search_filter = '({0}={1})' .format (ldap_identifier_attribute , identifier_value )
65
- satosa_logging (logger , logging .DEBUG , "Constructed search filter {}" .format (search_filter ), context .state )
117
+ satosa_logging (logger , logging .DEBUG , "{} Constructed search filter {}" .format (logprefix , search_filter ), context .state )
66
118
67
- satosa_logging (logger , logging .DEBUG , "Querying LDAP server..." , context .state )
119
+ satosa_logging (logger , logging .DEBUG , "{} Querying LDAP server..." . format ( logprefix ) , context .state )
68
120
connection .search (search_base , search_filter , attributes = search_return_attributes .keys ())
69
- satosa_logging (logger , logging .DEBUG , "Done querying LDAP server" , context .state )
121
+ satosa_logging (logger , logging .DEBUG , "{} Done querying LDAP server" . format ( logprefix ) , context .state )
70
122
71
123
entries = connection .entries
72
- satosa_logging (logger , logging .DEBUG , "LDAP server returned {} entries" .format (len (entries )), context .state )
124
+ satosa_logging (logger , logging .DEBUG , "{} LDAP server returned {} entries" .format (logprefix , len (entries )), context .state )
73
125
74
126
# for now consider only the first entry found (if any)
75
127
if len (entries ) > 0 :
76
128
if len (entries ) > 1 :
77
- satosa_logging (logger , logging .WARN , "LDAP server returned {} entries using IdP asserted attribute {}" .format (len (entries ), identifier ), context .state )
129
+ satosa_logging (logger , logging .WARN , "{} LDAP server returned {} entries using IdP asserted attribute {}" .format (logprefix , len (entries ), identifier ), context .state )
78
130
entry = entries [0 ]
79
131
break
80
132
81
133
else :
82
- satosa_logging (logger , logging .DEBUG , "IdP did not assert attribute {}" .format (identifier ), context .state )
134
+ satosa_logging (logger , logging .DEBUG , "{} IdP did not assert attribute {}" .format (logprefix , identifier ), context .state )
83
135
84
136
except Exception as err :
85
- satosa_logging (logger , logging .ERROR , "Caught exception: {0}" .format (err ), None )
137
+ satosa_logging (logger , logging .ERROR , "{} Caught exception: {0}" .format (logprefix , err ), None )
86
138
return super ().process (context , data )
87
139
88
140
else :
89
- satosa_logging (logger , logging .DEBUG , "Unbinding and closing connection to LDAP server" , context .state )
141
+ satosa_logging (logger , logging .DEBUG , "{} Unbinding and closing connection to LDAP server" . format ( logprefix ) , context .state )
90
142
connection .unbind ()
91
143
92
144
# use a found entry, if any, to populate attributes
93
145
if entry :
94
- satosa_logging (logger , logging .DEBUG , "Using entry with DN {}" .format (entry .entry_get_dn ()), context .state )
146
+ satosa_logging (logger , logging .DEBUG , "{} Using entry with DN {}" .format (logprefix , entry .entry_get_dn ()), context .state )
95
147
data .attributes = {}
96
148
for attr in search_return_attributes .keys ():
97
149
if attr in entry :
98
150
data .attributes [search_return_attributes [attr ]] = entry [attr ].values
99
- satosa_logging (logger , logging .DEBUG , "Setting internal attribute {} with values {}" .format (search_return_attributes [attr ], entry [attr ].values ), context .state )
151
+ satosa_logging (logger , logging .DEBUG , "{} Setting internal attribute {} with values {}" .format (logprefix , search_return_attributes [attr ], entry [attr ].values ), context .state )
100
152
101
153
else :
102
154
# We should probably have an option here to clear attributes from IdP
103
155
pass
104
156
105
- satosa_logging (logger , logging .DEBUG , "returning data.attributes %s" % str (data .attributes ), context .state )
157
+ satosa_logging (logger , logging .DEBUG , "{} returning data.attributes {}" . format ( logprefix , str (data .attributes ) ), context .state )
106
158
return super ().process (context , data )
0 commit comments