| index 71de11028..55e3d5cd3 100644 |
| --- a/internal/host/haingress_startup_test.go |
| +++ b/internal/host/haingress_startup_test.go |
| @@ -3,9 +3,13 @@ package host |
| import ( |
| "context" |
| "crypto/ed25519" |
| + "crypto/rand" |
| + "crypto/tls" |
| "crypto/x509" |
| + "encoding/json" |
| "fmt" |
| "io" |
| + "math/big" |
| "net" |
| "net/http" |
| "net/http/httptest" |
| @@ -17,6 +21,7 @@ import ( |
| _ "unsafe" |
| |
| "github.com/carlosframework/platform/internal/activator" |
| + "github.com/carlosframework/platform/internal/domains" |
| "github.com/carlosframework/platform/internal/edge" |
| "github.com/carlosframework/platform/internal/hafresh" |
| "github.com/carlosframework/platform/internal/lease/locktest" |
| @@ -37,17 +42,39 @@ func TestHAIngressStartupWiring(t *testing.T) { |
| t.Fatal(err) |
| } |
| body := string(src) |
| - start := strings.Index(body, "runHAIngressStartup(agentCtx, openDeployStore, boxJudge)") |
| - if strings.Count(body, "runHAIngressStartup(agentCtx, openDeployStore, boxJudge)") != 1 || start < 0 || start < strings.Index(body, "armChangeIndex(agentCtx,") || start > strings.Index(body, "go instancesLoop(") { |
| + start := strings.Index(body, "runHAIngressStartup(agentCtx, openDeployStore, boxJudge, reg)") |
| + if strings.Count(body, "runHAIngressStartup(agentCtx, openDeployStore, boxJudge, reg)") != 1 || start < 0 || start < strings.Index(body, "armChangeIndex(agentCtx,") || start > strings.Index(body, "go instancesLoop(") { |
| t.Fatal("agent does not build the ingress projection at startup after arming the index and before starting instancesLoop") |
| } |
| } |
| |
| func TestHAIngressStartupForwards(t *testing.T) { |
| + testHAIngressStartupForwards(t, false) |
| +} |
| + |
| +func TestHAIngressStartupNames(t *testing.T) { |
| + testHAIngressStartupForwards(t, true) |
| +} |
| + |
| +func testHAIngressStartupForwards(t *testing.T, names bool) { |
| + failures := []string{"", "accounts/"} |
| + if names { |
| + failures = append(failures, domains.ClaimsPrefix, "control/edges/") |
| + } |
| for _, indexed := range []bool{false, true} { |
| - for _, fail := range []bool{false, true} { |
| - t.Run(fmt.Sprintf("indexed=%v/failure=%v", indexed, fail), func(t *testing.T) { |
| + for _, failure := range failures { |
| + t.Run(fmt.Sprintf("indexed=%v/failure=%s", indexed, failure), func(t *testing.T) { |
| disarmView(t) |
| + oldAliases := boxHAIngress.aliases.Load() |
| + boxHAIngress.aliases.Store(nil) |
| + aliasClaimCache.Reset() |
| + oldAuthority := boxAssetAuthority |
| + boxAssetAuthority = newAssetAuthority(nil) |
| + t.Cleanup(func() { |
| + boxHAIngress.aliases.Store(oldAliases) |
| + aliasClaimCache.Reset() |
| + boxAssetAuthority = oldAuthority |
| + }) |
| oldRoutes, oldNames, oldEpoch := boxHAIngress.routes.Load(), boxHAIngress.regional.Load(), boxHAIngress.epoch |
| boxHAIngress.routes.Store(nil) |
| boxHAIngress.regional.Store(nil) |
| @@ -66,6 +93,23 @@ func TestHAIngressStartupForwards(t *testing.T) { |
| if err := provision.CreateRecord(t.Context(), w.live, rec); err != nil { |
| t.Fatal(err) |
| } |
| + hosts := []string{rec.Host} |
| + if names { |
| + hosts = []string{"alias.example.net", "regional.example.net"} |
| + for _, claim := range []domains.Claim{ |
| + {Schema: 1, Host: hosts[0], Route: rec.Host, Account: rec.Account, App: rec.App}, |
| + {Schema: 2, Host: hosts[1], Route: rec.Host, Account: rec.Account, App: rec.App, Placements: []domains.Placement{{Region: "", Host: rec.Host}}}, |
| + } { |
| + claim.CreatedAt = time.Now().UTC().Format(time.RFC3339) |
| + data, err := json.Marshal(claim) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if _, err := w.live.Put(t.Context(), domains.ClaimKey(claim.Host), data, ""); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + } |
| if indexed { |
| armView(t, w.live) |
| } |
| @@ -90,20 +134,33 @@ func TestHAIngressStartupForwards(t *testing.T) { |
| } |
| t.Setenv("CARLOS_POOLS", "other") |
| stale := registry.Route{Host: rec.Host, Account: rec.Account, App: rec.App, Pool: rec.Pool, Hibernate: true, Socket: "/unused"} |
| - if err := reg.Upsert(stale); err != nil { |
| - t.Fatal(err) |
| + if !names { |
| + if err := reg.Upsert(stale); err != nil { |
| + t.Fatal(err) |
| + } |
| } |
| if _, err := w.live.Put(t.Context(), "control/edge-mesh-key", []byte(strings.Repeat("a", 64)), ""); err != nil { |
| t.Fatal(err) |
| } |
| var deliveries atomic.Int32 |
| - peer := httptest.NewTLSServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { |
| - if r.Header.Get("X-Carlos-Hop-Auth") == "" || r.Host != rec.Host { |
| + peer := httptest.NewUnstartedServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { |
| + if r.Header.Get("X-Carlos-Hop-Auth") == "" || (r.Host != rec.Host && r.Host != "alias.example.net" && r.Host != "regional.example.net") { |
| t.Errorf("unsigned or wrong host: %s %v", r.Host, r.Header) |
| } |
| deliveries.Add(1) |
| io.WriteString(rw, "forwarded") |
| })) |
| + pub, key, err := ed25519.GenerateKey(rand.Reader) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + cert := &x509.Certificate{SerialNumber: big.NewInt(1), DNSNames: []string{rec.Host, "*.example.net"}, 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, cert, cert, pub, key) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + peer.TLS = &tls.Config{Certificates: []tls.Certificate{{Certificate: [][]byte{der}, PrivateKey: key}}} |
| + peer.StartTLS() |
| defer peer.Close() |
| oldDial, oldRoots := startupForwardDial, startupBackhaulRoots |
| startupBackhaulRoots = x509.NewCertPool() |
| @@ -132,36 +189,39 @@ func TestHAIngressStartupForwards(t *testing.T) { |
| if !snapshot.Usable || !snapshot.LocalDenied || len(snapshot.Serving) != 1 || snapshot.Serving[0].BoxID != "holder" { |
| t.Fatalf("startup peer view: %+v", snapshot) |
| } |
| - request := func() *httptest.ResponseRecorder { |
| - r := httptest.NewRequest(http.MethodPost, "https://"+rec.Host+"/", strings.NewReader("once")) |
| + request := func(host string) *httptest.ResponseRecorder { |
| + r := httptest.NewRequest(http.MethodPost, "https://"+host+"/", strings.NewReader("once")) |
| rw := httptest.NewRecorder() |
| e.ServeHTTP(rw, r) |
| return rw |
| } |
| - if fail { |
| - w.live.setFailList("accounts/") |
| + if failure != "" { |
| + w.live.setFailList(failure) |
| } |
| ctx, cancel := context.WithCancel(t.Context()) |
| done := make(chan struct{}) |
| go func() { |
| defer close(done) |
| - runHAIngressStartup(ctx, func(context.Context) (release.Store, error) { return w.live, nil }, newBoxJudge(claimsOffEnv)) |
| + runHAIngressStartup(ctx, func(context.Context) (release.Store, error) { return startupAliasStore{w.live}, nil }, newBoxJudge(claimsOffEnv), reg) |
| }() |
| defer func() { cancel(); <-done }() |
| start := time.Now() |
| - if fail { |
| + if failure != "" { |
| deadline := time.Now().Add(time.Second) |
| - for boxHAIngress.routes.Load() == nil && time.Now().Before(deadline) { |
| + for (boxHAIngress.routes.Load() == nil || ((failure == domains.ClaimsPrefix || failure == "control/edges/") && boxHAIngress.aliases.Load() == nil)) && time.Now().Before(deadline) { |
| time.Sleep(time.Millisecond) |
| } |
| if boxHAIngress.routes.Load() == nil { |
| t.Fatal("initial failure was not published") |
| } |
| - if rw := request(); rw.Code != 503 || rw.Header().Get("X-Carlos-Holder") != "none" || deliveries.Load() != 0 { |
| - t.Fatalf("failed projection served: %d", rw.Code) |
| - } |
| - if rt, _ := boxHAIngress.lookup(rec.Host); rt.HA { |
| - t.Fatal("failed projection usable") |
| + for _, host := range hosts { |
| + rw := request(host) |
| + if (rw.Code != 404 && rw.Code != 503) || (!names && (rw.Code != 503 || rw.Header().Get("X-Carlos-Holder") != "none")) || deliveries.Load() != 0 { |
| + t.Fatalf("failed projection served %s: %d", host, rw.Code) |
| + } |
| + if rt, _ := boxHAIngress.lookup(host); rt.HA { |
| + t.Fatalf("failed projection usable for %s", host) |
| + } |
| } |
| w.live.setFailList("") |
| } |
| @@ -173,14 +233,19 @@ func TestHAIngressStartupForwards(t *testing.T) { |
| if rt, ok := boxHAIngress.lookup(rec.Host); !ok || !rt.HA { |
| t.Fatalf("startup projection missing: %+v %v", rt, ok) |
| } |
| - rw := request() |
| - if rw.Code != 200 || rw.Body.String() != "forwarded" || deliveries.Load() != 1 { |
| - t.Fatalf("startup forward: %d %q deliveries=%d", rw.Code, rw.Body.String(), deliveries.Load()) |
| + for _, host := range hosts { |
| + t.Run(host, func(t *testing.T) { |
| + before := deliveries.Load() |
| + rw := request(host) |
| + if rw.Code != 200 || rw.Body.String() != "forwarded" || deliveries.Load() != before+1 { |
| + t.Fatalf("startup forward %s: %d %q deliveries=%d", host, rw.Code, rw.Body.String(), deliveries.Load()-before) |
| + } |
| + }) |
| } |
| if time.Since(start) >= 10*time.Second { |
| t.Fatal("forward took 10 seconds") |
| } |
| - if rows, err := reg.List(); err != nil || len(rows) != 1 || rows[0].Host != stale.Host || rows[0].HA || rows[0].Provisioned { |
| + if rows, err := reg.List(); err != nil || (names && len(rows) != 0) || (!names && (len(rows) != 1 || rows[0].Host != stale.Host || rows[0].HA || rows[0].Provisioned)) { |
| t.Fatalf("ingress acquired local routes: %+v %v", rows, err) |
| } |
| for _, req := range fx.Requests() { |
| @@ -201,7 +266,7 @@ func TestHAIngressStartupCancellation(t *testing.T) { |
| calls++ |
| cancel() |
| return nil, fmt.Errorf("store unavailable") |
| - }, nil) |
| + }, nil, nil) |
| if calls != 1 { |
| t.Fatalf("cancelled startup opened store %d times", calls) |
| } |
| @@ -221,3 +286,24 @@ func TestHAIngressStartupLockDeadline(t *testing.T) { |
| t.Fatalf("startup lock deadline: %v, %s", err, time.Since(start)) |
| } |
| } |
| + |
| +func TestHAIngressStartupAliasLockDeadline(t *testing.T) { |
| + aliasPassMu.Lock() |
| + defer aliasPassMu.Unlock() |
| + ctx, cancel := context.WithTimeout(t.Context(), 30*time.Millisecond) |
| + defer cancel() |
| + start := time.Now() |
| + complete, err := aliasPassReportingComplete(ctx, nil, nil, &aliasClaimCache, boxAssetAuthority, "") |
| + if complete || err != context.DeadlineExceeded || time.Since(start) > time.Second { |
| + t.Fatalf("startup alias lock deadline: complete=%v err=%v elapsed=%s", complete, err, time.Since(start)) |
| + } |
| +} |
| + |
| +type startupAliasStore struct{ *ciCountingStore } |
| + |
| +func (s startupAliasStore) ListMod(ctx context.Context, prefix string) ([]release.KeyMod, error) { |
| + if s.listFails(prefix) { |
| + return nil, fmt.Errorf("list mods refused") |
| + } |
| + return s.DirStore.ListMod(ctx, prefix) |
| +} |