|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "net/http/httptest" |
| 6 | + "sync/atomic" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/ipfs/kubo/config" |
| 11 | + "github.com/ipfs/kubo/test/cli/harness" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | +) |
| 14 | + |
| 15 | +// autotlsPostponedMsg is the marker of the ERROR the p2p-forge client logs |
| 16 | +// when its pre-issuance broker health check fails and certificate setup is |
| 17 | +// postponed until the broker recovers. |
| 18 | +const autotlsPostponedMsg = "certificate setup postponed" |
| 19 | + |
| 20 | +// wssWildcardFragment appears in swarm listen addrs only when the AutoTLS |
| 21 | +// machinery was wired up (AutoWSS appended the wildcard WSS listener). |
| 22 | +const wssWildcardFragment = "/tls/sni/" |
| 23 | + |
| 24 | +// unroutableURL is guaranteed to refuse connections without depending on the |
| 25 | +// state of any real port (port 0 is never connectable). |
| 26 | +const unroutableURL = "http://127.0.0.1:0" |
| 27 | + |
| 28 | +// countingBroker is a fake p2p-forge broker that records /v1/health probes. |
| 29 | +func countingBroker(t *testing.T, status int) (*httptest.Server, *atomic.Int32) { |
| 30 | + t.Helper() |
| 31 | + var probes atomic.Int32 |
| 32 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 33 | + if r.URL.Path == "/v1/health" { |
| 34 | + probes.Add(1) |
| 35 | + w.WriteHeader(status) |
| 36 | + return |
| 37 | + } |
| 38 | + w.WriteHeader(http.StatusNotFound) |
| 39 | + })) |
| 40 | + t.Cleanup(srv.Close) |
| 41 | + return srv, &probes |
| 42 | +} |
| 43 | + |
| 44 | +// autotlsNode inits a node with AutoTLS explicitly enabled against the given |
| 45 | +// broker URL. Forced public reachability plus an announced public address |
| 46 | +// satisfy the conditions the p2p-forge client waits for before it contacts |
| 47 | +// the broker, so the pre-issuance health check can be observed in an |
| 48 | +// isolated test. CAEndpoint points at an unroutable address so no test can |
| 49 | +// ever reach a real ACME CA, even when issuance starts. GOLOG env is pinned |
| 50 | +// so stderr assertions do not depend on ambient GOLOG_* variables. |
| 51 | +func autotlsNode(t *testing.T, brokerURL string) *harness.Node { |
| 52 | + node := harness.NewT(t).NewNode().Init("--profile=test") |
| 53 | + node.Runner.Env["GOLOG_LOG_LEVEL"] = "error" |
| 54 | + node.Runner.Env["GOLOG_OUTPUT"] = "stderr" |
| 55 | + node.UpdateConfig(func(cfg *config.Config) { |
| 56 | + cfg.AutoTLS.Enabled = config.True |
| 57 | + cfg.AutoTLS.RegistrationEndpoint = config.NewOptionalString(brokerURL) |
| 58 | + cfg.AutoTLS.CAEndpoint = config.NewOptionalString(unroutableURL) |
| 59 | + cfg.Internal.Libp2pForceReachability = config.NewOptionalString("public") |
| 60 | + cfg.Addresses.Announce = []string{"/ip4/1.2.3.4/tcp/4001"} |
| 61 | + }) |
| 62 | + return node |
| 63 | +} |
| 64 | + |
| 65 | +func TestAutoTLSBrokerHealthCheck(t *testing.T) { |
| 66 | + t.Parallel() |
| 67 | + |
| 68 | + t.Run("registration delay defers any broker traffic", func(t *testing.T) { |
| 69 | + t.Parallel() |
| 70 | + broker, probes := countingBroker(t, http.StatusNoContent) |
| 71 | + |
| 72 | + node := autotlsNode(t, broker.URL) |
| 73 | + node.UpdateConfig(func(cfg *config.Config) { |
| 74 | + // implicit enable: the default 1h registration delay applies, so |
| 75 | + // an ephemeral node like this one must produce no broker traffic |
| 76 | + // at all, even while it looks publicly reachable |
| 77 | + cfg.AutoTLS.Enabled = config.Default |
| 78 | + }) |
| 79 | + node.StartDaemon() |
| 80 | + defer node.StopDaemon() |
| 81 | + |
| 82 | + require.Never(t, func() bool { return probes.Load() > 0 }, 2*time.Second, 100*time.Millisecond, |
| 83 | + "daemon must not contact broker before the registration delay") |
| 84 | + require.NotContains(t, node.Daemon.Stderr.String(), autotlsPostponedMsg) |
| 85 | + }) |
| 86 | + |
| 87 | + t.Run("postpones issuance while broker is unhealthy", func(t *testing.T) { |
| 88 | + t.Parallel() |
| 89 | + broker, probes := countingBroker(t, http.StatusServiceUnavailable) |
| 90 | + |
| 91 | + node := autotlsNode(t, broker.URL) |
| 92 | + node.UpdateConfig(func(cfg *config.Config) { |
| 93 | + cfg.AutoTLS.RegistrationDelay = config.NewOptionalDuration(1 * time.Second) |
| 94 | + }) |
| 95 | + node.StartDaemon() |
| 96 | + defer node.StopDaemon() |
| 97 | + |
| 98 | + require.True(t, waitForLogMessage(node.Daemon.Stderr, autotlsPostponedMsg, 15*time.Second), |
| 99 | + "p2p-forge client should postpone certificate setup when broker is unhealthy") |
| 100 | + require.GreaterOrEqual(t, probes.Load(), int32(1)) |
| 101 | + }) |
| 102 | + |
| 103 | + t.Run("proceeds with issuance when broker is healthy", func(t *testing.T) { |
| 104 | + t.Parallel() |
| 105 | + broker, probes := countingBroker(t, http.StatusNoContent) |
| 106 | + |
| 107 | + node := autotlsNode(t, broker.URL) |
| 108 | + node.UpdateConfig(func(cfg *config.Config) { |
| 109 | + cfg.AutoTLS.RegistrationDelay = config.NewOptionalDuration(1 * time.Second) |
| 110 | + }) |
| 111 | + node.StartDaemon() |
| 112 | + defer node.StopDaemon() |
| 113 | + |
| 114 | + require.Eventually(t, func() bool { return probes.Load() >= 1 }, 15*time.Second, 100*time.Millisecond, |
| 115 | + "p2p-forge client should probe broker health before issuance") |
| 116 | + require.NotContains(t, node.Daemon.Stderr.String(), autotlsPostponedMsg) |
| 117 | + |
| 118 | + // AutoTLS machinery is wired up: AutoWSS added the wildcard listener |
| 119 | + listenAddrs := node.IPFS("swarm", "addrs", "listen").Stdout.String() |
| 120 | + require.Contains(t, listenAddrs, wssWildcardFragment) |
| 121 | + }) |
| 122 | + |
| 123 | + t.Run("explicit enable goes through the same health check", func(t *testing.T) { |
| 124 | + t.Parallel() |
| 125 | + broker, probes := countingBroker(t, http.StatusServiceUnavailable) |
| 126 | + |
| 127 | + // AutoTLS.Enabled=true with no custom delay means a zero registration |
| 128 | + // delay: the health check runs as soon as the node looks publicly |
| 129 | + // reachable, through the same code path as the delayed flow |
| 130 | + node := autotlsNode(t, broker.URL) |
| 131 | + node.StartDaemon() |
| 132 | + defer node.StopDaemon() |
| 133 | + |
| 134 | + require.True(t, waitForLogMessage(node.Daemon.Stderr, autotlsPostponedMsg, 15*time.Second), |
| 135 | + "explicitly enabled AutoTLS should postpone certificate setup when broker is unhealthy") |
| 136 | + require.GreaterOrEqual(t, probes.Load(), int32(1)) |
| 137 | + }) |
| 138 | +} |
0 commit comments