保存本机只读核查入口并开始全量失败修复
本机协议及菜单已实现,Linux专项通过;全量存在环境缺失和SQLite截止时间失败,尚未通过生产验收。
This commit is contained in:
1 parent
c9045a084f
commit
978f023a7f
23 files changed
+1386
-20
No files matched your search
@@ -57,6 +57,23 @@ func runConnected(ctx context.Context, conn net.Conn, screen terminal) error {
|
||||
return localreset.ErrUnavailable
|
||||
}
|
||||
defer client.Close()
|
||||
if client.RecoveryAvailable() {
|
||||
choice, err := screen.Read(ctx, "本机管理:1 重置超级管理员密码;2 只读核查部署事务;其他输入退出", false)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
switch strings.TrimSpace(choice) {
|
||||
case "1":
|
||||
case "2":
|
||||
if err := recoveryFlow(ctx, client, screen); err != nil {
|
||||
_ = screen.Show("部署核查未完成:会话过期、输入无效或现场不可读取。未修改事务或远端文件。请重新启动本机管理。")
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
if err := resetFlow(ctx, client, screen); err != nil {
|
||||
_ = screen.Show("密码重置未完成:操作已取消、验证失败、会话过期或保存失败。请检查服务日志后重新启动。")
|
||||
return localreset.ErrProtocol
|
||||
|
||||
@@ -0,0 +1,132 @@
|
||||
package adminconsole
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"unicode"
|
||||
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/deployment"
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/localreset"
|
||||
)
|
||||
|
||||
type recoveryClient interface {
|
||||
ListRecoveries(string) (deployment.PendingRecoveryPage, error)
|
||||
InspectRecovery(string) (deployment.RecoveryInspection, error)
|
||||
}
|
||||
|
||||
func recoveryFlow(ctx context.Context, client recoveryClient, screen terminal) error {
|
||||
if err := screen.Show("只读核查:不写入文件、不执行远端命令、不删锁、不改变事务状态。结果是当前采样,不证明历史部署成功。每页显示 5 条。"); err != nil {
|
||||
return err
|
||||
}
|
||||
cursor := ""
|
||||
for {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
page, err := client.ListRecoveries(cursor)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(page.Items) == 0 {
|
||||
return screen.Show("当前没有更多待核查的部署事务。")
|
||||
}
|
||||
for index, item := range page.Items {
|
||||
line := fmt.Sprintf("%d. 事务:%s;服务器:%s;阶段:%s;状态:%s;历史证据:%s", index+1,
|
||||
terminalValue(item.ID), terminalValue(item.ServerID), recoveryLabel(item.Phase), recoveryLabel(item.Status), evidenceLabel(item.HistoricalEvidence))
|
||||
if err := screen.Show(line); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
choice, err := screen.Read(ctx, "输入本页序号进行只读核查;输入 n 查看下一页;输入 q 退出", false)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := ctx.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
switch strings.TrimSpace(choice) {
|
||||
case "q":
|
||||
return nil
|
||||
case "n":
|
||||
if page.NextCursor == "" {
|
||||
return screen.Show("已到最后一页;本次未执行任何写入操作。")
|
||||
}
|
||||
cursor = page.NextCursor
|
||||
continue
|
||||
}
|
||||
index, err := strconv.Atoi(strings.TrimSpace(choice))
|
||||
if err != nil || index < 1 || index > len(page.Items) {
|
||||
return localreset.ErrProtocol
|
||||
}
|
||||
result, err := client.InspectRecovery(page.Items[index-1].ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return showRecovery(screen, result)
|
||||
}
|
||||
}
|
||||
|
||||
func showRecovery(screen terminal, result deployment.RecoveryInspection) error {
|
||||
lines := []string{
|
||||
fmt.Sprintf("事务:%s;服务器:%s;采样时间:%s", terminalValue(result.TransactionID), terminalValue(result.ServerID), result.ObservedAt.UTC().Format("2006-01-02 15:04:05 UTC")),
|
||||
"历史证据:" + evidenceLabel(result.HistoricalEvidence),
|
||||
}
|
||||
for _, file := range result.Files {
|
||||
exists := "不存在"
|
||||
if file.Exists {
|
||||
exists = "存在"
|
||||
}
|
||||
lines = append(lines, fmt.Sprintf("%s:%s;%s;比较:%s;摘要:%s", recoveryLabel(file.Kind), terminalValue(file.Path), exists, recoveryLabel(file.Comparison), terminalValue(file.Digest)))
|
||||
}
|
||||
for _, lock := range result.Locks {
|
||||
exists := "不存在"
|
||||
if lock.Exists {
|
||||
exists = "存在"
|
||||
}
|
||||
lines = append(lines, fmt.Sprintf("锁:%s;%s;归属:%s", terminalValue(lock.Path), exists, recoveryLabel(lock.Ownership)))
|
||||
}
|
||||
lines = append(lines, "只读采样已完成,未提交或回滚事务;缺少历史证据不能视为历史部署成功。")
|
||||
for _, line := range lines {
|
||||
if err := screen.Show(line); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func evidenceLabel(present bool) string {
|
||||
if present {
|
||||
return "记录已保存(不代表本次部署通过验收)"
|
||||
}
|
||||
return "缺失,禁止据此认定历史成功"
|
||||
}
|
||||
|
||||
func terminalValue(value string) string {
|
||||
var output strings.Builder
|
||||
for _, c := range value {
|
||||
if unicode.IsControl(c) || unicode.Is(unicode.Cf, c) || c == '\u2028' || c == '\u2029' {
|
||||
output.WriteString(fmt.Sprintf("\\u%04x", c))
|
||||
} else {
|
||||
output.WriteRune(c)
|
||||
}
|
||||
}
|
||||
return output.String()
|
||||
}
|
||||
|
||||
func recoveryLabel(value string) string {
|
||||
labels := map[string]string{
|
||||
"unrecorded": "无历史摘要", "unproven": "归属无法证明",
|
||||
"pending": "待处理", "recovery_required": "需要恢复", "locked": "已锁定", "acquiring": "取得锁中",
|
||||
"uploaded": "已上传", "replacing": "替换中", "installed": "已安装", "reload_attempted": "已尝试重载",
|
||||
"committing": "提交中", "commit_verified": "提交已核验", "rollback_verified": "回滚已核验",
|
||||
"committed": "已提交", "rolled_back": "已回滚", "target": "目标文件", "temporary": "临时文件", "backup": "备份文件",
|
||||
"match": "匹配", "mismatch": "不匹配", "missing": "缺失", "unknown": "未知", "unverifiable": "无法验证",
|
||||
"owned": "属于本事务", "foreign": "属于其他事务", "ownerless": "缺少归属", "absent": "不存在",
|
||||
}
|
||||
if label, ok := labels[value]; ok {
|
||||
return label
|
||||
}
|
||||
return "未识别(" + terminalValue(value) + ")"
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
//go:build linux
|
||||
|
||||
package adminconsole
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/deployment"
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/localreset"
|
||||
)
|
||||
|
||||
type consoleIPCSession struct {
|
||||
selected atomic.Int32
|
||||
verified atomic.Int32
|
||||
resets atomic.Int32
|
||||
inspected atomic.Int32
|
||||
}
|
||||
|
||||
func (*consoleIPCSession) Methods() []localreset.Method {
|
||||
return []localreset.Method{{ID: "email", Label: "绑定邮箱验证码"}}
|
||||
}
|
||||
func (s *consoleIPCSession) Select(context.Context, string) error { s.selected.Add(1); return nil }
|
||||
func (s *consoleIPCSession) Verify(context.Context, string) error { s.verified.Add(1); return nil }
|
||||
func (s *consoleIPCSession) Reset(context.Context, string) error { s.resets.Add(1); return nil }
|
||||
func (*consoleIPCSession) Close() {}
|
||||
func (*consoleIPCSession) RecoveryAvailable() bool { return true }
|
||||
func (*consoleIPCSession) ListRecoveries(context.Context, string) (deployment.PendingRecoveryPage, error) {
|
||||
return deployment.PendingRecoveryPage{Items: []deployment.PendingRecovery{{ID: "tx-1", ServerID: "server-1"}}}, nil
|
||||
}
|
||||
func (s *consoleIPCSession) InspectRecovery(_ context.Context, id string) (deployment.RecoveryInspection, error) {
|
||||
s.inspected.Add(1)
|
||||
return deployment.RecoveryInspection{TransactionID: id, ServerID: "server-1", ObservedAt: time.Now(),
|
||||
Files: make([]deployment.RecoveryFileObservation, 9), Locks: make([]deployment.RecoveryLockObservation, 3)}, nil
|
||||
}
|
||||
|
||||
func TestRecoveryConsoleOverProtectedUnixSocket(t *testing.T) {
|
||||
for _, action := range []string{"inspect", "reset", "quit"} {
|
||||
t.Run(action, func(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "control.sock")
|
||||
server, err := localreset.Listen(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer server.Close()
|
||||
info, err := os.Lstat(path)
|
||||
if err != nil || info.Mode().Perm() != 0600 {
|
||||
t.Fatal("unprotected socket")
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(t.Context(), 5*time.Second)
|
||||
defer cancel()
|
||||
session := &consoleIPCSession{}
|
||||
done := make(chan error, 1)
|
||||
go func() {
|
||||
done <- server.Serve(ctx, func(context.Context, localreset.Peer) (localreset.Session, error) { return session, nil })
|
||||
}()
|
||||
t.Cleanup(func() {
|
||||
cancel()
|
||||
if err := <-done; err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
})
|
||||
conn, err := localreset.Dial(ctx, path, uint32(os.Geteuid()))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
screen := &recoveryTestScreen{}
|
||||
switch action {
|
||||
case "inspect":
|
||||
screen.values = []string{"2", "1"}
|
||||
case "reset":
|
||||
screen.values = []string{"1", "1", "01234567", "NewPass123", "NewPass123"}
|
||||
case "quit":
|
||||
screen.values = []string{"q"}
|
||||
}
|
||||
if err := runConnected(ctx, conn, screen); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if action == "reset" {
|
||||
if session.resets.Load() != 1 || session.selected.Load() != 1 || session.verified.Load() != 1 || session.inspected.Load() != 0 {
|
||||
t.Fatal("reset verification flow changed")
|
||||
}
|
||||
} else {
|
||||
if session.resets.Load() != 0 || session.selected.Load() != 0 || session.verified.Load() != 0 {
|
||||
t.Fatal("non-reset menu invoked reset")
|
||||
}
|
||||
}
|
||||
if action == "inspect" && (session.inspected.Load() != 1 || !strings.Contains(strings.Join(screen.lines, "|"), "只读采样已完成")) {
|
||||
t.Fatal("inspection did not reach console")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
package adminconsole
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/deployment"
|
||||
)
|
||||
|
||||
type recoveryTestClient struct {
|
||||
cursors []string
|
||||
inspected []string
|
||||
err error
|
||||
}
|
||||
|
||||
func (c *recoveryTestClient) ListRecoveries(cursor string) (deployment.PendingRecoveryPage, error) {
|
||||
c.cursors = append(c.cursors, cursor)
|
||||
if cursor == "" {
|
||||
return deployment.PendingRecoveryPage{Items: []deployment.PendingRecovery{{ID: "tx-1", ServerID: "server-1"}}, NextCursor: "tx-1"}, nil
|
||||
}
|
||||
return deployment.PendingRecoveryPage{Items: []deployment.PendingRecovery{{ID: "tx-2", ServerID: "server-2"}}}, nil
|
||||
}
|
||||
|
||||
func (c *recoveryTestClient) InspectRecovery(id string) (deployment.RecoveryInspection, error) {
|
||||
c.inspected = append(c.inspected, id)
|
||||
return deployment.RecoveryInspection{TransactionID: id, ServerID: "server-2", ObservedAt: time.Now(),
|
||||
Files: []deployment.RecoveryFileObservation{{Path: "/ssl/\x1b[2J\n\u202efile", Kind: "target", Comparison: "unrecorded"}}}, c.err
|
||||
}
|
||||
|
||||
type recoveryTestScreen struct {
|
||||
testTerminalInput
|
||||
lines []string
|
||||
showError bool
|
||||
}
|
||||
|
||||
func (s *recoveryTestScreen) Show(line string) error {
|
||||
if s.showError {
|
||||
return errors.New("display-unavailable")
|
||||
}
|
||||
s.lines = append(s.lines, line)
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestRecoveryConsolePaginationAndSafeDisplay(t *testing.T) {
|
||||
client := &recoveryTestClient{}
|
||||
screen := &recoveryTestScreen{testTerminalInput: testTerminalInput{values: []string{"n", "1"}}}
|
||||
if err := recoveryFlow(t.Context(), client, screen); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(client.cursors) != 2 || client.cursors[1] != "tx-1" || len(client.inspected) != 1 || client.inspected[0] != "tx-2" {
|
||||
t.Fatal("pagination or selection incorrect")
|
||||
}
|
||||
output := strings.Join(screen.lines, "|")
|
||||
if strings.ContainsAny(output, "\x1b\n\u202e") || !strings.Contains(output, "\\u001b") ||
|
||||
!strings.Contains(output, "缺失,禁止据此认定历史成功") || !strings.Contains(output, "未提交或回滚事务") {
|
||||
t.Fatal("unsafe display or missing evidence warning")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRecoveryConsoleCancellationAndFailures(t *testing.T) {
|
||||
for _, scenario := range []string{"quit", "invalid", "cancelled", "display-error", "inspection-error"} {
|
||||
t.Run(scenario, func(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(t.Context())
|
||||
defer cancel()
|
||||
client := &recoveryTestClient{}
|
||||
screen := &recoveryTestScreen{testTerminalInput: testTerminalInput{values: []string{"1"}}}
|
||||
switch scenario {
|
||||
case "quit":
|
||||
screen.values[0] = "q"
|
||||
case "invalid":
|
||||
screen.values[0] = "0"
|
||||
case "cancelled":
|
||||
screen.readHook = func(int) { cancel() }
|
||||
case "display-error":
|
||||
screen.showError = true
|
||||
case "inspection-error":
|
||||
client.err = errors.New("unavailable")
|
||||
}
|
||||
err := recoveryFlow(ctx, client, screen)
|
||||
if (err == nil) != (scenario == "quit") {
|
||||
t.Fatalf("unexpected result: %v", err)
|
||||
}
|
||||
if scenario != "inspection-error" && len(client.inspected) != 0 {
|
||||
t.Fatal("cancelled operation inspected")
|
||||
}
|
||||
if strings.Contains(strings.Join(screen.lines, "|"), "只读采样已完成") {
|
||||
t.Fatal("failure reported success")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user