| new file mode 100644 |
| index 000000000..3a6cad5a5 |
| --- /dev/null |
| +++ b/internal/edge/pathhop_test.go |
| @@ -0,0 +1,338 @@ |
| +package edge |
| + |
| +import ( |
| + "context" |
| + "crypto/ecdsa" |
| + "crypto/elliptic" |
| + "crypto/rand" |
| + "crypto/tls" |
| + "crypto/x509" |
| + "crypto/x509/pkix" |
| + "fmt" |
| + "io" |
| + "math/big" |
| + "net" |
| + "net/http" |
| + "net/http/httptest" |
| + "sync" |
| + "testing" |
| + "time" |
| + |
| + "github.com/carlosframework/platform/internal/release" |
| +) |
| + |
| +// A path-selected hop (#186 spec §5.4, §5.7): the request's Host is the front |
| +// name hub.example, and the route the hop is for is tenant.example on another |
| +// box. The wire carries hub.example; every holder decision is tenant.example's. |
| +const ( |
| + pathHopWire = "hub.example" |
| + pathHopRoute = "tenant.example" |
| +) |
| + |
| +// pathHopScene is BackhaulHolderScene's layout for a path hop: a sending edge |
| +// (dennis, region us) and destination boxes at distinct 127.0.0.N addresses, |
| +// each dialled through the sender's own per-address backhaul transport. A |
| +// destination's edges file publishes the target route's row; the front name |
| +// is published nowhere. |
| +type pathHopScene struct { |
| + store release.Store |
| + ca *e2eCA |
| + bucket string |
| + sender *backhaulEdge |
| + edges []*backhaulEdge |
| +} |
| + |
| +func newPathHopScene(t *testing.T) *pathHopScene { |
| + t.Helper() |
| + s := &pathHopScene{bucket: t.TempDir(), ca: newE2ECA(t)} |
| + s.store = release.NewDirStore(s.bucket) |
| + s.ca.publish(t, s.store, pathHopWire) |
| + if err := MintMeshKey(t.Context(), s.store); err != nil { |
| + t.Fatal(err) |
| + } |
| + s.sender = newBackhaulEdge(t, s.bucket, "dennis", s.ca, nil, nil) |
| + s.edges = append(s.edges, s.sender) |
| + old := backhaulRootCAs |
| + backhaulRootCAs = s.ca.pool |
| + t.Cleanup(func() { backhaulRootCAs = old }) |
| + s.file(t, "dennis", "127.0.0.1", "us", 0) |
| + return s |
| +} |
| + |
| +// file publishes box's edges file at ip in region. A non-zero stamp gives it |
| +// the target route's row with that stamp (the higher stamp serves). |
| +func (s *pathHopScene) file(t *testing.T, box, ip, region string, stamp int) { |
| + t.Helper() |
| + hosts := "" |
| + if stamp != 0 { |
| + hosts = fmt.Sprintf(`%q:{"role":"home","account":"acct","app":"tenant","stamp":%d,"route_account":"acct","route_app":"tenant","path_target":true}`, pathHopRoute, stamp) |
| + } |
| + putJSON(t, s.store, "control/edges/"+box+".json", fmt.Sprintf(`{"v":1,"box":%q,"eip":%q,"region":%q,"placement":"p","data_bucket":"data","secrets_key_fp":"fp","box_role":"serve","admitted":true,"drained":false,"generation":%q,"hosts":{%s}}`, |
| + box, ip, region, time.Now().UTC().Format(time.RFC3339Nano), hosts)) |
| +} |
| + |
| +// dialAt points the sender's backhaul transport for ip at addr. |
| +func (s *pathHopScene) dialAt(t *testing.T, ip, addr string) { |
| + t.Helper() |
| + tr := s.sender.edge.backhaulTransport(ip) |
| + tr.DialContext = func(ctx context.Context, network, _ string) (net.Conn, error) { |
| + var d net.Dialer |
| + return d.DialContext(ctx, network, addr) |
| + } |
| + t.Cleanup(tr.CloseIdleConnections) |
| +} |
| + |
| +// edge adds a real receiving edge whose registry serves routes. |
| +func (s *pathHopScene) edge(t *testing.T, box, ip string, stamp int, routes map[string]string) *backhaulEdge { |
| + t.Helper() |
| + be := newBackhaulEdge(t, s.bucket, box, s.ca, nil, routes) |
| + s.edges = append(s.edges, be) |
| + s.file(t, box, ip, "eu", stamp) |
| + s.dialAt(t, ip, be.tlsAddr) |
| + return be |
| +} |
| + |
| +// pathHopBox is a destination box's 443 door reduced to a handler, presenting |
| +// the deployment CA's certificate for the front name only. It records what |
| +// arrived on the wire. |
| +type pathHopBox struct { |
| + mu sync.Mutex |
| + hosts []string // r.Host of each request |
| + auths []string // X-Carlos-Hop-Auth of each request |
| + srv *httptest.Server |
| +} |
| + |
| +func (b *pathHopBox) seen() (hosts, auths []string) { |
| + b.mu.Lock() |
| + defer b.mu.Unlock() |
| + return append([]string(nil), b.hosts...), append([]string(nil), b.auths...) |
| +} |
| + |
| +// box adds a raw destination box answering with h. |
| +func (s *pathHopScene) box(t *testing.T, name, ip string, stamp int, h http.HandlerFunc) *pathHopBox { |
| + t.Helper() |
| + b := &pathHopBox{} |
| + b.srv = httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| + b.mu.Lock() |
| + b.hosts = append(b.hosts, r.Host) |
| + b.auths = append(b.auths, r.Header.Get(hopAuthHeader)) |
| + b.mu.Unlock() |
| + h(w, r) |
| + })) |
| + b.srv.TLS = &tls.Config{Certificates: []tls.Certificate{s.ca.leaf(t, pathHopWire)}} |
| + b.srv.StartTLS() |
| + t.Cleanup(b.srv.Close) |
| + s.file(t, name, ip, "eu", stamp) |
| + s.dialAt(t, ip, b.srv.Listener.Addr().String()) |
| + return b |
| +} |
| + |
| +// leaf mints a serving certificate for host under the deployment CA. |
| +func (ca *e2eCA) leaf(t *testing.T, host string) tls.Certificate { |
| + t.Helper() |
| + key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + tmpl := &x509.Certificate{ |
| + SerialNumber: big.NewInt(time.Now().UnixNano()), |
| + Subject: pkix.Name{CommonName: host}, |
| + DNSNames: []string{host}, |
| + NotBefore: time.Now().Add(-time.Hour), |
| + NotAfter: time.Now().Add(time.Hour), |
| + KeyUsage: x509.KeyUsageDigitalSignature, |
| + ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}, |
| + } |
| + der, err := x509.CreateCertificate(rand.Reader, tmpl, ca.root, &key.PublicKey, ca.key) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + return tls.Certificate{Certificate: [][]byte{der}, PrivateKey: key} |
| +} |
| + |
| +func (s *pathHopScene) refresh(t *testing.T) { |
| + t.Helper() |
| + for _, be := range s.edges { |
| + refreshHomeMap(t, be.homes) |
| + if err := be.mirror.Refresh(t.Context()); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| +} |
| + |
| +// target is the target route's published row, as Task 8's dispatch reads it. |
| +func (s *pathHopScene) target(t *testing.T) pathRouteInfo { |
| + t.Helper() |
| + info, ok := s.sender.homes.exactPathRoute(pathHopRoute) |
| + if !ok { |
| + t.Fatalf("no published row for %s", pathHopRoute) |
| + } |
| + return info |
| +} |
| + |
| +// send makes one path hop from the sender for a request to the front name. |
| +func (s *pathHopScene) send(t *testing.T, r *http.Request) *httptest.ResponseRecorder { |
| + t.Helper() |
| + info := s.target(t) |
| + w := httptest.NewRecorder() |
| + if !s.sender.edge.backhaulRouteTo(w, r, pathHopWire, pathHopRoute, info.Dial, info.Box, info.Owner) { |
| + t.Fatal("backhaulRouteTo did not answer") |
| + } |
| + return w |
| +} |
| + |
| +func pathHopRequest() *http.Request { |
| + r := httptest.NewRequest(http.MethodGet, "https://"+pathHopWire+"/tenant/page", nil) |
| + r.RemoteAddr = "192.0.2.7:4242" |
| + return r |
| +} |
| + |
| +// The hop is signed for, sent to and presented as the front name: the |
| +// receiving edge verifies the MAC for hub.example (the client's chain is |
| +// believed, which only a verified hop earns) and the app behind it is asked |
| +// for hub.example. |
| +func TestBackhaulRouteToSignsWireHost(t *testing.T) { |
| + s := newPathHopScene(t) |
| + app := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| + fmt.Fprintf(w, "%s|%s|%s", r.Host, r.URL.Path, r.Header.Get("X-Forwarded-For")) |
| + })) |
| + t.Cleanup(app.Close) |
| + betty := s.edge(t, "betty", "127.0.0.2", 5, map[string]string{pathHopWire: app.Listener.Addr().String()}) |
| + tap := &e2eTap{} |
| + betty.tap.Store(tap) |
| + s.refresh(t) |
| + |
| + w := s.send(t, pathHopRequest()) |
| + if w.Code != http.StatusOK { |
| + t.Fatalf("hop: %d %q", w.Code, w.Body.String()) |
| + } |
| + if got, want := w.Body.String(), pathHopWire+"|/tenant/page|192.0.2.7"; got != want { |
| + t.Fatalf("app saw %q, want %q (Host, path, the verified hop's client chain)", got, want) |
| + } |
| + if n := betty.calls.Load(); n != 1 { |
| + t.Fatalf("betty calls=%d, want 1", n) |
| + } |
| + req, _ := tap.last() |
| + auth := req.Get(hopAuthHeader) |
| + key := signingKey(s.sender.homes.meshKeys()) |
| + if !verifyHop(key, pathHopWire, auth, time.Now()) { |
| + t.Fatalf("hop MAC %q does not verify for the wire host %s", auth, pathHopWire) |
| + } |
| + if verifyHop(key, pathHopRoute, auth, time.Now()) { |
| + t.Fatalf("hop MAC verifies for the route host %s", pathHopRoute) |
| + } |
| +} |
| + |
| +// A holder answer to a path hop is learned under the route, never under the |
| +// front name, and the next hop for the route goes straight to the holder, |
| +// still carrying the front name on the wire. A handover answer is the same. |
| +func TestBackhaulRouteToKeysHolderOnRoute(t *testing.T) { |
| + for _, tc := range []struct { |
| + name string |
| + refuse func(http.ResponseWriter) |
| + }{ |
| + {"holder", func(w http.ResponseWriter) { |
| + w.Header().Set(holderHeader, "carol") |
| + }}, |
| + {"handover", func(w http.ResponseWriter) { |
| + w.Header().Set(HandoverHeader, FormatHandover("carol", 3)) |
| + w.Header().Set(holderHeader, "carol") |
| + }}, |
| + } { |
| + t.Run(tc.name, func(t *testing.T) { |
| + s := newPathHopScene(t) |
| + betty := s.box(t, "betty", "127.0.0.2", 9, func(w http.ResponseWriter, _ *http.Request) { |
| + tc.refuse(w) |
| + w.WriteHeader(http.StatusServiceUnavailable) |
| + }) |
| + carol := s.box(t, "carol", "127.0.0.3", 5, func(w http.ResponseWriter, r *http.Request) { |
| + io.WriteString(w, "carol|"+r.Host) |
| + }) |
| + s.refresh(t) |
| + owner := s.target(t).Owner |
| + if box := s.target(t).Box; box != "betty" { |
| + t.Fatalf("precondition: the route is served from %s, want betty", box) |
| + } |
| + |
| + w := s.send(t, pathHopRequest()) |
| + if w.Code != http.StatusOK || w.Body.String() != "carol|"+pathHopWire { |
| + t.Fatalf("followed hop: %d %q", w.Code, w.Body.String()) |
| + } |
| + c := &s.sender.edge.backhaulHolders |
| + if ref := c.get(pathHopRoute, owner, time.Now()); ref != "carol" { |
| + t.Fatalf("holder under %s = %q, want carol", pathHopRoute, ref) |
| + } |
| + c.mu.Lock() |
| + _, underWire := c.entries[pathHopWire] |
| + c.mu.Unlock() |
| + if underWire { |
| + t.Fatalf("a holder was remembered under the front name %s", pathHopWire) |
| + } |
| + |
| + // The next hop starts at the learned holder. |
| + w = s.send(t, pathHopRequest()) |
| + if w.Code != http.StatusOK || w.Body.String() != "carol|"+pathHopWire { |
| + t.Fatalf("second hop: %d %q", w.Code, w.Body.String()) |
| + } |
| + bHosts, _ := betty.seen() |
| + cHosts, cAuths := carol.seen() |
| + if len(bHosts) != 1 || len(cHosts) != 2 { |
| + t.Fatalf("calls betty=%v carol=%v, want 1 and 2", bHosts, cHosts) |
| + } |
| + key := signingKey(s.sender.homes.meshKeys()) |
| + for i, h := range append(bHosts, cHosts...) { |
| + if h != pathHopWire { |
| + t.Fatalf("request %d carried Host %q, want %s", i, h, pathHopWire) |
| + } |
| + } |
| + for _, a := range cAuths { |
| + if !verifyHop(key, pathHopWire, a, time.Now()) { |
| + t.Fatalf("followed hop MAC %q is not over %s", a, pathHopWire) |
| + } |
| + } |
| + }) |
| + } |
| +} |
| + |
| +// A destination that cannot be reached falls back to another serving box of |
| +// the route (backhaulAlternative keyed on the route), and the next hop skips |
| +// the suspect box for the route's alternative. |
| +func TestBackhaulRouteToFallsBackOnRoute(t *testing.T) { |
| + s := newPathHopScene(t) |
| + betty := s.box(t, "betty", "127.0.0.2", 9, func(w http.ResponseWriter, _ *http.Request) { |
| + io.WriteString(w, "betty") |
| + }) |
| + carol := s.box(t, "carol", "127.0.0.3", 5, func(w http.ResponseWriter, r *http.Request) { |
| + io.WriteString(w, "carol|"+r.Host) |
| + }) |
| + s.refresh(t) |
| + betty.srv.Close() |
| + for i := range 2 { |
| + w := s.send(t, pathHopRequest()) |
| + if w.Code != http.StatusOK || w.Body.String() != "carol|"+pathHopWire { |
| + t.Fatalf("hop %d: %d %q", i, w.Code, w.Body.String()) |
| + } |
| + } |
| + if hosts, _ := carol.seen(); len(hosts) != 2 { |
| + t.Fatalf("carol calls=%v, want 2", hosts) |
| + } |
| +} |
| + |
| +// One hop, ever: a request that arrived as a verified hop is refused, not |
| +// forwarded, whatever route it would have been for. |
| +func TestBackhaulRouteToRefusesArrivedHop(t *testing.T) { |
| + s := newPathHopScene(t) |
| + betty := s.box(t, "betty", "127.0.0.2", 9, func(w http.ResponseWriter, _ *http.Request) { |
| + io.WriteString(w, "betty") |
| + }) |
| + s.refresh(t) |
| + r := pathHopRequest() |
| + r = r.WithContext(context.WithValue(r.Context(), hopVerifiedKey{}, true)) |
| + w := s.send(t, r) |
| + if w.Code != http.StatusServiceUnavailable || w.Header().Get("Retry-After") != "2" { |
| + t.Fatalf("arrived hop: %d %v, want the stale-hop refusal", w.Code, w.Header()) |
| + } |
| + if hosts, _ := betty.seen(); len(hosts) != 0 { |
| + t.Fatalf("an arrived hop was forwarded: %v", hosts) |
| + } |
| +} |