|
| 1 | +package handlers |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/urnetwork/server" |
| 10 | + "github.com/urnetwork/server/model" |
| 11 | +) |
| 12 | + |
| 13 | +// The pin endpoint is what stands between the prober and probing unpinned, so |
| 14 | +// its auth has to fail closed in exactly the two ways an operator gets wrong: |
| 15 | +// no header at all (a deployment that never configured the secret) and a wrong |
| 16 | +// one (a rotated secret on one side only). Both are 401, and neither reaches |
| 17 | +// the database. |
| 18 | +func TestGeolocationSourcePinsRejectsMissingSecret(t *testing.T) { |
| 19 | + req := httptest.NewRequest(http.MethodGet, "/network/geolocation-source-pins", nil) |
| 20 | + w := httptest.NewRecorder() |
| 21 | + |
| 22 | + GeolocationSourcePins(w, req) |
| 23 | + |
| 24 | + if w.Code != http.StatusUnauthorized { |
| 25 | + t.Fatalf("status = %d, want 401 when the operator secret header is absent", w.Code) |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +func TestGeolocationSourcePinsRejectsWrongSecret(t *testing.T) { |
| 30 | + req := httptest.NewRequest(http.MethodGet, "/network/geolocation-source-pins", nil) |
| 31 | + req.Header.Set(operatorSecretHeader, "definitely-not-the-secret") |
| 32 | + w := httptest.NewRecorder() |
| 33 | + |
| 34 | + GeolocationSourcePins(w, req) |
| 35 | + |
| 36 | + if w.Code != http.StatusUnauthorized { |
| 37 | + t.Fatalf("status = %d, want 401 on a wrong operator secret", w.Code) |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +// TestGeolocationSourcePinsRejectsAlteredSecret pins the comparison itself. |
| 42 | +// The two tests above both run with the vault unconfigured and take the |
| 43 | +// secret=="" short-circuit, so they would still pass if the comparison were |
| 44 | +// `strings.HasPrefix` or dropped entirely. This one configures a real secret |
| 45 | +// and offers a near miss. |
| 46 | +func TestGeolocationSourcePinsRejectsAlteredSecret(t *testing.T) { |
| 47 | + const secret = "correct-operator-secret-0123456789" |
| 48 | + defer withStubOperatorIngestSecret(secret)() |
| 49 | + |
| 50 | + for _, wrong := range []string{ |
| 51 | + secret + "x", |
| 52 | + secret[:len(secret)-1], |
| 53 | + "", |
| 54 | + } { |
| 55 | + req := httptest.NewRequest(http.MethodGet, "/network/geolocation-source-pins", nil) |
| 56 | + if wrong != "" { |
| 57 | + req.Header.Set(operatorSecretHeader, wrong) |
| 58 | + } |
| 59 | + w := httptest.NewRecorder() |
| 60 | + |
| 61 | + GeolocationSourcePins(w, req) |
| 62 | + |
| 63 | + if w.Code != http.StatusUnauthorized { |
| 64 | + t.Errorf("status = %d for secret %q, want 401", w.Code, wrong) |
| 65 | + } |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +// TestGeolocationSourcePinsServesTheObservedSet is the other half: the auth |
| 70 | +// gate must be able to ACCEPT, and what comes back must be what the |
| 71 | +// observation job stored, keyed by host, on the exact wire shape the prober |
| 72 | +// decodes (`{host: {leaf, intermediate}}`). |
| 73 | +// |
| 74 | +// It asserts on the decoded JSON rather than on the handler's Go types, |
| 75 | +// because the prober is a separate repository that only ever sees the bytes: |
| 76 | +// a renamed json tag would be invisible to a Go-level assertion and would take |
| 77 | +// the fleet's probing offline. |
| 78 | +func TestGeolocationSourcePinsServesTheObservedSet(t *testing.T) { |
| 79 | + t.Setenv("WARP_ENV", "local") |
| 80 | + server.DefaultTestEnv().Run(t, func(t testing.TB) { |
| 81 | + const secret = "correct-operator-secret-0123456789" |
| 82 | + defer withStubOperatorIngestSecret(secret)() |
| 83 | + |
| 84 | + observedAt := server.NowUtc() |
| 85 | + model.SetGeolocationSourcePin(t.Context(), &model.GeolocationSourcePin{ |
| 86 | + Host: "ipinfo.io", |
| 87 | + LeafSpki: "leaf-ipinfo", |
| 88 | + IntermediateSpki: "int-ipinfo", |
| 89 | + ObservedAt: observedAt, |
| 90 | + }) |
| 91 | + model.SetGeolocationSourcePin(t.Context(), &model.GeolocationSourcePin{ |
| 92 | + Host: "api.i.pn", |
| 93 | + LeafSpki: "leaf-ipn", |
| 94 | + IntermediateSpki: "int-ipn", |
| 95 | + ObservedAt: observedAt, |
| 96 | + }) |
| 97 | + |
| 98 | + req := httptest.NewRequest(http.MethodGet, "/network/geolocation-source-pins", nil) |
| 99 | + req.Header.Set(operatorSecretHeader, secret) |
| 100 | + w := httptest.NewRecorder() |
| 101 | + |
| 102 | + GeolocationSourcePins(w, req) |
| 103 | + |
| 104 | + if w.Code != http.StatusOK { |
| 105 | + t.Fatalf("status = %d, want 200 with the correct operator secret. body = %s", w.Code, w.Body.String()) |
| 106 | + } |
| 107 | + |
| 108 | + var got map[string]map[string]string |
| 109 | + if err := json.Unmarshal(w.Body.Bytes(), &got); err != nil { |
| 110 | + t.Fatalf("decode response %q: %s", w.Body.String(), err) |
| 111 | + } |
| 112 | + for host, want := range map[string][2]string{ |
| 113 | + "ipinfo.io": {"leaf-ipinfo", "int-ipinfo"}, |
| 114 | + "api.i.pn": {"leaf-ipn", "int-ipn"}, |
| 115 | + } { |
| 116 | + pin, ok := got[host] |
| 117 | + if !ok { |
| 118 | + t.Fatalf("host %q missing from the served set %v; the prober treats a missing source host as a hard stop", host, got) |
| 119 | + } |
| 120 | + if pin["leaf"] != want[0] { |
| 121 | + t.Errorf("%s leaf = %q, want %q", host, pin["leaf"], want[0]) |
| 122 | + } |
| 123 | + if pin["intermediate"] != want[1] { |
| 124 | + t.Errorf("%s intermediate = %q, want %q", host, pin["intermediate"], want[1]) |
| 125 | + } |
| 126 | + } |
| 127 | + }) |
| 128 | +} |
| 129 | + |
| 130 | +// A host that has never been observed must stay ABSENT from the answer. The |
| 131 | +// endpoint must not invent a placeholder row to make the map look complete: |
| 132 | +// the prober decides what to do about a missing host (refuse to probe), and it |
| 133 | +// can only decide that if the absence reaches it. |
| 134 | +func TestGeolocationSourcePinsOmitsUnobservedHosts(t *testing.T) { |
| 135 | + t.Setenv("WARP_ENV", "local") |
| 136 | + server.DefaultTestEnv().Run(t, func(t testing.TB) { |
| 137 | + const secret = "correct-operator-secret-0123456789" |
| 138 | + defer withStubOperatorIngestSecret(secret)() |
| 139 | + |
| 140 | + // exactly one of the source hosts observed |
| 141 | + model.SetGeolocationSourcePin(t.Context(), &model.GeolocationSourcePin{ |
| 142 | + Host: model.GeolocationSourceHosts[0], |
| 143 | + LeafSpki: "leaf-only", |
| 144 | + IntermediateSpki: "int-only", |
| 145 | + ObservedAt: server.NowUtc(), |
| 146 | + }) |
| 147 | + |
| 148 | + req := httptest.NewRequest(http.MethodGet, "/network/geolocation-source-pins", nil) |
| 149 | + req.Header.Set(operatorSecretHeader, secret) |
| 150 | + w := httptest.NewRecorder() |
| 151 | + |
| 152 | + GeolocationSourcePins(w, req) |
| 153 | + |
| 154 | + if w.Code != http.StatusOK { |
| 155 | + t.Fatalf("status = %d, want 200. body = %s", w.Code, w.Body.String()) |
| 156 | + } |
| 157 | + var got map[string]map[string]string |
| 158 | + if err := json.Unmarshal(w.Body.Bytes(), &got); err != nil { |
| 159 | + t.Fatalf("decode response %q: %s", w.Body.String(), err) |
| 160 | + } |
| 161 | + if len(got) != 1 { |
| 162 | + t.Fatalf("served %d host(s) %v, want only the one that was observed", len(got), got) |
| 163 | + } |
| 164 | + for _, host := range model.GeolocationSourceHosts[1:] { |
| 165 | + if _, ok := got[host]; ok { |
| 166 | + t.Errorf("unobserved host %q appears in the served set; a placeholder pin here would take the fail-closed decision away from the prober", host) |
| 167 | + } |
| 168 | + } |
| 169 | + }) |
| 170 | +} |
0 commit comments