2023-11-01 21:30:26 +01:00
|
|
|
"""Database schema for Lacre.
|
|
|
|
|
|
|
|
This definition includes:
|
|
|
|
|
2023-12-02 19:25:24 +01:00
|
|
|
- 'lacre_keys' -- temporary key storage, used by the frontend to submit keys and
|
2023-11-01 21:30:26 +01:00
|
|
|
by webgate-cron script to import submitted keys.
|
|
|
|
|
2023-12-02 19:25:24 +01:00
|
|
|
- 'lacre_identities' -- identity catalogue, used by encryption logic to match
|
2023-11-01 21:30:26 +01:00
|
|
|
emails with corresponding keys.
|
2024-02-21 21:09:15 +01:00
|
|
|
|
|
|
|
- 'lacre_locks' -- used only by the frontend.
|
2023-11-01 21:30:26 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
import sqlalchemy
|
|
|
|
|
2023-12-02 19:25:24 +01:00
|
|
|
# Values for lacre_keys.status column:
|
2024-01-20 18:52:47 +01:00
|
|
|
# - ST_DEFAULT: initial state;
|
|
|
|
# - ST_IMPORTED: key has been successfully processed by cron job;
|
2024-04-05 17:19:01 +02:00
|
|
|
# - ST_TO_BE_DELETED: key can be deleted. We only have checks for this value
|
|
|
|
# but never assign it, so this is a candidate for removal.
|
2023-11-01 21:30:26 +01:00
|
|
|
ST_DEFAULT, ST_IMPORTED, ST_TO_BE_DELETED = range(3)
|
|
|
|
|
2024-01-20 18:52:47 +01:00
|
|
|
# lacre_keys.confirmed is set to an empty string when a key is confirmed by the user.
|
|
|
|
CO_CONFIRMED = ''
|
|
|
|
|
2023-11-01 21:30:26 +01:00
|
|
|
_meta = sqlalchemy.MetaData()
|
|
|
|
|
2023-12-02 19:25:24 +01:00
|
|
|
LACRE_KEYS = sqlalchemy.Table('lacre_keys', _meta,
|
2024-03-03 09:25:14 +01:00
|
|
|
sqlalchemy.Column('id', sqlalchemy.Integer, primary_key=True, nullable=False, autoincrement='auto'),
|
2024-01-20 18:52:47 +01:00
|
|
|
sqlalchemy.Column('email', sqlalchemy.String(256), index=True),
|
2023-11-01 21:30:26 +01:00
|
|
|
# ASCII-armored key
|
|
|
|
sqlalchemy.Column('publickey', sqlalchemy.Text),
|
2024-01-20 18:52:47 +01:00
|
|
|
# Empty string means this key has been confirmed.
|
2023-11-01 21:30:26 +01:00
|
|
|
sqlalchemy.Column('confirm', sqlalchemy.String(32)),
|
|
|
|
# Status: see ST_* constants at the top of the file.
|
2024-03-03 09:25:14 +01:00
|
|
|
sqlalchemy.Column('status', sqlalchemy.Integer, nullable=False, default=0),
|
2023-11-01 21:30:26 +01:00
|
|
|
sqlalchemy.Column('time', sqlalchemy.DateTime))
|
|
|
|
|
2024-02-21 21:09:15 +01:00
|
|
|
LACRE_LOCKS = sqlalchemy.Table('lacre_locks', _meta,
|
|
|
|
sqlalchemy.Column('id', sqlalchemy.Integer, primary_key=True, nullable=False, autoincrement='auto'),
|
|
|
|
sqlalchemy.Column('ip', sqlalchemy.String(16)),
|
|
|
|
sqlalchemy.Column('time', sqlalchemy.Integer),
|
|
|
|
sqlalchemy.Column('action', sqlalchemy.String(16)),
|
|
|
|
sqlalchemy.Column('num', sqlalchemy.Integer),
|
|
|
|
)
|
|
|
|
|
2023-12-02 19:25:24 +01:00
|
|
|
LACRE_IDENTITIES = sqlalchemy.Table('lacre_identities', _meta,
|
2024-02-21 21:09:15 +01:00
|
|
|
sqlalchemy.Column('email', sqlalchemy.String(256), index=True, nullable=False),
|
2023-11-01 21:30:26 +01:00
|
|
|
# Key fingerprint
|
2024-02-21 21:09:15 +01:00
|
|
|
sqlalchemy.Column('fingerprint', sqlalchemy.String(64), index=True, nullable=False))
|
2023-11-12 20:20:38 +01:00
|
|
|
|
|
|
|
def init_identities_table() -> sqlalchemy.Table:
|
2023-12-02 19:25:24 +01:00
|
|
|
return LACRE_IDENTITIES
|
2023-11-20 22:09:50 +01:00
|
|
|
|
2024-02-21 21:09:15 +01:00
|
|
|
def init_locks_table() -> sqlalchemy.Table:
|
|
|
|
return LACRE_LOCKS
|
|
|
|
|
|
|
|
def init_keys_table() -> sqlalchemy.Table:
|
|
|
|
return LACRE_KEYS
|
|
|
|
|
|
|
|
def create_tables(engine):
|
|
|
|
_meta.create_all(engine)
|
|
|
|
|
2023-11-20 22:09:50 +01:00
|
|
|
def table_metadata():
|
|
|
|
return _meta
|