Skip to content

Commit

Permalink
Add the "edit --key <k>" action to sec and cfg objects
Browse files Browse the repository at this point in the history
So users can easily edit text keys (like config files) stored as keys in
these objects.
  • Loading branch information
cvaroqui committed Jan 31, 2020
1 parent 8865671 commit dbbf8b8
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 9 deletions.
6 changes: 6 additions & 0 deletions lib/cfgmgr_parser.py
Expand Up @@ -39,6 +39,12 @@
OPT.value,
],
},
"edit": {
"msg": "Edit the current value of a key.",
"options": mp.ACTION_OPTS + [
OPT.key,
],
},
"append": {
"msg": "Append data to a key in the object.",
"options": mp.ACTION_OPTS + [
Expand Down
39 changes: 38 additions & 1 deletion lib/data.py
Expand Up @@ -5,9 +5,10 @@
import fnmatch
import shutil
import glob
import tempfile

from rcGlobalEnv import rcEnv
from rcUtilities import lazy, makedirs, split_path, fmt_path, factory, want_context, bencode
from rcUtilities import lazy, makedirs, split_path, fmt_path, factory, want_context, bencode, which
from svc import BaseSvc
from converters import print_size
import rcExceptions as ex
Expand Down Expand Up @@ -113,6 +114,42 @@ def recurse(key, path):
recurse(key, fpath)
recurse(key, path)

@staticmethod
def tempfilename():
tmpf = tempfile.NamedTemporaryFile()
try:
return tmpf.name
finally:
tmpf.close()

def edit(self):
buff = self.decode_key(self.options.key)
if buff is None:
raise ex.excError("could not decode the secret key '%s'" % self.options.key)
if "EDITOR" in os.environ:
editor = os.environ["EDITOR"]
elif os.name == "nt":
editor = "notepad"
else:
editor = "vi"
if not which(editor):
raise ex.excError("%s not found" % editor)
fpath = self.tempfilename()
try:
with open(fpath, "wb") as f:
f.write(buff)
except TypeError as exc:
with open(fpath, "w") as f:
f.write(buff)
try:
os.system(' '.join((editor, fpath)))
with open(fpath, "r") as f:
edited = f.read()
if buff == edited:
return
self.add_key(self.options.key, edited)
finally:
os.unlink(fpath)

def decode(self):
buff = self.decode_key(self.options.key)
Expand Down
8 changes: 0 additions & 8 deletions lib/sec.py
Expand Up @@ -55,14 +55,6 @@ def decode_key(self, key):
data = data[6:]
return self.decrypt(base64.urlsafe_b64decode(data.encode("ascii")))[2]

@staticmethod
def tempfilename():
tmpf = tempfile.NamedTemporaryFile()
try:
return tmpf.name
finally:
tmpf.close()

def gen_cert(self):
data = {}
for key in ("cn", "c", "st", "l", "o", "ou", "email", "alt_names", "bits", "validity", "ca"):
Expand Down
6 changes: 6 additions & 0 deletions lib/secmgr_parser.py
Expand Up @@ -51,6 +51,12 @@
OPT.value,
],
},
"edit": {
"msg": "Edit the current value of a key.",
"options": mp.ACTION_OPTS + [
OPT.key,
],
},
"keys": {
"msg": "Show all keys available in this secret.",
},
Expand Down

0 comments on commit dbbf8b8

Please sign in to comment.