From 7c4be18a6de926fa56fd308c8c3dca8f52927859 Mon Sep 17 00:00:00 2001 From: "kuldeep.k@cisinlabs.com" Date: Mon, 7 Feb 2022 20:58:53 +0530 Subject: [PATCH] Updated docker-compose, removed config.ini and unuseful code --- .gitignore | 129 +++++++++++++++++++++++++++++++++++++++++++++ README.md | 9 +++- config.ini | 9 ---- docker-compose.yml | 3 +- main.py | 99 ++++------------------------------ requirements.txt | 3 -- 6 files changed, 149 insertions(+), 103 deletions(-) create mode 100644 .gitignore delete mode 100644 config.ini diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b6e4761 --- /dev/null +++ b/.gitignore @@ -0,0 +1,129 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +pip-wheel-metadata/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +.python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ diff --git a/README.md b/README.md index 91d92b2..477fc09 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,10 @@ # influx-smtp-gateway -SMTP gateway accessible from InfluxDB for sending alerts. \ No newline at end of file +SMTP gateway accessible from InfluxDB for sending alerts. + +# create .env file with following parameters +server_host = 0.0.0.0 +server_port = 8081 +to_mail = test111@mailinator.com +from_mail = test@gmail.com +from_mail_password = test@123 \ No newline at end of file diff --git a/config.ini b/config.ini deleted file mode 100644 index d0d3fc1..0000000 --- a/config.ini +++ /dev/null @@ -1,9 +0,0 @@ -[server] -server_host = 0.0.0.0 -server_port = 8081 - -[app] -to_mail = test111@mailinator.com -from_mail = test@gmail.com -from_mail_password = test@123 - diff --git a/docker-compose.yml b/docker-compose.yml index 631e8a5..97c700b 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -3,8 +3,7 @@ version: '3.8' services: web: build: . - command: python main.py ports: - 8081:8081 env_file: - - ./config.ini + - ./.env diff --git a/main.py b/main.py index 009898e..8271238 100644 --- a/main.py +++ b/main.py @@ -12,98 +12,14 @@ import sys from email.header import Header from email.mime.text import MIMEText -import uuid as uuid_module -from ipaddress import AddressValueError, IPv4Address, IPv6Address - import cherrypy -PATH = os.path.dirname(os.path.abspath(__file__)) -CONFIG = configparser.ConfigParser() -CONFIG.read(os.path.join(PATH, "config.ini")) - -TO_MAIL = CONFIG["app"].get("to_mail") -FROM_MAIL = CONFIG["app"].get("from_mail") -FROM_MAIL_PASSWORD = CONFIG["app"].get("from_mail_password") -print("TO_MAIL: ", TO_MAIL) -print("FROM_MAIL: ", FROM_MAIL) -print("FROM_MAIL_PASSWORD: ", FROM_MAIL_PASSWORD) - - -class CloudInitRequest: - """ - Request data for persistence across methods - """ - def __init__(self, request, uuid=None): - self.remoteip = None - self.hostinfo = ('localhost', ) - self.request = request - self.meta_data = None - self.meta_data_loaded = False - self.user_data = None - - try: - self.uuid = str(uuid_module.UUID('{' + uuid + '}')) - # ValueError is wrong UUID syntax - # TypeError is None - except (ValueError, TypeError): - self.uuid = None - - self._init_ip() - - def _can_ip_be_proxy(self): - """ - Assuming the connection is through a proxy, is this proxy permitted? - Can't proxy from a publicly reachable IP. - """ - self.remoteip = self.request.remote.ip - try: - ipobj = IPv4Address(self.remoteip) - except AddressValueError: - try: - ipobj = IPv6Address(self.remoteip) - except AddressValueError: - return False - return not ipobj.is_global - - def _init_ip(self): - """ - Get remote IP - """ - if self._can_ip_be_proxy(): - try: - self.remoteip = self.request.headers.get( - 'X-Real-Ip', - self.request.remote.ip - ) - except KeyError: - pass - - try: - self.hostinfo = socket.gethostbyaddr(self.remoteip) - forward_lookup = socket.gethostbyname(self.hostinfo[0]) - if forward_lookup != self.remoteip: - self.hostinfo = ('localhost', ) - except socket.herror: - self.hostinfo = ('localhost', ) - except socket.gaierror: - self.hostinfo = (self.remoteip, ) - class CloudInitApp: """ Serve cloud init files """ - @staticmethod - def _content_type(data): - if not data: - return "text/cloud-config" - if data.startswith("#include"): - return "text/x-include-url" - if data.startswith("## template: jinja"): - return "text/jinja2" - return "text/cloud-config" - def _send_mail(self): try: # pylint: disable=deprecated-lambda @@ -139,10 +55,17 @@ class CloudInitApp: ROOT = CloudInitApp() if __name__ == "__main__": - cherrypy.server.socket_host = \ - CONFIG["server"].get("server_host", "127.0.0.1") - cherrypy.server.socket_port = \ - CONFIG["server"].getint("server_port", 8081) + try: + SERVER_HOST = os.environ["server_host"] + SERVER_PORT = int(os.environ["server_port"]) + TO_MAIL = os.environ["to_mail"] + FROM_MAIL = os.environ["from_mail"] + FROM_MAIL_PASSWORD = os.environ["from_mail_password"] + except: + raise "Please check missing environment variables: to_mail, from_mail, from_mail_password" + + cherrypy.server.socket_host = SERVER_HOST + cherrypy.server.socket_port = SERVER_PORT ENGINE = cherrypy.engine CURRENT_DIR = os.path.dirname(os.path.abspath(__file__)) diff --git a/requirements.txt b/requirements.txt index ac9d4e8..d6e76e2 100644 --- a/requirements.txt +++ b/requirements.txt @@ -6,12 +6,9 @@ jaraco.collections==3.5.1 jaraco.context==4.1.1 jaraco.functools==3.5.0 jaraco.text==3.7.0 -Jinja2==3.0.3 -MarkupSafe==2.0.1 more-itertools==8.12.0 portend==3.1.0 pytz==2021.3 -PyYAML==6.0 six==1.16.0 tempora==5.0.1 zc.lockfile==2.0