|
1 | 1 | package m2m |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
| 5 | + "io" |
| 6 | + "net" |
| 7 | + "net/http" |
| 8 | + "net/http/httptest" |
| 9 | + "sync/atomic" |
4 | 10 | "testing" |
| 11 | + "time" |
5 | 12 |
|
| 13 | + "github.com/databricks/databricks-sql-go/auth" |
6 | 14 | "github.com/stretchr/testify/assert" |
| 15 | + "github.com/stretchr/testify/require" |
| 16 | + "golang.org/x/oauth2" |
| 17 | + "golang.org/x/oauth2/clientcredentials" |
7 | 18 | ) |
8 | 19 |
|
9 | 20 | func TestM2MScopes(t *testing.T) { |
@@ -38,3 +49,154 @@ func TestM2MScopes(t *testing.T) { |
38 | 49 | assert.Equal(t, []string{"all-apis", "my-scope"}, auth.scopes) |
39 | 50 | }) |
40 | 51 | } |
| 52 | + |
| 53 | +// tokenServer answers the first `answered` requests with a short-lived token and |
| 54 | +// holds every later request open until the test ends. |
| 55 | +func tokenServer(t *testing.T, answered int32) (*httptest.Server, *atomic.Int32) { |
| 56 | + t.Helper() |
| 57 | + var hits atomic.Int32 |
| 58 | + release := make(chan struct{}) |
| 59 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 60 | + if hits.Add(1) <= answered { |
| 61 | + w.Header().Set("Content-Type", "application/json") |
| 62 | + _, _ = io.WriteString(w, `{"access_token":"tok","token_type":"Bearer","expires_in":1}`) |
| 63 | + return |
| 64 | + } |
| 65 | + select { |
| 66 | + case <-release: |
| 67 | + case <-r.Context().Done(): |
| 68 | + } |
| 69 | + })) |
| 70 | + t.Cleanup(func() { |
| 71 | + close(release) |
| 72 | + srv.Close() |
| 73 | + }) |
| 74 | + return srv, &hits |
| 75 | +} |
| 76 | + |
| 77 | +// useTokenEndpoint skips endpoint discovery, sends token requests to tokenURL, |
| 78 | +// and shortens the request bound for the test. |
| 79 | +func useTokenEndpoint(t *testing.T, tokenURL string, bound time.Duration) { |
| 80 | + t.Helper() |
| 81 | + oldConfig, oldBound := getConfig, tokenRequestTimeout |
| 82 | + getConfig = func(_ context.Context, _, clientID, clientSecret string, scopes []string) (clientcredentials.Config, error) { |
| 83 | + return clientcredentials.Config{ClientID: clientID, ClientSecret: clientSecret, TokenURL: tokenURL, Scopes: scopes}, nil |
| 84 | + } |
| 85 | + tokenRequestTimeout = bound |
| 86 | + t.Cleanup(func() { getConfig, tokenRequestTimeout = oldConfig, oldBound }) |
| 87 | +} |
| 88 | + |
| 89 | +func newRequest(t *testing.T) *http.Request { |
| 90 | + t.Helper() |
| 91 | + req, err := http.NewRequest(http.MethodPost, "https://ws.cloud.databricks.com/sql/1.0/warehouses/x", nil) |
| 92 | + require.NoError(t, err) |
| 93 | + return req |
| 94 | +} |
| 95 | + |
| 96 | +// authenticate fails the test if Authenticate has not returned within limit. |
| 97 | +func authenticate(t *testing.T, authr auth.Authenticator, limit time.Duration) (*http.Request, error) { |
| 98 | + t.Helper() |
| 99 | + req := newRequest(t) |
| 100 | + done := make(chan error, 1) |
| 101 | + go func() { done <- authr.Authenticate(req) }() |
| 102 | + select { |
| 103 | + case err := <-done: |
| 104 | + return req, err |
| 105 | + case <-time.After(limit): |
| 106 | + t.Fatalf("Authenticate did not return within %v", limit) |
| 107 | + return nil, nil |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +func requireTimeout(t *testing.T, err error) { |
| 112 | + t.Helper() |
| 113 | + var netErr net.Error |
| 114 | + require.ErrorAs(t, err, &netErr) |
| 115 | + require.True(t, netErr.Timeout(), "want a timeout error, got %v", err) |
| 116 | +} |
| 117 | + |
| 118 | +func TestAuthenticateBoundsHungTokenEndpoint(t *testing.T) { |
| 119 | + const bound = 200 * time.Millisecond |
| 120 | + srv, hits := tokenServer(t, 0) |
| 121 | + useTokenEndpoint(t, srv.URL+"/oidc/v1/token", bound) |
| 122 | + authr := NewAuthenticator("id", "secret", "ws.cloud.databricks.com") |
| 123 | + |
| 124 | + start := time.Now() |
| 125 | + _, err := authenticate(t, authr, 10*time.Second) |
| 126 | + elapsed := time.Since(start) |
| 127 | + |
| 128 | + requireTimeout(t, err) |
| 129 | + // oauth2's AuthStyle auto-detect retries a failed first request once. |
| 130 | + assert.LessOrEqual(t, hits.Load(), int32(2)) |
| 131 | + assert.GreaterOrEqual(t, elapsed, bound) |
| 132 | + assert.Less(t, elapsed, 2*bound+time.Second) |
| 133 | +} |
| 134 | + |
| 135 | +func TestAuthenticateBoundsConcurrentCallers(t *testing.T) { |
| 136 | + const bound = 200 * time.Millisecond |
| 137 | + srv, _ := tokenServer(t, 0) |
| 138 | + useTokenEndpoint(t, srv.URL+"/oidc/v1/token", bound) |
| 139 | + authr := NewAuthenticator("id", "secret", "ws.cloud.databricks.com") |
| 140 | + |
| 141 | + start := time.Now() |
| 142 | + errs := make(chan error, 2) |
| 143 | + for range 2 { |
| 144 | + req := newRequest(t) |
| 145 | + go func() { errs <- authr.Authenticate(req) }() |
| 146 | + } |
| 147 | + for range 2 { |
| 148 | + select { |
| 149 | + case err := <-errs: |
| 150 | + requireTimeout(t, err) |
| 151 | + case <-time.After(10 * time.Second): |
| 152 | + t.Fatal("Authenticate did not return within 10s") |
| 153 | + } |
| 154 | + } |
| 155 | + // The second caller waits for the first fetch, then makes its own. |
| 156 | + assert.Less(t, time.Since(start), 4*bound+2*time.Second) |
| 157 | +} |
| 158 | + |
| 159 | +func TestAuthenticateBoundsTokenRefresh(t *testing.T) { |
| 160 | + const bound = 200 * time.Millisecond |
| 161 | + srv, hits := tokenServer(t, 1) |
| 162 | + useTokenEndpoint(t, srv.URL+"/oidc/v1/token", bound) |
| 163 | + authr := NewAuthenticator("id", "secret", "ws.cloud.databricks.com") |
| 164 | + |
| 165 | + req, err := authenticate(t, authr, 10*time.Second) |
| 166 | + require.NoError(t, err) |
| 167 | + assert.Equal(t, "Bearer tok", req.Header.Get("Authorization")) |
| 168 | + |
| 169 | + // The token expires inside oauth2's 10s early-expiry window, so this refreshes. |
| 170 | + start := time.Now() |
| 171 | + _, err = authenticate(t, authr, 10*time.Second) |
| 172 | + requireTimeout(t, err) |
| 173 | + assert.Less(t, time.Since(start), bound+time.Second) |
| 174 | + assert.Equal(t, int32(2), hits.Load(), "a refresh reuses the detected auth style") |
| 175 | +} |
| 176 | + |
| 177 | +func TestWithTokenRequestTimeout(t *testing.T) { |
| 178 | + clientOn := func(ctx context.Context) *http.Client { |
| 179 | + return ctx.Value(oauth2.HTTPClient).(*http.Client) |
| 180 | + } |
| 181 | + |
| 182 | + t.Run("default client gets the bound", func(t *testing.T) { |
| 183 | + c := clientOn(withTokenRequestTimeout(context.Background())) |
| 184 | + assert.Equal(t, tokenRequestTimeout, c.Timeout) |
| 185 | + assert.Nil(t, c.Transport) |
| 186 | + assert.Zero(t, http.DefaultClient.Timeout, "http.DefaultClient must not be mutated") |
| 187 | + }) |
| 188 | + |
| 189 | + t.Run("supplied client keeps its transport and is capped", func(t *testing.T) { |
| 190 | + base := &http.Client{Transport: &http.Transport{}, Timeout: 15 * time.Minute} |
| 191 | + c := clientOn(withTokenRequestTimeout(context.WithValue(context.Background(), oauth2.HTTPClient, base))) |
| 192 | + assert.Equal(t, tokenRequestTimeout, c.Timeout) |
| 193 | + assert.Same(t, base.Transport, c.Transport) |
| 194 | + assert.Equal(t, 15*time.Minute, base.Timeout, "supplied client must not be mutated") |
| 195 | + }) |
| 196 | + |
| 197 | + t.Run("shorter supplied timeout is kept", func(t *testing.T) { |
| 198 | + base := &http.Client{Timeout: 5 * time.Second} |
| 199 | + c := clientOn(withTokenRequestTimeout(context.WithValue(context.Background(), oauth2.HTTPClient, base))) |
| 200 | + assert.Equal(t, 5*time.Second, c.Timeout) |
| 201 | + }) |
| 202 | +} |
0 commit comments