Implement identity repository
Also: rename key_id to fingerprint.
This commit is contained in:
parent
56da7e0cb4
commit
bfa2643dc7
4 changed files with 40 additions and 12 deletions
|
@ -29,7 +29,10 @@ GPGMW_KEYS = sqlalchemy.Table('gpgmw_keys', _meta,
|
|||
GPGMW_IDENTITIES = sqlalchemy.Table('gpgmw_identities', _meta,
|
||||
sqlalchemy.Column('email', sqlalchemy.String(256), index=True),
|
||||
# Key fingerprint
|
||||
sqlalchemy.Column('key_id', sqlalchemy.String(64), index=True))
|
||||
sqlalchemy.Column('fingerprint', sqlalchemy.String(64), index=True))
|
||||
|
||||
def init_identities_table() -> sqlalchemy.Table:
|
||||
return GPGMW_IDENTITIES
|
||||
|
||||
def table_metadata():
|
||||
return _meta
|
||||
|
|
|
@ -13,9 +13,34 @@ class IdentityRepository:
|
|||
self._identities = identity_table
|
||||
self._conn = connection
|
||||
|
||||
def register(self, email, fingerprint):
|
||||
# TODO: upsert
|
||||
self._identities.insert().values(email=email, fingerprint=fingerprint)
|
||||
def register_or_update(self, email, fprint):
|
||||
assert email, "email is mandatory"
|
||||
assert fprint, "fprint is mandatory"
|
||||
|
||||
if self._exists(email):
|
||||
self._update(email, fprint)
|
||||
else:
|
||||
self._insert(email, fprint)
|
||||
|
||||
def _exists(self, email: str) -> bool:
|
||||
selq = select(self._identities.c.email).where(self._identities.c.email == email)
|
||||
emails = [e for e in self._conn.execute(selq)]
|
||||
assert len(emails) == 1
|
||||
return emails
|
||||
|
||||
def _insert(self, email, fprint):
|
||||
insq = self._identities.insert().values(email=email, fingerprint=fprint)
|
||||
|
||||
LOG.debug('Registering identity %s: %s', email, insq)
|
||||
self._conn.execute(insq)
|
||||
|
||||
def _update(self, email, fprint):
|
||||
upq = self._identities.update() \
|
||||
.values(fingerprint=fprint) \
|
||||
.where(self._identities.c.email == email)
|
||||
|
||||
LOG.debug('Updating identity %s: %s', email, upq)
|
||||
self._conn.execute(upq)
|
||||
|
||||
|
||||
class KeyConfirmationQueue:
|
||||
|
@ -32,8 +57,8 @@ class KeyConfirmationQueue:
|
|||
"""Runs a query to retrieve at most `keys_read_max` keys and returns db result."""
|
||||
max_keys = max_keys or self.keys_read_max
|
||||
|
||||
selq = select(self._keys.c.publickey, self._keys.c.id, self._keys.c.email)\
|
||||
.where(and_(self._keys.c.status == db.ST_DEFAULT, self._keys.c.confirm == ""))\
|
||||
selq = select(self._keys.c.publickey, self._keys.c.id, self._keys.c.email) \
|
||||
.where(and_(self._keys.c.status == db.ST_DEFAULT, self._keys.c.confirm == "")) \
|
||||
.limit(max_keys)
|
||||
|
||||
LOG.debug('Retrieving keys to be processed: %s', selq)
|
||||
|
|
|
@ -15,7 +15,7 @@ def define_db_schema():
|
|||
|
||||
identities = sqlalchemy.Table('gpgmw_identities', meta,
|
||||
sqlalchemy.Column('email', sqlalchemy.String(256), index=True),
|
||||
sqlalchemy.Column('key_id', sqlalchemy.String(64), index=True))
|
||||
sqlalchemy.Column('fingerprint', sqlalchemy.String(64), index=True))
|
||||
|
||||
return (meta, gpgmw_keys, identities)
|
||||
|
||||
|
@ -95,7 +95,7 @@ OjjB6xRD0Q2FN+alsNGCtdutAs18AZ5l33RMzws=\n\
|
|||
])
|
||||
|
||||
conn.execute(identities.insert(), [
|
||||
{'key_id': '1CD245308F0963D038E88357973CF4D9387C44D7', 'email': 'alice@disposlab'},
|
||||
{'key_id': '19CF4B47ECC9C47AFA84D4BD96F39FDA0E31BB67', 'email': 'bob@disposlab'},
|
||||
{'key_id': '530B1BB2D0CC7971648198BBA4774E507D3AF5BC', 'email': 'evan@disposlab'}
|
||||
{'fingerprint': '1CD245308F0963D038E88357973CF4D9387C44D7', 'email': 'alice@disposlab'},
|
||||
{'fingerprint': '19CF4B47ECC9C47AFA84D4BD96F39FDA0E31BB67', 'email': 'bob@disposlab'},
|
||||
{'fingerprint': '530B1BB2D0CC7971648198BBA4774E507D3AF5BC', 'email': 'evan@disposlab'}
|
||||
])
|
||||
|
|
|
@ -29,7 +29,6 @@ import logging
|
|||
import lacre
|
||||
import lacre.config as conf
|
||||
import lacre.dbschema as db
|
||||
from lacre.repositories import KeyConfirmationQueue, IdentityRepository
|
||||
from lacre.notify import notify
|
||||
|
||||
# Read configuration from /etc/gpg-mailgate.conf
|
||||
|
@ -39,6 +38,7 @@ lacre.init_logging(conf.get_item('logging', 'config'))
|
|||
LOG = logging.getLogger('webgate-cron.py')
|
||||
|
||||
import GnuPG
|
||||
from lacre.repositories import KeyConfirmationQueue, IdentityRepository
|
||||
|
||||
|
||||
def _setup_db_connection(url):
|
||||
|
@ -83,7 +83,7 @@ if conf.flag_enabled('database', 'enabled') and conf.config_item_set('database',
|
|||
(fingerprint, _) = GnuPG.add_key(key_dir, armored_key)
|
||||
|
||||
key_queue.mark_accepted(row_id)
|
||||
identities.register(email, fingerprint)
|
||||
identities.register_or_update(email, fingerprint)
|
||||
|
||||
LOG.info('Imported key from <%s>', email)
|
||||
if conf.flag_enabled('cron', 'send_email'):
|
||||
|
|
Loading…
Reference in a new issue