调整部分前端布局和后端增删改逻辑
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
from .command import app
|
||||
|
||||
__all__ = ['app']
|
||||
@@ -0,0 +1,94 @@
|
||||
import typer
|
||||
|
||||
from cli.context import AllowProdOption, DryRunOption, EnvOption, OutputOption, YesOption
|
||||
|
||||
from .controller import CryptoCommandController
|
||||
|
||||
app = typer.Typer(
|
||||
help='传输加密相关命令',
|
||||
no_args_is_help=True,
|
||||
context_settings={'help_option_names': ['-h', '--help']},
|
||||
)
|
||||
_CRYPTO_COMMAND_CONTROLLER = CryptoCommandController()
|
||||
|
||||
|
||||
@app.command('validate', help='校验传输加密配置')
|
||||
def validate(
|
||||
env: EnvOption = 'dev',
|
||||
output: OutputOption = 'text',
|
||||
) -> None:
|
||||
"""
|
||||
校验传输加密配置。
|
||||
|
||||
:param env: 当前命令运行环境
|
||||
:param output: 输出格式
|
||||
:return: None
|
||||
"""
|
||||
_CRYPTO_COMMAND_CONTROLLER.validate(env, output)
|
||||
|
||||
|
||||
@app.command('keygen', help='生成新的传输加密密钥对')
|
||||
def keygen(
|
||||
env: EnvOption = 'dev',
|
||||
output: OutputOption = 'text',
|
||||
kid: str = typer.Option('default', '--kid', help='生成的密钥版本标识'),
|
||||
key_size: int = typer.Option(2048, '--key-size', help='RSA 密钥长度'),
|
||||
) -> None:
|
||||
"""
|
||||
生成新的传输加密密钥对。
|
||||
|
||||
:param env: 当前命令运行环境
|
||||
:param output: 输出格式
|
||||
:param kid: 生成的密钥版本标识
|
||||
:param key_size: RSA 密钥长度
|
||||
:return: None
|
||||
"""
|
||||
_CRYPTO_COMMAND_CONTROLLER.keygen(env, output, kid=kid, key_size=key_size)
|
||||
|
||||
|
||||
@app.command('rotate', help='生成传输加密密钥轮换辅助结果')
|
||||
def rotate(
|
||||
env: EnvOption = 'dev',
|
||||
output: OutputOption = 'text',
|
||||
allow_prod: AllowProdOption = False,
|
||||
yes: YesOption = False,
|
||||
dry_run: DryRunOption = False,
|
||||
next_kid: str = typer.Option('rotated', '--next-kid', help='轮换后的新密钥版本标识'),
|
||||
key_size: int = typer.Option(2048, '--key-size', help='新密钥的 RSA 长度'),
|
||||
) -> None:
|
||||
"""
|
||||
生成传输加密密钥轮换辅助结果。
|
||||
|
||||
:param env: 当前命令运行环境
|
||||
:param output: 输出格式
|
||||
:param allow_prod: 是否允许生产环境危险命令
|
||||
:param yes: 是否跳过确认
|
||||
:param dry_run: 是否演练执行
|
||||
:param next_kid: 轮换后的新密钥版本标识
|
||||
:param key_size: 新密钥的 RSA 长度
|
||||
:return: None
|
||||
"""
|
||||
_CRYPTO_COMMAND_CONTROLLER.rotate(
|
||||
env,
|
||||
output,
|
||||
allow_prod,
|
||||
yes,
|
||||
dry_run,
|
||||
next_kid=next_kid,
|
||||
key_size=key_size,
|
||||
)
|
||||
|
||||
|
||||
@app.command('export-public', help='查看当前运行环境的公钥信息')
|
||||
def export_public(
|
||||
env: EnvOption = 'dev',
|
||||
output: OutputOption = 'text',
|
||||
) -> None:
|
||||
"""
|
||||
导出当前运行环境的公钥信息。
|
||||
|
||||
:param env: 当前命令运行环境
|
||||
:param output: 输出格式
|
||||
:return: None
|
||||
"""
|
||||
_CRYPTO_COMMAND_CONTROLLER.export_public(env, output)
|
||||
@@ -0,0 +1,125 @@
|
||||
from cli.core import (
|
||||
DEFAULT_CORE_SERVICES,
|
||||
CliContextFactory,
|
||||
CliExecutionService,
|
||||
)
|
||||
from cli.runtime.crypto import CRYPTO_RUNTIME, CryptoRuntimeService
|
||||
|
||||
from .presenter import CryptoCommandPresenter
|
||||
|
||||
|
||||
class CryptoCommandController:
|
||||
"""
|
||||
传输加密命令控制器。
|
||||
|
||||
该控制器负责组织 `crypto` 命令组的上下文准备、runtime 调用、
|
||||
payload 注入,以及基于输出格式选择 presenter 或直接返回 JSON。
|
||||
|
||||
:param context_factory: CLI 上下文工厂
|
||||
:param execution_service: CLI 执行服务
|
||||
:param presenter: 传输加密命令文本渲染器
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
*,
|
||||
context_factory: CliContextFactory | None = None,
|
||||
execution_service: CliExecutionService | None = None,
|
||||
presenter: CryptoCommandPresenter | None = None,
|
||||
runtime_service: CryptoRuntimeService | None = None,
|
||||
) -> None:
|
||||
"""
|
||||
初始化传输加密命令控制器。
|
||||
|
||||
:param context_factory: CLI 上下文工厂
|
||||
:param execution_service: CLI 执行服务
|
||||
:param presenter: 传输加密命令文本渲染器
|
||||
:param runtime_service: 传输加密运行时服务
|
||||
:return: None
|
||||
"""
|
||||
self.context_factory = context_factory or DEFAULT_CORE_SERVICES.context_factory
|
||||
self.execution_service = execution_service or DEFAULT_CORE_SERVICES.execution_service
|
||||
self.presenter = presenter or CryptoCommandPresenter()
|
||||
self.runtime_service = runtime_service or CRYPTO_RUNTIME
|
||||
|
||||
def validate(self, env: str, output: str) -> None:
|
||||
"""
|
||||
校验传输加密配置。
|
||||
|
||||
:param env: 当前命令运行环境
|
||||
:param output: 输出格式
|
||||
:return: None
|
||||
"""
|
||||
ctx = self.context_factory.build_readonly(env, output)
|
||||
self.execution_service.complete_payload(ctx, self.runtime_service.validate_crypto_config())
|
||||
|
||||
def keygen(self, env: str, output: str, *, kid: str, key_size: int) -> None:
|
||||
"""
|
||||
生成新的传输加密密钥对。
|
||||
|
||||
:param env: 当前命令运行环境
|
||||
:param output: 输出格式
|
||||
:param kid: 生成的密钥版本标识
|
||||
:param key_size: RSA 密钥长度
|
||||
:return: None
|
||||
"""
|
||||
ctx = self.context_factory.build_readonly(env, output)
|
||||
payload = self.runtime_service.generate_crypto_key_pair(kid, key_size)
|
||||
payload['env'] = ctx.env
|
||||
self.execution_service.complete_payload_with_text(
|
||||
ctx,
|
||||
payload,
|
||||
text_builder=self.presenter.build_crypto_keygen_text,
|
||||
text_condition=lambda data: data.get('ok', False),
|
||||
)
|
||||
|
||||
def rotate(
|
||||
self,
|
||||
env: str,
|
||||
output: str,
|
||||
allow_prod: bool,
|
||||
yes: bool,
|
||||
dry_run: bool,
|
||||
*,
|
||||
next_kid: str,
|
||||
key_size: int,
|
||||
) -> None:
|
||||
"""
|
||||
生成传输加密密钥轮换辅助结果。
|
||||
|
||||
:param env: 当前命令运行环境
|
||||
:param output: 输出格式
|
||||
:param allow_prod: 是否允许生产环境危险命令
|
||||
:param yes: 是否跳过确认
|
||||
:param dry_run: 是否演练执行
|
||||
:param next_kid: 轮换后的新密钥版本标识
|
||||
:param key_size: 新密钥的 RSA 长度
|
||||
:return: None
|
||||
"""
|
||||
ctx = self.context_factory.build_dangerous(
|
||||
env,
|
||||
output,
|
||||
allow_prod,
|
||||
yes,
|
||||
dry_run,
|
||||
command_name='crypto rotate',
|
||||
)
|
||||
self.execution_service.complete_payload(ctx, self.runtime_service.build_rotation_payload(next_kid, key_size))
|
||||
|
||||
def export_public(self, env: str, output: str) -> None:
|
||||
"""
|
||||
导出当前运行环境的公钥信息。
|
||||
|
||||
:param env: 当前命令运行环境
|
||||
:param output: 输出格式
|
||||
:return: None
|
||||
"""
|
||||
ctx = self.context_factory.build_readonly(env, output)
|
||||
payload = self.runtime_service.export_public_key()
|
||||
payload['env'] = ctx.env
|
||||
self.execution_service.complete_payload_with_text(
|
||||
ctx,
|
||||
payload,
|
||||
text_builder=self.presenter.build_export_public_text,
|
||||
text_condition=lambda data: data.get('ok', False),
|
||||
)
|
||||
@@ -0,0 +1,79 @@
|
||||
class CryptoCommandPresenter:
|
||||
"""
|
||||
传输加密命令文本渲染器。
|
||||
|
||||
该渲染器负责将 `crypto` 命令组产生的结构化 payload 转换为稳定的文本摘要,
|
||||
同时保持 JSON 输出仍由控制器直接返回,不在此处做契约变形。
|
||||
"""
|
||||
|
||||
def build_crypto_keygen_text(self, payload: dict[str, object]) -> str:
|
||||
"""
|
||||
将密钥生成结果渲染为文本摘要。
|
||||
|
||||
:param payload: 密钥生成结果字典
|
||||
:return: 文本摘要
|
||||
"""
|
||||
env_patch = payload.get('envPatch')
|
||||
env_patch_keys = sorted(env_patch.keys()) if isinstance(env_patch, dict) else []
|
||||
lines = [
|
||||
f'ok: {str(payload.get("ok", False)).lower()}',
|
||||
f'env: {payload.get("env", "")}',
|
||||
f'kid: {payload.get("kid", "-")}',
|
||||
f'key_size: {payload.get("keySize", "-")}',
|
||||
]
|
||||
lines.extend(self._build_multiline_block('public_key', payload.get('publicKey')))
|
||||
lines.extend(self._build_multiline_block('private_key', payload.get('privateKey')))
|
||||
if env_patch_keys:
|
||||
lines.append('env_patch_keys:')
|
||||
lines.extend(f' - {key}' for key in env_patch_keys)
|
||||
else:
|
||||
lines.append('env_patch_keys: none')
|
||||
return '\n'.join(lines)
|
||||
|
||||
def build_export_public_text(self, payload: dict[str, object]) -> str:
|
||||
"""
|
||||
将公钥导出结果渲染为文本摘要。
|
||||
|
||||
:param payload: 公钥导出结果字典
|
||||
:return: 文本摘要
|
||||
"""
|
||||
public_key_payload = payload.get('publicKey')
|
||||
if not isinstance(public_key_payload, dict):
|
||||
return '\n'.join(
|
||||
[
|
||||
f'ok: {str(payload.get("ok", False)).lower()}',
|
||||
f'env: {payload.get("env", "")}',
|
||||
'public_key: none',
|
||||
]
|
||||
)
|
||||
|
||||
supported_kids = public_key_payload.get('supportedKids')
|
||||
lines = [
|
||||
f'ok: {str(payload.get("ok", False)).lower()}',
|
||||
f'env: {payload.get("env", "")}',
|
||||
f'kid: {public_key_payload.get("kid", "-")}',
|
||||
f'alg: {public_key_payload.get("alg", "-")}',
|
||||
f'envelope_version: {public_key_payload.get("envelopeVersion", "-")}',
|
||||
f'expire_at: {public_key_payload.get("expireAt", "-")}',
|
||||
]
|
||||
if isinstance(supported_kids, list) and supported_kids:
|
||||
lines.append('supported_kids:')
|
||||
lines.extend(f' - {kid}' for kid in supported_kids)
|
||||
else:
|
||||
lines.append('supported_kids: none')
|
||||
lines.extend(self._build_multiline_block('public_key_pem', public_key_payload.get('publicKey')))
|
||||
return '\n'.join(lines)
|
||||
|
||||
@staticmethod
|
||||
def _build_multiline_block(title: str, value: object) -> list[str]:
|
||||
"""
|
||||
将多行文本构建为带标题的块级文本。
|
||||
|
||||
:param title: 块标题
|
||||
:param value: 原始文本值
|
||||
:return: 文本行列表
|
||||
"""
|
||||
text = '' if value is None else str(value).strip()
|
||||
if not text:
|
||||
return [f'{title}: -']
|
||||
return [f'{title}:', ' |', *[f' {line}' for line in text.splitlines()]]
|
||||
Reference in New Issue
Block a user