接入本机人工处置协议和终端并记录Linux回归进度

This commit is contained in:
xuans committed 2026-10-03 21:33:54 +08:00
1 parent bf4caebaae
commit 0f2e6bfcce
24 files changed
+1534 -48

No files matched your search

+13 -1
View File
@@ -58,7 +58,11 @@ func runConnected(ctx context.Context, conn net.Conn, screen terminal) error {
}
defer client.Close()
if client.RecoveryAvailable() {
choice, err := screen.Read(ctx, "本机管理:1 重置超级管理员密码;2 只读核查部署事务;其他输入退出", false)
prompt := "本机管理:1 重置超级管理员密码;2 只读核查部署事务;其他输入退出"
if client.DispositionAvailable() {
prompt = "本机管理:1 重置超级管理员密码;2 只读核查部署事务;3 人工处置部署事务;4 查询人工处置结果;其他输入退出"
}
choice, err := screen.Read(ctx, prompt, false)
if err != nil {
return err
}
@@ -70,6 +74,14 @@ func runConnected(ctx context.Context, conn net.Conn, screen terminal) error {
return err
}
return nil
case "3", "4":
if !client.DispositionAvailable() {
return localreset.ErrUnavailable
}
if strings.TrimSpace(choice) == "4" {
return recoveryResultFlow(ctx, client, screen)
}
return recoveryDispositionFlow(ctx, client, screen)
default:
return nil
}
@@ -0,0 +1,155 @@
package adminconsole
import (
"context"
"fmt"
"strings"
"time"
"git.pcux.cn/xuans/Scert/backend/internal/deployment"
"git.pcux.cn/xuans/Scert/backend/internal/localreset"
)
type recoveryDispositionClient interface {
InspectRecovery(string) (deployment.RecoveryInspection, error)
PrepareRecovery(string, deployment.RecoveryAction) (localreset.RecoveryPreparation, error)
ExecuteRecovery(string, string) (deployment.RecoveryDispositionSummary, error)
GetRecoveryDisposition(string) (deployment.RecoveryDispositionSummary, error)
}
func recoveryDispositionFlow(ctx context.Context, client recoveryDispositionClient, screen terminal) error {
id, err := screen.Read(ctx, "请输入待处置事务编号(可先使用只读核查获取);输入 q 退出", false)
if err != nil {
return err
}
id = strings.TrimSpace(id)
if id == "q" {
return nil
}
if err := ctx.Err(); err != nil {
return err
}
inspection, err := client.InspectRecovery(id)
if err != nil {
_ = screen.Show("无法核查事务,未提交人工处置请求。请重新启动本机管理。")
return err
}
if err := showRecovery(screen, inspection); err != nil {
return err
}
choice, err := screen.Read(ctx, "选择明确动作:1 仅记录核查;2 完成提交(保留新证书并清理备份);3 恢复基线(恢复旧证书并重载);其他输入取消", false)
if err != nil {
return err
}
actions := map[string]deployment.RecoveryAction{
"1": deployment.RecoveryActionRecord, "2": deployment.RecoveryActionCommit, "3": deployment.RecoveryActionRollback,
}
action, ok := actions[strings.TrimSpace(choice)]
if !ok {
return screen.Show("已取消,未提交人工处置请求。")
}
if err := ctx.Err(); err != nil {
return err
}
prepared, err := client.PrepareRecovery(id, action)
if err != nil {
_ = screen.Show("准备失败或历史证据不足,未提交执行请求。请重新核查,不要手动删锁。")
return err
}
defer func() { prepared.Token = "" }()
if prepared.Summary.Validate() != nil || prepared.Summary.TransactionID != id || prepared.Summary.Action != action || !time.Now().Before(prepared.Summary.ExpiresAt) {
return localreset.ErrProtocol
}
lines := []string{
fmt.Sprintf("处置编号:%s(请保留,用于断连后查询结果)", terminalValue(prepared.DispositionID)),
fmt.Sprintf("事务:%s;服务器:%s;阶段:%s;动作:%s", terminalValue(id), terminalValue(prepared.Summary.ServerID), recoveryLabel(prepared.Summary.Phase), dispositionActionLabel(action)),
"确认截止时间:" + prepared.Summary.ExpiresAt.Local().Format("2006-01-02 15:04:05 MST"),
"仅记录只保存核查审计,不结束原事务;提交或回滚会修改远端文件,可能清理备份或重载服务。动作失败不会自动切换方向。",
"断连或结果不确定时,禁止重复执行;重新启动本机管理,选择查询人工处置结果。",
}
for _, line := range lines {
if err := screen.Show(line); err != nil {
return err
}
}
expected := "确认 " + id + " " + dispositionActionLabel(action)
confirmation, err := screen.Read(ctx, "如确认执行,请完整输入:"+expected+";其他输入取消", false)
if err != nil {
return err
}
if confirmation != expected {
return screen.Show("已取消,未发送执行请求。")
}
if err := ctx.Err(); err != nil {
return err
}
if !time.Now().Before(prepared.Summary.ExpiresAt) {
return screen.Show("确认已过期,未发送执行请求;请重新核查。")
}
result, err := client.ExecuteRecovery(prepared.DispositionID, prepared.Token)
prepared.Token = ""
if err != nil {
_ = screen.Show("未取得可靠结果,不能断言未执行或执行成功。请重新连接,按处置编号查询;不要重复提交或删除远端锁。")
return err
}
if result.ID != prepared.DispositionID || result.TransactionID != id || result.ServerID != prepared.Summary.ServerID || result.Action != action {
_ = screen.Show("响应与确认内容不一致,不能认定成功;请按处置编号查询。")
return localreset.ErrProtocol
}
return showRecoveryDisposition(screen, result)
}
func recoveryResultFlow(ctx context.Context, client recoveryDispositionClient, screen terminal) error {
id, err := screen.Read(ctx, "请输入人工处置编号(不是事务编号);本操作只查询,不重放、不释放占用", false)
if err != nil {
return err
}
if err := ctx.Err(); err != nil {
return err
}
id = strings.TrimSpace(id)
result, err := client.GetRecoveryDisposition(id)
if err != nil || result.ID != id {
_ = screen.Show("未取得有效处置结果;记录不存在或通道不可用不代表未执行,请保留编号继续核查。")
return localreset.ErrUnavailable
}
return showRecoveryDisposition(screen, result)
}
func showRecoveryDisposition(screen terminal, result deployment.RecoveryDispositionSummary) error {
if result.Validate() != nil {
return localreset.ErrProtocol
}
message := "处置仍在运行或结束审计尚未完成;保留占用,禁止重复执行。"
switch result.Status {
case "succeeded":
switch result.Action {
case deployment.RecoveryActionRecord:
message = "核查结论已记录;未改变原事务终态,不代表历史部署成功。"
case deployment.RecoveryActionCommit:
message = "本次人工提交核验及结束审计完成;不代表项目全部生产验收通过。"
case deployment.RecoveryActionRollback:
message = "本次恢复基线核验及结束审计完成;不代表项目全部生产验收通过。"
}
case "rejected":
message = "处置已拒绝,未执行本次远端变更;请重新核查拒绝原因。"
case "uncertain":
message = "处置结果不确定,可能已修改远端;占用继续保留,禁止重复执行或手动删锁。"
}
if err := screen.Show(fmt.Sprintf("处置:%s;事务:%s;服务器:%s;动作:%s", terminalValue(result.ID), terminalValue(result.TransactionID), terminalValue(result.ServerID), dispositionActionLabel(result.Action))); err != nil {
return err
}
return screen.Show(message)
}
func dispositionActionLabel(action deployment.RecoveryAction) string {
switch action {
case deployment.RecoveryActionRecord:
return "仅记录核查"
case deployment.RecoveryActionCommit:
return "完成提交"
case deployment.RecoveryActionRollback:
return "恢复基线"
}
return "未知动作"
}
@@ -0,0 +1,96 @@
//go:build linux
package adminconsole
import (
"context"
"os"
"path/filepath"
"strings"
"testing"
"time"
"git.pcux.cn/xuans/Scert/backend/internal/deployment"
"git.pcux.cn/xuans/Scert/backend/internal/localreset"
)
type dispositionConsoleSession struct {
consoleIPCSession
actions dispositionConsoleClient
}
func (*dispositionConsoleSession) DispositionAvailable() bool { return true }
func (s *dispositionConsoleSession) PrepareRecovery(_ context.Context, id string, action deployment.RecoveryAction) (localreset.RecoveryPreparation, error) {
return s.actions.PrepareRecovery(id, action)
}
func (s *dispositionConsoleSession) ExecuteRecovery(_ context.Context, id, token string) (deployment.RecoveryDispositionSummary, error) {
return s.actions.ExecuteRecovery(id, token)
}
func (s *dispositionConsoleSession) GetRecoveryDisposition(_ context.Context, id string) (deployment.RecoveryDispositionSummary, error) {
return s.actions.GetRecoveryDisposition(id)
}
func TestDispositionConsoleUnixMenus(t *testing.T) {
for _, mode := range []string{"record", "commit", "rollback", "query", "cancel"} {
t.Run(mode, func(t *testing.T) {
path := filepath.Join(t.TempDir(), "control.sock")
server, err := localreset.Listen(path)
if err != nil {
t.Fatal(err)
}
ctx, cancel := context.WithTimeout(t.Context(), 5*time.Second)
session := &dispositionConsoleSession{}
done := make(chan error, 1)
screen := &recoveryTestScreen{}
switch mode {
case "record":
screen.values = []string{"3", "tx-1", "1", "确认 tx-1 仅记录核查"}
case "commit":
screen.values = []string{"3", "tx-1", "2", "确认 tx-1 完成提交"}
case "rollback":
screen.values = []string{"3", "tx-1", "3", "确认 tx-1 恢复基线"}
case "query":
screen.values = []string{"4", strings.Repeat("b", 64)}
session.actions.action = deployment.RecoveryActionRecord
case "cancel":
screen.values = []string{"3", "tx-1", "2", "取消"}
}
go func() {
done <- server.Serve(ctx, func(context.Context, localreset.Peer) (localreset.Session, error) { return session, nil })
}()
defer server.Close()
defer cancel()
conn, err := localreset.Dial(ctx, path, uint32(os.Geteuid()))
if err != nil {
t.Fatal(err)
}
err = runConnected(ctx, conn, screen)
cancel()
if serveErr := <-done; serveErr != nil {
t.Fatal(serveErr)
}
if err != nil {
t.Fatal(err)
}
if session.resets.Load() != 0 || session.selected.Load() != 0 || session.verified.Load() != 0 {
t.Fatal("recovery menu invoked password reset")
}
if mode == "query" {
if session.actions.queries != 1 || session.actions.prepares != 0 || session.actions.executes != 0 {
t.Fatal("query replayed")
}
} else {
expected := 1
if mode == "cancel" {
expected = 0
}
if session.actions.prepares != 1 || session.actions.executes != expected || session.actions.queries != 0 {
t.Fatal("wrong menu dispatch")
}
}
if strings.Contains(strings.Join(screen.lines, "|"), strings.Repeat("c", 64)) {
t.Fatal("token printed")
}
})
}
}
@@ -0,0 +1,154 @@
package adminconsole
import (
"context"
"errors"
"strings"
"testing"
"time"
"git.pcux.cn/xuans/Scert/backend/internal/deployment"
"git.pcux.cn/xuans/Scert/backend/internal/localreset"
)
type dispositionConsoleClient struct {
recoveryTestClient
prepares int
executes int
queries int
action deployment.RecoveryAction
executeError bool
expired bool
status string
}
func (c *dispositionConsoleClient) PrepareRecovery(id string, action deployment.RecoveryAction) (localreset.RecoveryPreparation, error) {
c.prepares++
c.action = action
expires := time.Now().Add(time.Minute)
if c.expired {
expires = time.Now().Add(-time.Second)
}
return localreset.RecoveryPreparation{DispositionID: strings.Repeat("b", 64), Token: strings.Repeat("c", 64), Summary: deployment.RecoveryReviewSummary{
TransactionID: id, ServerID: "server-1", Phase: "reload_attempted", Action: action, HistoricalEvidence: true, ObservedAt: time.Now(), ExpiresAt: expires,
}}, nil
}
func (c *dispositionConsoleClient) ExecuteRecovery(id, token string) (deployment.RecoveryDispositionSummary, error) {
c.executes++
if c.executeError {
return deployment.RecoveryDispositionSummary{}, errors.New("connection-lost")
}
return c.result(id), nil
}
func (c *dispositionConsoleClient) GetRecoveryDisposition(id string) (deployment.RecoveryDispositionSummary, error) {
c.queries++
return c.result(id), nil
}
func (c *dispositionConsoleClient) result(id string) deployment.RecoveryDispositionSummary {
now := time.Now().UTC()
status := c.status
if status == "" {
status = "succeeded"
}
codes := map[deployment.RecoveryAction]string{deployment.RecoveryActionRecord: "observed", deployment.RecoveryActionCommit: "committed", deployment.RecoveryActionRollback: "rolled_back"}
code := codes[c.action]
if status == "running" {
code = ""
} else if status != "succeeded" {
code = status
}
return deployment.RecoveryDispositionSummary{ID: id, TransactionID: "tx-1", ServerID: "server-1", Action: c.action, Status: status, ResultCode: code, CreatedAt: now, UpdatedAt: now}
}
func TestDispositionConsoleExplicitActions(t *testing.T) {
for _, row := range []struct {
choice string
action deployment.RecoveryAction
message string
}{
{"1", deployment.RecoveryActionRecord, "核查结论已记录"},
{"2", deployment.RecoveryActionCommit, "本次人工提交核验"},
{"3", deployment.RecoveryActionRollback, "本次恢复基线核验"},
} {
t.Run(row.choice, func(t *testing.T) {
client := &dispositionConsoleClient{}
screen := &recoveryTestScreen{testTerminalInput: testTerminalInput{values: []string{"tx-1", row.choice, "确认 tx-1 " + dispositionActionLabel(row.action)}}}
if err := recoveryDispositionFlow(t.Context(), client, screen); err != nil {
t.Fatal(err)
}
output := strings.Join(screen.lines, "|")
if client.action != row.action || client.prepares != 1 || client.executes != 1 || client.queries != 0 || !strings.Contains(output, row.message) {
t.Fatal("wrong action or result")
}
if strings.Contains(output, strings.Repeat("c", 64)) || !strings.Contains(output, strings.Repeat("b", 64)) || !strings.Contains(output, "禁止重复执行") {
t.Fatal("token leaked or recovery guidance absent")
}
})
}
}
func TestDispositionConsoleCancellationAndUncertainResult(t *testing.T) {
for _, mode := range []string{"quit", "cancel-action", "wrong-confirmation", "expired", "cancelled", "display-error", "disconnect"} {
t.Run(mode, func(t *testing.T) {
ctx, cancel := context.WithCancel(t.Context())
defer cancel()
client := &dispositionConsoleClient{}
screen := &recoveryTestScreen{testTerminalInput: testTerminalInput{values: []string{"tx-1", "2", "确认 tx-1 完成提交"}}}
switch mode {
case "quit":
screen.values[0] = "q"
case "cancel-action":
screen.values[1] = "q"
case "wrong-confirmation":
screen.values[2] = "确认 tx-1 恢复基线"
case "expired":
client.expired = true
case "cancelled":
screen.readHook = func(index int) {
if index == 3 {
cancel()
}
}
case "display-error":
screen.showError = true
case "disconnect":
client.executeError = true
}
_ = recoveryDispositionFlow(ctx, client, screen)
expected := 0
if mode == "disconnect" {
expected = 1
}
if client.executes != expected || client.queries != 0 {
t.Fatalf("unexpected retry or cancelled execution: %d", client.executes)
}
output := strings.Join(screen.lines, "|")
if strings.Contains(output, "本次人工提交核验及结束审计完成") {
t.Fatal("failure reported success")
}
if mode == "disconnect" && !strings.Contains(output, "不能断言未执行或执行成功") {
t.Fatal("missing uncertainty warning")
}
})
}
}
func TestDispositionConsoleQueryOnlyAndStatusWarnings(t *testing.T) {
for _, status := range []string{"running", "uncertain", "rejected", "succeeded"} {
client := &dispositionConsoleClient{status: status, action: deployment.RecoveryActionRecord}
screen := &recoveryTestScreen{testTerminalInput: testTerminalInput{values: []string{strings.Repeat("b", 64)}}}
if err := recoveryResultFlow(t.Context(), client, screen); err != nil {
t.Fatal(err)
}
if client.prepares != 0 || client.executes != 0 || client.queries != 1 {
t.Fatal("query changed state")
}
output := strings.Join(screen.lines, "|")
if (status == "running" || status == "uncertain") && !strings.Contains(output, "禁止重复执行") {
t.Fatal("missing warning")
}
if status == "succeeded" && !strings.Contains(output, "未改变原事务终态") {
t.Fatal("record action falsely claims recovery")
}
}
}