From a8ce60a47fa7d6dac4308a2d503907c830a3e1cc Mon Sep 17 00:00:00 2001 From: pengshiyu <1940607002@qq.com> Date: Fri, 22 Mar 2024 17:33:50 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=EF=BC=9A=E6=89=98=E7=AE=A1?= =?UTF-8?q?=E8=AF=81=E4=B9=A6=E5=88=B0=E6=9C=9F=E6=8F=90=E9=86=92=20#69?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- domain_admin/enums/event_enum.py | 2 ++ domain_admin/model/certificate_model.py | 12 ++++++- domain_admin/service/notify_service.py | 43 +++++++++++++++++++++++-- 3 files changed, 54 insertions(+), 3 deletions(-) diff --git a/domain_admin/enums/event_enum.py b/domain_admin/enums/event_enum.py index 5ac943f1ae..dc7747c92a 100644 --- a/domain_admin/enums/event_enum.py +++ b/domain_admin/enums/event_enum.py @@ -20,3 +20,5 @@ class EventEnum(object): # 监控异常 MONITOR_EXCEPTION = 2 + # 托管证书到期 + SSL_CERT_FILE_EXPIRE = 3 diff --git a/domain_admin/model/certificate_model.py b/domain_admin/model/certificate_model.py index 8c45a0dc1e..002e75aaeb 100644 --- a/domain_admin/model/certificate_model.py +++ b/domain_admin/model/certificate_model.py @@ -10,7 +10,7 @@ from playhouse.shortcuts import model_to_dict from domain_admin.model.base_model import BaseModel -from domain_admin.utils import datetime_util +from domain_admin.utils import datetime_util, time_util class CertificateModel(BaseModel): @@ -76,12 +76,22 @@ def expire_date(self): if self.expire_time and isinstance(self.expire_time, datetime): return self.expire_time.strftime('%Y-%m-%d') + @property + def real_time_expire_days(self): + """ + 实时ssl过期剩余天数 + expire_days 是更新数据时所计算的时间,有滞后性 + :return: int + """ + return time_util.get_diff_days(datetime.now(), self.expire_time) + def to_dict(self): data = model_to_dict( model=self, extra_attrs=[ 'create_time_label', 'update_time_label', + 'real_time_expire_days', 'start_date', 'expire_date', 'certificate_id', diff --git a/domain_admin/service/notify_service.py b/domain_admin/service/notify_service.py index b95ba453eb..d5acade46e 100644 --- a/domain_admin/service/notify_service.py +++ b/domain_admin/service/notify_service.py @@ -18,6 +18,7 @@ from domain_admin.enums.event_enum import EventEnum from domain_admin.enums.notify_type_enum import NotifyTypeEnum from domain_admin.log import logger +from domain_admin.model.certificate_model import CertificateModel from domain_admin.model.domain_info_model import DomainInfoModel from domain_admin.model.domain_model import DomainModel from domain_admin.model.group_user_model import GroupUserModel @@ -43,8 +44,12 @@ 'event_id': EventEnum.MONITOR_EXCEPTION, 'email_template': 'monitor-email.html', 'email_subject': '[Domain Admin]监控异常提醒', + }, + { + 'event_id': EventEnum.SSL_CERT_FILE_EXPIRE, + 'email_template': 'cert-email.html', + 'email_subject': '[Domain Admin]托管证书到期提醒', } - ] @@ -155,7 +160,8 @@ def notify_all_event(): NotifyModel.status == True, NotifyModel.event_id.in_([ EventEnum.SSL_CERT_EXPIRE, - EventEnum.DOMAIN_EXPIRE + EventEnum.DOMAIN_EXPIRE, + EventEnum.SSL_CERT_FILE_EXPIRE ]) ) @@ -197,6 +203,9 @@ def notify_user_about_some_event(notify_row): elif notify_row.event_id == EventEnum.DOMAIN_EXPIRE: # 域名过期 return notify_user_about_domain_expired(notify_row) + elif notify_row.event_id == EventEnum.SSL_CERT_FILE_EXPIRE: + # 托管证书到期 + return notify_user_about_cert_file_expired(notify_row) else: raise AppException("notify_row event_id not support: {}".format(notify_row.event_id)) @@ -269,6 +278,36 @@ def notify_user_about_cert_expired(notify_row): return notify_user(notify_row, lst) +def notify_user_about_cert_file_expired(notify_row): + """ + 托管证书到期 + :param notify_row: + :return: + """ + now = datetime.now() + + notify_expire_time = now + timedelta(days=notify_row.expire_days) + + # 注意null的情况 + query = CertificateModel.select() + + rows = query.where( + (CertificateModel.expire_time <= notify_expire_time) + | (CertificateModel.expire_time.is_null(True)) + ).order_by( + CertificateModel.expire_time.asc(), + CertificateModel.id.desc() + ) + + lst = [row.to_dict() for row in rows] + + for row in lst: + row['expire_days'] = row['real_time_expire_days'] + + if len(lst) > 0: + return notify_user(notify_row, lst) + + def notify_user_about_domain_expired(notify_row): """ 域名过期事件触发