Skip to content

Commit

Permalink
openpgp: Obtain debug logs from GnuPG
Browse files Browse the repository at this point in the history
Signed-off-by: Michał Górny <mgorny@gentoo.org>
  • Loading branch information
mgorny committed Oct 18, 2019
1 parent 51bd16f commit 3f3f721
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
2 changes: 1 addition & 1 deletion gemato/cli.py
Expand Up @@ -99,7 +99,7 @@ def parse_args(self, args, argp):
env_class = gemato.openpgp.OpenPGPEnvironment
else:
env_class = gemato.openpgp.OpenPGPSystemEnvironment
self.openpgp_env = env_class()
self.openpgp_env = env_class(debug=args.debug)

if args.openpgp_key is not None:
with io.open(args.openpgp_key, 'rb') as f:
Expand Down
41 changes: 31 additions & 10 deletions gemato/openpgp.py
Expand Up @@ -34,9 +34,10 @@ class OpenPGPSystemEnvironment(object):
(user's home directory or GNUPGHOME).
"""

__slots__ = ['_impl']
__slots__ = ['debug', '_impl']

def __init__(self):
def __init__(self, debug=False):
self.debug = debug
self._impl = None

def __enter__(self):
Expand Down Expand Up @@ -189,28 +190,43 @@ class OpenPGPEnvironment(OpenPGPSystemEnvironment):

__slots__ = ['_home']

def __init__(self):
super(OpenPGPEnvironment, self).__init__()
def __init__(self, debug=False):
super(OpenPGPEnvironment, self).__init__(debug=debug)
self._home = tempfile.mkdtemp()

with open(os.path.join(self._home, 'dirmngr.conf'), 'w') as f:
f.write('''# autogenerated by gemato
# honor user's http_proxy setting
honor-http-proxy
''')
# enable debugging, in case we needed it
log-file {debug_file}
debug-level guru
debug-all
'''.format(debug_file=os.path.join(self._home, 'dirmngr.log')))
with open(os.path.join(self._home, 'gpg.conf'), 'w') as f:
f.write('''# autogenerated by gemato
# we are using an isolated keyring, so always trust our keys
trust-model always
''')
# enable debugging, in case we needed it
log-file {debug_file}
debug-level guru
debug-all
'''.format(debug_file=os.path.join(self._home, 'gpg.log')))
with open(os.path.join(self._home, 'gpg-agent.conf'), 'w') as f:
f.write('''# autogenerated by gemato
# avoid any smartcard operations, we are running in isolation
disable-scdaemon
''')
# enable debugging, in case we needed it
log-file {debug_file}
debug-level guru
debug-all
'''.format(debug_file=os.path.join(self._home, 'gpg-agent.log')))

def __exit__(self, exc_type, exc_value, exc_cb):
if self._home is not None:
Expand All @@ -234,9 +250,14 @@ def _rmtree_error_handler(func, path, exc_info):

def close(self):
if self._home is not None:
# we need to loop due to ENOTEMPTY potential
while os.path.isdir(self._home):
shutil.rmtree(self._home, onerror=self._rmtree_error_handler)
if not self.debug:
# we need to loop due to ENOTEMPTY potential
while os.path.isdir(self._home):
shutil.rmtree(self._home,
onerror=self._rmtree_error_handler)
else:
logging.debug('GNUPGHOME left for debug purposes: {}'
.format(self._home))
self._home = None

def import_key(self, keyfile):
Expand Down

0 comments on commit 3f3f721

Please sign in to comment.