跳转至

SSL证书检查

Example

可以在此处找到完整的代码

手动管理 SSL 证书的到期日期可能是一项具有挑战性且容易出错的任务, 尤其是在需要检查大量域名的环境中。

未能及时更新 SSL 证书可能会导致服务意外中断以及安全漏洞。 为了解决此问题,可以使用 Plombery 来自动监控 SSL 证书的到期日期 ,并在证书即将到期时收到通知。

检查 SSL 证书的管道

检查 SSL 证书的管道

带有日志的运行状态页面

带有日志的运行状态页面

你也可以通过手动运行的方式动态的检查 SSL 证书:

手动运行

手动运行

如何实现

定义一个包含待检查域名的列表, 并单独为每个域名创建一个触发器, 这样一旦运行失败, 你就能快速定位至故障的域名:

EXPIRATION_WARNING_THRESHOLD = 30
hostnames = [
    "google.com",



register_pipeline(
    id="check_ssl_certificate",
    name="检查 SSL 证书",
    description="""检查网站的 SSL 证书是否已过期""",
    tasks=[check_certificate_expiration],
    triggers=[
        # 为每个主机单独创建一个触发器来运行检查
        Trigger(
            id=f"check-{host}",
            name=host,
            description="每周运行管道",
            params=InputParams(hostname=host),
            schedule=IntervalTrigger(
                weeks=1,
            ),
        )
        for host in hostnames
    ],

该示例中的管道只有一个任务, 但你可以将额外检查 SSL 证书做为附加的任务:

@task
async def check_certificate_expiration(params: InputParams):
    logger = get_logger()
    now = datetime.now()

    info = get_certificate_info(params.hostname)
    expiration: datetime = info.get("notAfter")

    if expiration <= now:
        raise Exception(f"The certificate expired on {expiration}")

    expires_in = expiration - now

    if expires_in.days < EXPIRATION_WARNING_THRESHOLD:
        raise Exception(f"Attention, the certificate expires in {expires_in.days} days")

    logger.info(