Add support for authentication/STARTTLS to SMTP

Add support for user/password based authentication to the SMTP driver of
Zuul. It is now possible to use "user" and "password" as options for the
smtp driver plus "use_starttls" to encrypt the SMTP conenction.

In addition, this change also changes the way error handling is done in
the SMTP driver: A failed send attempt is now immediately reported in
the log as a warning and no error string is returned anymore since it
wasn't used on the call site.

Change-Id: I456f7a2b3e21cf72f68820064ab6acc72a369ba1
This commit is contained in:
Markus Hosch 2018-09-19 14:55:16 +02:00
parent f7f8ea617a
commit c8d8d95a1d
2 changed files with 30 additions and 3 deletions

View File

@ -40,6 +40,22 @@ Connection Configuration
Who the report should be emailed to by default.
This can be overridden by individual pipelines.
.. attr:: user
Optional user name used to authenticate to the SMTP server. Used only in
conjunction with a password. If no password is present, this option is
ignored.
.. attr:: password
Optional password used to authenticate to the SMTP server.
.. attr:: use_starttls
:default: false
Issue a STARTTLS request to establish an encrypted channel after having
connected to the SMTP server.
Reporter Configuration
----------------------

View File

@ -36,6 +36,13 @@ class SMTPConnection(BaseConnection):
'default_from', 'zuul')
self.smtp_default_to = self.connection_config.get(
'default_to', 'zuul')
self.smtp_user = self.connection_config.get('user')
self.smtp_pass = self.connection_config.get('password')
starttls = self.connection_config.get('use_starttls', 'false')
if starttls.lower() == 'false':
self.smtp_starttls = False
else:
self.smtp_starttls = True
def sendMail(self, subject, message, from_email=None, to_email=None):
# Create a text/plain email message
@ -50,11 +57,15 @@ class SMTPConnection(BaseConnection):
try:
s = smtplib.SMTP(self.smtp_server, self.smtp_port)
if self.smtp_starttls:
s.starttls()
s.ehlo()
if self.smtp_user is not None and self.smtp_pass is not None:
s.login(self.smtp_user, self.smtp_pass)
s.sendmail(from_email, to_email.split(','), msg.as_string())
s.quit()
except Exception:
return "Could not send email via SMTP"
return
except Exception as e:
self.log.warning("Error sending mail via SMTP: %s", e)
def getSchema():