-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add EmailStore plugin #55
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
"""Email alert plugin.""" | ||
|
||
|
||
import email | ||
import logging | ||
import smtplib | ||
from email.mime.multipart import MIMEMultipart | ||
from email.mime.text import MIMEText | ||
|
||
# Define module-level logger. | ||
_log = logging.getLogger(__name__) | ||
|
||
|
||
class EmailAlert: | ||
"""A plugin to send email notification for anomalies found.""" | ||
|
||
def __init__(self, host, port, subject, to, sender, body): | ||
"""Create an instance of :class:`EmailAlert` plugin. | ||
|
||
Arguments: | ||
host (string): Server hostname for SMTP. | ||
port (int): Server port for SMTP. | ||
subject (string): Subject line for email. | ||
to (list): List of recipients (string). | ||
sender (string): From field for email. | ||
body (string): Email body. | ||
|
||
""" | ||
self.host = host | ||
self.port = port | ||
self.subject = subject | ||
self.to = to | ||
self.sender = sender | ||
self.body = body | ||
self.stringbuffer = [] | ||
|
||
def write(self, record): | ||
"""Write JSON records to the file system. | ||
|
||
This method is called once for every ``record`` read from a | ||
cloud. In this example implementation of a alert, we simply | ||
send the ``record`` in JSON format via email to the recipient. | ||
The records keeps appending to a stringbuffer which then is | ||
written in the email body | ||
|
||
Arguments: | ||
record (dict): Data to send via email. | ||
""" | ||
for _, value in record.items(): | ||
self.stringbuffer.append(repr(value)) | ||
|
||
def done(self): | ||
"""Perform final cleanup tasks. | ||
|
||
This method is called after all records have been written. In | ||
this example implementation, we properly terminate the JSON | ||
array in the email body. | ||
|
||
""" | ||
message = MIMEMultipart() | ||
message['Date'] = email.utils.formatdate(localtime=True) | ||
message['Subject'] = self.subject | ||
message['From'] = self.sender | ||
message['To'] = email.utils.COMMASPACE.join(self.to) | ||
|
||
# Incase if the buffer is empty then the default message will be sent | ||
if self.stringbuffer: | ||
self.body = ''.join(self.stringbuffer) | ||
message.attach(MIMEText(self.body)) | ||
|
||
smtp_connection = smtplib.SMTP(host=self.host, port=self.port) | ||
try: | ||
smtp_connection.sendmail(self.sender, self.to, message.as_string()) | ||
except smtplib.SMTPException as e: | ||
_log.error('Failed to send email: %s', e) | ||
finally: | ||
smtp_connection.quit() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,14 @@ cloudmarker.stores package | |
Submodules | ||
---------- | ||
|
||
cloudmarker.stores.emailalert module | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this needs to be corrected. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, there is issue #72 open to track this. |
||
------------------------------------ | ||
|
||
.. automodule:: cloudmarker.stores.emailalert | ||
:members: | ||
:undoc-members: | ||
:show-inheritance: | ||
|
||
cloudmarker.stores.filestore module | ||
----------------------------------- | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line should be removed from
config.base.yaml
. Any enabled audit config inconfig.base.yaml
should run smoothly without any errors right after a user or developer clones this project's GitHub repository. In other words, the following should run fine without errors without requiring any additional steps:Or equivalently, this should run without errors:
Since there is no way to configure the
EmailAlert
such that it works out of the box right after cloning the repository, we should not enableEmailAlert
by default. The sampleEmailAlert
configuration at line 20 can still be there. Only its usage at line 41 here should be removed.