| 1 | package cage |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "github.com/sylvinus/agent-vm/internal/managedproxy" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "testing" |
| 10 | "time" |
| 11 | ) |
| 12 | |
| 13 | func TestProxyRevisionRevokesOldReportAtSameEndpoint(t *testing.T) { |
| 14 | cfg := DefaultConfig() |
| 15 | cfg.ProxyRevision = "first" |
| 16 | g, err := NewEgressGate(cfg) |
| 17 | if err != nil { |
| 18 | t.Fatal(err) |
| 19 | } |
| 20 | defer g.Close("done") |
| 21 | if err = g.BeginCheck(); err != nil { |
| 22 | t.Fatal(err) |
| 23 | } |
| 24 | state := g.Status() |
| 25 | report := SafetyReport{Policy: SafetyPolicyVersion, ID: "old-report", RunID: state.RunID, CheckID: state.CheckID, Configuration: cfg.Fingerprint(), CreatedAt: time.Now()} |
| 26 | for _, id := range requiredSafetyChecks { |
| 27 | report.Checks = append(report.Checks, SafetyCheck{ID: id, State: CheckPassed, Critical: true, Evidence: "test", CheckedAt: time.Now()}) |
| 28 | } |
| 29 | if err = g.Grant(report); err != nil { |
| 30 | t.Fatal(err) |
| 31 | } |
| 32 | next := cfg |
| 33 | next.ProxyRevision = "second" |
| 34 | if err = g.SyncProxyRevision(next); err != nil { |
| 35 | t.Fatal(err) |
| 36 | } |
| 37 | state = g.Status() |
| 38 | if state.Mode != GateClosed || state.Report != nil || state.Configuration != next.Fingerprint() { |
| 39 | t.Fatal("old generation retained") |
| 40 | } |
| 41 | if err = g.Renew(report.ID); err == nil { |
| 42 | t.Fatal("old lease renewed") |
| 43 | } |
| 44 | if err = g.BeginCheck(); err != nil { |
| 45 | t.Fatal(err) |
| 46 | } |
| 47 | if err = g.Grant(report); err == nil { |
| 48 | t.Fatal("old report authorized a new node") |
| 49 | } |
| 50 | changed := next |
| 51 | changed.ProxyURL = "socks5://127.0.0.1:3333" |
| 52 | if err = g.SyncProxyRevision(changed); err == nil { |
| 53 | t.Fatal("transport changed without environment restart") |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | func TestProxyRevisionControlUpdatesHostRunningRecord(t *testing.T) { |
| 58 | profile := t.TempDir() |
| 59 | path := filepath.Join(profile, "config.json") |
| 60 | state := filepath.Join(profile, "state") |
| 61 | instance := filepath.Join(state, "lima", "qa") |
| 62 | if err := os.MkdirAll(instance, 0700); err != nil { |
| 63 | t.Fatal(err) |
| 64 | } |
| 65 | cfg := DefaultConfig() |
| 66 | cfg.ProxyRevision = "initial" |
| 67 | p, _ := managedproxy.Load(profile) |
| 68 | if err := p.Import("qa", "", []byte(`proxies: [{name: qa, type: socks5, server: 127.0.0.1, port: 1}]`), "qa", 0, ""); err != nil { |
| 69 | t.Fatal(err) |
| 70 | } |
| 71 | p.Selected = p.Sources[0].Nodes[0].ID |
| 72 | p.Revision = cfg.ProxyRevision |
| 73 | cfg.ProxyURL = p.Endpoint() |
| 74 | if err := p.Save(profile); err != nil { |
| 75 | t.Fatal(err) |
| 76 | } |
| 77 | write := func(c Config) { |
| 78 | b, _ := json.Marshal(c) |
| 79 | if err := os.WriteFile(path, b, 0600); err != nil { |
| 80 | t.Fatal(err) |
| 81 | } |
| 82 | } |
| 83 | write(cfg) |
| 84 | cfg, err := Load(path) |
| 85 | if err != nil { |
| 86 | t.Fatal(err) |
| 87 | } |
| 88 | g, err := NewEgressGate(cfg) |
| 89 | if err != nil { |
| 90 | t.Fatal(err) |
| 91 | } |
| 92 | stop, err := StartGateControl(path, instance, g) |
| 93 | if err != nil { |
| 94 | t.Fatal(err) |
| 95 | } |
| 96 | defer stop() |
| 97 | client, err := OpenGateClient(instance) |
| 98 | if err != nil { |
| 99 | t.Fatal(err) |
| 100 | } |
| 101 | next := cfg |
| 102 | next.ProxyRevision = "new-core" |
| 103 | p.Revision = next.ProxyRevision |
| 104 | if err := p.Save(profile); err != nil { |
| 105 | t.Fatal(err) |
| 106 | } |
| 107 | write(next) |
| 108 | status, err := client.SyncProxyRevision(context.Background()) |
| 109 | if err != nil { |
| 110 | t.Fatal(err) |
| 111 | } |
| 112 | if status.Configuration != next.Fingerprint() || status.Mode != GateClosed || status.Report != nil { |
| 113 | t.Fatal("control did not revoke old generation") |
| 114 | } |
| 115 | if err = next.CheckRunning(state); err != nil { |
| 116 | t.Fatal("running record did not adopt node generation", err) |
| 117 | } |
| 118 | if err = cfg.CheckRunning(state); err == nil { |
| 119 | t.Fatal("old configuration still matches running record") |
| 120 | } |
| 121 | } |