Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unreleased (v2.0.0 prototype)

- Fix OAuth M2M token requests hanging indefinitely when the token endpoint stops responding. Each request now uses a copy of `http.DefaultClient` with a timeout of at most 60s; `WithRetries` repeats failed fetches (databricks/databricks-sql-go#475).
- **Behavior change:** Connector M2M discovery, grants and refreshes send the connector User-Agent over a driver-owned transport (`HTTPS_PROXY`/`NO_PROXY`, system roots including `SSL_CERT_FILE`, at most 60s per request); changes to `http.DefaultTransport`/`http.DefaultClient` no longer reach them (databricks/databricks-sql-go#413).
- Render non-finite FLOAT / DOUBLE values inside ARRAY / MAP / STRUCT results as the JSON strings `"NaN"`, `"Infinity"`, and `"-Infinity"` on both backends instead of failing row iteration. SQL NULL remains `null`; top-level floats remain numeric (databricks/databricks-sql-go#573).
- **Behavior change:** Non-finite floating-point MAP keys use the same spellings as values on both backends. On the kernel backend, keys previously rendered as `"+Inf"` and `"-Inf"` now render as `"Infinity"` and `"-Infinity"`; `"NaN"` is unchanged. On Thrift, these keys previously failed row iteration (databricks/databricks-sql-go#573).
- Fix the module-root package conflict when building, linting, or running Gazelle with the `tools` build tag (databricks/databricks-sql-go#162).
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -350,8 +350,8 @@ token:<pat>@<host>:443[path]?useCloudFetch=false

| Connector option | Protocol | Description |
|---|---|---|
| `WithSkipTLSHostVerify()` | Both | Disable TLS chain + hostname verification. **Use only for internal private-link hostnames** — this is susceptible to machine-in-the-middle attacks. |
| `WithTransport(http.RoundTripper)` | Thrift only | Supply a custom HTTP transport (e.g. a custom CA, mTLS, or proxy). **Rejected** on the kernel path (wraps `ErrNotSupportedByKernel`) — the kernel uses its own HTTP stack; use `WithKernelTrustedCerts` / `WithKernelProxy` there. |
| `WithSkipTLSHostVerify()` | Both | Thrift: disable SQL TLS chain + hostname verification, **not OAuth**. Kernel (including Reyden fallback): disable both checks for all kernel traffic, **including M2M**. Susceptible to machine-in-the-middle attacks; use only for internal private-link hostnames. |
| `WithTransport(http.RoundTripper)` | Thrift only | Supply a custom transport for SQL and CloudFetch traffic. OAuth uses the driver's own verified transport with `HTTPS_PROXY`/`NO_PROXY` and system roots (including `SSL_CERT_FILE`). **Rejected** on the kernel path (wraps `ErrNotSupportedByKernel`) — use `WithKernelTrustedCerts` / `WithKernelProxy` there. |
| `WithKernelTrustedCerts(pem)` | SEA only | Add a PEM CA bundle on top of the system roots (for a re-signing proxy / on-prem CA). Needed because the kernel's TLS stack does not read `SSL_CERT_FILE`. |
| `WithKernelClientCertificate(certPEM, keyPEM)` | SEA only | Configure a paired mTLS client certificate and unencrypted private key. Both must be non-empty; PKCS#8 keys are recommended. |
| `WithKernelSkipHostnameVerify()` | SEA only | Skip **only** the hostname check while keeping chain validation (finer-grained than `WithSkipTLSHostVerify`). |
Expand Down
50 changes: 33 additions & 17 deletions auth/oauth/m2m/m2m.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func NewAuthenticatorWithScopes(clientID, clientSecret, hostName string, scopes
clientSecret: clientSecret,
hostName: hostName,
scopes: scopes,
cache: &tokenCache{},
}
}

Expand All @@ -35,8 +36,20 @@ type authClient struct {
clientSecret string
hostName string
scopes []string
tokenSource oauth2.TokenSource
mx sync.Mutex
httpClient *http.Client
cache *tokenCache
}

type tokenCache struct {
mutex sync.Mutex
token *oauth2.Token
config *clientcredentials.Config
}

func (c *authClient) WithHTTPClient(client *http.Client) auth.Authenticator {
configured := *c
configured.httpClient = client
return &configured
}

// M2MCredentials exposes the raw client-credentials so the SEA-via-kernel backend
Expand All @@ -56,31 +69,33 @@ func (c *authClient) M2MScopes() []string {
// Auth will start the OAuth Authorization Flow to authenticate the cli client
// using the users credentials in the browser. Compatible with SSO.
func (c *authClient) Authenticate(r *http.Request) error {
c.mx.Lock()
defer c.mx.Unlock()
if c.tokenSource != nil {
token, err := c.tokenSource.Token()
if err != nil {
return err
}
token.SetAuthHeader(r)
c.cache.mutex.Lock()
defer c.cache.mutex.Unlock()
if c.cache.token.Valid() {
c.cache.token.SetAuthHeader(r)
return nil
}

ctx := withTokenRequestTimeout(context.Background())
config, err := getConfig(ctx, c.hostName, c.clientID, c.clientSecret, c.scopes)
if err != nil {
return fmt.Errorf("unable to generate clientCredentials.Config: %w", err)
ctx := context.Background()
if c.httpClient != nil {
ctx = context.WithValue(ctx, oauth2.HTTPClient, c.httpClient)
}
ctx = withTokenRequestTimeout(ctx)
if c.cache.config == nil {
config, err := getConfig(ctx, c.hostName, c.clientID, c.clientSecret, c.scopes)
if err != nil {
return fmt.Errorf("unable to generate clientCredentials.Config: %w", err)
}
c.cache.config = &config
}

// The token source keeps ctx, so refreshes are bounded too.
c.tokenSource = config.TokenSource(ctx)
token, err := c.tokenSource.Token()
token, err := c.cache.config.Token(ctx)
if err != nil {
logger.Err(err).Msg("failed to get token")

return err
}
c.cache.token = token
// Log via the driver's configurable logger (defaults to Warn) rather than the
// global zerolog logger, so this line honors SetLogLevel like every other.
logger.Debug().Msgf("databricks OAuth token fetched successfully")
Expand Down Expand Up @@ -119,6 +134,7 @@ func GetTokenSource(config clientcredentials.Config) oauth2.TokenSource {
return tokenSource
}

// GetConfig uses the oauth2.HTTPClient from ctx for discovery when set.
func GetConfig(ctx context.Context, issuerURL, clientID, clientSecret string, scopes []string) (clientcredentials.Config, error) {
// Get the endpoint based on the host name
endpoint, err := oauth.GetEndpoint(ctx, issuerURL)
Expand Down
14 changes: 9 additions & 5 deletions auth/oauth/oauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ var azureTenants = map[string]string{
".databricks.azure.cn": "2ff814a6-3304-4ab8-85cb-cd0e6f879c1d",
}

// GetEndpoint uses the oauth2.HTTPClient from ctx for discovery when set.
func GetEndpoint(ctx context.Context, hostName string) (oauth2.Endpoint, error) {
if ctx == nil {
ctx = context.Background()
Expand All @@ -54,12 +55,15 @@ func GetEndpoint(ctx context.Context, hostName string) (oauth2.Endpoint, error)
// use their account-rooted endpoint instead of the account-agnostic console login.
// For normal workspace hosts this resolves to https://<host>/oidc, identical to
// the previous behavior.
//
// NOTE: this client uses the default transport, matching the existing
// oidc.NewProvider discovery below. A connector-supplied transport / TLS config
// (WithTransport, WithSkipTLSHostVerify) is not yet threaded into the OAuth
// endpoint-resolution path; that is a pre-existing limitation, tracked separately.
// NOTE: M2M connectors supply the driver's verified transport, not the
// caller's SQL transport or WithSkipTLSHostVerify settings.
client := &http.Client{Timeout: hostConfigTimeout}
if supplied, ok := ctx.Value(oauth2.HTTPClient).(*http.Client); ok && supplied != nil {
*client = *supplied
if client.Timeout <= 0 || client.Timeout > hostConfigTimeout {
client.Timeout = hostConfigTimeout
}
}
issuerURL := resolveOIDCIssuer(ctx, client, hostName)
ctx = oidc.ClientContext(ctx, client)
ctx = oidc.InsecureIssuerURLContext(ctx, issuerURL)
Expand Down
9 changes: 7 additions & 2 deletions connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,10 @@ func WithQueryTags(tags map[string]string) ConnOption {
}
}

// WithSkipTLSHostVerify disables the verification of the hostname in the TLS certificate.
// WithSkipTLSHostVerify disables certificate chain and hostname verification for SQL traffic.
// On Thrift, it does not relax OAuth discovery or the client-secret exchange.
// On kernel (including Reyden fallback), it also disables both checks for kernel M2M.
// WithTransport callers control their own TLS verification.
// WARNING:
// When this option is used, TLS is susceptible to machine-in-the-middle attacks.
// Please only use this option when the hostname is an internal private link hostname
Expand All @@ -527,7 +530,9 @@ func WithAuthenticator(authr auth.Authenticator) ConnOption {
}
}

// WithTransport sets up the transport configuration to be used by the httpclient.
// WithTransport supplies the transport for SQL and CloudFetch traffic.
// OAuth uses the driver's verified transport with HTTPS_PROXY/NO_PROXY and
// system roots, including SSL_CERT_FILE, independently of this option.
func WithTransport(t http.RoundTripper) ConnOption {
return func(c *config.Config) {
c.Transport = t
Expand Down
50 changes: 50 additions & 0 deletions connector_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package dbsql

import (
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"time"

"github.com/databricks/databricks-sql-go/auth"
"github.com/databricks/databricks-sql-go/auth/pat"
"github.com/databricks/databricks-sql-go/auth/tokenprovider"
dbsqlerr "github.com/databricks/databricks-sql-go/errors"
Expand Down Expand Up @@ -57,6 +60,53 @@ func TestFederatedTokenAuthenticatorPreservesThriftTokenExchange(t *testing.T) {
assert.Equal(t, subjectToken, exchange.subjectToken)
}

type connectorOAuthRecorder struct {
auth.Authenticator
client *http.Client
}

func (recorder *connectorOAuthRecorder) WithHTTPClient(client *http.Client) auth.Authenticator {
recorder.client = client
return recorder.Authenticator.(interface {
WithHTTPClient(*http.Client) auth.Authenticator
}).WithHTTPClient(client)
}

func TestM2MConnectorHTTPConfiguration(t *testing.T) {
agents := make(chan string, 1)
server := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
agents <- request.UserAgent()
writer.WriteHeader(http.StatusOK)
}))
t.Cleanup(server.Close)
for _, dsn := range []bool{false, true} {
t.Run(fmt.Sprintf("dsn=%t", dsn), func(t *testing.T) {
caller := &http.Transport{Proxy: func(*http.Request) (*url.URL, error) {
return nil, fmt.Errorf("OAuth must not use the caller transport")
}}
t.Cleanup(caller.CloseIdleConnections)
options := []ConnOption{WithServerHostname("test.cloud.databricks.com"), WithHTTPPath("/query"),
WithClientCredentials("id", "secret"), WithUserAgentEntry("Issue413/partner")}
if dsn {
cfg, err := config.ParseDSN("https://test.cloud.databricks.com:443/query?authType=OauthM2M&clientID=id&clientSecret=secret&userAgentEntry=Issue413%2Fpartner")
require.NoError(t, err)
options = []ConnOption{withUserConfig(cfg)}
}
recorder := &connectorOAuthRecorder{}
options = append(options, WithTransport(caller), func(cfg *config.Config) {
recorder.Authenticator, cfg.Authenticator = cfg.Authenticator, recorder
})
_, err := NewConnector(options...)
require.NoError(t, err)
require.NotNil(t, recorder.client)
response, err := recorder.client.Get(server.URL)
require.NoError(t, err, "bound OAuth base must bypass the caller transport")
require.NoError(t, response.Body.Close())
assert.Contains(t, <-agents, "Issue413/partner")
})
}
}

func TestWithClientQueryTimeout(t *testing.T) {
t.Run("omitted remains distinguishable from explicit zero", func(t *testing.T) {
con, err := NewConnector(WithUseKernel(true))
Expand Down
38 changes: 27 additions & 11 deletions internal/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"regexp"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"

Expand Down Expand Up @@ -639,30 +640,45 @@ func PooledTransport(cfg *config.Config) *http.Transport {
return transport
}

var oauthTransport = sync.OnceValue(func() *http.Transport { return PooledTransport(&config.Config{}) })

func PooledClient(cfg *config.Config) *http.Client {
if cfg.Authenticator == nil {
return nil
}

var tr *Transport
if cfg.Transport != nil {
tr = &Transport{
Base: cfg.Transport,
Authr: cfg.Authenticator,
}
} else {
tr = &Transport{
Base: PooledTransport(cfg),
Authr: cfg.Authenticator,
base := cfg.Transport
if base == nil {
base = PooledTransport(cfg)
}
authenticator := cfg.Authenticator
if configurable, ok := authenticator.(interface {
WithHTTPClient(*http.Client) auth.Authenticator
}); ok {
httpClient := &http.Client{
Transport: &userAgentTransport{base: oauthTransport(), userAgent: BuildUserAgent(cfg)},
Timeout: cfg.ClientTimeout,
}
authenticator = configurable.WithHTTPClient(httpClient)
}

return &http.Client{
Transport: tr,
Transport: &Transport{Base: base, Authr: authenticator},
Timeout: cfg.ClientTimeout,
}
}

type userAgentTransport struct {
base http.RoundTripper
userAgent string
}

func (transport *userAgentTransport) RoundTrip(request *http.Request) (*http.Response, error) {
cloned := cloneRequest(request)
cloned.Header.Set("User-Agent", transport.userAgent)
return transport.base.RoundTrip(cloned)
}

// cloneRequest returns a clone of the provided *http.Request.
// The clone is a shallow copy of the struct and its Header map.
func cloneRequest(r *http.Request) *http.Request {
Expand Down
Loading
Loading