Handle vaulted non-ascii characters for Python2 (#58503)

* Handle vaulted non-ascii characters for Python2

* Add a test to ensure str() no longer raises UnicodeEncodeError
This commit is contained in:
Sloane Hertel 2019-06-28 13:23:15 -04:00 committed by GitHub
parent e28bc46353
commit 826f224f02
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 2 deletions

View file

@ -0,0 +1,2 @@
bugfixes:
- vault - Fix traceback using Python2 if a vault contains non-ascii characters (https://github.com/ansible/ansible/issues/58351).

View file

@ -22,7 +22,7 @@ __metaclass__ = type
import yaml
from ansible.module_utils.six import text_type
from ansible.module_utils._text import to_bytes, to_text
from ansible.module_utils._text import to_bytes, to_text, to_native
class AnsibleBaseYAMLObject(object):
@ -128,7 +128,7 @@ class AnsibleVaultEncryptedUnicode(yaml.YAMLObject, AnsibleBaseYAMLObject):
return True
def __str__(self):
return str(self.data)
return to_native(self.data, errors='surrogate_or_strict')
def __unicode__(self):
return to_text(self.data, errors='surrogate_or_strict')

View file

@ -24,6 +24,8 @@ from units.compat import unittest
from ansible.errors import AnsibleError
from ansible.module_utils._text import to_native
from ansible.parsing import vault
from ansible.parsing.yaml.loader import AnsibleLoader
@ -155,3 +157,8 @@ class TestAnsibleVaultEncryptedUnicode(unittest.TestCase, YamlTestUtils):
seq = u"aöffü"
avu = self._from_plaintext(seq)
self.assert_values(avu, seq)
def test_str_vaulted_utf8_value_37258(self):
seq = u"aöffü"
avu = self._from_plaintext(seq)
assert str(avu) == to_native(seq)