2015-04-20 03:31:44 +00:00
|
|
|
# (c) 2014, James Tanner <tanner.jc@gmail.com>
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
# ansible-vault is a script that encrypts/decrypts YAML files. See
|
|
|
|
# http://docs.ansible.com/playbooks_vault.html for more details.
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import traceback
|
|
|
|
|
2015-05-01 01:22:23 +00:00
|
|
|
from ansible import constants as C
|
2015-04-30 03:55:44 +00:00
|
|
|
from ansible.errors import AnsibleError, AnsibleOptionsError
|
2015-04-20 17:42:02 +00:00
|
|
|
from ansible.parsing.vault import VaultEditor
|
2015-05-01 01:22:23 +00:00
|
|
|
from ansible.cli import CLI
|
2015-04-27 11:31:41 +00:00
|
|
|
from ansible.utils.display import Display
|
2015-04-20 03:31:44 +00:00
|
|
|
|
2015-04-30 22:43:53 +00:00
|
|
|
class VaultCLI(CLI):
|
2015-04-27 11:31:41 +00:00
|
|
|
""" Vault command line class """
|
2015-04-20 03:31:44 +00:00
|
|
|
|
|
|
|
VALID_ACTIONS = ("create", "decrypt", "edit", "encrypt", "rekey", "view")
|
|
|
|
|
2015-04-27 11:31:41 +00:00
|
|
|
def __init__(self, args, display=None):
|
2015-04-20 03:31:44 +00:00
|
|
|
|
|
|
|
self.vault_pass = None
|
2015-05-01 01:22:23 +00:00
|
|
|
super(VaultCLI, self).__init__(args, display)
|
2015-04-20 03:31:44 +00:00
|
|
|
|
|
|
|
def parse(self):
|
|
|
|
|
2015-04-27 11:31:41 +00:00
|
|
|
self.parser = CLI.base_parser(
|
2015-05-01 01:22:23 +00:00
|
|
|
vault_opts=True,
|
|
|
|
usage = "usage: %%prog [%s] [--help] [options] vaultfile.yml" % "|".join(self.VALID_ACTIONS),
|
|
|
|
epilog = "\nSee '%s <command> --help' for more information on a specific command.\n\n" % os.path.basename(sys.argv[0])
|
2015-04-20 03:31:44 +00:00
|
|
|
)
|
|
|
|
|
2015-04-27 11:31:41 +00:00
|
|
|
self.set_action()
|
|
|
|
|
|
|
|
# options specific to self.actions
|
|
|
|
if self.action == "create":
|
|
|
|
self.parser.set_usage("usage: %prog create [options] file_name")
|
|
|
|
elif self.action == "decrypt":
|
|
|
|
self.parser.set_usage("usage: %prog decrypt [options] file_name")
|
|
|
|
elif self.action == "edit":
|
|
|
|
self.parser.set_usage("usage: %prog edit [options] file_name")
|
|
|
|
elif self.action == "view":
|
|
|
|
self.parser.set_usage("usage: %prog view [options] file_name")
|
|
|
|
elif self.action == "encrypt":
|
|
|
|
self.parser.set_usage("usage: %prog encrypt [options] file_name")
|
2015-07-14 14:10:03 +00:00
|
|
|
elif self.action == "rekey":
|
2015-04-27 11:31:41 +00:00
|
|
|
self.parser.set_usage("usage: %prog rekey [options] file_name")
|
2015-04-20 03:31:44 +00:00
|
|
|
|
2015-04-27 11:31:41 +00:00
|
|
|
self.options, self.args = self.parser.parse_args()
|
2015-05-01 01:22:23 +00:00
|
|
|
self.display.verbosity = self.options.verbosity
|
2015-04-20 03:31:44 +00:00
|
|
|
|
2015-08-22 14:17:15 +00:00
|
|
|
if len(self.args) == 0:
|
|
|
|
raise AnsibleOptionsError("Vault requires at least one filename as a parameter")
|
2015-04-20 03:31:44 +00:00
|
|
|
|
2015-04-27 11:31:41 +00:00
|
|
|
def run(self):
|
2015-04-20 03:31:44 +00:00
|
|
|
|
2015-07-04 14:23:30 +00:00
|
|
|
super(VaultCLI, self).run()
|
|
|
|
|
2015-04-27 11:31:41 +00:00
|
|
|
if self.options.vault_password_file:
|
|
|
|
# read vault_pass from a file
|
2015-07-11 19:05:32 +00:00
|
|
|
self.vault_pass = CLI.read_vault_password_file(self.options.vault_password_file)
|
2015-07-20 02:24:20 +00:00
|
|
|
else:
|
2015-04-27 11:31:41 +00:00
|
|
|
self.vault_pass, _= self.ask_vault_passwords(ask_vault_pass=True, ask_new_vault_pass=False, confirm_new=False)
|
2015-04-20 03:31:44 +00:00
|
|
|
|
2015-07-28 09:48:57 +00:00
|
|
|
if self.options.new_vault_password_file:
|
|
|
|
# for rekey only
|
|
|
|
self.new_vault_pass = CLI.read_vault_password_file(self.options.new_vault_password_file)
|
|
|
|
|
2015-06-27 04:02:08 +00:00
|
|
|
if not self.vault_pass:
|
|
|
|
raise AnsibleOptionsError("A password is required to use Ansible's Vault")
|
|
|
|
|
2015-08-26 13:47:37 +00:00
|
|
|
self.editor = VaultEditor(self.vault_pass)
|
|
|
|
|
2015-04-27 11:31:41 +00:00
|
|
|
self.execute()
|
2015-04-20 03:31:44 +00:00
|
|
|
|
2015-04-27 11:31:41 +00:00
|
|
|
def execute_create(self):
|
2015-04-20 03:31:44 +00:00
|
|
|
|
2015-08-22 14:17:15 +00:00
|
|
|
if len(self.args) > 1:
|
|
|
|
raise AnsibleOptionsError("ansible-vault create can take only one filename argument")
|
|
|
|
|
2015-08-26 13:47:37 +00:00
|
|
|
self.editor.create_file(self.args[0])
|
2015-04-20 03:31:44 +00:00
|
|
|
|
2015-04-27 11:31:41 +00:00
|
|
|
def execute_decrypt(self):
|
2015-04-20 03:31:44 +00:00
|
|
|
|
2015-04-27 11:31:41 +00:00
|
|
|
for f in self.args:
|
2015-08-26 13:47:37 +00:00
|
|
|
self.editor.decrypt_file(f)
|
2015-04-20 03:31:44 +00:00
|
|
|
|
2015-08-26 13:47:37 +00:00
|
|
|
self.display.display("Decryption successful", stderr=True)
|
2015-04-20 03:31:44 +00:00
|
|
|
|
2015-04-27 11:31:41 +00:00
|
|
|
def execute_edit(self):
|
|
|
|
for f in self.args:
|
2015-08-26 13:47:37 +00:00
|
|
|
self.editor.edit_file(f)
|
2015-04-20 03:31:44 +00:00
|
|
|
|
2015-04-27 11:31:41 +00:00
|
|
|
def execute_view(self):
|
2015-04-20 03:31:44 +00:00
|
|
|
|
2015-04-27 11:31:41 +00:00
|
|
|
for f in self.args:
|
2015-08-26 13:47:37 +00:00
|
|
|
self.editor.view_file(f)
|
2015-04-20 03:31:44 +00:00
|
|
|
|
2015-04-27 11:31:41 +00:00
|
|
|
def execute_encrypt(self):
|
2015-04-20 03:31:44 +00:00
|
|
|
|
2015-04-27 11:31:41 +00:00
|
|
|
for f in self.args:
|
2015-08-26 13:47:37 +00:00
|
|
|
self.editor.encrypt_file(f)
|
2015-04-20 03:31:44 +00:00
|
|
|
|
2015-08-26 13:47:37 +00:00
|
|
|
self.display.display("Encryption successful", stderr=True)
|
2015-04-20 03:31:44 +00:00
|
|
|
|
2015-04-27 11:31:41 +00:00
|
|
|
def execute_rekey(self):
|
2015-06-21 20:51:07 +00:00
|
|
|
for f in self.args:
|
|
|
|
if not (os.path.isfile(f)):
|
|
|
|
raise AnsibleError(f + " does not exist")
|
2015-07-28 09:48:57 +00:00
|
|
|
|
|
|
|
if self.new_vault_pass:
|
|
|
|
new_password = self.new_vault_pass
|
|
|
|
else:
|
|
|
|
__, new_password = self.ask_vault_passwords(ask_vault_pass=False, ask_new_vault_pass=True, confirm_new=True)
|
2015-04-20 03:31:44 +00:00
|
|
|
|
2015-04-27 11:31:41 +00:00
|
|
|
for f in self.args:
|
2015-08-26 13:47:37 +00:00
|
|
|
self.editor.rekey_file(new_password, f)
|
2015-04-20 03:31:44 +00:00
|
|
|
|
2015-08-26 13:47:37 +00:00
|
|
|
self.display.display("Rekey successful", stderr=True)
|