Skip to content

Commit

Permalink
Email TLS fixes (#416)
Browse files Browse the repository at this point in the history
* cast GHC_SMTP env vars as booleans in Docker setup

* try to send email anyway even if SMTP AUTH fails

* cast None properly from Docker startup
  • Loading branch information
tomkralidis committed Jan 5, 2022
1 parent 97c6eef commit 32a571f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 23 deletions.
39 changes: 20 additions & 19 deletions GeoHealthCheck/notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,7 @@ def do_email(config, resource, run, status_changed, result):
result, resource.title)

if not config.get('GHC_SMTP') or not\
(any([config['GHC_SMTP'][k] for k in ('port',
'server',
'username',
'password',)])):
(any([config['GHC_SMTP'][k] for k in ('port', 'server',)])):

LOGGER.warning("No SMTP configuration. Not sending to %s",
notifications_email)
Expand All @@ -95,22 +92,26 @@ def do_email(config, resource, run, status_changed, result):
if config['DEBUG']:
server.set_debuglevel(True)

try:
if config['GHC_SMTP']['tls']:
if config['GHC_SMTP']['tls']:
LOGGER.debug('Authenticating via TLS')
try:
server.starttls()
except Exception as err:
LOGGER.exception("Cannot connect to smtp: %s[:%s]: %s",
config['GHC_SMTP']['server'],
config['GHC_SMTP']['port'],
err,
exc_info=err)
return
try:
server.login(config['GHC_SMTP']['username'],
config['GHC_SMTP']['password'])
except Exception as err:
LOGGER.exception("Cannot log in to smtp: %s", err,
exc_info=err)
except Exception as err:
LOGGER.exception("Cannot authenticate to SMTP: %s:%s: %s",
config['GHC_SMTP']['server'],
config['GHC_SMTP']['port'],
err,
exc_info=err)
return

if None not in [
config['GHC_SMTP'].get('username'), config['GHC_SMTP'].get('password')]:
try:
server.login(config['GHC_SMTP']['username'],
config['GHC_SMTP']['password'])
except Exception as err:
LOGGER.exception("Cannot log in to SMTP: %s", err, exc_info=err)

try:
server.sendmail(config['GHC_ADMIN_EMAIL'],
notifications_email,
Expand Down
12 changes: 8 additions & 4 deletions docker/config_site.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ def str2bool(v):
return v.lower() in ("yes", "true", "t", "1")


def str2None(v):
return None if v == 'None' else v


DEBUG = False
SQLALCHEMY_ECHO = False

Expand Down Expand Up @@ -76,10 +80,10 @@ def str2bool(v):
GHC_SMTP = {
'server': os.environ['GHC_SMTP_SERVER'],
'port': os.environ['GHC_SMTP_PORT'],
'tls': os.environ['GHC_SMTP_TLS'],
'ssl': os.environ['GHC_SMTP_SSL'],
'username': os.environ['GHC_SMTP_USERNAME'],
'password': os.environ['GHC_SMTP_PASSWORD']
'tls': str2bool(os.environ['GHC_SMTP_TLS']),
'ssl': str2bool(os.environ['GHC_SMTP_SSL']),
'username': str2None(os.environ.get('GHC_SMTP_USERNAME')),
'password': str2None(os.environ.get('GHC_SMTP_PASSWORD'))
}

# TODO: provide for GHC Plugins
Expand Down

0 comments on commit 32a571f

Please sign in to comment.