mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-05-07 13:52:54 +00:00
Implement vault encrypted yaml variables. (#16274)
Make !vault-encrypted create a AnsibleVaultUnicode
yaml object that can be used as a regular string object.
This allows a playbook to include a encrypted vault
blob for the value of a yaml variable. A 'secret_password'
variable can have it's value encrypted instead of having
to vault encrypt an entire vars file.
Add __ENCRYPTED__ to the vault yaml types so
template.Template can treat it similar
to __UNSAFE__ flags.
vault.VaultLib api changes:
- Split VaultLib.encrypt to encrypt and encrypt_bytestring
- VaultLib.encrypt() previously accepted the plaintext data
as either a byte string or a unicode string.
Doing the right thing based on the input type would fail
on py3 if given a arg of type 'bytes'. To simplify the
API, vaultlib.encrypt() now assumes input plaintext is a
py2 unicode or py3 str. It will encode to utf-8 then call
the new encrypt_bytestring(). The new methods are less
ambiguous.
- moved VaultLib.is_encrypted logic to vault module scope
and split to is_encrypted() and is_encrypted_file().
Add a test/unit/mock/yaml_helper.py
It has some helpers for testing parsing/yaml
Integration tests added as roles test_vault and test_vault_embedded
This commit is contained in:
@@ -227,7 +227,7 @@ class Templar:
|
||||
def _clean_data(self, orig_data):
|
||||
''' remove jinja2 template tags from a string '''
|
||||
|
||||
if not isinstance(orig_data, string_types):
|
||||
if not isinstance(orig_data, string_types) or hasattr(orig_data, '__ENCRYPTED__'):
|
||||
return orig_data
|
||||
|
||||
with contextlib.closing(StringIO(orig_data)) as data:
|
||||
@@ -281,7 +281,7 @@ class Templar:
|
||||
'''
|
||||
Templates (possibly recursively) any given data as input. If convert_bare is
|
||||
set to True, the given data will be wrapped as a jinja2 variable ('{{foo}}')
|
||||
before being sent through the template engine.
|
||||
before being sent through the template engine.
|
||||
'''
|
||||
|
||||
if fail_on_undefined is None:
|
||||
@@ -304,7 +304,6 @@ class Templar:
|
||||
result = variable
|
||||
|
||||
if self._contains_vars(variable):
|
||||
|
||||
# Check to see if the string we are trying to render is just referencing a single
|
||||
# var. In this case we don't want to accidentally change the type of the variable
|
||||
# to a string by using the jinja template renderer. We just want to pass it.
|
||||
@@ -438,7 +437,6 @@ class Templar:
|
||||
raise AnsibleError("lookup plugin (%s) not found" % name)
|
||||
|
||||
def _do_template(self, data, preserve_trailing_newlines=True, escape_backslashes=True, fail_on_undefined=None, overrides=None):
|
||||
|
||||
# For preserving the number of input newlines in the output (used
|
||||
# later in this method)
|
||||
data_newlines = _count_newlines_from_end(data)
|
||||
@@ -517,7 +515,6 @@ class Templar:
|
||||
res_newlines = _count_newlines_from_end(res)
|
||||
if data_newlines > res_newlines:
|
||||
res += '\n' * (data_newlines - res_newlines)
|
||||
|
||||
return res
|
||||
except (UndefinedError, AnsibleUndefinedVariable) as e:
|
||||
if fail_on_undefined:
|
||||
|
||||
Reference in New Issue
Block a user