feat: add diagnostics and safe reset maintenance

This commit is contained in:
proxyctl coordinator
2026-07-17 03:28:29 +08:00
parent 3003450dce
commit d4c76e4b3c
13 changed files with 1041 additions and 23 deletions
+128
View File
@@ -0,0 +1,128 @@
package main
import (
"bytes"
"context"
"errors"
"os"
"path/filepath"
"strings"
"testing"
"proxyctl/internal/operationlock"
"proxyctl/internal/oplog"
"proxyctl/internal/profile"
"proxyctl/internal/target"
)
type processFakeTarget struct {
id string
state target.State
err error
calls int
}
func (f *processFakeTarget) ID() string { return f.id }
func (f *processFakeTarget) Description() target.Description { return target.Description{Name: f.id} }
func (f *processFakeTarget) Availability(context.Context) target.Availability {
return target.Availability{Available: true, Evidence: "fake executable found"}
}
func (f *processFakeTarget) Inspect(context.Context, *profile.Profile) target.Inspection {
return target.Inspection{State: f.state, Evidence: "fake evidence for http://alice:secret@proxy.test"}
}
func (f *processFakeTarget) SetDesired(context.Context, bool, *profile.Profile) target.OperationResult {
f.calls++
if f.err == nil {
f.state = target.Disabled
}
return target.OperationResult{Inspection: target.Inspection{State: f.state, Evidence: "re-inspected fake state"}, Err: f.err}
}
func (f *processFakeTarget) DiagnosticChecks(context.Context, *profile.Profile) []target.Check {
return []target.Check{{Name: "required commands", OK: true, Evidence: "fake helper found"}, {Name: "write authority", OK: true, Evidence: "fake location writable"}}
}
func TestDoctorCommandUsesFakeTargetsAndIsolatedXDGWithoutMutation(t *testing.T) {
xdg := filepath.Join(t.TempDir(), "xdg-config")
store := profile.NewFileStore(filepath.Join(xdg, "proxyctl", "profiles.json"))
fake := &processFakeTarget{id: "fake", state: target.External}
var output bytes.Buffer
if err := runMaintenanceCommand([]string{"doctor"}, strings.NewReader(""), &output, store, []target.Target{fake}, nil, nil, []target.Check{{Name: "identity resolution", OK: true, Evidence: "fake user"}, {Name: "storage permissions", OK: true, Evidence: xdg}}); err != nil {
t.Fatal(err)
}
text := output.String()
for _, want := range []string{"Profile validity", "identity resolution", "storage permissions", "Target availability", "External", "required commands", "write authority"} {
if !strings.Contains(text, want) {
t.Errorf("doctor output missing %q: %s", want, text)
}
}
if strings.Contains(text, "secret") {
t.Fatalf("doctor leaked credentials: %s", text)
}
if fake.calls != 0 {
t.Fatalf("doctor mutated fake Target %d times", fake.calls)
}
if _, err := os.Stat(xdg); !os.IsNotExist(err) {
t.Fatalf("doctor created isolated XDG path: %v", err)
}
}
func TestDoctorStoragePermissionCheckIsReadOnly(t *testing.T) {
path := filepath.Join(t.TempDir(), "proxyctl", "profiles.json")
writeConfig(t, path, `{"profiles":[]}`)
if err := os.Chmod(path, 0o644); err != nil {
t.Fatal(err)
}
configuration := profile.Configuration{Path: path, Owner: profile.UserIdentity{UID: os.Getuid(), GID: os.Getgid()}}
checks := diagnosticChecks(configuration)
if checks[1].OK || !strings.Contains(checks[1].Evidence, "want 600") {
t.Fatalf("storage check = %#v", checks[1])
}
info, err := os.Stat(path)
if err != nil {
t.Fatal(err)
}
if info.Mode().Perm() != 0o644 {
t.Fatalf("doctor permission check changed mode to %o", info.Mode().Perm())
}
}
func TestResetCommandCancellationAndTruthfulPartialFailureWithFakeTargets(t *testing.T) {
root := t.TempDir()
config := filepath.Join(root, "xdg-config", "proxyctl", "profiles.json")
writeConfig(t, config, `{"profiles":[]}`)
store := profile.NewFileStore(config)
good := &processFakeTarget{id: "good", state: target.Enabled}
bad := &processFakeTarget{id: "bad", state: target.Drifted, err: errors.New("remove http://bob:hunter2@proxy.test failed")}
external := &processFakeTarget{id: "external", state: target.External}
stateDir := filepath.Join(root, "xdg-state", "proxyctl")
lock := operationlock.New(filepath.Join(stateDir, "operation.lock"))
logger := oplog.New(filepath.Join(stateDir, "operations.log"), 1024, 1)
var output bytes.Buffer
if err := runMaintenanceCommand([]string{"reset"}, strings.NewReader("no\n"), &output, store, []target.Target{good, bad, external}, lock, logger, nil); err != nil {
t.Fatal(err)
}
if good.calls+bad.calls+external.calls != 0 || !strings.Contains(output.String(), "cancelled") {
t.Fatalf("cancel result output=%q calls=%d/%d/%d", output.String(), good.calls, bad.calls, external.calls)
}
output.Reset()
err := runMaintenanceCommand([]string{"reset"}, strings.NewReader("yes\n"), &output, store, []target.Target{good, bad, external}, lock, logger, nil)
if err == nil {
t.Fatal("partial reset returned success")
}
text := output.String()
if good.calls != 1 || bad.calls != 1 || external.calls != 0 || !strings.Contains(text, "good: reset (Disabled)") || !strings.Contains(text, "bad: failed (Drifted)") || !strings.Contains(text, "external: skipped (External)") {
t.Fatalf("partial reset output=%q calls=%d/%d/%d", text, good.calls, bad.calls, external.calls)
}
if strings.Contains(text, "hunter2") || strings.Contains(err.Error(), "hunter2") {
t.Fatalf("reset leaked credentials: output=%q err=%v", text, err)
}
logData, readErr := os.ReadFile(filepath.Join(stateDir, "operations.log"))
if readErr != nil {
t.Fatal(readErr)
}
if strings.Contains(string(logData), "hunter2") || strings.Contains(string(logData), "bob:") {
t.Fatalf("log leaked full authenticated URL: %s", logData)
}
}