Merge pull request #46966 from terminalmage/issue46934

Fix traceback when attempting to decode binary data to unicode
This commit is contained in:
Nicole Thomas 2018-04-10 10:08:34 -04:00 committed by GitHub
commit 58f59cfbff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 10 deletions

View File

@ -141,7 +141,11 @@ class Serial(object):
# Due to this, if we don't need it, don't pass it at all so
# that under Python 2 we can still work with older versions
# of msgpack.
ret = msgpack.loads(msg, use_list=True, ext_hook=ext_type_decoder, encoding=encoding)
try:
ret = msgpack.loads(msg, use_list=True, ext_hook=ext_type_decoder, encoding=encoding)
except UnicodeDecodeError:
# msg contains binary data
ret = msgpack.loads(msg, use_list=True, ext_hook=ext_type_decoder)
else:
ret = msgpack.loads(msg, use_list=True, ext_hook=ext_type_decoder)
if six.PY3 and encoding is None and not raw:

View File

@ -738,7 +738,7 @@ class Pillar(object):
msg = 'Rendering SLS \'{0}\' failed, render error:\n{1}'.format(
sls, exc
)
log.critical(msg)
log.critical(msg, exc_info=True)
if self.opts.get('pillar_safe_render_error', True):
errors.append(
'Rendering SLS \'{0}\' failed. Please see master log for '

View File

@ -269,16 +269,22 @@ def _decrypt_ciphertext(cipher, translate_newlines=False):
decrypted, log the error, and return the ciphertext back out.
'''
if translate_newlines:
cipher = cipher.replace(r'\n', '\n')
if six.PY3:
cipher = cipher.encode(__salt_system_encoding__)
try:
cipher = salt.utils.stringutils.to_unicode(cipher).replace(r'\n', '\n')
except UnicodeDecodeError:
# ciphertext is binary
pass
cipher = salt.utils.stringutils.to_bytes(cipher)
cmd = [_get_gpg_exec(), '--homedir', _get_key_dir(), '--status-fd', '2',
'--no-tty', '-d']
proc = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE, shell=False)
decrypted_data, decrypt_error = proc.communicate(input=cipher)
if not decrypted_data:
if six.PY3:
cipher = cipher.decode(__salt_system_encoding__)
try:
cipher = salt.utils.stringutils.to_unicode(cipher)
except UnicodeDecodeError:
# decrypted data contains undecodable binary data
pass
log.warning(
'Could not decrypt cipher %s, received: %s',
cipher,
@ -286,9 +292,12 @@ def _decrypt_ciphertext(cipher, translate_newlines=False):
)
return cipher
else:
if six.PY3 and isinstance(decrypted_data, bytes):
decrypted_data = decrypted_data.decode(__salt_system_encoding__)
return six.text_type(decrypted_data)
try:
decrypted_data = salt.utils.stringutils.to_unicode(decrypted_data)
except UnicodeDecodeError:
# decrypted data contains undecodable binary data
pass
return decrypted_data
def _decrypt_object(obj, translate_newlines=False):