验收基线:保存无停机续期候选及真实挑战受阻证据
This commit is contained in:
1 parent
45078da64a
commit
9e2d1fe40f
9 files changed
+430
-4
No files matched your search
@@ -0,0 +1,5 @@
|
||||
# 仅在五个证书域名的公网 80 验证路径全部通过真实挑战后启用。
|
||||
# 正式文件为 /etc/smail/acme-renew.env,root 所有,权限 0600。
|
||||
# HTTP 模式复用 Nginx 的 /var/lib/smail-acme,不监听新端口,不停止服务。
|
||||
# 使用 DNS 模式时设为 dns,并在 acme-dns.env 中配置专用服务商凭据。
|
||||
SMAIL_ACME_CHALLENGE=http
|
||||
@@ -1,9 +1,10 @@
|
||||
#!/usr/bin/python3
|
||||
"""使用 DNS 验证续期,不停止 Nginx,候选证书通过校验后才发布。"""
|
||||
'''使用显式 DNS 或 Webroot 验证续期,不停止 Nginx,校验通过后才发布。'''
|
||||
|
||||
import os
|
||||
from pathlib import Path
|
||||
import re
|
||||
import stat
|
||||
import subprocess
|
||||
|
||||
import deploy_certificate as deploy
|
||||
@@ -12,6 +13,7 @@ import deploy_certificate as deploy
|
||||
ROOT = Path('/var/lib/smail-acme-tls')
|
||||
DOMAINS = ('solit.cn', 'mail.solit.cn', 'smtp.solit.cn', 'imap.solit.cn', 'pop.solit.cn')
|
||||
RENEW_TIMEOUT = 600
|
||||
WEBROOT = Path('/var/lib/smail-acme')
|
||||
|
||||
|
||||
def dns_provider():
|
||||
@@ -21,6 +23,25 @@ def dns_provider():
|
||||
return provider
|
||||
|
||||
|
||||
def challenge_arguments():
|
||||
method = os.environ.get('SMAIL_ACME_CHALLENGE', 'dns')
|
||||
if method == 'dns':
|
||||
return ['--dns', dns_provider()]
|
||||
if method != 'http':
|
||||
raise RuntimeError('验证方式必须为 dns 或 http,不允许自动回退到停机验证')
|
||||
private = ROOT.lstat()
|
||||
if (ROOT.resolve(strict=True) != ROOT or not stat.S_ISDIR(private.st_mode)
|
||||
or private.st_uid != 0 or private.st_mode & 0o077):
|
||||
raise RuntimeError('ACME 账号目录必须由 root 独占,禁止公开访问')
|
||||
for directory in (WEBROOT, WEBROOT / '.well-known', WEBROOT / '.well-known/acme-challenge'):
|
||||
info = directory.lstat()
|
||||
if (directory.resolve(strict=True) != directory or not stat.S_ISDIR(info.st_mode)
|
||||
or info.st_uid != 0 or info.st_mode & 0o022
|
||||
or info.st_mode & 0o005 != 0o005):
|
||||
raise RuntimeError('HTTP 验证目录必须由 root 管理、可读取且不能被其他用户写入')
|
||||
return ['--http', '--http.webroot', str(WEBROOT)]
|
||||
|
||||
|
||||
def main():
|
||||
if os.geteuid() != 0:
|
||||
raise RuntimeError('必须使用 root 执行证书续期')
|
||||
@@ -33,19 +54,20 @@ def main():
|
||||
raise RuntimeError('无法判断线上证书有效期,拒绝续期')
|
||||
due = result.returncode == 1
|
||||
if due:
|
||||
provider = dns_provider()
|
||||
challenge = challenge_arguments()
|
||||
deploy.check_nginx_paths()
|
||||
subprocess.run(['nginx', '-t'], check=True)
|
||||
subprocess.run(['systemctl', 'is-active', '--quiet', 'nginx'], check=True)
|
||||
command = ['/usr/local/libexec/smail-acme/lego', '--path', str(ROOT),
|
||||
'--email', '', '--accept-tos', '--key-type', 'rsa2048', '--dns', provider,
|
||||
'--email', '', '--accept-tos', '--key-type', 'rsa2048', *challenge,
|
||||
'--server', 'https://acme-v02.api.letsencrypt.org/directory',
|
||||
'--http-timeout', '20', '--cert.timeout', '30']
|
||||
for domain in DOMAINS:
|
||||
command.extend(['--domains', domain])
|
||||
command.extend(['renew', '--days', '30', '--reuse-key', '--no-random-sleep'])
|
||||
subprocess.run(command, check=True, timeout=RENEW_TIMEOUT,
|
||||
env={**os.environ, 'GOMEMLIMIT': '64MiB'})
|
||||
env={**os.environ, 'GOMEMLIMIT': '64MiB'},
|
||||
umask=0o022 if '--http' in challenge else 0o077)
|
||||
else:
|
||||
print('线上证书有效期大于 30 天,本次不申请新证书')
|
||||
deploy.publish()
|
||||
|
||||
@@ -7,6 +7,7 @@ Wants=network-online.target
|
||||
Type=oneshot
|
||||
ExecStart=/usr/local/libexec/smail-acme/renew_certificate.py
|
||||
EnvironmentFile=-/etc/smail/acme-dns.env
|
||||
EnvironmentFile=-/etc/smail/acme-renew.env
|
||||
TimeoutStartSec=720
|
||||
TimeoutStopSec=15
|
||||
MemoryHigh=96M
|
||||
|
||||
Reference in new issue
Block a user