Skip to content

Commit

Permalink
新增:托管证书到期提醒 #69
Browse files Browse the repository at this point in the history
  • Loading branch information
mouday committed Mar 22, 2024
1 parent 4154728 commit a8ce60a
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
2 changes: 2 additions & 0 deletions domain_admin/enums/event_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@ class EventEnum(object):
# 监控异常
MONITOR_EXCEPTION = 2

# 托管证书到期
SSL_CERT_FILE_EXPIRE = 3
12 changes: 11 additions & 1 deletion domain_admin/model/certificate_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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',
Expand Down
43 changes: 41 additions & 2 deletions domain_admin/service/notify_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]托管证书到期提醒',
}

]


Expand Down Expand Up @@ -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
])
)

Expand Down Expand Up @@ -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))

Expand Down Expand Up @@ -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):
"""
域名过期事件触发
Expand Down

0 comments on commit a8ce60a

Please sign in to comment.