package edge import ( "context" "encoding/pem" "fmt" "io" "net" "os" "path/filepath" "testing" "github.com/carlosframework/platform/internal/release" ) const dbfBackhaulHost = "console.backhaul.test" // TestDBForwardBackhaulPairHelper is not a test of its own: the root // package's TestDBForwardThroughTheBackhaulHop builds this package's test // binary and runs this as a helper process. It stands up two real edges // over one bucket, as TestBackhaulE2EAcrossTwoEdges does: annie, home of // console.backhaul.test, routed to DBF_BACKHAUL_UPSTREAM (the real console // the parent serves), and betty, which serves no host and so backhauls every // request for it to annie. It writes the test CA to DBF_BACKHAUL_DIR/ca.pem, // prints betty's TLS address on a line starting "DBF-BACKHAUL ", and serves // until its stdin closes. func TestDBForwardBackhaulPairHelper(t *testing.T) { if os.Getenv("DBF_BACKHAUL_HELPER") != "1" { t.Skip("a helper process for the root package's TestDBForwardThroughTheBackhaulHop") } upstream, dir := os.Getenv("DBF_BACKHAUL_UPSTREAM"), os.Getenv("DBF_BACKHAUL_DIR") e2eQuietLogs(t) bucket := t.TempDir() seed := release.NewDirStore(bucket) if err := MintMeshKey(context.Background(), seed); err != nil { t.Fatal(err) } ca := newE2ECA(t) ca.publish(t, seed, dbfBackhaulHost) home := newBackhaulEdge(t, bucket, "annie", ca, nil, map[string]string{dbfBackhaulHost: upstream}) sender := newBackhaulEdge(t, bucket, "betty", ca, nil, nil) eip, port, err := net.SplitHostPort(home.tlsAddr) if err != nil { t.Fatal(err) } oldPort, oldCAs := backhaulPort, backhaulRootCAs backhaulPort, backhaulRootCAs = port, ca.pool t.Cleanup(func() { backhaulPort, backhaulRootCAs = oldPort, oldCAs }) putJSON(t, seed, "control/edges/annie.json", fmt.Sprintf(`{"v":1,"box":"annie","eip":%q,"hosts":{%q:{"role":"home"}}}`, eip, dbfBackhaulHost)) bEIP, _, err := net.SplitHostPort(sender.tlsAddr) if err != nil { t.Fatal(err) } putJSON(t, seed, "control/edges/betty.json", fmt.Sprintf(`{"v":1,"box":"betty","eip":%q,"hosts":{}}`, bEIP)) for _, be := range []*backhaulEdge{home, sender} { if err := be.mirror.Refresh(context.Background()); err != nil { t.Fatal(err) } refreshHomeMap(t, be.homes) } caPEM := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: ca.root.Raw}) if err := os.WriteFile(filepath.Join(dir, "ca.pem"), caPEM, 0o600); err != nil { t.Fatal(err) } fmt.Println("DBF-BACKHAUL " + sender.tlsAddr) io.Copy(io.Discard, os.Stdin) }