done editing

This commit is contained in:
brent s. 2020-04-13 05:23:25 -04:00
parent 062dd46c27
commit 3138702abf
Signed by: bts
GPG Key ID: 8C004C2F93481F6B
3 changed files with 45 additions and 4 deletions

View File

@ -1,7 +1,6 @@
import io
import json
import os
import subprocess
import logging
_logger = logging.getLogger()
##

View File

@ -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,

37
vaultpass/editor.py Normal file
View File

@ -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)