ansible/test/units/parsing
Adrian Likins 29bdd0b326 Better handling of malformed vault data envelope (#32515)
If an embedded vaulted variable ('!vault' in yaml)
had an invalid format, it would eventually cause
an error for seemingly unrelated reasons.
"Invalid" meaning not valid hexlify (extra chars,
non-hex chars, etc).

For ex, if a host_vars file had invalid vault format
variables, on py2, it would cause an error like:

  'ansible.vars.hostvars.HostVars object' has no
  attribute u'broken.example.com'

Depending on where the invalid vault is, it could
also cause "VARIABLE IS NOT DEFINED!". The behavior
can also change if ansible-playbook is py2 or py3.

Root cause is errors from binascii.unhexlify() not
being handled consistently.

Fix is to add a AnsibleVaultFormatError exception and
raise it on any unhexlify() errors and to handle it
properly elsewhere.

Add a _unhexlify() that try/excepts around a binascii.unhexlify()
and raises an AnsibleVaultFormatError on invalid vault data.
This is so the same exception type is always raised for this
case. Previous it was different between py2 and py3.

binascii.unhexlify() raises a binascii.Error if the hexlified
blobs in a vault data blob are invalid.

On py2, binascii.Error is a subclass of Exception.
On py3, binascii.Error is a subclass of TypeError

When decrypting content of vault encrypted variables,
if a binascii.Error is raised it propagates up to
playbook.base.Base.post_validate(). post_validate()
handles exceptions for TypeErrors but not for
base Exception subclasses (like py2 binascii.Error).

* Add a display.warning on vault format errors
* Unit tests for _unhexlify, parse_vaulttext*
* Add intg test cases for invalid vault formats

Fixes #28038

(cherry picked from commit 9c58827410)
2017-11-10 14:31:32 -05:00
..
utils test/: PEP8 compliancy (#24803) 2017-05-30 18:05:19 +01:00
vault Better handling of malformed vault data envelope (#32515) 2017-11-10 14:31:32 -05:00
yaml Support multiple vault passwords (#22756) 2017-07-28 15:20:58 -04:00
__init__.py Add empty-init code-smell script. (#18406) 2016-11-07 15:02:13 -08:00
test_dataloader.py Fixes #23680 bug with py3.x due to binary string handling (#23688) 2017-09-09 18:40:30 -07:00
test_metadata.py metadata 1.1 2017-08-15 23:12:08 -07:00
test_mod_args.py test/: PEP8 compliancy (#24803) 2017-05-30 18:05:19 +01:00
test_splitter.py Switch tests to pytest and ansible-test. 2017-01-11 12:34:59 -08:00
test_unquote.py Switch tests to pytest and ansible-test. 2017-01-11 12:34:59 -08:00