Move the last key-related function to keyring module

This commit is contained in:
Piotr F. Mieszkowski 2023-04-16 07:28:46 +02:00
parent 285f5dbf18
commit 34e8b6a4eb
2 changed files with 13 additions and 8 deletions

View File

@ -51,7 +51,8 @@ def _gpg_encrypt(raw_message, recipients):
LOG.error("No valid entry for gpg keyhome. Encryption aborted.")
return recipients
gpg_recipients, cleartext_recipients = recpt.identify_gpg_recipients(recipients, _load_keys())
gpg_recipients, cleartext_recipients = \
recpt.identify_gpg_recipients(recipients, kcache.freeze_and_load_keys())
LOG.info(f"Got addresses: gpg_to={gpg_recipients!r}, ungpg_to={cleartext_recipients!r}")
@ -143,12 +144,6 @@ def _customise_headers(message: EmailMessage):
message['X-GPG-Mailgate'] = 'Encrypted by GPG Mailgate'
def _load_keys():
"""Return a map from a key's fingerprint to email address."""
keyring = kcache.KeyRing(conf.get_item('gpg', 'keyhome'))
return asyncio.run(keyring.freeze_identities())
def _encrypt_all_payloads_inline(message: EmailMessage, gpg_to_cmdline):
# This breaks cascaded MIME messages. Blame PGP/INLINE.

View File

@ -9,7 +9,7 @@ import lacre.config as conf
import logging
from os import stat
from watchdog.events import FileSystemEventHandler, FileSystemEvent
from asyncio import Semaphore, create_task, get_event_loop
from asyncio import Semaphore, create_task, get_event_loop, run
import copy
import GnuPG
@ -163,3 +163,13 @@ class KeyringModificationListener(FileSystemEventHandler):
# on_created = handle
# on_deleted = handle
on_modified = handle
def freeze_and_load_keys():
"""Load and return keys.
Doesn't refresh the keys when they change on disk.
'"""
keyring_dir = conf.get_item('gpg', 'keyhome')
keyring = KeyRing(keyring_dir)
return run(keyring.freeze_identities())