fix(chalice): check SMTP host:port (#1344)

fix(chalice): validate SMTP configuration
refactor(chalice): include Pipfile for similar dev-env
This commit is contained in:
Kraiem Taha Yassine 2023-06-14 13:21:22 +02:00 committed by GitHub
parent 837d5ebeb3
commit de3976a27d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 126 additions and 5 deletions

3
api/.gitignore vendored
View file

@ -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/

1
api/.gitkeep Normal file
View file

@ -0,0 +1 @@
.venv

24
api/Pipfile Normal file
View file

@ -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"

View file

@ -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")

View file

@ -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):

View file

@ -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()

3
ee/api/.gitignore vendored
View file

@ -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/*

1
ee/api/.gitkeep Normal file
View file

@ -0,0 +1 @@
.venv

28
ee/api/Pipfile Normal file
View file

@ -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"