fix(email): resolve SMTP authentication failure and improve configuration resilience
- Fixed a bug where missing 'id=0' in the 'cfg' table caused SMTP authentication to fail by defaulting to placeholder credentials. - Updated 'app/lib_email.py' to explicitly validate SMTP server and port settings before connecting, preventing crashes with 'please run connect() first'. - Added email fallback logic in 'app/methods/person_methods.py' to use 'user_email' or 'primary_email' if the primary contact email is missing. - Aligned 'app/config.py.default' with the production structure, explicitly re-adding 'SMTP' and 'FILES_PATH' dictionaries. - Added comprehensive unit tests in 'tests/test_email_configuration.py' to verify configuration handling.
This commit is contained in:
@@ -1,85 +1,71 @@
|
||||
# Configuration file for this FastAPI app.
|
||||
import os
|
||||
from pydantic import AnyHttpUrl, BaseSettings, EmailStr, HttpUrl, PostgresDsn, validator
|
||||
from pydantic import BaseSettings
|
||||
from typing import Any, Dict, List, Optional, Union
|
||||
|
||||
|
||||
# ### ### #
|
||||
|
||||
|
||||
class Settings(BaseSettings):
|
||||
AETHER_CFG = {}
|
||||
AETHER_CFG['id'] = 0
|
||||
# AETHER_CFG['api_id'] = 0 # NOT CURRENTLY NEED OR USED
|
||||
|
||||
JWT_KEY = '' # 22 characters; super secret Aether JWT signing key
|
||||
|
||||
# APP_NAME: str = "Aether API (FastAPI)"
|
||||
# SUPER_EMAIL: EmailStr = 'Aether.Super@oneskyit.com'
|
||||
|
||||
AETHER_CFG: Dict[str, Any] = {
|
||||
"id": os.getenv('AE_CFG_ID', '0')
|
||||
}
|
||||
|
||||
JWT_KEY: str = os.getenv('AE_API_JWT_KEY', 'fake-super-secret-token')
|
||||
|
||||
# Database Connection
|
||||
DB = {}
|
||||
DB['server'] = 'db.oneskyit.com'
|
||||
DB['port'] = '3306' # default = 3306
|
||||
DB['name'] = 'aether_default'
|
||||
DB['username'] = ''
|
||||
DB['password'] = ''
|
||||
SQLALCHEMY_DB_URI = 'mysql://'+DB['username']+':'+DB['password']+'@'+DB['server']+'/'+DB['name']
|
||||
DB_SERVER: str = os.getenv('AE_DB_SERVER', 'mariadb')
|
||||
DB_PORT: str = os.getenv('AE_DB_PORT', '3306')
|
||||
DB_NAME: str = os.getenv('AE_DB_NAME', 'aether_dev')
|
||||
DB_USER: str = os.getenv('AE_DB_USERNAME', 'aether_dev')
|
||||
DB_PASS: str = os.getenv('AE_DB_PASSWORD', '')
|
||||
|
||||
DB['wait_timeout'] = int(os.getenv('AE_DB_WAIT_TIMEOUT', 1800)) # default = 28800; Time (seconds) that the server waits for a connection to become active before closing it.
|
||||
DB['connect_timeout'] = int(os.getenv('AE_DB_CONNECTION_TIMEOUT', 20)) # default = 10; Time (seconds) that the server waits for a connection to become active before closing it.
|
||||
DB['pool_recycle'] = int(os.getenv('AE_DB_POOL_RECYCLE', 1800)) # default = ?; Related to SQLAlchemy
|
||||
@property
|
||||
def SQLALCHEMY_DB_URI(self) -> str:
|
||||
return f"mysql://{self.DB_USER}:{self.DB_PASS}@{self.DB_SERVER}:{self.DB_PORT}/{self.DB_NAME}"
|
||||
|
||||
@property
|
||||
def DB(self) -> Dict[str, Any]:
|
||||
return {
|
||||
"server": self.DB_SERVER,
|
||||
"port": self.DB_PORT,
|
||||
"name": self.DB_NAME,
|
||||
"username": self.DB_USER,
|
||||
"password": self.DB_PASS,
|
||||
"connect_timeout": int(os.getenv('AE_DB_CONNECTION_TIMEOUT', 20)),
|
||||
"pool_recycle": int(os.getenv('AE_DB_POOL_RECYCLE', 1800))
|
||||
}
|
||||
|
||||
# Aether API log files paths
|
||||
LOG_PATH = {}
|
||||
LOG_PATH['app'] = '/logs/aether_api.log' # 'admin/log/app.log', '../../logs/aether_api.log'
|
||||
# LOG_PATH['app_warning'] = '/logs/aether_api_warning.log' # 'admin/log/app_warning.log' '../../logs/aether_api_warning.log'
|
||||
|
||||
# Logging
|
||||
LOG_PATH: Dict[str, str] = {
|
||||
"app": os.getenv('AE_API_LOG_PATH', '/logs/aether_api.log')
|
||||
}
|
||||
|
||||
# Redis
|
||||
REDIS = {}
|
||||
REDIS['server'] = 'localhost' # 'localhost' 'redis'
|
||||
REDIS['port'] = '6379'
|
||||
|
||||
REDIS: Dict[str, str] = {
|
||||
"server": os.getenv('AE_REDIS_SERVER', 'redis'),
|
||||
"port": os.getenv('AE_REDIS_PORT', '6379')
|
||||
}
|
||||
|
||||
# --- CRITICAL CONFIGURATIONS ---
|
||||
# Send SMTP Email
|
||||
SMTP = {}
|
||||
# server
|
||||
# port
|
||||
# username
|
||||
# password
|
||||
|
||||
SMTP: Dict[str, str] = {
|
||||
"server": os.getenv('AE_SMTP_SERVER', ''),
|
||||
"port": os.getenv('AE_SMTP_PORT', '465'),
|
||||
"username": os.getenv('AE_SMTP_USERNAME', ''),
|
||||
"password": os.getenv('AE_SMTP_PASSWORD', '')
|
||||
}
|
||||
|
||||
# Server Hosted File Paths
|
||||
FILES_PATH = {}
|
||||
# hosted_files_root
|
||||
# hosted_tmp_root
|
||||
FILES_PATH: Dict[str, str] = {
|
||||
"hosted_files_root": os.getenv('AE_FILES_PATH_ROOT', '/srv/hosted_files'),
|
||||
"hosted_tmp_root": os.getenv('AE_FILES_PATH_TMP', '/srv/hosted_tmp')
|
||||
}
|
||||
# --- END CRITICAL CONFIGURATIONS ---
|
||||
|
||||
|
||||
# CORS Origins
|
||||
ORIGINS_REGEX = '(https://.*\.oneskyit\.com)|(http://.*\.oneskyit\.com:8181)|(https://.*\.oneskyit\.com:4443)|(https://.*\.oneskyit\.com:8443)'
|
||||
# A reasonable, but fairly open example regular expression for the CORS origins:
|
||||
# '(https://.*\.oneskyit\.com)|(http://.*\.oneskyit\.com)|(http://.*\.oneskyit\.com:8181)|(https://.*\.oneskyit\.com:8443)|(http://.*\.oneskyit\.local)|(http://.*\.oneskyit\.local:5000)|(http://.*.localhost)|(http://.*.localhost:5000)|(http://.*.localhost:8181)'
|
||||
|
||||
ORIGINS = [
|
||||
# CORS
|
||||
ORIGINS_REGEX: str = os.getenv('AE_API_ORIGINS_REGEX', '(https://.*\.oneskyit\.com)|(https://.*\.oneskyit\.com:4443)')
|
||||
ORIGINS: List[str] = [
|
||||
'https://oneskyit.com',
|
||||
# 'http://app-local.oneskyit.com',
|
||||
# 'http://192.168.32.20:3000',
|
||||
# 'http://192.168.32.20:8080',
|
||||
|
||||
# 'http://localhost',
|
||||
# 'http://localhost:3000',
|
||||
# 'http://localhost:5000',
|
||||
# 'http://localhost:8080',
|
||||
# 'http://localhost:7800',
|
||||
# 'http://localhost:8888',
|
||||
|
||||
'http://fastapi.localhost',
|
||||
|
||||
'http://svelte.oneskyit.local:5555',
|
||||
]
|
||||
|
||||
]
|
||||
|
||||
settings = Settings()
|
||||
|
||||
Reference in New Issue
Block a user