|
| 1 | +package auth |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "net" |
| 8 | + "net/http" |
| 9 | + "net/http/httptest" |
| 10 | + "net/http/httptrace" |
| 11 | + "strings" |
| 12 | + "sync/atomic" |
| 13 | + "testing" |
| 14 | + "time" |
| 15 | +) |
| 16 | + |
| 17 | +func TestHTTPKeyFetcherRetryCancellation(t *testing.T) { |
| 18 | + for _, duringBody := range []bool{false, true} { |
| 19 | + t.Run(fmt.Sprint(duringBody), func(t *testing.T) { |
| 20 | + var count atomic.Int32 |
| 21 | + started := make(chan struct{}) |
| 22 | + srv := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 23 | + if count.Add(1) == 1 { |
| 24 | + close(started) |
| 25 | + } |
| 26 | + if duringBody { |
| 27 | + w.WriteHeader(http.StatusOK) |
| 28 | + w.(http.Flusher).Flush() |
| 29 | + <-r.Context().Done() |
| 30 | + } else { |
| 31 | + w.WriteHeader(http.StatusServiceUnavailable) |
| 32 | + } |
| 33 | + })) |
| 34 | + defer srv.Close() |
| 35 | + ctx, cancel := context.WithCancel(context.Background()) |
| 36 | + defer cancel() |
| 37 | + done := make(chan error, 1) |
| 38 | + go func() { _, err := newLocalTLSFetcher(srv).FetchKey(ctx, internalTestDomain); done <- err }() |
| 39 | + <-started |
| 40 | + time.Sleep(50 * time.Millisecond) |
| 41 | + cancel() |
| 42 | + select { |
| 43 | + case err := <-done: |
| 44 | + if !errors.Is(err, context.Canceled) { |
| 45 | + t.Fatalf("error = %v", err) |
| 46 | + } |
| 47 | + case <-time.After(time.Second): |
| 48 | + t.Fatal("cancellation did not promptly stop fetch") |
| 49 | + } |
| 50 | + if count.Load() != 1 { |
| 51 | + t.Fatalf("requests = %d", count.Load()) |
| 52 | + } |
| 53 | + }) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +func TestHTTPKeyFetcherRetryHonorsEarlierCallerDeadline(t *testing.T) { |
| 58 | + var count atomic.Int32 |
| 59 | + srv := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 60 | + count.Add(1) |
| 61 | + w.WriteHeader(http.StatusOK) |
| 62 | + w.(http.Flusher).Flush() |
| 63 | + <-r.Context().Done() |
| 64 | + })) |
| 65 | + defer srv.Close() |
| 66 | + ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond) |
| 67 | + defer cancel() |
| 68 | + started := time.Now() |
| 69 | + _, err := newLocalTLSFetcher(srv).FetchKey(ctx, internalTestDomain) |
| 70 | + if !errors.Is(err, context.DeadlineExceeded) || count.Load() != 1 || time.Since(started) > time.Second { |
| 71 | + t.Fatalf("err=%v requests=%d elapsed=%s", err, count.Load(), time.Since(started)) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +func TestHTTPKeyFetcherRetryRecoversAfterAttemptTimeout(t *testing.T) { |
| 76 | + var count atomic.Int32 |
| 77 | + srv := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 78 | + if count.Add(1) == 1 { |
| 79 | + <-r.Context().Done() |
| 80 | + return |
| 81 | + } |
| 82 | + _, _ = w.Write([]byte("recovered")) |
| 83 | + })) |
| 84 | + defer srv.Close() |
| 85 | + fetcher := newLocalTLSFetcher(srv) |
| 86 | + fetcher.client.Timeout = 200 * time.Millisecond |
| 87 | + key, err := fetcher.FetchKey(context.Background(), internalTestDomain) |
| 88 | + if err != nil || key != "recovered" || count.Load() != 2 { |
| 89 | + t.Fatalf("key=%q err=%v requests=%d", key, err, count.Load()) |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +type failingKeyTransport struct { |
| 94 | + err error |
| 95 | + requests int |
| 96 | +} |
| 97 | + |
| 98 | +func (t *failingKeyTransport) RoundTrip(_ *http.Request) (*http.Response, error) { |
| 99 | + t.requests++ |
| 100 | + return nil, t.err |
| 101 | +} |
| 102 | + |
| 103 | +func TestHTTPKeyFetcherRetryRejectsPermanentTransportFailures(t *testing.T) { |
| 104 | + for _, tc := range []struct { |
| 105 | + name string |
| 106 | + err error |
| 107 | + }{ |
| 108 | + {"blocked-address", fmt.Errorf("dial example.com: %w", errHTTPKeyAddressBlocked)}, |
| 109 | + {"nonexistent-domain", &net.DNSError{Err: "no such host", Name: internalTestDomain, IsNotFound: true}}, |
| 110 | + } { |
| 111 | + t.Run(tc.name, func(t *testing.T) { |
| 112 | + transport := &failingKeyTransport{err: tc.err} |
| 113 | + fetcher := &DefaultHTTPKeyFetcher{client: newHTTPKeyFetcherClient(transport)} |
| 114 | + _, err := fetcher.FetchKey(context.Background(), internalTestDomain) |
| 115 | + if !errors.Is(err, tc.err) || transport.requests != 1 { |
| 116 | + t.Fatalf("err=%v requests=%d", err, transport.requests) |
| 117 | + } |
| 118 | + }) |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +func TestHTTPKeyFetcherRetryBodyAndRedirectBoundaries(t *testing.T) { |
| 123 | + for _, scenario := range []string{"truncated", "oversized", "redirect-after-retry"} { |
| 124 | + t.Run(scenario, func(t *testing.T) { |
| 125 | + var count atomic.Int32 |
| 126 | + var redirected atomic.Int32 |
| 127 | + target := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { redirected.Add(1); w.WriteHeader(http.StatusOK) })) |
| 128 | + defer target.Close() |
| 129 | + srv := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 130 | + attempt := count.Add(1) |
| 131 | + switch scenario { |
| 132 | + case "truncated": |
| 133 | + if attempt == 1 { |
| 134 | + w.Header().Set("Content-Length", "1000") |
| 135 | + _, _ = w.Write([]byte("partial")) |
| 136 | + return |
| 137 | + } |
| 138 | + _, _ = w.Write([]byte("complete")) |
| 139 | + case "oversized": |
| 140 | + _, _ = w.Write([]byte(strings.Repeat("x", MaxKeyResponseSize+1))) |
| 141 | + case "redirect-after-retry": |
| 142 | + if attempt == 1 { |
| 143 | + w.WriteHeader(http.StatusServiceUnavailable) |
| 144 | + return |
| 145 | + } |
| 146 | + w.Header().Set("Location", target.URL) |
| 147 | + w.WriteHeader(http.StatusFound) |
| 148 | + } |
| 149 | + })) |
| 150 | + defer srv.Close() |
| 151 | + key, err := newLocalTLSFetcher(srv).FetchKey(context.Background(), internalTestDomain) |
| 152 | + want := int32(2) |
| 153 | + if scenario == "truncated" { |
| 154 | + if err != nil || key != "complete" { |
| 155 | + t.Fatalf("key=%q err=%v", key, err) |
| 156 | + } |
| 157 | + } else if err == nil { |
| 158 | + t.Fatal("expected error") |
| 159 | + } |
| 160 | + if scenario == "oversized" { |
| 161 | + want = 1 |
| 162 | + } |
| 163 | + if count.Load() != want || redirected.Load() != 0 { |
| 164 | + t.Fatalf("requests=%d redirected=%d", count.Load(), redirected.Load()) |
| 165 | + } |
| 166 | + }) |
| 167 | + } |
| 168 | +} |
| 169 | + |
| 170 | +func TestHTTPKeyFetcherRetryTLSVerificationFailsClosed(t *testing.T) { |
| 171 | + var count atomic.Int32 |
| 172 | + srv := httptest.NewTLSServer(http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) { count.Add(1) })) |
| 173 | + defer srv.Close() |
| 174 | + transport := newLocalTLSTransport(srv) |
| 175 | + transport.TLSClientConfig.InsecureSkipVerify = false |
| 176 | + var handshakes atomic.Int32 |
| 177 | + ctx := httptrace.WithClientTrace(context.Background(), &httptrace.ClientTrace{TLSHandshakeStart: func() { handshakes.Add(1) }}) |
| 178 | + _, err := (&DefaultHTTPKeyFetcher{client: newHTTPKeyFetcherClient(transport)}).FetchKey(ctx, internalTestDomain) |
| 179 | + if err == nil || count.Load() != 0 || handshakes.Load() != 1 { |
| 180 | + t.Fatalf("err=%v HTTP requests=%d TLS handshakes=%d", err, count.Load(), handshakes.Load()) |
| 181 | + } |
| 182 | + t.Logf("TLS rejection: %v", err) |
| 183 | +} |
| 184 | + |
| 185 | +func TestHTTPKeyFetcherRetryProductionTimeBudget(t *testing.T) { |
| 186 | + var count atomic.Int32 |
| 187 | + srv := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 188 | + count.Add(1) |
| 189 | + w.WriteHeader(http.StatusOK) |
| 190 | + w.(http.Flusher).Flush() |
| 191 | + <-r.Context().Done() |
| 192 | + })) |
| 193 | + defer srv.Close() |
| 194 | + started := time.Now() |
| 195 | + _, err := newLocalTLSFetcher(srv).FetchKey(context.Background(), internalTestDomain) |
| 196 | + elapsed := time.Since(started) |
| 197 | + if !errors.Is(err, context.DeadlineExceeded) || count.Load() != 2 || elapsed < 14*time.Second || elapsed > 18*time.Second { |
| 198 | + t.Fatalf("err=%v requests=%d elapsed=%s", err, count.Load(), elapsed) |
| 199 | + } |
| 200 | + t.Logf("requests=%d elapsed=%s error=%v", count.Load(), elapsed, err) |
| 201 | +} |
0 commit comments