Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for text selection instead of whole file #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 40 additions & 20 deletions commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@

log = logging.getLogger(__name__)

ANSIBLE_COMMAND_TEMPLATE = 'ansible-vault {command} {extra_flags} {vault_password} "{vault_file}"'
ANSIBLE_COMMAND_TEMPLATE = 'ansible-vault {command} {extra_flags} {vault_password} {vault_file}'
GETPASS_WARNING = "Warning: Password input may be echoed.\nVault password: "


class AnsibleVaultBase:
open_new_tab = False
command = None
password = None
vault_file_path = None
vault_data = None
extra_flags = ''

@property
Expand Down Expand Up @@ -57,8 +57,8 @@ def get_setting(self, key, default=None):

return settings.get(key, default)

def prompt_vault_password(self, edit, vault_file_path):
bound_vault_command = partial(self.run_vault_command, edit, vault_file_path)
def prompt_vault_password(self, edit, vault_data, region):
bound_vault_command = partial(self.run_vault_command, edit, vault_data, region)
self.view.window().show_input_panel('Vault Password', '', bound_vault_command, self.on_change, self.on_cancel)

def on_change(self, password):
Expand All @@ -67,42 +67,65 @@ def on_change(self, password):
def on_cancel(self):
pass

def ansible_vault(self, edit, vault_file_path):
def ansible_vault(self, edit, vault_data, region, data_text=False):
# Use a password file is one is present
if self.password_file != '':
return self.run_vault_command(edit, vault_file_path, self.password_file, password_from_file=True)
return self.run_vault_command(edit, vault_data, region, self.password_file, data_text, password_from_file=True)

# Back out if we're doing area selection as password file is the only one that works
if data_text is True:
sublime.error_message("Text selection is only supported with a vault pass file")
return

# Use a password if one is present
if self.password != '':
return self.run_vault_command(edit, vault_file_path, self.password)
return self.run_vault_command(edit, vault_data, region, self.password, data_text)

# No configured password, fallback to a prompt
self.prompt_vault_password(edit, vault_file_path)
self.prompt_vault_password(edit, vault_data, region)

def run_vault_command(self, edit, vault_file_path, password, password_from_file=False):
def get_selection(self, edit, decrpyt = False):
selections = self.view.sel()
if len(selections[0]) < 1:
region = sublime.Region(0, self.view.size())
vault_file = '"{}"'.format(self.view.file_name())
self.ansible_vault(edit, vault_file, region)
return

for region in selections:
text = self.view.substr(region)
if decrpyt:
text = text.replace(" ", "")
text = text.replace(" ", "")
self.ansible_vault(edit, text, region, True)

def run_vault_command(self, edit, vault_data, region, password, data_text=False, password_from_file=False):
vault_password_flag = '--vault-password-file "%s"' % password
password_input = None
vault_input = ''

if password_from_file is False:
vault_password_flag = '--ask-vault-pass'
password_input = password
vault_input = password

if data_text is True:
vault_input = vault_data
vault_data = ''

command = ANSIBLE_COMMAND_TEMPLATE.format(
vault_password=vault_password_flag,
command=self.command,
vault_file=vault_file_path,
vault_file=vault_data,
extra_flags=self.extra_flags,
)
with Popen([command], stdin=PIPE, stdout=PIPE, stderr=PIPE, shell=True, universal_newlines=True) as proc:
output, error = proc.communicate(input=password_input)
output, error = proc.communicate(input=vault_input)

if self.error_handler(error) is True:
return

if self.open_new_tab is True:
self.view.run_command('ansible_vault_output', {'output': output, 'title': vault_file_path})
self.view.run_command('ansible_vault_output', {'output': output, 'title': vault_data})
else:
region = sublime.Region(0, self.view.size())
self.view.replace(edit, region, output)

def error_handler(self, error):
Expand Down Expand Up @@ -147,14 +170,11 @@ class AnsibleVaultDecryptCommand(AnsibleVaultBase, sublime_plugin.TextCommand):
extra_flags = '--output=-'

def run(self, edit):
vault_file = self.view.file_name()
self.ansible_vault(edit, vault_file)

self.get_selection(edit, True)

class AnsibleVaultEncryptCommand(AnsibleVaultBase, sublime_plugin.TextCommand):
command = 'encrypt'
extra_flags = '--output=-'

def run(self, edit):
vault_file = self.view.file_name()
self.ansible_vault(edit, vault_file)
self.get_selection(edit)