2022-01-03 09:33:21 +00:00
|
|
|
from app.config import (
|
|
|
|
ALIAS_DOMAINS,
|
|
|
|
PREMIUM_ALIAS_DOMAINS,
|
|
|
|
get_abs_path,
|
|
|
|
DISPOSABLE_FILE_PATH,
|
|
|
|
)
|
2021-10-12 12:36:47 +00:00
|
|
|
from app.db import Session
|
2020-03-08 22:09:32 +00:00
|
|
|
from app.log import LOG
|
2022-01-03 09:33:21 +00:00
|
|
|
from app.models import Mailbox, Contact, SLDomain, InvalidMailboxDomain
|
2020-03-08 22:09:32 +00:00
|
|
|
from app.pgp_utils import load_public_key
|
2021-10-26 08:52:28 +00:00
|
|
|
from server import create_light_app
|
2020-03-08 22:09:32 +00:00
|
|
|
|
|
|
|
|
2020-06-07 10:46:59 +00:00
|
|
|
def load_pgp_public_keys():
|
2020-03-08 22:09:32 +00:00
|
|
|
"""Load PGP public key to keyring"""
|
2021-10-12 12:36:47 +00:00
|
|
|
for mailbox in Mailbox.filter(Mailbox.pgp_public_key.isnot(None)).all():
|
2020-06-07 10:46:59 +00:00
|
|
|
LOG.d("Load PGP key for mailbox %s", mailbox)
|
|
|
|
fingerprint = load_public_key(mailbox.pgp_public_key)
|
2020-03-08 22:09:32 +00:00
|
|
|
|
2020-06-07 10:46:59 +00:00
|
|
|
# sanity check
|
|
|
|
if fingerprint != mailbox.pgp_finger_print:
|
2021-09-08 09:29:55 +00:00
|
|
|
LOG.e("fingerprint %s different for mailbox %s", fingerprint, mailbox)
|
2020-06-07 10:46:59 +00:00
|
|
|
mailbox.pgp_finger_print = fingerprint
|
2021-10-12 12:36:47 +00:00
|
|
|
Session.commit()
|
2020-06-06 22:09:06 +00:00
|
|
|
|
2021-10-12 12:36:47 +00:00
|
|
|
for contact in Contact.filter(Contact.pgp_public_key.isnot(None)).all():
|
2020-06-06 22:09:06 +00:00
|
|
|
LOG.d("Load PGP key for %s", contact)
|
|
|
|
fingerprint = load_public_key(contact.pgp_public_key)
|
|
|
|
|
|
|
|
# sanity check
|
|
|
|
if fingerprint != contact.pgp_finger_print:
|
2021-09-08 09:29:55 +00:00
|
|
|
LOG.e("fingerprint %s different for contact %s", fingerprint, contact)
|
2020-06-06 22:09:06 +00:00
|
|
|
contact.pgp_finger_print = fingerprint
|
2020-03-08 22:09:32 +00:00
|
|
|
|
2021-10-12 12:36:47 +00:00
|
|
|
Session.commit()
|
2020-03-08 22:09:32 +00:00
|
|
|
|
2020-03-14 21:24:02 +00:00
|
|
|
LOG.d("Finish load_pgp_public_keys")
|
|
|
|
|
2020-03-08 22:09:32 +00:00
|
|
|
|
2020-10-15 14:51:07 +00:00
|
|
|
def add_sl_domains():
|
2020-07-04 21:18:30 +00:00
|
|
|
for alias_domain in ALIAS_DOMAINS:
|
2020-10-15 14:51:07 +00:00
|
|
|
if SLDomain.get_by(domain=alias_domain):
|
2020-10-15 16:38:39 +00:00
|
|
|
LOG.d("%s is already a SL domain", alias_domain)
|
2020-07-04 21:18:30 +00:00
|
|
|
else:
|
2021-09-08 09:29:55 +00:00
|
|
|
LOG.i("Add %s to SL domain", alias_domain)
|
2020-10-15 14:51:07 +00:00
|
|
|
SLDomain.create(domain=alias_domain)
|
2020-07-04 21:18:30 +00:00
|
|
|
|
2020-10-15 14:21:31 +00:00
|
|
|
for premium_domain in PREMIUM_ALIAS_DOMAINS:
|
2020-10-15 14:51:07 +00:00
|
|
|
if SLDomain.get_by(domain=premium_domain):
|
2020-10-15 16:38:39 +00:00
|
|
|
LOG.d("%s is already a SL domain", premium_domain)
|
2020-10-15 14:21:31 +00:00
|
|
|
else:
|
2021-09-08 09:29:55 +00:00
|
|
|
LOG.i("Add %s to SL domain", premium_domain)
|
2020-10-15 14:51:07 +00:00
|
|
|
SLDomain.create(domain=premium_domain, premium_only=True)
|
2020-10-15 14:21:31 +00:00
|
|
|
|
2021-10-12 12:36:47 +00:00
|
|
|
Session.commit()
|
2020-07-04 21:18:30 +00:00
|
|
|
|
|
|
|
|
2022-01-03 09:33:21 +00:00
|
|
|
def add_invalid_mailbox_domains():
|
|
|
|
with open(get_abs_path(DISPOSABLE_FILE_PATH), "r") as f:
|
|
|
|
disposable_email_domains = f.readlines()
|
|
|
|
disposable_email_domains = [d.strip().lower() for d in disposable_email_domains]
|
|
|
|
disposable_email_domains = [
|
|
|
|
d for d in disposable_email_domains if not d.startswith("#")
|
|
|
|
]
|
|
|
|
|
|
|
|
for domain in disposable_email_domains:
|
|
|
|
if InvalidMailboxDomain.get_by(domain=domain) is None:
|
|
|
|
LOG.i("Add disposable domain %s as invalid mailbox domain", domain)
|
2022-01-03 16:09:40 +00:00
|
|
|
InvalidMailboxDomain.create(domain=domain, commit=True)
|
2022-01-03 09:33:21 +00:00
|
|
|
|
|
|
|
|
2020-03-08 22:09:32 +00:00
|
|
|
if __name__ == "__main__":
|
2021-10-26 08:52:28 +00:00
|
|
|
# wrap in an app context to benefit from app setup like database cleanup, sentry integration, etc
|
|
|
|
with create_light_app().app_context():
|
|
|
|
load_pgp_public_keys()
|
|
|
|
add_sl_domains()
|
2022-01-03 09:33:21 +00:00
|
|
|
add_invalid_mailbox_domains()
|