Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

SSL support for SMTP notifications #80

Open
wants to merge 5 commits into from

2 participants

@gi-lunaweb

Hi,

This changeset adds SSL support to God's mail notifications.

It adds two options to God::Contact::Email :

  • enable_starttls_auto
  • openssl_verify_mode

These work the same as in Mail::SMTP, except openssl_verify_mode only accept and OpenSSL::SSL::VERIFY_* constant.

I don't add any test to test/configs/contact as you may prefer to use your own credentials.

@mojombo
Owner

It would be great if you could add a test to this that just uses mocks and stubs so you don't have to test real credentials.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 75 additions and 41 deletions.
  1. +26 −18 doc/god.asciidoc
  2. +49 −23 lib/god/contacts/email.rb
View
44 doc/god.asciidoc
@@ -1002,28 +1002,36 @@ end
```
```
-to_email - The String email address to which the email will be sent.
-to_name - The String name corresponding to the recipient.
-from_email - The String email address from which the email will be sent.
-from_name - The String name corresponding to the sender.
-delivery_method - The Symbol delivery method. [ :smtp | :sendmail ]
- (default: :smtp).
+to_email - The String email address to which the email will be sent.
+to_name - The String name corresponding to the recipient.
+from_email - The String email address from which the email will be sent.
+from_name - The String name corresponding to the sender.
+delivery_method - The Symbol delivery method. [ :smtp | :sendmail ]
+ (default: :smtp).
=== SMTP Options (when delivery_method = :smtp) ===
-server_host - The String hostname of the SMTP server (default: localhost).
-server_port - The Integer port of the SMTP server (default: 25).
-server_auth - The Boolean of whether or not to use authentication
- (default: false).
-
-=== SMTP Auth Options (when server_auth = true) ===
-server_domain - The String domain.
-server_user - The String username.
-server_password - The String password.
+server_host - The String hostname of the SMTP server (default: localhost).
+server_port - The Integer port of the SMTP server (default: 25).
+server_auth - The Symbol authentication method. Possible values:
+ [ nil | :plain | :login | :cram_md5 ]
+ The default is nil, which means no authentication. To
+ enable authentication, pass the appropriate symbol and
+ then pass the appropriate SMTP Auth Options (below).
+
+=== SMTP Auth Options (when server_auth != nil) ===
+server_domain - The String domain.
+server_user - The String username.
+server_password - The String password.
+enable_starttls_auto - Enables SMTP/TLS (STARTTLS) if server accepts
+ (default: false)
+openssl_verify_mode - OpenSSL verify mode used to validate certificates
+ if needed. Accept an OpenSSL verify mode constant like
+ OpenSSL::SSL::VERIFY_NONE (default: nil)
=== Sendmail Options (when delivery_method = :sendmail) ===
-sendmail_path - The String path to the sendmail executable
- (default: "/usr/sbin/sendmail").
-sendmail_args - The String args to send to sendmail (default "-i -t").
+sendmail_path - The String path to the sendmail executable
+ (default: "/usr/sbin/sendmail").
+sendmail_args - The String args to send to sendmail (default "-i -t").
```
Jabber
View
72 lib/god/contacts/email.rb
@@ -1,30 +1,35 @@
# Send a notice to an email address.
#
-# to_email - The String email address to which the email will be sent.
-# to_name - The String name corresponding to the recipient.
-# from_email - The String email address from which the email will be sent.
-# from_name - The String name corresponding to the sender.
-# delivery_method - The Symbol delivery method. [ :smtp | :sendmail ]
-# (default: :smtp).
+# to_email - The String email address to which the email will be sent.
+# to_name - The String name corresponding to the recipient.
+# from_email - The String email address from which the email will be sent.
+# from_name - The String name corresponding to the sender.
+# delivery_method - The Symbol delivery method. [ :smtp | :sendmail ]
+# (default: :smtp).
#
# === SMTP Options (when delivery_method = :smtp) ===
-# server_host - The String hostname of the SMTP server (default: localhost).
-# server_port - The Integer port of the SMTP server (default: 25).
-# server_auth - The Symbol authentication method. Possible values:
-# [ nil | :plain | :login | :cram_md5 ]
-# The default is nil, which means no authentication. To
-# enable authentication, pass the appropriate symbol and
-# then pass the appropriate SMTP Auth Options (below).
+# server_host - The String hostname of the SMTP server (default: localhost).
+# server_port - The Integer port of the SMTP server (default: 25).
+# server_auth - The Symbol authentication method. Possible values:
+# [ nil | :plain | :login | :cram_md5 ]
+# The default is nil, which means no authentication. To
+# enable authentication, pass the appropriate symbol and
+# then pass the appropriate SMTP Auth Options (below).
+# enable_starttls_auto - Enables SMTP/TLS (STARTTLS) if server accepts
+# (default: false)
+# openssl_verify_mode - OpenSSL verify mode used to validate certificates
+# if needed. Accept an OpenSSL verify mode constant like
+# OpenSSL::SSL::VERIFY_NONE (default: nil)
#
# === SMTP Auth Options (when server_auth != nil) ===
-# server_domain - The String domain.
-# server_user - The String username.
-# server_password - The String password.
+# server_domain - The String domain.
+# server_user - The String username.
+# server_password - The String password.
#
# === Sendmail Options (when delivery_method = :sendmail) ===
-# sendmail_path - The String path to the sendmail executable
-# (default: "/usr/sbin/sendmail").
-# sendmail_args - The String args to send to sendmail (default "-i -t").
+# sendmail_path - The String path to the sendmail executable
+# (default: "/usr/sbin/sendmail").
+# sendmail_args - The String args to send to sendmail (default "-i -t").
require 'time'
require 'net/smtp'
@@ -36,6 +41,7 @@ class Email < Contact
class << self
attr_accessor :to_email, :to_name, :from_email, :from_name,
:delivery_method, :server_host, :server_port,
+ :enable_starttls_auto, :openssl_verify_mode,
:server_auth, :server_domain, :server_user,
:server_password, :sendmail_path, :sendmail_args
attr_accessor :format
@@ -44,6 +50,8 @@ class << self
self.from_email = 'god@example.com'
self.from_name = 'God Process Monitoring'
self.delivery_method = :smtp
+ self.enable_starttls_auto = false
+ self.openssl_verify_mode = nil
self.server_auth = nil
self.server_host = 'localhost'
self.server_port = 25
@@ -67,6 +75,7 @@ class << self
attr_accessor :to_email, :to_name, :from_email, :from_name,
:delivery_method, :server_host, :server_port,
+ :enable_starttls_auto, :openssl_verify_mode,
:server_auth, :server_domain, :server_user,
:server_password, :sendmail_path, :sendmail_args
@@ -82,6 +91,9 @@ def valid?
valid &= complain("Attribute 'server_user' must be specified", self) unless arg(:server_user)
valid &= complain("Attribute 'server_password' must be specified", self) unless arg(:server_password)
end
+ if arg(:enable_starttls_auto)
+ valid &= complain("Attribute 'openssl_verify_mode' must be one of [ nil, OpenSSL::SSL::VERIFY_NONE, OpenSSL::SSL::VERIFY_PEER, OpenSSL::SSL::VERIFY_CLIENT_ONCE, OpenSSL::SSL::VERIFY_FAIL_IF_NO_PEER_CERT ]", self) unless [ nil, OpenSSL::SSL::VERIFY_NONE, OpenSSL::SSL::VERIFY_PEER, OpenSSL::SSL::VERIFY_CLIENT_ONCE, OpenSSL::SSL::VERIFY_FAIL_IF_NO_PEER_CERT ].include?(arg(:openssl_verify_mode))
+ end
end
valid
end
@@ -105,16 +117,30 @@ def notify(message, time, priority, category, host)
end
def notify_smtp(mail)
- args = [arg(:server_host), arg(:server_port)]
+ smtp = Net::SMTP.new(arg(:server_host), arg(:server_port))
+
+ args = []
if arg(:server_auth)
args << arg(:server_domain)
args << arg(:server_user)
args << arg(:server_password)
args << arg(:server_auth)
end
-
- Net::SMTP.start(*args) do |smtp|
- smtp.send_message(mail, arg(:from_email), arg(:to_email))
+
+ if arg(:enable_starttls_auto)
+ if smtp.respond_to?(:enable_starttls_auto)
+ unless arg(:openssl_verify_mode)
+ smtp.enable_starttls_auto
+ else
+ context = Net::SMTP.default_ssl_context
+ context.verify_mode = arg(:openssl_verify_mode)
+ smtp.enable_starttls_auto(context)
+ end
+ end
+ end
+
+ smtp.start(*args) do |smtp|
+ smtp.send_message(mail, arg(:from_email), [arg(:to_email)] )
end
end
Something went wrong with that request. Please try again.