|
| 1 | +package integration_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "net" |
| 8 | + "net/http" |
| 9 | + "strconv" |
| 10 | + "strings" |
| 11 | + "time" |
| 12 | + |
| 13 | + "github.com/telepresenceio/clog" |
| 14 | + "github.com/telepresenceio/telepresence/v2/integration_test/itest" |
| 15 | + "github.com/telepresenceio/telepresence/v2/pkg/iputil" |
| 16 | +) |
| 17 | + |
| 18 | +type h2cInterceptSuite struct { |
| 19 | + itest.Suite |
| 20 | + itest.TrafficManager |
| 21 | +} |
| 22 | + |
| 23 | +func (s *h2cInterceptSuite) SuiteName() string { |
| 24 | + return "H2CIntercept" |
| 25 | +} |
| 26 | + |
| 27 | +func init() { |
| 28 | + itest.AddConnectedSuite("", func(h itest.TrafficManager) itest.TestingSuite { |
| 29 | + return &h2cInterceptSuite{Suite: itest.Suite{Harness: h}, TrafficManager: h} |
| 30 | + }) |
| 31 | +} |
| 32 | + |
| 33 | +func (s *h2cInterceptSuite) SetupSuite() { |
| 34 | + if !(s.ManagerIsVersion(">2.26.x") && s.ClientIsVersion(">2.26.x")) { |
| 35 | + s.T().Skip("H2C intercepts require Telepresence 2.27.0 or later") |
| 36 | + } |
| 37 | + s.Suite.SetupSuite() |
| 38 | +} |
| 39 | + |
| 40 | +// startLocalH2CEchoServer starts a local HTTP server that supports h2c (HTTP/2 cleartext) |
| 41 | +// and echoes a line with the given name and the current URL path. |
| 42 | +func startLocalH2CEchoServer(ctx context.Context, name string) (int, context.CancelFunc) { |
| 43 | + ctx, cancel := context.WithCancel(ctx) |
| 44 | + lc := net.ListenConfig{} |
| 45 | + l, err := lc.Listen(ctx, "tcp", "localhost:0") |
| 46 | + if err != nil { |
| 47 | + cancel() |
| 48 | + panic(fmt.Sprintf("failed to listen: %v", err)) |
| 49 | + } |
| 50 | + pr := new(http.Protocols) |
| 51 | + pr.SetHTTP1(true) |
| 52 | + pr.SetUnencryptedHTTP2(true) |
| 53 | + sc := &http.Server{ |
| 54 | + Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 55 | + fmt.Fprintf(w, "%s from intercept at %s", name, r.URL.Path) |
| 56 | + }), |
| 57 | + Protocols: pr, |
| 58 | + } |
| 59 | + go func() { |
| 60 | + _ = sc.Serve(l) |
| 61 | + }() |
| 62 | + go func() { |
| 63 | + <-ctx.Done() |
| 64 | + sdCtx, sdCancel := context.WithTimeout(context.WithoutCancel(ctx), time.Second) |
| 65 | + defer sdCancel() |
| 66 | + _ = sc.Shutdown(sdCtx) |
| 67 | + }() |
| 68 | + return l.Addr().(*net.TCPAddr).Port, cancel |
| 69 | +} |
| 70 | + |
| 71 | +func (s *h2cInterceptSuite) Test_H2CInterceptPreservesProtocol() { |
| 72 | + ctx := s.Context() |
| 73 | + require := s.Require() |
| 74 | + |
| 75 | + const svc = "echo-h2c" |
| 76 | + |
| 77 | + // Deploy echo-server with appProtocol: kubernetes.io/h2c |
| 78 | + itest.ApplyAppTemplate(ctx, s.AppNamespace(), &itest.AppData{ |
| 79 | + AppName: svc, |
| 80 | + Ports: []itest.AppPort{ |
| 81 | + { |
| 82 | + ServicePortNumber: 80, |
| 83 | + TargetPortNumber: 8080, |
| 84 | + AppProtocol: "kubernetes.io/h2c", |
| 85 | + }, |
| 86 | + }, |
| 87 | + Env: map[string]string{"PORTS": "8080:http"}, |
| 88 | + }) |
| 89 | + defer itest.DeleteSvcAndWorkload(ctx, "deploy", svc, s.AppNamespace()) |
| 90 | + |
| 91 | + // Start a local h2c-capable echo server to receive intercepted traffic. |
| 92 | + // The upstream handler must support the same protocol as the app. |
| 93 | + localPort, cancel := startLocalH2CEchoServer(ctx, svc) |
| 94 | + defer cancel() |
| 95 | + |
| 96 | + // Create a personal HTTP intercept with a header filter |
| 97 | + stdout, stderr, err := itest.Telepresence(ctx, "intercept", svc, |
| 98 | + "--http-header", "x-test=h2c", |
| 99 | + "--port", strconv.Itoa(localPort)+":80", |
| 100 | + "--mount=false") |
| 101 | + require.NoError(err, "stderr: %s", stderr) |
| 102 | + require.Contains(stdout, "Using Deployment") |
| 103 | + |
| 104 | + // Capture traffic-agent logs for debugging |
| 105 | + s.CapturePodLogs(ctx, svc, "traffic-agent", s.AppNamespace()) |
| 106 | + |
| 107 | + // Verify non-intercepted traffic uses HTTP/2 (h2c). |
| 108 | + // The intercept is active so traffic goes through the agent's HTTP handler. |
| 109 | + // Requests without the x-test header don't match the intercept and are forwarded |
| 110 | + // by the default reverse proxy. The fix ensures that this proxy uses h2c prior |
| 111 | + // knowledge, so the echo-server sees HTTP/2.0. |
| 112 | + require.Eventually(func() bool { |
| 113 | + ips, err := net.DefaultResolver.LookupIP(ctx, "ip", svc) |
| 114 | + if err != nil { |
| 115 | + clog.Info(ctx, err) |
| 116 | + return false |
| 117 | + } |
| 118 | + ips = iputil.UniqueSorted(ips) |
| 119 | + if len(ips) != 1 { |
| 120 | + clog.Infof(ctx, "Lookup for %s returned %v", svc, ips) |
| 121 | + return false |
| 122 | + } |
| 123 | + hc := http.Client{Timeout: 2 * time.Second} |
| 124 | + rq, err := http.NewRequestWithContext(ctx, http.MethodGet, fmt.Sprintf("http://%s", net.JoinHostPort(ips[0].String(), "80")), nil) |
| 125 | + if err != nil { |
| 126 | + clog.Info(ctx, err) |
| 127 | + return false |
| 128 | + } |
| 129 | + resp, err := hc.Do(rq) |
| 130 | + if err != nil { |
| 131 | + clog.Info(ctx, err) |
| 132 | + return false |
| 133 | + } |
| 134 | + defer resp.Body.Close() |
| 135 | + body, err := io.ReadAll(resp.Body) |
| 136 | + if err != nil { |
| 137 | + clog.Info(ctx, err) |
| 138 | + return false |
| 139 | + } |
| 140 | + r := string(body) |
| 141 | + clog.Infof(ctx, "non-intercepted response: %s", r) |
| 142 | + return strings.Contains(r, "HTTP/2.0 GET /") |
| 143 | + }, 30*time.Second, 3*time.Second, "expected HTTP/2.0 in non-intercepted response") |
| 144 | + |
| 145 | + // Verify intercepted traffic reaches the local h2c handler |
| 146 | + itest.PingInterceptedEchoServer(ctx, svc, "80", "x-test=h2c") |
| 147 | + |
| 148 | + // Leave the intercept |
| 149 | + _, _, err = itest.Telepresence(ctx, "leave", svc) |
| 150 | + require.NoError(err) |
| 151 | +} |
0 commit comments