Commit graph

145 commits

Author SHA1 Message Date
Toshio Kuratomi
8c03fec67a Remove method that isn't used here. (#18713)
We have two copies of this function and only the one in cli is used.
2016-12-02 00:30:41 -08:00
Adrian Likins
c09060e8ff Fix 'ansible-vault edit' crash on changed nonascii
ansible-vault edit was attempting to decode the file contents
and failing.

Fixes #18428
2016-11-16 15:14:07 -05:00
Adrian Likins
dd0189839e Fix bug (#18355) where encrypted inventories fail 18355 (#18373)
* Fix bug (#18355) where encrypted inventories fail

This is first part of fix for #18355
* Make DataLoader._get_file_contents return bytes

The issue #18355 is caused by a change to inventory to
stop using _get_file_contents so that it can handle text
encoding itself to better protect against harmless text
encoding errors in ini files (invalid unicode text in
comment fields).

So this makes _get_file_contents return bytes so it and other
callers can handle the to_text().

The data returned by _get_file_contents() is now a bytes object
instead of a text object. The callers of _get_file_contents() have
been updated to call to_text() themselves on the results.

Previously, the ini parser attempted to work around
ini files that potentially include non-vailid unicode
in comment lines. To do this, it stopped using
DataLoader._get_file_contents() which does the decryption of
files if vault encrypted. It didn't use that because _get_file_contents
previously did to_text() on the read data itself.

_get_file_contents() returns a bytestring now, so ini.py
can call it and still special case ini file comments when
converting to_text(). That also means encrypted inventory files
are decrypted first.

Fixes #18355
2016-11-07 10:07:26 -05:00
Toshio Kuratomi
ed134d81f1 Limit how much of the file we read to test if it's an encrypted vault file
Fixes memory errors reported in #16391
2016-11-04 12:30:50 -07:00
Adrian Likins
309f54b709 Fix 'vault rekey' with vault secret env var
if ANSIBLE_VAULT_PASSWORD_FILE is set, 'ansible-vault rekey myvault.yml'
will fail to prompt for the new vault password file, and will use
None.

Fix is to split out 'ask_vault_passwords' into 'ask_vault_passwords'
and 'ask_new_vault_passwords' to make the logic simpler. And then
make sure new_vault_pass is always set for 'rekey', and if not, then
call ask_new_vault_passwords() to set it.

ask_vault_passwords() would return values for vault_pass and new
vault_pass, and vault cli previously would not prompt for new_vault_pass
if there was a vault_pass set via a vault password file.

Fixes #18247
2016-11-01 13:07:48 -04:00
Andrew Gaffney
194c9c41eb Fix search path for relative paths in roles (fixes #17877)
(cherry picked from commit 72f0aaf606)
2016-10-17 11:21:46 -04:00
Toshio Kuratomi
bf3d546d9a Only dispkay failure to use cryptography at a higher verbosity
Fixes #17982
2016-10-12 10:48:36 -07:00
James Cammarata
c9d3d2b9a0 Allow for empty source in dwim_path_relative_stack
Fixes #17192
2016-09-21 16:16:14 -05:00
Toshio Kuratomi
2989527cd9 Fix dataloader using deprecated version of is_encrypted (#17615) 2016-09-17 00:45:29 -07:00
Toshio Kuratomi
8af8eec789 Merge pull request #17590 from abadger/vault-fixes
Vault fixes
2016-09-15 18:39:34 -07:00
Toshio Kuratomi
e70066a6f7 Many Cleanups to vault
* Make is_encrypted_file handle both files opened in text and binary mode
  On python3, by default files are opened in text mode.  Since we know
  the encoding of vault files (and especially the header which is the
  first set of bytes) we can decide whether the file is an encrypted
  vault file in either case.
* Fix is_encrypted_file not resetting the file position
* Update is_encrypted_file to check that all the data in the file is ascii
* For is_encrypted_file(), add start_pos and count parameters
  This allows callers to specify reading vaulttext from the middle of
  a file if necessary.
* Combine VaultLib.encrypt() and VaultLib.encrypt_bytestring()
* Change vault's is_encrypted() to take either text or byte strings and to return False if any part of the data is non-ascii.
* Remove unnecessary use of six.b
* Vault Cipher: mark a few methods as private.
* VaultAES256._is_equal throws a TypeError if given non byte strings
* Make VaultAES256 methods that don't need self staticmethods and classmethods
* Mark VaultAES and is_encrypted as deprecated
* Get rid of VaultFile (unused and feature implemented in a different way)
* Normalize variable and parameter names on plaintext, ciphertext, vaulttext
* Normalize variable and parameter names on "b_" prefix when dealing with bytes
* Test changes:
  * Remove redundant tests( both checking the same byte string)
  * Fix use of format string without format operator
  * Enable vault editor tests on python3
  * Initialize the vault_cipher for VaultAES256 testing in setUp()
  * Make assertTrue and assertFalse take the actual method calls for
    better error messages.
  * Test that non-ascii byte strings compare correctly.
  * Test that unicode strings and ints raise TypeError

* Test-specific:
  * Removed test_methods_exist().  We only have one VaultLib so the
    implementation is the assurance that the methods exist. (Can use an abc for
    this if it changes).
  * Add tests for both byte string and text string input where the API takes either.
  * Convert "assert" to unittest assert functions or add a custom message where
    that will make failures easier to debug.
  * Move instantiating the VaultLib into setUp().
2016-09-15 15:22:06 -07:00
jctanner
b93de25204 During initial argument evaluation, cast args to string. (#17595)
Later in the stack, further code will check and inform the user that var names must start with a letter
or underscore, so this fix only allows us to get to that previously existing policy.

Fixes #16008
2016-09-15 16:41:11 -04:00
Matt Davis
f497d771c8 win_shell/win_command changes + tests (#17557) 2016-09-15 11:25:56 -07:00
Toshio Kuratomi
4ed88512e4 Move uses of to_bytes, to_text, to_native to use the module_utils version (#17423)
We couldn't copy to_unicode, to_bytes, to_str into module_utils because
of licensing.  So once created it we had two sets of functions that did
the same things but had different implementations.  To remedy that, this
change removes the ansible.utils.unicode versions of those functions.
2016-09-06 22:54:17 -07:00
Brian Coca
f25ec5adb3 fix action parsing to avoid conflicts agin 2016-08-26 16:15:55 -04:00
Brian Coca
bd9094c925 include_role (role revamp implementation) (#17232)
* attempt #11 to role_include

* fixes from jimi-c

* do not override load_data, move all to load

* removed debugging

* implemented tasks_from parameter, must break cache

* fixed issue with cache and tasks_from

* make resolution of from_tasks prioritize literal

* avoid role dependency dedupe when include_role

* fixed role deps and handlers are now loaded

* simplified code, enabled k=v parsing

used example from jimi-c

* load role defaults for task when include_role

* fixed issue with from_Tasks overriding all subdirs

* corrected priority order of main candidates

* made tasks_from a more generic interface to roles

* fix block inheritance and handler order

* allow vars: clause into included role

* pull vars already processed vs from raw data

* fix from jimi-c blocks i broke

* added back append for dynamic includes

* only allow for basename in from parameter

* fix for docs when no default

* fixed notes

* added include_role to changelog
2016-08-26 13:42:13 -04:00
Adrian Likins
e396d5d508 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
2016-08-23 20:03:11 -04:00
Toshio Kuratomi
76f9935634 Add some missing imports from last night's py3 fixes (#17196) 2016-08-23 08:06:20 -07:00
Toshio Kuratomi
313d4b2c9e Move a path being passed around as a byte string to being passed around as a text string. (#17190)
This is enough to get minimal copy module working on python3

We have t omodify dataloader's path_dwim_relative_stack and everything
that calls it to use text paths instead of byte string paths
2016-08-22 21:55:30 -07:00
Toshio Kuratomi
384a01fcff Fix tmpfile misspelled as tmplfile (#17183) 2016-08-22 11:31:42 -07:00
Dag Wieers
cb5675a29f Remove a useless section, only act on 'shell' (#16205) 2016-08-12 10:13:02 -07:00
Brian Coca
4e14b7b783 warn when searching for an empty string or null 2016-07-26 08:26:07 -04:00
nyasukun
adea1f2b80 fixed memoryerror when coping huge file (#16392)
* fixed

* support both python 2 and 3
2016-07-22 09:06:06 -04:00
Toshio Kuratomi
84c1697271 Only show the traceback for importing cryptography when in Ansible Debug. (#16795) 2016-07-22 05:40:43 -07:00
Shintaro Kaneko
372018dfce Fix typo in lib/ansible/parsing/__init__.py (#16761) 2016-07-20 18:03:50 -04:00
Connor Osborn
b06c61c49b Fix exceptions thrown from cryptography import (#16723)
A simple import of cryptography can throw several types of errors. For example,
if `setuptools` is less than cryptography's minimum requirement of 11.3, then
this import of cryptography will throw a VersionConflict here. An earlier case
threw a DistributionNotFound exception.

An optional dependency should not stop ansible. If the error is more than
an ImportError, log a warning, so that errors can be fixed in ansible or
elsewhere.
2016-07-20 03:32:23 -07:00
Brian Coca
2bb7feec6d Search path (#16387)
* smarter function to figure out relative paths

takes list of paths in order of relevance to current task
and does the dwim magic on them

* shared function for action plugins using new dwim

unify path construction and error info/messaging
made include and role non exclusive
corrected order and now smarter about tasks
includes inside roles are currently broken as they don't provide the correct role data
make dirname full match to avoid corner cases

* migrated action plugins to new dwim function

reported plugins to use exceptions instead of info

* clarified needle
2016-06-28 17:23:30 -04:00
jctanner
1db02dfb71 If decryption of a vaulted file failed, include the filename in the error. (#16329)
Fixes #16327
2016-06-18 09:30:08 -04:00
Peter Oliver
95cfceda98 Catch DistributionNotFound when pycrypto is absent (#15731)
* Catch DistributionNotFound when pycrypto is absent

On Solaris 11, module `pkg_resources` throws `DistributionNotFound` on import if `cryptography` is installed but `pycrypto` is not.  This change causes that situation to be handled gracefully.

I'm not using Paramiko or Vault, so I my understanding is that I don't
need `pycrpto`.  I could install `pycrypto` to make the error go away, but:
- The latest released version of `pycrypto` doesn't build cleanly on Solaris (https://github.com/dlitz/pycrypto/issues/184).
- Solaris includes an old version of GMP that triggers warnings every time Ansible runs (https://github.com/ansible/ansible/issues/6941).  I notice that I can silence these warnings with `system_warnings` in `ansible.cfg`, but not installing `pycrypto` seems like a safer solution.

* Ignore only `pkg_resources.DistributionNotFound`, not other exceptions.
2016-05-19 11:39:34 -07:00
Brian Coca
e0573d3099 make vi the default editor if no EDITOR
fixes #15577
2016-05-03 09:39:19 -04:00
Toshio Kuratomi
b8a988e922 bytes when passing to os.path.* and opening the file, text at other times
Fixes #15644
2016-04-29 22:20:22 -07:00
Toshio Kuratomi
e386a51cf8 Trnasform file name to bytes before opening it to avoid unicode errors if python tries to encode it implicitly 2016-04-19 08:33:01 -07:00
Brian Coca
5940d3d45b fixes to vault/copy
rm _del_ as it might leak memory
renamed to tmp file cleanup
added exception handling when traversing file list, even if one fails try rest
added cleanup to finally to ensure removal in most cases
2016-04-14 14:12:48 -04:00
Cambell
cdf6e3e4bf feature/copy-vault-dataloader: Add method get_real_file(file_path) to dataloader
- get_real_file will decrypt vault encrypted files and return a path to
  a temporary file.

- cleanup_real_file will remove a temporary file created previously with
  get_real_file
2016-04-14 14:12:48 -04:00
James Cammarata
f2713f764c Take previous jinja2 blocks into account in splitter when we see quotes
Previously, split_args() was not taking print/block/comment depth into account
when splitting things, meaning that if there was a quote character inside an
un-quoted variable (ie. {{ foo | some_filter(' ') }}), it was incorrectly
splitting on the quotes instead of continuing to append to the previous param.

Fixes #13630
2016-03-28 15:43:43 -04:00
Konstantin Suvorov
1c922135a0 show error context in args splitter exception 2016-03-24 16:50:21 +03:00
Toshio Kuratomi
2ba4428424 Catch ValueError as well because of El Capitan provoking a bug in python2's subprocess
Fixes #14895
2016-03-18 05:52:53 -07:00
Matt Clay
5b79ed77e7 Use to_bytes on filenames in filesystem calls. 2016-03-04 09:08:41 -08:00
Brian Coca
e762095497 better task parsing errors
fixes #14790
2016-03-03 19:51:15 -05:00
Brian Coca
cc3cb0f65e fix issues with older yaml lib versions
also added missing json import and removed unused ones
2016-03-03 18:13:36 -05:00
Brian Coca
ea5e089056 restore initial json parsing attempt to loader
fixes issues with extra vars json strings not being parsed correctly by the yaml parser
2016-03-03 13:26:50 -05:00
Toshio Kuratomi
4657be4eab Transform pathnames to bytes before passing on to os.path functions 2016-03-03 09:03:28 -08:00
James Cammarata
7c049c3200 Fixing up jsonify and adding unit tests 2016-02-29 14:51:23 -05:00
Toshio Kuratomi
1f2595306a normalize path components to unicode before combining or operating on them
Note that this will break if we deal with non-utf8 paths.  Fixing this
way because converting everythig to byte strings instead is a very
invasive task so it should be done as a specific feature to provide
support for non-utf8 paths at some point in the future (if needed).
2016-02-26 10:29:37 -08:00
Toshio Kuratomi
ef8bec18bf Use a unicode format string so that we don't convert to byte strings
Fixes #14349
2016-02-26 10:29:37 -08:00
Matt Martz
38b663471d Merge pull request #14311 from sivel/unsafe-yaml-constructor
Add new 'unsafe' YAML constructor
2016-02-23 11:29:53 -06:00
Brian Coca
0f73fb0d6f better error messages when failing to decrypt 2016-02-18 08:57:28 -08:00
Jonathan Davila
b220051c14 Added more info to the no action detected error
Error fix
2016-02-08 16:51:10 -05:00
Matt Martz
8bc2d3be9c Add new 'unsafe' YAML constructor 2016-02-04 10:08:42 -06:00
Brian Coca
db375c22af load now does not modify the incomming data
also removed json loader as yaml loader can do both
2016-01-28 19:43:17 -05:00