merge: integrate final ticket 07 recovery
This commit is contained in:
+42
-10
@@ -80,9 +80,9 @@ func (m *Maintenance) Doctor(ctx context.Context, applicationChecks []target.Che
|
||||
active := activeFromState(m.state)
|
||||
for _, adapter := range m.targets {
|
||||
availability := adapter.Availability(ctx)
|
||||
report.Diagnostics = append(report.Diagnostics, Diagnostic{TargetID: adapter.ID(), Check: target.Check{Name: "Target availability", OK: availability.Available, Evidence: availability.Evidence}})
|
||||
report.Diagnostics = append(report.Diagnostics, Diagnostic{TargetID: adapter.ID(), Check: target.Check{Name: "Target availability", OK: availability.Available, Evidence: profile.RedactText(availability.Evidence)}})
|
||||
inspection := adapter.Inspect(ctx, active)
|
||||
report.Diagnostics = append(report.Diagnostics, Diagnostic{TargetID: adapter.ID(), Check: target.Check{Name: "detected state", OK: inspection.State != target.Drifted, Evidence: string(inspection.State) + ": " + inspection.Evidence}})
|
||||
report.Diagnostics = append(report.Diagnostics, Diagnostic{TargetID: adapter.ID(), Check: target.Check{Name: "detected state", OK: inspection.State != target.Drifted, Evidence: profile.RedactText(string(inspection.State) + ": " + inspection.Evidence)}})
|
||||
if diagnosable, ok := adapter.(target.Diagnosable); ok {
|
||||
for _, check := range diagnosable.DiagnosticChecks(ctx, active) {
|
||||
check.Evidence = profile.RedactText(check.Evidence)
|
||||
@@ -94,10 +94,12 @@ func (m *Maintenance) Doctor(ctx context.Context, applicationChecks []target.Che
|
||||
}
|
||||
|
||||
type ResetTargetResult struct {
|
||||
TargetID string
|
||||
Inspection target.Inspection
|
||||
Skipped bool
|
||||
Err error
|
||||
TargetID string
|
||||
Inspection target.Inspection
|
||||
Skipped bool
|
||||
OperationErr error
|
||||
LogErr error
|
||||
Err error
|
||||
}
|
||||
type ResetResult struct {
|
||||
Cancelled bool
|
||||
@@ -105,6 +107,23 @@ type ResetResult struct {
|
||||
Err error
|
||||
}
|
||||
|
||||
// ResetConfirmation performs only read-only inspection and returns every
|
||||
// warning required by Targets that a reset would attempt to disable.
|
||||
func (m *Maintenance) ResetConfirmation(ctx context.Context) string {
|
||||
active := activeFromState(m.state)
|
||||
var warnings []string
|
||||
for _, adapter := range m.targets {
|
||||
inspection := adapter.Inspect(ctx, active)
|
||||
if inspection.State != target.Enabled && inspection.State != target.Drifted {
|
||||
continue
|
||||
}
|
||||
if warning := adapter.Confirmation(false); warning != "" {
|
||||
warnings = append(warnings, adapter.ID()+": "+warning)
|
||||
}
|
||||
}
|
||||
return strings.Join(warnings, "\n")
|
||||
}
|
||||
|
||||
func (m *Maintenance) Reset(ctx context.Context, confirmed bool) ResetResult {
|
||||
if !confirmed {
|
||||
return ResetResult{Cancelled: true}
|
||||
@@ -126,17 +145,30 @@ func (m *Maintenance) Reset(ctx context.Context, confirmed bool) ResetResult {
|
||||
continue
|
||||
}
|
||||
operation := adapter.SetDesired(ctx, false, active)
|
||||
detail, outcome := operation.Inspection.Evidence, "success"
|
||||
finalInspection := operation.Inspection
|
||||
detail, outcome := finalInspection.Evidence, "success"
|
||||
if operation.Err != nil {
|
||||
// OperationResult inspection is explicitly advisory on failure. Reset
|
||||
// must publish and log a fresh observation just like interactive
|
||||
// Target operations do.
|
||||
finalInspection = adapter.Inspect(ctx, active)
|
||||
outcome = "failure"
|
||||
detail = operation.Err.Error()
|
||||
detail = fmt.Sprintf("%v; fresh inspection %s: %s", operation.Err, finalInspection.State, finalInspection.Evidence)
|
||||
result.Err = errors.Join(result.Err, fmt.Errorf("reset %s Target: %w", adapter.ID(), redactError(operation.Err)))
|
||||
}
|
||||
detail = RedactLogDetail(detail)
|
||||
operationErr := redactError(operation.Err)
|
||||
var logErr error
|
||||
if m.logger != nil {
|
||||
_ = m.logger.Write(LogEntry{Time: time.Now(), Operation: "reset", Target: adapter.ID(), Result: outcome, Detail: detail})
|
||||
if err := m.logger.Write(LogEntry{Time: time.Now(), Operation: "reset", Target: adapter.ID(), Result: outcome, Detail: detail}); err != nil {
|
||||
logErr = redactError(err)
|
||||
result.Err = errors.Join(result.Err, fmt.Errorf("record reset %s Target operation log: %w", adapter.ID(), logErr))
|
||||
}
|
||||
}
|
||||
result.Targets = append(result.Targets, ResetTargetResult{TargetID: adapter.ID(), Inspection: operation.Inspection, Err: redactError(operation.Err)})
|
||||
result.Targets = append(result.Targets, ResetTargetResult{
|
||||
TargetID: adapter.ID(), Inspection: finalInspection,
|
||||
OperationErr: operationErr, LogErr: logErr, Err: errors.Join(operationErr, logErr),
|
||||
})
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ type maintenanceTarget struct {
|
||||
checks []target.Check
|
||||
disableErr error
|
||||
disableCalls int
|
||||
confirmation string
|
||||
}
|
||||
|
||||
func (f *maintenanceTarget) ID() string { return f.id }
|
||||
@@ -26,7 +27,7 @@ func (f *maintenanceTarget) Description() target.Description {
|
||||
return target.Description{Name: strings.ToUpper(f.id)}
|
||||
}
|
||||
func (f *maintenanceTarget) Availability(context.Context) target.Availability { return f.available }
|
||||
func (f *maintenanceTarget) Confirmation(bool) string { return "" }
|
||||
func (f *maintenanceTarget) Confirmation(bool) string { return f.confirmation }
|
||||
func (f *maintenanceTarget) Inspect(context.Context, *profile.Profile) target.Inspection {
|
||||
return f.inspection
|
||||
}
|
||||
@@ -43,6 +44,28 @@ func (f *maintenanceTarget) DiagnosticChecks(context.Context, *profile.Profile)
|
||||
return append([]target.Check(nil), f.checks...)
|
||||
}
|
||||
|
||||
type staleFailureTarget struct{ inspectCalls int }
|
||||
|
||||
func (*staleFailureTarget) ID() string { return "stale" }
|
||||
func (*staleFailureTarget) Description() target.Description { return target.Description{Name: "stale"} }
|
||||
func (*staleFailureTarget) Availability(context.Context) target.Availability {
|
||||
return target.Availability{Available: true}
|
||||
}
|
||||
func (*staleFailureTarget) Confirmation(bool) string { return "" }
|
||||
func (f *staleFailureTarget) Inspect(context.Context, *profile.Profile) target.Inspection {
|
||||
f.inspectCalls++
|
||||
if f.inspectCalls == 1 {
|
||||
return target.Inspection{State: target.Enabled, Evidence: "initial managed state"}
|
||||
}
|
||||
return target.Inspection{State: target.External, Evidence: "fresh evidence http://alice:secret@proxy.example:8080"}
|
||||
}
|
||||
func (*staleFailureTarget) SetDesired(context.Context, bool, *profile.Profile) target.OperationResult {
|
||||
return target.OperationResult{
|
||||
Inspection: target.Inspection{State: target.Drifted, Evidence: "stale returned evidence"},
|
||||
Err: errors.New("failed https://bob:hunter2@proxy.example/path"),
|
||||
}
|
||||
}
|
||||
|
||||
type recordingLock struct{ held, acquired int }
|
||||
|
||||
func (l *recordingLock) Lock(context.Context) (func(), error) {
|
||||
@@ -51,11 +74,14 @@ func (l *recordingLock) Lock(context.Context) (func(), error) {
|
||||
return func() { l.held-- }, nil
|
||||
}
|
||||
|
||||
type recordingLog struct{ entries []app.LogEntry }
|
||||
type recordingLog struct {
|
||||
entries []app.LogEntry
|
||||
err error
|
||||
}
|
||||
|
||||
func (l *recordingLog) Write(entry app.LogEntry) error {
|
||||
l.entries = append(l.entries, entry)
|
||||
return nil
|
||||
return l.err
|
||||
}
|
||||
|
||||
func TestDoctorReportsReadOnlyChecksEvidenceAndRedactsCredentials(t *testing.T) {
|
||||
@@ -111,6 +137,57 @@ func TestApplicationSerializesAndLogsTargetMutations(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestResetFailureUsesFreshInspectionForResultAndRedactedLog(t *testing.T) {
|
||||
adapter := &staleFailureTarget{}
|
||||
log := &recordingLog{}
|
||||
result := app.NewMaintenance(profile.State{}, []target.Target{adapter}, &recordingLock{}, log).Reset(context.Background(), true)
|
||||
if result.Err == nil || adapter.inspectCalls != 2 || len(result.Targets) != 1 {
|
||||
t.Fatalf("reset result=%#v inspect calls=%d", result, adapter.inspectCalls)
|
||||
}
|
||||
got := result.Targets[0].Inspection
|
||||
if got.State != target.External || !strings.Contains(got.Evidence, "fresh evidence") || strings.Contains(got.Evidence, "stale") {
|
||||
t.Fatalf("failure inspection = %#v", got)
|
||||
}
|
||||
if len(log.entries) != 1 || !strings.Contains(log.entries[0].Detail, "fresh inspection External") || strings.Contains(log.entries[0].Detail, "secret") || strings.Contains(log.entries[0].Detail, "hunter2") {
|
||||
t.Fatalf("failure log = %#v", log.entries)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResetConfirmationAggregatesWarningsOnlyForManagedCandidatesWithoutMutation(t *testing.T) {
|
||||
managed := &maintenanceTarget{id: "managed", inspection: target.Inspection{State: target.Enabled, Evidence: "managed"}}
|
||||
drifted := &maintenanceTarget{id: "drifted", inspection: target.Inspection{State: target.Drifted, Evidence: "recovery metadata remains"}}
|
||||
external := &maintenanceTarget{id: "external", inspection: target.Inspection{State: target.External, Evidence: "administrator owned"}}
|
||||
managed.confirmation = "managed warning"
|
||||
drifted.confirmation = "drifted warning"
|
||||
external.confirmation = "must not be shown"
|
||||
maintenance := app.NewMaintenance(profile.State{}, []target.Target{managed, drifted, external}, &recordingLock{}, nil)
|
||||
|
||||
confirmation := maintenance.ResetConfirmation(context.Background())
|
||||
for _, want := range []string{"managed: managed warning", "drifted: drifted warning"} {
|
||||
if !strings.Contains(confirmation, want) {
|
||||
t.Errorf("confirmation missing %q: %q", want, confirmation)
|
||||
}
|
||||
}
|
||||
if strings.Contains(confirmation, "must not be shown") || managed.disableCalls+drifted.disableCalls+external.disableCalls != 0 {
|
||||
t.Fatalf("confirmation=%q mutation calls=%d/%d/%d", confirmation, managed.disableCalls, drifted.disableCalls, external.disableCalls)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResetLoggerFailureIsReportedWithoutUndoingCompletedConfiguration(t *testing.T) {
|
||||
adapter := &maintenanceTarget{id: "managed", inspection: target.Inspection{State: target.Enabled, Evidence: "managed"}}
|
||||
logger := &recordingLog{err: errors.New("operation log disk full")}
|
||||
|
||||
result := app.NewMaintenance(profile.State{}, []target.Target{adapter}, &recordingLock{}, logger).Reset(context.Background(), true)
|
||||
|
||||
if result.Err == nil || !strings.Contains(result.Err.Error(), "operation log disk full") || len(result.Targets) != 1 {
|
||||
t.Fatalf("reset result = %#v", result)
|
||||
}
|
||||
item := result.Targets[0]
|
||||
if item.Inspection.State != target.Disabled || item.OperationErr != nil || item.LogErr == nil || item.Err == nil || adapter.disableCalls != 1 {
|
||||
t.Fatalf("logger failure outcome=%#v disable calls=%d", item, adapter.disableCalls)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResetRequiresConfirmationAndReportsTruthfulPartialResults(t *testing.T) {
|
||||
good := &maintenanceTarget{id: "good", available: target.Availability{Available: true}, inspection: target.Inspection{State: target.Enabled, Evidence: "managed"}}
|
||||
failure := errors.New("remove https://alice:secret@proxy.example failed: permission denied")
|
||||
|
||||
Reference in New Issue
Block a user