Merge pull request #51964 from dwoz/issue_51816

[2019.2] Make sure ldap attrs are string types on python 2
This commit is contained in:
Daniel Wozniak 2019-03-05 12:18:29 -07:00 committed by GitHub
commit c310022bf2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 6 deletions

View file

@ -163,15 +163,17 @@ def _render_template(config_file):
return template.render(__grains__)
def _config(name, conf):
def _config(name, conf, default=None):
'''
Return a value for 'name' from the config file options.
Return a value for 'name' from the config file options. If the 'name' is
not in the config, the 'default' value is returned. This method converts
unicode values to str type under python 2.
'''
try:
value = salt.utils.data.decode(conf[name], to_str=True)
value = conf[name]
except KeyError:
value = None
return value
value = default
return salt.utils.data.decode(value, to_str=True)
def _result_to_dict(data, result, conf, source):
@ -285,7 +287,7 @@ def _do_search(conf):
scope = _config('scope', conf)
_lists = _config('lists', conf) or []
_attrs = _config('attrs', conf) or []
_dict_key_attr = _config('dict_key_attr', conf) or 'dn'
_dict_key_attr = _config('dict_key_attr', conf, 'dn')
attrs = _lists + _attrs + [_dict_key_attr]
if not attrs:
attrs = None

View file

@ -0,0 +1,26 @@
# -*- coding: utf-8 -*-
# Import python libs
from __future__ import absolute_import, print_function, unicode_literals
from tests.support.unit import TestCase
import salt.utils.stringutils
from salt.pillar.pillar_ldap import _config
class LdapPillarTestCase(TestCase):
def test__config_returns_str(self):
conf = {'foo': 'bar'}
assert _config('foo', conf) == salt.utils.stringutils.to_str('bar')
def test__conf_defaults_to_none(self):
conf = {'foo': 'bar'}
assert _config('bang', conf) is None
def test__conf_returns_str_from_unicode_default(self):
conf = {'foo': 'bar'}
default = salt.utils.stringutils.to_unicode('bam')
assert _config('bang', conf, default) == salt.utils.stringutils.to_str('bam')