IPA-EPN: add smtp_delay to limit the velocity of e-mails sent

Provide a knob so the mail queue doesn't get completely flooded
with new e-mails.

Default to no wait, value in milliseconds.

https://pagure.io/freeipa/issue/3687
Signed-off-by: Rob Crittenden <rcritten@redhat.com>
Reviewed-By: Francois Cami <fcami@redhat.com>
This commit is contained in:
Rob Crittenden 2020-05-21 12:59:02 -04:00
parent 759ab3120e
commit 3b266d3957
2 changed files with 14 additions and 0 deletions

View File

@ -70,6 +70,9 @@ Specifies the type of secure connection to make. Options are: none, starttls and
Specifies the From e-mail address value in the e-mails sent. The default is
root@localhost. Bounces will be sent here.
.TP
.B smtp_delay <milliseconds>
Time to wait, in milliseconds, between each e-mail sent to try to avoid overloading the mail queue.
.TP
.B mail_from <address>
Specifies the From: e-mal address value in the e-mails sent. The default is
noreply@ipadefaultemaildomain. This value can be found by running

View File

@ -29,6 +29,7 @@ import os
import pwd
import logging
import smtplib
import time
from collections import deque
from datetime import datetime, timedelta
@ -57,6 +58,7 @@ EPN_CONFIG = {
"smtp_timeout": 60,
"smtp_security": "none",
"smtp_admin": "root@localhost",
"smtp_delay": None,
"mail_from": None,
"notify_ttls": "28,14,7,3,1",
"msg_charset": "utf8",
@ -366,6 +368,13 @@ class EPN(admintool.AdminTool):
except ValueError as e:
raise RuntimeError('Failed to parse notify_ttls: \'%s\': %s' %
(api.env.notify_ttls, e))
if api.env.smtp_delay:
try:
float(api.env.smtp_delay)
except ValueError as e:
raise RuntimeError('smtp_delay is misformatted: %s' % e)
if float(api.env.smtp_delay) < 0:
raise RuntimeError('smtp_delay cannot be less than zero')
def _parse_configuration(self):
"""
@ -511,6 +520,8 @@ class EPN(admintool.AdminTool):
"Notified %s (%s). Password expiring in %d days at %s.",
entry["mail"], entry["uid"], (expdate - now).days,
expdate)
if api.env.smtp_delay:
time.sleep(float(api.env.smtp_delay) / 1000)
self._mailer.cleanup()
def _gentestdata(self):