| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "net/http" |
| 6 | "net/http/httptest" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | "time" |
| 12 | |
| 13 | "github.com/sylvinus/agent-vm/internal/cage" |
| 14 | "github.com/sylvinus/agent-vm/internal/guestos" |
| 15 | ) |
| 16 | |
| 17 | func initializationRequest(s *Server, action string, trusted bool) *httptest.ResponseRecorder { |
| 18 | method, body := http.MethodGet, "" |
| 19 | if action != "" { |
| 20 | method, body = http.MethodPost, `{"action":"`+action+`"}` |
| 21 | } |
| 22 | r := httptest.NewRequest(method, s.origin+"/api/environment/initialization", strings.NewReader(body)) |
| 23 | r.Header.Set("Origin", s.origin) |
| 24 | r.Header.Set("Content-Type", "application/json") |
| 25 | r.AddCookie(&http.Cookie{Name: s.cookieName(), Value: s.session}) |
| 26 | if trusted { |
| 27 | r.AddCookie(&http.Cookie{Name: s.cookieName() + "-desktop", Value: trustedViewProof(s.token)}) |
| 28 | } |
| 29 | w := httptest.NewRecorder() |
| 30 | s.handler().ServeHTTP(w, r) |
| 31 | return w |
| 32 | } |
| 33 | |
| 34 | func TestInitializationPersistsBeforeDiskCreationAndDoesNotResetExisting(t *testing.T) { |
| 35 | s := testServer(t) |
| 36 | read := func() initializationView { |
| 37 | t.Helper() |
| 38 | w := initializationRequest(s, "", true) |
| 39 | var view initializationView |
| 40 | if w.Code != 200 || json.Unmarshal(w.Body.Bytes(), &view) != nil { |
| 41 | t.Fatalf("initialization: %d %s", w.Code, w.Body.String()) |
| 42 | } |
| 43 | return view |
| 44 | } |
| 45 | if !read().Required { |
| 46 | t.Fatal("fresh profile bypassed initialization") |
| 47 | } |
| 48 | for _, action := range []string{"begin", "install"} { |
| 49 | if w := initializationRequest(s, action, true); w.Code != 200 { |
| 50 | t.Fatal(w.Code, w.Body.String()) |
| 51 | } |
| 52 | } |
| 53 | if err := os.MkdirAll(filepath.Join(s.profile, "state"), 0700); err != nil { |
| 54 | t.Fatal(err) |
| 55 | } |
| 56 | if err := os.WriteFile(filepath.Join(s.profile, "state", ".agent-vm-base-version"), []byte("fixture"), 0600); err != nil { |
| 57 | t.Fatal(err) |
| 58 | } |
| 59 | view := read() |
| 60 | if !view.Required || !view.Started || !view.Tracked { |
| 61 | t.Fatalf("partial installation bypassed durable state: %+v", view) |
| 62 | } |
| 63 | |
| 64 | restarted := testServer(t) |
| 65 | restarted.profile = s.profile |
| 66 | if next, err := restarted.initializationState(); err != nil || !next.Required || !next.Started { |
| 67 | t.Fatalf("restart: %+v %v", next, err) |
| 68 | } |
| 69 | if err := os.Remove(filepath.Join(s.profile, "initialization.json")); err != nil { |
| 70 | t.Fatal(err) |
| 71 | } |
| 72 | if read().Required { |
| 73 | t.Fatal("existing Linux installation forced to reinstall") |
| 74 | } |
| 75 | s.windowsGuest = &guestos.WindowsProfile{} |
| 76 | if next, err := s.initializationState(); err != nil || next.Required { |
| 77 | t.Fatalf("existing Windows installation: %+v %v", next, err) |
| 78 | } |
| 79 | s.windowsGuest = nil |
| 80 | s.macosGuest = &guestos.MacOSProfile{} |
| 81 | if next, err := s.initializationState(); err != nil || next.Required { |
| 82 | t.Fatalf("existing Mac installation: %+v %v", next, err) |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | func TestInitializationCompletionRequiresReadyGuestAndCurrentNetworkEvidence(t *testing.T) { |
| 87 | s := testServer(t) |
| 88 | if w := initializationRequest(s, "begin", false); w.Code != http.StatusForbidden { |
| 89 | t.Fatal("untrusted initialization accepted", w.Code) |
| 90 | } |
| 91 | if w := initializationRequest(s, "install", true); w.Code != 200 { |
| 92 | t.Fatal(w.Code, w.Body.String()) |
| 93 | } |
| 94 | if w := initializationRequest(s, "complete", true); w.Code == 200 { |
| 95 | t.Fatal("unready guest accepted") |
| 96 | } |
| 97 | s.windowsGuest = &guestos.WindowsProfile{} |
| 98 | s.windowsState.Running = true |
| 99 | s.ready = true |
| 100 | if w := initializationRequest(s, "complete", true); w.Code == 200 { |
| 101 | t.Fatal("missing network report accepted") |
| 102 | } |
| 103 | cfg, err := cage.Load(s.configPath) |
| 104 | if err != nil { |
| 105 | t.Fatal(err) |
| 106 | } |
| 107 | report := cage.SafetyReport{Policy: cage.SafetyPolicyVersion, ID: "initialization", RunID: "test-vm", CheckID: "test-check", Configuration: cfg.Fingerprint(), CreatedAt: time.Now()} |
| 108 | for _, id := range []string{"egress_policy", "proxy_transport", "dns_transport", "guest_settings", "browser_settings", "filesystem_boundary", "management_boundary", "fail_closed", "udp_policy", "ipv6_policy", "exit_consistency"} { |
| 109 | report.Checks = append(report.Checks, cage.SafetyCheck{ID: id, Critical: true, State: cage.CheckPassed, Evidence: "isolated test", CheckedAt: report.CreatedAt}) |
| 110 | } |
| 111 | s.safety.Gate = &cage.GateStatus{Mode: cage.GateOpen, PolicyVerified: true, RunID: report.RunID, Configuration: cfg.Fingerprint(), Until: time.Now().Add(time.Minute), Report: &report} |
| 112 | s.setJobForTest(Job{Running: true, Kind: "start"}) |
| 113 | if w := initializationRequest(s, "complete", true); w.Code == 200 { |
| 114 | t.Fatal("unfinished job accepted") |
| 115 | } |
| 116 | s.setJobForTest(Job{}) |
| 117 | s.safety.Gate.Configuration = "another-config" |
| 118 | if w := initializationRequest(s, "complete", true); w.Code == 200 { |
| 119 | t.Fatal("stale configuration accepted") |
| 120 | } |
| 121 | s.safety.Gate.Configuration = cfg.Fingerprint() |
| 122 | if w := initializationRequest(s, "complete", true); w.Code != 200 { |
| 123 | t.Fatal(w.Code, w.Body.String()) |
| 124 | } |
| 125 | s.ready = false |
| 126 | s.windowsState.Running = false |
| 127 | if view, err := s.initializationState(); err != nil || view.Required { |
| 128 | t.Fatalf("completed stopped environment lost initialization: %+v %v", view, err) |
| 129 | } |
| 130 | } |