| 1 | package edge |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "io" |
| 6 | "net" |
| 7 | "net/http" |
| 8 | "net/http/httptest" |
| 9 | "net/url" |
| 10 | "testing" |
| 11 | "time" |
| 12 | |
| 13 | "github.com/carlosframework/platform/internal/geo" |
| 14 | "github.com/carlosframework/platform/internal/geoip" |
| 15 | "github.com/carlosframework/platform/internal/registry" |
| 16 | "github.com/carlosframework/platform/internal/release" |
| 17 | ) |
| 18 | |
| 19 | // tunnelGeoScene is a hub edge routing demo.example over a real tunnelclient |
| 20 | // connection to a remote box whose own Edge serves the app (geoip spec §8.4, |
| 21 | // D2). It returns the hub, the remote edge, and a request func. |
| 22 | func tunnelGeoScene(t *testing.T) (hub, remote *Edge, get func() map[string]string) { |
| 23 | t.Helper() |
| 24 | hubReg := tunnelRoute(t, registry.Route{Host: "demo.example", Kind: "instance", Status: "ready", Tunnel: "pi-box"}) |
| 25 | hub, err := New(hubReg, nil) |
| 26 | if err != nil { |
| 27 | t.Fatal(err) |
| 28 | } |
| 29 | t.Cleanup(hub.probers.Stop) |
| 30 | srv := httptest.NewServer(hub) |
| 31 | t.Cleanup(srv.Close) |
| 32 | u, _ := url.Parse(srv.URL) |
| 33 | th := NewTunnelHub(u.Hostname(), map[string]string{"pi-box": "sekrit"}) |
| 34 | hub.EnableTunnels(th) |
| 35 | if err := hub.Reload(); err != nil { |
| 36 | t.Fatal(err) |
| 37 | } |
| 38 | remote = geoEdge(t, "demo.example") // the remote box's own edge, geo-echo backend |
| 39 | stop := startFakeRemoteHost(t, srv.URL, "pi-box", "sekrit", remote) |
| 40 | t.Cleanup(stop) |
| 41 | waitForTunnelState(t, th, "pi-box", "connected", e2eReconnectBound) |
| 42 | get = func() map[string]string { |
| 43 | t.Helper() |
| 44 | req, _ := http.NewRequest("GET", srv.URL+"/", nil) |
| 45 | req.Host = "demo.example" |
| 46 | req.Header.Set(geoip.HeaderCountry, "KP") // a forgery at the hub's door |
| 47 | resp, err := http.DefaultClient.Do(req) |
| 48 | if err != nil { |
| 49 | t.Fatal(err) |
| 50 | } |
| 51 | defer resp.Body.Close() |
| 52 | b, _ := io.ReadAll(resp.Body) |
| 53 | got := map[string]string{} |
| 54 | if err := json.Unmarshal(b, &got); err != nil { |
| 55 | t.Fatalf("remote app body %q: %v", b, err) |
| 56 | } |
| 57 | return got |
| 58 | } |
| 59 | return hub, remote, get |
| 60 | } |
| 61 | |
| 62 | func TestTunnelDeliversTheHubsAnswerAndTheRemoteNeverLooksUp(t *testing.T) { |
| 63 | hub, remote, get := tunnelGeoScene(t) |
| 64 | hubSpy, remoteSpy := spyGeo(hub, "GB"), spyGeo(remote, "XX") |
| 65 | h := get() |
| 66 | if h[geoip.HeaderCountry] != "GB" || h[geoip.HeaderSource] != "TestDB-GB" { |
| 67 | t.Fatalf("remote app saw %v, want the hub's GB answer", h) |
| 68 | } |
| 69 | if _, leaked := h[geoip.HeaderHop]; leaked { |
| 70 | t.Fatal("X-Carlos-Hop-Geo reached the remote app") |
| 71 | } |
| 72 | if hubSpy.Load() != 1 || remoteSpy.Load() != 0 { |
| 73 | t.Fatalf("lookups hub=%d remote=%d, want 1 and 0 (ingress computes)", hubSpy.Load(), remoteSpy.Load()) |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | // A miss at the hub crosses as a miss: the remote app gets no headers and |
| 78 | // the remote does not look up for itself. |
| 79 | func TestTunnelCarriesAMissAsAMiss(t *testing.T) { |
| 80 | hub, remote, get := tunnelGeoScene(t) |
| 81 | remoteSpy := spyGeo(remote, "XX") |
| 82 | hub.geoLookup = func(_ net.IP) geo.Snapshot { return geo.Snapshot{Loaded: true, Source: "TestDB"} } |
| 83 | if h := get(); len(h) != 0 { |
| 84 | t.Fatalf("a hub miss reached the remote app as %v", h) |
| 85 | } |
| 86 | if remoteSpy.Load() != 0 { |
| 87 | t.Fatal("the remote looked up after the hub missed") |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | // Spec §9, codex R4-1/R5: one analytics observation per served response. |
| 92 | // The hub observes every tunnelled response (fresh or from its own cache) |
| 93 | // with its own geo answer; the remote observes none of them. |
| 94 | func TestTunnelledPageviewsAreCountedOnceAtTheHub(t *testing.T) { |
| 95 | hubReg := tunnelRoute(t, registry.Route{Host: "demo.example", Kind: "instance", Status: "ready", Tunnel: "pi-box", App: "web"}) |
| 96 | hub, err := New(hubReg, nil) |
| 97 | if err != nil { |
| 98 | t.Fatal(err) |
| 99 | } |
| 100 | t.Cleanup(hub.probers.Stop) |
| 101 | hub.EnableCache(NewCache(DefaultCacheMaxEntries, DefaultCacheMaxBodyBytes)) |
| 102 | srv := httptest.NewServer(hub) |
| 103 | t.Cleanup(srv.Close) |
| 104 | u, _ := url.Parse(srv.URL) |
| 105 | th := NewTunnelHub(u.Hostname(), map[string]string{"pi-box": "sekrit"}) |
| 106 | hub.EnableTunnels(th) |
| 107 | if err := hub.Reload(); err != nil { |
| 108 | t.Fatal(err) |
| 109 | } |
| 110 | |
| 111 | page := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 112 | w.Header().Set("Content-Type", "text/html; charset=utf-8") |
| 113 | if r.URL.Path == "/cached" { |
| 114 | w.Header().Set("Cache-Control", "public, max-age=60") |
| 115 | } |
| 116 | w.Write([]byte("<h1>hi</h1>")) |
| 117 | })) |
| 118 | t.Cleanup(page.Close) |
| 119 | remoteReg := openReg(t) |
| 120 | if err := remoteReg.Upsert(registry.Route{Host: "demo.example", Kind: "service", Addr: page.Listener.Addr().String(), Status: "ready", App: "web"}); err != nil { |
| 121 | t.Fatal(err) |
| 122 | } |
| 123 | remote, err := New(remoteReg, nil) |
| 124 | if err != nil { |
| 125 | t.Fatal(err) |
| 126 | } |
| 127 | t.Cleanup(remote.probers.Stop) |
| 128 | stop := startFakeRemoteHost(t, srv.URL, "pi-box", "sekrit", remote) |
| 129 | t.Cleanup(stop) |
| 130 | waitForTunnelState(t, th, "pi-box", "connected", e2eReconnectBound) |
| 131 | |
| 132 | hubM, remoteM := &memAnalytics{}, &memAnalytics{} |
| 133 | on := map[string]bool{release.OpsAccount + "/web": true} |
| 134 | hub.EnableAnalytics(hubM) |
| 135 | hub.SetAnalyticsOn(on) |
| 136 | remote.EnableAnalytics(remoteM) |
| 137 | remote.SetAnalyticsOn(on) |
| 138 | spyGeo(hub, "GB") |
| 139 | spyGeo(remote, "XX") |
| 140 | |
| 141 | get := func(path string) { |
| 142 | req, _ := http.NewRequest("GET", srv.URL+path, nil) |
| 143 | req.Host = "demo.example" |
| 144 | req.Header.Set("User-Agent", "Mozilla/5.0") |
| 145 | resp, err := http.DefaultClient.Do(req) |
| 146 | if err != nil { |
| 147 | t.Fatal(err) |
| 148 | } |
| 149 | io.Copy(io.Discard, resp.Body) |
| 150 | resp.Body.Close() |
| 151 | } |
| 152 | get("/fresh") |
| 153 | get("/cached") |
| 154 | get("/cached") // the hub's cache answers; the remote never sees it |
| 155 | |
| 156 | deadline := time.Now().Add(2 * time.Second) |
| 157 | for len(hubM.snap()) < 3 && time.Now().Before(deadline) { |
| 158 | time.Sleep(10 * time.Millisecond) // the observer runs in a defer after the response |
| 159 | } |
| 160 | if n := len(remoteM.snap()); n != 0 { |
| 161 | t.Fatalf("the remote observed %d tunnelled responses, want 0", n) |
| 162 | } |
| 163 | obs := hubM.snap() |
| 164 | if len(obs) != 3 { |
| 165 | t.Fatalf("the hub observed %d responses, want 3 (one per served response)", len(obs)) |
| 166 | } |
| 167 | for _, o := range obs { |
| 168 | if o.Country != "GB" || o.GeoState != "hit" || o.GeoSource != "TestDB-GB" { |
| 169 | t.Fatalf("hub observation %+v, want its own GB answer", o) |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | |