| 1 | //go:build e2elive |
| 2 | |
| 3 | package e2elivetest |
| 4 | |
| 5 | import ( |
| 6 | "bytes" |
| 7 | "encoding/json" |
| 8 | "io" |
| 9 | "net/http" |
| 10 | "net/http/httptest" |
| 11 | "net/http/httputil" |
| 12 | "net/url" |
| 13 | "os" |
| 14 | "path/filepath" |
| 15 | "strings" |
| 16 | "sync" |
| 17 | "testing" |
| 18 | ) |
| 19 | |
| 20 | type livePacket struct { |
| 21 | Path string `json:"path"` |
| 22 | Request json.RawMessage `json:"request"` |
| 23 | Response json.RawMessage `json:"response"` |
| 24 | } |
| 25 | |
| 26 | func liveRecordingProxy(t *testing.T, target string) (string, func()) { |
| 27 | t.Helper() |
| 28 | u, err := url.Parse(target) |
| 29 | executionMust(t, err) |
| 30 | proxy := httputil.NewSingleHostReverseProxy(u) |
| 31 | var mu sync.Mutex |
| 32 | packets := map[string]livePacket{} |
| 33 | server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 34 | // Nothing from headers (bearers, cookies or enrollment secrets) is recorded. |
| 35 | if !strings.HasPrefix(r.URL.Path, "/api/runner/v2/") { |
| 36 | proxy.ServeHTTP(w, r) |
| 37 | return |
| 38 | } |
| 39 | body, e := io.ReadAll(r.Body) |
| 40 | if e != nil { |
| 41 | http.Error(w, "read", 500) |
| 42 | return |
| 43 | } |
| 44 | r.Body = io.NopCloser(bytes.NewReader(body)) |
| 45 | capture := httptest.NewRecorder() |
| 46 | proxy.ServeHTTP(capture, r) |
| 47 | for k, v := range capture.Header() { |
| 48 | w.Header()[k] = v |
| 49 | } |
| 50 | w.WriteHeader(capture.Code) |
| 51 | _, _ = w.Write(capture.Body.Bytes()) |
| 52 | if capture.Code != 200 { |
| 53 | return |
| 54 | } |
| 55 | key := "" |
| 56 | switch r.URL.Path { |
| 57 | case "/api/runner/v2/heartbeat": |
| 58 | key = "heartbeat" |
| 59 | case "/api/runner/v2/coordinator/poll": |
| 60 | if !bytes.Contains(capture.Body.Bytes(), []byte(`"result":[]`)) { |
| 61 | key = "poll" |
| 62 | } |
| 63 | case "/api/runner/v2/coordinator/claim": |
| 64 | key = "claim" |
| 65 | case "/api/runner/v2/coordinator/tool": |
| 66 | if bytes.Contains(body, []byte(`"name":"post_message"`)) { |
| 67 | key = "reply" |
| 68 | } |
| 69 | case "/api/runner/v2/coordinator/event": |
| 70 | if bytes.Contains(body, []byte(`"kind":"coordinator.delivery"`)) { |
| 71 | key = "delivery" |
| 72 | } else if bytes.Contains(body, []byte(`"kind":"coordinator.text"`)) { |
| 73 | key = "text" |
| 74 | } |
| 75 | } |
| 76 | if key != "" { |
| 77 | mu.Lock() |
| 78 | if _, ok := packets[key]; !ok { |
| 79 | packets[key] = livePacket{r.URL.Path, body, append([]byte(nil), capture.Body.Bytes()...)} |
| 80 | } |
| 81 | mu.Unlock() |
| 82 | } |
| 83 | })) |
| 84 | t.Cleanup(server.Close) |
| 85 | return server.URL, func() { |
| 86 | dir := os.Getenv("EQUIPO_E2E_RECORD") |
| 87 | if dir == "" { |
| 88 | return |
| 89 | } |
| 90 | mu.Lock() |
| 91 | defer mu.Unlock() |
| 92 | if len(packets) != 6 { |
| 93 | t.Fatalf("stage wire recording: got %d contracts", len(packets)) |
| 94 | } |
| 95 | executionMust(t, os.MkdirAll(dir, 0700)) |
| 96 | var claim struct { |
| 97 | Result struct { |
| 98 | Admission struct{ Dispatch struct{ Key string } } |
| 99 | } |
| 100 | } |
| 101 | executionMust(t, json.Unmarshal(packets["claim"].Response, &claim)) |
| 102 | var beat struct { |
| 103 | Reports []struct { |
| 104 | Routing struct { |
| 105 | Home string `json:"credential_home_hash"` |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | executionMust(t, json.Unmarshal(packets["heartbeat"].Request, &beat)) |
| 110 | for key, p := range packets { |
| 111 | raw, e := json.MarshalIndent(p, "", " ") |
| 112 | executionMust(t, e) |
| 113 | raw = bytes.ReplaceAll(raw, []byte("paul@tito.io"), []byte("owner@example.test")) |
| 114 | if claim.Result.Admission.Dispatch.Key != "" { |
| 115 | raw = bytes.ReplaceAll(raw, []byte(claim.Result.Admission.Dispatch.Key), []byte("fixture-admission-key")) |
| 116 | } |
| 117 | for _, report := range beat.Reports { |
| 118 | if report.Routing.Home != "" { |
| 119 | raw = bytes.ReplaceAll(raw, []byte(report.Routing.Home), []byte(strings.Repeat("b", 64))) |
| 120 | } |
| 121 | } |
| 122 | executionMust(t, os.WriteFile(filepath.Join(dir, key+".json"), append(raw, '\n'), 0600)) |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | |