This is an automated email from the git hooks/post-receive script.
praiskup pushed a commit to branch master
in repository copr/copr.
The following commit(s) were added to refs/heads/master by this push:
new 51e452a frontend: copr-fe-dev shouldn't send emails to everybody
51e452a is described below
commit 51e452acaba2859e14165f7d77131ef29da52329
Author: Silvie Chlupova <sisi.chlupova(a)gmail.com>
AuthorDate: Wed Aug 28 13:19:03 2019 +0200
frontend: copr-fe-dev shouldn't send emails to everybody
Fixes: #793
Merges: #959
---
.../commands/notify_outdated_chroots.py | 2 +-
frontend/coprs_frontend/coprs/config.py | 2 ++
frontend/coprs_frontend/coprs/mail.py | 28 ++++++++++++++++------
.../coprs/views/apiv3_ns/apiv3_permissions.py | 4 ++--
.../coprs/views/coprs_ns/coprs_general.py | 2 +-
.../tests/test_apiv3/test_projects.py | 4 ++--
frontend/coprs_frontend/tests/test_mail.py | 15 +++++++++++-
7 files changed, 43 insertions(+), 14 deletions(-)
diff --git a/frontend/coprs_frontend/commands/notify_outdated_chroots.py b/frontend/coprs_frontend/commands/notify_outdated_chroots.py
index 30d37d2..5d92ab5 100644
--- a/frontend/coprs_frontend/commands/notify_outdated_chroots.py
+++ b/frontend/coprs_frontend/commands/notify_outdated_chroots.py
@@ -74,7 +74,7 @@ class NotifyOutdatedChrootsCommand(Command):
class Notifier(object):
def notify(self, user, chroots):
msg = OutdatedChrootMessage(chroots)
- send_mail(user.mail, msg)
+ send_mail([user.mail], msg)
def store_timestamp(self, chroots):
for chroot in chroots:
diff --git a/frontend/coprs_frontend/coprs/config.py b/frontend/coprs_frontend/coprs/config.py
index 3e56df4..fb3059f 100644
--- a/frontend/coprs_frontend/coprs/config.py
+++ b/frontend/coprs_frontend/coprs/config.py
@@ -93,6 +93,8 @@ class Config(object):
ENABLE_DISCUSSION = False
DISCOURSE_URL = ''
+ WHITELIST_EMAILS = []
+
class ProductionConfig(Config):
DEBUG = False
# SECRET_KEY = "put_some_secret_here"
diff --git a/frontend/coprs_frontend/coprs/mail.py b/frontend/coprs_frontend/coprs/mail.py
index c310dec..2d7662a 100644
--- a/frontend/coprs_frontend/coprs/mail.py
+++ b/frontend/coprs_frontend/coprs/mail.py
@@ -1,6 +1,6 @@
import flask
import platform
-import smtplib
+from smtplib import SMTP
from email.mime.text import MIMEText
from coprs import app, helpers
@@ -114,9 +114,21 @@ class OutdatedChrootMessage(Message):
"{3}\n\n".format(chroot.copr.full_name, chroot.name, chroot.delete_after_days, url))
-def send_mail(recipient, message, sender=None, reply_to=None):
+def filter_whitelisted_recipients(recipients):
"""
- :param str/list recipient: One recipient email as a string or multiple emails in a list
+ Filters e-mail recipients if the white list of recipients in conf is not empty.
+ :param recipients: list of recipients
+ :return: list of recipients who should receive an e-mail
+ """
+ if not app.config["WHITELIST_EMAILS"]:
+ return recipients
+
+ return [r for r in recipients if r in app.config["WHITELIST_EMAILS"]]
+
+
+def send_mail(recipients, message, sender=None, reply_to=None):
+ """
+ :param list recipients: List of email recipients
:param Message message:
:param str sender: Email of a sender
:return:
@@ -124,8 +136,10 @@ def send_mail(recipient, message, sender=None, reply_to=None):
msg = MIMEText(message.text)
msg["Subject"] = message.subject
msg["From"] = sender or "root@{0}".format(platform.node())
- msg["To"] = ", ".join(recipient) if type(recipient) == list else recipient
+ msg["To"] = ", ".join(recipients)
msg.add_header("reply-to", reply_to or app.config["REPLY_TO"])
- s = smtplib.SMTP("localhost")
- s.sendmail("root@{0}".format(platform.node()), recipient, msg.as_string())
- s.quit()
+ recipients = filter_whitelisted_recipients(recipients)
+ if not recipients:
+ return
+ with SMTP("localhost") as smtp:
+ smtp.sendmail("root@{0}".format(platform.node()), recipients, msg.as_string())
diff --git a/frontend/coprs_frontend/coprs/views/apiv3_ns/apiv3_permissions.py b/frontend/coprs_frontend/coprs/views/apiv3_ns/apiv3_permissions.py
index dd7e541..15c81fd 100644
--- a/frontend/coprs_frontend/coprs/views/apiv3_ns/apiv3_permissions.py
+++ b/frontend/coprs_frontend/coprs/views/apiv3_ns/apiv3_permissions.py
@@ -67,7 +67,7 @@ def set_permissions(copr):
# send emails only if transaction succeeded
for task in messages:
if flask.current_app.config.get("SEND_EMAILS", False):
- send_mail(task['address'], task['message'])
+ send_mail([task['address']], task['message'])
return flask.jsonify({'updated': list(updated.keys())})
@@ -96,6 +96,6 @@ def request_permissions(ownername, projectname):
msg = PermissionRequestMessage(copr, flask.g.user, permission_dict)
for address in copr.admin_mails:
if flask.current_app.config.get("SEND_EMAILS", False):
- send_mail(address, msg)
+ send_mail([address], msg)
return flask.jsonify({'updated': bool(permission_dict)})
diff --git a/frontend/coprs_frontend/coprs/views/coprs_ns/coprs_general.py b/frontend/coprs_frontend/coprs/views/coprs_ns/coprs_general.py
index 9bb39cd..33517da 100644
--- a/frontend/coprs_frontend/coprs/views/coprs_ns/coprs_general.py
+++ b/frontend/coprs_frontend/coprs/views/coprs_ns/coprs_general.py
@@ -522,7 +522,7 @@ def copr_permissions_applier_change(copr):
permission_dict = {"old_builder": old_builder, "old_admin": old_admin,
"new_builder": new_builder, "new_admin": new_admin}
msg = PermissionRequestMessage(copr, flask.g.user, permission_dict)
- send_mail(mail, msg, )
+ send_mail([mail], msg)
return flask.redirect(helpers.copr_url("coprs_ns.copr_detail", copr))
diff --git a/frontend/coprs_frontend/tests/test_apiv3/test_projects.py b/frontend/coprs_frontend/tests/test_apiv3/test_projects.py
index a5d83e8..564dd61 100644
--- a/frontend/coprs_frontend/tests/test_apiv3/test_projects.py
+++ b/frontend/coprs_frontend/tests/test_apiv3/test_projects.py
@@ -213,8 +213,8 @@ class TestApiV3Permissions(CoprsTestCase):
emails = ['user1(a)spam.foo', 'user2(a)spam.foo']
calls = send_mail.call_args_list
assert len(calls) == 2
- assert calls[0][0][0] == "user2(a)spam.foo"
- assert calls[1][0][0] == "user1(a)foo.bar"
+ assert calls[0][0][0] == ["user2(a)spam.foo"]
+ assert calls[1][0][0] == ["user1(a)foo.bar"]
assert str(calls[0][0][1]) == msg
assert str(calls[1][0][1]) == msg
diff --git a/frontend/coprs_frontend/tests/test_mail.py b/frontend/coprs_frontend/tests/test_mail.py
index 519a7e0..2a364ef 100644
--- a/frontend/coprs_frontend/tests/test_mail.py
+++ b/frontend/coprs_frontend/tests/test_mail.py
@@ -1,6 +1,6 @@
import pytest
import datetime
-from coprs.mail import PermissionRequestMessage, PermissionChangeMessage, LegalFlagMessage, OutdatedChrootMessage
+from coprs.mail import PermissionRequestMessage, PermissionChangeMessage, LegalFlagMessage, OutdatedChrootMessage, filter_whitelisted_recipients
from tests.coprs_test_case import CoprsTestCase
from coprs import app
@@ -84,3 +84,16 @@ class TestMail(CoprsTestCase):
with pytest.raises(AttributeError) as ex:
OutdatedChrootMessage(copr_chroots=[])
assert "No outdated chroots" in str(ex)
+
+ def test_filter_recipients(self):
+ app.config["WHITELIST_EMAILS"] = ["test(a)redhat.com"]
+ recipient = filter_whitelisted_recipients(["test(a)redhat.com", "user(a)redhat.com"])
+ assert recipient == ["test(a)redhat.com"]
+
+ app.config["WHITELIST_EMAILS"] = ["test(a)redhat.com", "user(a)redhat.com"]
+ recipient = filter_whitelisted_recipients(["test(a)redhat.com", "user(a)redhat.com"])
+ assert recipient == ["test(a)redhat.com", "user(a)redhat.com"]
+
+ app.config["WHITELIST_EMAILS"] = []
+ recipient = filter_whitelisted_recipients(["test(a)redhat.com", "user(a)redhat.com"])
+ assert recipient == ["test(a)redhat.com", "user(a)redhat.com"]
--
To stop receiving notification emails like this one, please contact
the administrator of this repository.