289 lines
14 KiB
Python
289 lines
14 KiB
Python
#!/usr/bin/env python3
|
|
"""Build a signed, program-only Windows update bundle without network access."""
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import base64
|
|
from dataclasses import dataclass
|
|
import hashlib
|
|
import os
|
|
from pathlib import Path
|
|
import re
|
|
import stat
|
|
import tempfile
|
|
import zipfile
|
|
|
|
from cryptography.hazmat.primitives import serialization
|
|
from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PrivateKey, Ed25519PublicKey
|
|
|
|
import update_manager
|
|
|
|
|
|
VERSION = re.compile(r'[A-Za-z0-9][A-Za-z0-9._-]{0,79}')
|
|
ARCHIVE_VERSION = re.compile(r'[A-Za-z0-9][A-Za-z0-9._-]{0,100}')
|
|
REQUIRED_PROGRAM_FILES = {
|
|
'app/backend.jar',
|
|
'deploy/offline/control.py',
|
|
'deploy/offline/update_manager.py',
|
|
'backend/src/main/resources/analysis-runtime-policy.json',
|
|
}
|
|
SENSITIVE_PARTS = {'.runtime', 'runtimedata', 'migration', 'private', 'secrets', 'credentials'}
|
|
SENSITIVE_SUFFIXES = {'.pem', '.key', '.p12', '.pfx', '.jks'}
|
|
|
|
|
|
class PublishError(RuntimeError):
|
|
"""Release inputs cannot produce a safe update package."""
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class PublishedArtifacts:
|
|
archive: Path
|
|
latest: Path
|
|
file_count: int
|
|
archive_sha256: str
|
|
|
|
|
|
def _manifest(root: Path) -> dict:
|
|
try:
|
|
value = update_manager.read(root / 'manifest.json')
|
|
except Exception as error:
|
|
raise PublishError('无法读取部署包 manifest.json。') from error
|
|
if not isinstance(value, dict) or value.get('platform') != 'windows' or value.get('architecture') != 'x86_64':
|
|
raise PublishError('输入目录必须是 Windows x64 离线部署包。')
|
|
if not isinstance(value.get('files'), list) or len(value['files']) > 50000:
|
|
raise PublishError('部署包文件清单无效。')
|
|
for key in ('runtimes', 'pythonWheels'):
|
|
if not isinstance(value.get(key), list):
|
|
raise PublishError('部署包缺少运行环境指纹信息。')
|
|
current_sequence = value.get('updateSequence', 0)
|
|
if type(current_sequence) is not int or current_sequence < 0:
|
|
raise PublishError('部署包更新序号无效。')
|
|
return value
|
|
|
|
|
|
def _sensitive_path(name: str) -> bool:
|
|
path = update_manager.safe_path(name)
|
|
parts = [part.casefold() for part in path.parts]
|
|
return (name == update_manager.KEY
|
|
or name.startswith('deploy/bootstrap/')
|
|
or any(part in SENSITIVE_PARTS for part in parts)
|
|
or path.suffix.casefold() in SENSITIVE_SUFFIXES)
|
|
|
|
|
|
def _program_inventory(root: Path, manifest: dict) -> list[dict]:
|
|
entries: dict[str, dict] = {}
|
|
folded: set[str] = set()
|
|
for entry in manifest['files']:
|
|
if not isinstance(entry, dict):
|
|
raise PublishError('部署包文件清单无效。')
|
|
name = entry.get('path')
|
|
try:
|
|
update_manager.safe_path(name)
|
|
except update_manager.UpdateError as error:
|
|
raise PublishError('部署包文件清单含有无效路径。') from error
|
|
if name.casefold() in folded:
|
|
raise PublishError('部署包文件清单含有重复或大小写冲突路径。')
|
|
folded.add(name.casefold())
|
|
if not update_manager.program_path(name):
|
|
continue
|
|
if _sensitive_path(name):
|
|
raise PublishError('程序清单中包含受保护的私有、迁移、引导或密钥文件路径。')
|
|
if entry.get('symlink') is not None or not update_manager.SHA.fullmatch(str(entry.get('sha256', ''))):
|
|
raise PublishError('程序文件清单缺少有效摘要或包含符号链接。')
|
|
path = update_manager.destination(root, name)
|
|
if not path.is_file() or path.is_symlink():
|
|
raise PublishError('程序清单文件缺失或不是普通文件。')
|
|
try:
|
|
digest = update_manager.digest(path)
|
|
except OSError as error:
|
|
raise PublishError('读取程序清单文件失败。') from error
|
|
if digest != entry['sha256']:
|
|
raise PublishError('部署包程序文件与 manifest.json 摘要不一致。')
|
|
entries[name] = {'path': name, 'sha256': digest, 'size': path.stat().st_size}
|
|
|
|
if not REQUIRED_PROGRAM_FILES.issubset(entries) or not any(name.startswith('app/web/') for name in entries):
|
|
raise PublishError('程序清单缺少更新所需的后端、网页、策略或更新管理程序。')
|
|
|
|
# The updater replaces program files as a complete snapshot. Refuse to
|
|
# sign an incomplete manifest if a program file exists outside its list.
|
|
present: set[str] = set()
|
|
for path in root.rglob('*'):
|
|
name = path.relative_to(root).as_posix()
|
|
if not update_manager.program_path(name):
|
|
continue
|
|
if path.is_symlink():
|
|
raise PublishError('程序目录含有符号链接或特殊文件。')
|
|
if path.is_dir():
|
|
continue
|
|
if not path.is_file():
|
|
raise PublishError('程序目录含有符号链接或特殊文件。')
|
|
present.add(name)
|
|
if present != set(entries):
|
|
raise PublishError('manifest.json 与磁盘上的完整程序文件清单不一致。')
|
|
return [entries[name] for name in sorted(entries)]
|
|
|
|
|
|
def _load_private_key(path: Path, root: Path, output_dir: Path) -> Ed25519PrivateKey:
|
|
try:
|
|
resolved = path.expanduser().resolve(strict=True)
|
|
if resolved.is_relative_to(root) or resolved.is_relative_to(output_dir):
|
|
raise PublishError('签名私钥必须保存在部署包和更新产物目录之外。')
|
|
key = serialization.load_pem_private_key(resolved.read_bytes(), password=None)
|
|
except PublishError:
|
|
raise
|
|
except Exception as error:
|
|
raise PublishError('无法读取未加密 PEM 格式的 Ed25519 签名私钥。') from error
|
|
if not isinstance(key, Ed25519PrivateKey):
|
|
raise PublishError('签名私钥必须是 Ed25519。')
|
|
return key
|
|
|
|
|
|
def _write_archive(root: Path, archive_temp: Path, files: list[dict]) -> None:
|
|
with zipfile.ZipFile(archive_temp, 'w', compression=zipfile.ZIP_DEFLATED, compresslevel=9, allowZip64=True) as output:
|
|
for entry in files:
|
|
name = entry['path']
|
|
source = update_manager.destination(root, name)
|
|
info = zipfile.ZipInfo(name, date_time=(1980, 1, 1, 0, 0, 0))
|
|
info.compress_type = zipfile.ZIP_DEFLATED
|
|
info.create_system = 3
|
|
info.external_attr = (stat.S_IFREG | 0o644) << 16
|
|
digest = hashlib.sha256()
|
|
size = 0
|
|
with source.open('rb') as reader, output.open(info, 'w') as writer:
|
|
while block := reader.read(1024 * 1024):
|
|
size += len(block)
|
|
if size > entry['size'] or size > update_manager.MAX_EXPANDED:
|
|
raise PublishError('程序文件在打包期间发生变化。')
|
|
digest.update(block)
|
|
writer.write(block)
|
|
if size != entry['size'] or digest.hexdigest() != entry['sha256']:
|
|
raise PublishError('程序文件在打包期间发生变化,已停止发布。')
|
|
|
|
|
|
def publish(root: Path, version: str, sequence: int, private_key_path: Path, output_dir: Path,
|
|
allow_baseline: bool = False) -> PublishedArtifacts:
|
|
"""Create quality-update-<version>.zip and signed latest.json locally."""
|
|
root = root.expanduser().resolve(strict=True)
|
|
if not root.is_dir():
|
|
raise PublishError('部署包根路径必须是目录。')
|
|
if not isinstance(version, str) or not VERSION.fullmatch(version):
|
|
raise PublishError('版本号只能包含字母、数字、点、下划线和连字符。')
|
|
if not ARCHIVE_VERSION.fullmatch(version):
|
|
raise PublishError('版本号不能用于更新文件名。')
|
|
if type(sequence) is not int or sequence <= 0:
|
|
raise PublishError('更新序号必须是正整数。')
|
|
|
|
manifest = _manifest(root)
|
|
current_sequence = manifest['updateSequence']
|
|
if sequence < current_sequence:
|
|
raise PublishError('更新序号不能低于部署包当前序号。')
|
|
if sequence == current_sequence and not allow_baseline:
|
|
raise PublishError('更新序号必须大于部署包当前序号。')
|
|
if sequence == current_sequence and version != manifest.get('updateVersion'):
|
|
raise PublishError('基线版本必须与部署包当前 updateVersion 完全一致。')
|
|
files = _program_inventory(root, manifest)
|
|
if sum(entry['size'] for entry in files) > update_manager.MAX_EXPANDED:
|
|
raise PublishError('更新程序文件总大小超过限制。')
|
|
|
|
output_dir = output_dir.expanduser().resolve()
|
|
if output_dir.is_relative_to(root):
|
|
raise PublishError('更新产物目录必须位于部署包目录之外。')
|
|
private_key = _load_private_key(Path(private_key_path), root, output_dir)
|
|
public_key_path = update_manager.destination(root, update_manager.KEY)
|
|
try:
|
|
public_key = serialization.load_pem_public_key(public_key_path.read_bytes())
|
|
except Exception as error:
|
|
raise PublishError('无法读取部署包中的更新公钥。') from error
|
|
if not isinstance(public_key, Ed25519PublicKey) or public_key.public_bytes(
|
|
serialization.Encoding.Raw, serialization.PublicFormat.Raw) != private_key.public_key().public_bytes(
|
|
serialization.Encoding.Raw, serialization.PublicFormat.Raw):
|
|
raise PublishError('签名私钥与部署包中的更新公钥不匹配。')
|
|
|
|
output_dir.mkdir(parents=True, exist_ok=True)
|
|
archive_name = f'quality-update-{version}.zip'
|
|
archive_path = output_dir / archive_name
|
|
latest_path = output_dir / 'latest.json'
|
|
if archive_path.exists() or latest_path.exists():
|
|
raise PublishError('输出目录已包含同名发布文件;请使用新的空输出目录。')
|
|
|
|
archive_temp: Path | None = None
|
|
latest_temp: Path | None = None
|
|
published_archive = False
|
|
try:
|
|
with tempfile.NamedTemporaryFile(prefix='.quality-update-', suffix='.tmp', dir=output_dir, delete=False) as stream:
|
|
archive_temp = Path(stream.name)
|
|
_write_archive(root, archive_temp, files)
|
|
archive_size = archive_temp.stat().st_size
|
|
if not 0 < archive_size <= update_manager.MAX_ARCHIVE:
|
|
raise PublishError('更新压缩包大小超过限制。')
|
|
archive_hash = update_manager.digest(archive_temp)
|
|
release = {
|
|
'schema': 1,
|
|
'version': version,
|
|
'sequence': sequence,
|
|
'platform': 'windows-x64',
|
|
'runtimeFingerprint': update_manager.runtime_fingerprint(manifest),
|
|
'archive': {'file': archive_name, 'size': archive_size, 'sha256': archive_hash},
|
|
'files': files,
|
|
}
|
|
raw = update_manager.canonical(release)
|
|
if len(raw) > update_manager.MAX_MANIFEST:
|
|
raise PublishError('更新文件清单超过大小限制。')
|
|
envelope = {
|
|
'payload': base64.b64encode(raw).decode('ascii'),
|
|
'signature': base64.b64encode(private_key.sign(raw)).decode('ascii'),
|
|
}
|
|
try:
|
|
update_manager.validate_release(envelope, root)
|
|
except update_manager.UpdateError as error:
|
|
raise PublishError('生成的更新包未通过客户端契约校验:' + str(error)) from error
|
|
latest_bytes = update_manager.canonical(envelope)
|
|
with tempfile.NamedTemporaryFile(prefix='.latest-', suffix='.tmp', dir=output_dir, delete=False) as stream:
|
|
latest_temp = Path(stream.name)
|
|
stream.write(latest_bytes)
|
|
stream.flush()
|
|
os.fsync(stream.fileno())
|
|
os.link(archive_temp, archive_path)
|
|
archive_temp.unlink()
|
|
archive_temp = None
|
|
published_archive = True
|
|
os.link(latest_temp, latest_path)
|
|
latest_temp.unlink()
|
|
latest_temp = None
|
|
return PublishedArtifacts(archive_path, latest_path, len(files), archive_hash)
|
|
except FileExistsError as error:
|
|
raise PublishError('输出目录出现同名发布文件;请使用新的空输出目录。') from error
|
|
except OSError as error:
|
|
raise PublishError('写入更新产物失败。') from error
|
|
finally:
|
|
if archive_temp is not None:
|
|
archive_temp.unlink(missing_ok=True)
|
|
if latest_temp is not None:
|
|
latest_temp.unlink(missing_ok=True)
|
|
if published_archive and not latest_path.exists():
|
|
archive_path.unlink(missing_ok=True)
|
|
|
|
|
|
def main(argv: list[str] | None = None) -> int:
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument('--root', type=Path, required=True, help='Windows x64 offline bundle directory')
|
|
parser.add_argument('--version', required=True, help='Release version, for example 2026.11.04.2')
|
|
parser.add_argument('--sequence', type=int, required=True, help='Monotonically increasing update sequence')
|
|
parser.add_argument('--private-key', type=Path, required=True, help='Ed25519 PEM private key kept outside the bundle')
|
|
parser.add_argument('--output-dir', type=Path, required=True, help='New output directory for the ZIP and latest.json')
|
|
parser.add_argument('--allow-baseline', action='store_true',
|
|
help='Allow a seed release only when version and sequence exactly match the bundle manifest')
|
|
args = parser.parse_args(argv)
|
|
try:
|
|
result = publish(args.root, args.version, args.sequence, args.private_key, args.output_dir, args.allow_baseline)
|
|
except (PublishError, update_manager.UpdateError) as error:
|
|
parser.error(str(error))
|
|
print(f'已生成更新包:{result.archive}')
|
|
print(f'已生成签名清单:{result.latest}')
|
|
print(f'版本 {args.version},序号 {args.sequence},程序文件 {result.file_count} 个,SHA-256 {result.archive_sha256}')
|
|
return 0
|
|
|
|
|
|
if __name__ == '__main__':
|
|
raise SystemExit(main())
|