2018-12-18 02:10:59 +00:00
|
|
|
# Copyright: (c) 2012-2014, Michael DeHaan <michael.dehaan@gmail.com>
|
|
|
|
# Copyright: (c) 2016, Toshio Kuratomi <tkuratomi@ansible.com>
|
|
|
|
# Copyright: (c) 2018, Ansible Project
|
|
|
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
2014-11-14 22:14:08 +00:00
|
|
|
|
|
|
|
# Make coding more python3-ish
|
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
|
|
|
|
2017-06-02 11:14:11 +00:00
|
|
|
import getpass
|
2014-11-14 22:14:08 +00:00
|
|
|
import os
|
2017-06-02 11:14:11 +00:00
|
|
|
import re
|
2018-12-19 08:28:33 +00:00
|
|
|
import subprocess
|
2015-05-01 02:54:38 +00:00
|
|
|
import sys
|
2017-06-02 11:14:11 +00:00
|
|
|
|
2016-09-29 21:14:02 +00:00
|
|
|
from abc import ABCMeta, abstractmethod
|
2014-11-14 22:14:08 +00:00
|
|
|
|
2019-05-22 15:06:54 +00:00
|
|
|
from ansible.cli.arguments import option_helpers as opt_help
|
2014-11-14 22:14:08 +00:00
|
|
|
from ansible import constants as C
|
2018-12-18 02:10:59 +00:00
|
|
|
from ansible import context
|
2019-04-23 18:54:39 +00:00
|
|
|
from ansible.errors import AnsibleError
|
2017-05-23 21:16:49 +00:00
|
|
|
from ansible.inventory.manager import InventoryManager
|
|
|
|
from ansible.module_utils.six import with_metaclass, string_types
|
2016-09-07 05:54:17 +00:00
|
|
|
from ansible.module_utils._text import to_bytes, to_text
|
2017-05-23 21:16:49 +00:00
|
|
|
from ansible.parsing.dataloader import DataLoader
|
2019-05-22 15:06:54 +00:00
|
|
|
from ansible.parsing.vault import PromptVaultSecret, get_file_vault_secret
|
|
|
|
from ansible.plugins.loader import add_all_plugin_dirs
|
2017-06-02 11:14:11 +00:00
|
|
|
from ansible.release import __version__
|
2019-05-22 15:06:54 +00:00
|
|
|
from ansible.utils.collection_loader import set_collection_playbook_paths
|
2018-11-20 23:06:51 +00:00
|
|
|
from ansible.utils.display import Display
|
2017-03-01 11:43:48 +00:00
|
|
|
from ansible.utils.path import unfrackpath
|
2017-05-23 21:16:49 +00:00
|
|
|
from ansible.vars.manager import VariableManager
|
2015-11-10 19:40:55 +00:00
|
|
|
|
2019-04-23 18:54:39 +00:00
|
|
|
try:
|
|
|
|
import argcomplete
|
|
|
|
HAS_ARGCOMPLETE = True
|
|
|
|
except ImportError:
|
|
|
|
HAS_ARGCOMPLETE = False
|
|
|
|
|
2018-12-18 02:10:59 +00:00
|
|
|
|
2018-11-20 23:06:51 +00:00
|
|
|
display = Display()
|
2015-11-10 19:40:55 +00:00
|
|
|
|
2014-11-14 22:14:08 +00:00
|
|
|
|
2016-09-29 21:14:02 +00:00
|
|
|
class CLI(with_metaclass(ABCMeta, object)):
|
2015-04-27 11:31:41 +00:00
|
|
|
''' code behind bin/ansible* programs '''
|
2014-11-14 22:14:08 +00:00
|
|
|
|
2015-05-01 02:54:38 +00:00
|
|
|
_ITALIC = re.compile(r"I\(([^)]+)\)")
|
2017-06-02 11:14:11 +00:00
|
|
|
_BOLD = re.compile(r"B\(([^)]+)\)")
|
2015-05-01 02:54:38 +00:00
|
|
|
_MODULE = re.compile(r"M\(([^)]+)\)")
|
2017-06-02 11:14:11 +00:00
|
|
|
_URL = re.compile(r"U\(([^)]+)\)")
|
|
|
|
_CONST = re.compile(r"C\(([^)]+)\)")
|
2015-05-01 02:54:38 +00:00
|
|
|
|
2017-06-02 11:14:11 +00:00
|
|
|
PAGER = 'less'
|
2017-01-27 21:04:59 +00:00
|
|
|
|
|
|
|
# -F (quit-if-one-screen) -R (allow raw ansi control chars)
|
|
|
|
# -S (chop long lines) -X (disable termcap init and de-init)
|
|
|
|
LESS_OPTS = 'FRSX'
|
2017-10-25 23:55:48 +00:00
|
|
|
SKIP_INVENTORY_DEFAULTS = False
|
2015-05-01 02:54:38 +00:00
|
|
|
|
2015-12-10 13:04:06 +00:00
|
|
|
def __init__(self, args, callback=None):
|
2015-04-27 11:31:41 +00:00
|
|
|
"""
|
|
|
|
Base init method for all command line programs
|
|
|
|
"""
|
|
|
|
|
2019-04-29 20:38:31 +00:00
|
|
|
if not args:
|
|
|
|
raise ValueError('A non-empty list for args is required')
|
|
|
|
|
2015-04-27 11:31:41 +00:00
|
|
|
self.args = args
|
|
|
|
self.parser = None
|
2015-12-10 13:04:06 +00:00
|
|
|
self.callback = callback
|
2015-04-27 11:31:41 +00:00
|
|
|
|
2016-09-29 21:14:02 +00:00
|
|
|
@abstractmethod
|
2015-04-27 11:31:41 +00:00
|
|
|
def run(self):
|
2016-09-29 21:14:02 +00:00
|
|
|
"""Run the ansible command
|
|
|
|
|
|
|
|
Subclasses must implement this method. It does the actual work of
|
|
|
|
running an Ansible command.
|
|
|
|
"""
|
2018-12-18 02:10:59 +00:00
|
|
|
self.parse()
|
2015-07-04 14:23:30 +00:00
|
|
|
|
2019-04-23 18:54:39 +00:00
|
|
|
display.vv(to_text(opt_help.version(self.parser.prog)))
|
2017-05-24 18:45:35 +00:00
|
|
|
|
|
|
|
if C.CONFIG_FILE:
|
|
|
|
display.v(u"Using %s as config file" % to_text(C.CONFIG_FILE))
|
|
|
|
else:
|
|
|
|
display.v(u"No config file found; using defaults")
|
2015-04-27 11:31:41 +00:00
|
|
|
|
2017-09-14 20:56:52 +00:00
|
|
|
# warn about deprecated config options
|
2017-08-20 15:20:30 +00:00
|
|
|
for deprecated in C.config.DEPRECATED:
|
|
|
|
name = deprecated[0]
|
|
|
|
why = deprecated[1]['why']
|
2018-01-10 00:07:06 +00:00
|
|
|
if 'alternatives' in deprecated[1]:
|
|
|
|
alt = ', use %s instead' % deprecated[1]['alternatives']
|
2017-08-20 15:20:30 +00:00
|
|
|
else:
|
|
|
|
alt = ''
|
|
|
|
ver = deprecated[1]['version']
|
|
|
|
display.deprecated("%s option, %s %s" % (name, why, alt), version=ver)
|
|
|
|
|
2015-04-27 11:31:41 +00:00
|
|
|
@staticmethod
|
Support multiple vault passwords (#22756)
Fixes #13243
** Add --vault-id to name/identify multiple vault passwords
Use --vault-id to indicate id and path/type
--vault-id=prompt # prompt for default vault id password
--vault-id=myorg@prompt # prompt for a vault_id named 'myorg'
--vault-id=a_password_file # load ./a_password_file for default id
--vault-id=myorg@a_password_file # load file for 'myorg' vault id
vault_id's are created implicitly for existing --vault-password-file
and --ask-vault-pass options.
Vault ids are just for UX purposes and bookkeeping. Only the vault
payload and the password bytestring is needed to decrypt a
vault blob.
Replace passing password around everywhere with
a VaultSecrets object.
If we specify a vault_id, mention that in password prompts
Specifying multiple -vault-password-files will
now try each until one works
** Rev vault format in a backwards compatible way
The 1.2 vault format adds the vault_id to the header line
of the vault text. This is backwards compatible with older
versions of ansible. Old versions will just ignore it and
treat it as the default (and only) vault id.
Note: only 2.4+ supports multiple vault passwords, so while
earlier ansible versions can read the vault-1.2 format, it
does not make them magically support multiple vault passwords.
use 1.1 format for 'default' vault_id
Vaulted items that need to include a vault_id will be
written in 1.2 format.
If we set a new DEFAULT_VAULT_IDENTITY, then the default will
use version 1.2
vault will only use a vault_id if one is specified. So if none
is specified and C.DEFAULT_VAULT_IDENTITY is 'default'
we use the old format.
** Changes/refactors needed to implement multiple vault passwords
raise exceptions on decrypt fail, check vault id early
split out parsing the vault plaintext envelope (with the
sha/original plaintext) to _split_plaintext_envelope()
some cli fixups for specifying multiple paths in
the unfrack_paths optparse callback
fix py3 dict.keys() 'dict_keys object is not indexable' error
pluralize cli.options.vault_password_file -> vault_password_files
pluralize cli.options.new_vault_password_file -> new_vault_password_files
pluralize cli.options.vault_id -> cli.options.vault_ids
** Add a config option (vault_id_match) to force vault id matching.
With 'vault_id_match=True' and an ansible
vault that provides a vault_id, then decryption will require
that a matching vault_id is required. (via
--vault-id=my_vault_id@password_file, for ex).
In other words, if the config option is true, then only
the vault secrets with matching vault ids are candidates for
decrypting a vault. If option is false (the default), then
all of the provided vault secrets will be selected.
If a user doesn't want all vault secrets to be tried to
decrypt any vault content, they can enable this option.
Note: The vault id used for the match is not encrypted or
cryptographically signed. It is just a label/id/nickname used
for referencing a specific vault secret.
2017-07-28 19:20:58 +00:00
|
|
|
def split_vault_id(vault_id):
|
|
|
|
# return (before_@, after_@)
|
|
|
|
# if no @, return whole string as after_
|
|
|
|
if '@' not in vault_id:
|
|
|
|
return (None, vault_id)
|
2015-04-27 11:31:41 +00:00
|
|
|
|
Support multiple vault passwords (#22756)
Fixes #13243
** Add --vault-id to name/identify multiple vault passwords
Use --vault-id to indicate id and path/type
--vault-id=prompt # prompt for default vault id password
--vault-id=myorg@prompt # prompt for a vault_id named 'myorg'
--vault-id=a_password_file # load ./a_password_file for default id
--vault-id=myorg@a_password_file # load file for 'myorg' vault id
vault_id's are created implicitly for existing --vault-password-file
and --ask-vault-pass options.
Vault ids are just for UX purposes and bookkeeping. Only the vault
payload and the password bytestring is needed to decrypt a
vault blob.
Replace passing password around everywhere with
a VaultSecrets object.
If we specify a vault_id, mention that in password prompts
Specifying multiple -vault-password-files will
now try each until one works
** Rev vault format in a backwards compatible way
The 1.2 vault format adds the vault_id to the header line
of the vault text. This is backwards compatible with older
versions of ansible. Old versions will just ignore it and
treat it as the default (and only) vault id.
Note: only 2.4+ supports multiple vault passwords, so while
earlier ansible versions can read the vault-1.2 format, it
does not make them magically support multiple vault passwords.
use 1.1 format for 'default' vault_id
Vaulted items that need to include a vault_id will be
written in 1.2 format.
If we set a new DEFAULT_VAULT_IDENTITY, then the default will
use version 1.2
vault will only use a vault_id if one is specified. So if none
is specified and C.DEFAULT_VAULT_IDENTITY is 'default'
we use the old format.
** Changes/refactors needed to implement multiple vault passwords
raise exceptions on decrypt fail, check vault id early
split out parsing the vault plaintext envelope (with the
sha/original plaintext) to _split_plaintext_envelope()
some cli fixups for specifying multiple paths in
the unfrack_paths optparse callback
fix py3 dict.keys() 'dict_keys object is not indexable' error
pluralize cli.options.vault_password_file -> vault_password_files
pluralize cli.options.new_vault_password_file -> new_vault_password_files
pluralize cli.options.vault_id -> cli.options.vault_ids
** Add a config option (vault_id_match) to force vault id matching.
With 'vault_id_match=True' and an ansible
vault that provides a vault_id, then decryption will require
that a matching vault_id is required. (via
--vault-id=my_vault_id@password_file, for ex).
In other words, if the config option is true, then only
the vault secrets with matching vault ids are candidates for
decrypting a vault. If option is false (the default), then
all of the provided vault secrets will be selected.
If a user doesn't want all vault secrets to be tried to
decrypt any vault content, they can enable this option.
Note: The vault id used for the match is not encrypted or
cryptographically signed. It is just a label/id/nickname used
for referencing a specific vault secret.
2017-07-28 19:20:58 +00:00
|
|
|
parts = vault_id.split('@', 1)
|
|
|
|
ret = tuple(parts)
|
|
|
|
return ret
|
2016-10-31 16:56:18 +00:00
|
|
|
|
Support multiple vault passwords (#22756)
Fixes #13243
** Add --vault-id to name/identify multiple vault passwords
Use --vault-id to indicate id and path/type
--vault-id=prompt # prompt for default vault id password
--vault-id=myorg@prompt # prompt for a vault_id named 'myorg'
--vault-id=a_password_file # load ./a_password_file for default id
--vault-id=myorg@a_password_file # load file for 'myorg' vault id
vault_id's are created implicitly for existing --vault-password-file
and --ask-vault-pass options.
Vault ids are just for UX purposes and bookkeeping. Only the vault
payload and the password bytestring is needed to decrypt a
vault blob.
Replace passing password around everywhere with
a VaultSecrets object.
If we specify a vault_id, mention that in password prompts
Specifying multiple -vault-password-files will
now try each until one works
** Rev vault format in a backwards compatible way
The 1.2 vault format adds the vault_id to the header line
of the vault text. This is backwards compatible with older
versions of ansible. Old versions will just ignore it and
treat it as the default (and only) vault id.
Note: only 2.4+ supports multiple vault passwords, so while
earlier ansible versions can read the vault-1.2 format, it
does not make them magically support multiple vault passwords.
use 1.1 format for 'default' vault_id
Vaulted items that need to include a vault_id will be
written in 1.2 format.
If we set a new DEFAULT_VAULT_IDENTITY, then the default will
use version 1.2
vault will only use a vault_id if one is specified. So if none
is specified and C.DEFAULT_VAULT_IDENTITY is 'default'
we use the old format.
** Changes/refactors needed to implement multiple vault passwords
raise exceptions on decrypt fail, check vault id early
split out parsing the vault plaintext envelope (with the
sha/original plaintext) to _split_plaintext_envelope()
some cli fixups for specifying multiple paths in
the unfrack_paths optparse callback
fix py3 dict.keys() 'dict_keys object is not indexable' error
pluralize cli.options.vault_password_file -> vault_password_files
pluralize cli.options.new_vault_password_file -> new_vault_password_files
pluralize cli.options.vault_id -> cli.options.vault_ids
** Add a config option (vault_id_match) to force vault id matching.
With 'vault_id_match=True' and an ansible
vault that provides a vault_id, then decryption will require
that a matching vault_id is required. (via
--vault-id=my_vault_id@password_file, for ex).
In other words, if the config option is true, then only
the vault secrets with matching vault ids are candidates for
decrypting a vault. If option is false (the default), then
all of the provided vault secrets will be selected.
If a user doesn't want all vault secrets to be tried to
decrypt any vault content, they can enable this option.
Note: The vault id used for the match is not encrypted or
cryptographically signed. It is just a label/id/nickname used
for referencing a specific vault secret.
2017-07-28 19:20:58 +00:00
|
|
|
@staticmethod
|
2017-08-15 15:01:46 +00:00
|
|
|
def build_vault_ids(vault_ids, vault_password_files=None,
|
2017-09-19 21:39:51 +00:00
|
|
|
ask_vault_pass=None, create_new_password=None,
|
|
|
|
auto_prompt=True):
|
Support multiple vault passwords (#22756)
Fixes #13243
** Add --vault-id to name/identify multiple vault passwords
Use --vault-id to indicate id and path/type
--vault-id=prompt # prompt for default vault id password
--vault-id=myorg@prompt # prompt for a vault_id named 'myorg'
--vault-id=a_password_file # load ./a_password_file for default id
--vault-id=myorg@a_password_file # load file for 'myorg' vault id
vault_id's are created implicitly for existing --vault-password-file
and --ask-vault-pass options.
Vault ids are just for UX purposes and bookkeeping. Only the vault
payload and the password bytestring is needed to decrypt a
vault blob.
Replace passing password around everywhere with
a VaultSecrets object.
If we specify a vault_id, mention that in password prompts
Specifying multiple -vault-password-files will
now try each until one works
** Rev vault format in a backwards compatible way
The 1.2 vault format adds the vault_id to the header line
of the vault text. This is backwards compatible with older
versions of ansible. Old versions will just ignore it and
treat it as the default (and only) vault id.
Note: only 2.4+ supports multiple vault passwords, so while
earlier ansible versions can read the vault-1.2 format, it
does not make them magically support multiple vault passwords.
use 1.1 format for 'default' vault_id
Vaulted items that need to include a vault_id will be
written in 1.2 format.
If we set a new DEFAULT_VAULT_IDENTITY, then the default will
use version 1.2
vault will only use a vault_id if one is specified. So if none
is specified and C.DEFAULT_VAULT_IDENTITY is 'default'
we use the old format.
** Changes/refactors needed to implement multiple vault passwords
raise exceptions on decrypt fail, check vault id early
split out parsing the vault plaintext envelope (with the
sha/original plaintext) to _split_plaintext_envelope()
some cli fixups for specifying multiple paths in
the unfrack_paths optparse callback
fix py3 dict.keys() 'dict_keys object is not indexable' error
pluralize cli.options.vault_password_file -> vault_password_files
pluralize cli.options.new_vault_password_file -> new_vault_password_files
pluralize cli.options.vault_id -> cli.options.vault_ids
** Add a config option (vault_id_match) to force vault id matching.
With 'vault_id_match=True' and an ansible
vault that provides a vault_id, then decryption will require
that a matching vault_id is required. (via
--vault-id=my_vault_id@password_file, for ex).
In other words, if the config option is true, then only
the vault secrets with matching vault ids are candidates for
decrypting a vault. If option is false (the default), then
all of the provided vault secrets will be selected.
If a user doesn't want all vault secrets to be tried to
decrypt any vault content, they can enable this option.
Note: The vault id used for the match is not encrypted or
cryptographically signed. It is just a label/id/nickname used
for referencing a specific vault secret.
2017-07-28 19:20:58 +00:00
|
|
|
vault_password_files = vault_password_files or []
|
|
|
|
vault_ids = vault_ids or []
|
2015-04-27 11:31:41 +00:00
|
|
|
|
Support multiple vault passwords (#22756)
Fixes #13243
** Add --vault-id to name/identify multiple vault passwords
Use --vault-id to indicate id and path/type
--vault-id=prompt # prompt for default vault id password
--vault-id=myorg@prompt # prompt for a vault_id named 'myorg'
--vault-id=a_password_file # load ./a_password_file for default id
--vault-id=myorg@a_password_file # load file for 'myorg' vault id
vault_id's are created implicitly for existing --vault-password-file
and --ask-vault-pass options.
Vault ids are just for UX purposes and bookkeeping. Only the vault
payload and the password bytestring is needed to decrypt a
vault blob.
Replace passing password around everywhere with
a VaultSecrets object.
If we specify a vault_id, mention that in password prompts
Specifying multiple -vault-password-files will
now try each until one works
** Rev vault format in a backwards compatible way
The 1.2 vault format adds the vault_id to the header line
of the vault text. This is backwards compatible with older
versions of ansible. Old versions will just ignore it and
treat it as the default (and only) vault id.
Note: only 2.4+ supports multiple vault passwords, so while
earlier ansible versions can read the vault-1.2 format, it
does not make them magically support multiple vault passwords.
use 1.1 format for 'default' vault_id
Vaulted items that need to include a vault_id will be
written in 1.2 format.
If we set a new DEFAULT_VAULT_IDENTITY, then the default will
use version 1.2
vault will only use a vault_id if one is specified. So if none
is specified and C.DEFAULT_VAULT_IDENTITY is 'default'
we use the old format.
** Changes/refactors needed to implement multiple vault passwords
raise exceptions on decrypt fail, check vault id early
split out parsing the vault plaintext envelope (with the
sha/original plaintext) to _split_plaintext_envelope()
some cli fixups for specifying multiple paths in
the unfrack_paths optparse callback
fix py3 dict.keys() 'dict_keys object is not indexable' error
pluralize cli.options.vault_password_file -> vault_password_files
pluralize cli.options.new_vault_password_file -> new_vault_password_files
pluralize cli.options.vault_id -> cli.options.vault_ids
** Add a config option (vault_id_match) to force vault id matching.
With 'vault_id_match=True' and an ansible
vault that provides a vault_id, then decryption will require
that a matching vault_id is required. (via
--vault-id=my_vault_id@password_file, for ex).
In other words, if the config option is true, then only
the vault secrets with matching vault ids are candidates for
decrypting a vault. If option is false (the default), then
all of the provided vault secrets will be selected.
If a user doesn't want all vault secrets to be tried to
decrypt any vault content, they can enable this option.
Note: The vault id used for the match is not encrypted or
cryptographically signed. It is just a label/id/nickname used
for referencing a specific vault secret.
2017-07-28 19:20:58 +00:00
|
|
|
# convert vault_password_files into vault_ids slugs
|
|
|
|
for password_file in vault_password_files:
|
|
|
|
id_slug = u'%s@%s' % (C.DEFAULT_VAULT_IDENTITY, password_file)
|
2016-10-31 16:56:18 +00:00
|
|
|
|
2018-12-14 09:42:58 +00:00
|
|
|
# note this makes --vault-id higher precedence than --vault-password-file
|
Support multiple vault passwords (#22756)
Fixes #13243
** Add --vault-id to name/identify multiple vault passwords
Use --vault-id to indicate id and path/type
--vault-id=prompt # prompt for default vault id password
--vault-id=myorg@prompt # prompt for a vault_id named 'myorg'
--vault-id=a_password_file # load ./a_password_file for default id
--vault-id=myorg@a_password_file # load file for 'myorg' vault id
vault_id's are created implicitly for existing --vault-password-file
and --ask-vault-pass options.
Vault ids are just for UX purposes and bookkeeping. Only the vault
payload and the password bytestring is needed to decrypt a
vault blob.
Replace passing password around everywhere with
a VaultSecrets object.
If we specify a vault_id, mention that in password prompts
Specifying multiple -vault-password-files will
now try each until one works
** Rev vault format in a backwards compatible way
The 1.2 vault format adds the vault_id to the header line
of the vault text. This is backwards compatible with older
versions of ansible. Old versions will just ignore it and
treat it as the default (and only) vault id.
Note: only 2.4+ supports multiple vault passwords, so while
earlier ansible versions can read the vault-1.2 format, it
does not make them magically support multiple vault passwords.
use 1.1 format for 'default' vault_id
Vaulted items that need to include a vault_id will be
written in 1.2 format.
If we set a new DEFAULT_VAULT_IDENTITY, then the default will
use version 1.2
vault will only use a vault_id if one is specified. So if none
is specified and C.DEFAULT_VAULT_IDENTITY is 'default'
we use the old format.
** Changes/refactors needed to implement multiple vault passwords
raise exceptions on decrypt fail, check vault id early
split out parsing the vault plaintext envelope (with the
sha/original plaintext) to _split_plaintext_envelope()
some cli fixups for specifying multiple paths in
the unfrack_paths optparse callback
fix py3 dict.keys() 'dict_keys object is not indexable' error
pluralize cli.options.vault_password_file -> vault_password_files
pluralize cli.options.new_vault_password_file -> new_vault_password_files
pluralize cli.options.vault_id -> cli.options.vault_ids
** Add a config option (vault_id_match) to force vault id matching.
With 'vault_id_match=True' and an ansible
vault that provides a vault_id, then decryption will require
that a matching vault_id is required. (via
--vault-id=my_vault_id@password_file, for ex).
In other words, if the config option is true, then only
the vault secrets with matching vault ids are candidates for
decrypting a vault. If option is false (the default), then
all of the provided vault secrets will be selected.
If a user doesn't want all vault secrets to be tried to
decrypt any vault content, they can enable this option.
Note: The vault id used for the match is not encrypted or
cryptographically signed. It is just a label/id/nickname used
for referencing a specific vault secret.
2017-07-28 19:20:58 +00:00
|
|
|
# if we want to intertwingle them in order probably need a cli callback to populate vault_ids
|
|
|
|
# used by --vault-id and --vault-password-file
|
|
|
|
vault_ids.append(id_slug)
|
2016-10-31 16:56:18 +00:00
|
|
|
|
2017-08-15 15:01:46 +00:00
|
|
|
# if an action needs an encrypt password (create_new_password=True) and we dont
|
|
|
|
# have other secrets setup, then automatically add a password prompt as well.
|
2017-11-15 19:01:32 +00:00
|
|
|
# prompts cant/shouldnt work without a tty, so dont add prompt secrets
|
|
|
|
if ask_vault_pass or (not vault_ids and auto_prompt):
|
|
|
|
|
2017-08-01 20:39:54 +00:00
|
|
|
id_slug = u'%s@%s' % (C.DEFAULT_VAULT_IDENTITY, u'prompt_ask_vault_pass')
|
Support multiple vault passwords (#22756)
Fixes #13243
** Add --vault-id to name/identify multiple vault passwords
Use --vault-id to indicate id and path/type
--vault-id=prompt # prompt for default vault id password
--vault-id=myorg@prompt # prompt for a vault_id named 'myorg'
--vault-id=a_password_file # load ./a_password_file for default id
--vault-id=myorg@a_password_file # load file for 'myorg' vault id
vault_id's are created implicitly for existing --vault-password-file
and --ask-vault-pass options.
Vault ids are just for UX purposes and bookkeeping. Only the vault
payload and the password bytestring is needed to decrypt a
vault blob.
Replace passing password around everywhere with
a VaultSecrets object.
If we specify a vault_id, mention that in password prompts
Specifying multiple -vault-password-files will
now try each until one works
** Rev vault format in a backwards compatible way
The 1.2 vault format adds the vault_id to the header line
of the vault text. This is backwards compatible with older
versions of ansible. Old versions will just ignore it and
treat it as the default (and only) vault id.
Note: only 2.4+ supports multiple vault passwords, so while
earlier ansible versions can read the vault-1.2 format, it
does not make them magically support multiple vault passwords.
use 1.1 format for 'default' vault_id
Vaulted items that need to include a vault_id will be
written in 1.2 format.
If we set a new DEFAULT_VAULT_IDENTITY, then the default will
use version 1.2
vault will only use a vault_id if one is specified. So if none
is specified and C.DEFAULT_VAULT_IDENTITY is 'default'
we use the old format.
** Changes/refactors needed to implement multiple vault passwords
raise exceptions on decrypt fail, check vault id early
split out parsing the vault plaintext envelope (with the
sha/original plaintext) to _split_plaintext_envelope()
some cli fixups for specifying multiple paths in
the unfrack_paths optparse callback
fix py3 dict.keys() 'dict_keys object is not indexable' error
pluralize cli.options.vault_password_file -> vault_password_files
pluralize cli.options.new_vault_password_file -> new_vault_password_files
pluralize cli.options.vault_id -> cli.options.vault_ids
** Add a config option (vault_id_match) to force vault id matching.
With 'vault_id_match=True' and an ansible
vault that provides a vault_id, then decryption will require
that a matching vault_id is required. (via
--vault-id=my_vault_id@password_file, for ex).
In other words, if the config option is true, then only
the vault secrets with matching vault ids are candidates for
decrypting a vault. If option is false (the default), then
all of the provided vault secrets will be selected.
If a user doesn't want all vault secrets to be tried to
decrypt any vault content, they can enable this option.
Note: The vault id used for the match is not encrypted or
cryptographically signed. It is just a label/id/nickname used
for referencing a specific vault secret.
2017-07-28 19:20:58 +00:00
|
|
|
vault_ids.append(id_slug)
|
|
|
|
|
|
|
|
return vault_ids
|
|
|
|
|
|
|
|
# TODO: remove the now unused args
|
2016-10-31 16:56:18 +00:00
|
|
|
@staticmethod
|
Support multiple vault passwords (#22756)
Fixes #13243
** Add --vault-id to name/identify multiple vault passwords
Use --vault-id to indicate id and path/type
--vault-id=prompt # prompt for default vault id password
--vault-id=myorg@prompt # prompt for a vault_id named 'myorg'
--vault-id=a_password_file # load ./a_password_file for default id
--vault-id=myorg@a_password_file # load file for 'myorg' vault id
vault_id's are created implicitly for existing --vault-password-file
and --ask-vault-pass options.
Vault ids are just for UX purposes and bookkeeping. Only the vault
payload and the password bytestring is needed to decrypt a
vault blob.
Replace passing password around everywhere with
a VaultSecrets object.
If we specify a vault_id, mention that in password prompts
Specifying multiple -vault-password-files will
now try each until one works
** Rev vault format in a backwards compatible way
The 1.2 vault format adds the vault_id to the header line
of the vault text. This is backwards compatible with older
versions of ansible. Old versions will just ignore it and
treat it as the default (and only) vault id.
Note: only 2.4+ supports multiple vault passwords, so while
earlier ansible versions can read the vault-1.2 format, it
does not make them magically support multiple vault passwords.
use 1.1 format for 'default' vault_id
Vaulted items that need to include a vault_id will be
written in 1.2 format.
If we set a new DEFAULT_VAULT_IDENTITY, then the default will
use version 1.2
vault will only use a vault_id if one is specified. So if none
is specified and C.DEFAULT_VAULT_IDENTITY is 'default'
we use the old format.
** Changes/refactors needed to implement multiple vault passwords
raise exceptions on decrypt fail, check vault id early
split out parsing the vault plaintext envelope (with the
sha/original plaintext) to _split_plaintext_envelope()
some cli fixups for specifying multiple paths in
the unfrack_paths optparse callback
fix py3 dict.keys() 'dict_keys object is not indexable' error
pluralize cli.options.vault_password_file -> vault_password_files
pluralize cli.options.new_vault_password_file -> new_vault_password_files
pluralize cli.options.vault_id -> cli.options.vault_ids
** Add a config option (vault_id_match) to force vault id matching.
With 'vault_id_match=True' and an ansible
vault that provides a vault_id, then decryption will require
that a matching vault_id is required. (via
--vault-id=my_vault_id@password_file, for ex).
In other words, if the config option is true, then only
the vault secrets with matching vault ids are candidates for
decrypting a vault. If option is false (the default), then
all of the provided vault secrets will be selected.
If a user doesn't want all vault secrets to be tried to
decrypt any vault content, they can enable this option.
Note: The vault id used for the match is not encrypted or
cryptographically signed. It is just a label/id/nickname used
for referencing a specific vault secret.
2017-07-28 19:20:58 +00:00
|
|
|
def setup_vault_secrets(loader, vault_ids, vault_password_files=None,
|
2017-09-19 21:39:51 +00:00
|
|
|
ask_vault_pass=None, create_new_password=False,
|
|
|
|
auto_prompt=True):
|
Support multiple vault passwords (#22756)
Fixes #13243
** Add --vault-id to name/identify multiple vault passwords
Use --vault-id to indicate id and path/type
--vault-id=prompt # prompt for default vault id password
--vault-id=myorg@prompt # prompt for a vault_id named 'myorg'
--vault-id=a_password_file # load ./a_password_file for default id
--vault-id=myorg@a_password_file # load file for 'myorg' vault id
vault_id's are created implicitly for existing --vault-password-file
and --ask-vault-pass options.
Vault ids are just for UX purposes and bookkeeping. Only the vault
payload and the password bytestring is needed to decrypt a
vault blob.
Replace passing password around everywhere with
a VaultSecrets object.
If we specify a vault_id, mention that in password prompts
Specifying multiple -vault-password-files will
now try each until one works
** Rev vault format in a backwards compatible way
The 1.2 vault format adds the vault_id to the header line
of the vault text. This is backwards compatible with older
versions of ansible. Old versions will just ignore it and
treat it as the default (and only) vault id.
Note: only 2.4+ supports multiple vault passwords, so while
earlier ansible versions can read the vault-1.2 format, it
does not make them magically support multiple vault passwords.
use 1.1 format for 'default' vault_id
Vaulted items that need to include a vault_id will be
written in 1.2 format.
If we set a new DEFAULT_VAULT_IDENTITY, then the default will
use version 1.2
vault will only use a vault_id if one is specified. So if none
is specified and C.DEFAULT_VAULT_IDENTITY is 'default'
we use the old format.
** Changes/refactors needed to implement multiple vault passwords
raise exceptions on decrypt fail, check vault id early
split out parsing the vault plaintext envelope (with the
sha/original plaintext) to _split_plaintext_envelope()
some cli fixups for specifying multiple paths in
the unfrack_paths optparse callback
fix py3 dict.keys() 'dict_keys object is not indexable' error
pluralize cli.options.vault_password_file -> vault_password_files
pluralize cli.options.new_vault_password_file -> new_vault_password_files
pluralize cli.options.vault_id -> cli.options.vault_ids
** Add a config option (vault_id_match) to force vault id matching.
With 'vault_id_match=True' and an ansible
vault that provides a vault_id, then decryption will require
that a matching vault_id is required. (via
--vault-id=my_vault_id@password_file, for ex).
In other words, if the config option is true, then only
the vault secrets with matching vault ids are candidates for
decrypting a vault. If option is false (the default), then
all of the provided vault secrets will be selected.
If a user doesn't want all vault secrets to be tried to
decrypt any vault content, they can enable this option.
Note: The vault id used for the match is not encrypted or
cryptographically signed. It is just a label/id/nickname used
for referencing a specific vault secret.
2017-07-28 19:20:58 +00:00
|
|
|
# list of tuples
|
|
|
|
vault_secrets = []
|
|
|
|
|
2017-08-01 20:39:54 +00:00
|
|
|
# Depending on the vault_id value (including how --ask-vault-pass / --vault-password-file create a vault_id)
|
|
|
|
# we need to show different prompts. This is for compat with older Towers that expect a
|
|
|
|
# certain vault password prompt format, so 'promp_ask_vault_pass' vault_id gets the old format.
|
|
|
|
prompt_formats = {}
|
2017-08-01 22:07:33 +00:00
|
|
|
|
2017-08-15 15:56:17 +00:00
|
|
|
# If there are configured default vault identities, they are considered 'first'
|
|
|
|
# so we prepend them to vault_ids (from cli) here
|
|
|
|
|
2017-08-01 22:07:33 +00:00
|
|
|
vault_password_files = vault_password_files or []
|
|
|
|
if C.DEFAULT_VAULT_PASSWORD_FILE:
|
|
|
|
vault_password_files.append(C.DEFAULT_VAULT_PASSWORD_FILE)
|
|
|
|
|
Support multiple vault passwords (#22756)
Fixes #13243
** Add --vault-id to name/identify multiple vault passwords
Use --vault-id to indicate id and path/type
--vault-id=prompt # prompt for default vault id password
--vault-id=myorg@prompt # prompt for a vault_id named 'myorg'
--vault-id=a_password_file # load ./a_password_file for default id
--vault-id=myorg@a_password_file # load file for 'myorg' vault id
vault_id's are created implicitly for existing --vault-password-file
and --ask-vault-pass options.
Vault ids are just for UX purposes and bookkeeping. Only the vault
payload and the password bytestring is needed to decrypt a
vault blob.
Replace passing password around everywhere with
a VaultSecrets object.
If we specify a vault_id, mention that in password prompts
Specifying multiple -vault-password-files will
now try each until one works
** Rev vault format in a backwards compatible way
The 1.2 vault format adds the vault_id to the header line
of the vault text. This is backwards compatible with older
versions of ansible. Old versions will just ignore it and
treat it as the default (and only) vault id.
Note: only 2.4+ supports multiple vault passwords, so while
earlier ansible versions can read the vault-1.2 format, it
does not make them magically support multiple vault passwords.
use 1.1 format for 'default' vault_id
Vaulted items that need to include a vault_id will be
written in 1.2 format.
If we set a new DEFAULT_VAULT_IDENTITY, then the default will
use version 1.2
vault will only use a vault_id if one is specified. So if none
is specified and C.DEFAULT_VAULT_IDENTITY is 'default'
we use the old format.
** Changes/refactors needed to implement multiple vault passwords
raise exceptions on decrypt fail, check vault id early
split out parsing the vault plaintext envelope (with the
sha/original plaintext) to _split_plaintext_envelope()
some cli fixups for specifying multiple paths in
the unfrack_paths optparse callback
fix py3 dict.keys() 'dict_keys object is not indexable' error
pluralize cli.options.vault_password_file -> vault_password_files
pluralize cli.options.new_vault_password_file -> new_vault_password_files
pluralize cli.options.vault_id -> cli.options.vault_ids
** Add a config option (vault_id_match) to force vault id matching.
With 'vault_id_match=True' and an ansible
vault that provides a vault_id, then decryption will require
that a matching vault_id is required. (via
--vault-id=my_vault_id@password_file, for ex).
In other words, if the config option is true, then only
the vault secrets with matching vault ids are candidates for
decrypting a vault. If option is false (the default), then
all of the provided vault secrets will be selected.
If a user doesn't want all vault secrets to be tried to
decrypt any vault content, they can enable this option.
Note: The vault id used for the match is not encrypted or
cryptographically signed. It is just a label/id/nickname used
for referencing a specific vault secret.
2017-07-28 19:20:58 +00:00
|
|
|
if create_new_password:
|
2017-08-01 20:39:54 +00:00
|
|
|
prompt_formats['prompt'] = ['New vault password (%(vault_id)s): ',
|
2018-12-04 11:04:29 +00:00
|
|
|
'Confirm new vault password (%(vault_id)s): ']
|
2017-08-10 13:34:16 +00:00
|
|
|
# 2.3 format prompts for --ask-vault-pass
|
|
|
|
prompt_formats['prompt_ask_vault_pass'] = ['New Vault password: ',
|
|
|
|
'Confirm New Vault password: ']
|
Support multiple vault passwords (#22756)
Fixes #13243
** Add --vault-id to name/identify multiple vault passwords
Use --vault-id to indicate id and path/type
--vault-id=prompt # prompt for default vault id password
--vault-id=myorg@prompt # prompt for a vault_id named 'myorg'
--vault-id=a_password_file # load ./a_password_file for default id
--vault-id=myorg@a_password_file # load file for 'myorg' vault id
vault_id's are created implicitly for existing --vault-password-file
and --ask-vault-pass options.
Vault ids are just for UX purposes and bookkeeping. Only the vault
payload and the password bytestring is needed to decrypt a
vault blob.
Replace passing password around everywhere with
a VaultSecrets object.
If we specify a vault_id, mention that in password prompts
Specifying multiple -vault-password-files will
now try each until one works
** Rev vault format in a backwards compatible way
The 1.2 vault format adds the vault_id to the header line
of the vault text. This is backwards compatible with older
versions of ansible. Old versions will just ignore it and
treat it as the default (and only) vault id.
Note: only 2.4+ supports multiple vault passwords, so while
earlier ansible versions can read the vault-1.2 format, it
does not make them magically support multiple vault passwords.
use 1.1 format for 'default' vault_id
Vaulted items that need to include a vault_id will be
written in 1.2 format.
If we set a new DEFAULT_VAULT_IDENTITY, then the default will
use version 1.2
vault will only use a vault_id if one is specified. So if none
is specified and C.DEFAULT_VAULT_IDENTITY is 'default'
we use the old format.
** Changes/refactors needed to implement multiple vault passwords
raise exceptions on decrypt fail, check vault id early
split out parsing the vault plaintext envelope (with the
sha/original plaintext) to _split_plaintext_envelope()
some cli fixups for specifying multiple paths in
the unfrack_paths optparse callback
fix py3 dict.keys() 'dict_keys object is not indexable' error
pluralize cli.options.vault_password_file -> vault_password_files
pluralize cli.options.new_vault_password_file -> new_vault_password_files
pluralize cli.options.vault_id -> cli.options.vault_ids
** Add a config option (vault_id_match) to force vault id matching.
With 'vault_id_match=True' and an ansible
vault that provides a vault_id, then decryption will require
that a matching vault_id is required. (via
--vault-id=my_vault_id@password_file, for ex).
In other words, if the config option is true, then only
the vault secrets with matching vault ids are candidates for
decrypting a vault. If option is false (the default), then
all of the provided vault secrets will be selected.
If a user doesn't want all vault secrets to be tried to
decrypt any vault content, they can enable this option.
Note: The vault id used for the match is not encrypted or
cryptographically signed. It is just a label/id/nickname used
for referencing a specific vault secret.
2017-07-28 19:20:58 +00:00
|
|
|
else:
|
2017-08-01 20:39:54 +00:00
|
|
|
prompt_formats['prompt'] = ['Vault password (%(vault_id)s): ']
|
|
|
|
# The format when we use just --ask-vault-pass needs to match 'Vault password:\s*?$'
|
|
|
|
prompt_formats['prompt_ask_vault_pass'] = ['Vault password: ']
|
Support multiple vault passwords (#22756)
Fixes #13243
** Add --vault-id to name/identify multiple vault passwords
Use --vault-id to indicate id and path/type
--vault-id=prompt # prompt for default vault id password
--vault-id=myorg@prompt # prompt for a vault_id named 'myorg'
--vault-id=a_password_file # load ./a_password_file for default id
--vault-id=myorg@a_password_file # load file for 'myorg' vault id
vault_id's are created implicitly for existing --vault-password-file
and --ask-vault-pass options.
Vault ids are just for UX purposes and bookkeeping. Only the vault
payload and the password bytestring is needed to decrypt a
vault blob.
Replace passing password around everywhere with
a VaultSecrets object.
If we specify a vault_id, mention that in password prompts
Specifying multiple -vault-password-files will
now try each until one works
** Rev vault format in a backwards compatible way
The 1.2 vault format adds the vault_id to the header line
of the vault text. This is backwards compatible with older
versions of ansible. Old versions will just ignore it and
treat it as the default (and only) vault id.
Note: only 2.4+ supports multiple vault passwords, so while
earlier ansible versions can read the vault-1.2 format, it
does not make them magically support multiple vault passwords.
use 1.1 format for 'default' vault_id
Vaulted items that need to include a vault_id will be
written in 1.2 format.
If we set a new DEFAULT_VAULT_IDENTITY, then the default will
use version 1.2
vault will only use a vault_id if one is specified. So if none
is specified and C.DEFAULT_VAULT_IDENTITY is 'default'
we use the old format.
** Changes/refactors needed to implement multiple vault passwords
raise exceptions on decrypt fail, check vault id early
split out parsing the vault plaintext envelope (with the
sha/original plaintext) to _split_plaintext_envelope()
some cli fixups for specifying multiple paths in
the unfrack_paths optparse callback
fix py3 dict.keys() 'dict_keys object is not indexable' error
pluralize cli.options.vault_password_file -> vault_password_files
pluralize cli.options.new_vault_password_file -> new_vault_password_files
pluralize cli.options.vault_id -> cli.options.vault_ids
** Add a config option (vault_id_match) to force vault id matching.
With 'vault_id_match=True' and an ansible
vault that provides a vault_id, then decryption will require
that a matching vault_id is required. (via
--vault-id=my_vault_id@password_file, for ex).
In other words, if the config option is true, then only
the vault secrets with matching vault ids are candidates for
decrypting a vault. If option is false (the default), then
all of the provided vault secrets will be selected.
If a user doesn't want all vault secrets to be tried to
decrypt any vault content, they can enable this option.
Note: The vault id used for the match is not encrypted or
cryptographically signed. It is just a label/id/nickname used
for referencing a specific vault secret.
2017-07-28 19:20:58 +00:00
|
|
|
|
|
|
|
vault_ids = CLI.build_vault_ids(vault_ids,
|
|
|
|
vault_password_files,
|
2017-08-15 17:09:24 +00:00
|
|
|
ask_vault_pass,
|
2017-09-19 21:39:51 +00:00
|
|
|
create_new_password,
|
|
|
|
auto_prompt=auto_prompt)
|
Support multiple vault passwords (#22756)
Fixes #13243
** Add --vault-id to name/identify multiple vault passwords
Use --vault-id to indicate id and path/type
--vault-id=prompt # prompt for default vault id password
--vault-id=myorg@prompt # prompt for a vault_id named 'myorg'
--vault-id=a_password_file # load ./a_password_file for default id
--vault-id=myorg@a_password_file # load file for 'myorg' vault id
vault_id's are created implicitly for existing --vault-password-file
and --ask-vault-pass options.
Vault ids are just for UX purposes and bookkeeping. Only the vault
payload and the password bytestring is needed to decrypt a
vault blob.
Replace passing password around everywhere with
a VaultSecrets object.
If we specify a vault_id, mention that in password prompts
Specifying multiple -vault-password-files will
now try each until one works
** Rev vault format in a backwards compatible way
The 1.2 vault format adds the vault_id to the header line
of the vault text. This is backwards compatible with older
versions of ansible. Old versions will just ignore it and
treat it as the default (and only) vault id.
Note: only 2.4+ supports multiple vault passwords, so while
earlier ansible versions can read the vault-1.2 format, it
does not make them magically support multiple vault passwords.
use 1.1 format for 'default' vault_id
Vaulted items that need to include a vault_id will be
written in 1.2 format.
If we set a new DEFAULT_VAULT_IDENTITY, then the default will
use version 1.2
vault will only use a vault_id if one is specified. So if none
is specified and C.DEFAULT_VAULT_IDENTITY is 'default'
we use the old format.
** Changes/refactors needed to implement multiple vault passwords
raise exceptions on decrypt fail, check vault id early
split out parsing the vault plaintext envelope (with the
sha/original plaintext) to _split_plaintext_envelope()
some cli fixups for specifying multiple paths in
the unfrack_paths optparse callback
fix py3 dict.keys() 'dict_keys object is not indexable' error
pluralize cli.options.vault_password_file -> vault_password_files
pluralize cli.options.new_vault_password_file -> new_vault_password_files
pluralize cli.options.vault_id -> cli.options.vault_ids
** Add a config option (vault_id_match) to force vault id matching.
With 'vault_id_match=True' and an ansible
vault that provides a vault_id, then decryption will require
that a matching vault_id is required. (via
--vault-id=my_vault_id@password_file, for ex).
In other words, if the config option is true, then only
the vault secrets with matching vault ids are candidates for
decrypting a vault. If option is false (the default), then
all of the provided vault secrets will be selected.
If a user doesn't want all vault secrets to be tried to
decrypt any vault content, they can enable this option.
Note: The vault id used for the match is not encrypted or
cryptographically signed. It is just a label/id/nickname used
for referencing a specific vault secret.
2017-07-28 19:20:58 +00:00
|
|
|
|
2017-08-08 20:10:03 +00:00
|
|
|
for vault_id_slug in vault_ids:
|
Support multiple vault passwords (#22756)
Fixes #13243
** Add --vault-id to name/identify multiple vault passwords
Use --vault-id to indicate id and path/type
--vault-id=prompt # prompt for default vault id password
--vault-id=myorg@prompt # prompt for a vault_id named 'myorg'
--vault-id=a_password_file # load ./a_password_file for default id
--vault-id=myorg@a_password_file # load file for 'myorg' vault id
vault_id's are created implicitly for existing --vault-password-file
and --ask-vault-pass options.
Vault ids are just for UX purposes and bookkeeping. Only the vault
payload and the password bytestring is needed to decrypt a
vault blob.
Replace passing password around everywhere with
a VaultSecrets object.
If we specify a vault_id, mention that in password prompts
Specifying multiple -vault-password-files will
now try each until one works
** Rev vault format in a backwards compatible way
The 1.2 vault format adds the vault_id to the header line
of the vault text. This is backwards compatible with older
versions of ansible. Old versions will just ignore it and
treat it as the default (and only) vault id.
Note: only 2.4+ supports multiple vault passwords, so while
earlier ansible versions can read the vault-1.2 format, it
does not make them magically support multiple vault passwords.
use 1.1 format for 'default' vault_id
Vaulted items that need to include a vault_id will be
written in 1.2 format.
If we set a new DEFAULT_VAULT_IDENTITY, then the default will
use version 1.2
vault will only use a vault_id if one is specified. So if none
is specified and C.DEFAULT_VAULT_IDENTITY is 'default'
we use the old format.
** Changes/refactors needed to implement multiple vault passwords
raise exceptions on decrypt fail, check vault id early
split out parsing the vault plaintext envelope (with the
sha/original plaintext) to _split_plaintext_envelope()
some cli fixups for specifying multiple paths in
the unfrack_paths optparse callback
fix py3 dict.keys() 'dict_keys object is not indexable' error
pluralize cli.options.vault_password_file -> vault_password_files
pluralize cli.options.new_vault_password_file -> new_vault_password_files
pluralize cli.options.vault_id -> cli.options.vault_ids
** Add a config option (vault_id_match) to force vault id matching.
With 'vault_id_match=True' and an ansible
vault that provides a vault_id, then decryption will require
that a matching vault_id is required. (via
--vault-id=my_vault_id@password_file, for ex).
In other words, if the config option is true, then only
the vault secrets with matching vault ids are candidates for
decrypting a vault. If option is false (the default), then
all of the provided vault secrets will be selected.
If a user doesn't want all vault secrets to be tried to
decrypt any vault content, they can enable this option.
Note: The vault id used for the match is not encrypted or
cryptographically signed. It is just a label/id/nickname used
for referencing a specific vault secret.
2017-07-28 19:20:58 +00:00
|
|
|
vault_id_name, vault_id_value = CLI.split_vault_id(vault_id_slug)
|
2017-08-01 20:39:54 +00:00
|
|
|
if vault_id_value in ['prompt', 'prompt_ask_vault_pass']:
|
2017-08-15 17:09:24 +00:00
|
|
|
|
2017-08-01 20:39:54 +00:00
|
|
|
# --vault-id some_name@prompt_ask_vault_pass --vault-id other_name@prompt_ask_vault_pass will be a little
|
|
|
|
# confusing since it will use the old format without the vault id in the prompt
|
2017-08-15 15:01:46 +00:00
|
|
|
built_vault_id = vault_id_name or C.DEFAULT_VAULT_IDENTITY
|
|
|
|
|
|
|
|
# choose the prompt based on --vault-id=prompt or --ask-vault-pass. --ask-vault-pass
|
|
|
|
# always gets the old format for Tower compatibility.
|
|
|
|
# ie, we used --ask-vault-pass, so we need to use the old vault password prompt
|
|
|
|
# format since Tower needs to match on that format.
|
|
|
|
prompted_vault_secret = PromptVaultSecret(prompt_formats=prompt_formats[vault_id_value],
|
|
|
|
vault_id=built_vault_id)
|
|
|
|
|
|
|
|
# a empty or invalid password from the prompt will warn and continue to the next
|
2018-12-14 09:42:58 +00:00
|
|
|
# without erroring globally
|
2017-08-15 15:01:46 +00:00
|
|
|
try:
|
Support multiple vault passwords (#22756)
Fixes #13243
** Add --vault-id to name/identify multiple vault passwords
Use --vault-id to indicate id and path/type
--vault-id=prompt # prompt for default vault id password
--vault-id=myorg@prompt # prompt for a vault_id named 'myorg'
--vault-id=a_password_file # load ./a_password_file for default id
--vault-id=myorg@a_password_file # load file for 'myorg' vault id
vault_id's are created implicitly for existing --vault-password-file
and --ask-vault-pass options.
Vault ids are just for UX purposes and bookkeeping. Only the vault
payload and the password bytestring is needed to decrypt a
vault blob.
Replace passing password around everywhere with
a VaultSecrets object.
If we specify a vault_id, mention that in password prompts
Specifying multiple -vault-password-files will
now try each until one works
** Rev vault format in a backwards compatible way
The 1.2 vault format adds the vault_id to the header line
of the vault text. This is backwards compatible with older
versions of ansible. Old versions will just ignore it and
treat it as the default (and only) vault id.
Note: only 2.4+ supports multiple vault passwords, so while
earlier ansible versions can read the vault-1.2 format, it
does not make them magically support multiple vault passwords.
use 1.1 format for 'default' vault_id
Vaulted items that need to include a vault_id will be
written in 1.2 format.
If we set a new DEFAULT_VAULT_IDENTITY, then the default will
use version 1.2
vault will only use a vault_id if one is specified. So if none
is specified and C.DEFAULT_VAULT_IDENTITY is 'default'
we use the old format.
** Changes/refactors needed to implement multiple vault passwords
raise exceptions on decrypt fail, check vault id early
split out parsing the vault plaintext envelope (with the
sha/original plaintext) to _split_plaintext_envelope()
some cli fixups for specifying multiple paths in
the unfrack_paths optparse callback
fix py3 dict.keys() 'dict_keys object is not indexable' error
pluralize cli.options.vault_password_file -> vault_password_files
pluralize cli.options.new_vault_password_file -> new_vault_password_files
pluralize cli.options.vault_id -> cli.options.vault_ids
** Add a config option (vault_id_match) to force vault id matching.
With 'vault_id_match=True' and an ansible
vault that provides a vault_id, then decryption will require
that a matching vault_id is required. (via
--vault-id=my_vault_id@password_file, for ex).
In other words, if the config option is true, then only
the vault secrets with matching vault ids are candidates for
decrypting a vault. If option is false (the default), then
all of the provided vault secrets will be selected.
If a user doesn't want all vault secrets to be tried to
decrypt any vault content, they can enable this option.
Note: The vault id used for the match is not encrypted or
cryptographically signed. It is just a label/id/nickname used
for referencing a specific vault secret.
2017-07-28 19:20:58 +00:00
|
|
|
prompted_vault_secret.load()
|
2017-08-15 15:01:46 +00:00
|
|
|
except AnsibleError as exc:
|
|
|
|
display.warning('Error in vault password prompt (%s): %s' % (vault_id_name, exc))
|
|
|
|
raise
|
|
|
|
|
|
|
|
vault_secrets.append((built_vault_id, prompted_vault_secret))
|
Support multiple vault passwords (#22756)
Fixes #13243
** Add --vault-id to name/identify multiple vault passwords
Use --vault-id to indicate id and path/type
--vault-id=prompt # prompt for default vault id password
--vault-id=myorg@prompt # prompt for a vault_id named 'myorg'
--vault-id=a_password_file # load ./a_password_file for default id
--vault-id=myorg@a_password_file # load file for 'myorg' vault id
vault_id's are created implicitly for existing --vault-password-file
and --ask-vault-pass options.
Vault ids are just for UX purposes and bookkeeping. Only the vault
payload and the password bytestring is needed to decrypt a
vault blob.
Replace passing password around everywhere with
a VaultSecrets object.
If we specify a vault_id, mention that in password prompts
Specifying multiple -vault-password-files will
now try each until one works
** Rev vault format in a backwards compatible way
The 1.2 vault format adds the vault_id to the header line
of the vault text. This is backwards compatible with older
versions of ansible. Old versions will just ignore it and
treat it as the default (and only) vault id.
Note: only 2.4+ supports multiple vault passwords, so while
earlier ansible versions can read the vault-1.2 format, it
does not make them magically support multiple vault passwords.
use 1.1 format for 'default' vault_id
Vaulted items that need to include a vault_id will be
written in 1.2 format.
If we set a new DEFAULT_VAULT_IDENTITY, then the default will
use version 1.2
vault will only use a vault_id if one is specified. So if none
is specified and C.DEFAULT_VAULT_IDENTITY is 'default'
we use the old format.
** Changes/refactors needed to implement multiple vault passwords
raise exceptions on decrypt fail, check vault id early
split out parsing the vault plaintext envelope (with the
sha/original plaintext) to _split_plaintext_envelope()
some cli fixups for specifying multiple paths in
the unfrack_paths optparse callback
fix py3 dict.keys() 'dict_keys object is not indexable' error
pluralize cli.options.vault_password_file -> vault_password_files
pluralize cli.options.new_vault_password_file -> new_vault_password_files
pluralize cli.options.vault_id -> cli.options.vault_ids
** Add a config option (vault_id_match) to force vault id matching.
With 'vault_id_match=True' and an ansible
vault that provides a vault_id, then decryption will require
that a matching vault_id is required. (via
--vault-id=my_vault_id@password_file, for ex).
In other words, if the config option is true, then only
the vault secrets with matching vault ids are candidates for
decrypting a vault. If option is false (the default), then
all of the provided vault secrets will be selected.
If a user doesn't want all vault secrets to be tried to
decrypt any vault content, they can enable this option.
Note: The vault id used for the match is not encrypted or
cryptographically signed. It is just a label/id/nickname used
for referencing a specific vault secret.
2017-07-28 19:20:58 +00:00
|
|
|
|
|
|
|
# update loader with new secrets incrementally, so we can load a vault password
|
|
|
|
# that is encrypted with a vault secret provided earlier
|
|
|
|
loader.set_vault_secrets(vault_secrets)
|
|
|
|
continue
|
|
|
|
|
|
|
|
# assuming anything else is a password file
|
|
|
|
display.vvvvv('Reading vault password file: %s' % vault_id_value)
|
|
|
|
# read vault_pass from a file
|
|
|
|
file_vault_secret = get_file_vault_secret(filename=vault_id_value,
|
2017-10-13 19:23:08 +00:00
|
|
|
vault_id=vault_id_name,
|
Support multiple vault passwords (#22756)
Fixes #13243
** Add --vault-id to name/identify multiple vault passwords
Use --vault-id to indicate id and path/type
--vault-id=prompt # prompt for default vault id password
--vault-id=myorg@prompt # prompt for a vault_id named 'myorg'
--vault-id=a_password_file # load ./a_password_file for default id
--vault-id=myorg@a_password_file # load file for 'myorg' vault id
vault_id's are created implicitly for existing --vault-password-file
and --ask-vault-pass options.
Vault ids are just for UX purposes and bookkeeping. Only the vault
payload and the password bytestring is needed to decrypt a
vault blob.
Replace passing password around everywhere with
a VaultSecrets object.
If we specify a vault_id, mention that in password prompts
Specifying multiple -vault-password-files will
now try each until one works
** Rev vault format in a backwards compatible way
The 1.2 vault format adds the vault_id to the header line
of the vault text. This is backwards compatible with older
versions of ansible. Old versions will just ignore it and
treat it as the default (and only) vault id.
Note: only 2.4+ supports multiple vault passwords, so while
earlier ansible versions can read the vault-1.2 format, it
does not make them magically support multiple vault passwords.
use 1.1 format for 'default' vault_id
Vaulted items that need to include a vault_id will be
written in 1.2 format.
If we set a new DEFAULT_VAULT_IDENTITY, then the default will
use version 1.2
vault will only use a vault_id if one is specified. So if none
is specified and C.DEFAULT_VAULT_IDENTITY is 'default'
we use the old format.
** Changes/refactors needed to implement multiple vault passwords
raise exceptions on decrypt fail, check vault id early
split out parsing the vault plaintext envelope (with the
sha/original plaintext) to _split_plaintext_envelope()
some cli fixups for specifying multiple paths in
the unfrack_paths optparse callback
fix py3 dict.keys() 'dict_keys object is not indexable' error
pluralize cli.options.vault_password_file -> vault_password_files
pluralize cli.options.new_vault_password_file -> new_vault_password_files
pluralize cli.options.vault_id -> cli.options.vault_ids
** Add a config option (vault_id_match) to force vault id matching.
With 'vault_id_match=True' and an ansible
vault that provides a vault_id, then decryption will require
that a matching vault_id is required. (via
--vault-id=my_vault_id@password_file, for ex).
In other words, if the config option is true, then only
the vault secrets with matching vault ids are candidates for
decrypting a vault. If option is false (the default), then
all of the provided vault secrets will be selected.
If a user doesn't want all vault secrets to be tried to
decrypt any vault content, they can enable this option.
Note: The vault id used for the match is not encrypted or
cryptographically signed. It is just a label/id/nickname used
for referencing a specific vault secret.
2017-07-28 19:20:58 +00:00
|
|
|
loader=loader)
|
2017-08-15 15:01:46 +00:00
|
|
|
|
|
|
|
# an invalid password file will error globally
|
|
|
|
try:
|
|
|
|
file_vault_secret.load()
|
|
|
|
except AnsibleError as exc:
|
2019-01-17 17:18:01 +00:00
|
|
|
display.warning('Error in vault password file loading (%s): %s' % (vault_id_name, to_text(exc)))
|
2017-08-15 15:01:46 +00:00
|
|
|
raise
|
|
|
|
|
Support multiple vault passwords (#22756)
Fixes #13243
** Add --vault-id to name/identify multiple vault passwords
Use --vault-id to indicate id and path/type
--vault-id=prompt # prompt for default vault id password
--vault-id=myorg@prompt # prompt for a vault_id named 'myorg'
--vault-id=a_password_file # load ./a_password_file for default id
--vault-id=myorg@a_password_file # load file for 'myorg' vault id
vault_id's are created implicitly for existing --vault-password-file
and --ask-vault-pass options.
Vault ids are just for UX purposes and bookkeeping. Only the vault
payload and the password bytestring is needed to decrypt a
vault blob.
Replace passing password around everywhere with
a VaultSecrets object.
If we specify a vault_id, mention that in password prompts
Specifying multiple -vault-password-files will
now try each until one works
** Rev vault format in a backwards compatible way
The 1.2 vault format adds the vault_id to the header line
of the vault text. This is backwards compatible with older
versions of ansible. Old versions will just ignore it and
treat it as the default (and only) vault id.
Note: only 2.4+ supports multiple vault passwords, so while
earlier ansible versions can read the vault-1.2 format, it
does not make them magically support multiple vault passwords.
use 1.1 format for 'default' vault_id
Vaulted items that need to include a vault_id will be
written in 1.2 format.
If we set a new DEFAULT_VAULT_IDENTITY, then the default will
use version 1.2
vault will only use a vault_id if one is specified. So if none
is specified and C.DEFAULT_VAULT_IDENTITY is 'default'
we use the old format.
** Changes/refactors needed to implement multiple vault passwords
raise exceptions on decrypt fail, check vault id early
split out parsing the vault plaintext envelope (with the
sha/original plaintext) to _split_plaintext_envelope()
some cli fixups for specifying multiple paths in
the unfrack_paths optparse callback
fix py3 dict.keys() 'dict_keys object is not indexable' error
pluralize cli.options.vault_password_file -> vault_password_files
pluralize cli.options.new_vault_password_file -> new_vault_password_files
pluralize cli.options.vault_id -> cli.options.vault_ids
** Add a config option (vault_id_match) to force vault id matching.
With 'vault_id_match=True' and an ansible
vault that provides a vault_id, then decryption will require
that a matching vault_id is required. (via
--vault-id=my_vault_id@password_file, for ex).
In other words, if the config option is true, then only
the vault secrets with matching vault ids are candidates for
decrypting a vault. If option is false (the default), then
all of the provided vault secrets will be selected.
If a user doesn't want all vault secrets to be tried to
decrypt any vault content, they can enable this option.
Note: The vault id used for the match is not encrypted or
cryptographically signed. It is just a label/id/nickname used
for referencing a specific vault secret.
2017-07-28 19:20:58 +00:00
|
|
|
if vault_id_name:
|
|
|
|
vault_secrets.append((vault_id_name, file_vault_secret))
|
|
|
|
else:
|
|
|
|
vault_secrets.append((C.DEFAULT_VAULT_IDENTITY, file_vault_secret))
|
2016-10-31 16:56:18 +00:00
|
|
|
|
Support multiple vault passwords (#22756)
Fixes #13243
** Add --vault-id to name/identify multiple vault passwords
Use --vault-id to indicate id and path/type
--vault-id=prompt # prompt for default vault id password
--vault-id=myorg@prompt # prompt for a vault_id named 'myorg'
--vault-id=a_password_file # load ./a_password_file for default id
--vault-id=myorg@a_password_file # load file for 'myorg' vault id
vault_id's are created implicitly for existing --vault-password-file
and --ask-vault-pass options.
Vault ids are just for UX purposes and bookkeeping. Only the vault
payload and the password bytestring is needed to decrypt a
vault blob.
Replace passing password around everywhere with
a VaultSecrets object.
If we specify a vault_id, mention that in password prompts
Specifying multiple -vault-password-files will
now try each until one works
** Rev vault format in a backwards compatible way
The 1.2 vault format adds the vault_id to the header line
of the vault text. This is backwards compatible with older
versions of ansible. Old versions will just ignore it and
treat it as the default (and only) vault id.
Note: only 2.4+ supports multiple vault passwords, so while
earlier ansible versions can read the vault-1.2 format, it
does not make them magically support multiple vault passwords.
use 1.1 format for 'default' vault_id
Vaulted items that need to include a vault_id will be
written in 1.2 format.
If we set a new DEFAULT_VAULT_IDENTITY, then the default will
use version 1.2
vault will only use a vault_id if one is specified. So if none
is specified and C.DEFAULT_VAULT_IDENTITY is 'default'
we use the old format.
** Changes/refactors needed to implement multiple vault passwords
raise exceptions on decrypt fail, check vault id early
split out parsing the vault plaintext envelope (with the
sha/original plaintext) to _split_plaintext_envelope()
some cli fixups for specifying multiple paths in
the unfrack_paths optparse callback
fix py3 dict.keys() 'dict_keys object is not indexable' error
pluralize cli.options.vault_password_file -> vault_password_files
pluralize cli.options.new_vault_password_file -> new_vault_password_files
pluralize cli.options.vault_id -> cli.options.vault_ids
** Add a config option (vault_id_match) to force vault id matching.
With 'vault_id_match=True' and an ansible
vault that provides a vault_id, then decryption will require
that a matching vault_id is required. (via
--vault-id=my_vault_id@password_file, for ex).
In other words, if the config option is true, then only
the vault secrets with matching vault ids are candidates for
decrypting a vault. If option is false (the default), then
all of the provided vault secrets will be selected.
If a user doesn't want all vault secrets to be tried to
decrypt any vault content, they can enable this option.
Note: The vault id used for the match is not encrypted or
cryptographically signed. It is just a label/id/nickname used
for referencing a specific vault secret.
2017-07-28 19:20:58 +00:00
|
|
|
# update loader with as-yet-known vault secrets
|
|
|
|
loader.set_vault_secrets(vault_secrets)
|
2015-04-27 11:31:41 +00:00
|
|
|
|
Support multiple vault passwords (#22756)
Fixes #13243
** Add --vault-id to name/identify multiple vault passwords
Use --vault-id to indicate id and path/type
--vault-id=prompt # prompt for default vault id password
--vault-id=myorg@prompt # prompt for a vault_id named 'myorg'
--vault-id=a_password_file # load ./a_password_file for default id
--vault-id=myorg@a_password_file # load file for 'myorg' vault id
vault_id's are created implicitly for existing --vault-password-file
and --ask-vault-pass options.
Vault ids are just for UX purposes and bookkeeping. Only the vault
payload and the password bytestring is needed to decrypt a
vault blob.
Replace passing password around everywhere with
a VaultSecrets object.
If we specify a vault_id, mention that in password prompts
Specifying multiple -vault-password-files will
now try each until one works
** Rev vault format in a backwards compatible way
The 1.2 vault format adds the vault_id to the header line
of the vault text. This is backwards compatible with older
versions of ansible. Old versions will just ignore it and
treat it as the default (and only) vault id.
Note: only 2.4+ supports multiple vault passwords, so while
earlier ansible versions can read the vault-1.2 format, it
does not make them magically support multiple vault passwords.
use 1.1 format for 'default' vault_id
Vaulted items that need to include a vault_id will be
written in 1.2 format.
If we set a new DEFAULT_VAULT_IDENTITY, then the default will
use version 1.2
vault will only use a vault_id if one is specified. So if none
is specified and C.DEFAULT_VAULT_IDENTITY is 'default'
we use the old format.
** Changes/refactors needed to implement multiple vault passwords
raise exceptions on decrypt fail, check vault id early
split out parsing the vault plaintext envelope (with the
sha/original plaintext) to _split_plaintext_envelope()
some cli fixups for specifying multiple paths in
the unfrack_paths optparse callback
fix py3 dict.keys() 'dict_keys object is not indexable' error
pluralize cli.options.vault_password_file -> vault_password_files
pluralize cli.options.new_vault_password_file -> new_vault_password_files
pluralize cli.options.vault_id -> cli.options.vault_ids
** Add a config option (vault_id_match) to force vault id matching.
With 'vault_id_match=True' and an ansible
vault that provides a vault_id, then decryption will require
that a matching vault_id is required. (via
--vault-id=my_vault_id@password_file, for ex).
In other words, if the config option is true, then only
the vault secrets with matching vault ids are candidates for
decrypting a vault. If option is false (the default), then
all of the provided vault secrets will be selected.
If a user doesn't want all vault secrets to be tried to
decrypt any vault content, they can enable this option.
Note: The vault id used for the match is not encrypted or
cryptographically signed. It is just a label/id/nickname used
for referencing a specific vault secret.
2017-07-28 19:20:58 +00:00
|
|
|
return vault_secrets
|
2015-04-27 11:31:41 +00:00
|
|
|
|
2018-12-20 04:45:47 +00:00
|
|
|
@staticmethod
|
|
|
|
def ask_passwords():
|
2015-04-30 22:43:53 +00:00
|
|
|
''' prompt for connection and become passwords if needed '''
|
2015-04-27 11:31:41 +00:00
|
|
|
|
2018-12-18 02:10:59 +00:00
|
|
|
op = context.CLIARGS
|
2015-04-27 11:31:41 +00:00
|
|
|
sshpass = None
|
|
|
|
becomepass = None
|
|
|
|
become_prompt = ''
|
|
|
|
|
2018-12-18 02:10:59 +00:00
|
|
|
become_prompt_method = "BECOME" if C.AGNOSTIC_BECOME_PROMPT else op['become_method'].upper()
|
2018-01-12 16:28:46 +00:00
|
|
|
|
2015-07-01 09:21:46 +00:00
|
|
|
try:
|
2018-12-18 02:10:59 +00:00
|
|
|
if op['ask_pass']:
|
2015-07-01 09:21:46 +00:00
|
|
|
sshpass = getpass.getpass(prompt="SSH password: ")
|
2018-01-12 16:28:46 +00:00
|
|
|
become_prompt = "%s password[defaults to SSH password]: " % become_prompt_method
|
2015-07-01 09:21:46 +00:00
|
|
|
if sshpass:
|
2018-07-11 14:24:45 +00:00
|
|
|
sshpass = to_bytes(sshpass, errors='strict', nonstring='simplerepr')
|
2015-07-01 09:21:46 +00:00
|
|
|
else:
|
2018-01-12 16:28:46 +00:00
|
|
|
become_prompt = "%s password: " % become_prompt_method
|
2015-07-01 09:21:46 +00:00
|
|
|
|
2018-12-18 02:10:59 +00:00
|
|
|
if op['become_ask_pass']:
|
2015-07-01 09:21:46 +00:00
|
|
|
becomepass = getpass.getpass(prompt=become_prompt)
|
2018-12-18 02:10:59 +00:00
|
|
|
if op['ask_pass'] and becomepass == '':
|
2015-07-01 09:21:46 +00:00
|
|
|
becomepass = sshpass
|
|
|
|
if becomepass:
|
2018-07-11 14:24:45 +00:00
|
|
|
becomepass = to_bytes(becomepass)
|
2015-07-01 09:21:46 +00:00
|
|
|
except EOFError:
|
|
|
|
pass
|
2015-04-27 11:31:41 +00:00
|
|
|
|
|
|
|
return (sshpass, becomepass)
|
|
|
|
|
2019-04-23 18:54:39 +00:00
|
|
|
def validate_conflicts(self, op, runas_opts=False, fork_opts=False):
|
2018-12-18 02:10:59 +00:00
|
|
|
''' check for conflicting options '''
|
2015-04-27 11:31:41 +00:00
|
|
|
|
2015-08-02 08:40:42 +00:00
|
|
|
if fork_opts:
|
|
|
|
if op.forks < 1:
|
|
|
|
self.parser.error("The number of processes (--forks) must be >= 1")
|
|
|
|
|
2018-12-18 02:10:59 +00:00
|
|
|
return op
|
|
|
|
|
|
|
|
@abstractmethod
|
2018-12-19 08:28:33 +00:00
|
|
|
def init_parser(self, usage="", desc=None, epilog=None):
|
2018-12-18 02:10:59 +00:00
|
|
|
"""
|
|
|
|
Create an options parser for most ansible scripts
|
2015-05-01 01:22:23 +00:00
|
|
|
|
2018-12-18 02:10:59 +00:00
|
|
|
Subclasses need to implement this method. They will usually call the base class's
|
|
|
|
init_parser to create a basic version and then add their own options on top of that.
|
2015-06-09 21:24:06 +00:00
|
|
|
|
2018-12-18 02:10:59 +00:00
|
|
|
An implementation will look something like this::
|
2017-12-07 04:50:51 +00:00
|
|
|
|
2018-12-18 02:10:59 +00:00
|
|
|
def init_parser(self):
|
2018-12-20 04:45:47 +00:00
|
|
|
super(MyCLI, self).init_parser(usage="My Ansible CLI", inventory_opts=True)
|
2019-04-23 18:54:39 +00:00
|
|
|
ansible.arguments.option_helpers.add_runas_options(self.parser)
|
2018-12-18 02:10:59 +00:00
|
|
|
self.parser.add_option('--my-option', dest='my_option', action='store')
|
|
|
|
"""
|
2019-04-29 20:38:31 +00:00
|
|
|
self.parser = opt_help.create_base_parser(os.path.basename(self.args[0]), usage=usage, desc=desc, epilog=epilog, )
|
2015-04-27 11:31:41 +00:00
|
|
|
|
2016-09-29 21:14:02 +00:00
|
|
|
@abstractmethod
|
2019-04-23 18:54:39 +00:00
|
|
|
def post_process_args(self, options):
|
2018-12-18 02:10:59 +00:00
|
|
|
"""Process the command line args
|
|
|
|
|
|
|
|
Subclasses need to implement this method. This method validates and transforms the command
|
|
|
|
line arguments. It can be used to check whether conflicting values were given, whether filenames
|
|
|
|
exist, etc.
|
|
|
|
|
|
|
|
An implementation will look something like this::
|
|
|
|
|
2019-04-23 18:54:39 +00:00
|
|
|
def post_process_args(self, options):
|
|
|
|
options = super(MyCLI, self).post_process_args(options)
|
2018-12-18 02:10:59 +00:00
|
|
|
if options.addition and options.subtraction:
|
|
|
|
raise AnsibleOptionsError('Only one of --addition and --subtraction can be specified')
|
|
|
|
if isinstance(options.listofhosts, string_types):
|
|
|
|
options.listofhosts = string_types.split(',')
|
2019-04-23 18:54:39 +00:00
|
|
|
return options
|
2016-09-29 21:14:02 +00:00
|
|
|
"""
|
2017-05-23 21:16:49 +00:00
|
|
|
|
|
|
|
# process tags
|
2018-12-18 02:10:59 +00:00
|
|
|
if hasattr(options, 'tags') and not options.tags:
|
2016-09-29 21:14:02 +00:00
|
|
|
# optparse defaults does not do what's expected
|
2018-12-18 02:10:59 +00:00
|
|
|
options.tags = ['all']
|
|
|
|
if hasattr(options, 'tags') and options.tags:
|
2016-09-29 21:14:02 +00:00
|
|
|
tags = set()
|
2018-12-18 02:10:59 +00:00
|
|
|
for tag_set in options.tags:
|
2016-09-29 21:14:02 +00:00
|
|
|
for tag in tag_set.split(u','):
|
|
|
|
tags.add(tag.strip())
|
2018-12-18 02:10:59 +00:00
|
|
|
options.tags = list(tags)
|
2016-09-29 21:14:02 +00:00
|
|
|
|
2017-05-23 21:16:49 +00:00
|
|
|
# process skip_tags
|
2018-12-18 02:10:59 +00:00
|
|
|
if hasattr(options, 'skip_tags') and options.skip_tags:
|
2016-09-29 21:14:02 +00:00
|
|
|
skip_tags = set()
|
2018-12-18 02:10:59 +00:00
|
|
|
for tag_set in options.skip_tags:
|
2016-09-29 21:14:02 +00:00
|
|
|
for tag in tag_set.split(u','):
|
|
|
|
skip_tags.add(tag.strip())
|
2018-12-18 02:10:59 +00:00
|
|
|
options.skip_tags = list(skip_tags)
|
2016-09-29 21:14:02 +00:00
|
|
|
|
2017-10-25 23:55:48 +00:00
|
|
|
# process inventory options except for CLIs that require their own processing
|
2018-12-18 02:10:59 +00:00
|
|
|
if hasattr(options, 'inventory') and not self.SKIP_INVENTORY_DEFAULTS:
|
2017-05-23 21:16:49 +00:00
|
|
|
|
2018-12-18 02:10:59 +00:00
|
|
|
if options.inventory:
|
2017-05-23 21:16:49 +00:00
|
|
|
|
|
|
|
# should always be list
|
2018-12-18 02:10:59 +00:00
|
|
|
if isinstance(options.inventory, string_types):
|
|
|
|
options.inventory = [options.inventory]
|
2017-05-23 21:16:49 +00:00
|
|
|
|
|
|
|
# Ensure full paths when needed
|
2018-12-18 02:10:59 +00:00
|
|
|
options.inventory = [unfrackpath(opt, follow=False) if ',' not in opt else opt for opt in options.inventory]
|
2017-05-23 21:16:49 +00:00
|
|
|
else:
|
2018-12-18 02:10:59 +00:00
|
|
|
options.inventory = C.DEFAULT_HOST_LIST
|
|
|
|
|
2019-04-23 18:54:39 +00:00
|
|
|
return options
|
2018-12-18 02:10:59 +00:00
|
|
|
|
|
|
|
def parse(self):
|
|
|
|
"""Parse the command line args
|
|
|
|
|
|
|
|
This method parses the command line arguments. It uses the parser
|
|
|
|
stored in the self.parser attribute and saves the args and options in
|
|
|
|
context.CLIARGS.
|
|
|
|
|
|
|
|
Subclasses need to implement two helper methods, init_parser() and post_process_args() which
|
|
|
|
are called from this function before and after parsing the arguments.
|
|
|
|
"""
|
|
|
|
self.init_parser()
|
2019-04-23 18:54:39 +00:00
|
|
|
|
|
|
|
if HAS_ARGCOMPLETE:
|
|
|
|
argcomplete.autocomplete(self.parser)
|
|
|
|
|
|
|
|
options = self.parser.parse_args(self.args[1:])
|
|
|
|
options = self.post_process_args(options)
|
2018-12-18 02:10:59 +00:00
|
|
|
context._init_global_context(options)
|
2017-05-23 21:16:49 +00:00
|
|
|
|
2015-04-27 11:31:41 +00:00
|
|
|
@staticmethod
|
|
|
|
def version_info(gitinfo=False):
|
2015-04-30 22:43:53 +00:00
|
|
|
''' return full ansible version info '''
|
2015-04-27 11:31:41 +00:00
|
|
|
if gitinfo:
|
|
|
|
# expensive call, user with care
|
2018-12-19 08:28:33 +00:00
|
|
|
ansible_version_string = opt_help.version()
|
2015-04-27 11:31:41 +00:00
|
|
|
else:
|
|
|
|
ansible_version_string = __version__
|
|
|
|
ansible_version = ansible_version_string.split()[0]
|
|
|
|
ansible_versions = ansible_version.split('.')
|
|
|
|
for counter in range(len(ansible_versions)):
|
|
|
|
if ansible_versions[counter] == "":
|
|
|
|
ansible_versions[counter] = 0
|
|
|
|
try:
|
|
|
|
ansible_versions[counter] = int(ansible_versions[counter])
|
2018-06-29 23:45:38 +00:00
|
|
|
except Exception:
|
2015-04-27 11:31:41 +00:00
|
|
|
pass
|
|
|
|
if len(ansible_versions) < 3:
|
|
|
|
for counter in range(len(ansible_versions), 3):
|
|
|
|
ansible_versions.append(0)
|
2017-06-02 11:14:11 +00:00
|
|
|
return {'string': ansible_version_string.strip(),
|
|
|
|
'full': ansible_version,
|
|
|
|
'major': ansible_versions[0],
|
|
|
|
'minor': ansible_versions[1],
|
|
|
|
'revision': ansible_versions[2]}
|
2014-11-14 22:14:08 +00:00
|
|
|
|
2018-12-20 04:45:47 +00:00
|
|
|
@staticmethod
|
|
|
|
def pager(text):
|
2015-05-01 02:54:38 +00:00
|
|
|
''' find reasonable way to display text '''
|
|
|
|
# this is a much simpler form of what is in pydoc.py
|
|
|
|
if not sys.stdout.isatty():
|
2017-06-15 14:45:01 +00:00
|
|
|
display.display(text, screen_only=True)
|
2015-05-01 02:54:38 +00:00
|
|
|
elif 'PAGER' in os.environ:
|
|
|
|
if sys.platform == 'win32':
|
2017-06-15 14:45:01 +00:00
|
|
|
display.display(text, screen_only=True)
|
2015-05-01 02:54:38 +00:00
|
|
|
else:
|
2018-12-20 04:45:47 +00:00
|
|
|
CLI.pager_pipe(text, os.environ['PAGER'])
|
2015-05-01 02:54:38 +00:00
|
|
|
else:
|
2016-09-09 13:30:37 +00:00
|
|
|
p = subprocess.Popen('less --version', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
|
|
p.communicate()
|
|
|
|
if p.returncode == 0:
|
2018-12-20 04:45:47 +00:00
|
|
|
CLI.pager_pipe(text, 'less')
|
2016-09-09 13:30:37 +00:00
|
|
|
else:
|
2017-06-15 14:45:01 +00:00
|
|
|
display.display(text, screen_only=True)
|
2015-05-01 02:54:38 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def pager_pipe(text, cmd):
|
|
|
|
''' pipe text through a pager '''
|
|
|
|
if 'LESS' not in os.environ:
|
2015-05-12 16:26:20 +00:00
|
|
|
os.environ['LESS'] = CLI.LESS_OPTS
|
2015-05-01 02:54:38 +00:00
|
|
|
try:
|
|
|
|
cmd = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE, stdout=sys.stdout)
|
2016-01-28 18:50:20 +00:00
|
|
|
cmd.communicate(input=to_bytes(text))
|
2015-05-01 02:54:38 +00:00
|
|
|
except IOError:
|
|
|
|
pass
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
pass
|
|
|
|
|
|
|
|
@classmethod
|
2015-10-09 04:46:31 +00:00
|
|
|
def tty_ify(cls, text):
|
2015-05-01 02:54:38 +00:00
|
|
|
|
2015-10-09 04:46:31 +00:00
|
|
|
t = cls._ITALIC.sub("`" + r"\1" + "'", text) # I(word) => `word'
|
|
|
|
t = cls._BOLD.sub("*" + r"\1" + "*", t) # B(word) => *word*
|
|
|
|
t = cls._MODULE.sub("[" + r"\1" + "]", t) # M(word) => [word]
|
|
|
|
t = cls._URL.sub(r"\1", t) # U(word) => word
|
|
|
|
t = cls._CONST.sub("`" + r"\1" + "'", t) # C(word) => `word'
|
2015-05-01 02:54:38 +00:00
|
|
|
|
|
|
|
return t
|
2015-07-11 18:24:45 +00:00
|
|
|
|
2017-05-23 21:16:49 +00:00
|
|
|
@staticmethod
|
2018-12-18 02:10:59 +00:00
|
|
|
def _play_prereqs():
|
|
|
|
options = context.CLIARGS
|
2017-05-23 21:16:49 +00:00
|
|
|
|
|
|
|
# all needs loader
|
|
|
|
loader = DataLoader()
|
|
|
|
|
2018-12-18 02:10:59 +00:00
|
|
|
basedir = options.get('basedir', False)
|
2017-10-31 19:41:30 +00:00
|
|
|
if basedir:
|
|
|
|
loader.set_basedir(basedir)
|
Become plugins (#50991)
* [WIP] become plugins
Move from hardcoded method to plugins for ease of use, expansion and overrides
- load into connection as it is going to be the main consumer
- play_context will also use to keep backwards compat API
- ensure shell is used to construct commands when needed
- migrate settings remove from base config in favor of plugin specific configs
- cleanup ansible-doc
- add become plugin docs
- remove deprecated sudo/su code and keywords
- adjust become options for cli
- set plugin options from context
- ensure config defs are avaialbe before instance
- refactored getting the shell plugin, fixed tests
- changed into regex as they were string matching, which does not work with random string generation
- explicitly set flags for play context tests
- moved plugin loading up front
- now loads for basedir also
- allow pyc/o for non m modules
- fixes to tests and some plugins
- migrate to play objects fro play_context
- simiplify gathering
- added utf8 headers
- moved option setting
- add fail msg to dzdo
- use tuple for multiple options on fail/missing
- fix relative plugin paths
- shift from play context to play
- all tasks already inherit this from play directly
- remove obsolete 'set play'
- correct environment handling
- add wrap_exe option to pfexec
- fix runas to noop
- fixed setting play context
- added password configs
- removed required false
- remove from doc building till they are ready
future development:
- deal with 'enable' and 'runas' which are not 'command wrappers' but 'state flags' and currently hardcoded in diff subsystems
* cleanup
remove callers to removed func
removed --sudo cli doc refs
remove runas become_exe
ensure keyerorr on plugin
also fix backwards compat, missing method is attributeerror, not ansible error
get remote_user consistently
ignore missing system_tmpdirs on plugin load
correct config precedence
add deprecation
fix networking imports
backwards compat for plugins using BECOME_METHODS
* Port become_plugins to context.CLIARGS
This is a work in progress:
* Stop passing options around everywhere as we can use context.CLIARGS
instead
* Refactor make_become_commands as asked for by alikins
* Typo in comment fix
* Stop loading values from the cli in more than one place
Both play and play_context were saving default values from the cli
arguments directly. This changes things so that the default values are
loaded into the play and then play_context takes them from there.
* Rename BECOME_PLUGIN_PATH to DEFAULT_BECOME_PLUGIN_PATH
As alikins said, all other plugin paths are named
DEFAULT_plugintype_PLUGIN_PATH. If we're going to rename these, that
should be done all at one time rather than piecemeal.
* One to throw away
This is a set of hacks to get setting FieldAttribute defaults to command
line args to work. It's not fully done yet.
After talking it over with sivel and jimi-c this should be done by
fixing FieldAttributeBase and _get_parent_attribute() calls to do the
right thing when there is a non-None default.
What we want to be able to do ideally is something like this:
class Base(FieldAttributeBase):
_check_mode = FieldAttribute([..] default=lambda: context.CLIARGS['check'])
class Play(Base):
# lambda so that we have a chance to parse the command line args
# before we get here. In the future we might be able to restructure
# this so that the cli parsing code runs before these classes are
# defined.
class Task(Base):
pass
And still have a playbook like this function:
---
- hosts:
tasks:
- command: whoami
check_mode: True
(The check_mode test that is added as a separate commit in this PR will
let you test variations on this case).
There's a few separate reasons that the code doesn't let us do this or
a non-ugly workaround for this as written right now. The fix that
jimi-c, sivel, and I talked about may let us do this or it may still
require a workaround (but less ugly) (having one class that has the
FieldAttributes with default values and one class that inherits from
that but just overrides the FieldAttributes which now have defaults)
* Revert "One to throw away"
This reverts commit 23aa883cbed11429ef1be2a2d0ed18f83a3b8064.
* Set FieldAttr defaults directly from CLIARGS
* Remove dead code
* Move timeout directly to PlayContext, it's never needed on Play
* just for backwards compat, add a static version of BECOME_METHODS to constants
* Make the become attr on the connection public, since it's used outside of the connection
* Logic fix
* Nuke connection testing if it supports specific become methods
* Remove unused vars
* Address rebase issues
* Fix path encoding issue
* Remove unused import
* Various cleanups
* Restore network_cli check in _low_level_execute_command
* type improvements for cliargs_deferred_get and swap shallowcopy to default to False
* minor cleanups
* Allow the su plugin to work, since it doesn't define a prompt the same way
* Fix up ksu become plugin
* Only set prompt if build_become_command was called
* Add helper to assist connection plugins in knowing they need to wait for a prompt
* Fix tests and code expectations
* Doc updates
* Various additional minor cleanups
* Make doas functional
* Don't change connection signature, load become plugin from TaskExecutor
* Remove unused imports
* Add comment about setting the become plugin on the playcontext
* Fix up tests for recent changes
* Support 'Password:' natively for the doas plugin
* Make default prompts raw
* wording cleanups. ci_complete
* Remove unrelated changes
* Address spelling mistake
* Restore removed test, and udpate to use new functionality
* Add changelog fragment
* Don't hard fail in set_attributes_from_cli on missing CLI keys
* Remove unrelated change to loader
* Remove internal deprecated FieldAttributes now
* Emit deprecation warnings now
2019-02-11 17:27:44 +00:00
|
|
|
add_all_plugin_dirs(basedir)
|
2019-05-22 15:06:54 +00:00
|
|
|
set_collection_playbook_paths(basedir)
|
2017-10-31 19:41:30 +00:00
|
|
|
|
2018-12-18 02:10:59 +00:00
|
|
|
vault_ids = list(options['vault_ids'])
|
2017-08-28 14:13:14 +00:00
|
|
|
default_vault_ids = C.DEFAULT_VAULT_IDENTITY_LIST
|
|
|
|
vault_ids = default_vault_ids + vault_ids
|
|
|
|
|
Support multiple vault passwords (#22756)
Fixes #13243
** Add --vault-id to name/identify multiple vault passwords
Use --vault-id to indicate id and path/type
--vault-id=prompt # prompt for default vault id password
--vault-id=myorg@prompt # prompt for a vault_id named 'myorg'
--vault-id=a_password_file # load ./a_password_file for default id
--vault-id=myorg@a_password_file # load file for 'myorg' vault id
vault_id's are created implicitly for existing --vault-password-file
and --ask-vault-pass options.
Vault ids are just for UX purposes and bookkeeping. Only the vault
payload and the password bytestring is needed to decrypt a
vault blob.
Replace passing password around everywhere with
a VaultSecrets object.
If we specify a vault_id, mention that in password prompts
Specifying multiple -vault-password-files will
now try each until one works
** Rev vault format in a backwards compatible way
The 1.2 vault format adds the vault_id to the header line
of the vault text. This is backwards compatible with older
versions of ansible. Old versions will just ignore it and
treat it as the default (and only) vault id.
Note: only 2.4+ supports multiple vault passwords, so while
earlier ansible versions can read the vault-1.2 format, it
does not make them magically support multiple vault passwords.
use 1.1 format for 'default' vault_id
Vaulted items that need to include a vault_id will be
written in 1.2 format.
If we set a new DEFAULT_VAULT_IDENTITY, then the default will
use version 1.2
vault will only use a vault_id if one is specified. So if none
is specified and C.DEFAULT_VAULT_IDENTITY is 'default'
we use the old format.
** Changes/refactors needed to implement multiple vault passwords
raise exceptions on decrypt fail, check vault id early
split out parsing the vault plaintext envelope (with the
sha/original plaintext) to _split_plaintext_envelope()
some cli fixups for specifying multiple paths in
the unfrack_paths optparse callback
fix py3 dict.keys() 'dict_keys object is not indexable' error
pluralize cli.options.vault_password_file -> vault_password_files
pluralize cli.options.new_vault_password_file -> new_vault_password_files
pluralize cli.options.vault_id -> cli.options.vault_ids
** Add a config option (vault_id_match) to force vault id matching.
With 'vault_id_match=True' and an ansible
vault that provides a vault_id, then decryption will require
that a matching vault_id is required. (via
--vault-id=my_vault_id@password_file, for ex).
In other words, if the config option is true, then only
the vault secrets with matching vault ids are candidates for
decrypting a vault. If option is false (the default), then
all of the provided vault secrets will be selected.
If a user doesn't want all vault secrets to be tried to
decrypt any vault content, they can enable this option.
Note: The vault id used for the match is not encrypted or
cryptographically signed. It is just a label/id/nickname used
for referencing a specific vault secret.
2017-07-28 19:20:58 +00:00
|
|
|
vault_secrets = CLI.setup_vault_secrets(loader,
|
2017-08-28 14:13:14 +00:00
|
|
|
vault_ids=vault_ids,
|
2018-12-18 02:10:59 +00:00
|
|
|
vault_password_files=list(options['vault_password_files']),
|
|
|
|
ask_vault_pass=options['ask_vault_pass'],
|
2017-09-19 21:39:51 +00:00
|
|
|
auto_prompt=False)
|
Support multiple vault passwords (#22756)
Fixes #13243
** Add --vault-id to name/identify multiple vault passwords
Use --vault-id to indicate id and path/type
--vault-id=prompt # prompt for default vault id password
--vault-id=myorg@prompt # prompt for a vault_id named 'myorg'
--vault-id=a_password_file # load ./a_password_file for default id
--vault-id=myorg@a_password_file # load file for 'myorg' vault id
vault_id's are created implicitly for existing --vault-password-file
and --ask-vault-pass options.
Vault ids are just for UX purposes and bookkeeping. Only the vault
payload and the password bytestring is needed to decrypt a
vault blob.
Replace passing password around everywhere with
a VaultSecrets object.
If we specify a vault_id, mention that in password prompts
Specifying multiple -vault-password-files will
now try each until one works
** Rev vault format in a backwards compatible way
The 1.2 vault format adds the vault_id to the header line
of the vault text. This is backwards compatible with older
versions of ansible. Old versions will just ignore it and
treat it as the default (and only) vault id.
Note: only 2.4+ supports multiple vault passwords, so while
earlier ansible versions can read the vault-1.2 format, it
does not make them magically support multiple vault passwords.
use 1.1 format for 'default' vault_id
Vaulted items that need to include a vault_id will be
written in 1.2 format.
If we set a new DEFAULT_VAULT_IDENTITY, then the default will
use version 1.2
vault will only use a vault_id if one is specified. So if none
is specified and C.DEFAULT_VAULT_IDENTITY is 'default'
we use the old format.
** Changes/refactors needed to implement multiple vault passwords
raise exceptions on decrypt fail, check vault id early
split out parsing the vault plaintext envelope (with the
sha/original plaintext) to _split_plaintext_envelope()
some cli fixups for specifying multiple paths in
the unfrack_paths optparse callback
fix py3 dict.keys() 'dict_keys object is not indexable' error
pluralize cli.options.vault_password_file -> vault_password_files
pluralize cli.options.new_vault_password_file -> new_vault_password_files
pluralize cli.options.vault_id -> cli.options.vault_ids
** Add a config option (vault_id_match) to force vault id matching.
With 'vault_id_match=True' and an ansible
vault that provides a vault_id, then decryption will require
that a matching vault_id is required. (via
--vault-id=my_vault_id@password_file, for ex).
In other words, if the config option is true, then only
the vault secrets with matching vault ids are candidates for
decrypting a vault. If option is false (the default), then
all of the provided vault secrets will be selected.
If a user doesn't want all vault secrets to be tried to
decrypt any vault content, they can enable this option.
Note: The vault id used for the match is not encrypted or
cryptographically signed. It is just a label/id/nickname used
for referencing a specific vault secret.
2017-07-28 19:20:58 +00:00
|
|
|
loader.set_vault_secrets(vault_secrets)
|
2017-05-23 21:16:49 +00:00
|
|
|
|
|
|
|
# create the inventory, and filter it based on the subset specified (if any)
|
2018-12-18 02:10:59 +00:00
|
|
|
inventory = InventoryManager(loader=loader, sources=options['inventory'])
|
2017-05-23 21:16:49 +00:00
|
|
|
|
|
|
|
# create the variable manager, which will be shared throughout
|
|
|
|
# the code, ensuring a consistent view of global variables
|
2019-01-30 21:25:36 +00:00
|
|
|
variable_manager = VariableManager(loader=loader, inventory=inventory, version_info=CLI.version_info(gitinfo=False))
|
2017-05-23 21:16:49 +00:00
|
|
|
|
|
|
|
return loader, inventory, variable_manager
|
2017-12-15 20:43:51 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_host_list(inventory, subset, pattern='all'):
|
|
|
|
|
|
|
|
no_hosts = False
|
|
|
|
if len(inventory.list_hosts()) == 0:
|
|
|
|
# Empty inventory
|
2018-11-07 16:09:32 +00:00
|
|
|
if C.LOCALHOST_WARNING and pattern not in C.LOCALHOST:
|
2018-03-24 12:59:19 +00:00
|
|
|
display.warning("provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'")
|
2017-12-15 20:43:51 +00:00
|
|
|
no_hosts = True
|
|
|
|
|
2019-02-13 17:52:30 +00:00
|
|
|
inventory.subset(subset)
|
|
|
|
|
2017-12-15 20:43:51 +00:00
|
|
|
hosts = inventory.list_hosts(pattern)
|
2019-02-13 17:52:30 +00:00
|
|
|
if not hosts and no_hosts is False:
|
2017-12-15 20:43:51 +00:00
|
|
|
raise AnsibleError("Specified hosts and/or --limit does not match any hosts")
|
|
|
|
|
|
|
|
return hosts
|