Adding max notification constraint. (#704)

* Adds additional constraints to the max notification time. With an increasing number of certificates we need to limit the max notification time to reduce the number of certificates that need to be analyzed for notification eligibility.
This commit is contained in:
kevgliss
2017-03-03 12:59:16 -08:00
committed by GitHub
parent 5f5583e2cb
commit d53f64890c
6 changed files with 110 additions and 10 deletions

View File

@ -11,12 +11,13 @@
from itertools import groupby
from collections import defaultdict
from sqlalchemy.orm import joinedload
import arrow
from datetime import timedelta
from flask import current_app
from lemur import database, metrics
from lemur.common.utils import windowed_query
from lemur.certificates.schemas import certificate_notification_output_schema
from lemur.certificates.models import Certificate
@ -29,11 +30,21 @@ def get_certificates():
Finds all certificates that are eligible for notifications.
:return:
"""
return database.session_query(Certificate)\
.options(joinedload('notifications'))\
.filter(Certificate.notify == True)\
.filter(Certificate.expired == False)\
.filter(Certificate.notifications.any()).all() # noqa
now = arrow.utcnow()
max = now + timedelta(days=90)
q = database.db.session.query(Certificate) \
.filter(Certificate.not_after <= max) \
.filter(Certificate.notify == True) \
.filter(Certificate.expired == False) # noqa
certs = []
for c in windowed_query(q, Certificate.id, 100):
if needs_notification(c):
certs.append(c)
return certs
def get_eligible_certificates():
@ -151,6 +162,9 @@ def needs_notification(certificate):
days = (certificate.not_after - now).days
for notification in certificate.notifications:
if not notification.options:
return
interval = get_plugin_option('interval', notification.options)
unit = get_plugin_option('unit', notification.options)

View File

@ -21,6 +21,7 @@ def create_default_expiration_notifications(name, recipients):
already exist these will be returned instead of new notifications.
:param name:
:param recipients:
:return:
"""
if not recipients: