Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[2019.2] Make sure ldap attrs are string types on python 2 #51964

Merged
merged 4 commits into from Mar 5, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 8 additions & 6 deletions salt/pillar/pillar_ldap.py
Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down
26 changes: 26 additions & 0 deletions tests/unit/pillar/test_pillar_ldap.py
@@ -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')