From 2ffea278dd5cee9741457a2885702dffbd97d33a Mon Sep 17 00:00:00 2001 From: Luc Didry Date: Tue, 28 Nov 2023 12:20:49 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A8=20=E2=80=94=20Improve=20mail=20ale?= =?UTF-8?q?rting=20code?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- argos/schemas/config.py | 8 +++--- argos/server/alerting.py | 56 ++++++++++++---------------------------- 2 files changed, 20 insertions(+), 44 deletions(-) diff --git a/argos/schemas/config.py b/argos/schemas/config.py index 890ff2d..5fe305b 100644 --- a/argos/schemas/config.py +++ b/argos/schemas/config.py @@ -102,10 +102,10 @@ class MailAuth(BaseModel): class Mail(BaseModel): """Mail configuration""" mailfrom: EmailStr - host: Optional[str] = None - port: Optional[PositiveInt] = None - ssl: Optional[StrictBool] = None - starttls: Optional[StrictBool] = None + host: str = '127.0.0.1' + port: PositiveInt = 25 + ssl: StrictBool = False + starttls: StrictBool = False auth: Optional[MailAuth] = None addresses: List[EmailStr] diff --git a/argos/server/alerting.py b/argos/server/alerting.py index 7fc071b..ed9c106 100644 --- a/argos/server/alerting.py +++ b/argos/server/alerting.py @@ -4,16 +4,17 @@ import smtplib from urllib.parse import urlparse from argos.logging import logger +from argos.schemas.config import Config, Mail # XXX Implement mail alerts https://framagit.org/framasoft/framaspace/argos/-/issues/15 # XXX Implement gotify alerts https://framagit.org/framasoft/framaspace/argos/-/issues/16 -def handle_alert(config, result, task, severity): +def handle_alert(config: Config, result, task, severity): """Dispatch alert through configured alert channels""" msg = f"task={task.id}, status={result.status}, {severity=}" logger.error("Alerting stub: %s", msg) - if 'mail' in config and 'addresses' in config['mail']: + if config.general.mail is not None: logger.debug('Notify of failure by mail') msg = f"""\ @@ -24,57 +25,32 @@ Check: {task.check} Status: {severity} Time: {result.submitted_at} """ - notify_by_mail(msg=msg, config=config['mail']) + notify_by_mail(msg, config.general.mail) -def notify_by_mail(msg: str, config: dict) -> None: - """Notify by mail - - Keyword argument: - msg -- string, the mail to send (including subject) - config -- dict, configuration of mail system - """ - if 'mailfrom' not in config: - logger.error('No "mailfrom" address in mail config. ' - 'Not sending mail.') - return None - if 'host' not in config: - config['host'] = '127.0.0.1' - if 'port' not in config: - config['port'] = 25 - if 'ssl' not in config: - config['ssl'] = False - if 'starttls' not in config: - config['starttls'] = False - if config['ssl']: +def notify_by_mail(msg: str, config: Mail) -> None: + if config.ssl: logger.debug('Mail notification: SSL') context = ssl.create_default_context() - smtp = smtplib.SMTP_SSL(host=config['host'], - port=config['port'], + smtp = smtplib.SMTP_SSL(host=config.host, + port=config.port, context=context) else: - smtp = smtplib.SMTP(host=config['host'], # type: ignore - port=config['port']) - if config['starttls']: + smtp = smtplib.SMTP(host=config.host, # type: ignore + port=config.port) + if config.starttls: logger.debug('Mail notification: STARTTLS') context = ssl.create_default_context() smtp.starttls(context=context) - if 'auth' in config: - if 'login' not in config['auth'] \ - or 'password' not in config['auth']: - logger.warning('Mail credentials are incomplete. ' - 'No mail authentication can be done. ' - 'Not sending mail.') - return None - + if config.auth is not None: logger.debug('Mail notification: authentification') - smtp.login(config['auth']['login'], - config['auth']['password']) + smtp.login(config.auth.login, + config.auth.password) - for address in config['addresses']: + for address in config.addresses: logger.debug('Sending mail to %s', address) logger.debug(msg) - smtp.sendmail(config['from'], address, msg) + smtp.sendmail(config.mailfrom, address, msg) return None