Simplify code, improve log entries, add comments

This commit is contained in:
Piotr F. Mieszkowski 2022-10-22 11:19:47 +02:00
parent 00289759a3
commit 2ac26c09ce
6 changed files with 30 additions and 32 deletions

View File

@ -26,28 +26,29 @@ import logging
import lacre import lacre
import lacre.config as conf import lacre.config as conf
start = time.time() start = time.process_time()
conf.load_config() conf.load_config()
lacre.init_logging(conf.get_item('logging', 'config')) lacre.init_logging(conf.get_item('logging', 'config'))
# This has to be executed *after* logging initialisation. # This has to be executed *after* logging initialisation.
import lacre.core as core import lacre.core as core
LOG = logging.getLogger(__name__) LOG = logging.getLogger('gpg-mailgate.py')
missing_params = conf.validate_config() missing_params = conf.validate_config()
if missing_params: if missing_params:
LOG.error(f"Aborting delivery! Following mandatory config parameters are missing: {missing_params!r}") LOG.error(f"Aborting delivery! Following mandatory config parameters are missing: {missing_params!r}")
sys.exit(lacre.EX_CONFIG) sys.exit(lacre.EX_CONFIG)
# Read e-mail from stdin # Read e-mail from stdin, parse it
raw = sys.stdin.read() raw = sys.stdin.read()
raw_message = email.message_from_string(raw) raw_message = email.message_from_string(raw)
from_addr = raw_message['From'] from_addr = raw_message['From']
# Read recipients from the command-line
to_addrs = sys.argv[1:] to_addrs = sys.argv[1:]
# Let's start # Let's start
core.deliver_message(raw_message, from_addr, to_addrs) core.deliver_message(raw_message, from_addr, to_addrs)
(elapsed_s, process_t) = core.exec_time_info(start) process_t = (time.process_time() - start) * 1000
LOG.info("Elapsed-time: {elapsed:.2f}s; Process-time: {process:.4f}s".format(elapsed=elapsed_s, process=process_t)) LOG.info("Message delivered in {process:.2f} ms".format(process=process_t))

View File

@ -1,6 +1,7 @@
"""Lacre configuration """Lacre configuration.
Routines defined here are responsible for processing configuration. Routines defined here are responsible for processing and validating
configuration.
""" """
from configparser import RawConfigParser from configparser import RawConfigParser
@ -18,7 +19,8 @@ CONFIG_PATH_ENV = "GPG_MAILGATE_CONFIG"
MANDATORY_CONFIG_ITEMS = [("relay", "host"), MANDATORY_CONFIG_ITEMS = [("relay", "host"),
("relay", "port"), ("relay", "port"),
("daemon", "host"), ("daemon", "host"),
("daemon", "port")] ("daemon", "port"),
("gpg", "keyhome")]
# Global dict to keep configuration parameters. It's hidden behind several # Global dict to keep configuration parameters. It's hidden behind several
# utility functions to make it easy to replace it with ConfigParser object in # utility functions to make it easy to replace it with ConfigParser object in
@ -38,6 +40,9 @@ def load_config() -> dict:
parser = _read_config(configFile) parser = _read_config(configFile)
# XXX: Global variable. It is a left-over from old GPG-Mailgate code. We
# should drop it and probably use ConfigParser instance where configuration
# parameters are needed.
global cfg global cfg
cfg = _copy_to_dict(parser) cfg = _copy_to_dict(parser)
return cfg return cfg

View File

@ -1,9 +1,3 @@
"""Lacre's actual mail-delivery module.
IMPORTANT: This module has to be loaded _after_ initialisation of the logging
module.
"""
# #
# gpg-mailgate # gpg-mailgate
# #
@ -23,6 +17,12 @@ module.
# along with gpg-mailgate source code. If not, see <http://www.gnu.org/licenses/>. # along with gpg-mailgate source code. If not, see <http://www.gnu.org/licenses/>.
# #
"""Lacre's actual mail-delivery module.
IMPORTANT: This module has to be loaded _after_ initialisation of the logging
module.
"""
from email.mime.multipart import MIMEMultipart from email.mime.multipart import MIMEMultipart
import copy import copy
import email import email
@ -32,7 +32,6 @@ import GnuPG
import os import os
import smtplib import smtplib
import sys import sys
import time
import asyncio import asyncio
# imports for S/MIME # imports for S/MIME
@ -383,7 +382,7 @@ def _smime_encrypt(raw_message, recipients):
unsmime_to = list() unsmime_to = list()
for addr in recipients: for addr in recipients:
cert_and_email = _get_cert_for_email(addr[0], cert_path) cert_and_email = _get_cert_for_email(addr, cert_path)
if not (cert_and_email is None): if not (cert_and_email is None):
(to_cert, normal_email) = cert_and_email (to_cert, normal_email) = cert_and_email
@ -556,10 +555,3 @@ def deliver_message(raw_message: email.message.Message, from_address, to_addrs):
# Send out mail to recipients which are left # Send out mail to recipients which are left
LOG.debug("Sending the rest as text/plain") LOG.debug("Sending the rest as text/plain")
send_msg(raw_message.as_string(), recipients_left) send_msg(raw_message.as_string(), recipients_left)
def exec_time_info(start_timestamp):
"""Calculate time since the given timestamp."""
elapsed_s = time.time() - start_timestamp
process_t = time.process_time()
return (elapsed_s, process_t)

View File

@ -22,7 +22,7 @@ RESULT_NOT_IMPLEMENTED = '500 Not implemented yet'
# the last Lacre module, i.e. lacre.mailgate. # the last Lacre module, i.e. lacre.mailgate.
conf.load_config() conf.load_config()
lacre.init_logging(conf.get_item("logging", "config")) lacre.init_logging(conf.get_item("logging", "config"))
LOG = logging.getLogger(__name__) LOG = logging.getLogger('lacre.daemon')
import lacre.core as gate import lacre.core as gate
import lacre.keyring as kcache import lacre.keyring as kcache
@ -103,6 +103,8 @@ def _main():
asyncio.run(_sleep()) asyncio.run(_sleep())
except KeyboardInterrupt: except KeyboardInterrupt:
LOG.info("Finishing...") LOG.info("Finishing...")
except:
LOG.exception('Unexpected exception caught, your system may be unstable')
finally: finally:
LOG.info('Shutting down keyring watcher and the daemon...') LOG.info('Shutting down keyring watcher and the daemon...')
reloader.stop() reloader.stop()

View File

@ -17,10 +17,7 @@ LOG = logging.getLogger(__name__)
def _sanitize(keys): def _sanitize(keys):
for fingerprint in keys: return {fingerprint: text.sanitize_case_sense(keys[fingerprint]) for fingerprint in keys}
keys[fingerprint] = text.sanitize_case_sense(keys[fingerprint])
return keys
class KeyCacheMisconfiguration(Exception): class KeyCacheMisconfiguration(Exception):
@ -118,6 +115,7 @@ class KeyRing:
def _read_mod_time(self): def _read_mod_time(self):
# (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime) # (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)
# 0 1 2 3 4 5 6 7 8 9
MTIME = 8 MTIME = 8
st = stat(self._path) st = stat(self._path)
return st[MTIME] return st[MTIME]

View File

@ -73,8 +73,8 @@ class InlineOpenPGPEncrypt(OpenPGPEncrypt):
"""Encrypt with PGP Inline.""" """Encrypt with PGP Inline."""
LOG.debug('Sending PGP/Inline...') LOG.debug('Sending PGP/Inline...')
return core._gpg_encrypt_and_return(msg, return core._gpg_encrypt_and_return(msg,
self._keys, self._recipients, self._keys, self._recipients,
core._encrypt_all_payloads_inline) core._encrypt_all_payloads_inline)
class MimeOpenPGPEncrypt(OpenPGPEncrypt): class MimeOpenPGPEncrypt(OpenPGPEncrypt):
@ -88,8 +88,8 @@ class MimeOpenPGPEncrypt(OpenPGPEncrypt):
"""Encrypt with PGP MIME.""" """Encrypt with PGP MIME."""
LOG.debug('Sending PGP/MIME...') LOG.debug('Sending PGP/MIME...')
return core._gpg_encrypt_and_return(msg, return core._gpg_encrypt_and_return(msg,
self._keys, self._recipients, self._keys, self._recipients,
core._encrypt_all_payloads_mime) core._encrypt_all_payloads_mime)
class SMimeEncrypt(MailOperation): class SMimeEncrypt(MailOperation):