2014-02-11 17:03:11 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
# (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/>.
|
|
|
|
#
|
2014-09-16 20:37:36 +00:00
|
|
|
# ansible-vault is a script that encrypts/decrypts YAML files. See
|
|
|
|
# http://docs.ansible.com/playbooks_vault.html for more details.
|
2014-02-11 17:03:11 +00:00
|
|
|
|
2014-04-29 20:24:00 +00:00
|
|
|
#__requires__ = ['ansible']
|
|
|
|
#import pkg_resources
|
2014-03-14 18:37:24 +00:00
|
|
|
|
2014-02-24 18:09:36 +00:00
|
|
|
import os
|
2014-02-11 17:03:11 +00:00
|
|
|
import sys
|
|
|
|
import traceback
|
|
|
|
|
2014-08-11 20:58:10 +00:00
|
|
|
import ansible.constants as C
|
|
|
|
|
2014-02-11 17:03:11 +00:00
|
|
|
from ansible import utils
|
|
|
|
from ansible import errors
|
2014-02-24 18:09:36 +00:00
|
|
|
from ansible.utils.vault import VaultEditor
|
2014-02-11 17:03:11 +00:00
|
|
|
|
|
|
|
from optparse import OptionParser
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------------------------
|
|
|
|
# Utility functions for parsing actions/options
|
|
|
|
#-------------------------------------------------------------------------------------
|
|
|
|
|
2014-08-11 19:59:05 +00:00
|
|
|
VALID_ACTIONS = ("create", "decrypt", "edit", "encrypt", "rekey", "view")
|
2014-02-11 17:03:11 +00:00
|
|
|
|
|
|
|
def build_option_parser(action):
|
|
|
|
"""
|
|
|
|
Builds an option parser object based on the action
|
|
|
|
the user wants to execute.
|
|
|
|
"""
|
|
|
|
|
|
|
|
usage = "usage: %%prog [%s] [--help] [options] file_name" % "|".join(VALID_ACTIONS)
|
|
|
|
epilog = "\nSee '%s <command> --help' for more information on a specific command.\n\n" % os.path.basename(sys.argv[0])
|
|
|
|
OptionParser.format_epilog = lambda self, formatter: self.epilog
|
|
|
|
parser = OptionParser(usage=usage, epilog=epilog)
|
|
|
|
|
|
|
|
if not action:
|
|
|
|
parser.print_help()
|
|
|
|
sys.exit()
|
|
|
|
|
|
|
|
# options for all actions
|
2014-03-10 21:15:44 +00:00
|
|
|
#parser.add_option('-c', '--cipher', dest='cipher', default="AES256", help="cipher to use")
|
2014-02-26 15:01:59 +00:00
|
|
|
parser.add_option('--debug', dest='debug', action="store_true", help="debug")
|
|
|
|
parser.add_option('--vault-password-file', dest='password_file',
|
2014-08-11 20:58:10 +00:00
|
|
|
help="vault password file", default=C.DEFAULT_VAULT_PASSWORD_FILE)
|
2014-02-11 17:03:11 +00:00
|
|
|
|
|
|
|
# options specific to actions
|
|
|
|
if action == "create":
|
|
|
|
parser.set_usage("usage: %prog create [options] file_name")
|
|
|
|
elif action == "decrypt":
|
|
|
|
parser.set_usage("usage: %prog decrypt [options] file_name")
|
|
|
|
elif action == "edit":
|
|
|
|
parser.set_usage("usage: %prog edit [options] file_name")
|
2014-04-17 19:13:48 +00:00
|
|
|
elif action == "view":
|
|
|
|
parser.set_usage("usage: %prog view [options] file_name")
|
2014-02-11 17:03:11 +00:00
|
|
|
elif action == "encrypt":
|
|
|
|
parser.set_usage("usage: %prog encrypt [options] file_name")
|
|
|
|
elif action == "rekey":
|
|
|
|
parser.set_usage("usage: %prog rekey [options] file_name")
|
|
|
|
|
|
|
|
# done, return the parser
|
|
|
|
return parser
|
|
|
|
|
|
|
|
def get_action(args):
|
|
|
|
"""
|
|
|
|
Get the action the user wants to execute from the
|
|
|
|
sys argv list.
|
|
|
|
"""
|
|
|
|
for i in range(0,len(args)):
|
|
|
|
arg = args[i]
|
|
|
|
if arg in VALID_ACTIONS:
|
|
|
|
del args[i]
|
|
|
|
return arg
|
|
|
|
return None
|
|
|
|
|
|
|
|
def get_opt(options, k, defval=""):
|
|
|
|
"""
|
|
|
|
Returns an option from an Optparse values instance.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
data = getattr(options, k)
|
|
|
|
except:
|
|
|
|
return defval
|
|
|
|
if k == "roles_path":
|
|
|
|
if os.pathsep in data:
|
|
|
|
data = data.split(os.pathsep)[0]
|
|
|
|
return data
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------------------------
|
|
|
|
# Command functions
|
|
|
|
#-------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
def execute_create(args, options, parser):
|
|
|
|
if len(args) > 1:
|
2014-02-24 18:09:36 +00:00
|
|
|
raise errors.AnsibleError("'create' does not accept more than one filename")
|
2014-02-26 15:01:59 +00:00
|
|
|
|
2014-08-11 20:58:10 +00:00
|
|
|
if not options.password_file:
|
2014-02-26 15:01:59 +00:00
|
|
|
password, new_password = utils.ask_vault_passwords(ask_vault_pass=True, confirm_vault=True)
|
|
|
|
else:
|
2014-08-11 20:58:10 +00:00
|
|
|
password = utils.read_vault_file(options.password_file)
|
2014-02-11 17:03:11 +00:00
|
|
|
|
2014-03-10 21:15:44 +00:00
|
|
|
cipher = 'AES256'
|
2014-02-24 18:09:36 +00:00
|
|
|
if hasattr(options, 'cipher'):
|
|
|
|
cipher = options.cipher
|
|
|
|
|
|
|
|
this_editor = VaultEditor(cipher, password, args[0])
|
|
|
|
this_editor.create_file()
|
2014-02-11 17:03:11 +00:00
|
|
|
|
|
|
|
def execute_decrypt(args, options, parser):
|
|
|
|
|
2014-08-11 20:58:10 +00:00
|
|
|
if not options.password_file:
|
2014-02-26 15:01:59 +00:00
|
|
|
password, new_password = utils.ask_vault_passwords(ask_vault_pass=True)
|
|
|
|
else:
|
2014-08-26 14:02:22 +00:00
|
|
|
password = utils.read_vault_file(options.password_file)
|
2014-02-11 17:03:11 +00:00
|
|
|
|
2014-03-10 21:15:44 +00:00
|
|
|
cipher = 'AES256'
|
2014-02-24 18:09:36 +00:00
|
|
|
if hasattr(options, 'cipher'):
|
|
|
|
cipher = options.cipher
|
|
|
|
|
2014-02-11 17:03:11 +00:00
|
|
|
for f in args:
|
2014-02-24 18:09:36 +00:00
|
|
|
this_editor = VaultEditor(cipher, password, f)
|
|
|
|
this_editor.decrypt_file()
|
2014-02-11 17:03:11 +00:00
|
|
|
|
|
|
|
print "Decryption successful"
|
|
|
|
|
|
|
|
def execute_edit(args, options, parser):
|
|
|
|
|
|
|
|
if len(args) > 1:
|
2014-04-17 19:37:53 +00:00
|
|
|
raise errors.AnsibleError("edit does not accept more than one filename")
|
2014-02-11 17:03:11 +00:00
|
|
|
|
2014-08-11 20:58:10 +00:00
|
|
|
if not options.password_file:
|
2014-02-26 15:01:59 +00:00
|
|
|
password, new_password = utils.ask_vault_passwords(ask_vault_pass=True)
|
|
|
|
else:
|
2014-08-11 20:58:10 +00:00
|
|
|
password = utils.read_vault_file(options.password_file)
|
2014-02-11 17:03:11 +00:00
|
|
|
|
2014-02-24 18:09:36 +00:00
|
|
|
cipher = None
|
|
|
|
|
2014-02-11 17:03:11 +00:00
|
|
|
for f in args:
|
2014-02-24 18:09:36 +00:00
|
|
|
this_editor = VaultEditor(cipher, password, f)
|
|
|
|
this_editor.edit_file()
|
2014-02-11 17:03:11 +00:00
|
|
|
|
2014-04-17 19:13:48 +00:00
|
|
|
def execute_view(args, options, parser):
|
|
|
|
|
|
|
|
if len(args) > 1:
|
|
|
|
raise errors.AnsibleError("view does not accept more than one filename")
|
|
|
|
|
|
|
|
if not options.password_file:
|
|
|
|
password, new_password = utils.ask_vault_passwords(ask_vault_pass=True)
|
|
|
|
else:
|
2014-08-11 20:58:10 +00:00
|
|
|
password = utils.read_vault_file(options.password_file)
|
2014-04-17 19:13:48 +00:00
|
|
|
|
|
|
|
cipher = None
|
|
|
|
|
|
|
|
for f in args:
|
|
|
|
this_editor = VaultEditor(cipher, password, f)
|
|
|
|
this_editor.view_file()
|
|
|
|
|
2014-02-11 17:03:11 +00:00
|
|
|
def execute_encrypt(args, options, parser):
|
|
|
|
|
2014-08-11 20:58:10 +00:00
|
|
|
if not options.password_file:
|
2014-02-26 15:01:59 +00:00
|
|
|
password, new_password = utils.ask_vault_passwords(ask_vault_pass=True, confirm_vault=True)
|
|
|
|
else:
|
2014-08-11 20:58:10 +00:00
|
|
|
password = utils.read_vault_file(options.password_file)
|
2014-02-11 17:03:11 +00:00
|
|
|
|
2014-03-10 21:15:44 +00:00
|
|
|
cipher = 'AES256'
|
2014-02-24 18:09:36 +00:00
|
|
|
if hasattr(options, 'cipher'):
|
|
|
|
cipher = options.cipher
|
|
|
|
|
2014-02-11 17:03:11 +00:00
|
|
|
for f in args:
|
2014-02-24 18:09:36 +00:00
|
|
|
this_editor = VaultEditor(cipher, password, f)
|
|
|
|
this_editor.encrypt_file()
|
2014-02-11 17:03:11 +00:00
|
|
|
|
|
|
|
print "Encryption successful"
|
|
|
|
|
|
|
|
def execute_rekey(args, options, parser):
|
|
|
|
|
2014-08-11 20:58:10 +00:00
|
|
|
if not options.password_file:
|
2014-02-26 15:01:59 +00:00
|
|
|
password, __ = utils.ask_vault_passwords(ask_vault_pass=True)
|
|
|
|
else:
|
2014-08-11 20:58:10 +00:00
|
|
|
password = utils.read_vault_file(options.password_file)
|
2014-02-26 15:01:59 +00:00
|
|
|
|
|
|
|
__, new_password = utils.ask_vault_passwords(ask_vault_pass=False, ask_new_vault_pass=True, confirm_new=True)
|
|
|
|
|
2014-02-24 18:09:36 +00:00
|
|
|
cipher = None
|
2014-02-11 17:03:11 +00:00
|
|
|
for f in args:
|
2014-02-24 18:09:36 +00:00
|
|
|
this_editor = VaultEditor(cipher, password, f)
|
|
|
|
this_editor.rekey_file(new_password)
|
2014-02-11 17:03:11 +00:00
|
|
|
|
|
|
|
print "Rekey successful"
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------------------------
|
|
|
|
# MAIN
|
|
|
|
#-------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
|
|
|
action = get_action(sys.argv)
|
|
|
|
parser = build_option_parser(action)
|
|
|
|
(options, args) = parser.parse_args()
|
|
|
|
|
2014-06-14 11:42:47 +00:00
|
|
|
if not len(args):
|
|
|
|
raise errors.AnsibleError(
|
|
|
|
"The '%s' command requires a filename as the first argument" % action
|
|
|
|
)
|
|
|
|
|
2014-02-11 17:03:11 +00:00
|
|
|
# execute the desired action
|
|
|
|
try:
|
|
|
|
fn = globals()["execute_%s" % action]
|
|
|
|
fn(args, options, parser)
|
|
|
|
except Exception, err:
|
|
|
|
if options.debug:
|
|
|
|
print traceback.format_exc()
|
|
|
|
print "ERROR:",err
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|