停用历史业务命令并明确CLI运维范围
This commit is contained in:
1 parent
25fd4cf89c
commit
d90ddaadbb
9 files changed
+202
-25
No files matched your search
+4
-4
@@ -2,7 +2,7 @@
|
||||
|
||||
Scert 是面向 Windows 和 Linux 的 SSL 证书管理服务。当前代码已覆盖核心证书生命周期、DNS-01、自动续期、SSH 部署、通知、认证和 Web 管理流程。
|
||||
|
||||
正式交付仅包含 Web 和服务端。CLI 限定为初始化、服务启动、运维诊断、数据库迁移、备份恢复、服务安装和卸载入口;历史 `domain` 命令保留兼容,不再扩展成另一套业务管理界面。Wails 历史实现不参与正式发布及测试门禁。
|
||||
正式交付仅包含 Web 和服务端。CLI 限定为初始化、服务启动、运维诊断、数据库维护、备份恢复、服务安装和卸载入口;历史 `domain`、`server`、`ssh-key`、`renew`、`deploy` 命令已停用,仅返回中文迁移提示,不执行原业务操作。命令边界见 `../doc/命令行运维范围说明.md`。Wails 历史实现不参与正式发布及测试门禁。
|
||||
|
||||
## 当前已实现
|
||||
|
||||
@@ -13,7 +13,7 @@ Scert 是面向 Windows 和 Linux 的 SSL 证书管理服务。当前代码已
|
||||
- 三种数据库的迁移脚本、嵌入检查和迁移历史保护;实际版本及验证边界见 `../doc/构建版本与数据库支持矩阵.md`。
|
||||
- 域名基础模型与 Repository。
|
||||
- 健康检查、状态和域名基础 API。
|
||||
- 中文 CLI:`version`、`help`、`init`、`status`、`serve`、`domain list`、`domain add`。
|
||||
- 中文运维 CLI:`version`、`help`、`init`、`status`、`serve`、证书诊断、日志、配置、备份恢复和系统服务管理。
|
||||
- Vue 前端管理页面。
|
||||
|
||||
## 首轮运行
|
||||
@@ -23,13 +23,13 @@ Scert 是面向 Windows 和 Linux 的 SSL 证书管理服务。当前代码已
|
||||
```powershell
|
||||
go run ./cmd/scert init --base-dir C:\ScertData
|
||||
go run ./cmd/scert status --base-dir C:\ScertData
|
||||
go run ./cmd/scert domain add --base-dir C:\ScertData --name example.com --type single
|
||||
go run ./cmd/scert domain list --base-dir C:\ScertData
|
||||
go run ./cmd/scert serve --base-dir C:\ScertData
|
||||
```
|
||||
|
||||
服务启动后可访问:
|
||||
|
||||
业务操作应在 Web 登录后进行。下列接口沿用服务端认证及授权规则,不能因列在运行示例中就视为允许匿名调用:
|
||||
|
||||
- `GET /healthz`
|
||||
- `GET /api/v1/status`
|
||||
- `GET /api/v1/domains`
|
||||
|
||||
@@ -66,7 +66,7 @@ func TestSetConfigValueUpdatesAndValidatesAllowedFields(t *testing.T) {
|
||||
|
||||
func TestRootIncludesOperationalCommands(t *testing.T) {
|
||||
root := newRootCommand()
|
||||
for _, name := range []string{"certificate", "renew", "deploy", "logs", "config", "backup", "restore"} {
|
||||
for _, name := range []string{"certificate", "logs", "config", "backup", "restore", "service", "dns"} {
|
||||
if root.CommandPath() == name || root.Commands() == nil {
|
||||
t.Fatalf("根命令状态异常:%s", name)
|
||||
}
|
||||
|
||||
@@ -64,7 +64,7 @@ func Execute() {
|
||||
}
|
||||
|
||||
func newRootCommand() *cobra.Command {
|
||||
root := &cobra.Command{Use: "scert", Short: "SSL 证书管理服务", SilenceUsage: true}
|
||||
root := &cobra.Command{Use: "scert", Short: "SSL 证书服务与运维工具,业务管理请使用 Web", SilenceUsage: true}
|
||||
root.CompletionOptions.DisableDefaultCmd = true
|
||||
root.SetHelpCommand(newHelpCommand())
|
||||
root.SetHelpTemplate(helpTemplate)
|
||||
@@ -72,14 +72,30 @@ func newRootCommand() *cobra.Command {
|
||||
root.PersistentFlags().StringVar(&baseDir, "base-dir", "", "Windows 默认安装根目录")
|
||||
root.AddCommand(
|
||||
&cobra.Command{Use: "version", Short: "显示版本信息", Run: func(cmd *cobra.Command, args []string) { cmd.Printf("Scert %s\n", version) }},
|
||||
newInitCommand(), newStatusCommand(), newServeCommand(), newDomainCommand(), newCertificateCommand(),
|
||||
newRenewCommand(), newDeployCommand(), newLogsCommand(), newConfigCommand(), newBackupCommand(), newRestoreCommand(),
|
||||
newServerCommand(), newSSHKeyCommand(), newServiceCommand(), newDNSCommand(),
|
||||
newInitCommand(), newStatusCommand(), newServeCommand(), newCertificateCommand(),
|
||||
newLogsCommand(), newConfigCommand(), newBackupCommand(), newRestoreCommand(),
|
||||
newServiceCommand(), newDNSCommand(),
|
||||
)
|
||||
for _, name := range []string{"domain", "server", "ssh-key", "renew", "deploy"} {
|
||||
root.AddCommand(newRetiredBusinessCommand(name))
|
||||
}
|
||||
localizeHelpFlags(root)
|
||||
return root
|
||||
}
|
||||
|
||||
// 历史业务命令只保留迁移提示,不读取配置、凭据或数据库,也不执行远程操作。
|
||||
func newRetiredBusinessCommand(name string) *cobra.Command {
|
||||
return &cobra.Command{
|
||||
Use: name,
|
||||
Short: "业务命令已迁移至 Web 管理页面",
|
||||
Hidden: true,
|
||||
DisableFlagParsing: true,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return errors.New("此业务命令已停用,请使用 Web 管理页面;CLI 仅保留初始化、服务运行和运维诊断、数据库维护、备份恢复及系统服务管理")
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func newHelpCommand() *cobra.Command {
|
||||
return &cobra.Command{
|
||||
Use: "help [命令]",
|
||||
|
||||
@@ -72,29 +72,29 @@ func TestHelpSupportsSubcommand(t *testing.T) {
|
||||
var output bytes.Buffer
|
||||
root.SetOut(&output)
|
||||
root.SetErr(&output)
|
||||
root.SetArgs([]string{"help", "domain"})
|
||||
root.SetArgs([]string{"help", "certificate"})
|
||||
if err := root.Execute(); err != nil {
|
||||
t.Fatalf("查看子命令帮助失败:%v", err)
|
||||
}
|
||||
text := output.String()
|
||||
if !strings.Contains(text, "管理托管域名") || !strings.Contains(text, "添加托管域名") {
|
||||
if !strings.Contains(text, "查询和校验证书") || !strings.Contains(text, "校验当前证书") {
|
||||
t.Fatalf("子命令帮助内容不正确:%s", text)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHelpIncludesServerManagementCommands(t *testing.T) {
|
||||
func TestHelpIncludesSystemServiceCommands(t *testing.T) {
|
||||
root := newRootCommand()
|
||||
var output bytes.Buffer
|
||||
root.SetOut(&output)
|
||||
root.SetErr(&output)
|
||||
root.SetArgs([]string{"help", "server"})
|
||||
root.SetArgs([]string{"help", "service"})
|
||||
if err := root.Execute(); err != nil {
|
||||
t.Fatalf("查看服务器管理帮助失败:%v", err)
|
||||
t.Fatalf("查看系统服务帮助失败:%v", err)
|
||||
}
|
||||
text := output.String()
|
||||
for _, expected := range []string{"管理部署服务器", "添加部署服务器", "编辑部署服务器", "删除部署服务器"} {
|
||||
for _, expected := range []string{"管理系统服务", "注册 Scert 系统服务", "取消注册 Scert 系统服务"} {
|
||||
if !strings.Contains(text, expected) {
|
||||
t.Fatalf("服务器管理帮助缺少 %q:%s", expected, text)
|
||||
t.Fatalf("系统服务帮助缺少 %q:%s", expected, text)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -117,19 +117,19 @@ func TestServerCommandUsesDefaultCertificatePathTemplates(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestHelpIncludesSSHKeyManagementCommands(t *testing.T) {
|
||||
func TestHelpIncludesDNSDiagnosticCommand(t *testing.T) {
|
||||
root := newRootCommand()
|
||||
var output bytes.Buffer
|
||||
root.SetOut(&output)
|
||||
root.SetErr(&output)
|
||||
root.SetArgs([]string{"help", "ssh-key"})
|
||||
root.SetArgs([]string{"help", "dns"})
|
||||
if err := root.Execute(); err != nil {
|
||||
t.Fatalf("查看 SSH 私钥管理帮助失败:%v", err)
|
||||
t.Fatalf("查看 DNS 诊断帮助失败:%v", err)
|
||||
}
|
||||
text := output.String()
|
||||
for _, expected := range []string{"管理 SSH 部署私钥", "安全录入 SSH 私钥", "删除未被服务器使用的 SSH 私钥"} {
|
||||
for _, expected := range []string{"检查指定域名的阿里云 RAM 权限", "验证当前权限"} {
|
||||
if !strings.Contains(text, expected) {
|
||||
t.Fatalf("SSH 私钥管理帮助缺少 %q:%s", expected, text)
|
||||
t.Fatalf("DNS 诊断帮助缺少 %q:%s", expected, text)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,87 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestRootExposesOnlyOperationalCommands(t *testing.T) {
|
||||
allowed := map[string]bool{
|
||||
"version": true, "init": true, "status": true, "serve": true,
|
||||
"certificate": true, "logs": true, "config": true,
|
||||
"backup": true, "restore": true, "service": true, "dns": true,
|
||||
}
|
||||
root := newRootCommand()
|
||||
for _, command := range root.Commands() {
|
||||
if command.Hidden {
|
||||
continue
|
||||
}
|
||||
if !allowed[command.Name()] {
|
||||
t.Errorf("正式 CLI 暴露了范围外命令:%s", command.Name())
|
||||
}
|
||||
delete(allowed, command.Name())
|
||||
}
|
||||
if len(allowed) != 0 {
|
||||
t.Fatalf("正式 CLI 缺少运维命令:%v", allowed)
|
||||
}
|
||||
var output bytes.Buffer
|
||||
root.SetOut(&output)
|
||||
root.SetArgs([]string{"help"})
|
||||
if err := root.Execute(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, name := range []string{"domain", "server", "ssh-key", "renew", "deploy"} {
|
||||
if strings.Contains(output.String(), "\n "+name+" ") {
|
||||
t.Errorf("正式帮助仍展示历史业务命令:%s", name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRetiredBusinessCommandsRejectBeforeAccessingData(t *testing.T) {
|
||||
cases := [][]string{
|
||||
{"domain", "list"},
|
||||
{"domain", "add", "--name", "example.com"},
|
||||
{"domain", "issue", "--id", "example"},
|
||||
{"domain", "import", "--private-key", "not-a-real-key.pem"},
|
||||
{"server", "add", "--password", "test-secret-not-real"},
|
||||
{"server", "delete", "--id", "example"},
|
||||
{"ssh-key", "add", "--name", "example"},
|
||||
{"ssh-key", "delete", "--id", "example"},
|
||||
{"renew"},
|
||||
{"renew", "--id", "example"},
|
||||
{"deploy", "--id", "example"},
|
||||
}
|
||||
for _, args := range cases {
|
||||
t.Run(strings.Join(args[:min(2, len(args))], "_"), func(t *testing.T) {
|
||||
directory := t.TempDir()
|
||||
path := filepath.Join(directory, "invalid.json")
|
||||
original := []byte("invalid-config-sentinel")
|
||||
if err := os.WriteFile(path, original, 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
root := newRootCommand()
|
||||
var output bytes.Buffer
|
||||
root.SetOut(&output)
|
||||
root.SetErr(&output)
|
||||
root.SetArgs(append([]string{"--config", path, "--base-dir", directory}, args...))
|
||||
err := root.Execute()
|
||||
if err == nil || !strings.Contains(err.Error(), "此业务命令已停用,请使用 Web 管理页面") {
|
||||
t.Fatalf("旧入口未在读取无效配置前拒绝:%v", err)
|
||||
}
|
||||
if strings.Contains(output.String()+err.Error(), "test-secret-not-real") {
|
||||
t.Fatal("迁移提示泄露了旧命令中的凭据参数")
|
||||
}
|
||||
content, readErr := os.ReadFile(path)
|
||||
if readErr != nil || !bytes.Equal(content, original) {
|
||||
t.Fatalf("被停用命令修改了配置:%v", readErr)
|
||||
}
|
||||
entries, readErr := os.ReadDir(directory)
|
||||
if readErr != nil || len(entries) != 1 {
|
||||
t.Fatalf("被停用命令创建了运行数据:%v", readErr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user