| new file mode 100644 |
| index 000000000..f9e4241de |
| --- /dev/null |
| +++ b/internal/host/edgesfile_import_test.go |
| @@ -0,0 +1,177 @@ |
| +package host |
| + |
| +import ( |
| + "context" |
| + "crypto/ecdsa" |
| + "crypto/elliptic" |
| + "crypto/rand" |
| + "crypto/x509" |
| + "crypto/x509/pkix" |
| + "encoding/json" |
| + "math/big" |
| + "os" |
| + "path/filepath" |
| + "sync" |
| + "testing" |
| + "time" |
| + |
| + "github.com/carlosframework/platform/internal/domains" |
| + "github.com/carlosframework/platform/internal/registry" |
| + "github.com/carlosframework/platform/internal/release" |
| +) |
| + |
| +// writeImportEntry writes an imported-certificate entry (#1429) for host into |
| +// acmeDir, as the edge's mirror copies it from control/certs/<host>+import: |
| +// the binding (bound to account), the PKCS#8 key, then the leaf, valid until |
| +// notAfter. |
| +func writeImportEntry(t *testing.T, acmeDir, host, account string, notAfter time.Time) { |
| + t.Helper() |
| + key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + tmpl := &x509.Certificate{SerialNumber: big.NewInt(1), Subject: pkix.Name{CommonName: host}, DNSNames: []string{host}, |
| + NotBefore: time.Now().Add(-48 * time.Hour), NotAfter: notAfter} |
| + der, err := x509.CreateCertificate(rand.Reader, tmpl, tmpl, &key.PublicKey, key) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + pkcs8, err := x509.MarshalPKCS8PrivateKey(key) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + body := domains.EncodeImport(domains.ImportBinding{Host: host, Account: account, App: "app", ClaimCreated: "2026-11-25T09:00:00Z"}, pkcs8, [][]byte{der}) |
| + if err := os.MkdirAll(acmeDir, 0o700); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := os.WriteFile(filepath.Join(acmeDir, domains.ImportName(host)), body, 0o600); err != nil { |
| + t.Fatal(err) |
| + } |
| +} |
| + |
| +type importHostView struct { |
| + Role string `json:"role"` |
| + Alias bool `json:"alias"` |
| + TargetHost string `json:"target_host"` |
| + DNSPointing *bool `json:"dns_pointing"` |
| +} |
| + |
| +func publishedHosts(t *testing.T, s release.Store, box string) map[string]importHostView { |
| + t.Helper() |
| + body, _, err := s.Get(context.Background(), edgesFileKey(box)) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + var f struct { |
| + Hosts map[string]importHostView `json:"hosts"` |
| + } |
| + if err := json.Unmarshal(body, &f); err != nil { |
| + t.Fatal(err) |
| + } |
| + return f.Hosts |
| +} |
| + |
| +// #1429: an alias whose imported certificate sits in the edge's cache, bound |
| +// to the alias row's account, is published before its DNS points here, |
| +// marked dns_pointing false, with no DNS-01 scope configured at all. Another |
| +// account's import, no import, or an expired one publishes nothing early. |
| +func TestEdgesFilePublishesImportedAliasesBeforeDNS(t *testing.T) { |
| + withheldAliasSaid = sync.Map{} |
| + t.Setenv("PLATFORM_APPS_DOMAIN", "apps.test") |
| + t.Setenv("CARLOS_DNS01_ZONES", "") |
| + t.Setenv("CARLOS_DNS01_ACCOUNTS", "") |
| + s, reg := release.NewDirStore(t.TempDir()), edgesTestRegistry(t) |
| + if err := reg.Upsert(registry.Route{Host: "app.acct.apps.test", Kind: "service", Addr: "10.0.0.1:3000", Status: "ready", Platform: true}); err != nil { |
| + t.Fatal(err) |
| + } |
| + acme := filepath.Join(t.TempDir(), "acme") |
| + future, past := time.Now().Add(60*24*time.Hour), time.Now().Add(-time.Minute) |
| + for _, a := range []struct { |
| + alias, aliasAccount, importAccount string |
| + notAfter time.Time |
| + }{ |
| + {"billing.customer.example", "acct", "acct", future}, // the case |
| + {"otheracct.customer.example", "acct", "zzz", future}, // bound to another account |
| + {"noimport.customer.example", "acct", "", future}, // no import |
| + {"expired.customer.example", "acct", "acct", past}, // expired |
| + {"wrongrow.customer.example", "zzz", "acct", future}, // the alias row is another account's |
| + } { |
| + if err := reg.UpsertAlias(registry.RouteAlias{Alias: a.alias, Host: "app.acct.apps.test", Account: a.aliasAccount, App: "app", DNSStatus: "elsewhere"}); err != nil { |
| + t.Fatal(err) |
| + } |
| + if a.importAccount != "" { |
| + writeImportEntry(t, acme, a.alias, a.importAccount, a.notAfter) |
| + } |
| + } |
| + publish := func() map[string]importHostView { |
| + t.Helper() |
| + var last []byte |
| + if err := edgesFilePass(context.Background(), s, reg, edgesFile{Box: "annie", AcmeDir: acme}, "1.2.3.4", &last); err != nil { |
| + t.Fatal(err) |
| + } |
| + return publishedHosts(t, s, "annie") |
| + } |
| + hosts := publish() |
| + if h, ok := hosts["billing.customer.example"]; !ok || h.Role != "home" || !h.Alias || h.TargetHost != "app.acct.apps.test" || h.DNSPointing == nil || *h.DNSPointing { |
| + t.Errorf("billing.customer.example = %+v (published %v); want home, alias, dns_pointing false", h, ok) |
| + } |
| + for _, absent := range []string{"otheracct.customer.example", "noimport.customer.example", "expired.customer.example", "wrongrow.customer.example"} { |
| + if _, ok := hosts[absent]; ok { |
| + t.Errorf("%s must not be published before it points here", absent) |
| + } |
| + } |
| + |
| + // The mirror removing the disk copy takes the name back out. |
| + if err := os.Remove(filepath.Join(acme, domains.ImportName("billing.customer.example"))); err != nil { |
| + t.Fatal(err) |
| + } |
| + if _, ok := publish()["billing.customer.example"]; ok { |
| + t.Error("billing.customer.example still published after its import was removed") |
| + } |
| +} |
| + |
| +// The real use (#1429): a fleet app's custom domain. The hub holds the fleet |
| +// app's forward row and its alias, and publishes them in its own edges file; |
| +// region edges terminate TLS from control/certs and relay to the hub. A |
| +// usable import in the hub's cache publishes the alias early, marked |
| +// dns_pointing false, served by the forward row. |
| +func TestEdgesFileHubPublishesAFleetAliasWithAnImportBeforeDNS(t *testing.T) { |
| + setHubEnv(t, fpTunnel, "") |
| + t.Setenv("PLATFORM_APPS_DOMAIN", "example.com") |
| + t.Setenv("CARLOS_DNS01_ZONES", "") |
| + withheldAliasSaid = sync.Map{} |
| + reg := fpRegistry(t) |
| + fpPutForward(t, reg, fpForward(fpEligible, "home", fpOwner)) |
| + const alias = "shop.customer.example" |
| + if err := reg.UpsertAlias(registry.RouteAlias{Alias: alias, Host: fpEligible, Account: fpOwner, App: "shop", DNSStatus: "elsewhere"}); err != nil { |
| + t.Fatal(err) |
| + } |
| + acme := filepath.Join(t.TempDir(), "acme") |
| + s := release.NewDirStore(t.TempDir()) |
| + write := func() map[string]importHostView { |
| + t.Helper() |
| + var last []byte |
| + if err := edgesFileWriteClaims(context.Background(), s, reg, edgesFile{Box: "hub", AcmeDir: acme}, "198.51.100.7", &last, false, nil, false, nil, nil); err != nil { |
| + t.Fatal(err) |
| + } |
| + return publishedHosts(t, s, "hub") |
| + } |
| + if _, ok := write()[alias]; ok { |
| + t.Fatal("without an import the fleet alias must wait for the pointing stamp") |
| + } |
| + writeImportEntry(t, acme, alias, fpOwner, time.Now().Add(60*24*time.Hour)) |
| + hosts := write() |
| + if _, ok := hosts[fpEligible]; !ok { |
| + t.Fatal("test bug: the hub must publish the eligible forward row") |
| + } |
| + h, ok := hosts[alias] |
| + if !ok || h.Role != "home" || !h.Alias || h.TargetHost != fpEligible || h.DNSPointing == nil || *h.DNSPointing { |
| + t.Fatalf("%s = %+v (published %v); want home, alias of %s, dns_pointing false", alias, h, ok, fpEligible) |
| + } |
| + |
| + // An import bound to another account publishes nothing early. |
| + writeImportEntry(t, acme, alias, fpOther, time.Now().Add(60*24*time.Hour)) |
| + if _, ok := write()[alias]; ok { |
| + t.Fatal("an import bound to another account published the fleet alias") |
| + } |
| +} |