diff --git a/api/.gitignore b/api/.gitignore index 5a72abd47..8cf51ee60 100644 --- a/api/.gitignore +++ b/api/.gitignore @@ -83,7 +83,6 @@ wheels/ .installed.cfg *.egg MANIFEST -Pipfile Pipfile.lock # PyInstaller @@ -144,7 +143,7 @@ celerybeat-schedule # Environments .env -.venv +.venv/* env/ venv/ ENV/ diff --git a/api/.gitkeep b/api/.gitkeep new file mode 100644 index 000000000..b694934fb --- /dev/null +++ b/api/.gitkeep @@ -0,0 +1 @@ +.venv \ No newline at end of file diff --git a/api/Pipfile b/api/Pipfile new file mode 100644 index 000000000..174b668fa --- /dev/null +++ b/api/Pipfile @@ -0,0 +1,24 @@ +[[source]] +url = "https://pypi.org/simple" +verify_ssl = true +name = "pypi" + +[packages] +requests = "==2.31.0" +urllib3 = "==1.26.16" +boto3 = "==1.26.148" +pyjwt = "==2.7.0" +psycopg2-binary = "==2.9.6" +elasticsearch = "==8.8.0" +jira = "==3.5.1" +fastapi = "==0.96.0" +uvicorn = {version = "==0.22.0", extras = ["standard"]} +python-decouple = "==3.8" +pydantic = {version = "==1.10.8", extras = ["email"]} +apscheduler = "==3.10.1" +redis = "==4.5.5" + +[dev-packages] + +[requires] +python_version = "3.11" diff --git a/api/chalicelib/utils/__init__.py b/api/chalicelib/utils/__init__.py index e69de29bb..0744a56a2 100644 --- a/api/chalicelib/utils/__init__.py +++ b/api/chalicelib/utils/__init__.py @@ -0,0 +1,10 @@ +from . import helper +import logging +from decouple import config + +logging.basicConfig(level=config("LOGLEVEL", default=logging.INFO)) + +if helper.has_smtp(): + logging.info("valid SMTP configuration found") +else: + logging.info("no SMTP configuration found or SMTP validation failed") diff --git a/api/chalicelib/utils/helper.py b/api/chalicelib/utils/helper.py index 4ae0bd113..1afc2e39e 100644 --- a/api/chalicelib/utils/helper.py +++ b/api/chalicelib/utils/helper.py @@ -1,3 +1,4 @@ +import logging import math import random import re @@ -8,6 +9,7 @@ from urllib.parse import urlparse from decouple import config import schemas +from chalicelib.utils import smtp from chalicelib.utils.TimeUTC import TimeUTC @@ -264,8 +266,25 @@ def __decimal_limit(value, limit): return value / factor +VALID_SMTP = None +SMTP_ERROR = None + + def has_smtp(): - return config("EMAIL_HOST") is not None and len(config("EMAIL_HOST")) > 0 + global VALID_SMTP, SMTP_ERROR + if SMTP_ERROR is not None: + logging.error("!!! SMTP error found, disabling SMTP configuration:") + logging.error(SMTP_ERROR) + + if VALID_SMTP is not None: + return VALID_SMTP + + if config("EMAIL_HOST") is not None and len(config("EMAIL_HOST")) > 0: + VALID_SMTP, SMTP_ERROR = smtp.check_connexion() + return VALID_SMTP + else: + logging.info("no SMTP configuration found") + return False def old_search_payload_to_flat(values): diff --git a/api/chalicelib/utils/smtp.py b/api/chalicelib/utils/smtp.py index a4710b42f..03e812a1f 100644 --- a/api/chalicelib/utils/smtp.py +++ b/api/chalicelib/utils/smtp.py @@ -45,3 +45,43 @@ class SMTPClient: if self.server is None: return self.server.quit() + + def test_configuration(self): + # check server connexion + try: + status = self.server.noop()[0] + if not (status == 250): + raise Exception(f"SMTP connexion error, status:{status}") + except Exception as e: # smtplib.SMTPServerDisconnected + logging.error( + f'!! SMTP connexion error to {config("EMAIL_HOST")}:{config("EMAIL_PORT", cast=int)}') + logging.error(e) + return False, e + + # check authentication + try: + self.__enter__() + self.__exit__() + except Exception as e: + logging.error(f'!! SMTP authentication error to {config("EMAIL_HOST")}:{config("EMAIL_PORT", cast=int)}') + logging.error(e) + return False, e + + return True, None + + +def check_connexion(): + # check SMTP host&port + import socket + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + sock.settimeout(config("EMAIL_CHECK_TIMEOUT", cast=int, default=5)) + result = sock.connect_ex((config("EMAIL_HOST"), config("EMAIL_PORT", cast=int))) + sock.close() + if not (result == 0): + error = f"""!! SMTP {config("EMAIL_HOST")}:{config("EMAIL_PORT", cast=int)} is unreachable +f'please make sure the host&port are correct, and the SMTP protocol is authorized on your server.""" + logging.error(error) + sock.close() + return False, error + + return SMTPClient().test_configuration() diff --git a/ee/api/.gitignore b/ee/api/.gitignore index 8f88e3b1e..98b09f866 100644 --- a/ee/api/.gitignore +++ b/ee/api/.gitignore @@ -142,7 +142,7 @@ celerybeat-schedule # Environments .env -.venv +.venv/* env/ venv/ ENV/ @@ -175,7 +175,6 @@ SUBNETS.json chalicelib/.config chalicelib/saas README/* -Pipfile Pipfile.lock .local/* diff --git a/ee/api/.gitkeep b/ee/api/.gitkeep new file mode 100644 index 000000000..b694934fb --- /dev/null +++ b/ee/api/.gitkeep @@ -0,0 +1 @@ +.venv \ No newline at end of file diff --git a/ee/api/Pipfile b/ee/api/Pipfile new file mode 100644 index 000000000..ceab80dcb --- /dev/null +++ b/ee/api/Pipfile @@ -0,0 +1,28 @@ +[[source]] +url = "https://pypi.org/simple" +verify_ssl = true +name = "pypi" + +[packages] +requests = "==2.31.0" +urllib3 = "==1.26.16" +boto3 = "==1.26.148" +pyjwt = "==2.7.0" +psycopg2-binary = "==2.9.6" +elasticsearch = "==8.8.0" +jira = "==3.5.1" +fastapi = "==0.96.0" +python-decouple = "==3.8" +apscheduler = "==3.10.1" +python3-saml = "==1.15.0" +python-multipart = "==0.0.6" +redis = "==4.5.5" +azure-storage-blob = "==12.16.0" +uvicorn = {version = "==0.22.0", extras = ["standard"]} +pydantic = {version = "==1.10.8", extras = ["email"]} +clickhouse-driver = {version = "==0.2.5", extras = ["lz4"]} + +[dev-packages] + +[requires] +python_version = "3.11"