From 8c641c66fb42b69a12e69ef3d66a2efed3c0e35f Mon Sep 17 00:00:00 2001 From: "Daniel A. Wozniak" Date: Mon, 4 Mar 2019 21:35:17 +0000 Subject: [PATCH 1/3] make LDAP attr defaults string types on py2 --- salt/modules/ldapmod.py | 4 ++-- salt/pillar/pillar_ldap.py | 14 ++++++++------ tests/unit/pillar/test_pillar_ldap.py | 27 +++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 8 deletions(-) create mode 100644 tests/unit/pillar/test_pillar_ldap.py diff --git a/salt/modules/ldapmod.py b/salt/modules/ldapmod.py index c41b04d3f17d..5b40cc15ea48 100644 --- a/salt/modules/ldapmod.py +++ b/salt/modules/ldapmod.py @@ -144,9 +144,9 @@ def search(filter, # pylint: disable=C0103 attrs = _config('attrs') _ldap = _connect(**kwargs) start = time.time() - log.debug( + log.warn( 'Running LDAP search with filter:%s, dn:%s, scope:%s, ' - 'attrs:%s', filter, dn, scope, attrs + 'attrs:%r', filter, dn, scope, attrs ) results = _ldap.search_s(dn, int(scope), filter, attrs) elapsed = (time.time() - start) diff --git a/salt/pillar/pillar_ldap.py b/salt/pillar/pillar_ldap.py index 6dbbaa759112..b2321ebc1b44 100644 --- a/salt/pillar/pillar_ldap.py +++ b/salt/pillar/pillar_ldap.py @@ -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 diff --git a/tests/unit/pillar/test_pillar_ldap.py b/tests/unit/pillar/test_pillar_ldap.py new file mode 100644 index 000000000000..dc9573ce7b05 --- /dev/null +++ b/tests/unit/pillar/test_pillar_ldap.py @@ -0,0 +1,27 @@ +# -*- 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) == 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') From 171217ae3c6c1db79541c3b2680dcb085ec94d14 Mon Sep 17 00:00:00 2001 From: "Daniel A. Wozniak" Date: Mon, 4 Mar 2019 14:39:36 -0700 Subject: [PATCH 2/3] Revert debug logging --- salt/modules/ldapmod.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/salt/modules/ldapmod.py b/salt/modules/ldapmod.py index 5b40cc15ea48..c41b04d3f17d 100644 --- a/salt/modules/ldapmod.py +++ b/salt/modules/ldapmod.py @@ -144,9 +144,9 @@ def search(filter, # pylint: disable=C0103 attrs = _config('attrs') _ldap = _connect(**kwargs) start = time.time() - log.warn( + log.debug( 'Running LDAP search with filter:%s, dn:%s, scope:%s, ' - 'attrs:%r', filter, dn, scope, attrs + 'attrs:%s', filter, dn, scope, attrs ) results = _ldap.search_s(dn, int(scope), filter, attrs) elapsed = (time.time() - start) From 0e61cf39e4c25c9ade403fbebff48f2b13ad7073 Mon Sep 17 00:00:00 2001 From: "Daniel A. Wozniak" Date: Mon, 4 Mar 2019 14:48:33 -0700 Subject: [PATCH 3/3] Fix linter errors/warnings --- tests/unit/pillar/test_pillar_ldap.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/unit/pillar/test_pillar_ldap.py b/tests/unit/pillar/test_pillar_ldap.py index dc9573ce7b05..53ae293dcdf7 100644 --- a/tests/unit/pillar/test_pillar_ldap.py +++ b/tests/unit/pillar/test_pillar_ldap.py @@ -10,7 +10,6 @@ from salt.pillar.pillar_ldap import _config - class LdapPillarTestCase(TestCase): def test__config_returns_str(self): @@ -19,7 +18,7 @@ def test__config_returns_str(self): def test__conf_defaults_to_none(self): conf = {'foo': 'bar'} - assert _config('bang', conf) == None + assert _config('bang', conf) is None def test__conf_returns_str_from_unicode_default(self): conf = {'foo': 'bar'}