保存本机只读核查入口并开始全量失败修复
本机协议及菜单已实现,Linux专项通过;全量存在环境缺失和SQLite截止时间失败,尚未通过生产验收。
This commit is contained in:
1 parent
c9045a084f
commit
978f023a7f
23 files changed
+1386
-20
No files matched your search
@@ -0,0 +1,78 @@
|
||||
package deployment
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
const RecoveryPageSize = 5
|
||||
|
||||
type PendingRecovery struct {
|
||||
ID string `json:"id"`
|
||||
ServerID string `json:"server_id"`
|
||||
Phase string `json:"phase"`
|
||||
Status string `json:"status"`
|
||||
HistoricalEvidence bool `json:"historical_evidence"`
|
||||
}
|
||||
|
||||
type PendingRecoveryPage struct {
|
||||
Items []PendingRecovery `json:"items"`
|
||||
NextCursor string `json:"next_cursor,omitempty"`
|
||||
}
|
||||
|
||||
// ListPendingRecoveries 只返回有界摘要,不返回现场路径、错误详情、凭据或恢复证据内容。
|
||||
// 游标按事务标识排序;分页期间现场可能变化,不提供一致性快照承诺。
|
||||
func (s *Service) ListPendingRecoveries(ctx context.Context, after string) (PendingRecoveryPage, error) {
|
||||
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
defer cancel()
|
||||
if err := ctx.Err(); err != nil {
|
||||
return PendingRecoveryPage{}, err
|
||||
}
|
||||
if s == nil || s.journal == nil {
|
||||
return PendingRecoveryPage{}, ErrRecoveryInspectionUnavailable
|
||||
}
|
||||
if after != "" && !validInspectionID(after) {
|
||||
return PendingRecoveryPage{}, ErrRecoveryInspectionRecord
|
||||
}
|
||||
records, err := s.journal.ListPendingDeploymentTransactions(ctx)
|
||||
if err != nil {
|
||||
return PendingRecoveryPage{}, inspectionError(ctx, ErrRecoveryInspectionUnavailable)
|
||||
}
|
||||
if err := ctx.Err(); err != nil {
|
||||
return PendingRecoveryPage{}, err
|
||||
}
|
||||
items := make([]PendingRecovery, 0, len(records))
|
||||
seen := make(map[string]bool, len(records))
|
||||
for _, record := range records {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return PendingRecoveryPage{}, err
|
||||
}
|
||||
if !validInspectionID(record.ID) || seen[record.ID] || !validRecoverySummary(record.ServerID) ||
|
||||
(record.Phase != "" && !validRecoverySummary(record.Phase)) ||
|
||||
(record.Status != TransactionStatusPending && record.Status != TransactionStatusRecoveryRequired) {
|
||||
return PendingRecoveryPage{}, ErrRecoveryInspectionRecord
|
||||
}
|
||||
seen[record.ID] = true
|
||||
if record.ID > after {
|
||||
items = append(items, PendingRecovery{ID: record.ID, ServerID: record.ServerID,
|
||||
Phase: record.Phase, Status: record.Status, HistoricalEvidence: record.Evidence != nil})
|
||||
}
|
||||
}
|
||||
sort.Slice(items, func(i, j int) bool { return items[i].ID < items[j].ID })
|
||||
if err := ctx.Err(); err != nil {
|
||||
return PendingRecoveryPage{}, err
|
||||
}
|
||||
page := PendingRecoveryPage{Items: items}
|
||||
if len(items) > RecoveryPageSize {
|
||||
page.Items = items[:RecoveryPageSize]
|
||||
page.NextCursor = page.Items[len(page.Items)-1].ID
|
||||
}
|
||||
return page, nil
|
||||
}
|
||||
|
||||
func validRecoverySummary(value string) bool {
|
||||
return len(value) > 0 && len(value) <= 160 && !strings.ContainsFunc(value, unicode.IsControl)
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package deployment
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type cancelledRecoveryListJournal struct {
|
||||
memoryTransactionJournal
|
||||
cancel context.CancelFunc
|
||||
}
|
||||
|
||||
func (j *cancelledRecoveryListJournal) ListPendingDeploymentTransactions(context.Context) ([]TransactionRecord, error) {
|
||||
j.cancel()
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func TestRecoveryListCancellationAfterEmptyRead(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(t.Context())
|
||||
defer cancel()
|
||||
journal := &cancelledRecoveryListJournal{cancel: cancel}
|
||||
page, err := NewServiceWithJournal(nil, journal).ListPendingRecoveries(ctx, "")
|
||||
if !errors.Is(err, context.Canceled) || page.Items != nil {
|
||||
t.Fatal("cancelled empty result accepted")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRecoveryListLegacyWithoutPhase(t *testing.T) {
|
||||
journal := &memoryTransactionJournal{pending: []TransactionRecord{{ID: "legacy", ServerID: "server-1", Status: TransactionStatusRecoveryRequired}}}
|
||||
page, err := NewServiceWithJournal(nil, journal).ListPendingRecoveries(t.Context(), "")
|
||||
if err != nil || len(page.Items) != 1 || page.Items[0].HistoricalEvidence {
|
||||
t.Fatal("legacy record omitted or evidence invented")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
package deployment
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestRecoveryListPaginationAndRedaction(t *testing.T) {
|
||||
journal := &memoryTransactionJournal{}
|
||||
for i := 12; i > 0; i-- {
|
||||
journal.pending = append(journal.pending, TransactionRecord{
|
||||
ID: fmt.Sprintf("tx-%02d", i), ServerID: "server-1", Phase: TransactionPhaseInstalled,
|
||||
Status: TransactionStatusRecoveryRequired, Message: "secret-message", ServerName: "secret-name",
|
||||
Paths: []string{"secret-path"}, Evidence: &RecoveryEvidence{},
|
||||
})
|
||||
}
|
||||
factory := &inspectionFactory{}
|
||||
service := NewServiceWithJournal(factory, journal)
|
||||
cursor := ""
|
||||
var ids []string
|
||||
for _, size := range []int{5, 5, 2} {
|
||||
page, err := service.ListPendingRecoveries(t.Context(), cursor)
|
||||
if err != nil || len(page.Items) != size {
|
||||
t.Fatalf("page: %+v, %v", page, err)
|
||||
}
|
||||
data, _ := json.Marshal(page)
|
||||
if strings.Contains(string(data), "secret-") || len(data) > 16<<10 {
|
||||
t.Fatal("summary leaked data or exceeded frame")
|
||||
}
|
||||
for _, item := range page.Items {
|
||||
ids = append(ids, item.ID)
|
||||
if !item.HistoricalEvidence {
|
||||
t.Fatal("evidence presence lost")
|
||||
}
|
||||
}
|
||||
cursor = page.NextCursor
|
||||
}
|
||||
if cursor != "" || len(ids) != 12 || ids[0] != "tx-01" || ids[11] != "tx-12" ||
|
||||
factory.calls != 0 || len(journal.updated) != 0 || len(journal.finished) != 0 {
|
||||
t.Fatalf("list mutated state, connected, or lost ordering: %v", ids)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRecoveryListRejectsInvalidRecords(t *testing.T) {
|
||||
for _, scenario := range []string{"duplicate", "id", "server", "phase", "status", "cursor", "cancelled"} {
|
||||
t.Run(scenario, func(t *testing.T) {
|
||||
record := TransactionRecord{ID: "tx-1", ServerID: "server-1", Phase: TransactionPhaseInstalled, Status: TransactionStatusPending}
|
||||
journal := &memoryTransactionJournal{}
|
||||
ctx, cancel := context.WithCancel(t.Context())
|
||||
defer cancel()
|
||||
cursor := ""
|
||||
switch scenario {
|
||||
case "duplicate":
|
||||
journal.pending = append(journal.pending, record)
|
||||
case "id":
|
||||
record.ID = "../bad"
|
||||
case "server":
|
||||
record.ServerID = strings.Repeat("x", 161)
|
||||
case "phase":
|
||||
record.Phase = "\x1b[2J"
|
||||
case "status":
|
||||
record.Status = TransactionStatusCommitted
|
||||
case "cursor":
|
||||
cursor = "../bad"
|
||||
case "cancelled":
|
||||
cancel()
|
||||
}
|
||||
journal.pending = append(journal.pending, record)
|
||||
page, err := NewServiceWithJournal(nil, journal).ListPendingRecoveries(ctx, cursor)
|
||||
if err == nil || !reflect.DeepEqual(page, PendingRecoveryPage{}) {
|
||||
t.Fatal("invalid list returned data")
|
||||
}
|
||||
if scenario == "cancelled" && !errors.Is(err, context.Canceled) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user