验收基线:保存无停机续期候选及真实挑战受阻证据
This commit is contained in:
1 parent
45078da64a
commit
9e2d1fe40f
9 files changed
+430
-4
No files matched your search
@@ -96,6 +96,7 @@ class RenewalTests(unittest.TestCase):
|
||||
return subprocess.CompletedProcess(command, 1)
|
||||
if command[0].endswith('/lego'):
|
||||
self.assertEqual(kwargs['timeout'], 600)
|
||||
self.assertEqual(kwargs['umask'], 0o077)
|
||||
self.assertEqual(command[command.index('--dns') + 1], 'alidns')
|
||||
self.assertNotIn('--tls', command)
|
||||
self.assertIn('--server', command)
|
||||
|
||||
@@ -0,0 +1,152 @@
|
||||
'''无停机 HTTP 验证选项回归,仅使用隔离目录和模拟续期命令。'''
|
||||
|
||||
from contextlib import nullcontext
|
||||
import os
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
|
||||
BASE = Path(__file__).resolve().parent
|
||||
SCRIPTS = BASE if (BASE / 'renew_certificate.py').exists() else BASE.parents[1] / 'deploy/nginx'
|
||||
sys.path.insert(0, str(SCRIPTS))
|
||||
import renew_certificate as renew
|
||||
|
||||
|
||||
class WebrootTests(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.directory = Path(tempfile.mkdtemp(prefix='webroot-tests-', dir=Path.cwd()))
|
||||
self.webroot = self.directory / 'public'
|
||||
self.challenge = self.webroot / '.well-known/acme-challenge'
|
||||
self.challenge.mkdir(parents=True)
|
||||
for path in (self.webroot, self.challenge.parent, self.challenge):
|
||||
path.chmod(0o755)
|
||||
for context in (patch.object(renew, 'WEBROOT', self.webroot),
|
||||
patch.object(renew, 'ROOT', self.directory),
|
||||
patch.dict(os.environ, {'SMAIL_ACME_CHALLENGE': 'http'}, clear=True)):
|
||||
context.start()
|
||||
self.addCleanup(context.stop)
|
||||
|
||||
def execute(self, failure=None, due=True):
|
||||
calls = []
|
||||
|
||||
def run(command, **kwargs):
|
||||
calls.append(command)
|
||||
if '-checkend' in command:
|
||||
return subprocess.CompletedProcess(command, 1 if due else 0)
|
||||
if command[0].endswith('/lego'):
|
||||
self.assertEqual(kwargs['timeout'], 600)
|
||||
self.assertEqual(kwargs['umask'], 0o022)
|
||||
self.assertEqual(command[command.index('--http.webroot') + 1], str(self.webroot))
|
||||
for required in ('--http', '--reuse-key', '--no-random-sleep'):
|
||||
self.assertIn(required, command)
|
||||
for forbidden in ('--dns', '--tls', '--http.port'):
|
||||
self.assertNotIn(forbidden, command)
|
||||
if failure:
|
||||
raise failure
|
||||
return subprocess.CompletedProcess(command, 0)
|
||||
|
||||
with patch.object(renew.os, 'geteuid', return_value=0), \
|
||||
patch.object(renew.deploy, 'certificate_lock', return_value=nullcontext()), \
|
||||
patch.object(renew.deploy, 'check_nginx_paths'), \
|
||||
patch.object(renew.deploy, 'publish') as publish, \
|
||||
patch.object(renew.subprocess, 'run', side_effect=run):
|
||||
if failure:
|
||||
with self.assertRaises(type(failure)):
|
||||
renew.main()
|
||||
publish.assert_not_called()
|
||||
else:
|
||||
renew.main()
|
||||
publish.assert_called_once()
|
||||
self.assertFalse(any(command[:2] in (['systemctl', 'stop'], ['systemctl', 'start'])
|
||||
for command in calls))
|
||||
return calls
|
||||
|
||||
def test_http_explicitly_uses_existing_webroot(self):
|
||||
self.assertEqual(renew.challenge_arguments(), ['--http', '--http.webroot', str(self.webroot)])
|
||||
|
||||
def test_due_http_success_reuses_key_without_port_binding(self):
|
||||
self.assertTrue(any(command[0].endswith('/lego') for command in self.execute()))
|
||||
|
||||
def test_due_http_failure_does_not_publish_or_stop(self):
|
||||
self.execute(subprocess.CalledProcessError(1, 'lego'))
|
||||
|
||||
def test_due_http_timeout_does_not_publish_or_stop(self):
|
||||
self.execute(subprocess.TimeoutExpired('lego', 600))
|
||||
|
||||
def test_not_due_does_not_request_new_certificate(self):
|
||||
self.assertFalse(any(command[0].endswith('/lego') for command in self.execute(due=False)))
|
||||
|
||||
def test_invalid_method_does_not_fall_back(self):
|
||||
for method in ('', 'tls', 'HTTP', 'http dns'):
|
||||
with self.subTest(method=method), patch.dict(os.environ, {'SMAIL_ACME_CHALLENGE': method}):
|
||||
with self.assertRaises(RuntimeError):
|
||||
renew.challenge_arguments()
|
||||
|
||||
def test_dns_remains_explicit_and_requires_provider(self):
|
||||
with patch.dict(os.environ, {'SMAIL_ACME_CHALLENGE': 'dns'}):
|
||||
with self.assertRaises(RuntimeError):
|
||||
renew.challenge_arguments()
|
||||
with patch.dict(os.environ, {'SMAIL_ACME_DNS_PROVIDER': 'alidns'}):
|
||||
self.assertEqual(renew.challenge_arguments(), ['--dns', 'alidns'])
|
||||
|
||||
def test_missing_directory_is_rejected(self):
|
||||
with patch.object(renew, 'WEBROOT', self.directory / 'missing'):
|
||||
with self.assertRaises(FileNotFoundError):
|
||||
renew.challenge_arguments()
|
||||
|
||||
def test_public_account_directory_is_rejected(self):
|
||||
self.directory.chmod(0o755)
|
||||
try:
|
||||
with self.assertRaises(RuntimeError):
|
||||
renew.challenge_arguments()
|
||||
finally:
|
||||
self.directory.chmod(0o700)
|
||||
|
||||
def test_child_umask_preserves_private_parent_and_readable_challenge(self):
|
||||
token = self.challenge / 'token'
|
||||
key = self.directory / 'key'
|
||||
previous = os.umask(0o077)
|
||||
try:
|
||||
subprocess.run([
|
||||
sys.executable, '-c',
|
||||
'import os,sys; fd=os.open(sys.argv[1],os.O_CREAT|os.O_EXCL|os.O_WRONLY,0o644); os.close(fd)',
|
||||
str(token),
|
||||
], check=True, umask=0o022, timeout=10)
|
||||
key.write_bytes(b'private-test-data')
|
||||
self.assertEqual(token.stat().st_mode & 0o777, 0o644)
|
||||
self.assertEqual(key.stat().st_mode & 0o777, 0o600)
|
||||
self.assertEqual(self.directory.stat().st_mode & 0o777, 0o700)
|
||||
finally:
|
||||
os.umask(previous)
|
||||
|
||||
def test_unsafe_permissions_on_each_directory_are_rejected(self):
|
||||
for directory in (self.webroot, self.challenge.parent, self.challenge):
|
||||
for mode in (0o775, 0o777, 0o750, 0o700):
|
||||
with self.subTest(directory=directory.name, mode=oct(mode)):
|
||||
directory.chmod(mode)
|
||||
try:
|
||||
with self.assertRaises(RuntimeError):
|
||||
renew.challenge_arguments()
|
||||
finally:
|
||||
directory.chmod(0o755)
|
||||
|
||||
def test_symlink_directory_is_rejected(self):
|
||||
link = self.directory / 'linked'
|
||||
link.symlink_to(self.webroot, target_is_directory=True)
|
||||
with patch.object(renew, 'WEBROOT', link), self.assertRaises(RuntimeError):
|
||||
renew.challenge_arguments()
|
||||
|
||||
def test_regular_file_is_not_webroot(self):
|
||||
path = self.directory / 'file'
|
||||
path.write_bytes(b'not-a-directory')
|
||||
path.chmod(0o755)
|
||||
with patch.object(renew, 'WEBROOT', path), self.assertRaises(RuntimeError):
|
||||
renew.challenge_arguments()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main(verbosity=2)
|
||||
@@ -0,0 +1,177 @@
|
||||
'''使用全新 ACME 测试账号验证公网 HTTP 挑战,不停服,不发布测试证书。'''
|
||||
|
||||
import argparse
|
||||
from datetime import datetime, timezone
|
||||
import hashlib
|
||||
import http.client
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import socket
|
||||
import subprocess
|
||||
import threading
|
||||
import time
|
||||
|
||||
import nginx_certificate_cutover as monitor
|
||||
import renew_certificate as renew
|
||||
|
||||
|
||||
SERVER = 'https://acme-staging-v02.api.letsencrypt.org/directory'
|
||||
CONFIG = Path('/etc/nginx/conf.d/smail.conf')
|
||||
CONFIG_SHA = 'bf6816c38f9c639ab17fd7344fff4b06afc5a2388f74318e18c60b8b3311ebd8'
|
||||
|
||||
|
||||
def run(*command):
|
||||
return subprocess.check_output(command, text=True, timeout=15).strip()
|
||||
|
||||
|
||||
def file_state(path):
|
||||
info = path.stat()
|
||||
return {'sha256': hashlib.sha256(path.read_bytes()).hexdigest(),
|
||||
'uid': info.st_uid, 'gid': info.st_gid, 'mode': oct(info.st_mode & 0o777)}
|
||||
|
||||
|
||||
def service_state():
|
||||
return {name: run('systemctl', 'show', name, '--property=ActiveState,MainPID,InvocationID,NRestarts')
|
||||
for name in ('smail.service', 'nginx.service', 'Scert.service', 'mysql.service')}
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument('--confirm-staging-challenge', action='store_true')
|
||||
args = parser.parse_args()
|
||||
if not args.confirm_staging_challenge or os.geteuid() != 0 or run('hostname') != 'ikun2':
|
||||
parser.error('只允许在 47 服务器明确执行测试 CA 的全新 HTTP 挑战')
|
||||
if file_state(CONFIG)['sha256'] != CONFIG_SHA:
|
||||
parser.error('Nginx 配置与已审查模板不符,停止演练')
|
||||
memory = dict(line.split(':', 1) for line in Path('/proc/meminfo').read_text().splitlines())
|
||||
if int(memory['MemAvailable'].split()[0]) < 256 * 1024:
|
||||
parser.error('可用内存不足,停止演练')
|
||||
run('nginx', '-t')
|
||||
run('systemctl', 'is-active', 'nginx', 'smail', 'Scert', 'mysql')
|
||||
for domain in renew.DOMAINS:
|
||||
addresses = {entry[4][0] for entry in socket.getaddrinfo(domain, 80, type=socket.SOCK_STREAM)}
|
||||
if addresses != {'47.108.78.12'}:
|
||||
parser.error('域名解析与本次授权目标不符:' + domain)
|
||||
connection = http.client.HTTPConnection('127.0.0.1', 80, timeout=5)
|
||||
try:
|
||||
connection.request('GET', '/.well-known/acme-challenge/preflight-absent', headers={'Host': domain})
|
||||
response = connection.getresponse()
|
||||
response.read()
|
||||
if response.status != 404:
|
||||
parser.error('验证路径未返回预期状态:' + domain)
|
||||
finally:
|
||||
connection.close()
|
||||
os.umask(0o077)
|
||||
stamp = time.strftime('%Y%m%d-%H%M%S')
|
||||
evidence = Path('/var/tmp') / ('smail-review-http-challenge-' + stamp)
|
||||
state = Path('/var/lib') / ('smail-acme-http-review-' + stamp)
|
||||
evidence.mkdir(mode=0o700)
|
||||
state.mkdir(mode=0o700)
|
||||
assert not list(state.iterdir()), '必须使用全新账号目录,不能复用授权缓存'
|
||||
renew.ROOT = state
|
||||
os.environ['SMAIL_ACME_CHALLENGE'] = 'http'
|
||||
challenge = renew.challenge_arguments()
|
||||
protected = [Path('/opt/smail/smail'), Path('/opt/smail/smail-admin'),
|
||||
Path('/opt/smail/ssl/custom.crt'), Path('/opt/smail/ssl/custom.key'),
|
||||
Path('/usr/local/libexec/smail-acme/renew_certificate.py'),
|
||||
Path('/usr/local/libexec/smail-acme/deploy_certificate.py'),
|
||||
Path('/etc/systemd/system/smail-certificate-renew.service')]
|
||||
protected.extend(sorted(Path('/etc/nginx/conf.d').glob('*.conf')))
|
||||
protected.extend(sorted(Path('/var/lib/smail-acme-tls/certificates').glob('solit.cn.*')))
|
||||
before = {str(path): file_state(path) for path in protected}
|
||||
before_services = service_state()
|
||||
challenge_dir = renew.WEBROOT / '.well-known/acme-challenge'
|
||||
before_tokens = {path.name: file_state(path) for path in challenge_dir.iterdir()}
|
||||
report = {'status': 'running', 'startedUTC': datetime.now(timezone.utc).isoformat(),
|
||||
'stagingOnly': True, 'freshAccount': True, 'productionDeploymentExercised': False,
|
||||
'evidence': str(evidence), 'state': str(state), 'beforeServices': before_services,
|
||||
'protectedBefore': before, 'samples': []}
|
||||
stop = threading.Event()
|
||||
|
||||
def sample():
|
||||
for host in ('mail.solit.cn', 'web.apmmk.cn'):
|
||||
value = {'host': host, 'timeUTC': datetime.now(timezone.utc).isoformat()}
|
||||
try:
|
||||
value['bytes'] = len(monitor.request(host, 443))
|
||||
value['ok'] = True
|
||||
except Exception as error:
|
||||
value.update(ok=False, error=type(error).__name__)
|
||||
report['samples'].append(value)
|
||||
|
||||
def watch():
|
||||
while not stop.wait(0.5):
|
||||
sample()
|
||||
|
||||
unit = 'smail-http-challenge-' + stamp
|
||||
command = [
|
||||
'systemd-run', '--wait', '--pipe', '--unit=' + unit,
|
||||
'--property=Type=exec', '--property=UMask=0022',
|
||||
'--property=ProtectSystem=strict', '--property=ProtectHome=yes',
|
||||
'--property=NoNewPrivileges=yes', '--property=PrivateTmp=yes',
|
||||
'--property=InaccessiblePaths=/opt/smail /run/smail /etc/nginx /var/lib/smail-acme-tls',
|
||||
'--property=ReadWritePaths=' + str(state) + ' ' + str(challenge_dir),
|
||||
'--property=CapabilityBoundingSet=', '--property=MemoryHigh=96M',
|
||||
'--property=MemoryMax=128M', '--property=MemorySwapMax=0', '--property=CPUQuota=100%',
|
||||
'--property=TasksMax=64', '--property=RuntimeMaxSec=600s',
|
||||
'--property=TimeoutStopSec=15s', '--property=KillMode=control-group',
|
||||
'--property=OOMPolicy=stop', '--setenv=GOMEMLIMIT=64MiB',
|
||||
'/usr/local/libexec/smail-acme/lego', '--path', str(state), '--server', SERVER,
|
||||
'--email', '', '--accept-tos', '--key-type', 'rsa2048',
|
||||
'--http-timeout', '20', '--cert.timeout', '30', *challenge,
|
||||
]
|
||||
for domain in renew.DOMAINS:
|
||||
command.extend(['--domains', domain])
|
||||
command.append('run')
|
||||
thread = threading.Thread(target=watch, daemon=True)
|
||||
with renew.deploy.certificate_lock():
|
||||
try:
|
||||
sample()
|
||||
assert all(item['ok'] for item in report['samples']), '演练前业务探测失败'
|
||||
thread.start()
|
||||
with (evidence / 'acme.log').open('xb') as log:
|
||||
result = subprocess.run(command, stdout=log, stderr=subprocess.STDOUT, timeout=630)
|
||||
report['exitCode'] = result.returncode
|
||||
assert result.returncode == 0, '全新 HTTP 挑战失败,日志已保留'
|
||||
certificate = state / 'certificates/solit.cn.crt'
|
||||
key = state / 'certificates/solit.cn.key'
|
||||
public_cert = run('openssl', 'x509', '-in', str(certificate), '-pubkey', '-noout')
|
||||
public_key = run('openssl', 'pkey', '-in', str(key), '-pubout')
|
||||
assert public_cert == public_key
|
||||
for domain in renew.DOMAINS:
|
||||
run('openssl', 'x509', '-in', str(certificate), '-checkhost', domain, '-noout')
|
||||
report['certificate'] = file_state(certificate)
|
||||
report['domains'] = list(renew.DOMAINS)
|
||||
report['certificateMetadata'] = run('openssl', 'x509', '-in', str(certificate),
|
||||
'-noout', '-issuer', '-dates', '-ext', 'subjectAltName')
|
||||
report['status'] = 'passed'
|
||||
except BaseException as error:
|
||||
report.update(status='failed', error=type(error).__name__)
|
||||
raise
|
||||
finally:
|
||||
try:
|
||||
stopped = subprocess.run(['systemctl', 'stop', unit], capture_output=True, text=True, timeout=25)
|
||||
report['testUnitStopped'] = stopped.returncode == 0 or 'not loaded' in stopped.stderr
|
||||
sample()
|
||||
finally:
|
||||
stop.set()
|
||||
if thread.ident:
|
||||
thread.join(timeout=10)
|
||||
report['afterServices'] = service_state()
|
||||
report['servicesUnchanged'] = report['afterServices'] == before_services
|
||||
report['protectedUnchanged'] = all(file_state(path) == before[str(path)] for path in protected)
|
||||
report['challengeFilesRestored'] = before_tokens == {
|
||||
path.name: file_state(path) for path in challenge_dir.iterdir()}
|
||||
report['probesPassed'] = bool(report['samples']) and all(item['ok'] for item in report['samples'])
|
||||
if not all(report.get(key) for key in ('testUnitStopped', 'servicesUnchanged',
|
||||
'protectedUnchanged', 'challengeFilesRestored', 'probesPassed')):
|
||||
report['status'] = 'failed'
|
||||
report['finishedUTC'] = datetime.now(timezone.utc).isoformat()
|
||||
(evidence / 'result.json').write_text(json.dumps(report, ensure_ascii=False, indent=2), encoding='utf-8')
|
||||
print(json.dumps({key: value for key, value in report.items()
|
||||
if key not in ('samples', 'protectedBefore')}, ensure_ascii=False, indent=2))
|
||||
assert report['status'] == 'passed'
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in new issue
Block a user