From 2b9b78d3f900e4af39eb7538cd52ad9c0cdc7e56 Mon Sep 17 00:00:00 2001 From: brent s Date: Sun, 29 Mar 2020 20:40:38 -0400 Subject: [PATCH] WHEW. GPG decryption now works correctly. --- testxml.py | 7 ------- vaultpass/config.py | 17 +++++++++-------- vaultpass/gpg_handler.py | 4 ++-- 3 files changed, 11 insertions(+), 17 deletions(-) delete mode 100755 testxml.py diff --git a/testxml.py b/testxml.py deleted file mode 100755 index 8e96509..0000000 --- a/testxml.py +++ /dev/null @@ -1,7 +0,0 @@ -#!/usr/bin/env python3 - -import vaultpass - -cfg = vaultpass.config.LocalFile('/tmp/vaultpass.xml') -cfg.main() - diff --git a/vaultpass/config.py b/vaultpass/config.py index 11090f7..3c9493f 100644 --- a/vaultpass/config.py +++ b/vaultpass/config.py @@ -46,8 +46,10 @@ class Config(object): def decryptGpg(self, gpg_xml): home = gpg_xml.attrib.get('gpgHome') tag = gpg_xml.tag - ns_xml = self.xml.find(tag) - xml = self.stripNS(obj = ns_xml).tag + ns_search = './/{0}'.format(tag) + ns_xml = self.namespaced_xml.find(ns_search) + search = './/{0}'.format(self.stripNS(obj = ns_xml).tag) + xml = self.xml.find(search) fpath = gpg_xml.text if not self.gpg: self.gpg = gpg_handler.GPG(home = home) @@ -129,15 +131,14 @@ class Config(object): return(None) def parseGpg(self): - gpg_elem_found = False # Change to True if we find any GPG-encrypted elems + gpg_elem_found = False search = [] for x in self.gpg_elems: search.append("local-name()='{0}'".format(x)) - search = '[{0}]'.format(' or '.join(search)) - print(search) - gpg_elems = self.namespaced_xml.findall('|'.join(search)) + search = '//*[{0}]'.format(' or '.join(search)) + gpg_elems = self.namespaced_xml.xpath(search) for e in gpg_elems: - print(e) + self.decryptGpg(e) return(gpg_elem_found) def parseRaw(self, parser = None): @@ -166,7 +167,7 @@ class Config(object): _logger.debug('Stripping namespace.') # https://stackoverflow.com/questions/30232031/how-can-i-strip-namespaces-out-of-an-lxml-tree/30233635#30233635 xpathq = "descendant-or-self::*[namespace-uri()!='']" - if not obj: + if obj is None: _logger.debug('No XML object selected; using instance\'s xml and tree.') for x in (self.tree, self.xml): for e in x.xpath(xpathq): diff --git a/vaultpass/gpg_handler.py b/vaultpass/gpg_handler.py index dfa2d01..a82686e 100644 --- a/vaultpass/gpg_handler.py +++ b/vaultpass/gpg_handler.py @@ -13,8 +13,7 @@ class GPG(object): gpg = None def __init__(self, home = None): - if home: - self.home = home + self.home = home self.initHome() def decrypt(self, fpath): @@ -36,4 +35,5 @@ class GPG(object): if not os.path.isdir(self.home): raise ValueError('GPG home does not exist') _logger.debug('Set GPG home to explicitly specified value {0}'.format(self.home)) + self.gpg = gpg.Context(home_dir = self.home) return(None)