Skip to content

Commit 84616a4

Browse files
committed
fix LDAP connector not to assume YAML parameter values are unicode.
Turns out PyYAML, when running in Py2, will return `str` rather than `unicode` string values for any all-ascii strings, even when parsing YAML from a Unicode string source. Since the LDAP module requires Unicode strings as parameter inputs, we have to ensure that any ASCII strings returned by PyYAML are up-converted.
1 parent 665ffc8 commit 84616a4

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

user_sync/connector/directory_ldap.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,14 @@ def __init__(self, caller_options):
102102
if not options['require_tls_cert']:
103103
ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_NEVER)
104104
try:
105+
# Be careful in Py2!! We are setting bytes_mode = False, so we must give all attribute names
106+
# and other protocol-defined strings (such as username) as Unicode. But the PyYAML parser
107+
# will always return ascii strings as str type (rather than Unicode). So we must be careful
108+
# to upconvert all parameter strings to unicode when passing them in.
105109
connection = ldap.initialize(host, bytes_mode=False)
106110
connection.protocol_version = ldap.VERSION3
107111
connection.set_option(ldap.OPT_REFERRALS, 0)
108-
connection.simple_bind_s(username, password)
112+
connection.simple_bind_s(six.text_type(username), six.text_type(password))
109113
except Exception as e:
110114
raise AssertionException('LDAP connection failure: %s' % e)
111115
self.connection = connection
@@ -323,11 +327,12 @@ class LDAPValueFormatter(object):
323327

324328
def __init__(self, string_format):
325329
"""
326-
:type string_format: unicode
330+
The format string must be a unicode or ascii string: see notes above about being careful in Py2!
327331
"""
328332
if string_format is None:
329333
attribute_names = []
330334
else:
335+
string_format = six.text_type(string_format) # force unicode so attribute values are unicode
331336
formatter = string.Formatter()
332337
attribute_names = [item[1] for item in formatter.parse(string_format) if item[1]]
333338
self.string_format = string_format

0 commit comments

Comments
 (0)