Allow binary pillar data

There's no real reason that pillars can't/shouldn't be able to
contain binary data. This gives us the ability to say that it's OK.
This commit is contained in:
Wayne Werner 2019-03-22 15:31:01 -05:00
parent 604b671ef9
commit c4b385b92a
No known key found for this signature in database
GPG key ID: C36D3A8D5BEF0935
2 changed files with 12 additions and 3 deletions

View file

@ -214,10 +214,14 @@ def decode(data, encoding=None, errors='strict', keep=False,
def decode_dict(data, encoding=None, errors='strict', keep=False,
normalize=False, preserve_dict_class=False,
preserve_tuples=False, to_str=False):
preserve_tuples=False, to_str=False, keep_pillar=False):
'''
Decode all string values to Unicode. Optionally use to_str=True to ensure
strings are str types and not unicode on Python 2.
keep_pillar
If ``True``, pillar data that cannot be converted to unicode should be
kept as binary data (bytes on Python 3, str on Python 2).
'''
_decode_func = salt.utils.stringutils.to_unicode \
if not to_str \
@ -240,7 +244,7 @@ def decode_dict(data, encoding=None, errors='strict', keep=False,
# means we are going to leave the value as-is.
pass
except UnicodeDecodeError:
if not keep:
if (key == 'pillar' and not keep_pillar) or not keep:
raise
if isinstance(value, list):
@ -264,7 +268,7 @@ def decode_dict(data, encoding=None, errors='strict', keep=False,
# means we are going to leave the value as-is.
pass
except UnicodeDecodeError:
if not keep:
if (key == 'pillar' and not keep_pillar) and not keep:
raise
rv[key] = value

View file

@ -598,3 +598,8 @@ class DataTestCase(TestCase):
salt.utils.data.stringify(['one', 'two', str('three'), 4, 5]), # future lint: disable=blacklisted-function
['one', 'two', 'three', '4', '5']
)
def test_decode_dict_with_keep_pillar_and_binary_data_should_keep_binary_data(self):
data = {'pillar': b'\x8b'}
decoded_data = salt.utils.data.decode_dict(data, keep_pillar=True)
self.assertEqual(decoded_data, data)