AnsibleVaultEncryptedUnicode should be considered a string (#71609)

* AnsibleVaultEncryptedUnicode should be considered a string

* linting fix

* clog frag
pull/68151/head
Matt Martz 4 years ago committed by GitHub
parent 7bfeed3e24
commit 48f12c14e9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,3 @@
bugfixes:
- is_string/vault - Ensure the is_string helper properly identifies AnsibleVaultEncryptedUnicode
as a string (https://github.com/ansible/ansible/pull/71609)

@ -67,7 +67,8 @@ class ImmutableDict(Hashable, Mapping):
def is_string(seq): def is_string(seq):
"""Identify whether the input has a string-like type (inclding bytes).""" """Identify whether the input has a string-like type (inclding bytes)."""
return isinstance(seq, (text_type, binary_type)) # AnsibleVaultEncryptedUnicode inherits from Sequence, but is expected to be a string like object
return isinstance(seq, (text_type, binary_type)) or getattr(seq, '__ENCRYPTED__', False)
def is_iterable(seq, include_strings=False): def is_iterable(seq, include_strings=False):

@ -31,7 +31,7 @@ def _preprocess_unsafe_encode(value):
""" """
if _is_unsafe(value): if _is_unsafe(value):
value = {'__ansible_unsafe': to_text(value, errors='surrogate_or_strict', nonstring='strict')} value = {'__ansible_unsafe': to_text(value, errors='surrogate_or_strict', nonstring='strict')}
elif is_sequence(value) and not _is_vault(value): elif is_sequence(value):
value = [_preprocess_unsafe_encode(v) for v in value] value = [_preprocess_unsafe_encode(v) for v in value]
elif isinstance(value, Mapping): elif isinstance(value, Mapping):
value = dict((k, _preprocess_unsafe_encode(v)) for k, v in value.items()) value = dict((k, _preprocess_unsafe_encode(v)) for k, v in value.items())

@ -35,8 +35,21 @@ class IterableStub:
return IteratorStub() return IteratorStub()
class FakeAnsibleVaultEncryptedUnicode(Sequence):
__ENCRYPTED__ = True
def __init__(self, data):
self.data = data
def __getitem__(self, index):
return self.data[index]
def __len__(self):
return len(self.data)
TEST_STRINGS = u'he', u'Україна', u'Česká republika' TEST_STRINGS = u'he', u'Україна', u'Česká republika'
TEST_STRINGS = TEST_STRINGS + tuple(s.encode('utf-8') for s in TEST_STRINGS) TEST_STRINGS = TEST_STRINGS + tuple(s.encode('utf-8') for s in TEST_STRINGS) + (FakeAnsibleVaultEncryptedUnicode(u'foo'),)
TEST_ITEMS_NON_SEQUENCES = ( TEST_ITEMS_NON_SEQUENCES = (
{}, object(), frozenset(), {}, object(), frozenset(),

Loading…
Cancel
Save