From 3138702abfd7a6106ef6800820317febb504efc6 Mon Sep 17 00:00:00 2001 From: brent s Date: Mon, 13 Apr 2020 05:23:25 -0400 Subject: [PATCH] done editing --- vaultpass/QR.py | 1 - vaultpass/__init__.py | 11 ++++++++--- vaultpass/editor.py | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 4 deletions(-) create mode 100644 vaultpass/editor.py diff --git a/vaultpass/QR.py b/vaultpass/QR.py index 82d7219..3f9658a 100644 --- a/vaultpass/QR.py +++ b/vaultpass/QR.py @@ -1,7 +1,6 @@ import io import json import os -import subprocess import logging _logger = logging.getLogger() ## diff --git a/vaultpass/__init__.py b/vaultpass/__init__.py index 43c821f..ae6866d 100644 --- a/vaultpass/__init__.py +++ b/vaultpass/__init__.py @@ -14,6 +14,7 @@ from . import auth from . import clipboard from . import config from . import constants +from . import editor from . import gpg_handler from . import mounts from . import pass_import @@ -150,7 +151,6 @@ class VaultPass(object): if not data: _logger.error('No secret found') _logger.debug('The secret at path {0} on mount {1} does not exist.'.format(oldpath, mount)) - # TODO: left off here newexists = self._pathExists(newpath, mount = newmount) if newexists and not force: _logger.debug('The newpath {0}:{1} exists; prompting for confirmation.'.format(newmount, newpath)) @@ -230,8 +230,13 @@ class VaultPass(object): self.removeSecretName(kname, path, mount, force = force, destroy = destroy) return(handler(**args)) - def editSecret(self, path, mount, editor = constants.EDITOR, *args, **kwargs): - pass # TODO + def editSecret(self, path, mount, editor_prog = constants.EDITOR, *args, **kwargs): + data = self.getSecret(path, mount) + newdata, fpath = editor.Editor(data, editor = editor_prog) + print('Done. Deleting generated file.') + os.remove(fpath) + self.createSecret(newdata, path, mount, force = True) + return(newdata) def generateSecret(self, path, diff --git a/vaultpass/editor.py b/vaultpass/editor.py new file mode 100644 index 0000000..20136a8 --- /dev/null +++ b/vaultpass/editor.py @@ -0,0 +1,37 @@ +import json +import logging +import subprocess +import tempfile +_logger = logging.getLogger() +## +from . import constants + + +def Editor(data, editor = constants.EDITOR, *args, **kwargs): + if isinstance(data, dict): + data = json.dumps(dict, indent = 4) + if not isinstance(data, str): + data = str(data) + _logger.debug('Spawning edit instance.') + fpath = tempfile.mkstemp(prefix = '.vaultpass.edit.', suffix = '.json', dir = '/dev/shm')[1] + _logger.debug('Writing secret to {0} for editing.'.format(fpath)) + with open(fpath, 'w') as fh: + fh.write(data) + # We want this to block. + cmd = subprocess.run([editor, fpath], stdout = subprocess.PIPE, stderr = subprocess.PIPE) + if cmd.returncode != 0: + _logger.error('{0} returned non-zero status code'.format(editor)) + for x in ('stdin', 'stdout'): + o = getattr(cmd, x) + if not o: + continue + o = o.decode('utf-8').strip() + if o != '': + _logger.debug('{0}: {1}'.format(x.upper(), o)) + with open(fpath, 'r') as fh: + rawdata = fh.read() + try: + data = json.loads(rawdata) + except (json.decoder.JSONDecodeError, TypeError): + data = rawdata + return(data, fpath)