From 43ccccb6e0658fbb0c90cc163f578bd3ab23f385 Mon Sep 17 00:00:00 2001 From: Tal Borenstein Date: Wed, 30 Oct 2024 11:33:22 +0200 Subject: [PATCH 1/3] fix(smtp): username and password are not mandatory --- keep/providers/smtp_provider/smtp_provider.py | 62 ++++++++++--------- 1 file changed, 32 insertions(+), 30 deletions(-) diff --git a/keep/providers/smtp_provider/smtp_provider.py b/keep/providers/smtp_provider/smtp_provider.py index c0606349a..17f8ca03f 100644 --- a/keep/providers/smtp_provider/smtp_provider.py +++ b/keep/providers/smtp_provider/smtp_provider.py @@ -4,11 +4,11 @@ import dataclasses import typing - -import pydantic -from smtplib import SMTP, SMTP_SSL from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText +from smtplib import SMTP, SMTP_SSL + +import pydantic from keep.contextmanager.contextmanager import ContextManager from keep.providers.base.base_provider import BaseProvider @@ -19,19 +19,21 @@ class SmtpProviderAuthConfig: smtp_username: str = dataclasses.field( metadata={ - "required": True, + "required": False, "description": "SMTP username", "config_main_group": "authentication", - } + }, + default="", ) smtp_password: str = dataclasses.field( metadata={ - "required": True, + "required": False, "sensitive": True, "description": "SMTP password", "config_main_group": "authentication", - } + }, + default="", ) smtp_server: str = dataclasses.field( @@ -75,18 +77,18 @@ class SmtpProvider(BaseProvider): PROVIDER_DISPLAY_NAME = "SMTP" def __init__( - self, context_manager: ContextManager, provider_id: str, config: ProviderConfig + self, context_manager: ContextManager, provider_id: str, config: ProviderConfig ): super().__init__(context_manager, provider_id, config) def dispose(self): pass - + def validate_config(self): self.authentication_config = SmtpProviderAuthConfig( **self.config.authentication ) - + def validate_scopes(self): """ Validate that the scopes provided are correct. @@ -97,7 +99,7 @@ def validate_scopes(self): return {"send_email": True} except Exception as e: return {"send_email": str(e)} - + def generate_smtp_client(self): """ Generate an SMTP client. @@ -108,18 +110,20 @@ def generate_smtp_client(self): smtp_port = self.authentication_config.smtp_port encryption = self.authentication_config.encryption - if (encryption == "SSL"): + if encryption == "SSL": smtp = SMTP_SSL(smtp_server, smtp_port) - smtp.login(smtp_username, smtp_password) - return smtp - - elif (encryption == "TLS"): + elif encryption == "TLS": smtp = SMTP(smtp_server, smtp_port) smtp.starttls() + + if smtp_username and smtp_password: smtp.login(smtp_username, smtp_password) - return smtp - - def send_email(self, from_email: str, from_name: str, to_email: str, subject: str, body: str): + + return smtp + + def send_email( + self, from_email: str, from_name: str, to_email: str, subject: str, body: str + ): """ Send an email using SMTP protocol. """ @@ -127,9 +131,9 @@ def send_email(self, from_email: str, from_name: str, to_email: str, subject: st if from_name == "": msg["From"] = from_email msg["From"] = f"{from_name} <{from_email}>" - msg['To'] = to_email - msg['Subject'] = subject - msg.attach(MIMEText(body, 'plain')) + msg["To"] = to_email + msg["Subject"] = subject + msg.attach(MIMEText(body, "plain")) try: smtp = self.generate_smtp_client() @@ -138,18 +142,16 @@ def send_email(self, from_email: str, from_name: str, to_email: str, subject: st except Exception as e: raise Exception(f"Failed to send email: {str(e)}") - - def _notify(self, from_email: str, from_name: str, to_email: str, subject: str, body: str): + + def _notify( + self, from_email: str, from_name: str, to_email: str, subject: str, body: str + ): """ Send an email using SMTP protocol. """ self.send_email(from_email, from_name, to_email, subject, body) - return { - "from": from_email, - "to": to_email, - "subject": subject, - "body": body - } + return {"from": from_email, "to": to_email, "subject": subject, "body": body} + if __name__ == "__main__": import logging From 494c9027f4a962e61a0e13b0c8fd02ff826af95f Mon Sep 17 00:00:00 2001 From: Tal Borenstein Date: Wed, 30 Oct 2024 11:33:51 +0200 Subject: [PATCH 2/3] chore: update version --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 758f57d9e..0fea838e2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "keep" -version = "0.27.7" +version = "0.27.8" description = "Alerting. for developers, by developers." authors = ["Keep Alerting LTD"] readme = "README.md" From 16941efb4e433183435f785b3e1ba4c9e4b7ee95 Mon Sep 17 00:00:00 2001 From: Tal Borenstein Date: Wed, 30 Oct 2024 11:36:55 +0200 Subject: [PATCH 3/3] fix: fix --- keep/providers/smtp_provider/smtp_provider.py | 41 ++++++++++--------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/keep/providers/smtp_provider/smtp_provider.py b/keep/providers/smtp_provider/smtp_provider.py index 17f8ca03f..f8f187c09 100644 --- a/keep/providers/smtp_provider/smtp_provider.py +++ b/keep/providers/smtp_provider/smtp_provider.py @@ -17,25 +17,6 @@ @pydantic.dataclasses.dataclass class SmtpProviderAuthConfig: - smtp_username: str = dataclasses.field( - metadata={ - "required": False, - "description": "SMTP username", - "config_main_group": "authentication", - }, - default="", - ) - - smtp_password: str = dataclasses.field( - metadata={ - "required": False, - "sensitive": True, - "description": "SMTP password", - "config_main_group": "authentication", - }, - default="", - ) - smtp_server: str = dataclasses.field( metadata={ "required": True, @@ -49,7 +30,8 @@ class SmtpProviderAuthConfig: "required": True, "description": "SMTP port", "config_main_group": "authentication", - } + }, + default=587, ) encryption: typing.Literal["SSL", "TLS"] = dataclasses.field( @@ -63,6 +45,25 @@ class SmtpProviderAuthConfig: }, ) + smtp_username: str = dataclasses.field( + metadata={ + "required": False, + "description": "SMTP username", + "config_main_group": "authentication", + }, + default="", + ) + + smtp_password: str = dataclasses.field( + metadata={ + "required": False, + "sensitive": True, + "description": "SMTP password", + "config_main_group": "authentication", + }, + default="", + ) + class SmtpProvider(BaseProvider): PROVIDER_SCOPES = [