Enabled unsafe and vault in JSON (#38759)

* allow to load json marked as unsafe or vault

 * centralized json code/decode, add vault support
 * use generics to allow for more varied inputs
 * allow inventory to dump vault w/o decrypting
 * override simplejson also
 * add entry for unsafe also
 * load vaulted and unsafe json, support unvaulting if secrets provided
This commit is contained in:
Brian Coca 2018-05-09 14:01:51 -04:00 committed by GitHub
parent ff16e993be
commit cbb6a7f4e8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 99 additions and 56 deletions

View file

@ -53,13 +53,13 @@
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
import json
from collections import Mapping, MutableSequence, Set
from ansible.module_utils.six import string_types, text_type
from ansible.module_utils._text import to_text
__all__ = ['UnsafeProxy', 'AnsibleUnsafe', 'AnsibleJSONUnsafeEncoder', 'AnsibleJSONUnsafeDecoder', 'wrap_var']
__all__ = ['UnsafeProxy', 'AnsibleUnsafe', 'wrap_var']
class AnsibleUnsafe(object):
@ -82,24 +82,6 @@ class UnsafeProxy(object):
return obj
class AnsibleJSONUnsafeEncoder(json.JSONEncoder):
def encode(self, obj):
if isinstance(obj, AnsibleUnsafe):
return super(AnsibleJSONUnsafeEncoder, self).encode(dict(__ansible_unsafe=True,
value=to_text(obj, errors='surrogate_or_strict', nonstring='strict')))
else:
return super(AnsibleJSONUnsafeEncoder, self).encode(obj)
class AnsibleJSONUnsafeDecoder(json.JSONDecoder):
def decode(self, obj):
value = super(AnsibleJSONUnsafeDecoder, self).decode(obj)
if isinstance(value, dict) and '__ansible_unsafe' in value:
return UnsafeProxy(value.get('value', ''))
else:
return value
def _wrap_dict(v):
for k in v.keys():
if v[k] is not None:
@ -115,11 +97,10 @@ def _wrap_list(v):
def wrap_var(v):
if isinstance(v, dict):
if isinstance(v, Mapping):
v = _wrap_dict(v)
elif isinstance(v, list):
elif isinstance(v, (MutableSequence, Set)):
v = _wrap_list(v)
else:
if v is not None and not isinstance(v, AnsibleUnsafe):
v = UnsafeProxy(v)
elif v is not None and not isinstance(v, AnsibleUnsafe):
v = UnsafeProxy(v)
return v